|
| 1 | +import os |
| 2 | +from shutil import which |
| 3 | + |
| 4 | +import nibabel as nb |
| 5 | +import numpy as np |
| 6 | +import pytest |
| 7 | +from nipype.pipeline import engine as pe |
| 8 | + |
| 9 | +from ..surfaces import init_anat_ribbon_wf, init_gifti_surfaces_wf |
| 10 | +from ...data import load_resource |
| 11 | + |
| 12 | + |
| 13 | +def test_ribbon_workflow(tmp_path): |
| 14 | + """Create ribbon mask for fsaverage subject""" |
| 15 | + |
| 16 | + for command in ("mris_convert", "wb_command", "fslmaths"): |
| 17 | + if not which(command): |
| 18 | + pytest.skip(f"Could not find {command} in PATH") |
| 19 | + |
| 20 | + if not os.path.exists(os.getenv('SUBJECTS_DIR')): |
| 21 | + pytest.skip("Could not find $SUBJECTS_DIR") |
| 22 | + |
| 23 | + # Low-res file that includes the fsaverage surfaces in its bounding box |
| 24 | + # We will use it both as a template and a comparison. |
| 25 | + test_ribbon = load_resource( |
| 26 | + "../interfaces/tests/data/sub-fsaverage_res-4_desc-cropped_ribbon.nii.gz" |
| 27 | + ) |
| 28 | + |
| 29 | + gifti_surfaces_wf = init_gifti_surfaces_wf(surfaces=['white', 'pial']) |
| 30 | + anat_ribbon_wf = init_anat_ribbon_wf() |
| 31 | + anat_ribbon_wf.inputs.inputnode.t1w_mask = test_ribbon |
| 32 | + |
| 33 | + gifti_surfaces_wf.inputs.inputnode.subjects_dir = os.getenv('SUBJECTS_DIR') |
| 34 | + gifti_surfaces_wf.inputs.inputnode.subject_id = 'fsaverage' |
| 35 | + |
| 36 | + wf = pe.Workflow(name='test_ribbon_wf', base_dir=tmp_path) |
| 37 | + wf.connect( |
| 38 | + [ |
| 39 | + ( |
| 40 | + gifti_surfaces_wf, |
| 41 | + anat_ribbon_wf, |
| 42 | + [ |
| 43 | + ('outputnode.white', 'inputnode.white'), |
| 44 | + ('outputnode.pial', 'inputnode.pial'), |
| 45 | + ], |
| 46 | + ), |
| 47 | + ] |
| 48 | + ) |
| 49 | + result = wf.run() |
| 50 | + |
| 51 | + combine_ribbon_vol_hemis = next( |
| 52 | + node for node in result.nodes() if node.name == 'combine_ribbon_vol_hemis' |
| 53 | + ) |
| 54 | + |
| 55 | + expected = nb.load(test_ribbon) |
| 56 | + ribbon = nb.load(combine_ribbon_vol_hemis.result.outputs.out_file) |
| 57 | + |
| 58 | + assert ribbon.shape == expected.shape |
| 59 | + assert np.allclose(ribbon.affine, expected.affine) |
| 60 | + # Mask data is binary, so we can use np.array_equal |
| 61 | + assert np.array_equal(ribbon.dataobj, expected.dataobj) |
0 commit comments