|
| 1 | +from pathlib import Path |
| 2 | + |
| 3 | +import nibabel as nb |
| 4 | +import numpy as np |
| 5 | +import pytest |
| 6 | + |
| 7 | +from niworkflows.utils.spaces import SpatialReferences, Reference |
| 8 | +from niworkflows.utils.testing import generate_bids_skeleton |
| 9 | + |
| 10 | +from ..anatomical import init_anat_preproc_wf, init_anat_fit_wf |
| 11 | + |
| 12 | +BASE_LAYOUT = { |
| 13 | + "01": { |
| 14 | + "anat": [{"suffix": "T1w"}, {"suffix": "T2w"}], |
| 15 | + "func": [ |
| 16 | + { |
| 17 | + "task": "rest", |
| 18 | + "run": i, |
| 19 | + "suffix": "bold", |
| 20 | + "metadata": {"PhaseEncodingDirection": "j", "TotalReadoutTime": 0.6}, |
| 21 | + } |
| 22 | + for i in range(1, 3) |
| 23 | + ], |
| 24 | + "fmap": [ |
| 25 | + {"suffix": "phasediff", "metadata": {"EchoTime1": 0.005, "EchoTime2": 0.007}}, |
| 26 | + {"suffix": "magnitude1", "metadata": {"EchoTime": 0.005}}, |
| 27 | + { |
| 28 | + "suffix": "epi", |
| 29 | + "direction": "PA", |
| 30 | + "metadata": {"PhaseEncodingDirection": "j", "TotalReadoutTime": 0.6}, |
| 31 | + }, |
| 32 | + { |
| 33 | + "suffix": "epi", |
| 34 | + "direction": "AP", |
| 35 | + "metadata": {"PhaseEncodingDirection": "j-", "TotalReadoutTime": 0.6}, |
| 36 | + }, |
| 37 | + ], |
| 38 | + }, |
| 39 | +} |
| 40 | + |
| 41 | + |
| 42 | +@pytest.fixture(scope="module") |
| 43 | +def bids_root(tmp_path_factory): |
| 44 | + base = tmp_path_factory.mktemp("base") |
| 45 | + bids_dir = base / "bids" |
| 46 | + generate_bids_skeleton(bids_dir, BASE_LAYOUT) |
| 47 | + yield bids_dir |
| 48 | + |
| 49 | + |
| 50 | +@pytest.mark.parametrize("freesurfer", [True, False]) |
| 51 | +@pytest.mark.parametrize("cifti_output", [False, "91k"]) |
| 52 | +def test_init_anat_preproc_wf( |
| 53 | + bids_root: Path, |
| 54 | + tmp_path: Path, |
| 55 | + freesurfer: bool, |
| 56 | + cifti_output: bool, |
| 57 | +): |
| 58 | + output_dir = tmp_path / 'output' |
| 59 | + output_dir.mkdir() |
| 60 | + |
| 61 | + init_anat_preproc_wf( |
| 62 | + bids_root=str(bids_root), |
| 63 | + output_dir=str(output_dir), |
| 64 | + freesurfer=freesurfer, |
| 65 | + hires=False, |
| 66 | + longitudinal=False, |
| 67 | + msm_sulc=False, |
| 68 | + t1w=[str(bids_root / "sub-01" / "anat" / "sub-01_T1w.nii.gz")], |
| 69 | + t2w=[str(bids_root / "sub-01" / "anat" / "sub-01_T2w.nii.gz")], |
| 70 | + skull_strip_mode=False, |
| 71 | + skull_strip_template=Reference("OASIS30ANTs"), |
| 72 | + spaces=SpatialReferences( |
| 73 | + spaces=["MNI152NLin2009cAsym", "fsaverage5"], |
| 74 | + checkpoint=True, |
| 75 | + ), |
| 76 | + precomputed={}, |
| 77 | + omp_nthreads=1, |
| 78 | + cifti_output=cifti_output, |
| 79 | + ) |
| 80 | + |
| 81 | + |
| 82 | +@pytest.mark.parametrize("msm_sulc", [True, False]) |
| 83 | +@pytest.mark.parametrize("skull_strip_mode", ['skip', 'force']) |
| 84 | +def test_anat_fit_wf( |
| 85 | + bids_root: Path, |
| 86 | + tmp_path: Path, |
| 87 | + msm_sulc: bool, |
| 88 | + skull_strip_mode: str, |
| 89 | +): |
| 90 | + output_dir = tmp_path / 'output' |
| 91 | + output_dir.mkdir() |
| 92 | + |
| 93 | + init_anat_fit_wf( |
| 94 | + bids_root=str(bids_root), |
| 95 | + output_dir=str(output_dir), |
| 96 | + freesurfer=True, |
| 97 | + hires=False, |
| 98 | + longitudinal=False, |
| 99 | + msm_sulc=msm_sulc, |
| 100 | + t1w=[str(bids_root / "sub-01" / "anat" / "sub-01_T1w.nii.gz")], |
| 101 | + t2w=[str(bids_root / "sub-01" / "anat" / "sub-01_T2w.nii.gz")], |
| 102 | + skull_strip_mode=skull_strip_mode, |
| 103 | + skull_strip_template=Reference("OASIS30ANTs"), |
| 104 | + spaces=SpatialReferences( |
| 105 | + spaces=["MNI152NLin2009cAsym", "fsaverage5"], |
| 106 | + checkpoint=True, |
| 107 | + ), |
| 108 | + precomputed={}, |
| 109 | + omp_nthreads=1, |
| 110 | + ) |
| 111 | + |
| 112 | + |
| 113 | +@pytest.mark.parametrize("t1w_preproc", [False, True]) |
| 114 | +@pytest.mark.parametrize("t2w_preproc", [False, True]) |
| 115 | +@pytest.mark.parametrize("t1w_mask", [False, True]) |
| 116 | +@pytest.mark.parametrize("t1w_dseg", [False, True]) |
| 117 | +@pytest.mark.parametrize("t1w_tpms", [False, True]) |
| 118 | +def test_anat_fit_precomputes( |
| 119 | + bids_root: Path, |
| 120 | + tmp_path: Path, |
| 121 | + t1w_preproc: bool, |
| 122 | + t2w_preproc: bool, |
| 123 | + t1w_mask: bool, |
| 124 | + t1w_dseg: bool, |
| 125 | + t1w_tpms: bool, |
| 126 | +): |
| 127 | + output_dir = tmp_path / 'output' |
| 128 | + output_dir.mkdir() |
| 129 | + |
| 130 | + empty_img = nb.Nifti1Image(np.zeros((1, 1, 1)), np.eye(4)) |
| 131 | + precomputed = {} |
| 132 | + if t1w_preproc: |
| 133 | + precomputed["t1w_preproc"] = str(tmp_path / "t1w_preproc.nii.gz") |
| 134 | + if t2w_preproc: |
| 135 | + precomputed["t2w_preproc"] = str(tmp_path / "t2w_preproc.nii.gz") |
| 136 | + if t1w_mask: |
| 137 | + precomputed["t1w_mask"] = str(tmp_path / "t1w_mask.nii.gz") |
| 138 | + if t1w_dseg: |
| 139 | + precomputed["t1w_dseg"] = str(tmp_path / "t1w_dseg.nii.gz") |
| 140 | + if t1w_tpms: |
| 141 | + precomputed["t1w_tpms"] = str(tmp_path / "t1w_tpms.nii.gz") |
| 142 | + |
| 143 | + for path in precomputed.values(): |
| 144 | + empty_img.to_filename(path) |
| 145 | + |
| 146 | + init_anat_fit_wf( |
| 147 | + bids_root=str(bids_root), |
| 148 | + output_dir=str(output_dir), |
| 149 | + freesurfer=False, |
| 150 | + hires=False, |
| 151 | + longitudinal=False, |
| 152 | + msm_sulc=True, |
| 153 | + t1w=[str(bids_root / "sub-01" / "anat" / "sub-01_T1w.nii.gz")], |
| 154 | + t2w=[str(bids_root / "sub-01" / "anat" / "sub-01_T2w.nii.gz")], |
| 155 | + skull_strip_mode=False, |
| 156 | + skull_strip_template=Reference("OASIS30ANTs"), |
| 157 | + spaces=SpatialReferences( |
| 158 | + spaces=["MNI152NLin2009cAsym", "fsaverage5"], |
| 159 | + checkpoint=True, |
| 160 | + ), |
| 161 | + precomputed=precomputed, |
| 162 | + omp_nthreads=1, |
| 163 | + ) |
0 commit comments