Skip to content

Commit 94445d9

Browse files
authored
Merge pull request #23 from mgxd/enh/cov
ENH: Increase coverage of linear transforms code
2 parents 2ab68b3 + 3552e4f commit 94445d9

File tree

4 files changed

+75
-2
lines changed

4 files changed

+75
-2
lines changed

nitransforms/linear.py

+35-1
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,13 @@ def resample(self, moving, order=3, mode='constant', cval=0.0, prefilter=True,
108108
109109
Examples
110110
--------
111+
>>> a = Affine()
112+
>>> a.matrix
113+
array([[[1., 0., 0., 0.],
114+
[0., 1., 0., 0.],
115+
[0., 0., 1., 0.],
116+
[0., 0., 0., 1.]]])
117+
111118
>>> xfm = Affine([[1, 0, 0, 4], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]])
112119
>>> ref = nb.load(testfile)
113120
>>> xfm.reference = ref
@@ -172,7 +179,34 @@ def resample(self, moving, order=3, mode='constant', cval=0.0, prefilter=True,
172179
return moved_image
173180

174181
def map_point(self, coords, index=0, forward=True):
175-
"""Apply y = f(x), where x is the argument `coords`."""
182+
"""
183+
Apply y = f(x), where x is the argument `coords`.
184+
185+
Parameters
186+
----------
187+
coords : array_like
188+
RAS coordinates to map
189+
index : int, optional
190+
Transformation index
191+
forward: bool, optional
192+
Direction of mapping. Default is set to ``True``. If ``False``,
193+
the inverse transformation is applied.
194+
195+
Returns
196+
-------
197+
out: ndarray
198+
Transformed coordinates
199+
200+
Examples
201+
--------
202+
>>> xfm = Affine([[1, 0, 0, 1], [0, 1, 0, 2], [0, 0, 1, 3], [0, 0, 0, 1]])
203+
>>> xfm.map_point((0,0,0))
204+
array([1, 2, 3])
205+
206+
>>> xfm.map_point((0,0,0), forward=False)
207+
array([-1., -2., -3.])
208+
209+
"""
176210
coords = np.array(coords)
177211
if coords.shape[0] == self._matrix[index].shape[0] - 1:
178212
coords = np.append(coords, [1])

nitransforms/tests/test_base.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"""Tests of the base module"""
2+
import numpy as np
3+
4+
from ..base import ImageSpace
5+
6+
7+
def test_ImageSpace(get_data):
8+
im = get_data['RAS']
9+
10+
img = ImageSpace(im)
11+
assert np.all(img.affine == np.linalg.inv(img.inverse))
12+
13+
# nd index / coords
14+
idxs = img.ndindex
15+
coords = img.ndcoords
16+
assert len(idxs.shape) == len(coords.shape) == 2
17+
assert idxs.shape[0] == coords.shape[0] == img.ndim == 3
18+
assert idxs.shape[1] == coords.shape[1] == img.nvox == np.prod(im.shape)

nitransforms/tests/test_io.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,4 @@ def test_LinearTransformArray(tmpdir, data_path):
5353

5454
with open(outlta) as fp:
5555
lta2 = LTA.from_fileobj(fp)
56-
np.allclose(lta['xforms'][0]['m_L'], lta2['xforms'][0]['m_L'])
56+
assert np.allclose(lta['xforms'][0]['m_L'], lta2['xforms'][0]['m_L'])

nitransforms/tests/test_transform.py

+21
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,27 @@
3131
}
3232

3333

34+
def test_map_voxel(tmpdir, get_data):
35+
xfm = nbl.Affine([[1, 0, 0, 1], [0, 1, 0, 2], [0, 0, 1, 3], [0, 0, 0, 1]])
36+
with pytest.raises(ValueError):
37+
# reference / moving are not set
38+
xfm.map_voxel((0, 0, 0))
39+
40+
img = get_data['RAS']
41+
xfm.reference = img
42+
assert xfm.map_voxel((0, 0, 0)) == (2.75, 5.5, 8.25)
43+
del xfm
44+
45+
# use moving space as reference
46+
xfm = nbl.Affine([[1, 0, 0, 1], [0, 1, 0, 2], [0, 0, 1, 3], [0, 0, 0, 1]])
47+
mov = get_data['LPS']
48+
assert xfm.map_voxel((0, 0, 0), moving=mov) == (-2.75, -5.5, 8.25)
49+
50+
# use RAS space as reference
51+
xfm.reference = img
52+
assert xfm.map_voxel((0, 0, 0), moving=mov) == (0.75, 5.0, 8.25)
53+
54+
3455
@pytest.mark.xfail(reason="Not fully implemented")
3556
@pytest.mark.parametrize('image_orientation', [
3657
'RAS', 'LAS', 'LPS', # 'oblique',

0 commit comments

Comments
 (0)