Permalink
Browse files

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.
  • Loading branch information...
1 parent bd31169 commit 1c4ebaa6ce55c2e5efbaee2df6eb8ae9bcb43799 @rgommers rgommers committed Jan 15, 2011
View
@@ -2437,7 +2437,11 @@ def spearmanr(a, b=None, axis=0):
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):
@@ -158,33 +158,40 @@
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
+ 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():
@@ -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 @@ def test_discrete_basic():
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 @@ def check_discrete_chisquare(distfn, arg, rvs, alpha, msg):
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)))
@@ -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 @@ def test_expon(self):
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,6 +102,9 @@ def test_bad_arg(self):
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):
@@ -124,7 +133,7 @@ def test_trimmed1(self):
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 @@ def test_equal_mean_median(self):
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_keyword(self):
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 @@ def test_trimmed1(self):
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 @@ def test_trimmed2(self):
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,
@@ -128,11 +128,15 @@ class TestCorr(TestCase):
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)
#
@@ -158,7 +158,11 @@ def test_nanmean_some(self):
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_some(self):
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 @@ def test_ttest_rel():
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,16 +1402,18 @@ def test_ttest_ind():
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))
-
- #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]))
-
+ 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]))
+ finally:
+ np.seterr(**olderr)
def test_ttest_1samp_new():
@@ -1429,14 +1443,19 @@ def test_ttest_1samp_new():
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]))
+ finally:
+ np.seterr(**olderr)
- #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]))
def test_describe():
x = np.vstack((np.ones((3,4)),2*np.ones((2,4))))
@@ -1663,22 +1682,41 @@ def test_1dlist0(self):
''' 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):

0 comments on commit 1c4ebaa

Please sign in to comment.