summaryrefslogtreecommitdiff
path: root/numpy/lib/tests
diff options
context:
space:
mode:
Diffstat (limited to 'numpy/lib/tests')
-rw-r--r--numpy/lib/tests/test_function_base.py33
-rw-r--r--numpy/lib/tests/test_io.py29
-rw-r--r--numpy/lib/tests/test_nanfunctions.py84
-rw-r--r--numpy/lib/tests/test_twodim_base.py2
4 files changed, 73 insertions, 75 deletions
diff --git a/numpy/lib/tests/test_function_base.py b/numpy/lib/tests/test_function_base.py
index 0d7b11c44..4535c1e7f 100644
--- a/numpy/lib/tests/test_function_base.py
+++ b/numpy/lib/tests/test_function_base.py
@@ -8,7 +8,7 @@ from numpy.testing import (
run_module_suite, TestCase, assert_, assert_equal, assert_array_equal,
assert_almost_equal, assert_array_almost_equal, assert_raises,
assert_allclose, assert_array_max_ulp, assert_warns,
- assert_raises_regex, dec, clear_and_catch_warnings
+ assert_raises_regex, dec, suppress_warnings
)
from numpy.testing.utils import HAS_REFCOUNT
import numpy.lib.function_base as nfb
@@ -320,7 +320,11 @@ class TestAverage(TestCase):
a = np.array([[1,2],[3,4]]).view(subclass)
w = np.array([[1,2],[3,4]]).view(subclass)
- assert_equal(type(np.average(a, weights=w)), subclass)
+ with suppress_warnings() as sup:
+ # Note that the warning is spurious, because the test checks
+ # for weights while a is ignored.
+ sup.filter(FutureWarning, "np.average currently does not preserve")
+ assert_equal(type(np.average(a, weights=w)), subclass)
# also test matrices
a = np.matrix([[1,2],[3,4]])
@@ -1457,12 +1461,12 @@ class TestHistogram(TestCase):
a, b = histogram([], bins=([0, 1]))
assert_array_equal(a, np.array([0]))
assert_array_equal(b, np.array([0, 1]))
-
+
def test_error_binnum_type (self):
# Tests if right Error is raised if bins argument is float
vals = np.linspace(0.0, 1.0, num=100)
histogram(vals, 5)
- assert_raises(TypeError, histogram, vals, 2.4)
+ assert_raises(TypeError, histogram, vals, 2.4)
def test_finite_range(self):
# Normal ranges should be fine
@@ -1792,15 +1796,6 @@ class TestCheckFinite(TestCase):
assert_(a.dtype == np.float64)
-class catch_warn_nfb(clear_and_catch_warnings):
-
- """
- Context manager to catch, reset warnings in function_base module
-
- """
- class_modules = (nfb,)
-
-
class TestCorrCoef(TestCase):
A = np.array(
[[0.15391142, 0.18045767, 0.14197213],
@@ -1837,10 +1832,10 @@ class TestCorrCoef(TestCase):
def test_ddof(self):
# ddof raises DeprecationWarning
- with catch_warn_nfb():
+ with suppress_warnings() as sup:
warnings.simplefilter("always")
assert_warns(DeprecationWarning, corrcoef, self.A, ddof=-1)
- warnings.simplefilter("ignore")
+ sup.filter(DeprecationWarning)
# ddof has no or negligible effect on the function
assert_almost_equal(corrcoef(self.A, ddof=-1), self.res1)
assert_almost_equal(corrcoef(self.A, self.B, ddof=-1), self.res2)
@@ -1849,11 +1844,11 @@ class TestCorrCoef(TestCase):
def test_bias(self):
# bias raises DeprecationWarning
- with catch_warn_nfb():
+ with suppress_warnings() as sup:
warnings.simplefilter("always")
assert_warns(DeprecationWarning, corrcoef, self.A, self.B, 1, 0)
assert_warns(DeprecationWarning, corrcoef, self.A, bias=0)
- warnings.simplefilter("ignore")
+ sup.filter(DeprecationWarning)
# bias has no or negligible effect on the function
assert_almost_equal(corrcoef(self.A, bias=1), self.res1)
@@ -2316,7 +2311,7 @@ class TestInterp(TestCase):
assert_almost_equal(np.interp(x0, x, y), x0)
x0 = np.nan
assert_almost_equal(np.interp(x0, x, y), x0)
-
+
def test_complex_interp(self):
# test complex interpolation
x = np.linspace(0, 1, 5)
@@ -2335,7 +2330,7 @@ class TestInterp(TestCase):
x = [-180, -170, -185, 185, -10, -5, 0, 365]
xp = [190, -190, 350, -350]
fp = [5+1.0j, 10+2j, 3+3j, 4+4j]
- y = [7.5+1.5j, 5.+1.0j, 8.75+1.75j, 6.25+1.25j, 3.+3j, 3.25+3.25j,
+ y = [7.5+1.5j, 5.+1.0j, 8.75+1.75j, 6.25+1.25j, 3.+3j, 3.25+3.25j,
3.5+3.5j, 3.75+3.75j]
assert_almost_equal(np.interp(x, xp, fp, period=360), y)
diff --git a/numpy/lib/tests/test_io.py b/numpy/lib/tests/test_io.py
index 1474349de..333891d46 100644
--- a/numpy/lib/tests/test_io.py
+++ b/numpy/lib/tests/test_io.py
@@ -19,7 +19,7 @@ from numpy.ma.testutils import assert_equal
from numpy.testing import (
TestCase, run_module_suite, assert_warns, assert_,
assert_raises_regex, assert_raises, assert_allclose,
- assert_array_equal, temppath, dec, IS_PYPY
+ assert_array_equal, temppath, dec, IS_PYPY, suppress_warnings
)
@@ -282,8 +282,8 @@ class TestSavezLoad(RoundtripTest, TestCase):
# collector, so we catch the warnings. Because ResourceWarning
# is unknown in Python < 3.x, we take the easy way out and
# catch all warnings.
- with warnings.catch_warnings():
- warnings.simplefilter("ignore")
+ with suppress_warnings() as sup:
+ sup.filter(Warning) # TODO: specify exact message
for i in range(1, 1025):
try:
np.load(tmp)["data"]
@@ -687,9 +687,8 @@ class TestLoadTxt(TestCase):
assert_array_equal(x, a)
def test_empty_file(self):
- with warnings.catch_warnings():
- warnings.filterwarnings("ignore",
- message="loadtxt: Empty input file:")
+ with suppress_warnings() as sup:
+ sup.filter(message="loadtxt: Empty input file:")
c = TextIO()
x = np.loadtxt(c)
assert_equal(x.shape, (0,))
@@ -826,9 +825,8 @@ class TestLoadTxt(TestCase):
assert_(x.shape == (3,))
# Test ndmin kw with empty file.
- with warnings.catch_warnings():
- warnings.filterwarnings("ignore",
- message="loadtxt: Empty input file:")
+ with suppress_warnings() as sup:
+ sup.filter(message="loadtxt: Empty input file:")
f = TextIO()
assert_(np.loadtxt(f, ndmin=2).shape == (0, 1,))
assert_(np.loadtxt(f, ndmin=1).shape == (0,))
@@ -974,8 +972,8 @@ class TestFromTxt(TestCase):
assert_equal(test, ctrl)
def test_skip_footer_with_invalid(self):
- with warnings.catch_warnings():
- warnings.filterwarnings("ignore")
+ with suppress_warnings() as sup:
+ sup.filter(ConversionWarning)
basestr = '1 1\n2 2\n3 3\n4 4\n5 \n6 \n7 \n'
# Footer too small to get rid of all invalid values
assert_raises(ValueError, np.genfromtxt,
@@ -1302,9 +1300,8 @@ M 33 21.99
def test_empty_file(self):
# Test that an empty file raises the proper warning.
- with warnings.catch_warnings():
- warnings.filterwarnings("ignore",
- message="genfromtxt: Empty input file:")
+ with suppress_warnings() as sup:
+ sup.filter(message="genfromtxt: Empty input file:")
data = TextIO()
test = np.genfromtxt(data)
assert_equal(test, np.array([]))
@@ -1751,8 +1748,8 @@ M 33 21.99
assert_raises(ValueError, np.genfromtxt, TextIO(data), max_rows=4)
# Test with invalid not raise
- with warnings.catch_warnings():
- warnings.filterwarnings("ignore")
+ with suppress_warnings() as sup:
+ sup.filter(ConversionWarning)
test = np.genfromtxt(TextIO(data), max_rows=4, invalid_raise=False)
control = np.array([[1., 1.], [2., 2.], [3., 3.], [4., 4.]])
diff --git a/numpy/lib/tests/test_nanfunctions.py b/numpy/lib/tests/test_nanfunctions.py
index 03f9beff6..06c0953b5 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
)
@@ -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
@@ -317,26 +317,34 @@ 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:
+ 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)
+ # 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:
+ 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_dtype_from_input(self):
codes = 'efdgFDG'
@@ -524,16 +532,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)
@@ -585,8 +593,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)
@@ -642,22 +650,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))
@@ -685,8 +691,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('ignore', 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])
@@ -723,8 +729,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)
@@ -819,8 +825,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)
diff --git a/numpy/lib/tests/test_twodim_base.py b/numpy/lib/tests/test_twodim_base.py
index 31925d5fe..98b8aa39c 100644
--- a/numpy/lib/tests/test_twodim_base.py
+++ b/numpy/lib/tests/test_twodim_base.py
@@ -5,7 +5,7 @@ from __future__ import division, absolute_import, print_function
from numpy.testing import (
TestCase, run_module_suite, assert_equal, assert_array_equal,
- assert_array_max_ulp, assert_array_almost_equal, assert_raises
+ assert_array_max_ulp, assert_array_almost_equal, assert_raises,
)
from numpy import (