[Numpy-svn] r8658 - in branches/1.5.x/numpy: core/tests lib/tests ma/tests

numpy-svn@scip... numpy-svn@scip...
Sun Aug 22 00:25:08 CDT 2010


Author: rgommers
Date: 2010-08-22 00:25:08 -0500 (Sun, 22 Aug 2010)
New Revision: 8658

Modified:
   branches/1.5.x/numpy/core/tests/test_multiarray.py
   branches/1.5.x/numpy/core/tests/test_regression.py
   branches/1.5.x/numpy/core/tests/test_umath.py
   branches/1.5.x/numpy/core/tests/test_umath_complex.py
   branches/1.5.x/numpy/lib/tests/test_io.py
   branches/1.5.x/numpy/ma/tests/test_core.py
   branches/1.5.x/numpy/ma/tests/test_subclassing.py
Log:
TST: silence NaN related test warnings. See #1587.

The final decision on what to do with this issue still has to be made, so this
patch is only applied to 1.5.x now.

Thanks to Christoph Gohlke for the patch.

Modified: branches/1.5.x/numpy/core/tests/test_multiarray.py
===================================================================
--- branches/1.5.x/numpy/core/tests/test_multiarray.py	2010-08-21 02:32:18 UTC (rev 8657)
+++ branches/1.5.x/numpy/core/tests/test_multiarray.py	2010-08-22 05:25:08 UTC (rev 8658)
@@ -702,9 +702,13 @@
             assert all(amax == aargmax.choose(*a.transpose(i,*axes)))
 
     def test_combinations(self):
-        for arr, pos in self.nan_arr:
-            assert_equal(np.argmax(arr), pos, err_msg="%r"%arr)
-            assert_equal(arr[np.argmax(arr)], np.max(arr), err_msg="%r"%arr)
+        err = np.seterr(invalid="ignore")
+        try:
+            for arr, pos in self.nan_arr:
+                assert_equal(np.argmax(arr), pos, err_msg="%r"%arr)
+                assert_equal(arr[np.argmax(arr)], np.max(arr), err_msg="%r"%arr)
+        finally:
+            np.seterr(**err)
 
 
 class TestMinMax(TestCase):

Modified: branches/1.5.x/numpy/core/tests/test_regression.py
===================================================================
--- branches/1.5.x/numpy/core/tests/test_regression.py	2010-08-21 02:32:18 UTC (rev 8657)
+++ branches/1.5.x/numpy/core/tests/test_regression.py	2010-08-22 05:25:08 UTC (rev 8658)
@@ -1008,7 +1008,11 @@
     def test_sign_for_complex_nan(self, level=rlevel):
         """Ticket 794."""
         C = np.array([-np.inf, -2+1j, 0, 2-1j, np.inf, np.nan])
-        have = np.sign(C)
+        err = np.seterr(invalid="ignore")
+        try:
+            have = np.sign(C)
+        finally:
+            np.seterr(**err)        
         want = np.array([-1+0j, -1+0j, 0+0j, 1+0j, 1+0j, np.nan])
         assert_equal(have, want)
 

Modified: branches/1.5.x/numpy/core/tests/test_umath.py
===================================================================
--- branches/1.5.x/numpy/core/tests/test_umath.py	2010-08-21 02:32:18 UTC (rev 8657)
+++ branches/1.5.x/numpy/core/tests/test_umath.py	2010-08-22 05:25:08 UTC (rev 8658)
@@ -170,11 +170,15 @@
             np.seterr(**err)
 
     def test_nan(self):
-        assert np.isnan(np.logaddexp2(np.nan, np.inf))
-        assert np.isnan(np.logaddexp2(np.inf, np.nan))
-        assert np.isnan(np.logaddexp2(np.nan, 0))
-        assert np.isnan(np.logaddexp2(0, np.nan))
-        assert np.isnan(np.logaddexp2(np.nan, np.nan))
+        err = np.seterr(invalid="ignore")
+        try:
+            assert np.isnan(np.logaddexp2(np.nan, np.inf))
+            assert np.isnan(np.logaddexp2(np.inf, np.nan))
+            assert np.isnan(np.logaddexp2(np.nan, 0))
+            assert np.isnan(np.logaddexp2(0, np.nan))
+            assert np.isnan(np.logaddexp2(np.nan, np.nan))
+        finally:
+            np.seterr(**err)
 
 
 class TestLog(TestCase):
@@ -236,11 +240,15 @@
             np.seterr(**err)
 
     def test_nan(self):
-        assert np.isnan(np.logaddexp(np.nan, np.inf))
-        assert np.isnan(np.logaddexp(np.inf, np.nan))
-        assert np.isnan(np.logaddexp(np.nan, 0))
-        assert np.isnan(np.logaddexp(0, np.nan))
-        assert np.isnan(np.logaddexp(np.nan, np.nan))
+        err = np.seterr(invalid="ignore")
+        try:
+            assert np.isnan(np.logaddexp(np.nan, np.inf))
+            assert np.isnan(np.logaddexp(np.inf, np.nan))
+            assert np.isnan(np.logaddexp(np.nan, 0))
+            assert np.isnan(np.logaddexp(0, np.nan))
+            assert np.isnan(np.logaddexp(np.nan, np.nan))
+        finally:
+            np.seterr(**err)
 
 
 class TestLog1p(TestCase):
@@ -399,15 +407,23 @@
         arg1 = np.array([0,   nan, nan])
         arg2 = np.array([nan, 0,   nan])
         out  = np.array([nan, nan, nan])
-        assert_equal(np.maximum(arg1, arg2), out)
+        err = np.seterr(invalid="ignore")
+        try:
+            assert_equal(np.maximum(arg1, arg2), out)
+        finally:
+            np.seterr(**err)
 
     def test_complex_nans(self):
         nan = np.nan
-        for cnan in [complex(nan, 0), complex(0, nan), complex(nan, nan)] :
-            arg1 = np.array([0, cnan, cnan], dtype=np.complex)
-            arg2 = np.array([cnan, 0, cnan], dtype=np.complex)
-            out  = np.array([nan, nan, nan], dtype=np.complex)
-            assert_equal(np.maximum(arg1, arg2), out)
+        err = np.seterr(invalid="ignore")
+        try:
+            for cnan in [complex(nan, 0), complex(0, nan), complex(nan, nan)] :
+                arg1 = np.array([0, cnan, cnan], dtype=np.complex)
+                arg2 = np.array([cnan, 0, cnan], dtype=np.complex)
+                out  = np.array([nan, nan, nan], dtype=np.complex)
+                assert_equal(np.maximum(arg1, arg2), out)
+        finally:
+            np.seterr(**err)
 
 
 class TestMinimum(TestCase):
@@ -420,15 +436,23 @@
         arg1 = np.array([0,   nan, nan])
         arg2 = np.array([nan, 0,   nan])
         out  = np.array([nan, nan, nan])
-        assert_equal(np.minimum(arg1, arg2), out)
+        err = np.seterr(invalid="ignore")
+        try:
+            assert_equal(np.minimum(arg1, arg2), out)
+        finally:
+            np.seterr(**err)
 
     def test_complex_nans(self):
         nan = np.nan
-        for cnan in [complex(nan, 0), complex(0, nan), complex(nan, nan)] :
-            arg1 = np.array([0, cnan, cnan], dtype=np.complex)
-            arg2 = np.array([cnan, 0, cnan], dtype=np.complex)
-            out  = np.array([nan, nan, nan], dtype=np.complex)
-            assert_equal(np.minimum(arg1, arg2), out)
+        err = np.seterr(invalid="ignore")
+        try:
+            for cnan in [complex(nan, 0), complex(0, nan), complex(nan, nan)] :
+                arg1 = np.array([0, cnan, cnan], dtype=np.complex)
+                arg2 = np.array([cnan, 0, cnan], dtype=np.complex)
+                out  = np.array([nan, nan, nan], dtype=np.complex)
+                assert_equal(np.minimum(arg1, arg2), out)
+        finally:
+            np.seterr(**err)
 
 
 class TestFmax(TestCase):
@@ -441,15 +465,23 @@
         arg1 = np.array([0,   nan, nan])
         arg2 = np.array([nan, 0,   nan])
         out  = np.array([0,   0,   nan])
-        assert_equal(np.fmax(arg1, arg2), out)
+        err = np.seterr(invalid="ignore")
+        try:
+            assert_equal(np.fmax(arg1, arg2), out)
+        finally:
+            np.seterr(**err)
 
     def test_complex_nans(self):
         nan = np.nan
-        for cnan in [complex(nan, 0), complex(0, nan), complex(nan, nan)] :
-            arg1 = np.array([0, cnan, cnan], dtype=np.complex)
-            arg2 = np.array([cnan, 0, cnan], dtype=np.complex)
-            out  = np.array([0,    0, nan], dtype=np.complex)
-            assert_equal(np.fmax(arg1, arg2), out)
+        err = np.seterr(invalid="ignore")
+        try:
+            for cnan in [complex(nan, 0), complex(0, nan), complex(nan, nan)]:
+                arg1 = np.array([0, cnan, cnan], dtype=np.complex)
+                arg2 = np.array([cnan, 0, cnan], dtype=np.complex)
+                out  = np.array([0,    0, nan], dtype=np.complex)
+                assert_equal(np.fmax(arg1, arg2), out)
+        finally:
+            np.seterr(**err)
 
 
 class TestFmin(TestCase):
@@ -462,15 +494,23 @@
         arg1 = np.array([0,   nan, nan])
         arg2 = np.array([nan, 0,   nan])
         out  = np.array([0,   0,   nan])
-        assert_equal(np.fmin(arg1, arg2), out)
+        err = np.seterr(invalid="ignore")
+        try:
+            assert_equal(np.fmin(arg1, arg2), out)
+        finally:
+            np.seterr(**err)
 
     def test_complex_nans(self):
         nan = np.nan
-        for cnan in [complex(nan, 0), complex(0, nan), complex(nan, nan)] :
-            arg1 = np.array([0, cnan, cnan], dtype=np.complex)
-            arg2 = np.array([cnan, 0, cnan], dtype=np.complex)
-            out  = np.array([0,    0, nan], dtype=np.complex)
-            assert_equal(np.fmin(arg1, arg2), out)
+        err = np.seterr(invalid="ignore")
+        try:
+            for cnan in [complex(nan, 0), complex(0, nan), complex(nan, nan)]:
+                arg1 = np.array([0, cnan, cnan], dtype=np.complex)
+                arg2 = np.array([cnan, 0, cnan], dtype=np.complex)
+                out  = np.array([0,    0, nan], dtype=np.complex)
+                assert_equal(np.fmin(arg1, arg2), out)
+        finally:
+            np.seterr(**err)
 
 
 class TestFloatingPoint(TestCase):
@@ -1080,19 +1120,23 @@
     fins = [complex(1, 0), complex(-1, 0), complex(0, 1), complex(0, -1),
             complex(1, 1), complex(-1, -1), complex(0, 0)]
 
-    for x in nans + fins:
-        x = np.array([x])
-        for y in nans + fins:
-            y = np.array([y])
+    err = np.seterr(invalid="ignore")
+    try:
+        for x in nans + fins:
+            x = np.array([x])
+            for y in nans + fins:
+                y = np.array([y])
+    
+                if np.isfinite(x) and np.isfinite(y):
+                    continue
+    
+                assert_equal(x < y, False, err_msg="%r < %r" % (x, y))
+                assert_equal(x > y, False, err_msg="%r > %r" % (x, y))
+                assert_equal(x <= y, False, err_msg="%r <= %r" % (x, y))
+                assert_equal(x >= y, False, err_msg="%r >= %r" % (x, y))
+                assert_equal(x == y, False, err_msg="%r == %r" % (x, y))
+    finally:
+        np.seterr(**err)
 
-            if np.isfinite(x) and np.isfinite(y):
-                continue
-
-            assert_equal(x < y, False, err_msg="%r < %r" % (x, y))
-            assert_equal(x > y, False, err_msg="%r > %r" % (x, y))
-            assert_equal(x <= y, False, err_msg="%r <= %r" % (x, y))
-            assert_equal(x >= y, False, err_msg="%r >= %r" % (x, y))
-            assert_equal(x == y, False, err_msg="%r == %r" % (x, y))
-
 if __name__ == "__main__":
     run_module_suite()

Modified: branches/1.5.x/numpy/core/tests/test_umath_complex.py
===================================================================
--- branches/1.5.x/numpy/core/tests/test_umath_complex.py	2010-08-21 02:32:18 UTC (rev 8657)
+++ branches/1.5.x/numpy/core/tests/test_umath_complex.py	2010-08-22 05:25:08 UTC (rev 8658)
@@ -239,7 +239,11 @@
         # clog(- inf + i inf) returns +inf + i3pi /4.
         x = np.array([complex(-np.inf, np.inf)], dtype=np.complex)
         y = np.complex(np.inf, 0.75 * np.pi)
-        assert_almost_equal(np.log(x), y)
+        err = np.seterr(invalid="ignore")
+        try:
+            assert_almost_equal(np.log(x), y)
+        finally:
+            np.seterr(**err)
         xl.append(x)
         yl.append(y)
 
@@ -424,9 +428,13 @@
     def test_simple(self):
         x = np.array([1+1j, 0+2j, 1+2j, np.inf, np.nan])
         y_r = np.array([np.sqrt(2.), 2, np.sqrt(5), np.inf, np.nan])
+        err = np.seterr(invalid="ignore")
         y = np.abs(x)
-        for i in range(len(x)):
-            assert_almost_equal(y[i], y_r[i])
+        try:
+            for i in range(len(x)):
+                assert_almost_equal(y[i], y_r[i])
+        finally:
+            np.seterr(**err)
 
     def test_fabs(self):
         # Test that np.abs(x +- 0j) == np.abs(x) (as mandated by C99 for cabs)
@@ -437,7 +445,11 @@
         assert_array_equal(np.abs(x), np.real(x))
 
         x = np.array([complex(np.inf, np.NZERO)], dtype=np.complex)
-        assert_array_equal(np.abs(x), np.real(x))
+        err = np.seterr(invalid="ignore")
+        try:
+            assert_array_equal(np.abs(x), np.real(x))
+        finally:
+            np.seterr(**err)
 
         x = np.array([complex(np.nan, np.NZERO)], dtype=np.complex)
         assert_array_equal(np.abs(x), np.real(x))

Modified: branches/1.5.x/numpy/lib/tests/test_io.py
===================================================================
--- branches/1.5.x/numpy/lib/tests/test_io.py	2010-08-21 02:32:18 UTC (rev 8657)
+++ branches/1.5.x/numpy/lib/tests/test_io.py	2010-08-22 05:25:08 UTC (rev 8658)
@@ -849,7 +849,11 @@
     def test_withmissing(self):
         data = StringIO('A,B\n0,1\n2,N/A')
         kwargs = dict(delimiter=",", missing_values="N/A", names=True)
-        test = np.mafromtxt(data, dtype=None, **kwargs)
+        err = np.seterr(invalid='ignore')
+        try:
+            test = np.mafromtxt(data, dtype=None, **kwargs)
+        finally:
+            np.seterr(**err)
         control = ma.array([(0, 1), (2, -1)],
                            mask=[(False, False), (False, True)],
                            dtype=[('A', np.int), ('B', np.int)])
@@ -857,7 +861,11 @@
         assert_equal(test.mask, control.mask)
         #
         data.seek(0)
-        test = np.mafromtxt(data, **kwargs)
+        err = np.seterr(invalid='ignore')
+        try:
+            test = np.mafromtxt(data, **kwargs)
+        finally:
+            np.seterr(**err)
         control = ma.array([(0, 1), (2, -1)],
                            mask=[(False, False), (False, True)],
                            dtype=[('A', np.float), ('B', np.float)])
@@ -870,8 +878,12 @@
         basekwargs = dict(dtype=None, delimiter=",", names=True,)
         mdtype = [('A', int), ('B', float), ('C', complex)]
         #
-        test = np.mafromtxt(StringIO(data), missing_values="N/A",
-                            **basekwargs)
+        err = np.seterr(invalid='ignore')
+        try:
+            test = np.mafromtxt(StringIO(data), missing_values="N/A",
+                                **basekwargs)
+        finally:
+            np.seterr(**err)
         control = ma.array([(0, 0.0, 0j), (1, -999, 1j),
                             (-9, 2.2, -999j), (3, -99, 3j)],
                             mask=[(0, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 0)],
@@ -879,17 +891,26 @@
         assert_equal(test, control)
         #
         basekwargs['dtype'] = mdtype
-        test = np.mafromtxt(StringIO(data),
-                            missing_values={0:-9, 1:-99, 2:-999j}, **basekwargs)
+        err = np.seterr(invalid='ignore')
+        try:
+            test = np.mafromtxt(StringIO(data),
+                                missing_values={0:-9, 1:-99, 2:-999j},
+                                **basekwargs)
+        finally:
+            np.seterr(**err)
         control = ma.array([(0, 0.0, 0j), (1, -999, 1j),
                             (-9, 2.2, -999j), (3, -99, 3j)],
                             mask=[(0, 0, 0), (0, 1, 0), (1, 0, 1), (0, 1, 0)],
                             dtype=mdtype)
         assert_equal(test, control)
         #
-        test = np.mafromtxt(StringIO(data),
-                            missing_values={0:-9, 'B':-99, 'C':-999j},
-                            **basekwargs)
+        err = np.seterr(invalid='ignore')
+        try:
+            test = np.mafromtxt(StringIO(data),
+                                missing_values={0:-9, 'B':-99, 'C':-999j},
+                                **basekwargs)
+        finally:
+            np.seterr(**err)
         control = ma.array([(0, 0.0, 0j), (1, -999, 1j),
                             (-9, 2.2, -999j), (3, -99, 3j)],
                             mask=[(0, 0, 0), (0, 1, 0), (1, 0, 1), (0, 1, 0)],

Modified: branches/1.5.x/numpy/ma/tests/test_core.py
===================================================================
--- branches/1.5.x/numpy/ma/tests/test_core.py	2010-08-21 02:32:18 UTC (rev 8657)
+++ branches/1.5.x/numpy/ma/tests/test_core.py	2010-08-22 05:25:08 UTC (rev 8658)
@@ -2702,8 +2702,12 @@
             _ = method(out=mout)
             self.assertTrue(mout is not masked)
             assert_equal(mout.mask, True)
-            # Using a ndarray as explicit output
-            _ = method(out=nout)
+            err = np.seterr(invalid="ignore")
+            try:
+                # Using a ndarray as explicit output
+                _ = method(out=nout)
+            finally:
+                np.seterr(**err)
             self.assertTrue(np.isnan(nout))
         #
         x = array(arange(10), mask=True)
@@ -2717,8 +2721,12 @@
             _ = method(out=mout, ddof=1)
             self.assertTrue(mout is not masked)
             assert_equal(mout.mask, True)
-            # Using a ndarray as explicit output
-            _ = method(out=nout, ddof=1)
+            err = np.seterr(invalid="ignore")
+            try:
+                # Using a ndarray as explicit output
+                _ = method(out=nout, ddof=1)
+            finally:
+                np.seterr(**err)
             self.assertTrue(np.isnan(nout))
 
 

Modified: branches/1.5.x/numpy/ma/tests/test_subclassing.py
===================================================================
--- branches/1.5.x/numpy/ma/tests/test_subclassing.py	2010-08-21 02:32:18 UTC (rev 8657)
+++ branches/1.5.x/numpy/ma/tests/test_subclassing.py	2010-08-22 05:25:08 UTC (rev 8658)
@@ -94,7 +94,11 @@
         "Tests masked_unary_operation"
         (x, mx) = self.data
         self.assertTrue(isinstance(log(mx), mmatrix))
-        assert_equal(log(x), np.log(x))
+        err = np.seterr(divide="ignore")
+        try:
+            assert_equal(log(x), np.log(x))
+        finally:
+            np.seterr(**err)
 
     def test_masked_binary_operations(self):
         "Tests masked_binary_operation"



More information about the Numpy-svn mailing list