|
| 1 | +from math import prod |
| 2 | +from pathlib import Path |
| 3 | +from unittest import skipUnless |
| 4 | + |
| 5 | +import numpy as np |
| 6 | +import pytest |
| 7 | + |
| 8 | +from nibabel import pointset as ps |
| 9 | +from nibabel.affines import apply_affine |
| 10 | +from nibabel.arrayproxy import ArrayProxy |
| 11 | +from nibabel.fileslice import strided_scalar |
| 12 | +from nibabel.onetime import auto_attr |
| 13 | +from nibabel.optpkg import optional_package |
| 14 | +from nibabel.spatialimages import SpatialImage |
| 15 | +from nibabel.tests.nibabel_data import get_nibabel_data |
| 16 | + |
| 17 | +h5, has_h5py, _ = optional_package('h5py') |
| 18 | + |
| 19 | +FS_DATA = Path(get_nibabel_data()) / 'nitest-freesurfer' |
| 20 | + |
| 21 | + |
| 22 | +class TestPointsets: |
| 23 | + rng = np.random.default_rng() |
| 24 | + |
| 25 | + @pytest.mark.parametrize('shape', [(5, 2), (5, 3), (5, 4)]) |
| 26 | + @pytest.mark.parametrize('homogeneous', [True, False]) |
| 27 | + def test_init(self, shape, homogeneous): |
| 28 | + coords = self.rng.random(shape) |
| 29 | + |
| 30 | + if homogeneous: |
| 31 | + coords = np.column_stack([coords, np.ones(shape[0])]) |
| 32 | + |
| 33 | + points = ps.Pointset(coords, homogeneous=homogeneous) |
| 34 | + assert np.allclose(points.affine, np.eye(shape[1] + 1)) |
| 35 | + assert points.homogeneous is homogeneous |
| 36 | + assert (points.n_coords, points.dim) == shape |
| 37 | + |
| 38 | + points = ps.Pointset(coords, affine=np.diag([2] * shape[1] + [1]), homogeneous=homogeneous) |
| 39 | + assert np.allclose(points.affine, np.diag([2] * shape[1] + [1])) |
| 40 | + assert points.homogeneous is homogeneous |
| 41 | + assert (points.n_coords, points.dim) == shape |
| 42 | + |
| 43 | + # Badly shaped affine |
| 44 | + with pytest.raises(ValueError): |
| 45 | + ps.Pointset(coords, affine=[0, 1]) |
| 46 | + |
| 47 | + # Badly valued affine |
| 48 | + with pytest.raises(ValueError): |
| 49 | + ps.Pointset(coords, affine=np.ones((shape[1] + 1, shape[1] + 1))) |
| 50 | + |
| 51 | + @pytest.mark.parametrize('shape', [(5, 2), (5, 3), (5, 4)]) |
| 52 | + @pytest.mark.parametrize('homogeneous', [True, False]) |
| 53 | + def test_affines(self, shape, homogeneous): |
| 54 | + orig_coords = coords = self.rng.random(shape) |
| 55 | + |
| 56 | + if homogeneous: |
| 57 | + coords = np.column_stack([coords, np.ones(shape[0])]) |
| 58 | + |
| 59 | + points = ps.Pointset(coords, homogeneous=homogeneous) |
| 60 | + assert np.allclose(points.get_coords(), orig_coords) |
| 61 | + |
| 62 | + # Apply affines |
| 63 | + scaler = np.diag([2] * shape[1] + [1]) |
| 64 | + scaled = scaler @ points |
| 65 | + assert np.array_equal(scaled.coordinates, points.coordinates) |
| 66 | + assert np.array_equal(scaled.affine, scaler) |
| 67 | + assert np.allclose(scaled.get_coords(), 2 * orig_coords) |
| 68 | + |
| 69 | + flipper = np.eye(shape[1] + 1) |
| 70 | + # [[1, 0, 0], [0, 1, 0], [0, 0, 1]] becomes [[0, 1, 0], [1, 0, 0], [0, 0, 1]] |
| 71 | + flipper[:-1] = flipper[-2::-1] |
| 72 | + flipped = flipper @ points |
| 73 | + assert np.array_equal(flipped.coordinates, points.coordinates) |
| 74 | + assert np.array_equal(flipped.affine, flipper) |
| 75 | + assert np.allclose(flipped.get_coords(), orig_coords[:, ::-1]) |
| 76 | + |
| 77 | + # Concatenate affines, with any associativity |
| 78 | + for doubledup in [(scaler @ flipper) @ points, scaler @ (flipper @ points)]: |
| 79 | + assert np.array_equal(doubledup.coordinates, points.coordinates) |
| 80 | + assert np.allclose(doubledup.affine, scaler @ flipper) |
| 81 | + assert np.allclose(doubledup.get_coords(), 2 * orig_coords[:, ::-1]) |
| 82 | + |
| 83 | + def test_homogeneous_coordinates(self): |
| 84 | + ccoords = self.rng.random((5, 3)) |
| 85 | + hcoords = np.column_stack([ccoords, np.ones(5)]) |
| 86 | + |
| 87 | + cartesian = ps.Pointset(ccoords) |
| 88 | + homogeneous = ps.Pointset(hcoords, homogeneous=True) |
| 89 | + |
| 90 | + for points in (cartesian, homogeneous): |
| 91 | + assert np.array_equal(points.get_coords(), ccoords) |
| 92 | + assert np.array_equal(points.get_coords(as_homogeneous=True), hcoords) |
| 93 | + |
| 94 | + affine = np.diag([2, 3, 4, 1]) |
| 95 | + cart2 = affine @ cartesian |
| 96 | + homo2 = affine @ homogeneous |
| 97 | + |
| 98 | + exp_c = apply_affine(affine, ccoords) |
| 99 | + exp_h = (affine @ hcoords.T).T |
| 100 | + for points in (cart2, homo2): |
| 101 | + assert np.array_equal(points.get_coords(), exp_c) |
| 102 | + assert np.array_equal(points.get_coords(as_homogeneous=True), exp_h) |
| 103 | + |
| 104 | + |
| 105 | +def test_GridIndices(): |
| 106 | + # 2D case |
| 107 | + shape = (2, 3) |
| 108 | + gi = ps.GridIndices(shape) |
| 109 | + |
| 110 | + assert gi.dtype == np.dtype('u1') |
| 111 | + assert gi.shape == (6, 2) |
| 112 | + assert repr(gi) == '<GridIndices(2, 3)>' |
| 113 | + |
| 114 | + gi_arr = np.asanyarray(gi) |
| 115 | + assert gi_arr.dtype == np.dtype('u1') |
| 116 | + assert gi_arr.shape == (6, 2) |
| 117 | + # Tractable to write out |
| 118 | + assert np.array_equal(gi_arr, [[0, 0], [0, 1], [0, 2], [1, 0], [1, 1], [1, 2]]) |
| 119 | + |
| 120 | + shape = (2, 3, 4) |
| 121 | + gi = ps.GridIndices(shape) |
| 122 | + |
| 123 | + assert gi.dtype == np.dtype('u1') |
| 124 | + assert gi.shape == (24, 3) |
| 125 | + assert repr(gi) == '<GridIndices(2, 3, 4)>' |
| 126 | + |
| 127 | + gi_arr = np.asanyarray(gi) |
| 128 | + assert gi_arr.dtype == np.dtype('u1') |
| 129 | + assert gi_arr.shape == (24, 3) |
| 130 | + # Separate implementation |
| 131 | + assert np.array_equal(gi_arr, np.mgrid[:2, :3, :4].reshape(3, -1).T) |
| 132 | + |
| 133 | + |
| 134 | +class TestGrids(TestPointsets): |
| 135 | + @pytest.mark.parametrize('shape', [(5, 5, 5), (5, 5, 5, 5), (5, 5, 5, 5, 5)]) |
| 136 | + def test_from_image(self, shape): |
| 137 | + # Check image is generates voxel coordinates |
| 138 | + affine = np.diag([2, 3, 4, 1]) |
| 139 | + img = SpatialImage(strided_scalar(shape), affine) |
| 140 | + grid = ps.Grid.from_image(img) |
| 141 | + grid_coords = grid.get_coords() |
| 142 | + |
| 143 | + assert grid.n_coords == prod(shape[:3]) |
| 144 | + assert grid.dim == 3 |
| 145 | + assert np.allclose(grid.affine, affine) |
| 146 | + |
| 147 | + assert np.allclose(grid_coords[0], [0, 0, 0]) |
| 148 | + # Final index is [4, 4, 4], scaled by affine |
| 149 | + assert np.allclose(grid_coords[-1], [8, 12, 16]) |
| 150 | + |
| 151 | + def test_from_mask(self): |
| 152 | + affine = np.diag([2, 3, 4, 1]) |
| 153 | + mask = np.zeros((3, 3, 3)) |
| 154 | + mask[1, 1, 1] = 1 |
| 155 | + img = SpatialImage(mask, affine) |
| 156 | + |
| 157 | + grid = ps.Grid.from_mask(img) |
| 158 | + grid_coords = grid.get_coords() |
| 159 | + |
| 160 | + assert grid.n_coords == 1 |
| 161 | + assert grid.dim == 3 |
| 162 | + assert np.array_equal(grid_coords, [[2, 3, 4]]) |
| 163 | + |
| 164 | + def test_to_mask(self): |
| 165 | + coords = np.array([[1, 1, 1]]) |
| 166 | + |
| 167 | + grid = ps.Grid(coords) |
| 168 | + |
| 169 | + mask_img = grid.to_mask() |
| 170 | + assert mask_img.shape == (2, 2, 2) |
| 171 | + assert np.array_equal(mask_img.get_fdata(), [[[0, 0], [0, 0]], [[0, 0], [0, 1]]]) |
| 172 | + assert np.array_equal(mask_img.affine, np.eye(4)) |
| 173 | + |
| 174 | + mask_img = grid.to_mask(shape=(3, 3, 3)) |
| 175 | + assert mask_img.shape == (3, 3, 3) |
| 176 | + assert np.array_equal( |
| 177 | + mask_img.get_fdata(), |
| 178 | + [ |
| 179 | + [[0, 0, 0], [0, 0, 0], [0, 0, 0]], |
| 180 | + [[0, 0, 0], [0, 1, 0], [0, 0, 0]], |
| 181 | + [[0, 0, 0], [0, 0, 0], [0, 0, 0]], |
| 182 | + ], |
| 183 | + ) |
| 184 | + assert np.array_equal(mask_img.affine, np.eye(4)) |
0 commit comments