|
| 1 | +import typing as ty |
| 2 | +from pathlib import Path |
| 3 | + |
| 4 | +import nibabel as nb |
| 5 | +import numpy as np |
| 6 | +import pytest |
| 7 | + |
| 8 | +from nibabies.workflows.anatomical.preproc import _normalize_roi, init_csf_norm_wf |
| 9 | + |
| 10 | +EXPECTED_CSF_NORM = np.array([[[10, 73], [73, 29]], [[77, 80], [6, 16]]], dtype='uint8') |
| 11 | + |
| 12 | + |
| 13 | +@pytest.fixture |
| 14 | +def csf_norm_data(tmp_path) -> ty.Generator[tuple[Path, list[Path]], None, None]: |
| 15 | + np.random.seed(10) |
| 16 | + |
| 17 | + in_file = tmp_path / 'input.nii.gz' |
| 18 | + data = np.random.randint(1, 101, size=(2, 2, 2), dtype='uint8') |
| 19 | + img = nb.Nifti1Image(data, np.eye(4)) |
| 20 | + img.to_filename(in_file) |
| 21 | + |
| 22 | + masks = [] |
| 23 | + for tpm in ('gm', 'wm', 'csf'): |
| 24 | + name = tmp_path / f'{tpm}.nii.gz' |
| 25 | + binmask = data > np.random.randint(10, 90) |
| 26 | + masked = (binmask * 1).astype('uint8') |
| 27 | + mask = nb.Nifti1Image(masked, img.affine) |
| 28 | + mask.to_filename(name) |
| 29 | + masks.append(name) |
| 30 | + |
| 31 | + yield in_file, masks |
| 32 | + |
| 33 | + in_file.unlink() |
| 34 | + for m in masks: |
| 35 | + m.unlink() |
| 36 | + |
| 37 | + |
| 38 | +def test_csf_norm_wf(tmp_path, csf_norm_data): |
| 39 | + anat, tpms = csf_norm_data |
| 40 | + wf = init_csf_norm_wf() |
| 41 | + wf.base_dir = tmp_path |
| 42 | + |
| 43 | + wf.inputs.inputnode.anat_preproc = anat |
| 44 | + wf.inputs.inputnode.anat_tpms = tpms |
| 45 | + |
| 46 | + # verify workflow runs |
| 47 | + wf.run() |
| 48 | + |
| 49 | + # verify function works as expected |
| 50 | + outfile = _normalize_roi(anat, tpms[2]) |
| 51 | + assert np.array_equal( |
| 52 | + np.asanyarray(nb.load(outfile).dataobj), |
| 53 | + EXPECTED_CSF_NORM, |
| 54 | + ) |
| 55 | + Path(outfile).unlink() |
0 commit comments