Skip to content

Commit 283b648

Browse files
committed
TST - add tests for overflowing scaling
Confirm that intercepts and slopes causing overflow on the data during ``array_to_file`` will still scale as expected.
1 parent 2f891fa commit 283b648

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

nibabel/tests/test_utils.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,34 @@ def test_array_to_file():
160160
assert_array_equal(data_back, np.zeros(arr.shape))
161161

162162

163+
def test_a2f_big_scalers():
164+
# Check that clip works even for overflowing scalers / data
165+
info = np.finfo(np.float32)
166+
arr = np.array([info.min, np.nan, info.max], dtype=np.float32)
167+
str_io = BytesIO()
168+
# Intercept causes overflow - does routine scale correctly?
169+
array_to_file(arr, str_io, np.int8, intercept=np.float32(2**120))
170+
data_back = array_from_file(arr.shape, np.int8, str_io)
171+
assert_array_equal(data_back, [-128, 0, 127])
172+
# Scales also if mx, mn specified?
173+
str_io.seek(0)
174+
array_to_file(arr, str_io, np.int8, mn=info.min, mx=info.max,
175+
intercept=np.float32(2**120))
176+
data_back = array_from_file(arr.shape, np.int8, str_io)
177+
assert_array_equal(data_back, [-128, 0, 127])
178+
# And if slope causes overflow?
179+
str_io.seek(0)
180+
array_to_file(arr, str_io, np.int8, divslope=np.float32(0.5))
181+
data_back = array_from_file(arr.shape, np.int8, str_io)
182+
assert_array_equal(data_back, [-128, 0, 127])
183+
# with mn, mx specified?
184+
str_io.seek(0)
185+
array_to_file(arr, str_io, np.int8, mn=info.min, mx=info.max,
186+
divslope=np.float32(0.5))
187+
data_back = array_from_file(arr.shape, np.int8, str_io)
188+
assert_array_equal(data_back, [-128, 0, 127])
189+
190+
163191
def write_return(data, fileobj, out_dtype, *args, **kwargs):
164192
fileobj.truncate(0)
165193
array_to_file(data, fileobj, out_dtype, *args, **kwargs)

0 commit comments

Comments
 (0)