Skip to content

Commit

Permalink
Merge pull request #15 from Australian-Imaging-Service/simple-dir-data
Browse files Browse the repository at this point in the history
Simple dir data
  • Loading branch information
tclose authored Aug 15, 2023
2 parents 7dbd302 + 1e0363e commit 9fe48c4
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
2 changes: 1 addition & 1 deletion scripts/add_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
from xnat4tests.data import add_data

start_xnat()
add_data("user-training")
add_data("simple-dir")
26 changes: 26 additions & 0 deletions tests/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,29 @@ def test_add_nifti(config, launched_xnat):
assert sorted(s.type for s in xsess2.scans.values()) == [
"t1w",
]


def test_simple_dir(config, launched_xnat):

add_data("simple-dir", config_name=config)

with connect(config) as login:
xsess1 = (
login.projects["SIMPLE_DIR"]
.subjects["subject01"]
.experiments["subject01_1"]
)

assert sorted(s.type for s in xsess1.scans.values()) == [
"a-directory",
]

xsess2 = (
login.projects["SIMPLE_DIR"]
.subjects["subject02"]
.experiments["subject02_1"]
)

assert sorted(s.type for s in xsess2.scans.values()) == [
"a-directory",
]
16 changes: 11 additions & 5 deletions xnat4tests/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,36 +81,41 @@ def add_data(dataset: str, config_name: str or dict = "default"):
project_id="OPENNEURO_T1W",
subject_id="subject01",
session_id="subject01_MR01",
resource_name="NIFTI",
)
_upload_directly(
{"t1w": openneuro_t1w()},
config,
project_id="OPENNEURO_T1W",
subject_id="subject02",
session_id="subject02_MR01",
resource_name="NIFTI",
)

elif dataset == "simple-dir":

tmp_dir = Path(tempfile.mkdtemp())
a_dir = tmp_dir / "a-dir"
a_dir.mkdir()
for i in range(3):
a_file = a_dir / f"file{i + 1}.txt"
a_file.write_text("A dummy file - {i}\n")
a_file.write_text(f"A dummy file - {i + 1}\n")

_upload_directly(
{"a-directory": a_dir},
config,
project_id="SIMPLEDIR",
project_id="SIMPLE_DIR",
subject_id="subject01",
session_id="subject01_1",
resource_name="DIRECTORY",
)
_upload_directly(
{"a-directory": a_dir},
config,
project_id="SIMPLEDIR",
project_id="SIMPLE_DIR",
subject_id="subject02",
session_id="subject02_1",
resource_name="DIRECTORY",
)

elif dataset == "user-training":
Expand Down Expand Up @@ -186,7 +191,7 @@ def add_data(dataset: str, config_name: str or dict = "default"):


def _upload_dicom_data(
to_upload: Path or ty.List[Path] or str,
to_upload: ty.Union[Path, ty.List[Path], str],
config: dict,
project_id: str,
subject_id: str,
Expand Down Expand Up @@ -281,6 +286,7 @@ def _upload_directly(
project_id: str,
subject_id: str,
session_id: str,
resource_name: str,
):

with connect(config) as login:
Expand Down Expand Up @@ -325,6 +331,6 @@ def _upload_directly(
for i, (scan_type, fspath) in enumerate(to_upload.items(), start=1):
xdataset = login.classes.MrScanData(id=i, type=scan_type, parent=xsession)
resource = xdataset.create_resource(
"NIFTI"
resource_name
) # TODO get this resource name from somewhere else
resource.upload_dir(fspath.parent, method="tar_file")

0 comments on commit 9fe48c4

Please sign in to comment.