Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fetch all descriptions: id list #283

Merged
merged 2 commits into from
Dec 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dcm2bids/acquisition.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ def dstSidecarData(self, idList):
values.append(idList.get(val, val))
if values[-1] != val:
if isinstance(values[-1], list):
values[-1] = "bids::" + values[-1][0]
values[-1] = ["bids::" + img_dest for img_dest in values[-1]]
else:
values[-1] = "bids::" + values[-1]

Expand Down
33 changes: 33 additions & 0 deletions tests/data/config_test_multiple_intendedfor.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"search_method": "fnmatch",
"extractors": {"SeriesDescription": ["task-(?P<task>[a-zA-Z0-9]+)"]},
"descriptions": [
{
"id": "localizer",
"datatype": "localizer",
"suffix": "localizer",
"criteria": {
"SeriesDescription": "locali*"
}
},
{
"id": "T1",
"datatype": "anat",
"suffix": "T1w",
"criteria": {
"SidecarFilename": "*MPRAGE*"
}
},
{
"datatype": "fmap",
"suffix": "fmap",
"criteria": {
"EchoNumber": 1,
"EchoTime": 0.00492
},
"sidecar_changes": {
"IntendedFor": ["localizer", "T1"]
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DUMMY_FILE_FOR_TEST
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DUMMY_FILE_FOR_TEST
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DUMMY_FILE_FOR_TEST
91 changes: 75 additions & 16 deletions tests/test_dcm2bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ def test_dcm2bids():
"sub-01_echo-738_fmap.json")
data = load_json(fmapFile)
fmapMtime = os.stat(fmapFile).st_mtime
assert data["IntendedFor"] == "bids::" + os.path.join("sub-01", "dwi", "sub-01_dwi.nii.gz")
assert data["IntendedFor"] == "bids::" + os.path.join("sub-01",
"dwi",
"sub-01_dwi.nii.gz")

data = load_json(
os.path.join(
Expand Down Expand Up @@ -231,16 +233,22 @@ def test_dcm2bids_case_sensitive():
"fmap",
"sub-01_echo-492_fmap.json")
data = load_json(fmapFile)
assert data["IntendedFor"] == ["bids::" + os.path.join("sub-01", "dwi", "sub-01_dwi.nii.gz"),
"bids::" + os.path.join("sub-01", "anat", "sub-01_T1w.nii")]
assert data["IntendedFor"] == ["bids::" + os.path.join("sub-01",
"dwi",
"sub-01_dwi.nii.gz"),
"bids::" + os.path.join("sub-01",
"anat",
"sub-01_T1w.nii")]

fmapFile = os.path.join(bids_dir.name,
"sub-01",
"fmap",
"sub-01_echo-738_fmap.json")
data = load_json(fmapFile)
fmapMtime = os.stat(fmapFile).st_mtime
assert data["IntendedFor"] == "bids::" + os.path.join("sub-01", "dwi", "sub-01_dwi.nii.gz")
assert data["IntendedFor"] == "bids::" + os.path.join("sub-01",
"dwi",
"sub-01_dwi.nii.gz")

data = load_json(
os.path.join(
Expand Down Expand Up @@ -288,8 +296,12 @@ def test_dcm2bids_auto_extract():
data = load_json(epi_file)

assert os.path.exists(epi_file)
assert data["IntendedFor"] == ["bids::" + os.path.join("sub-01", "dwi", "sub-01_dwi.nii.gz"),
"bids::" + os.path.join("sub-01", "anat", "sub-01_T1w.nii")]
assert data["IntendedFor"] == ["bids::" + os.path.join("sub-01",
"dwi",
"sub-01_dwi.nii.gz"),
"bids::" + os.path.join("sub-01",
"anat",
"sub-01_T1w.nii")]

func_task = os.path.join(bids_dir.name, "sub-01",
"func",
Expand Down Expand Up @@ -459,8 +471,6 @@ def test_dcm2bids_sidecar():
session="dev")
app.run()

layout = BIDSLayout(bids_dir.name, validate=False)

# existing field
data = load_json(os.path.join(bids_dir.name,
"sub-01",
Expand All @@ -483,8 +493,8 @@ def test_dcm2bids_sidecar():
"ses-dev",
"fmap",
"sub-01_ses-dev_echo-492_fmap.json"))
assert data["MTState"] == True
assert data["MTState"]

# boolean value if input as a string
data = load_json(os.path.join(bids_dir.name,
"sub-01",
Expand All @@ -499,16 +509,65 @@ def test_dcm2bids_sidecar():
"ses-dev",
"dwi",
"sub-01_ses-dev_desc-fa01_dwi.json"))
assert data["IntendedFor"] == ["bids::" + os.path.join("sub-01", "ses-dev", "dwi", "sub-01_ses-dev_dwi.nii.gz"),
"bids::" + os.path.join("sub-01", "ses-dev", "anat", "sub-01_ses-dev_T1w.nii")]
assert data["Sources"] == ["bids::" + os.path.join("sub-01", "ses-dev", "anat", "sub-01_ses-dev_T1w.nii"),
"bids::" + os.path.join("sub-01", "ses-dev", "dwi", "sub-01_ses-dev_dwi.nii.gz")]
assert data["IntendedFor"] == ["bids::" + os.path.join("sub-01",
"ses-dev",
"dwi",
"sub-01_ses-dev_dwi.nii.gz"),
"bids::" + os.path.join("sub-01",
"ses-dev",
"anat",
"sub-01_ses-dev_T1w.nii")]
assert data["Sources"] == ["bids::" + os.path.join("sub-01",
"ses-dev",
"anat",
"sub-01_ses-dev_T1w.nii"),
"bids::" + os.path.join("sub-01",
"ses-dev",
"dwi",
"sub-01_ses-dev_dwi.nii.gz")]

# list with 1 item
data = load_json(os.path.join(bids_dir.name,
"sub-01",
"ses-dev",
"dwi",
"sub-01_ses-dev_desc-trace_dwi.json"))
assert data["IntendedFor"] == "bids::" + os.path.join("sub-01", "ses-dev", "dwi", "sub-01_ses-dev_dwi.nii.gz")
assert data["Sources"] == "bids::" + os.path.join("sub-01", "ses-dev", "anat", "sub-01_ses-dev_T1w.nii")
assert data["IntendedFor"] == "bids::" + os.path.join("sub-01",
"ses-dev",
"dwi",
"sub-01_ses-dev_dwi.nii.gz")
assert data["Sources"] == "bids::" + os.path.join("sub-01",
"ses-dev",
"anat",
"sub-01_ses-dev_T1w.nii")


def test_dcm2bids_multiple_intendedFor():
bids_dir = TemporaryDirectory()

tmp_sub_dir = os.path.join(bids_dir.name, DEFAULT.tmp_dir_name, "sub-01")
shutil.copytree(os.path.join(TEST_DATA_DIR, "sidecars"), tmp_sub_dir)

app = Dcm2BidsGen(TEST_DATA_DIR, "01",
os.path.join(TEST_DATA_DIR,
"config_test_multiple_intendedfor.json"),
bids_dir.name,
auto_extract_entities=True)
app.run()

epi_file = os.path.join(bids_dir.name, "sub-01", "fmap", "sub-01_fmap.json")
data = load_json(epi_file)

assert os.path.exists(epi_file)
assert data["IntendedFor"] == ["bids::" + os.path.join("sub-01",
"localizer",
"sub-01_run-01_localizer.nii"),
"bids::" + os.path.join("sub-01",
"localizer",
"sub-01_run-02_localizer.nii"),
"bids::" + os.path.join("sub-01",
"localizer",
"sub-01_run-03_localizer.nii"),
"bids::" + os.path.join("sub-01",
"anat",
"sub-01_T1w.nii")]