From c1ddf841f6a48248b946a990ae750505b8b91686 Mon Sep 17 00:00:00 2001 From: Sebastian Berg Date: Sun, 19 Jun 2016 14:01:47 +0200 Subject: ENH: Remove warning ignoring from nanfuncs Comment mentions a speedup, but it seems unsure why it should be there. Instead use an error state in divide_by_count. Some extra complex warnings had to be ignored (but those seemed correct) --- numpy/lib/tests/test_nanfunctions.py | 62 +++++++++++++++++++----------------- 1 file changed, 32 insertions(+), 30 deletions(-) (limited to 'numpy/lib/tests/test_nanfunctions.py') diff --git a/numpy/lib/tests/test_nanfunctions.py b/numpy/lib/tests/test_nanfunctions.py index 03f9beff6..e062bc032 100644 --- a/numpy/lib/tests/test_nanfunctions.py +++ b/numpy/lib/tests/test_nanfunctions.py @@ -5,7 +5,7 @@ import warnings import numpy as np from numpy.testing import ( run_module_suite, TestCase, assert_, assert_equal, assert_almost_equal, - assert_warns, assert_no_warnings, assert_raises, assert_array_equal + assert_no_warnings, assert_raises, assert_array_equal, suppress_warnings ) @@ -317,26 +317,30 @@ class SharedNanFunctionsTestsMixin(object): codes = 'efdgFDG' for nf, rf in zip(self.nanfuncs, self.stdfuncs): for c in codes: - tgt = rf(mat, dtype=np.dtype(c), axis=1).dtype.type - res = nf(mat, dtype=np.dtype(c), axis=1).dtype.type - assert_(res is tgt) - # scalar case - tgt = rf(mat, dtype=np.dtype(c), axis=None).dtype.type - res = nf(mat, dtype=np.dtype(c), axis=None).dtype.type - assert_(res is tgt) + with suppress_warnings() as sup: + sup.filter(np.ComplexWarning) + tgt = rf(mat, dtype=np.dtype(c), axis=1).dtype.type + res = nf(mat, dtype=np.dtype(c), axis=1).dtype.type + assert_(res is tgt) + # scalar case + tgt = rf(mat, dtype=np.dtype(c), axis=None).dtype.type + res = nf(mat, dtype=np.dtype(c), axis=None).dtype.type + assert_(res is tgt) 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: - 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) + with suppress_warnings() as sup: + 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_dtype_from_input(self): codes = 'efdgFDG' @@ -524,16 +528,16 @@ class TestNanFunctions_MeanVarStd(TestCase, SharedNanFunctionsTestsMixin): dsize = [len(d) for d in _rdat] for nf, rf in zip(nanfuncs, stdfuncs): for ddof in range(5): - with warnings.catch_warnings(record=True) as w: - warnings.simplefilter('always') + 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(w) == 1) - assert_(issubclass(w[0].category, RuntimeWarning)) + assert_(len(sup.log) == 1) else: - assert_(len(w) == 0) + assert_(len(sup.log) == 0) def test_allnans(self): mat = np.array([np.nan]*9).reshape(3, 3) @@ -642,22 +646,20 @@ class TestNanFunctions_Median(TestCase): def test_allnans(self): mat = np.array([np.nan]*9).reshape(3, 3) for axis in [None, 0, 1]: - with warnings.catch_warnings(record=True) as w: - warnings.simplefilter('always') - warnings.simplefilter('ignore', FutureWarning) + with suppress_warnings() as sup: + sup.record(RuntimeWarning) + assert_(np.isnan(np.nanmedian(mat, axis=axis)).all()) if axis is None: - assert_(len(w) == 1) + assert_(len(sup.log) == 1) else: - assert_(len(w) == 3) - assert_(issubclass(w[0].category, RuntimeWarning)) + assert_(len(sup.log) == 3) # Check scalar assert_(np.isnan(np.nanmedian(np.nan))) if axis is None: - assert_(len(w) == 2) + assert_(len(sup.log) == 2) else: - assert_(len(w) == 4) - assert_(issubclass(w[0].category, RuntimeWarning)) + assert_(len(sup.log) == 4) def test_empty(self): mat = np.zeros((0, 3)) @@ -686,7 +688,7 @@ class TestNanFunctions_Median(TestCase): def test_float_special(self): with warnings.catch_warnings(record=True): - warnings.simplefilter('ignore', RuntimeWarning) + warnings.simplefilter('always', RuntimeWarning) a = np.array([[np.inf, np.nan], [np.nan, np.nan]]) assert_equal(np.nanmedian(a, axis=0), [np.inf, np.nan]) assert_equal(np.nanmedian(a, axis=1), [np.inf, np.nan]) -- cgit v1.2.1 From 20ea3a25119834c2224e32caebf4f2fb4966edd3 Mon Sep 17 00:00:00 2001 From: Sebastian Berg Date: Wed, 31 Aug 2016 21:16:08 +0200 Subject: TST: Replace catch_warnings when recording is not enforced in test_nanfuncs --- numpy/lib/tests/test_nanfunctions.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'numpy/lib/tests/test_nanfunctions.py') diff --git a/numpy/lib/tests/test_nanfunctions.py b/numpy/lib/tests/test_nanfunctions.py index e062bc032..e4aea4482 100644 --- a/numpy/lib/tests/test_nanfunctions.py +++ b/numpy/lib/tests/test_nanfunctions.py @@ -167,8 +167,8 @@ class TestNanFunctions_ArgminArgmax(TestCase): def test_result_values(self): for f, fcmp in zip(self.nanfuncs, [np.greater, np.less]): for row in _ndat: - with warnings.catch_warnings(record=True): - warnings.simplefilter('always') + 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 @@ -589,8 +589,8 @@ class TestNanFunctions_Median(TestCase): w = np.random.random((4, 200)) * np.array(d.shape)[:, None] w = w.astype(np.intp) d[tuple(w)] = np.nan - with warnings.catch_warnings(record=True) as w: - warnings.simplefilter('always', RuntimeWarning) + with suppress_warnings() as sup: + sup.filter(RuntimeWarning) res = np.nanmedian(d, axis=None, keepdims=True) assert_equal(res.shape, (1, 1, 1, 1)) res = np.nanmedian(d, axis=(0, 1), keepdims=True) @@ -687,8 +687,8 @@ class TestNanFunctions_Median(TestCase): assert_raises(ValueError, np.nanmedian, d, axis=(1, 1)) def test_float_special(self): - with warnings.catch_warnings(record=True): - warnings.simplefilter('always', RuntimeWarning) + with suppress_warnings() as sup: + sup.filter(RuntimeWarning) a = np.array([[np.inf, np.nan], [np.nan, np.nan]]) assert_equal(np.nanmedian(a, axis=0), [np.inf, np.nan]) assert_equal(np.nanmedian(a, axis=1), [np.inf, np.nan]) @@ -725,8 +725,8 @@ class TestNanFunctions_Percentile(TestCase): w = np.random.random((4, 200)) * np.array(d.shape)[:, None] w = w.astype(np.intp) d[tuple(w)] = np.nan - with warnings.catch_warnings(record=True) as w: - warnings.simplefilter('always', RuntimeWarning) + 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) @@ -821,8 +821,8 @@ class TestNanFunctions_Percentile(TestCase): large_mat[:, :, 3:] *= 2 for axis in [None, 0, 1]: for keepdim in [False, True]: - with warnings.catch_warnings(record=True) as w: - warnings.simplefilter('always') + 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) -- cgit v1.2.1 From 514d13679a55a82a96689679002c4ddc514641ce Mon Sep 17 00:00:00 2001 From: Sebastian Berg Date: Wed, 31 Aug 2016 21:22:17 +0200 Subject: TST: Use ComplexWarning suppression only where needed --- numpy/lib/tests/test_nanfunctions.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'numpy/lib/tests/test_nanfunctions.py') diff --git a/numpy/lib/tests/test_nanfunctions.py b/numpy/lib/tests/test_nanfunctions.py index e4aea4482..06c0953b5 100644 --- a/numpy/lib/tests/test_nanfunctions.py +++ b/numpy/lib/tests/test_nanfunctions.py @@ -318,7 +318,9 @@ class SharedNanFunctionsTestsMixin(object): for nf, rf in zip(self.nanfuncs, self.stdfuncs): for c in codes: with suppress_warnings() as sup: - sup.filter(np.ComplexWarning) + 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=np.dtype(c), axis=1).dtype.type res = nf(mat, dtype=np.dtype(c), axis=1).dtype.type assert_(res is tgt) @@ -333,7 +335,9 @@ class SharedNanFunctionsTestsMixin(object): for nf, rf in zip(self.nanfuncs, self.stdfuncs): for c in codes: with suppress_warnings() as sup: - sup.filter(np.ComplexWarning) + 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) -- cgit v1.2.1