Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: Added some minimal test-cases to the Affine class #33

Merged
merged 2 commits into from
Oct 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions nitransforms/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ def __eq__(self, other):
return (np.allclose(self.affine, other.affine, rtol=EQUALITY_TOL) and
self.shape == other.shape)

def __ne__(self, other):
"""Overload not equal operator."""
return not self == other


class TransformBase(object):
"""Abstract image class to represent transforms."""
Expand Down
2 changes: 1 addition & 1 deletion nitransforms/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def doctest_autoimport(doctest_namespace):
doctest_namespace['nb'] = nb
doctest_namespace['os'] = os
doctest_namespace['Path'] = Path
doctest_namespace['datadir'] = os.path.join(os.path.dirname(__file__), 'tests/data')
doctest_namespace['datadir'] = Path(__file__).parent / 'tests' / 'data'

tmpdir = tempfile.TemporaryDirectory()
doctest_namespace['tmpdir'] = tmpdir.name
Expand Down
23 changes: 14 additions & 9 deletions nitransforms/linear.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
#
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
"""Linear transforms."""
import numpy as np
from pathlib import Path
import warnings
import numpy as np

from nibabel.loadsave import load as loadimg
from nibabel.affines import from_matvec, voxel_sizes, obliquity
Expand Down Expand Up @@ -40,6 +41,13 @@ def __init__(self, matrix=None, reference=None):

Examples
--------
>>> xfm = Affine(reference=datadir / 'someones_anatomy.nii.gz')
>>> xfm.matrix # doctest: +NORMALIZE_WHITESPACE
array([[[1., 0., 0., 0.],
[0., 1., 0., 0.],
[0., 0., 1., 0.],
[0., 0., 0., 1.]]])

>>> xfm = Affine([[1, 0, 0, 4], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]])
>>> xfm.matrix # doctest: +NORMALIZE_WHITESPACE
array([[[1, 0, 0, 4],
Expand All @@ -62,8 +70,6 @@ def __init__(self, matrix=None, reference=None):
), 'affine matrix is not square'

if reference:
if isinstance(reference, str):
reference = loadimg(reference)
self.reference = reference

def __eq__(self, other):
Expand All @@ -78,9 +84,10 @@ def __eq__(self, other):
True

"""
if not self._reference == other._reference:
return False
return np.allclose(self.matrix, other.matrix, rtol=EQUALITY_TOL)
_eq = np.allclose(self.matrix, other.matrix, rtol=EQUALITY_TOL)
if _eq and self._reference != other._reference:
warnings.warn('Affines are equal, but references do not match.')
return _eq

@property
def matrix(self):
Expand Down Expand Up @@ -257,9 +264,7 @@ def load(filename, fmt='X5', reference=None):
else:
raise NotImplementedError

if reference and isinstance(reference, str):
reference = loadimg(reference)
return Affine(matrix, reference)
return Affine(matrix, reference=reference)


def _fsl_aff_adapt(space):
Expand Down
1 change: 1 addition & 0 deletions nitransforms/tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def test_ImageGrid(get_testdata, image_orientation):

img2 = ImageGrid(img)
assert img2 == img
assert (img2 != img) is False


def test_ImageGrid_utils(tmpdir, data_path, get_testdata):
Expand Down
7 changes: 7 additions & 0 deletions nitransforms/tests/test_linear.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import numpy as np
from subprocess import check_call
import shutil
import h5py

import nibabel as nb
from nibabel.eulerangles import euler2mat
Expand Down Expand Up @@ -155,3 +156,9 @@ def test_apply_linear_transform(
diff = sw_moved.get_fdata() - nt_moved.get_fdata()
# A certain tolerance is necessary because of resampling at borders
assert (np.abs(diff) > 1e-3).sum() / diff.size < TESTS_BORDER_TOLERANCE


def test_Affine(tmpdir):
"""Test affine's operations."""
with h5py.File('xfm.x5', 'w') as f:
nbl.Affine()._to_hdf5(f.create_group('Affine'))