Skip to content

Commit 63bcf29

Browse files
Bago AmirbekianBago Amirbekian
authored andcommitted
BF - only check dtype min-max is clip is needed
1 parent e49e33c commit 63bcf29

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

nibabel/tests/test_analyze.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,14 @@ def test_read_write_data(self):
300300
_write_data(hdr, data, S3)
301301
data_back = hdr.data_from_fileobj(S3)
302302
assert_false(np.allclose(data, data_back))
303+
# Test RGB image
304+
dtype = np.dtype([('R', 'uint8'), ('G', 'uint8'), ('B', 'uint8')])
305+
data = np.ones((1, 2, 3), dtype)
306+
hdr.set_data_dtype(dtype)
307+
S4 = BytesIO()
308+
hdr.data_to_fileobj(data, S4)
309+
data_back = hdr.data_from_fileobj(S4)
310+
assert_array_equal(data, data_back)
303311

304312
def test_datatype(self):
305313
ehdr = self.header_class()

nibabel/volumeutils.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,8 @@ def array_to_file(data, fileobj, out_dtype=None, offset=0,
601601
if not needs_f2i:
602602
# Apply min max thresholding the standard way
603603
needs_pre_clip = (mn, mx) != (None, None)
604-
mn, mx = _dt_min_max(in_dtype, mn, mx)
604+
if needs_pre_clip:
605+
mn, mx = _dt_min_max(in_dtype, mn, mx)
605606
else: # We do need float to int machinery
606607
# Replace Nones in (mn, mx) with type min / max if necessary
607608
dt_mnmx = _dt_min_max(in_dtype, mn, mx)
@@ -670,6 +671,8 @@ def _dt_min_max(dtype_like, mn, mx):
670671
elif dt.kind in 'iu':
671672
info = np.iinfo(dt)
672673
mnmx = (info.min, info.max)
674+
else:
675+
raise NotImplementedError("unknown dtype")
673676
return mnmx[0] if mn is None else mn, mnmx[1] if mx is None else mx
674677

675678

0 commit comments

Comments
 (0)