Skip to content

Commit 2018e94

Browse files
committed
RF: Implement AFNIArrayProxy._get_scaled(), replacing other overrides
1 parent ea3e607 commit 2018e94

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

nibabel/brikhead.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -262,19 +262,20 @@ def __init__(self, file_like, header, mmap=True, keep_file_open=None):
262262
def scaling(self):
263263
return self._scaling
264264

265-
def __array__(self):
266-
raw_data = self.get_unscaled()
267-
# datatype may change if applying self._scaling
268-
return raw_data if self.scaling is None else raw_data * self.scaling
269-
270-
def __getitem__(self, slicer):
271-
raw_data = super(AFNIArrayProxy, self).__getitem__(slicer)
272-
# apply volume specific scaling (may change datatype!)
273-
if self.scaling is not None:
274-
fake_data = strided_scalar(self._shape)
275-
_, scaling = np.broadcast_arrays(fake_data, self.scaling)
276-
raw_data = raw_data * scaling[slicer]
277-
return raw_data
265+
def _get_scaled(self, dtype, slicer):
266+
raw_data = self._get_unscaled(slicer=slicer)
267+
if self.scaling is None:
268+
if dtype is None or raw_data.dtype >= np.dtype(dtype):
269+
return raw_data
270+
return np.asanyarray(raw_data, dtype=dtype)
271+
272+
if dtype is None:
273+
dtype = self.scaling.dtype
274+
275+
fake_data = strided_scalar(self._shape)
276+
_, scaling = np.broadcast_arrays(fake_data, self.scaling.astype(dtype))
277+
278+
return raw_data * scaling[slicer]
278279

279280

280281
class AFNIHeader(SpatialHeader):

0 commit comments

Comments
 (0)