Skip to content

Commit 29a68dd

Browse files
committed
RF: Uniformize AFNI/PARREC scaling
1 parent c0237f1 commit 29a68dd

File tree

2 files changed

+13
-17
lines changed

2 files changed

+13
-17
lines changed

nibabel/brikhead.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -265,17 +265,21 @@ def scaling(self):
265265
def _get_scaled(self, dtype, slicer):
266266
raw_data = self._get_unscaled(slicer=slicer)
267267
if self.scaling is None:
268-
if dtype is None or raw_data.dtype >= np.dtype(dtype):
268+
if dtype is None:
269269
return raw_data
270-
return np.asanyarray(raw_data, dtype=dtype)
271-
272-
if dtype is None:
273-
dtype = self.scaling.dtype
270+
final_type = np.promote_types(raw_data.dtype, dtype)
271+
return raw_data.astype(final_type, copy=False)
274272

273+
# Broadcast scaling to shape of original data
275274
fake_data = strided_scalar(self._shape)
276-
_, scaling = np.broadcast_arrays(fake_data, self.scaling.astype(dtype))
275+
_, scaling = np.broadcast_arrays(fake_data, self.scaling)
276+
277+
final_type = np.result_type(raw_data, scaling)
278+
if dtype is not None:
279+
final_type = np.promote_types(final_type, dtype)
277280

278-
return raw_data * scaling[slicer]
281+
# Slice scaling to give output shape
282+
return raw_data * scaling[slicer].astype(final_type)
279283

280284

281285
class AFNIHeader(SpatialHeader):

nibabel/parrec.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -664,23 +664,15 @@ def _get_scaled(self, dtype, slicer):
664664
return raw_data.astype(final_type, copy=False)
665665

666666
# Broadcast scaling to shape of original data
667-
slopes, inters = self._slice_scaling
668667
fake_data = strided_scalar(self._shape)
669-
_, slopes, inters = np.broadcast_arrays(fake_data, slopes, inters)
668+
_, slopes, inters = np.broadcast_arrays(fake_data, *self._slice_scaling)
670669

671670
final_type = np.result_type(raw_data, slopes, inters)
672671
if dtype is not None:
673672
final_type = np.promote_types(final_type, dtype)
674673

675-
slopes = slopes.astype(final_type)
676-
inters = inters.astype(final_type)
677-
678-
if slicer is not None:
679-
slopes = slopes[slicer]
680-
inters = inters[slicer]
681-
682674
# Slice scaling to give output shape
683-
return raw_data * slopes + inters
675+
return raw_data * slopes[slicer].astype(final_type) + inters[slicer].astype(final_type)
684676

685677

686678
def get_scaled(self, dtype=None):

0 commit comments

Comments
 (0)