Skip to content

Commit df47554

Browse files
matthew-bretteffigies
authored andcommitted
NF+TST: add dtype attribute to Analyze arrayproxy
Add read-only dtype property to ArrayProxy class, and test with proxy api tests, refactoring the dtype checking a bit on the way.
1 parent 3d21376 commit df47554

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

nibabel/arrayproxy.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,12 @@ def shape(self):
113113
return self._shape
114114

115115
@property
116-
def is_proxy(self):
117-
return True
116+
def dtype(self):
117+
return self._dtype
118+
119+
@property
120+
def offset(self):
121+
return self._offset
118122

119123
@property
120124
def slope(self):
@@ -125,8 +129,8 @@ def inter(self):
125129
return self._inter
126130

127131
@property
128-
def offset(self):
129-
return self._offset
132+
def is_proxy(self):
133+
return True
130134

131135
def get_unscaled(self):
132136
''' Read of data from file

nibabel/tests/test_proxy_api.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959

6060
from numpy.testing import (assert_almost_equal, assert_array_equal)
6161

62-
from ..testing import data_path as DATA_PATH
62+
from ..testing import data_path as DATA_PATH, assert_dt_equal
6363

6464
from ..tmpdirs import InTemporaryDirectory
6565

@@ -122,7 +122,7 @@ def validate_asarray(self, pmaker, params):
122122
prox, fio, hdr = pmaker()
123123
out = np.asarray(prox)
124124
assert_array_equal(out, params['arr_out'])
125-
assert_equal(out.dtype.type, params['dtype_out'])
125+
assert_dt_equal(out.dtype, params['dtype_out'])
126126
# Shape matches expected shape
127127
assert_equal(out.shape, params['shape'])
128128

@@ -204,8 +204,8 @@ def obj_params(self):
204204
slopes,
205205
inters):
206206
n_els = np.prod(shape)
207-
dt = np.dtype(dtype).newbyteorder(self.data_endian)
208-
arr = np.arange(n_els, dtype=dt).reshape(shape)
207+
dtype = np.dtype(dtype).newbyteorder(self.data_endian)
208+
arr = np.arange(n_els, dtype=dtype).reshape(shape)
209209
data = arr.tostring(order=self.array_order)
210210
hdr = self.header_class()
211211
hdr.set_data_dtype(dtype)
@@ -264,6 +264,13 @@ def fname_func():
264264
params = params.copy()
265265
yield fname_func, params
266266

267+
def validate_dtype(self, pmaker, params):
268+
# Read-only dtype attribute
269+
prox, fio, hdr = pmaker()
270+
assert_dt_equal(prox.dtype, params['dtype'])
271+
assert_raises(AttributeError,
272+
prox.__setattr__, 'dtype', np.dtype(prox.dtype))
273+
267274
def validate_slope_inter_offset(self, pmaker, params):
268275
# Check slope, inter, offset
269276
prox, fio, hdr = pmaker()

0 commit comments

Comments
 (0)