Skip to content

Commit 791678e

Browse files
authored
Merge pull request #674 from effigies/enh/arrayproxy_ndim
ENH: Add ndim to ArrayProxy and DataobjImage
2 parents 679aa5b + ce210bd commit 791678e

File tree

7 files changed

+44
-2
lines changed

7 files changed

+44
-2
lines changed

nibabel/arrayproxy.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,10 @@ def header(self):
232232
def shape(self):
233233
return self._shape
234234

235+
@property
236+
def ndim(self):
237+
return len(self.shape)
238+
235239
@property
236240
def dtype(self):
237241
return self._dtype

nibabel/dataobj_images.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ def __init__(self, dataobj, header=None, extra=None, file_map=None):
2828
----------
2929
dataobj : object
3030
Object containg image data. It should be some object that retuns an
31-
array from ``np.asanyarray``. It should have a ``shape`` attribute
32-
or property
31+
array from ``np.asanyarray``. It should have ``shape`` and ``ndim``
32+
attributes or properties
3333
header : None or mapping or header instance, optional
3434
metadata for this image format
3535
extra : None or mapping, optional
@@ -392,6 +392,10 @@ def uncache(self):
392392
def shape(self):
393393
return self._dataobj.shape
394394

395+
@property
396+
def ndim(self):
397+
return self._dataobj.ndim
398+
395399
@deprecate_with_version('get_shape method is deprecated.\n'
396400
'Please use the ``img.shape`` property '
397401
'instead.',

nibabel/ecat.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,10 @@ def __init__(self, subheader):
680680
def shape(self):
681681
return self._shape
682682

683+
@property
684+
def ndim(self):
685+
return len(self.shape)
686+
683687
@property
684688
def is_proxy(self):
685689
return True

nibabel/minc1.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,10 @@ def __init__(self, minc_file):
252252
def shape(self):
253253
return self._shape
254254

255+
@property
256+
def ndim(self):
257+
return len(self.shape)
258+
255259
@property
256260
def is_proxy(self):
257261
return True

nibabel/parrec.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,10 @@ def __init__(self, file_like, header, mmap=True, scaling='dv'):
622622
def shape(self):
623623
return self._shape
624624

625+
@property
626+
def ndim(self):
627+
return len(self.shape)
628+
625629
@property
626630
def dtype(self):
627631
return self._dtype

nibabel/tests/test_image_api.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ def validate_data_interface(self, imaker, params):
202202
# Check get data returns array, and caches
203203
img = imaker()
204204
assert_equal(img.shape, img.dataobj.shape)
205+
assert_equal(img.ndim, len(img.shape))
205206
assert_data_similar(img.dataobj, params)
206207
for meth_name in self.meth_names:
207208
if params['is_proxy']:
@@ -210,6 +211,8 @@ def validate_data_interface(self, imaker, params):
210211
self._check_array_interface(imaker, meth_name)
211212
# Data shape is same as image shape
212213
assert_equal(img.shape, getattr(img, meth_name)().shape)
214+
# Data ndim is same as image ndim
215+
assert_equal(img.ndim, getattr(img, meth_name)().ndim)
213216
# Values to get_data caching parameter must be 'fill' or
214217
# 'unchanged'
215218
assert_raises(ValueError, img.get_data, caching='something')
@@ -394,6 +397,17 @@ def validate_shape(self, imaker, params):
394397
# Read only
395398
assert_raises(AttributeError, setattr, img, 'shape', np.eye(4))
396399

400+
def validate_ndim(self, imaker, params):
401+
# Validate shape
402+
img = imaker()
403+
# Same as expected ndim
404+
assert_equal(img.ndim, len(params['shape']))
405+
# Same as array ndim if passed
406+
if 'data' in params:
407+
assert_equal(img.ndim, params['data'].ndim)
408+
# Read only
409+
assert_raises(AttributeError, setattr, img, 'ndim', 5)
410+
397411
def validate_shape_deprecated(self, imaker, params):
398412
# Check deprecated get_shape API
399413
img = imaker()

nibabel/tests/test_proxy_api.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,14 @@ def validate_shape(self, pmaker, params):
108108
# Read only
109109
assert_raises(AttributeError, setattr, prox, 'shape', params['shape'])
110110

111+
def validate_ndim(self, pmaker, params):
112+
# Check shape
113+
prox, fio, hdr = pmaker()
114+
assert_equal(prox.ndim, len(params['shape']))
115+
# Read only
116+
assert_raises(AttributeError, setattr, prox,
117+
'ndim', len(params['shape']))
118+
111119
def validate_is_proxy(self, pmaker, params):
112120
# Check shape
113121
prox, fio, hdr = pmaker()

0 commit comments

Comments
 (0)