Skip to content

ENH: Add ndim to ArrayProxy and DataobjImage #674

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

Merged
merged 4 commits into from
Oct 12, 2018
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
4 changes: 4 additions & 0 deletions nibabel/arrayproxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,10 @@ def header(self):
def shape(self):
return self._shape

@property
def ndim(self):
return len(self.shape)

@property
def dtype(self):
return self._dtype
Expand Down
8 changes: 6 additions & 2 deletions nibabel/dataobj_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ def __init__(self, dataobj, header=None, extra=None, file_map=None):
----------
dataobj : object
Object containg image data. It should be some object that retuns an
array from ``np.asanyarray``. It should have a ``shape`` attribute
or property
array from ``np.asanyarray``. It should have ``shape`` and ``ndim``
attributes or properties
header : None or mapping or header instance, optional
metadata for this image format
extra : None or mapping, optional
Expand Down Expand Up @@ -392,6 +392,10 @@ def uncache(self):
def shape(self):
return self._dataobj.shape

@property
def ndim(self):
return self._dataobj.ndim

@deprecate_with_version('get_shape method is deprecated.\n'
'Please use the ``img.shape`` property '
'instead.',
Expand Down
4 changes: 4 additions & 0 deletions nibabel/ecat.py
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,10 @@ def __init__(self, subheader):
def shape(self):
return self._shape

@property
def ndim(self):
return len(self.shape)

@property
def is_proxy(self):
return True
Expand Down
4 changes: 4 additions & 0 deletions nibabel/minc1.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,10 @@ def __init__(self, minc_file):
def shape(self):
return self._shape

@property
def ndim(self):
return len(self.shape)

@property
def is_proxy(self):
return True
Expand Down
4 changes: 4 additions & 0 deletions nibabel/parrec.py
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,10 @@ def __init__(self, file_like, header, mmap=True, scaling='dv'):
def shape(self):
return self._shape

@property
def ndim(self):
return len(self.shape)

@property
def dtype(self):
return self._dtype
Expand Down
14 changes: 14 additions & 0 deletions nibabel/tests/test_image_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ def validate_data_interface(self, imaker, params):
# Check get data returns array, and caches
img = imaker()
assert_equal(img.shape, img.dataobj.shape)
assert_equal(img.ndim, len(img.shape))
assert_data_similar(img.dataobj, params)
for meth_name in self.meth_names:
if params['is_proxy']:
Expand All @@ -210,6 +211,8 @@ def validate_data_interface(self, imaker, params):
self._check_array_interface(imaker, meth_name)
# Data shape is same as image shape
assert_equal(img.shape, getattr(img, meth_name)().shape)
# Data ndim is same as image ndim
assert_equal(img.ndim, getattr(img, meth_name)().ndim)
# Values to get_data caching parameter must be 'fill' or
# 'unchanged'
assert_raises(ValueError, img.get_data, caching='something')
Expand Down Expand Up @@ -394,6 +397,17 @@ def validate_shape(self, imaker, params):
# Read only
assert_raises(AttributeError, setattr, img, 'shape', np.eye(4))

def validate_ndim(self, imaker, params):
# Validate shape
img = imaker()
# Same as expected ndim
assert_equal(img.ndim, len(params['shape']))
# Same as array ndim if passed
if 'data' in params:
assert_equal(img.ndim, params['data'].ndim)
# Read only
assert_raises(AttributeError, setattr, img, 'ndim', 5)

def validate_shape_deprecated(self, imaker, params):
# Check deprecated get_shape API
img = imaker()
Expand Down
8 changes: 8 additions & 0 deletions nibabel/tests/test_proxy_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,14 @@ def validate_shape(self, pmaker, params):
# Read only
assert_raises(AttributeError, setattr, prox, 'shape', params['shape'])

def validate_ndim(self, pmaker, params):
# Check shape
prox, fio, hdr = pmaker()
assert_equal(prox.ndim, len(params['shape']))
# Read only
assert_raises(AttributeError, setattr, prox,
'ndim', len(params['shape']))

def validate_is_proxy(self, pmaker, params):
# Check shape
prox, fio, hdr = pmaker()
Expand Down