From 7e038071eed1f1811ff579e06e8cd31802222f0d Mon Sep 17 00:00:00 2001 From: "Christopher J. Markiewicz" Date: Wed, 3 Oct 2018 11:25:22 -0400 Subject: [PATCH 1/4] ENH: Add ndim to ArrayProxy protocol --- nibabel/arrayproxy.py | 4 ++++ nibabel/ecat.py | 4 ++++ nibabel/minc1.py | 4 ++++ nibabel/parrec.py | 4 ++++ 4 files changed, 16 insertions(+) diff --git a/nibabel/arrayproxy.py b/nibabel/arrayproxy.py index b3faa21a1f..084a74f0b2 100644 --- a/nibabel/arrayproxy.py +++ b/nibabel/arrayproxy.py @@ -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 diff --git a/nibabel/ecat.py b/nibabel/ecat.py index 3c0957e11d..c2d343f739 100644 --- a/nibabel/ecat.py +++ b/nibabel/ecat.py @@ -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 diff --git a/nibabel/minc1.py b/nibabel/minc1.py index 5eb077ada0..57042f32f0 100644 --- a/nibabel/minc1.py +++ b/nibabel/minc1.py @@ -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 diff --git a/nibabel/parrec.py b/nibabel/parrec.py index 5fd460b4e1..87e1ac81e6 100644 --- a/nibabel/parrec.py +++ b/nibabel/parrec.py @@ -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 From c940deaf783b6506c20334a164aa84c639e72a4f Mon Sep 17 00:00:00 2001 From: "Christopher J. Markiewicz" Date: Wed, 3 Oct 2018 11:25:57 -0400 Subject: [PATCH 2/4] TEST: Validate expected ndim behavior --- nibabel/tests/test_image_api.py | 1 + nibabel/tests/test_proxy_api.py | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/nibabel/tests/test_image_api.py b/nibabel/tests/test_image_api.py index c53b012cc2..761c9750c7 100644 --- a/nibabel/tests/test_image_api.py +++ b/nibabel/tests/test_image_api.py @@ -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(len(img.shape), img.dataobj.ndim) assert_data_similar(img.dataobj, params) for meth_name in self.meth_names: if params['is_proxy']: diff --git a/nibabel/tests/test_proxy_api.py b/nibabel/tests/test_proxy_api.py index 285674083b..7280c5552d 100644 --- a/nibabel/tests/test_proxy_api.py +++ b/nibabel/tests/test_proxy_api.py @@ -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() From d2d07d41d2f430eda40691a7efe07c09b6bfb695 Mon Sep 17 00:00:00 2001 From: "Christopher J. Markiewicz" Date: Thu, 4 Oct 2018 09:13:15 -0400 Subject: [PATCH 3/4] ENH: Pass ndim through to DataobjImage --- nibabel/dataobj_images.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/nibabel/dataobj_images.py b/nibabel/dataobj_images.py index 66043858d6..86185a7aef 100644 --- a/nibabel/dataobj_images.py +++ b/nibabel/dataobj_images.py @@ -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 @@ -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.', From ce210bd61101292a6ee6856eb2f1746b48aeba17 Mon Sep 17 00:00:00 2001 From: "Christopher J. Markiewicz" Date: Thu, 4 Oct 2018 09:17:09 -0400 Subject: [PATCH 4/4] TEST: Validate DataobjImage.ndim --- nibabel/tests/test_image_api.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/nibabel/tests/test_image_api.py b/nibabel/tests/test_image_api.py index 761c9750c7..ba51878715 100644 --- a/nibabel/tests/test_image_api.py +++ b/nibabel/tests/test_image_api.py @@ -202,7 +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(len(img.shape), img.dataobj.ndim) + assert_equal(img.ndim, len(img.shape)) assert_data_similar(img.dataobj, params) for meth_name in self.meth_names: if params['is_proxy']: @@ -211,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') @@ -395,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()