|
| 1 | +# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*- |
| 2 | +# vi: set ft=python sts=4 ts=4 sw=4 et: |
| 3 | +import numpy as np |
| 4 | +import nibabel as nb |
| 5 | +import pytest |
| 6 | + |
| 7 | +from nibabel.orientations import axcodes2ornt, ornt_transform |
| 8 | + |
| 9 | +from ..image import _as_reoriented_backport, _orientations |
| 10 | +from ... import LooseVersion |
| 11 | + |
| 12 | +nibabel22 = LooseVersion(nb.__version__) >= LooseVersion('2.2.0') |
| 13 | + |
| 14 | + |
| 15 | +@pytest.mark.skipif(not nibabel22, |
| 16 | + reason="Old nibabel - can't directly compare") |
| 17 | +def test_reorientation_backport(): |
| 18 | + pixdims = ((1, 1, 1), (2, 2, 3)) |
| 19 | + data = np.random.normal(size=(17, 18, 19, 2)) |
| 20 | + |
| 21 | + for pixdim in pixdims: |
| 22 | + # Generate a randomly rotated affine |
| 23 | + angles = np.random.uniform(-np.pi, np.pi, 3) * [1, 0.5, 1] |
| 24 | + rot = nb.eulerangles.euler2mat(*angles) |
| 25 | + scale = np.diag(pixdim) |
| 26 | + translation = np.array((17, 18, 19)) / 2 |
| 27 | + affine = nb.affines.from_matvec(rot.dot(scale), translation) |
| 28 | + |
| 29 | + # Create image |
| 30 | + img = nb.Nifti1Image(data, affine) |
| 31 | + dim_info = {'freq': 0, 'phase': 1, 'slice': 2} |
| 32 | + img.header.set_dim_info(**dim_info) |
| 33 | + |
| 34 | + # Find a random, non-identity transform |
| 35 | + targ_ornt = orig_ornt = nb.io_orientation(affine) |
| 36 | + while np.array_equal(targ_ornt, orig_ornt): |
| 37 | + new_code = np.random.choice(_orientations) |
| 38 | + targ_ornt = axcodes2ornt(new_code) |
| 39 | + |
| 40 | + identity = ornt_transform(orig_ornt, orig_ornt) |
| 41 | + transform = ornt_transform(orig_ornt, targ_ornt) |
| 42 | + |
| 43 | + # Identity transform returns exact image |
| 44 | + assert img.as_reoriented(identity) is img |
| 45 | + assert _as_reoriented_backport(img, identity) is img |
| 46 | + |
| 47 | + reoriented_a = img.as_reoriented(transform) |
| 48 | + reoriented_b = _as_reoriented_backport(img, transform) |
| 49 | + |
| 50 | + # Reorientation changes affine and data array |
| 51 | + assert not np.allclose(img.affine, reoriented_a.affine) |
| 52 | + assert not (img.shape == reoriented_a.shape and |
| 53 | + np.allclose(img.get_data(), reoriented_a.get_data())) |
| 54 | + assert not np.array_equal(img.header.get_dim_info(), |
| 55 | + reoriented_a.header.get_dim_info()) |
| 56 | + |
| 57 | + # Both approaches produce equivalent images |
| 58 | + assert np.allclose(reoriented_a.affine, reoriented_b.affine) |
| 59 | + assert np.array_equal(reoriented_a.get_data(), reoriented_b.get_data()) |
| 60 | + assert np.array_equal(reoriented_a.header.get_dim_info(), |
| 61 | + reoriented_b.header.get_dim_info()) |
0 commit comments