|
| 1 | +""" Tests for spaces module |
| 2 | +""" |
| 3 | + |
| 4 | +import numpy as np |
| 5 | +import numpy.linalg as npl |
| 6 | + |
| 7 | +from ..spaces import vox2out_vox, slice2volume |
| 8 | +from ..affines import apply_affine, from_matvec |
| 9 | +from ..nifti1 import Nifti1Image |
| 10 | +from ..eulerangles import euler2mat |
| 11 | + |
| 12 | + |
| 13 | +from numpy.testing import (assert_almost_equal, |
| 14 | + assert_array_equal) |
| 15 | + |
| 16 | +from nose.tools import (assert_true, assert_false, assert_raises, |
| 17 | + assert_equal, assert_not_equal) |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | +def assert_all_in(in_shape, in_affine, out_shape, out_affine): |
| 22 | + slices = tuple(slice(N) for N in in_shape) |
| 23 | + n_axes = len(in_shape) |
| 24 | + in_grid = np.mgrid[slices] |
| 25 | + in_grid = np.rollaxis(in_grid, 0, n_axes + 1) |
| 26 | + v2v = npl.inv(out_affine).dot(in_affine) |
| 27 | + if n_axes < 3: # reduced dimensions case |
| 28 | + new_v2v = np.eye(n_axes + 1) |
| 29 | + new_v2v[:n_axes, :n_axes] = v2v[:n_axes, :n_axes] |
| 30 | + new_v2v[:n_axes, -1] = v2v[:n_axes, -1] |
| 31 | + v2v = new_v2v |
| 32 | + out_grid = apply_affine(v2v, in_grid) |
| 33 | + TINY = 1e-12 |
| 34 | + assert_true(np.all(out_grid > -TINY)) |
| 35 | + assert_true(np.all(out_grid < np.array(out_shape) + TINY)) |
| 36 | + |
| 37 | + |
| 38 | +def test_vox2out_vox(): |
| 39 | + # Test world space bounding box |
| 40 | + # Test basic case, identity, no voxel sizes passed |
| 41 | + shape, aff = vox2out_vox(((2, 3, 4), np.eye(4))) |
| 42 | + assert_array_equal(shape, (2, 3, 4)) |
| 43 | + assert_array_equal(aff, np.eye(4)) |
| 44 | + # Some affines as input to the tests |
| 45 | + trans_123 = [[1, 0, 0, 1], [0, 1, 0, 2], [0, 0, 1, 3], [0, 0, 0, 1]] |
| 46 | + trans_m123 = [[1, 0, 0, -1], [0, 1, 0, -2], [0, 0, 1, -3], [0, 0, 0, 1]] |
| 47 | + rot_3 = from_matvec(euler2mat(np.pi / 4), [0, 0, 0]) |
| 48 | + for in_shape, in_aff, vox, out_shape, out_aff in ( |
| 49 | + # Identity |
| 50 | + ((2, 3, 4), np.eye(4), None, (2, 3, 4), np.eye(4)), |
| 51 | + # Flip first axis |
| 52 | + ((2, 3, 4), np.diag([-1, 1, 1, 1]), None, |
| 53 | + (2, 3, 4), [[1, 0, 0, -1], # axis reversed -> -ve offset |
| 54 | + [0, 1, 0, 0], |
| 55 | + [0, 0, 1, 0], |
| 56 | + [0, 0, 0, 1]]), |
| 57 | + # zooms for affine > 1 -> larger grid with default 1mm output voxels |
| 58 | + ((2, 3, 4), np.diag([4, 5, 6, 1]), None, |
| 59 | + (5, 11, 19), np.eye(4)), |
| 60 | + # set output voxels to be same size as input. back to original shape |
| 61 | + ((2, 3, 4), np.diag([4, 5, 6, 1]), (4, 5, 6), |
| 62 | + (2, 3, 4), np.diag([4, 5, 6, 1])), |
| 63 | + # Translation preserved in output |
| 64 | + ((2, 3, 4), trans_123, None, |
| 65 | + (2, 3, 4), trans_123), |
| 66 | + ((2, 3, 4), trans_m123, None, |
| 67 | + (2, 3, 4), trans_m123), |
| 68 | + # rotation around 3rd axis |
| 69 | + ((2, 3, 4), rot_3, None, |
| 70 | + # x diff, y diff now 3 cos pi / 4 == 2.12, ceil to 3, add 1 |
| 71 | + # most negative x now 2 cos pi / 4 |
| 72 | + (4, 4, 4), [[1, 0, 0, -2 * np.cos(np.pi / 4)], |
| 73 | + [0, 1, 0, 0], |
| 74 | + [0, 0, 1, 0], |
| 75 | + [0, 0, 0, 1]]), |
| 76 | + # Less than 3 axes |
| 77 | + ((2, 3), np.eye(4), None, |
| 78 | + (2, 3), np.eye(4)), |
| 79 | + ((2,), np.eye(4), None, |
| 80 | + (2,), np.eye(4)), |
| 81 | + # Number of voxel sizes matches length |
| 82 | + ((2, 3), np.diag([4, 5, 6, 1]), (4, 5), |
| 83 | + (2, 3), np.diag([4, 5, 1, 1])), |
| 84 | + ): |
| 85 | + img = Nifti1Image(np.ones(in_shape), in_aff) |
| 86 | + for input in ((in_shape, in_aff), img): |
| 87 | + shape, aff = vox2out_vox(input, vox) |
| 88 | + assert_all_in(in_shape, in_aff, shape, aff) |
| 89 | + assert_equal(shape, out_shape) |
| 90 | + assert_almost_equal(aff, out_aff) |
| 91 | + assert_true(isinstance(shape, tuple)) |
| 92 | + assert_true(isinstance(shape[0], int)) |
| 93 | + # Enforce number of axes |
| 94 | + assert_raises(ValueError, vox2out_vox, ((2, 3, 4, 5), np.eye(4))) |
| 95 | + assert_raises(ValueError, vox2out_vox, ((2, 3, 4, 5, 6), np.eye(4))) |
| 96 | + # Voxel sizes must be positive |
| 97 | + assert_raises(ValueError, vox2out_vox, ((2, 3, 4), np.eye(4), [-1, 1, 1])) |
| 98 | + assert_raises(ValueError, vox2out_vox, ((2, 3, 4), np.eye(4), [1, 0, 1])) |
| 99 | + |
| 100 | + |
| 101 | +def test_slice2volume(): |
| 102 | + # Get affine expressing selection of single slice from volume |
| 103 | + for axis, def_aff in zip((0, 1, 2), ( |
| 104 | + [[0, 0, 0], [1, 0, 0], [0, 1, 0], [0, 0, 1]], |
| 105 | + [[1, 0, 0], [0, 0, 0], [0, 1, 0], [0, 0, 1]], |
| 106 | + [[1, 0, 0], [0, 1, 0], [0, 0, 0], [0, 0, 1]])): |
| 107 | + for val in (0, 5, 10): |
| 108 | + exp_aff = np.array(def_aff) |
| 109 | + exp_aff[axis, -1] = val |
| 110 | + assert_array_equal(slice2volume(val, axis), exp_aff) |
| 111 | + assert_raises(ValueError, slice2volume, -1, 0) |
| 112 | + assert_raises(ValueError, slice2volume, 0, -1) |
| 113 | + assert_raises(ValueError, slice2volume, 0, 3) |
0 commit comments