Skip to content

Commit

Permalink
TEST: Better warnings checks, rather than comments for future improve…
Browse files Browse the repository at this point in the history
…ments
  • Loading branch information
effigies committed Jun 18, 2022
1 parent 4e77024 commit 7d3979d
Showing 1 changed file with 18 additions and 20 deletions.
38 changes: 18 additions & 20 deletions nibabel/tests/test_volumeutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import bz2
import threading
import time
from packaging.version import Version

import numpy as np

Expand Down Expand Up @@ -68,6 +69,8 @@
IUINT_TYPES = INT_TYPES + np.sctypes['uint']
NUMERIC_TYPES = CFLOAT_TYPES + IUINT_TYPES

FP_RUNTIME_WARN = Version(np.__version__) >= Version('1.24.0.dev0+239')


def test__is_compressed_fobj():
# _is_compressed helper function
Expand Down Expand Up @@ -672,50 +675,45 @@ def test_a2f_nan2zero_range():
arr = np.array([-1, 0, 1, np.nan], dtype=dt)
# Error occurs for arrays without nans too
arr_no_nan = np.array([-1, 0, 1, 2], dtype=dt)
warn_type = np.ComplexWarning if np.issubdtype(dt, np.complexfloating) else None
complex_warn = (np.ComplexWarning,) if np.issubdtype(dt, np.complexfloating) else ()
# Casting nan to int will produce a RuntimeWarning in numpy 1.24
nan_warn = (RuntimeWarning,) if FP_RUNTIME_WARN else ()
c_and_n_warn = complex_warn + nan_warn
# No errors from explicit thresholding
# mn thresholding excluding zero
with pytest.warns(warn_type) if warn_type else error_warnings():
with pytest.warns(complex_warn) if complex_warn else error_warnings():
assert_array_equal([1, 1, 1, 0],
write_return(arr, fobj, np.int8, mn=1))
# mx thresholding excluding zero
with pytest.warns(warn_type) if warn_type else error_warnings():
with pytest.warns(complex_warn) if complex_warn else error_warnings():
assert_array_equal([-1, -1, -1, 0],
write_return(arr, fobj, np.int8, mx=-1))
# Errors from datatype threshold after scaling
with pytest.warns(warn_type) if warn_type else error_warnings():
with pytest.warns(complex_warn) if complex_warn else error_warnings():
back_arr = write_return(arr, fobj, np.int8, intercept=128)
assert_array_equal([-128, -128, -127, -128], back_arr)
with pytest.raises(ValueError):
write_return(arr, fobj, np.int8, intercept=129)
with pytest.raises(ValueError):
write_return(arr_no_nan, fobj, np.int8, intercept=129)
# OK with nan2zero false, but we get whatever nan casts to
with pytest.warns(warn_type) if warn_type else error_warnings():
# XXX NP1.24
# Casting nan to int will produce a RuntimeWarning in numpy 1.24
# Change to expecting this warning when this becomes our minimum
with np.errstate(invalid='ignore'):
nan_cast = np.array(np.nan, dtype=dt).astype(np.int8)
with pytest.warns(warn_type) if warn_type else error_warnings():
# XXX NP1.24 - expect RuntimeWarning
with np.errstate(invalid='ignore'):
back_arr = write_return(arr, fobj, np.int8, intercept=129, nan2zero=False)
with pytest.warns(c_and_n_warn) if c_and_n_warn else error_warnings():
nan_cast = np.array(np.nan, dtype=dt).astype(np.int8)
with pytest.warns(c_and_n_warn) if c_and_n_warn else error_warnings():
back_arr = write_return(arr, fobj, np.int8, intercept=129, nan2zero=False)
assert_array_equal([-128, -128, -128, nan_cast], back_arr)
# divslope
with pytest.warns(warn_type) if warn_type else error_warnings():
with pytest.warns(complex_warn) if complex_warn else error_warnings():
back_arr = write_return(arr, fobj, np.int8, intercept=256, divslope=2)
assert_array_equal([-128, -128, -128, -128], back_arr)
with pytest.raises(ValueError):
write_return(arr, fobj, np.int8, intercept=257.1, divslope=2)
with pytest.raises(ValueError):
write_return(arr_no_nan, fobj, np.int8, intercept=257.1, divslope=2)
# OK with nan2zero false
with pytest.warns(warn_type) if warn_type else error_warnings():
# XXX NP1.24 - expect RuntimeWarning
with np.errstate(invalid='ignore'):
back_arr = write_return(arr, fobj, np.int8,
intercept=257.1, divslope=2, nan2zero=False)
with pytest.warns(c_and_n_warn) if c_and_n_warn else error_warnings():
back_arr = write_return(arr, fobj, np.int8,
intercept=257.1, divslope=2, nan2zero=False)
assert_array_equal([-128, -128, -128, nan_cast], back_arr)


Expand Down

0 comments on commit 7d3979d

Please sign in to comment.