Skip to content

Commit 215f7ae

Browse files
Merge pull request #1118 from effigies/test/numpy_1.24-dev
TEST: Suppress new numpy warning on nan-to-int cast numpy/numpy#21437 does not require us to change our behavior, but does require us to change our expectations of what warnings are raised. Since this is quite an edge case, it seems to make more sense to suppress the warning and eventually expect it when numpy 1.24 becomes our minimum.
2 parents f469c68 + 7d3979d commit 215f7ae

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

nibabel/tests/test_volumeutils.py

Lines changed: 15 additions & 9 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
@@ -413,7 +416,7 @@ def test_a2f_nan2zero():
413416
# How weird? Look at arr.astype(np.int64)
414417
with np.errstate(invalid='ignore'):
415418
data_back = write_return(arr, str_io, np.int64, nan2zero=False)
416-
assert_array_equal(data_back, arr.astype(np.int64))
419+
assert_array_equal(data_back, arr.astype(np.int64))
417420

418421

419422
def test_a2f_nan2zero_scaling():
@@ -672,40 +675,43 @@ 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():
700+
with pytest.warns(c_and_n_warn) if c_and_n_warn else error_warnings():
695701
nan_cast = np.array(np.nan, dtype=dt).astype(np.int8)
696-
with pytest.warns(warn_type) if warn_type else error_warnings():
702+
with pytest.warns(c_and_n_warn) if c_and_n_warn else error_warnings():
697703
back_arr = write_return(arr, fobj, np.int8, intercept=129, nan2zero=False)
698704
assert_array_equal([-128, -128, -128, nan_cast], back_arr)
699705
# divslope
700-
with pytest.warns(warn_type) if warn_type else error_warnings():
706+
with pytest.warns(complex_warn) if complex_warn else error_warnings():
701707
back_arr = write_return(arr, fobj, np.int8, intercept=256, divslope=2)
702708
assert_array_equal([-128, -128, -128, -128], back_arr)
703709
with pytest.raises(ValueError):
704710
write_return(arr, fobj, np.int8, intercept=257.1, divslope=2)
705711
with pytest.raises(ValueError):
706712
write_return(arr_no_nan, fobj, np.int8, intercept=257.1, divslope=2)
707713
# OK with nan2zero false
708-
with pytest.warns(warn_type) if warn_type else error_warnings():
714+
with pytest.warns(c_and_n_warn) if c_and_n_warn else error_warnings():
709715
back_arr = write_return(arr, fobj, np.int8,
710716
intercept=257.1, divslope=2, nan2zero=False)
711717
assert_array_equal([-128, -128, -128, nan_cast], back_arr)

0 commit comments

Comments
 (0)