Package mvpa :: Package tests :: Module test_datasetfx_sp

[hide private]
[frames] | no frames]

# Source Code for Module mvpa.tests.test_datasetfx_sp

      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 miscelaneouse functions operating on datasets and requiring SciPy"""
     10
     11  import unittest
     12
     13  import numpy as N
     14
     15  from mvpa.base import externals
     16
     17  from mvpa.datasets import Dataset
     18  from mvpa.datasets.miscfx import removeInvariantFeatures
     19
     20  if externals.exists('scipy', raiseException=True):
     21      from scipy import linalg
     22  from mvpa.datasets.miscfx import detrend
     23
     24 -class MiscDatasetFxSpTests(unittest.TestCase):
     25
     26 -    def testDetrend(self):
     27          thr = 1e-9;                    # threshold for comparison
     28          samples = N.array( [[1.0, 2, 3, 3, 2, 1],
     29                              [-2.0, -4, -6, -6, -4, -2]], ndmin=2 ).T
     30
     31          chunks = [0, 0, 0, 1, 1, 1]
     32          chunks_bad = [ 0, 0, 1, 1, 1, 0]
     33          target_all = N.array( [[-1.0, 0, 1, 1, 0, -1],
     34                                 [2, 0, -2, -2, 0, 2]], ndmin=2 ).T
     35
     36
     37          ds = Dataset(samples=samples, labels=chunks, chunks=chunks,
     38                       copy_samples=True)
     39          detrend(ds, perchunk=False)
     40
     41          self.failUnless(linalg.norm(ds.samples - target_all) < thr,
     42                  msg="Detrend should have detrended all the samples at once")
     43
     44
     45          ds_bad = Dataset(samples=samples, labels=chunks, chunks=chunks_bad,
     46                           copy_samples=True)
     47          self.failUnlessRaises(ValueError, detrend, ds_bad, perchunk=True)
     48
     49
     50          ds = Dataset(samples=samples, labels=chunks, chunks=chunks,
     51                       copy_samples=True)
     52          detrend(ds, perchunk=True)
     53
     54          self.failUnless(linalg.norm(ds.samples) < thr,
     55                      msg="Detrend should have detrended each chunk separately")
     56
     57          self.failUnless(ds.samples.shape == samples.shape,
     58                          msg="Detrend must preserve the size of dataset")
     59
     60
     61          # small additional test for break points
     62          ds = Dataset(samples=N.array([[1.0, 2, 3, 1, 2, 3]], ndmin=2).T,
     63                       labels=chunks, chunks=chunks, copy_samples=True)
     64          detrend(ds, perchunk=True)
     65          self.failUnless(linalg.norm(ds.samples) < thr,
     66                          msg="Detrend should have removed all the signal")
     67
     68          # tests of the regress version of detrend
     69          ds = Dataset(samples=samples, labels=chunks, chunks=chunks,
     70                       copy_samples=True)
     71          detrend(ds, perchunk=False, model='regress', polyord=1)
     72          self.failUnless(linalg.norm(ds.samples - target_all) < thr,
     73                  msg="Detrend should have detrended all the samples at once")
     74
     75          ds = Dataset(samples=samples, labels=chunks, chunks=chunks,
     76                       copy_samples=True)
     77          (res, reg) = detrend(ds, perchunk=True, model='regress', polyord=2)
     78          psamps = ds.samples.copy()
     79          self.failUnless(linalg.norm(ds.samples) < thr,
     80                  msg="Detrend should have detrended each chunk separately")
     81
     82          self.failUnless(ds.samples.shape == samples.shape,
     83                          msg="Detrend must preserve the size of dataset")
     84
     85          ods = Dataset(samples=samples, labels=chunks, chunks=chunks,
     86                        copy_samples=True)
     87          opt_reg = reg.copy()
     88          (ores, oreg) = detrend(ods, perchunk=True, model='regress',
     89                                 opt_reg=opt_reg)
     90          dsamples = (ods.samples - psamps).sum()
     91          self.failUnless(abs(dsamples) <= 1e-10,
     92              msg="Detrend for polyord reg should be same as opt_reg " + \
     93                  "when popt_reg is the same as the polyord reg. But got %g" \
     94                  % dsamples)
     95
     96          self.failUnless(linalg.norm(ds.samples) < thr,
     97                  msg="Detrend should have detrended each chunk separately")
     98
     99
    100          # test of different polyord on each chunk
    101          target_mixed = N.array( [[-1.0, 0, 1, 0, 0, 0],
    102                                   [2.0, 0, -2, 0, 0, 0]], ndmin=2 ).T
    103
    104          ds = Dataset(samples=samples, labels=chunks, chunks=chunks,
    105                       copy_samples=True)
    106          (res, reg) = detrend(ds, perchunk=True, model='regress', polyord=[0,1])
    107          self.failUnless(linalg.norm(ds.samples - target_mixed) < thr,
    108              msg="Detrend should have baseline corrected the first chunk, " + \
    109                  "but baseline and linear detrended the second.")
    110
    111          # test applying detrend in sequence
    112          ds = Dataset(samples=samples, labels=chunks, chunks=chunks,
    113                       copy_samples=True)
    114          (res, reg) = detrend(ds, perchunk=True, model='regress', polyord=1)
    115          opt_reg = reg[N.ix_(range(reg.shape[0]),[1,3])]
    116          final_samps = ds.samples.copy()
    117          ds = Dataset(samples=samples, labels=chunks, chunks=chunks,
    118                       copy_samples=True)
    119          (res, reg) = detrend(ds, perchunk=True, model='regress', polyord=0)
    120          (res, reg) = detrend(ds, perchunk=True, model='regress',
    121                               opt_reg=opt_reg)
    122          self.failUnless(linalg.norm(ds.samples - final_samps) < thr,
    123                  msg="Detrend of polyord 1 should be same as detrend with " + \
    124                      "0 followed by opt_reg the same as a 1st order.")
    125
    126
    127
    128 -def suite():
    129      return unittest.makeSuite(MiscDatasetFxSpTests)
    130
    131
    132  if __name__ == '__main__':
    133      import runner
    134