Skip to content

Commit

Permalink
Merge branch 'main' into test/hotfix-file-type
Browse files Browse the repository at this point in the history
  • Loading branch information
htwangtw authored May 31, 2024
2 parents 6af3eea + 8e8be89 commit 541e0fd
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 7 deletions.
9 changes: 8 additions & 1 deletion giga_connectome/mask.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,20 @@ def generate_gm_mask_atlas(
subject_mask_dir / target_subject_mask_file_name
)

if not target_subject_seg:
if not target_subject_seg or not target_subject_mask:
# resample if the grey matter mask was not generated
# or the atlas was not present
subject_seg_niis = resample_atlas_collection(
target_subject_seg_file_names,
atlas,
subject_mask_dir,
subject_mask_nii,
)
else:
subject_seg_niis = [
load_img(subject_mask_dir / i)
for i in target_subject_seg_file_names
]

return subject_mask_nii, subject_seg_niis

Expand Down
53 changes: 53 additions & 0 deletions giga_connectome/tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,14 @@ def test_smoke(tmp_path, caplog):
"simple",
"--reindex-bids",
"--calculate-intranetwork-average-correlation",
"--bids-filter-file",
str(Path(bids_dir).parent / "bids_filter.json"),
str(bids_dir),
str(output_dir),
"participant",
]
)
# check outputs
assert "has been deprecated" in caplog.text.splitlines()[0]

output_folder = output_dir / "sub-1" / "ses-timepoint1" / "func"
Expand Down Expand Up @@ -94,3 +97,53 @@ def test_smoke(tmp_path, caplog):
assert timeseries_file.exists()
timeseries = pd.read_csv(timeseries_file, sep="\t")
assert len(timeseries.columns) == 100

# immediately rerun should cover the case where the output already exists
main(
[
"--participant_label",
"1",
"-a",
str(atlases_dir),
"--atlas",
"Schaefer2018",
"--denoise-strategy",
"simple",
"--calculate-intranetwork-average-correlation",
"--bids-filter-file",
str(Path(bids_dir).parent / "bids_filter.json"),
str(bids_dir),
str(output_dir),
"participant",
]
)

# deleate gm mask to trigger rerun the atlas generation

gm_path = (
atlases_dir
/ "sub-1"
/ "func"
/ "sub-1_space-MNI152NLin2009cAsym_res-2_label-GM_mask.nii.gz"
)
# delete gm_path
gm_path.unlink()
# rerun
main(
[
"--participant_label",
"1",
"-a",
str(atlases_dir),
"--atlas",
"Schaefer2018",
"--denoise-strategy",
"simple",
"--calculate-intranetwork-average-correlation",
"--bids-filter-file",
str(Path(bids_dir).parent / "bids_filter.json"),
str(bids_dir),
str(output_dir),
"participant",
]
)
28 changes: 23 additions & 5 deletions giga_connectome/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,32 @@ def test_output_filename(suffix, extension, target):


@pytest.mark.parametrize(
"atlas,atlas_desc,suffix,target",
"source_file,atlas,atlas_desc,suffix,target",
[
("fake", "100", "dseg", "sub-01_seg-fake100_dseg.nii.gz"),
("", "", "mask", "sub-01_space-MNIfake_res-2_label-GM_mask.nii.gz"),
(
"sub-01_ses-ah_task-rest_run-1_space-MNIfake_res-2_desc-brain_mask.nii.gz",
"fake",
"100",
"dseg",
"sub-01_seg-fake100_dseg.nii.gz",
),
(
"sub-01_ses-ah_task-rest_run-1_space-MNIfake_res-2_desc-brain_mask.nii.gz",
"",
"",
"mask",
"sub-01_space-MNIfake_res-2_label-GM_mask.nii.gz",
),
(
"sub-01_ses-ah_task-rest_run-1_space-MNIfake_desc-brain_mask.nii.gz",
"",
"",
"mask",
"sub-01_space-MNIfake_label-GM_mask.nii.gz",
),
],
)
def test_output_filename_seg(atlas, atlas_desc, suffix, target):
source_file = "sub-01_ses-ah_task-rest_run-1_space-MNIfake_res-2_desc-brain_mask.nii.gz"
def test_output_filename_seg(source_file, atlas, atlas_desc, suffix, target):
generated_target = utils.output_filename(
source_file=source_file,
atlas=atlas,
Expand Down
3 changes: 2 additions & 1 deletion giga_connectome/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,8 @@ def output_filename(

elif suffix == "mask":
reference = parse_bids_filename(source_file)
tpl: str = f"space-{reference['space']}_res-{reference['res']}"
tpl: str = f"space-{reference['space']}"
tpl += f"_res-{reference['res']}" if "res" in reference else ""
return f"{subject}_{tpl}_label-GM_{suffix}.{extension}"
else:
return f"{subject}_{seg}_{suffix}.{extension}"
Expand Down

0 comments on commit 541e0fd

Please sign in to comment.