|
22 | 22 | # |
23 | 23 | """Test EPI manipulation routines.""" |
24 | 24 | import numpy as np |
| 25 | +import pytest |
25 | 26 | import nibabel as nb |
26 | | -from ..tools import brain_masker |
| 27 | +from nitransforms.linear import Affine |
| 28 | +from sdcflows.utils.tools import brain_masker, deoblique_and_zooms |
27 | 29 |
|
28 | 30 |
|
29 | 31 | def test_epi_mask(tmpdir, testdata_dir): |
30 | 32 | """Check mask algorithm.""" |
31 | 33 | tmpdir.chdir() |
32 | 34 | mask = brain_masker(testdata_dir / "epi.nii.gz")[-1] |
33 | 35 | assert abs(np.asanyarray(nb.load(mask).dataobj).sum() - 166476) < 10 |
| 36 | + |
| 37 | + |
| 38 | +@pytest.mark.parametrize("padding", [0, 1, 4]) |
| 39 | +@pytest.mark.parametrize("factor", [1, 4, 0.5]) |
| 40 | +def test_deoblique_and_zooms(padding, factor): |
| 41 | + """Check deoblique and denser.""" |
| 42 | + |
| 43 | + # Generate an example reference image |
| 44 | + ref_data = np.zeros((20, 30, 40), dtype=np.float32) |
| 45 | + ref_affine = np.eye(4) |
| 46 | + ref_affine[:3, :3] = np.diag([1.0, 1.2, 0.8]) # Set zooms to (2, 3, 4) |
| 47 | + ref_img = nb.Nifti1Image(ref_data, ref_affine) |
| 48 | + ref_zooms = np.array(ref_img.header.get_zooms()[:3]) |
| 49 | + |
| 50 | + # Generate an example oblique image |
| 51 | + ob_data = np.ones_like(ref_data) |
| 52 | + rotate = np.eye(4) |
| 53 | + |
| 54 | + # Rotate 90 degrees around x-axis |
| 55 | + rotate[:3, :3] = np.array([[0, 1, 0], [0, 0, 1], [1, 0, 0]]) |
| 56 | + ob_img = nb.Nifti1Image(ob_data, rotate @ ref_affine) |
| 57 | + |
| 58 | + # Call function with default parameters |
| 59 | + out_img = deoblique_and_zooms(ref_img, ob_img, padding=padding, factor=factor) |
| 60 | + |
| 61 | + # Check output shape and zooms |
| 62 | + assert np.allclose(out_img.header.get_zooms()[:3], ref_zooms / factor) |
| 63 | + |
| 64 | + resampled = Affine(reference=out_img).apply(ob_img, order=0) |
| 65 | + ref_volume = ref_data.size * ref_zooms.prod() |
| 66 | + res_volume = resampled.get_fdata().sum() * np.prod(resampled.header.get_zooms()) |
| 67 | + # 20% of change in volume is too high, must be an error somewhere |
| 68 | + assert abs(ref_volume - res_volume) < ref_volume * 0.2 |
0 commit comments