nipy / nibabel

Python package to access a cacophony of neuro-imaging file formats

Home Page:http://nipy.org/nibabel/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Test failure in `4.0.0` (`RuntimeWarning`)

TheChymera opened this issue · comments

This is the full build log: https://ppb.chymera.eu/e8be90.log

Traceback of the specific failure:

_________________________________________________________ test_a2f_nan2zero_range __________________________________________________________

    def test_a2f_nan2zero_range():
        # array_to_file should check if nan can be represented as zero
        # This comes about when the writer can't write the value (-intercept /
        # divslope) because it does not fit in the output range.  Input clipping
        # should not affect this
        fobj = BytesIO()
        # No problem for input integer types - they don't have NaNs
        for dt in INT_TYPES:
            arr_no_nan = np.array([-1, 0, 1, 2], dtype=dt)
            # No errors from explicit thresholding (nor for input float types)
            back_arr = write_return(arr_no_nan, fobj, np.int8, mn=1, nan2zero=True)
            assert_array_equal([1, 1, 1, 2], back_arr)
            back_arr = write_return(arr_no_nan, fobj, np.int8, mx=-1, nan2zero=True)
            assert_array_equal([-1, -1, -1, -1], back_arr)
            # Pushing zero outside the output data range does not generate error
            back_arr = write_return(arr_no_nan, fobj, np.int8, intercept=129, nan2zero=True)
            assert_array_equal([-128, -128, -128, -127], back_arr)
            back_arr = write_return(arr_no_nan, fobj, np.int8,
                                    intercept=257.1, divslope=2, nan2zero=True)
            assert_array_equal([-128, -128, -128, -128], back_arr)
        for dt in CFLOAT_TYPES:
            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
            # No errors from explicit thresholding
            # mn thresholding excluding zero
            with pytest.warns(warn_type) if warn_type 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():
                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():
                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():
>               nan_cast = np.array(np.nan, dtype=dt).astype(np.int8)
E               RuntimeWarning: invalid value encountered in cast

arr        = array([-1.,  0.,  1., nan], dtype=float16)
arr_no_nan = array([-1.,  0.,  1.,  2.], dtype=float16)
back_arr   = array([-128, -128, -127, -128], dtype=int8)
dt         = <class 'numpy.float16'>
fobj       = <_io.BytesIO object at 0x7f94b02771a0>
warn_type  = None

nibabel/tests/test_volumeutils.py:695: RuntimeWarning

Is this perhaps a warning that pytest incorrectly counts towards test failure?

with pytest.warns(warn_type) if warn_type else error_warnings():

Looks like numpy is changing the warning type on us or expanding cases where it warns.

@TheChymera What version of numpy are you using?

@effigies Using =numpy-1.24.2.

Oh, you said 4.0.0... does this reproduce in 4.0.2 or 5.0.1?

Found it. This was fixed in #1118, released in 4.0.1.