Package mvpa :: Package tests :: Module test_pcamapper

[hide private]
[frames] | no frames]

# Source Code for Module mvpa.tests.test_pcamapper

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