Skip to content

TEST: Suppress new numpy warning on nan-to-int cast #1118

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 18, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 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 @@ -413,7 +416,7 @@ def test_a2f_nan2zero():
# How weird? Look at arr.astype(np.int64)
with np.errstate(invalid='ignore'):
data_back = write_return(arr, str_io, np.int64, nan2zero=False)
assert_array_equal(data_back, arr.astype(np.int64))
assert_array_equal(data_back, arr.astype(np.int64))


def test_a2f_nan2zero_scaling():
Expand Down Expand Up @@ -672,40 +675,43 @@ 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():
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(warn_type) if warn_type else error_warnings():
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():
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