Package mvpa :: Package tests :: Module test_stats
[hide private]
[frames] | no frames]

Source Code for Module mvpa.tests.test_stats

  1  # emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*- 
  2  # vi: set ft=python sts=4 ts=4 sw=4 et: 
  3  ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ## 
  4  # 
  5  #   See COPYING file distributed along with the PyMVPA package for the 
  6  #   copyright and license terms. 
  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  # Prepare few distributions to test 
 22  #kwargs = {'permutations':10, 'tail':'any'} 
 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 40 -class StatsTests(unittest.TestCase):
41 """Unittests for various statistics""" 42 43 44 @sweepargs(null=nulldist_sweep[1:])
45 - def testNullDistProb(self, null):
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 # check reasonable output. 54 # p-values for non-bogus features should significantly different, 55 # while bogus (0) not 56 prob = null.p([20, 0, 0, 0, 0, N.nan]) 57 # XXX this is labile! it also needs checking since the F-scores 58 # of the MCNullDists using normal distribution are apparently not 59 # distributed that way, hence the test often (if not always) fails. 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 # has to have matching shape 69 if not isinstance(null, FixedNullDist): 70 # Fixed dist is univariate ATM so it doesn't care 71 # about dimensionality and gives 1 output value 72 self.failUnlessRaises(ValueError, null.p, [5, 3, 4])
73 74
75 - def testAnova(self):
76 """Do some extended testing of OneWayAnova 77 78 in particular -- compound estimation 79 """ 80 81 m = OneWayAnova() # default must be not compound ? 82 mc = CompoundOneWayAnova(combiner=None) 83 ds = datasets['uni2medium'] 84 85 # For 2 labels it must be identical for both and equal to 86 # simple OneWayAnova 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 # All non-bogus features must be high for a corresponding feature 100 self.failUnless((ac[(N.array(ds.nonbogus_features), 101 N.arange(4))] >= 1).all()) 102 # All features should have slightly but different CompoundAnova 103 # values. I really doubt that there will be a case when this 104 # test would fail just to being 'labile' 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
109 -def suite():
110 """Create the suite""" 111 return unittest.makeSuite(StatsTests)
112 113 114 if __name__ == '__main__': 115 import runner 116