1
2
3
4
5
6
7
8
9 """Unit tests for PyMVPA stats helpers"""
10
11 from mvpa.base import externals
12 from mvpa.clfs.stats import MCNullDist, FixedNullDist, NullDist
13 from mvpa.datasets import Dataset
14 from mvpa.measures.glm import GLM
15 from mvpa.measures.anova import OneWayAnova, CompoundOneWayAnova
16 from mvpa.misc.fx import doubleGammaHRF, singleGammaHRF
17 from tests_warehouse import *
18 from mvpa import cfg
19 from numpy.testing import assert_array_almost_equal
20
21
22
23 nulldist_sweep = [ MCNullDist(permutations=30, tail='any'),
24 MCNullDist(permutations=30, tail='right')]
25
26 if externals.exists('scipy'):
27 from mvpa.support.stats import scipy
28 from scipy.stats import f_oneway
29 nulldist_sweep += [ MCNullDist(scipy.stats.norm, permutations=30,
30 tail='any'),
31 MCNullDist(scipy.stats.norm, permutations=30,
32 tail='right'),
33 MCNullDist(scipy.stats.expon, permutations=30,
34 tail='right'),
35 FixedNullDist(scipy.stats.norm(0, 10.0), tail='any'),
36 FixedNullDist(scipy.stats.norm(0, 10.0), tail='right'),
37 scipy.stats.norm(0, 0.1)
38 ]
39
41 """Unittests for various statistics"""
42
43
44 @sweepargs(null=nulldist_sweep[1:])
46 """Testing null dist probability"""
47 if not isinstance(null, NullDist):
48 return
49 ds = datasets['uni2small']
50
51 null.fit(OneWayAnova(), ds)
52
53
54
55
56 prob = null.p([20, 0, 0, 0, 0, N.nan])
57
58
59
60 if cfg.getboolean('tests', 'labile', default='yes'):
61 self.failUnless(N.abs(prob[0]) < 0.05,
62 msg="Expected small p, got %g" % prob[0])
63 if cfg.getboolean('tests', 'labile', default='yes'):
64 self.failUnless((N.abs(prob[1:]) > 0.05).all(),
65 msg="Bogus features should have insignificant p."
66 " Got %s" % (N.abs(prob[1:]),))
67
68
69 if not isinstance(null, FixedNullDist):
70
71
72 self.failUnlessRaises(ValueError, null.p, [5, 3, 4])
73
74
76 """Do some extended testing of OneWayAnova
77
78 in particular -- compound estimation
79 """
80
81 m = OneWayAnova()
82 mc = CompoundOneWayAnova(combiner=None)
83 ds = datasets['uni2medium']
84
85
86
87 a, ac = m(ds), mc(ds)
88
89 self.failUnless(a.shape == (ds.nfeatures,))
90 self.failUnless(ac.shape == (ds.nfeatures, len(ds.uniquelabels)))
91
92 self.failUnless((ac[:, 0] == ac[:, 1]).all())
93 self.failUnless((a == ac[:, 1]).all())
94
95 ds = datasets['uni4large']
96 ac = mc(ds)
97
98 if cfg.getboolean('tests', 'labile', default='yes'):
99
100 self.failUnless((ac[(N.array(ds.nonbogus_features),
101 N.arange(4))] >= 1).all())
102
103
104
105 self.failUnless(N.max(N.std(ac, axis=1))>0,
106 msg='In compound anova, we should get different'
107 ' results for different labels. Got %s' % ac)
108
110 """Create the suite"""
111 return unittest.makeSuite(StatsTests)
112
113
114 if __name__ == '__main__':
115 import runner
116