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      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                          ]
     42
     43 -class StatsTests(unittest.TestCase):
     44      """Unittests for various statistics"""
     45
     46
     47      @sweepargs(null=nulldist_sweep[1:])
     48 -    def testNullDistProb(self, null):
     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          # check reasonable output.
     57          # p-values for non-bogus features should significantly different,
     58          # while bogus (0) not
     59          prob = null.p([20, 0, 0, 0, 0, N.nan])
     60          # XXX this is labile! it also needs checking since the F-scores
     61          # of the MCNullDists using normal distribution are apparently not
     62          # distributed that way, hence the test often (if not always) fails.
     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          # has to have matching shape
     72          if not isinstance(null, FixedNullDist):
     73              # Fixed dist is univariate ATM so it doesn't care
     74              # about dimensionality and gives 1 output value
     75              self.failUnlessRaises(ValueError, null.p, [5, 3, 4])
     76
     77
     78 -    def testAnova(self):
     79          """Do some extended testing of OneWayAnova
     80
     81          in particular -- compound estimation
     82          """
     83
     84          m = OneWayAnova()               # default must be not compound ?
     85          mc = CompoundOneWayAnova(combiner=None)
     86          ds = datasets['uni2medium']
     87
     88          # For 2 labels it must be identical for both and equal to
     89          # simple OneWayAnova
     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              # All non-bogus features must be high for a corresponding feature
    103              self.failUnless((ac[(N.array(ds.nonbogus_features),
    104                                   N.arange(4))] >= 1).all())
    105          # All features should have slightly but different CompoundAnova
    106          # values. I really doubt that there will be a case when this
    107          # test would fail just to being 'labile'
    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
    112 -def suite():
    113      """Create the suite"""
    114      return unittest.makeSuite(StatsTests)
    115
    116
    117  if __name__ == '__main__':
    118      import runner
    119