|
| 1 | +import json |
| 2 | + |
| 3 | +import nibabel as nb |
| 4 | +import numpy as np |
| 5 | +import pytest |
| 6 | +from niworkflows.utils.testing import generate_bids_skeleton |
| 7 | + |
| 8 | +from smriprep.cli.run import build_workflow, get_parser |
| 9 | + |
| 10 | +NO_SESSION_LAYOUT = { |
| 11 | + '01': [ |
| 12 | + { |
| 13 | + 'anat': [ |
| 14 | + {'run': 1, 'suffix': 'T1w'}, |
| 15 | + {'run': 2, 'acq': 'test', 'suffix': 'T1w'}, |
| 16 | + {'suffix': 'T2w'}, |
| 17 | + ], |
| 18 | + }, |
| 19 | + ], |
| 20 | +} |
| 21 | + |
| 22 | +SESSION_LAYOUT = { |
| 23 | + '01': [ |
| 24 | + { |
| 25 | + 'session': 'pre', |
| 26 | + 'anat': [ |
| 27 | + {'run': 1, 'suffix': 'T1w'}, |
| 28 | + {'run': 2, 'acq': 'test', 'suffix': 'T1w'}, |
| 29 | + {'suffix': 'T2w'}, |
| 30 | + ], |
| 31 | + }, |
| 32 | + { |
| 33 | + 'session': 'post', |
| 34 | + 'anat': [ |
| 35 | + {'run': 1, 'suffix': 'T1w'}, |
| 36 | + {'run': 2, 'acq': 'test', 'suffix': 'T1w'}, |
| 37 | + {'suffix': 'T2w'}, |
| 38 | + ], |
| 39 | + }, |
| 40 | + ], |
| 41 | +} |
| 42 | + |
| 43 | + |
| 44 | +@pytest.fixture(scope='module') |
| 45 | +def bids_no_session(tmp_path_factory): |
| 46 | + base = tmp_path_factory.mktemp('bids_dirs') |
| 47 | + bids_dir = base / 'no_session' |
| 48 | + generate_bids_skeleton(bids_dir, NO_SESSION_LAYOUT) |
| 49 | + |
| 50 | + img = nb.Nifti1Image(np.zeros((10, 10, 10, 10)), np.eye(4)) |
| 51 | + |
| 52 | + for path in bids_dir.glob('sub-01/**/*.nii.gz'): |
| 53 | + img.to_filename(path) |
| 54 | + return bids_dir |
| 55 | + |
| 56 | + |
| 57 | +@pytest.fixture(scope='module') |
| 58 | +def bids_session(tmp_path_factory): |
| 59 | + base = tmp_path_factory.mktemp('bids_dirs') |
| 60 | + bids_dir = base / 'with_session' |
| 61 | + generate_bids_skeleton(bids_dir, SESSION_LAYOUT) |
| 62 | + |
| 63 | + img = nb.Nifti1Image(np.zeros((10, 10, 10, 10)), np.eye(4)) |
| 64 | + |
| 65 | + for path in bids_dir.glob('sub-01/**/*.nii.gz'): |
| 66 | + img.to_filename(path) |
| 67 | + return bids_dir |
| 68 | + |
| 69 | + |
| 70 | +T1_FILTER = { |
| 71 | + 't1w': { |
| 72 | + 'acquisition': None, |
| 73 | + 'session': ['post'], |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | + |
| 78 | +@pytest.mark.parametrize( |
| 79 | + ('session', 'additional_args', 'filters', 'fail'), |
| 80 | + [ |
| 81 | + (False, [], None, False), |
| 82 | + (True, [], None, False), |
| 83 | + (True, ['--subject-anatomical-reference', 'sessionwise'], None, False), |
| 84 | + (True, ['--subject-anatomical-reference', 'sessionwise'], T1_FILTER, True), |
| 85 | + (True, ['--session-label', 'pre'], None, False), |
| 86 | + (True, ['--session-label', 'pre'], T1_FILTER, True), |
| 87 | + (True, ['--session-label', 'post'], None, False), |
| 88 | + ], |
| 89 | +) |
| 90 | +def test_build_workflow( |
| 91 | + monkeypatch, |
| 92 | + tmp_path, |
| 93 | + bids_no_session, |
| 94 | + bids_session, |
| 95 | + session, |
| 96 | + additional_args, |
| 97 | + filters, |
| 98 | + fail, |
| 99 | +): |
| 100 | + parser = get_parser() |
| 101 | + bids_dir = bids_no_session if not session else bids_session |
| 102 | + base_args = [ |
| 103 | + str(bids_dir), |
| 104 | + str(bids_dir / 'derivatives' / 'smriprep'), |
| 105 | + 'participant', |
| 106 | + '--participant-label', |
| 107 | + '01', |
| 108 | + ] |
| 109 | + if filters: |
| 110 | + filter_file = bids_session / '.filter.json' |
| 111 | + filter_file.write_text(json.dumps(filters)) |
| 112 | + additional_args += ['--bids-filter-file', str(filter_file)] |
| 113 | + |
| 114 | + base_args += additional_args |
| 115 | + pargs = parser.parse_args(base_args) |
| 116 | + |
| 117 | + fs_dir = tmp_path / 'freesurfer' |
| 118 | + fs_dir.mkdir() |
| 119 | + monkeypatch.setenv('FREESURFER_HOME', str(fs_dir)) |
| 120 | + monkeypatch.setenv('SUBJECTS_DIR', str(fs_dir)) |
| 121 | + |
| 122 | + if fail: |
| 123 | + with pytest.raises(ValueError, match='Conflicting entities'): |
| 124 | + build_workflow(pargs, {}) |
| 125 | + return |
| 126 | + |
| 127 | + ret = build_workflow(pargs, {}) |
| 128 | + assert ret['workflow'] |
0 commit comments