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: increase coverage #23

Merged
merged 3 commits into from
Oct 22, 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
36 changes: 35 additions & 1 deletion nitransforms/linear.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,13 @@ def resample(self, moving, order=3, mode='constant', cval=0.0, prefilter=True,

Examples
--------
>>> a = Affine()
>>> a.matrix
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]])
>>> ref = nb.load(testfile)
>>> xfm.reference = ref
Expand Down Expand Up @@ -172,7 +179,34 @@ def resample(self, moving, order=3, mode='constant', cval=0.0, prefilter=True,
return moved_image

def map_point(self, coords, index=0, forward=True):
"""Apply y = f(x), where x is the argument `coords`."""
"""
Apply y = f(x), where x is the argument `coords`.

Parameters
----------
coords : array_like
RAS coordinates to map
index : int, optional
Transformation index
forward: bool, optional
Direction of mapping. Default is set to ``True``. If ``False``,
the inverse transformation is applied.

Returns
-------
out: ndarray
Transformed coordinates

Examples
--------
>>> xfm = Affine([[1, 0, 0, 1], [0, 1, 0, 2], [0, 0, 1, 3], [0, 0, 0, 1]])
>>> xfm.map_point((0,0,0))
array([1, 2, 3])

>>> xfm.map_point((0,0,0), forward=False)
array([-1., -2., -3.])

"""
coords = np.array(coords)
if coords.shape[0] == self._matrix[index].shape[0] - 1:
coords = np.append(coords, [1])
Expand Down
18 changes: 18 additions & 0 deletions nitransforms/tests/test_base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"""Tests of the base module"""
import numpy as np

from ..base import ImageSpace


def test_ImageSpace(get_data):
im = get_data['RAS']

img = ImageSpace(im)
assert np.all(img.affine == np.linalg.inv(img.inverse))

# nd index / coords
idxs = img.ndindex
coords = img.ndcoords
assert len(idxs.shape) == len(coords.shape) == 2
assert idxs.shape[0] == coords.shape[0] == img.ndim == 3
assert idxs.shape[1] == coords.shape[1] == img.nvox == np.prod(im.shape)
2 changes: 1 addition & 1 deletion nitransforms/tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,4 @@ def test_LinearTransformArray(tmpdir, data_path):

with open(outlta) as fp:
lta2 = LTA.from_fileobj(fp)
np.allclose(lta['xforms'][0]['m_L'], lta2['xforms'][0]['m_L'])
assert np.allclose(lta['xforms'][0]['m_L'], lta2['xforms'][0]['m_L'])
21 changes: 21 additions & 0 deletions nitransforms/tests/test_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,27 @@
}


def test_map_voxel(tmpdir, get_data):
xfm = nbl.Affine([[1, 0, 0, 1], [0, 1, 0, 2], [0, 0, 1, 3], [0, 0, 0, 1]])
with pytest.raises(ValueError):
# reference / moving are not set
xfm.map_voxel((0, 0, 0))

img = get_data['RAS']
xfm.reference = img
assert xfm.map_voxel((0, 0, 0)) == (2.75, 5.5, 8.25)
del xfm

# use moving space as reference
xfm = nbl.Affine([[1, 0, 0, 1], [0, 1, 0, 2], [0, 0, 1, 3], [0, 0, 0, 1]])
mov = get_data['LPS']
assert xfm.map_voxel((0, 0, 0), moving=mov) == (-2.75, -5.5, 8.25)

# use RAS space as reference
xfm.reference = img
assert xfm.map_voxel((0, 0, 0), moving=mov) == (0.75, 5.0, 8.25)


@pytest.mark.xfail(reason="Not fully implemented")
@pytest.mark.parametrize('image_orientation', [
'RAS', 'LAS', 'LPS', # 'oblique',
Expand Down