diff --git a/nitransforms/base.py b/nitransforms/base.py index 68b97f75..96f00edb 100644 --- a/nitransforms/base.py +++ b/nitransforms/base.py @@ -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.""" @@ -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, diff --git a/nitransforms/linear.py b/nitransforms/linear.py index eb4a95d7..af14f396 100644 --- a/nitransforms/linear.py +++ b/nitransforms/linear.py @@ -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)`. diff --git a/nitransforms/tests/test_base.py b/nitransforms/tests/test_base.py index 8422ca10..07a7e4ec 100644 --- a/nitransforms/tests/test_base.py +++ b/nitransforms/tests/test_base.py @@ -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()) @@ -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=[