Skip to content

Commit c19aa04

Browse files
committed
test: Access MRS NIfTI extension
1 parent fcf02a5 commit c19aa04

File tree

2 files changed

+52
-14
lines changed

2 files changed

+52
-14
lines changed

tests/conftest.py

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,37 @@
1010
from .data import load_data
1111

1212

13+
def get_submod_or_env(dir_name: str, env_var: str) -> Path:
14+
"""Get submodule data path or override from environment variable.
15+
16+
Signals to skip tests if submodule is not checked out and no variable is set.
17+
"""
18+
ret = os.getenv(env_var)
19+
if not ret:
20+
data_path = load_data(dir_name)
21+
if not any(data_path.iterdir()):
22+
pytest.skip(f'{dir_name} submodule is not checked out')
23+
return data_path
24+
return Path(ret)
25+
26+
1327
@pytest.fixture(scope='session')
1428
def examples() -> Path:
1529
"""Get bids-examples from submodule, allow environment variable override."""
16-
ret = os.getenv('BIDS_EXAMPLES')
17-
if not ret:
18-
examples = load_data('bids-examples')
19-
if not any(examples.iterdir()):
20-
pytest.skip('bids-examples submodule is not checked out')
21-
return examples
22-
return Path(ret)
30+
return get_submod_or_env('bids-examples', 'BIDS_EXAMPLES')
2331

2432

2533
@pytest.fixture(scope='session')
2634
def gitignore_test() -> Path:
2735
"""Get bids-examples from submodule, allow environment variable override."""
28-
ret = os.getenv('GITIGNORE_TEST_DIR')
29-
if not ret:
30-
test_data = load_data('gitignore-test')
31-
if not any(test_data.iterdir()):
32-
pytest.skip('gitignore-test submodule is not checked out')
33-
return test_data
34-
return Path(ret)
36+
return get_submod_or_env('gitignore-test', 'GITIGNORE_TEST_DIR')
37+
38+
39+
@pytest.fixture(scope='session')
40+
def mrs_data() -> Path:
41+
"""Get MRS data from submodule, allow environment variable override."""
42+
mrs_nifti_standard = get_submod_or_env('mrs_nifti_standard', 'MRS_NIFTI_STANDARD')
43+
return mrs_nifti_standard / 'example_data' / 'examples'
3544

3645

3746
@pytest.fixture(scope='session')

tests/test_context.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,3 +206,32 @@ def test_load_tsv_gz(synthetic_dataset: FileTree) -> None:
206206

207207
assert tuple(tsvgz_file.keys()) == headers
208208
# Will need an additional test for the content
209+
210+
211+
def test_nifti_mrs_header(
212+
mrs_data: Path,
213+
schema: Namespace,
214+
memfs: fsspec.AbstractFileSystem,
215+
tmp_path: Path,
216+
) -> None:
217+
example_01 = mrs_data / 'example_01.nii.gz'
218+
memfs.pipe(
219+
{
220+
'/dataset_description.json': json.dumps(
221+
{'Name': 'MRS Test Dataset', 'BIDSVersion': '1.10.1'}
222+
).encode(),
223+
'/sub-01/mrs/sub-01_acq-press_mrs.nii.gz': example_01.read_bytes(),
224+
}
225+
)
226+
memfs.get('memory:///', str(tmp_path), recursive=True)
227+
dataset = FileTree.read_from_filesystem(tmp_path)
228+
sub01 = dataset / 'sub-01'
229+
mrs = dataset / 'sub-01' / 'mrs' / 'sub-01_acq-press_mrs.nii.gz'
230+
231+
ds = context.Dataset(dataset, schema)
232+
subject = Subject(context.Sessions(sub01))
233+
mrs_context = context.Context(mrs, ds, subject)
234+
235+
assert mrs_context.nifti_header is not None
236+
assert isinstance(mrs_context.nifti_header.mrs, Namespace)
237+
assert mrs_context.nifti_header.mrs.ResonantNucleus == ['1H']

0 commit comments

Comments
 (0)