Skip to content

Commit

Permalink
Merge pull request #197 from jmarabotto:Implement_ndim
Browse files Browse the repository at this point in the history
FIX: Update implementation of ``ndim`` property of transforms (#197)

Co-authored-by: Oscar Esteban <[email protected]>
Co-authored-by: Julien Marabotto <[email protected]>
  • Loading branch information
3 people authored Apr 19, 2024
1 parent 0027d1b commit 592f91b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
4 changes: 2 additions & 2 deletions nitransforms/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ def __ne__(self, other):
class TransformBase:
"""Abstract image class to represent transforms."""

__slots__ = ("_reference",)
__slots__ = ("_reference", "_ndim",)

def __init__(self, reference=None):
"""Instantiate a transform."""
Expand Down Expand Up @@ -220,7 +220,7 @@ def reference(self, image):
@property
def ndim(self):
"""Access the dimensions of the reference space."""
return self.reference.ndim
raise TypeError("TransformBase has no dimensions")

def apply(
self,
Expand Down
5 changes: 5 additions & 0 deletions nitransforms/linear.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,11 @@ def matrix(self):
"""Access the internal representation of this affine."""
return self._matrix

@property
def ndim(self):
"""Access the internal representation of this affine."""
return self._matrix.ndim + 1

def map(self, x, inverse=False):
r"""
Apply :math:`y = f(x)`.
Expand Down
12 changes: 10 additions & 2 deletions nitransforms/tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ def _to_hdf5(klass, x5_root):
# Test identity transform
xfm = TransformBase()
xfm.reference = fname
assert xfm.ndim == 3
with pytest.raises(TypeError):
_ = xfm.ndim
moved = xfm.apply(fname, order=0)
assert np.all(
imgdata == np.asanyarray(moved.dataobj, dtype=moved.get_data_dtype())
Expand All @@ -103,12 +104,19 @@ def _to_hdf5(klass, x5_root):
# Test identity transform - setting reference
xfm = TransformBase()
xfm.reference = fname
assert xfm.ndim == 3
with pytest.raises(TypeError):
_ = xfm.ndim
moved = xfm.apply(str(fname), reference=fname, order=0)
assert np.all(
imgdata == np.asanyarray(moved.dataobj, dtype=moved.get_data_dtype())
)

# Test ndim returned by affine
assert nitl.Affine().ndim == 3
assert nitl.LinearTransformsMapping(
[nitl.Affine(), nitl.Affine()]
).ndim == 4

# Test applying to Gifti
gii = nb.gifti.GiftiImage(
darrays=[
Expand Down

0 comments on commit 592f91b

Please sign in to comment.