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 from mvpa.clfs.stats import rv_semifrozen
30 nulldist_sweep += [ MCNullDist(scipy.stats.norm, permutations=30,
31 tail='any'),
32 MCNullDist(scipy.stats.norm, permutations=30,
33 tail='right'),
34 MCNullDist(rv_semifrozen(scipy.stats.norm, loc=0),
35 permutations=30, tail='right'),
36 MCNullDist(scipy.stats.expon, permutations=30,
37 tail='right'),
38 FixedNullDist(scipy.stats.norm(0, 10.0), tail='any'),
39 FixedNullDist(scipy.stats.norm(0, 10.0), tail='right'),
40 scipy.stats.norm(0, 0.1)
41 ]
44 """Unittests for various statistics"""
45
46
47 @sweepargs(null=nulldist_sweep[1:])
49 """Testing null dist probability"""
50 if not isinstance(null, NullDist):
51 return
52 ds = datasets['uni2small']
53
54 null.fit(OneWayAnova(), ds)
55
56
57
58
59 prob = null.p([20, 0, 0, 0, 0, N.nan])
60
61
62
63 if cfg.getboolean('tests', 'labile', default='yes'):
64 self.failUnless(N.abs(prob[0]) < 0.05,
65 msg="Expected small p, got %g" % prob[0])
66 if cfg.getboolean('tests', 'labile', default='yes'):
67 self.failUnless((N.abs(prob[1:]) > 0.05).all(),
68 msg="Bogus features should have insignificant p."
69 " Got %s" % (N.abs(prob[1:]),))
70
71
72 if not isinstance(null, FixedNullDist):
73
74
75 self.failUnlessRaises(ValueError, null.p, [5, 3, 4])
76
77
79 """Do some extended testing of OneWayAnova
80
81 in particular -- compound estimation
82 """
83
84 m = OneWayAnova()
85 mc = CompoundOneWayAnova(combiner=None)
86 ds = datasets['uni2medium']
87
88
89
90 a, ac = m(ds), mc(ds)
91
92 self.failUnless(a.shape == (ds.nfeatures,))
93 self.failUnless(ac.shape == (ds.nfeatures, len(ds.uniquelabels)))
94
95 self.failUnless((ac[:, 0] == ac[:, 1]).all())
96 self.failUnless((a == ac[:, 1]).all())
97
98 ds = datasets['uni4large']
99 ac = mc(ds)
100
101 if cfg.getboolean('tests', 'labile', default='yes'):
102
103 self.failUnless((ac[(N.array(ds.nonbogus_features),
104 N.arange(4))] >= 1).all())
105
106
107
108 self.failUnless(N.max(N.std(ac, axis=1))>0,
109 msg='In compound anova, we should get different'
110 ' results for different labels. Got %s' % ac)
111
113 """Create the suite"""
114 return unittest.makeSuite(StatsTests)
115
116
117 if __name__ == '__main__':
118 import runner
119