Package mvpa :: Package tests :: Module test_svdmapper

[hide private]
[frames] | no frames]

# Source Code for Module mvpa.tests.test_svdmapper

      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 SVD mapper"""
     10
     11
     12  import unittest
     13  from mvpa.support.copy import deepcopy
     14  import numpy as N
     15  from mvpa.datasets import Dataset
     16  from mvpa.mappers.svd import SVDMapper
     17
     18
     19 -class SVDMapperTests(unittest.TestCase):
     20
     21 -    def setUp(self):
     22          # data: 40 sample feature line in 20d space (40x20; samples x features)
     23          self.ndlin = Dataset(samples=N.concatenate(
     24              [N.arange(40) for i in range(20)]).reshape(20,-1).T, labels=1, chunks=1)
     25
     26          # data: 10 sample feature line in 40d space
     27          #       (10x40; samples x features)
     28          self.largefeat = Dataset(samples=N.concatenate(
     29              [N.arange(10) for i in range(40)]).reshape(40,-1).T, labels=1, chunks=1)
     30
     31
     32 -    def testSimpleSVD(self):
     33          pm = SVDMapper()
     34          # train SVD
     35          pm.train(self.ndlin)
     36
     37          self.failUnlessEqual(pm.proj.shape, (20, 20))
     38
     39          # now project data into PCA space
     40          p = pm.forward(self.ndlin.samples)
     41
     42          # only first eigenvalue significant
     43          self.failUnless(pm.sv[:1] > 1.0)
     44          self.failUnless((pm.sv[1:] < 0.0001).all())
     45
     46          # only variance of first component significant
     47          var = p.var(axis=0)
     48
     49         # test that only one component has variance
     50          self.failUnless(var[:1] > 1.0)
     51          self.failUnless((var[1:] < 0.0001).all())
     52
     53          # check that the mapped data can be fully recovered by 'reverse()'
     54          pr = pm.reverse(p)
     55
     56          self.failUnlessEqual(pr.shape, (40,20))
     57          self.failUnless(N.abs(pm.reverse(p) - self.ndlin.samples).sum() < 0.0001)
     58
     59
     60 -    def testMoreSVD(self):
     61          pm = SVDMapper()
     62          # train SVD
     63          pm.train(self.largefeat)
     64
     65          # mixing matrix cannot be square
     66          self.failUnlessEqual(pm.proj.shape, (40, 10))
     67
     68          # only first singular value significant
     69          self.failUnless(pm.sv[:1] > 10)
     70          self.failUnless((pm.sv[1:] < 10).all())
     71
     72          # now project data into SVD space
     73          p = pm.forward(self.largefeat.samples)
     74
     75          # only variance of first component significant
     76          var = p.var(axis=0)
     77
     78          # test that only one component has variance
     79          self.failUnless(var[:1] > 1.0)
     80          self.failUnless((var[1:] < 0.0001).all())
     81
     82          # check that the mapped data can be fully recovered by 'reverse()'
     83          rp = pm.reverse(p)
     84          self.failUnlessEqual(rp.shape, self.largefeat.samples.shape)
     85          self.failUnless((N.round(rp) == self.largefeat.samples).all())
     86
     87          self.failUnlessEqual(pm.getInSize(), 40)
     88          self.failUnlessEqual(pm.getOutSize(), 10)
     89
     90          # copy mapper
     91          pm2 = deepcopy(pm)
     92
     93          # now remove all but the first 2 components from the mapper
     94          pm2.selectOut([0,1])
     95
     96          # sanity check
     97          self.failUnlessEqual(pm2.getInSize(), 40)
     98          self.failUnlessEqual(pm2.getOutSize(), 2)
     99
    100          # but orginal mapper must be left intact
    101          self.failUnlessEqual(pm.getInSize(), 40)
    102          self.failUnlessEqual(pm.getOutSize(), 10)
    103
    104          # data should still be fully recoverable by 'reverse()'
    105          rp2 = pm2.reverse(p[:,[0,1]])
    106          self.failUnlessEqual(rp2.shape, self.largefeat.samples.shape)
    107          self.failUnless(N.abs(rp2 - self.largefeat.samples).sum() < 0.0001)
    108
    109
    110          # now make new random data and do forward->reverse check
    111          data = N.random.normal(size=(98,40))
    112          data_f = pm.forward(data)
    113
    114          self.failUnlessEqual(data_f.shape, (98,10))
    115
    116          data_r = pm.reverse(data_f)
    117          self.failUnlessEqual(data_r.shape, (98,40))
    118
    119
    120
    121 -def suite():
    122      return unittest.makeSuite(SVDMapperTests)
    123
    124
    125  if __name__ == '__main__':
    126      import runner
    127