Skip to content

Commit 45a0a2f

Browse files
authored
Merge pull request #33 from oesteban/enh/increase-linear-coverage
ENH: Added some minimal test-cases to the Affine class
2 parents f39e6fd + 7488421 commit 45a0a2f

File tree

5 files changed

+27
-10
lines changed

5 files changed

+27
-10
lines changed

nitransforms/base.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,10 @@ def __eq__(self, other):
154154
return (np.allclose(self.affine, other.affine, rtol=EQUALITY_TOL) and
155155
self.shape == other.shape)
156156

157+
def __ne__(self, other):
158+
"""Overload not equal operator."""
159+
return not self == other
160+
157161

158162
class TransformBase(object):
159163
"""Abstract image class to represent transforms."""

nitransforms/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def doctest_autoimport(doctest_namespace):
1919
doctest_namespace['nb'] = nb
2020
doctest_namespace['os'] = os
2121
doctest_namespace['Path'] = Path
22-
doctest_namespace['datadir'] = os.path.join(os.path.dirname(__file__), 'tests/data')
22+
doctest_namespace['datadir'] = Path(__file__).parent / 'tests' / 'data'
2323

2424
tmpdir = tempfile.TemporaryDirectory()
2525
doctest_namespace['tmpdir'] = tmpdir.name

nitransforms/linear.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
#
88
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
99
"""Linear transforms."""
10-
import numpy as np
1110
from pathlib import Path
11+
import warnings
12+
import numpy as np
1213

1314
from nibabel.loadsave import load as loadimg
1415
from nibabel.affines import from_matvec, voxel_sizes, obliquity
@@ -40,6 +41,13 @@ def __init__(self, matrix=None, reference=None):
4041
4142
Examples
4243
--------
44+
>>> xfm = Affine(reference=datadir / 'someones_anatomy.nii.gz')
45+
>>> xfm.matrix # doctest: +NORMALIZE_WHITESPACE
46+
array([[[1., 0., 0., 0.],
47+
[0., 1., 0., 0.],
48+
[0., 0., 1., 0.],
49+
[0., 0., 0., 1.]]])
50+
4351
>>> xfm = Affine([[1, 0, 0, 4], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]])
4452
>>> xfm.matrix # doctest: +NORMALIZE_WHITESPACE
4553
array([[[1, 0, 0, 4],
@@ -62,8 +70,6 @@ def __init__(self, matrix=None, reference=None):
6270
), 'affine matrix is not square'
6371

6472
if reference:
65-
if isinstance(reference, str):
66-
reference = loadimg(reference)
6773
self.reference = reference
6874

6975
def __eq__(self, other):
@@ -78,9 +84,10 @@ def __eq__(self, other):
7884
True
7985
8086
"""
81-
if not self._reference == other._reference:
82-
return False
83-
return np.allclose(self.matrix, other.matrix, rtol=EQUALITY_TOL)
87+
_eq = np.allclose(self.matrix, other.matrix, rtol=EQUALITY_TOL)
88+
if _eq and self._reference != other._reference:
89+
warnings.warn('Affines are equal, but references do not match.')
90+
return _eq
8491

8592
@property
8693
def matrix(self):
@@ -257,9 +264,7 @@ def load(filename, fmt='X5', reference=None):
257264
else:
258265
raise NotImplementedError
259266

260-
if reference and isinstance(reference, str):
261-
reference = loadimg(reference)
262-
return Affine(matrix, reference)
267+
return Affine(matrix, reference=reference)
263268

264269

265270
def _fsl_aff_adapt(space):

nitransforms/tests/test_base.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ def test_ImageGrid(get_testdata, image_orientation):
4646

4747
img2 = ImageGrid(img)
4848
assert img2 == img
49+
assert (img2 != img) is False
4950

5051

5152
def test_ImageGrid_utils(tmpdir, data_path, get_testdata):

nitransforms/tests/test_linear.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import numpy as np
77
from subprocess import check_call
88
import shutil
9+
import h5py
910

1011
import nibabel as nb
1112
from nibabel.eulerangles import euler2mat
@@ -155,3 +156,9 @@ def test_apply_linear_transform(
155156
diff = sw_moved.get_fdata() - nt_moved.get_fdata()
156157
# A certain tolerance is necessary because of resampling at borders
157158
assert (np.abs(diff) > 1e-3).sum() / diff.size < TESTS_BORDER_TOLERANCE
159+
160+
161+
def test_Affine(tmpdir):
162+
"""Test affine's operations."""
163+
with h5py.File('xfm.x5', 'w') as f:
164+
nbl.Affine()._to_hdf5(f.create_group('Affine'))

0 commit comments

Comments
 (0)