|
4 | 4 | import nibabel as nb
|
5 | 5 | import pytest
|
6 | 6 |
|
7 |
| -from ..nibabel import Binarize, ApplyMask |
| 7 | +from ..nibabel import Binarize, ApplyMask, SplitSeries, MergeSeries |
8 | 8 |
|
9 | 9 |
|
10 | 10 | def test_Binarize(tmp_path):
|
@@ -69,3 +69,79 @@ def test_ApplyMask(tmp_path):
|
69 | 69 | ApplyMask(in_file=str(in_file), in_mask=str(in_mask), threshold=0.4).run()
|
70 | 70 | with pytest.raises(ValueError):
|
71 | 71 | ApplyMask(in_file=str(in_file4d), in_mask=str(in_mask), threshold=0.4).run()
|
| 72 | + |
| 73 | + |
| 74 | +def test_SplitSeries(tmp_path): |
| 75 | + """Test 4-to-3 NIfTI split interface.""" |
| 76 | + os.chdir(str(tmp_path)) |
| 77 | + |
| 78 | + # Test the 4D |
| 79 | + data = np.ones((20, 20, 20, 15), dtype=float) |
| 80 | + in_file = tmp_path / 'input4D.nii.gz' |
| 81 | + nb.Nifti1Image(data, np.eye(4), None).to_filename(str(in_file)) |
| 82 | + |
| 83 | + split = SplitSeries(in_file=str(in_file)).run() |
| 84 | + assert len(split.outputs.out_files) == 15 |
| 85 | + |
| 86 | + # Test the 3D |
| 87 | + data = np.ones((20, 20, 20), dtype=float) |
| 88 | + in_file = tmp_path / 'input3D.nii.gz' |
| 89 | + nb.Nifti1Image(data, np.eye(4), None).to_filename(str(in_file)) |
| 90 | + |
| 91 | + with pytest.raises(RuntimeError): |
| 92 | + SplitSeries(in_file=str(in_file)).run() |
| 93 | + |
| 94 | + split = SplitSeries(in_file=str(in_file), accept_3D=True).run() |
| 95 | + assert isinstance(split.outputs.out_files, str) |
| 96 | + |
| 97 | + # Test the 3D |
| 98 | + data = np.ones((20, 20, 20, 1), dtype=float) |
| 99 | + in_file = tmp_path / 'input3D.nii.gz' |
| 100 | + nb.Nifti1Image(data, np.eye(4), None).to_filename(str(in_file)) |
| 101 | + |
| 102 | + with pytest.raises(RuntimeError): |
| 103 | + SplitSeries(in_file=str(in_file)).run() |
| 104 | + |
| 105 | + split = SplitSeries(in_file=str(in_file), accept_3D=True).run() |
| 106 | + assert isinstance(split.outputs.out_files, str) |
| 107 | + |
| 108 | + # Test the 5D |
| 109 | + data = np.ones((20, 20, 20, 2, 2), dtype=float) |
| 110 | + in_file = tmp_path / 'input5D.nii.gz' |
| 111 | + nb.Nifti1Image(data, np.eye(4), None).to_filename(str(in_file)) |
| 112 | + |
| 113 | + with pytest.raises(RuntimeError): |
| 114 | + SplitSeries(in_file=str(in_file)).run() |
| 115 | + |
| 116 | + with pytest.raises(RuntimeError): |
| 117 | + SplitSeries(in_file=str(in_file), accept_3D=True).run() |
| 118 | + |
| 119 | + # Test splitting ANTs warpfields |
| 120 | + data = np.ones((20, 20, 20, 1, 3), dtype=float) |
| 121 | + in_file = tmp_path / 'warpfield.nii.gz' |
| 122 | + nb.Nifti1Image(data, np.eye(4), None).to_filename(str(in_file)) |
| 123 | + |
| 124 | + split = SplitSeries(in_file=str(in_file)).run() |
| 125 | + assert len(split.outputs.out_files) == 3 |
| 126 | + |
| 127 | +def test_MergeSeries(tmp_path): |
| 128 | + """Test 3-to-4 NIfTI concatenation interface.""" |
| 129 | + os.chdir(str(tmp_path)) |
| 130 | + |
| 131 | + data = np.ones((20, 20, 20), dtype=float) |
| 132 | + in_file = tmp_path / 'input3D.nii.gz' |
| 133 | + nb.Nifti1Image(data, np.eye(4), None).to_filename(str(in_file)) |
| 134 | + |
| 135 | + merge = MergeSeries(in_files=[str(in_file)] * 5).run() |
| 136 | + assert nb.load(merge.outputs.out_file).dataobj.shape == (20, 20, 20, 5) |
| 137 | + |
| 138 | + in_4D = tmp_path / 'input4D.nii.gz' |
| 139 | + nb.Nifti1Image( |
| 140 | + np.ones((20, 20, 20, 4), dtype=float), np.eye(4), None |
| 141 | + ).to_filename(str(in_4D)) |
| 142 | + |
| 143 | + merge = MergeSeries(in_files=[str(in_file)] + [str(in_4D)]).run() |
| 144 | + assert nb.load(merge.outputs.out_file).dataobj.shape == (20, 20, 20, 5) |
| 145 | + |
| 146 | + with pytest.raises(ValueError): |
| 147 | + MergeSeries(in_files=[str(in_file)] + [str(in_4D)], allow_4D=False).run() |
0 commit comments