Package mvpa :: Package tests :: Module test_transformers

[hide private]
[frames] | no frames]

# Source Code for Module mvpa.tests.test_transformers

      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 transformers."""
     10
     11  import unittest
     12  import numpy as N
     13  from numpy.testing import assert_array_almost_equal
     14
     15  from mvpa.base import externals
     16
     17  from mvpa.misc.transformers import Absolute, OneMinus, RankOrder, \
     18       ReverseRankOrder, L1Normed, L2Normed, OverAxis, \
     19       DistPValue, FirstAxisSumNotZero
     20
     21  from tests_warehouse import sweepargs, datasets
     22
     23  from mvpa.base import cfg
     24
     25 -class TransformerTests(unittest.TestCase):
     26
     27 -    def setUp(self):
     28          self.d1 = N.array([ 1,  0, -1, -2, -3])
     29          self.d2 = N.array([ 2.3,  0, -1, 2, -30, 1])
     30
     31 -    def testAbsolute(self):
     32          # generate 100 values (gaussian noise mean -1000 -> all negative)
     33          out = Absolute(N.random.normal(-1000, size=100))
     34
     35          self.failUnless(out.min() >= 0)
     36          self.failUnless(len(out) == 100)
     37
     38 -    def testAbsolute2(self):
     39          target = self.d1
     40          out = OneMinus(N.arange(5))
     41          self.failUnless((out == target).all())
     42
     43 -    def testFirstAxisSumNotZero(self):
     44          src = [[ 1, -22.9, 6.8, 0],
     45                 [ -.8, 7, 0, 0.0],
     46                 [88, 0, 0.0, 0],
     47                 [0, 0, 0, 0.0]]
     48          target = N.array([ 3, 2, 1, 0])
     49          out = FirstAxisSumNotZero(src)
     50          self.failUnless((out == target).all())
     51
     52 -    def testRankOrder(self):
     53          nelements = len(self.d2)
     54          out = RankOrder(self.d2)
     55          outr = ReverseRankOrder(self.d2)
     56          uout = N.unique(out)
     57          uoutr = N.unique(outr)
     58          self.failUnless((uout == N.arange(nelements)).all(),
     59                          msg="We should get all indexes. Got just %s" % uout)
     60          self.failUnless((uoutr == N.arange(nelements)).all(),
     61                          msg="We should get all indexes. Got just %s" % uoutr)
     62          self.failUnless((out+outr+1 == nelements).all())
     63          self.failUnless((out == [ 0,  3,  4,  1,  5,  2]).all())
     64
     65 -    def testL2Norm(self):
     66          out = L2Normed(self.d2)
     67          self.failUnless(N.abs(N.sum(out*out)-1.0) < 1e-10)
     68
     69 -    def testL1Norm(self):
     70          out = L1Normed(self.d2)
     71          self.failUnless(N.abs(N.sum(N.abs(out))-1.0) < 1e-10)
     72
     73
     74 -    def testOverAxis(self):
     75          data = datasets['uni4large'].samples[:120,0].reshape((2,3,4,5))
     76          # Simple transformer/combiner which collapses across given
     77          # dimension, e.g. sum
     78          for axis in [None, 0, 1, 2]:
     79              oversum = OverAxis(N.sum, axis=axis)(data)
     80              sum_ = N.sum(data, axis=axis)
     81              assert_array_almost_equal(sum_, oversum)
     82
     83          # Transformer which doesn't modify dimensionality of the data
     84          data = data.reshape((6, -1))
     85          overnorm = OverAxis(L2Normed, axis=1)(data)
     86          self.failUnless(N.linalg.norm(overnorm)!=1.0)
     87          for d in overnorm:
     88              self.failUnless(N.abs(N.linalg.norm(d) - 1.0)<0.00001)
     89
     90          overnorm = OverAxis(L2Normed, axis=0)(data)
     91          self.failUnless(N.linalg.norm(overnorm)!=1.0)
     92          for d in overnorm.T:
     93              self.failUnless(N.abs(N.linalg.norm(d) - 1.0)<0.00001)
     94
     95
     96 -    def testDistPValue(self):
     97          """Basic testing of DistPValue"""
     98          if not externals.exists('scipy'):
     99              return
    100          ndb = 200
    101          ndu = 20
    102          nperd = 2
    103          pthr = 0.05
    104          Nbins = 400
    105
    106          # Lets generate already normed data (on sphere) and add some nonbogus features
    107          datau = (N.random.normal(size=(nperd, ndb)))
    108          dist = N.sqrt((datau * datau).sum(axis=1))
    109
    110          datas = (datau.T / dist.T).T
    111          tn = datax = datas[0, :]
    112          dataxmax = N.max(N.abs(datax))
    113
    114          # now lets add true positive features
    115          tp = [-dataxmax * 1.1] * (ndu/2) + [dataxmax * 1.1] * (ndu/2)
    116          x = N.hstack((datax, tp))
    117
    118          # lets add just pure normal to it
    119          x = N.vstack((x, N.random.normal(size=x.shape))).T
    120          for distPValue in (DistPValue(), DistPValue(fpp=0.05)):
    121              result = distPValue(x)
    122              self.failUnless((result>=0).all)
    123              self.failUnless((result<=1).all)
    124
    125          if cfg.getboolean('tests', 'labile', default='yes'):
    126              self.failUnless(distPValue.positives_recovered[0] > 10)
    127              self.failUnless((N.array(distPValue.positives_recovered) +
    128                               N.array(distPValue.nulldist_number) == ndb + ndu).all())
    129              self.failUnless(distPValue.positives_recovered[1] == 0)
    130
    131
    132 -def suite():
    133      return unittest.makeSuite(TransformerTests)
    134
    135
    136  if __name__ == '__main__':
    137      import runner
    138