Skip to content

Commit 7d3979d

Browse files
committed
TEST: Better warnings checks, rather than comments for future improvements
1 parent 4e77024 commit 7d3979d

File tree

1 file changed

+18
-20
lines changed

1 file changed

+18
-20
lines changed

nibabel/tests/test_volumeutils.py

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import bz2
2121
import threading
2222
import time
23+
from packaging.version import Version
2324

2425
import numpy as np
2526

@@ -68,6 +69,8 @@
6869
IUINT_TYPES = INT_TYPES + np.sctypes['uint']
6970
NUMERIC_TYPES = CFLOAT_TYPES + IUINT_TYPES
7071

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

7275
def test__is_compressed_fobj():
7376
# _is_compressed helper function
@@ -672,50 +675,45 @@ def test_a2f_nan2zero_range():
672675
arr = np.array([-1, 0, 1, np.nan], dtype=dt)
673676
# Error occurs for arrays without nans too
674677
arr_no_nan = np.array([-1, 0, 1, 2], dtype=dt)
675-
warn_type = np.ComplexWarning if np.issubdtype(dt, np.complexfloating) else None
678+
complex_warn = (np.ComplexWarning,) if np.issubdtype(dt, np.complexfloating) else ()
679+
# Casting nan to int will produce a RuntimeWarning in numpy 1.24
680+
nan_warn = (RuntimeWarning,) if FP_RUNTIME_WARN else ()
681+
c_and_n_warn = complex_warn + nan_warn
676682
# No errors from explicit thresholding
677683
# mn thresholding excluding zero
678-
with pytest.warns(warn_type) if warn_type else error_warnings():
684+
with pytest.warns(complex_warn) if complex_warn else error_warnings():
679685
assert_array_equal([1, 1, 1, 0],
680686
write_return(arr, fobj, np.int8, mn=1))
681687
# mx thresholding excluding zero
682-
with pytest.warns(warn_type) if warn_type else error_warnings():
688+
with pytest.warns(complex_warn) if complex_warn else error_warnings():
683689
assert_array_equal([-1, -1, -1, 0],
684690
write_return(arr, fobj, np.int8, mx=-1))
685691
# Errors from datatype threshold after scaling
686-
with pytest.warns(warn_type) if warn_type else error_warnings():
692+
with pytest.warns(complex_warn) if complex_warn else error_warnings():
687693
back_arr = write_return(arr, fobj, np.int8, intercept=128)
688694
assert_array_equal([-128, -128, -127, -128], back_arr)
689695
with pytest.raises(ValueError):
690696
write_return(arr, fobj, np.int8, intercept=129)
691697
with pytest.raises(ValueError):
692698
write_return(arr_no_nan, fobj, np.int8, intercept=129)
693699
# OK with nan2zero false, but we get whatever nan casts to
694-
with pytest.warns(warn_type) if warn_type else error_warnings():
695-
# XXX NP1.24
696-
# Casting nan to int will produce a RuntimeWarning in numpy 1.24
697-
# Change to expecting this warning when this becomes our minimum
698-
with np.errstate(invalid='ignore'):
699-
nan_cast = np.array(np.nan, dtype=dt).astype(np.int8)
700-
with pytest.warns(warn_type) if warn_type else error_warnings():
701-
# XXX NP1.24 - expect RuntimeWarning
702-
with np.errstate(invalid='ignore'):
703-
back_arr = write_return(arr, fobj, np.int8, intercept=129, nan2zero=False)
700+
with pytest.warns(c_and_n_warn) if c_and_n_warn else error_warnings():
701+
nan_cast = np.array(np.nan, dtype=dt).astype(np.int8)
702+
with pytest.warns(c_and_n_warn) if c_and_n_warn else error_warnings():
703+
back_arr = write_return(arr, fobj, np.int8, intercept=129, nan2zero=False)
704704
assert_array_equal([-128, -128, -128, nan_cast], back_arr)
705705
# divslope
706-
with pytest.warns(warn_type) if warn_type else error_warnings():
706+
with pytest.warns(complex_warn) if complex_warn else error_warnings():
707707
back_arr = write_return(arr, fobj, np.int8, intercept=256, divslope=2)
708708
assert_array_equal([-128, -128, -128, -128], back_arr)
709709
with pytest.raises(ValueError):
710710
write_return(arr, fobj, np.int8, intercept=257.1, divslope=2)
711711
with pytest.raises(ValueError):
712712
write_return(arr_no_nan, fobj, np.int8, intercept=257.1, divslope=2)
713713
# OK with nan2zero false
714-
with pytest.warns(warn_type) if warn_type else error_warnings():
715-
# XXX NP1.24 - expect RuntimeWarning
716-
with np.errstate(invalid='ignore'):
717-
back_arr = write_return(arr, fobj, np.int8,
718-
intercept=257.1, divslope=2, nan2zero=False)
714+
with pytest.warns(c_and_n_warn) if c_and_n_warn else error_warnings():
715+
back_arr = write_return(arr, fobj, np.int8,
716+
intercept=257.1, divslope=2, nan2zero=False)
719717
assert_array_equal([-128, -128, -128, nan_cast], back_arr)
720718

721719

0 commit comments

Comments
 (0)