[Scipy-svn] r7041 - in trunk/scipy/stats: . tests

scipy-svn at scipy.org scipy-svn at scipy.org
Sat Jan 15 07:47:36 EST 2011


Author: rgommers
Date: 2011-01-15 06:47:35 -0600 (Sat, 15 Jan 2011)
New Revision: 7041

Modified:
   trunk/scipy/stats/stats.py
   trunk/scipy/stats/tests/test_continuous_basic.py
   trunk/scipy/stats/tests/test_discrete_basic.py
   trunk/scipy/stats/tests/test_morestats.py
   trunk/scipy/stats/tests/test_mstats_basic.py
   trunk/scipy/stats/tests/test_stats.py
Log:
TST: filter all test noise from scipy.stats

The tests for the zipf distribution are disabled, because they were generating
floating point warnings for many (all) methods that can't easily be filtered.
The chisquare test of dlaplace is disabled for the same reason.

Modified: trunk/scipy/stats/stats.py
===================================================================
--- trunk/scipy/stats/stats.py	2011-01-15 12:46:40 UTC (rev 7040)
+++ trunk/scipy/stats/stats.py	2011-01-15 12:47:35 UTC (rev 7041)
@@ -2437,7 +2437,11 @@
     n = a.shape[axisout]
     rs = np.corrcoef(ar,br,rowvar=axisout)
 
-    t = rs * np.sqrt((n-2) / ((rs+1.0)*(1.0-rs)))
+    olderr = np.seterr(divide='ignore')  # rs can have elements equal to 1
+    try:
+        t = rs * np.sqrt((n-2) / ((rs+1.0)*(1.0-rs)))
+    finally:
+        np.seterr(**olderr)
     prob = distributions.t.sf(np.abs(t),n-2)*2
 
     if rs.shape == (2,2):

Modified: trunk/scipy/stats/tests/test_continuous_basic.py
===================================================================
--- trunk/scipy/stats/tests/test_continuous_basic.py	2011-01-15 12:46:40 UTC (rev 7040)
+++ trunk/scipy/stats/tests/test_continuous_basic.py	2011-01-15 12:47:35 UTC (rev 7041)
@@ -158,34 +158,41 @@
 
 def test_cont_basic():
     # this test skips slow distributions
-    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)
+    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)
 
-        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
+            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)
 
+
 @npt.dec.slow
 def test_cont_basic_slow():
     # same as above for slow distributions

Modified: trunk/scipy/stats/tests/test_discrete_basic.py
===================================================================
--- trunk/scipy/stats/tests/test_discrete_basic.py	2011-01-15 12:46:40 UTC (rev 7040)
+++ trunk/scipy/stats/tests/test_discrete_basic.py	2011-01-15 12:47:35 UTC (rev 7041)
@@ -21,8 +21,8 @@
     ['planck',   (0.51,)],   #4.1
     ['poisson',  (0.6,)],
     ['randint',  (7, 31)],
-    ['skellam',  (15, 8)],
-    ['zipf',     (4,)] ]   # arg=4 is ok,
+    ['skellam',  (15, 8)]]
+#    ['zipf',     (4,)] ]   # arg=4 is ok,
                            # Zipf broken for arg = 2, e.g. weird .stats
                            # looking closer, mean, var should be inf for arg=2
 
@@ -43,16 +43,24 @@
         yield check_cdf_ppf, distfn, arg, distname + ' cdf_ppf'
         yield check_cdf_ppf2, distfn, arg, supp, distname + ' cdf_ppf'
         yield check_pmf_cdf, distfn, arg, distname + ' pmf_cdf'
-        yield check_oth, distfn, arg, distname + ' oth'
-        skurt = stats.kurtosis(rvs)
-        sskew = stats.skew(rvs)
-        yield check_sample_skew_kurt, distfn, arg, skurt, sskew, \
-                      distname + ' skew_kurt'
-        if not distname in ['']:#['logser']:  #known failure, fixed
+
+        # zipf doesn't fail, but generates floating point warnings.
+        # Should be checked.
+        if not distname in ['zipf']:
+            yield check_oth, distfn, arg, distname + ' oth'
+            skurt = stats.kurtosis(rvs)
+            sskew = stats.skew(rvs)
+            yield check_sample_skew_kurt, distfn, arg, skurt, sskew, \
+                          distname + ' skew_kurt'
+
+        # dlaplace doesn't fail, but generates lots of floating point warnings.
+        # Should be checked.
+        if not distname in ['dlaplace']: #['logser']:  #known failure, fixed
             alpha = 0.01
             yield check_discrete_chisquare, distfn, arg, rvs, alpha, \
                           distname + ' chisquare'
 
+
 @npt.dec.slow
 def test_discrete_extra():
     for distname, arg in distdiscrete:
@@ -252,7 +260,7 @@
     cdfs = distfn.cdf(distsupp,*arg)
     (chis,pval) = stats.chisquare(np.array(freq),n*distmass)
 
-    npt.assert_(pval > alpha, 'chisquare - test for %s' 
+    npt.assert_(pval > alpha, 'chisquare - test for %s'
            ' at arg = %s with pval = %s' % (msg,str(arg),str(pval)))
 
 

Modified: trunk/scipy/stats/tests/test_morestats.py
===================================================================
--- trunk/scipy/stats/tests/test_morestats.py	2011-01-15 12:46:40 UTC (rev 7040)
+++ trunk/scipy/stats/tests/test_morestats.py	2011-01-15 12:47:35 UTC (rev 7041)
@@ -3,6 +3,8 @@
 # Further enhancements and tests added by numerous SciPy developers.
 #
 
+import warnings
+
 from numpy.testing import TestCase, run_module_suite, assert_array_equal, \
     assert_almost_equal, assert_array_less, assert_array_almost_equal, \
     assert_raises
@@ -62,7 +64,11 @@
         x2 = rs.standard_normal(size=50)
         A,crit,sig = stats.anderson(x1,'expon')
         assert_array_less(A, crit[-2:])
-        A,crit,sig = stats.anderson(x2,'expon')
+        olderr = np.seterr(all='ignore')
+        try:
+            A,crit,sig = stats.anderson(x2,'expon')
+        finally:
+            np.seterr(**olderr)
         assert_array_less(crit[:-1], A)
 
     def test_bad_arg(self):
@@ -96,7 +102,10 @@
         assert_raises(ValueError, stats.ansari, [], [1])
         assert_raises(ValueError, stats.ansari, [1], [])
 
+warnings.filterwarnings('ignore',
+                        message="Ties preclude use of exact statistic.")
 
+
 class TestBartlett(TestCase):
 
     def test_data(self):
@@ -124,7 +133,7 @@
         W2, pval2 = stats.levene(g1, g2, g3, center='trimmed', proportiontocut=0.0)
         assert_almost_equal(W1, W2)
         assert_almost_equal(pval1, pval2)
-    
+
     def test_trimmed2(self):
         x = [1.2, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 100.0]
         y = [0.0, 3.0, 3.5, 4.0, 4.5, 5.0, 5.5, 200.0]
@@ -141,7 +150,7 @@
         y = x**3
         W1, pval1 = stats.levene(x, y, center='mean')
         W2, pval2 = stats.levene(x, y, center='median')
-        assert_almost_equal(W1, W2)        
+        assert_almost_equal(W1, W2)
         assert_almost_equal(pval1, pval2)
 
     def test_bad_keyword(self):
@@ -151,7 +160,7 @@
     def test_bad_center_value(self):
         x = np.linspace(-1,1,21)
         assert_raises(ValueError, stats.levene, x, x, center='trim')
-        
+
     def test_too_few_args(self):
         assert_raises(ValueError, stats.levene, [1])
 
@@ -203,7 +212,7 @@
         Xsq2, pval2 = stats.fligner(g1, g2, g3, center='trimmed', proportiontocut=0.0)
         assert_almost_equal(Xsq1, Xsq2)
         assert_almost_equal(pval1, pval2)
-        
+
     def test_trimmed2(self):
         x = [1.2, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 100.0]
         y = [0.0, 3.0, 3.5, 4.0, 4.5, 5.0, 5.5, 200.0]
@@ -213,7 +222,7 @@
         Xsq2, pval2 = stats.fligner(x[1:-1], y[1:-1], center='mean')
         # Result should be the same.
         assert_almost_equal(Xsq1, Xsq2)
-        assert_almost_equal(pval1, pval2) 
+        assert_almost_equal(pval1, pval2)
 
     # The following test looks reasonable at first, but fligner() uses the
     # function stats.rankdata(), and in one of the cases in this test,

Modified: trunk/scipy/stats/tests/test_mstats_basic.py
===================================================================
--- trunk/scipy/stats/tests/test_mstats_basic.py	2011-01-15 12:46:40 UTC (rev 7040)
+++ trunk/scipy/stats/tests/test_mstats_basic.py	2011-01-15 12:47:35 UTC (rev 7041)
@@ -128,11 +128,15 @@
     def test_pearsonr(self):
         "Tests some computations of Pearson's r"
         x = ma.arange(10)
-        assert_almost_equal(mstats.pearsonr(x,x)[0], 1.0)
-        assert_almost_equal(mstats.pearsonr(x,x[::-1])[0], -1.0)
-        #
-        x = ma.array(x, mask=True)
-        pr = mstats.pearsonr(x,x)
+        olderr = np.seterr(all='ignore')
+        try:
+            assert_almost_equal(mstats.pearsonr(x,x)[0], 1.0)
+            assert_almost_equal(mstats.pearsonr(x,x[::-1])[0], -1.0)
+
+            x = ma.array(x, mask=True)
+            pr = mstats.pearsonr(x,x)
+        finally:
+            np.seterr(**olderr)
         assert_(pr[0] is masked)
         assert_(pr[1] is masked)
     #

Modified: trunk/scipy/stats/tests/test_stats.py
===================================================================
--- trunk/scipy/stats/tests/test_stats.py	2011-01-15 12:46:40 UTC (rev 7040)
+++ trunk/scipy/stats/tests/test_stats.py	2011-01-15 12:47:35 UTC (rev 7041)
@@ -158,7 +158,11 @@
 
     def test_nanmean_all(self):
         """Check nanmean when all values are nan."""
-        m = stats.nanmean(self.Xall)
+        olderr = np.seterr(all='ignore')
+        try:
+            m = stats.nanmean(self.Xall)
+        finally:
+            np.seterr(**olderr)
         assert_(np.isnan(m))
 
     def test_nanstd_none(self):
@@ -173,7 +177,11 @@
 
     def test_nanstd_all(self):
         """Check nanstd when all values are nan."""
-        s = stats.nanstd(self.Xall)
+        olderr = np.seterr(all='ignore')
+        try:
+            s = stats.nanstd(self.Xall)
+        finally:
+            np.seterr(**olderr)
         assert_(np.isnan(s))
 
     def test_nanstd_negative_axis(self):
@@ -1349,14 +1357,18 @@
     assert_array_almost_equal(np.abs(p), pr)
     assert_equal(t.shape, (3, 2))
 
-    #test zero division problem
-    t,p = stats.ttest_rel([0,0,0],[1,1,1])
-    assert_equal((np.abs(t),p), (np.inf, 0))
-    assert_almost_equal(stats.ttest_rel([0,0,0], [0,0,0]), (1.0, 0.42264973081037421))
+    olderr = np.seterr(all='ignore')
+    try:
+        #test zero division problem
+        t,p = stats.ttest_rel([0,0,0],[1,1,1])
+        assert_equal((np.abs(t),p), (np.inf, 0))
+        assert_almost_equal(stats.ttest_rel([0,0,0], [0,0,0]), (1.0, 0.42264973081037421))
 
-    #check that nan in input array result in nan output
-    anan = np.array([[1,np.nan],[-1,1]])
-    assert_equal(stats.ttest_ind(anan, np.zeros((2,2))),([0, np.nan], [1,np.nan]))
+        #check that nan in input array result in nan output
+        anan = np.array([[1,np.nan],[-1,1]])
+        assert_equal(stats.ttest_ind(anan, np.zeros((2,2))),([0, np.nan], [1,np.nan]))
+    finally:
+        np.seterr(**olderr)
 
 
 def test_ttest_ind():
@@ -1390,18 +1402,20 @@
     assert_array_almost_equal(np.abs(p), pr)
     assert_equal(t.shape, (3, 2))
 
-    #test zero division problem
-    t,p = stats.ttest_ind([0,0,0],[1,1,1])
-    assert_equal((np.abs(t),p), (np.inf, 0))
-    assert_almost_equal(stats.ttest_ind([0,0,0], [0,0,0]), (1.0, 0.37390096630005898))
+    olderr = np.seterr(all='ignore')
+    try:
+        #test zero division problem
+        t,p = stats.ttest_ind([0,0,0],[1,1,1])
+        assert_equal((np.abs(t),p), (np.inf, 0))
+        assert_almost_equal(stats.ttest_ind([0,0,0], [0,0,0]), (1.0, 0.37390096630005898))
 
-    #check that nan in input array result in nan output
-    anan = np.array([[1,np.nan],[-1,1]])
-    assert_equal(stats.ttest_ind(anan, np.zeros((2,2))),([0, np.nan], [1,np.nan]))
+        #check that nan in input array result in nan output
+        anan = np.array([[1,np.nan],[-1,1]])
+        assert_equal(stats.ttest_ind(anan, np.zeros((2,2))),([0, np.nan], [1,np.nan]))
+    finally:
+        np.seterr(**olderr)
 
 
-
-
 def test_ttest_1samp_new():
     n1, n2, n3 = (10,15,20)
     rvn1 = stats.norm.rvs(loc=5,scale=10,size=(n1,n2,n3))
@@ -1429,15 +1443,20 @@
     assert_almost_equal(t1[0,0],t3, decimal=14)
     assert_equal(t1.shape, (n1,n2))
 
-    #test zero division problem
-    t,p = stats.ttest_1samp([0,0,0], 1)
-    assert_equal((np.abs(t),p), (np.inf, 0))
-    assert_almost_equal(stats.ttest_1samp([0,0,0], 0), (1.0, 0.42264973081037421))
+    olderr = np.seterr(all='ignore')
+    try:
+        #test zero division problem
+        t,p = stats.ttest_1samp([0,0,0], 1)
+        assert_equal((np.abs(t),p), (np.inf, 0))
+        assert_almost_equal(stats.ttest_1samp([0,0,0], 0), (1.0, 0.42264973081037421))
 
-    #check that nan in input array result in nan output
-    anan = np.array([[1,np.nan],[-1,1]])
-    assert_equal(stats.ttest_1samp(anan, 0),([0, np.nan], [1,np.nan]))
+        #check that nan in input array result in nan output
+        anan = np.array([[1,np.nan],[-1,1]])
+        assert_equal(stats.ttest_1samp(anan, 0),([0, np.nan], [1,np.nan]))
+    finally:
+        np.seterr(**olderr)
 
+
 def test_describe():
     x = np.vstack((np.ones((3,4)),2*np.ones((2,4))))
     nc, mmc = (5, ([ 1.,  1.,  1.,  1.], [ 2.,  2.,  2.,  2.]))
@@ -1663,22 +1682,41 @@
         ''' Test a 1d list with zero element'''
         a=[10, 20, 30, 40, 50, 60, 70, 80, 90, 0]
         b = 0.0 # due to exp(-inf)=0
-        self.do(a, b)
+        olderr = np.seterr(all='ignore')
+        try:
+            self.do(a, b)
+        finally:
+            np.seterr(**olderr)
+
     def test_1darray0(self):
         ''' Test a 1d array with zero element'''
         a=np.array([10, 20, 30, 40, 50, 60, 70, 80, 90, 0])
         b = 0.0 # due to exp(-inf)=0
-        self.do(a, b)
+        olderr = np.seterr(all='ignore')
+        try:
+            self.do(a, b)
+        finally:
+            np.seterr(**olderr)
+
     def test_1dma0(self):
         ''' Test a 1d masked array with zero element'''
         a=np.ma.array([10, 20, 30, 40, 50, 60, 70, 80, 90, 0])
         b = 41.4716627439
-        self.do(a, b)
+        olderr = np.seterr(all='ignore')
+        try:
+            self.do(a, b)
+        finally:
+            np.seterr(**olderr)
+
     def test_1dmainf(self):
         ''' Test a 1d masked array with negative element'''
         a=np.ma.array([10, 20, 30, 40, 50, 60, 70, 80, 90, -1])
         b = 41.4716627439
-        self.do(a, b)
+        olderr = np.seterr(all='ignore')
+        try:
+            self.do(a, b)
+        finally:
+            np.seterr(**olderr)
 
 class TestGeoMean(GeoMeanTestCase, TestCase):
     def do(self, a, b, axis=None, dtype=None):




More information about the Scipy-svn mailing list