def test_suppress_warnings_type(): # Initial state of module, no warnings my_mod = _get_fresh_mod() assert_equal(getattr(my_mod, '__warningregistry__', {}), {}) # Test module based warning suppression: with suppress_warnings() as sup: sup.filter(UserWarning) warnings.warn('Some warning') assert_warn_len_equal(my_mod, 0) sup = suppress_warnings() sup.filter(UserWarning) with sup: warnings.warn('Some warning') assert_warn_len_equal(my_mod, 0) # And test repeat works: sup.filter(module=my_mod) with sup: warnings.warn('Some warning') assert_warn_len_equal(my_mod, 0) # Without specified modules, don't clear warnings during context # Python 3.7 does not add ignored warnings. with suppress_warnings(): warnings.simplefilter('ignore') warnings.warn('Some warning') assert_warn_len_equal(my_mod, 1, py37=0)
def test_keepdims(self): mat = np.eye(3) for axis in [None, 0, 1]: tgt = np.percentile(mat, 70, axis=axis, out=None, overwrite_input=False) res = np.nanpercentile(mat, 70, axis=axis, out=None, overwrite_input=False) assert_(res.ndim == tgt.ndim) d = np.ones((3, 5, 7, 11)) # Randomly set some elements to NaN: w = np.random.random((4, 200)) * np.array(d.shape)[:, None] w = w.astype(np.intp) d[tuple(w)] = np.nan with suppress_warnings() as sup: sup.filter(RuntimeWarning) res = np.nanpercentile(d, 90, axis=None, keepdims=True) assert_equal(res.shape, (1, 1, 1, 1)) res = np.nanpercentile(d, 90, axis=(0, 1), keepdims=True) assert_equal(res.shape, (1, 1, 7, 11)) res = np.nanpercentile(d, 90, axis=(0, 3), keepdims=True) assert_equal(res.shape, (1, 5, 7, 1)) res = np.nanpercentile(d, 90, axis=(1,), keepdims=True) assert_equal(res.shape, (3, 1, 7, 11)) res = np.nanpercentile(d, 90, axis=(0, 1, 2, 3), keepdims=True) assert_equal(res.shape, (1, 1, 1, 1)) res = np.nanpercentile(d, 90, axis=(0, 1, 3), keepdims=True) assert_equal(res.shape, (1, 1, 7, 1))
def test_multiple_percentiles(self): perc = [50, 100] mat = np.ones((4, 3)) nan_mat = np.nan * mat # For checking consistency in higher dimensional case large_mat = np.ones((3, 4, 5)) large_mat[:, 0:2:4, :] = 0 large_mat[:, :, 3:] *= 2 for axis in [None, 0, 1]: for keepdim in [False, True]: with suppress_warnings() as sup: sup.filter(RuntimeWarning, "All-NaN slice encountered") val = np.percentile(mat, perc, axis=axis, keepdims=keepdim) nan_val = np.nanpercentile(nan_mat, perc, axis=axis, keepdims=keepdim) assert_equal(nan_val.shape, val.shape) val = np.percentile(large_mat, perc, axis=axis, keepdims=keepdim) nan_val = np.nanpercentile(large_mat, perc, axis=axis, keepdims=keepdim) assert_equal(nan_val, val) megamat = np.ones((3, 4, 5, 6)) assert_equal(np.nanpercentile(megamat, perc, axis=(1, 2)).shape, (2, 3, 6))
def test_result(self): types = np.typecodes['AllInteger'] + np.typecodes['AllFloat'] with suppress_warnings() as sup: sup.filter(RuntimeWarning) for dt in types: a = np.ones((), dtype=dt)[()] assert_equal(operator.sub(a, a), 0)
def test_float_modulus_corner_cases(self): # Check remainder magnitude. for dt in np.typecodes['Float']: b = np.array(1.0, dtype=dt) a = np.nextafter(np.array(0.0, dtype=dt), -b) rem = operator.mod(a, b) assert_(rem <= b, 'dt: %s' % dt) rem = operator.mod(-a, -b) assert_(rem >= -b, 'dt: %s' % dt) # Check nans, inf with suppress_warnings() as sup: sup.filter(RuntimeWarning, "invalid value encountered in remainder") for dt in np.typecodes['Float']: fone = np.array(1.0, dtype=dt) fzer = np.array(0.0, dtype=dt) finf = np.array(np.inf, dtype=dt) fnan = np.array(np.nan, dtype=dt) rem = operator.mod(fone, fzer) assert_(np.isnan(rem), 'dt: %s' % dt) # MSVC 2008 returns NaN here, so disable the check. #rem = operator.mod(fone, finf) #assert_(rem == fone, 'dt: %s' % dt) rem = operator.mod(fone, fnan) assert_(np.isnan(rem), 'dt: %s' % dt) rem = operator.mod(finf, fone) assert_(np.isnan(rem), 'dt: %s' % dt)
def test_int_from_infinite_longdouble___int__(self): x = np.longdouble(np.inf) assert_raises(OverflowError, x.__int__) with suppress_warnings() as sup: sup.record(np.ComplexWarning) x = np.clongdouble(np.inf) assert_raises(OverflowError, x.__int__) assert_equal(len(sup.log), 1)
def test_suppress_warnings_module(): # Initial state of module, no warnings my_mod = _get_fresh_mod() assert_equal(getattr(my_mod, '__warningregistry__', {}), {}) def warn_other_module(): # Apply along axis is implemented in python; stacklevel=2 means # we end up inside its module, not ours. def warn(arr): warnings.warn("Some warning 2", stacklevel=2) return arr np.apply_along_axis(warn, 0, [0]) # Test module based warning suppression: assert_warn_len_equal(my_mod, 0) with suppress_warnings() as sup: sup.record(UserWarning) # suppress warning from other module (may have .pyc ending), # if apply_along_axis is moved, had to be changed. sup.filter(module=np.lib.shape_base) warnings.warn("Some warning") warn_other_module() # Check that the suppression did test the file correctly (this module # got filtered) assert_equal(len(sup.log), 1) assert_equal(sup.log[0].message.args[0], "Some warning") assert_warn_len_equal(my_mod, 0, py37=0) sup = suppress_warnings() # Will have to be changed if apply_along_axis is moved: sup.filter(module=my_mod) with sup: warnings.warn('Some warning') assert_warn_len_equal(my_mod, 0) # And test repeat works: sup.filter(module=my_mod) with sup: warnings.warn('Some warning') assert_warn_len_equal(my_mod, 0) # Without specified modules, don't clear warnings during context # Python 3.7 does not add ignored warnings. with suppress_warnings(): warnings.simplefilter('ignore') warnings.warn('Some warning') assert_warn_len_equal(my_mod, 1, py37=0)
def test_corner(self): y = list(linspace(0, 1, 1)) assert_(y == [0.0], y) with suppress_warnings() as sup: sup.filter(DeprecationWarning, ".*safely interpreted as an integer") y = list(linspace(0, 1, 2.5)) assert_(y == [0.0, 1.0])
def test_slice_decref_getsetslice(self): # See gh-10066, a temporary slice object should be discarted. # This test is only really interesting on Python 2 since # it goes through `__set/getslice__` here and can probably be # removed. Use 0:7 to make sure it is never None:7. class KeepIndexObject(np.ndarray): def __getitem__(self, indx): self.indx = indx if indx == slice(0, 7): raise ValueError def __setitem__(self, indx, val): self.indx = indx if indx == slice(0, 4): raise ValueError k = np.array([1]).view(KeepIndexObject) k[0:5] assert_equal(k.indx, slice(0, 5)) assert_equal(sys.getrefcount(k.indx), 2) try: k[0:7] raise AssertionError except ValueError: # The exception holds a reference to the slice so clear on Py2 if hasattr(sys, 'exc_clear'): with suppress_warnings() as sup: sup.filter(DeprecationWarning) sys.exc_clear() assert_equal(k.indx, slice(0, 7)) assert_equal(sys.getrefcount(k.indx), 2) k[0:3] = 6 assert_equal(k.indx, slice(0, 3)) assert_equal(sys.getrefcount(k.indx), 2) try: k[0:4] = 2 raise AssertionError except ValueError: # The exception holds a reference to the slice so clear on Py2 if hasattr(sys, 'exc_clear'): with suppress_warnings() as sup: sup.filter(DeprecationWarning) sys.exc_clear() assert_equal(k.indx, slice(0, 4)) assert_equal(sys.getrefcount(k.indx), 2)
def test_ddof_corrcoef(self): # See gh-3336 x = np.ma.masked_equal([1, 2, 3, 4, 5], 4) y = np.array([2, 2.5, 3.1, 3, 5]) # this test can be removed after deprecation. with suppress_warnings() as sup: sup.filter(DeprecationWarning, "bias and ddof have no effect") r0 = np.ma.corrcoef(x, y, ddof=0) r1 = np.ma.corrcoef(x, y, ddof=1) # ddof should not have an effect (it gets cancelled out) assert_allclose(r0.data, r1.data)
def test_result_values(self): for f, fcmp in zip(self.nanfuncs, [np.greater, np.less]): for row in _ndat: with suppress_warnings() as sup: sup.filter(RuntimeWarning, "invalid value encountered in") ind = f(row) val = row[ind] # comparing with NaN is tricky as the result # is always false except for NaN != NaN assert_(not np.isnan(val)) assert_(not fcmp(val, row).any()) assert_(not np.equal(val, row[:ind]).any())
def test_suppress_warnings_decorate_no_record(): sup = suppress_warnings() sup.filter(UserWarning) @sup def warn(category): warnings.warn('Some warning', category) with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") warn(UserWarning) # should be supppressed warn(RuntimeWarning) assert_equal(len(w), 1)
def test_suppress_warnings_record(): sup = suppress_warnings() log1 = sup.record() with sup: log2 = sup.record(message='Some other warning 2') sup.filter(message='Some warning') warnings.warn('Some warning') warnings.warn('Some other warning') warnings.warn('Some other warning 2') assert_equal(len(sup.log), 2) assert_equal(len(log1), 1) assert_equal(len(log2), 1) assert_equal(log2[0].message.args[0], 'Some other warning 2') # Do it again, with the same context to see if some warnings survived: with sup: log2 = sup.record(message='Some other warning 2') sup.filter(message='Some warning') warnings.warn('Some warning') warnings.warn('Some other warning') warnings.warn('Some other warning 2') assert_equal(len(sup.log), 2) assert_equal(len(log1), 1) assert_equal(len(log2), 1) assert_equal(log2[0].message.args[0], 'Some other warning 2') # Test nested: with suppress_warnings() as sup: sup.record() with suppress_warnings() as sup2: sup2.record(message='Some warning') warnings.warn('Some warning') warnings.warn('Some other warning') assert_equal(len(sup2.log), 1) assert_equal(len(sup.log), 1)
def test_normed(self): sup = suppress_warnings() with sup: rec = sup.record(np.VisibleDeprecationWarning, '.*normed.*') # Check that the integral of the density equals 1. n = 100 v = np.random.rand(n) a, b = histogram(v, normed=True) area = np.sum(a * np.diff(b)) assert_almost_equal(area, 1) assert_equal(len(rec), 1) sup = suppress_warnings() with sup: rec = sup.record(np.VisibleDeprecationWarning, '.*normed.*') # Check with non-constant bin widths (buggy but backwards # compatible) v = np.arange(10) bins = [0, 1, 5, 9, 10] a, b = histogram(v, bins, normed=True) area = np.sum(a * np.diff(b)) assert_almost_equal(area, 1) assert_equal(len(rec), 1)
def test_suppress_warnings_forwarding(): def warn_other_module(): # Apply along axis is implemented in python; stacklevel=2 means # we end up inside its module, not ours. def warn(arr): warnings.warn("Some warning", stacklevel=2) return arr np.apply_along_axis(warn, 0, [0]) with suppress_warnings() as sup: sup.record() with suppress_warnings("always"): for i in range(2): warnings.warn("Some warning") assert_equal(len(sup.log), 2) with suppress_warnings() as sup: sup.record() with suppress_warnings("location"): for i in range(2): warnings.warn("Some warning") warnings.warn("Some warning") assert_equal(len(sup.log), 2) with suppress_warnings() as sup: sup.record() with suppress_warnings("module"): for i in range(2): warnings.warn("Some warning") warnings.warn("Some warning") warn_other_module() assert_equal(len(sup.log), 2) with suppress_warnings() as sup: sup.record() with suppress_warnings("once"): for i in range(2): warnings.warn("Some warning") warnings.warn("Some other warning") warn_other_module() assert_equal(len(sup.log), 2)
def test_py2_float_print(self): # gh-10753 # In python2, the python float type implements an obsolte method # tp_print, which overrides tp_repr and tp_str when using "print" to # output to a "real file" (ie, not a StringIO). Make sure we don't # inherit it. x = np.double(0.1999999999999) with TemporaryFile('r+t') as f: print(x, file=f) f.seek(0) output = f.read() assert_equal(output, str(x) + '\n') # In python2 the value float('0.1999999999999') prints with reduced # precision as '0.2', but we want numpy's np.double('0.1999999999999') # to print the unique value, '0.1999999999999'. # gh-11031 # Only in the python2 interactive shell and when stdout is a "real" # file, the output of the last command is printed to stdout without # Py_PRINT_RAW (unlike the print statement) so `>>> x` and `>>> print # x` are potentially different. Make sure they are the same. The only # way I found to get prompt-like output is using an actual prompt from # the 'code' module. Again, must use tempfile to get a "real" file. # dummy user-input which enters one line and then ctrl-Ds. def userinput(): yield 'np.sqrt(2)' raise EOFError gen = userinput() input_func = lambda prompt="": next(gen) with TemporaryFile('r+t') as fo, TemporaryFile('r+t') as fe: orig_stdout, orig_stderr = sys.stdout, sys.stderr sys.stdout, sys.stderr = fo, fe # py2 code.interact sends irrelevant internal DeprecationWarnings with suppress_warnings() as sup: sup.filter(DeprecationWarning) code.interact(local={'np': np}, readfunc=input_func, banner='') sys.stdout, sys.stderr = orig_stdout, orig_stderr fo.seek(0) capture = fo.read().strip() assert_equal(capture, repr(np.sqrt(2)))
def test_bool_conversion(self): # gh-12107 # Reference integer histogram a = np.array([1, 1, 0], dtype=np.uint8) int_hist, int_edges = np.histogram(a) # Should raise an warning on booleans # Ensure that the histograms are equivalent, need to suppress # the warnings to get the actual outputs with suppress_warnings() as sup: rec = sup.record(RuntimeWarning, 'Converting input from .*') hist, edges = np.histogram([True, True, False]) # A warning should be issued assert_equal(len(rec), 1) assert_array_equal(hist, int_hist) assert_array_equal(edges, int_edges)
def test_ddof_too_big(self): nanfuncs = [np.nanvar, np.nanstd] stdfuncs = [np.var, np.std] dsize = [len(d) for d in _rdat] for nf, rf in zip(nanfuncs, stdfuncs): for ddof in range(5): with suppress_warnings() as sup: sup.record(RuntimeWarning) sup.filter(np.ComplexWarning) tgt = [ddof >= d for d in dsize] res = nf(_ndat, axis=1, ddof=ddof) assert_equal(np.isnan(res), tgt) if any(tgt): assert_(len(sup.log) == 1) else: assert_(len(sup.log) == 0)
def test_dtype_from_char(self): mat = np.eye(3) codes = 'efdgFDG' for nf, rf in zip(self.nanfuncs, self.stdfuncs): for c in codes: with suppress_warnings() as sup: if nf in {np.nanstd, np.nanvar} and c in 'FDG': # Giving the warning is a small bug, see gh-8000 sup.filter(np.ComplexWarning) tgt = rf(mat, dtype=c, axis=1).dtype.type res = nf(mat, dtype=c, axis=1).dtype.type assert_(res is tgt) # scalar case tgt = rf(mat, dtype=c, axis=None).dtype.type res = nf(mat, dtype=c, axis=None).dtype.type assert_(res is tgt)
def test_recarrays(self): """Test record arrays.""" a = np.empty(2, [('floupi', float), ('floupa', float)]) a['floupi'] = [1, 2] a['floupa'] = [1, 2] b = a.copy() self._test_equal(a, b) c = np.empty(2, [('floupipi', float), ('floupa', float)]) c['floupipi'] = a['floupi'].copy() c['floupa'] = a['floupa'].copy() with suppress_warnings() as sup: l = sup.record(FutureWarning, message="elementwise == ") self._test_not_equal(c, b) assert_equal(len(l), 1)
def test_allnans(self): mat = np.array([np.nan]*9).reshape(3, 3) for axis in [None, 0, 1]: with suppress_warnings() as sup: sup.record(RuntimeWarning) assert_(np.isnan(np.nanmedian(mat, axis=axis)).all()) if axis is None: assert_(len(sup.log) == 1) else: assert_(len(sup.log) == 3) # Check scalar assert_(np.isnan(np.nanmedian(np.nan))) if axis is None: assert_(len(sup.log) == 2) else: assert_(len(sup.log) == 4)
def test_float_special(self): with suppress_warnings() as sup: sup.filter(RuntimeWarning) for inf in [np.inf, -np.inf]: a = np.array([[inf, np.nan], [np.nan, np.nan]]) assert_equal(np.nanmedian(a, axis=0), [inf, np.nan]) assert_equal(np.nanmedian(a, axis=1), [inf, np.nan]) assert_equal(np.nanmedian(a), inf) # minimum fill value check a = np.array([[np.nan, np.nan, inf], [np.nan, np.nan, inf]]) assert_equal(np.nanmedian(a), inf) assert_equal(np.nanmedian(a, axis=0), [np.nan, np.nan, inf]) assert_equal(np.nanmedian(a, axis=1), inf) # no mask path a = np.array([[inf, inf], [inf, inf]]) assert_equal(np.nanmedian(a, axis=1), inf) a = np.array([[inf, 7, -inf, -9], [-10, np.nan, np.nan, 5], [4, np.nan, np.nan, inf]], dtype=np.float32) if inf > 0: assert_equal(np.nanmedian(a, axis=0), [4., 7., -inf, 5.]) assert_equal(np.nanmedian(a), 4.5) else: assert_equal(np.nanmedian(a, axis=0), [-10., 7., -inf, -9.]) assert_equal(np.nanmedian(a), -2.5) assert_equal(np.nanmedian(a, axis=-1), [-1., -2.5, inf]) for i in range(0, 10): for j in range(1, 10): a = np.array([([np.nan] * i) + ([inf] * j)] * 2) assert_equal(np.nanmedian(a), inf) assert_equal(np.nanmedian(a, axis=1), inf) assert_equal(np.nanmedian(a, axis=0), ([np.nan] * i) + [inf] * j) a = np.array([([np.nan] * i) + ([-inf] * j)] * 2) assert_equal(np.nanmedian(a), -inf) assert_equal(np.nanmedian(a, axis=1), -inf) assert_equal(np.nanmedian(a, axis=0), ([np.nan] * i) + [-inf] * j)
def test_ufunc_return_ndarray(self): fp = memmap(self.tmpfp, dtype=self.dtype, shape=self.shape) fp[:] = self.data with suppress_warnings() as sup: sup.filter(FutureWarning, "np.average currently does not preserve") for unary_op in [sum, average, product]: result = unary_op(fp) assert_(isscalar(result)) assert_(result.__class__ is self.data[0, 0].__class__) assert_(unary_op(fp, axis=0).__class__ is ndarray) assert_(unary_op(fp, axis=1).__class__ is ndarray) for binary_op in [add, subtract, multiply]: assert_(binary_op(fp, self.data).__class__ is ndarray) assert_(binary_op(self.data, fp).__class__ is ndarray) assert_(binary_op(fp, fp).__class__ is ndarray) fp += 1 assert (fp.__class__ is memmap) add(fp, 1, out=fp) assert (fp.__class__ is memmap)
def test_some_nan_values(self): # gh-7503 one_nan = np.array([0, 1, np.nan]) all_nan = np.array([np.nan, np.nan]) # the internal comparisons with NaN give warnings sup = suppress_warnings() sup.filter(RuntimeWarning) with sup: # can't infer range with nan assert_raises(ValueError, histogram, one_nan, bins='auto') assert_raises(ValueError, histogram, all_nan, bins='auto') # explicit range solves the problem h, b = histogram(one_nan, bins='auto', range=(0, 1)) assert_equal(h.sum(), 2) # nan is not counted h, b = histogram(all_nan, bins='auto', range=(0, 1)) assert_equal(h.sum(), 0) # nan is not counted # as does an explicit set of bins h, b = histogram(one_nan, bins=[0, 1]) assert_equal(h.sum(), 2) # nan is not counted h, b = histogram(all_nan, bins=[0, 1]) assert_equal(h.sum(), 0) # nan is not counted