[Scipy-svn] r7123 - in trunk/scipy: interpolate/tests special/tests stats/tests

scipy-svn at scipy.org scipy-svn at scipy.org
Wed Feb 2 17:52:06 EST 2011


Author: ptvirtan
Date: 2011-02-02 16:52:06 -0600 (Wed, 02 Feb 2011)
New Revision: 7123

Modified:
   trunk/scipy/interpolate/tests/test_rbf.py
   trunk/scipy/special/tests/test_data.py
   trunk/scipy/stats/tests/test_continuous_basic.py
Log:
TST: Use np.seterr in test generators in Python2.4 compatible way

The seterr should be called in the test routine itself and not in the
test generator.

Modified: trunk/scipy/interpolate/tests/test_rbf.py
===================================================================
--- trunk/scipy/interpolate/tests/test_rbf.py	2011-02-02 22:51:52 UTC (rev 7122)
+++ trunk/scipy/interpolate/tests/test_rbf.py	2011-02-02 22:52:06 UTC (rev 7123)
@@ -12,59 +12,71 @@
 
 def check_rbf1d_interpolation(function):
     """Check that the Rbf function interpolates throught the nodes (1D)"""
-    x = linspace(0,10,9)
-    y = sin(x)
-    rbf = Rbf(x, y, function=function)
-    yi = rbf(x)
-    assert_array_almost_equal(y, yi)
-    assert_almost_equal(rbf(float(x[0])), y[0])
+    olderr = np.seterr(all="ignore")
+    try:
+        x = linspace(0,10,9)
+        y = sin(x)
+        rbf = Rbf(x, y, function=function)
+        yi = rbf(x)
+        assert_array_almost_equal(y, yi)
+        assert_almost_equal(rbf(float(x[0])), y[0])
+    finally:
+        np.seterr(**olderr)
 
 def check_rbf2d_interpolation(function):
     """Check that the Rbf function interpolates throught the nodes (2D)"""
-    x = random.rand(50,1)*4-2
-    y = random.rand(50,1)*4-2
-    z = x*exp(-x**2-1j*y**2)
-    rbf = Rbf(x, y, z, epsilon=2, function=function)
-    zi = rbf(x, y)
-    zi.shape = x.shape
-    assert_array_almost_equal(z, zi)
+    olderr = np.seterr(all="ignore")
+    try:
+        x = random.rand(50,1)*4-2
+        y = random.rand(50,1)*4-2
+        z = x*exp(-x**2-1j*y**2)
+        rbf = Rbf(x, y, z, epsilon=2, function=function)
+        zi = rbf(x, y)
+        zi.shape = x.shape
+        assert_array_almost_equal(z, zi)
+    finally:
+        np.seterr(**olderr)
 
 def check_rbf3d_interpolation(function):
     """Check that the Rbf function interpolates throught the nodes (3D)"""
-    x = random.rand(50,1)*4-2
-    y = random.rand(50,1)*4-2
-    z = random.rand(50,1)*4-2
-    d = x*exp(-x**2-y**2)
-    rbf = Rbf(x, y, z, d, epsilon=2, function=function)
-    di = rbf(x, y, z)
-    di.shape = x.shape
-    assert_array_almost_equal(di, d)
-
-def test_rbf_interpolation():
     olderr = np.seterr(all="ignore")
     try:
-        for function in FUNCTIONS:
-            yield check_rbf1d_interpolation, function
-            yield check_rbf2d_interpolation, function
-            yield check_rbf3d_interpolation, function
+        x = random.rand(50,1)*4-2
+        y = random.rand(50,1)*4-2
+        z = random.rand(50,1)*4-2
+        d = x*exp(-x**2-y**2)
+        rbf = Rbf(x, y, z, d, epsilon=2, function=function)
+        di = rbf(x, y, z)
+        di.shape = x.shape
+        assert_array_almost_equal(di, d)
     finally:
         np.seterr(**olderr)
 
+def test_rbf_interpolation():
+    for function in FUNCTIONS:
+        yield check_rbf1d_interpolation, function
+        yield check_rbf2d_interpolation, function
+        yield check_rbf3d_interpolation, function
+
 def check_rbf1d_regularity(function, atol):
     """Check that the Rbf function approximates a smooth function well away
     from the nodes."""
-    x = linspace(0, 10, 9)
-    y = sin(x)
-    rbf = Rbf(x, y, function=function)
-    xi = linspace(0, 10, 100)
-    yi = rbf(xi)
-    #import matplotlib.pyplot as plt
-    #plt.figure()
-    #plt.plot(x, y, 'o', xi, sin(xi), ':', xi, yi, '-')
-    #plt.title(function)
-    #plt.show()
-    msg = "abs-diff: %f" % abs(yi - sin(xi)).max()
-    assert_(allclose(yi, sin(xi), atol=atol), msg)
+    olderr = np.seterr(all="ignore")
+    try:
+        x = linspace(0, 10, 9)
+        y = sin(x)
+        rbf = Rbf(x, y, function=function)
+        xi = linspace(0, 10, 100)
+        yi = rbf(xi)
+        #import matplotlib.pyplot as plt
+        #plt.figure()
+        #plt.plot(x, y, 'o', xi, sin(xi), ':', xi, yi, '-')
+        #plt.title(function)
+        #plt.show()
+        msg = "abs-diff: %f" % abs(yi - sin(xi)).max()
+        assert_(allclose(yi, sin(xi), atol=atol), msg)
+    finally:
+        np.seterr(**olderr)
 
 def test_rbf_regularity():
     tolerances = {
@@ -76,12 +88,8 @@
         'thin-plate': 0.1,
         'linear': 0.2
     }
-    olderr = np.seterr(all="ignore")
-    try:
-        for function in FUNCTIONS:
-            yield check_rbf1d_regularity, function, tolerances.get(function, 1e-2)
-    finally:
-        np.seterr(**olderr)
+    for function in FUNCTIONS:
+        yield check_rbf1d_regularity, function, tolerances.get(function, 1e-2)
 
 def test_default_construction():
     """Check that the Rbf class can be constructed with the default

Modified: trunk/scipy/special/tests/test_data.py
===================================================================
--- trunk/scipy/special/tests/test_data.py	2011-02-02 22:51:52 UTC (rev 7122)
+++ trunk/scipy/special/tests/test_data.py	2011-02-02 22:52:06 UTC (rev 7123)
@@ -206,13 +206,13 @@
         # tgamma_ratio_data.txt
     ]
 
+    for test in TESTS:
+        yield _test_factory, test
+
+def _test_factory(test, dtype=np.double):
+    """Boost test"""
     olderr = np.seterr(all='ignore')
     try:
-        for test in TESTS:
-            yield _test_factory, test
+        test.check(dtype=dtype)
     finally:
         np.seterr(**olderr)
-
-def _test_factory(test, dtype=np.double):
-    """Boost test"""
-    test.check(dtype=dtype)

Modified: trunk/scipy/stats/tests/test_continuous_basic.py
===================================================================
--- trunk/scipy/stats/tests/test_continuous_basic.py	2011-02-02 22:51:52 UTC (rev 7122)
+++ trunk/scipy/stats/tests/test_continuous_basic.py	2011-02-02 22:52:06 UTC (rev 7123)
@@ -156,43 +156,48 @@
             'powerlognorm', 'johnsonsu', 'kstwobign']
 #distslow are sorted by speed (very slow to slow)
 
+def _silence_fp_errors(func):
+    def wrap(*a, **kw):
+        olderr = np.seterr(all='ignore')
+        try:
+            return func(*a, **kw)
+        finally:
+            np.seterr(**olderr)
+    wrap.__name__ = func.__name__
+    return wrap
+
 def test_cont_basic():
     # this test skips slow distributions
-    olderr = np.seterr(all='ignore')
-    try:
-        for distname, arg in distcont[:]:
-            if distname in distslow:
-                continue
-            distfn = getattr(stats, distname)
-            np.random.seed(765456)
-            sn = 1000
-            rvs = distfn.rvs(size=sn,*arg)
-            sm = rvs.mean()
-            sv = rvs.var()
-            skurt = stats.kurtosis(rvs)
-            sskew = stats.skew(rvs)
-            m,v = distfn.stats(*arg)
+    for distname, arg in distcont[:]:
+        if distname in distslow:
+            continue
+        distfn = getattr(stats, distname)
+        np.random.seed(765456)
+        sn = 1000
+        rvs = distfn.rvs(size=sn,*arg)
+        sm = rvs.mean()
+        sv = rvs.var()
+        skurt = stats.kurtosis(rvs)
+        sskew = stats.skew(rvs)
+        m,v = distfn.stats(*arg)
 
-            yield check_sample_meanvar_, distfn, arg, m, v, sm, sv, sn, distname + \
-                  'sample mean test'
-            # the sample skew kurtosis test has known failures, not very good distance measure
-            #yield check_sample_skew_kurt, distfn, arg, sskew, skurt, distname
-            yield check_moment, distfn, arg, m, v, distname
-            yield check_cdf_ppf, distfn, arg, distname
-            yield check_sf_isf, distfn, arg, distname
-            yield check_pdf, distfn, arg, distname
-            if distname in ['wald']:
-                continue
-            yield check_pdf_logpdf, distfn, arg, distname
-            yield check_cdf_logcdf, distfn, arg, distname
-            yield check_sf_logsf, distfn, arg, distname
-            if distname in distmissing:
-                alpha = 0.01
-                yield check_distribution_rvs, distname, arg, alpha, rvs
-    finally:
-        np.seterr(**olderr)
+        yield check_sample_meanvar_, distfn, arg, m, v, sm, sv, sn, distname + \
+              'sample mean test'
+        # the sample skew kurtosis test has known failures, not very good distance measure
+        #yield check_sample_skew_kurt, distfn, arg, sskew, skurt, distname
+        yield check_moment, distfn, arg, m, v, distname
+        yield check_cdf_ppf, distfn, arg, distname
+        yield check_sf_isf, distfn, arg, distname
+        yield check_pdf, distfn, arg, distname
+        if distname in ['wald']:
+            continue
+        yield check_pdf_logpdf, distfn, arg, distname
+        yield check_cdf_logcdf, distfn, arg, distname
+        yield check_sf_logsf, distfn, arg, distname
+        if distname in distmissing:
+            alpha = 0.01
+            yield check_distribution_rvs, distname, arg, alpha, rvs
 
-
 @npt.dec.slow
 def test_cont_basic_slow():
     # same as above for slow distributions
@@ -223,6 +228,7 @@
             alpha = 0.01
             yield check_distribution_rvs, distname, arg, alpha, rvs
 
+ at _silence_fp_errors
 def check_moment(distfn, arg, m, v, msg):
     m1  = distfn.moment(1,*arg)
     m2  = distfn.moment(2,*arg)
@@ -241,6 +247,7 @@
                msg + ' - 2nd moment -infinite, m2=%s' % str(m2))
         #np.isnan(m2) temporary special treatment for loggamma
 
+ at _silence_fp_errors
 def check_sample_meanvar_(distfn, arg, m, v, sm, sv, sn, msg):
     #this did not work, skipped silently by nose
     #check_sample_meanvar, sm, m, msg + 'sample mean test'
@@ -296,11 +303,13 @@
 ##    else:
 ##        assert abs(sm) > 10000, 'infinite moment, sm = ' + str(sm)
 
+ at _silence_fp_errors
 def check_cdf_ppf(distfn,arg,msg):
     npt.assert_almost_equal(distfn.cdf(distfn.ppf([0.001,0.5,0.999], *arg), *arg),
                             [0.001,0.5,0.999], decimal=DECIMAL, err_msg= msg + \
                             ' - cdf-ppf roundtrip')
 
+ at _silence_fp_errors
 def check_sf_isf(distfn,arg,msg):
     npt.assert_almost_equal(distfn.sf(distfn.isf([0.1,0.5,0.9], *arg), *arg),
                             [0.1,0.5,0.9], decimal=DECIMAL, err_msg= msg + \
@@ -310,6 +319,7 @@
                             decimal=DECIMAL, err_msg= msg + \
                             ' - cdf-sf relationship')
 
+ at _silence_fp_errors
 def check_pdf(distfn, arg, msg):
     # compares pdf at median with numerical derivative of cdf
     median = distfn.ppf(0.5, *arg)
@@ -326,6 +336,7 @@
     npt.assert_almost_equal(pdfv, cdfdiff,
                 decimal=DECIMAL, err_msg= msg + ' - cdf-pdf relationship')
 
+ at _silence_fp_errors
 def check_pdf_logpdf(distfn, args, msg):
     # compares pdf at several points with the log of the pdf
     points = np.array([0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8])
@@ -336,6 +347,7 @@
     logpdf = logpdf[np.isfinite(logpdf)]
     npt.assert_almost_equal(np.log(pdf), logpdf, decimal=7, err_msg=msg + " - logpdf-log(pdf) relationship")
 
+ at _silence_fp_errors
 def check_sf_logsf(distfn, args, msg):
     # compares sf at several points with the log of the sf
     points = np.array([0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8])
@@ -346,6 +358,7 @@
     logsf = logsf[np.isfinite(logsf)]
     npt.assert_almost_equal(np.log(sf), logsf, decimal=7, err_msg=msg + " - logsf-log(sf) relationship")
 
+ at _silence_fp_errors
 def check_cdf_logcdf(distfn, args, msg):
     # compares cdf at several points with the log of the cdf
     points = np.array([0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8])
@@ -357,6 +370,7 @@
     npt.assert_almost_equal(np.log(cdf), logcdf, decimal=7, err_msg=msg + " - logcdf-log(cdf) relationship")
 
 
+ at _silence_fp_errors
 def check_distribution_rvs(dist, args, alpha, rvs):
     #test from scipy.stats.tests
     #this version reuses existing random variables




More information about the Scipy-svn mailing list