|
| 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 | +"""Tests of the transform module.""" |
| 4 | +import os |
| 5 | +import numpy as np |
| 6 | +from numpy.testing import assert_array_equal, assert_almost_equal, \ |
| 7 | + assert_array_almost_equal |
| 8 | +import pytest |
| 9 | + |
| 10 | +from ..loadsave import load as loadimg |
| 11 | +from ..nifti1 import Nifti1Image |
| 12 | +from ..eulerangles import euler2mat |
| 13 | +from ..affines import from_matvec |
| 14 | +from ..volumeutils import shape_zoom_affine |
| 15 | +from ..transform import linear as nbl |
| 16 | +from ..testing import (assert_equal, assert_not_equal, assert_true, |
| 17 | + assert_false, assert_raises, data_path, |
| 18 | + suppress_warnings, assert_dt_equal) |
| 19 | +from ..tmpdirs import InTemporaryDirectory |
| 20 | + |
| 21 | + |
| 22 | +SOMEONES_ANATOMY = os.path.join(data_path, 'someones_anatomy.nii.gz') |
| 23 | +# SOMEONES_ANATOMY = os.path.join(data_path, 'someones_anatomy.nii.gz') |
| 24 | + |
| 25 | + |
| 26 | +@pytest.mark.parametrize('image_orientation', ['RAS', 'LAS', 'LPS', 'oblique']) |
| 27 | +def test_affines_save(image_orientation): |
| 28 | + """Check implementation of exporting affines to formats.""" |
| 29 | + # Generate test transform |
| 30 | + img = loadimg(SOMEONES_ANATOMY) |
| 31 | + imgaff = img.affine |
| 32 | + |
| 33 | + if image_orientation == 'LAS': |
| 34 | + newaff = imgaff.copy() |
| 35 | + newaff[0, 0] *= -1.0 |
| 36 | + newaff[0, 3] = imgaff.dot(np.hstack((np.array(img.shape[:3]) - 1, 1.0)))[0] |
| 37 | + img = Nifti1Image(np.flip(img.get_fdata(), 0), newaff, img.header) |
| 38 | + elif image_orientation == 'LPS': |
| 39 | + newaff = imgaff.copy() |
| 40 | + newaff[0, 0] *= -1.0 |
| 41 | + newaff[1, 1] *= -1.0 |
| 42 | + newaff[:2, 3] = imgaff.dot(np.hstack((np.array(img.shape[:3]) - 1, 1.0)))[:2] |
| 43 | + img = Nifti1Image(np.flip(np.flip(img.get_fdata(), 0), 1), newaff, img.header) |
| 44 | + elif image_orientation == 'oblique': |
| 45 | + A = shape_zoom_affine(img.shape, img.header.get_zooms(), x_flip=False) |
| 46 | + R = from_matvec(euler2mat(x=0.09, y=0.001, z=0.001)) |
| 47 | + newaff = R.dot(A) |
| 48 | + img = Nifti1Image(img.get_fdata(), newaff, img.header) |
| 49 | + img.header.set_qform(newaff, 1) |
| 50 | + img.header.set_sform(newaff, 1) |
| 51 | + |
| 52 | + T = from_matvec(euler2mat(x=0.9, y=0.001, z=0.001), [4.0, 2.0, -1.0]) |
| 53 | + |
| 54 | + xfm = nbl.Affine(T) |
| 55 | + xfm.reference = img |
| 56 | + |
| 57 | + itk = nbl.load(os.path.join(data_path, 'affine-%s-itk.tfm' % image_orientation), |
| 58 | + fmt='itk') |
| 59 | + fsl = np.loadtxt(os.path.join(data_path, 'affine-%s.fsl' % image_orientation)) |
| 60 | + |
| 61 | + with InTemporaryDirectory(): |
| 62 | + xfm.to_filename('M.tfm', fmt='itk') |
| 63 | + xfm.to_filename('M.fsl', fmt='fsl') |
| 64 | + |
| 65 | + nb_itk = nbl.load('M.tfm', fmt='itk') |
| 66 | + nb_fsl = np.loadtxt('M.fsl') |
| 67 | + |
| 68 | + assert_equal(itk, nb_itk) |
| 69 | + assert_almost_equal(fsl, nb_fsl) |
| 70 | + |
| 71 | +# Create version not aligned to canonical |
0 commit comments