Skip to content

Commit 592f91b

Browse files
jmarabottooestebanJulien Marabotto
authored
Merge pull request #197 from jmarabotto:Implement_ndim
FIX: Update implementation of ``ndim`` property of transforms (#197) Co-authored-by: Oscar Esteban <[email protected]> Co-authored-by: Julien Marabotto <[email protected]>
1 parent 0027d1b commit 592f91b

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

nitransforms/base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ def __ne__(self, other):
178178
class TransformBase:
179179
"""Abstract image class to represent transforms."""
180180

181-
__slots__ = ("_reference",)
181+
__slots__ = ("_reference", "_ndim",)
182182

183183
def __init__(self, reference=None):
184184
"""Instantiate a transform."""
@@ -220,7 +220,7 @@ def reference(self, image):
220220
@property
221221
def ndim(self):
222222
"""Access the dimensions of the reference space."""
223-
return self.reference.ndim
223+
raise TypeError("TransformBase has no dimensions")
224224

225225
def apply(
226226
self,

nitransforms/linear.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,11 @@ def matrix(self):
143143
"""Access the internal representation of this affine."""
144144
return self._matrix
145145

146+
@property
147+
def ndim(self):
148+
"""Access the internal representation of this affine."""
149+
return self._matrix.ndim + 1
150+
146151
def map(self, x, inverse=False):
147152
r"""
148153
Apply :math:`y = f(x)`.

nitransforms/tests/test_base.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ def _to_hdf5(klass, x5_root):
9494
# Test identity transform
9595
xfm = TransformBase()
9696
xfm.reference = fname
97-
assert xfm.ndim == 3
97+
with pytest.raises(TypeError):
98+
_ = xfm.ndim
9899
moved = xfm.apply(fname, order=0)
99100
assert np.all(
100101
imgdata == np.asanyarray(moved.dataobj, dtype=moved.get_data_dtype())
@@ -103,12 +104,19 @@ def _to_hdf5(klass, x5_root):
103104
# Test identity transform - setting reference
104105
xfm = TransformBase()
105106
xfm.reference = fname
106-
assert xfm.ndim == 3
107+
with pytest.raises(TypeError):
108+
_ = xfm.ndim
107109
moved = xfm.apply(str(fname), reference=fname, order=0)
108110
assert np.all(
109111
imgdata == np.asanyarray(moved.dataobj, dtype=moved.get_data_dtype())
110112
)
111113

114+
# Test ndim returned by affine
115+
assert nitl.Affine().ndim == 3
116+
assert nitl.LinearTransformsMapping(
117+
[nitl.Affine(), nitl.Affine()]
118+
).ndim == 4
119+
112120
# Test applying to Gifti
113121
gii = nb.gifti.GiftiImage(
114122
darrays=[

0 commit comments

Comments
 (0)