Package mvpa :: Package tests :: Module test_boxcarmapper
[hide private]
[frames] | no frames]

Source Code for Module mvpa.tests.test_boxcarmapper

  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 Boxcar mapper""" 
 10   
 11   
 12  import unittest 
 13  from mvpa.support.copy import deepcopy 
 14  import numpy as N 
 15   
 16  from mvpa.mappers.boxcar import BoxcarMapper 
 17  from mvpa.datasets import Dataset 
 18   
 19   
20 -class BoxcarMapperTests(unittest.TestCase):
21
22 - def testSimple(self):
23 """Just the same tests as for transformWithBoxcar. 24 25 Mention that BoxcarMapper doesn't apply function with each boxcar 26 """ 27 data = N.arange(10) 28 sp = N.arange(10) 29 30 # check if stupid thing don't work 31 self.failUnlessRaises(ValueError, 32 BoxcarMapper, 33 sp, 34 0 ) 35 36 # now do an identity transformation 37 bcm = BoxcarMapper(sp, 1) 38 trans = bcm(data) 39 # ,0 is a feature below, so we get explicit 2D out of 1D 40 self.failUnless( (trans[:,0] == data).all() ) 41 42 # now check for illegal boxes 43 self.failUnlessRaises(ValueError, 44 BoxcarMapper(sp, 2), 45 data) 46 47 # now something that should work 48 sp = N.arange(9) 49 bcm = BoxcarMapper(sp,2) 50 trans = bcm(data) 51 self.failUnless( (trans == N.vstack((N.arange(9), 52 N.arange(9)+1)).T ).all() ) 53 54 55 # now test for proper data shape 56 data = N.ones((10,3,4,2)) 57 sp = [ 2, 4, 3, 5 ] 58 trans = BoxcarMapper(sp, 4)(data) 59 self.failUnless( trans.shape == (4,4,3,4,2) ) 60 61 # test reverse 62 data = N.arange(240).reshape(10, 3, 4, 2) 63 sp = [ 2, 4, 3, 5 ] 64 m = BoxcarMapper(sp, 2) 65 mp = m.forward(data) 66 self.failUnless(mp.shape == (4, 2, 3, 4, 2)) 67 68 # try full reconstruct 69 mr = m.reverse(mp) 70 # shape has to match 71 self.failUnless(mr.shape == data.shape) 72 # first two samples where not in any of the boxcars and cannot be 73 # reconstructed 74 self.failUnless(N.sum(mr[:2]) == 0) 75 # same for the last ones 76 self.failUnless(N.sum(mr[7:]) == 0) 77 78 # check proper reconstruction of non-conflicting sample 79 self.failUnless((mr[2].ravel() == N.arange(48, 72)).all()) 80 81 # check proper reconstruction of samples being part of multiple 82 # mapped samples 83 self.failUnless((mr[3].ravel() == N.arange(72, 96)).all()) 84 85 # test reverse of a single sample 86 singlesample = N.arange(48).reshape(2, 3, 4, 2) 87 self.failUnless((singlesample == m.reverse(singlesample)).all()) 88 # should not work for shape mismatch 89 self.failUnlessRaises(ValueError, m.reverse, singlesample[0]) 90 91 # check broadcasting of 'raw' samples into proper boxcars on forward() 92 bc = m.forward(N.arange(24).reshape(3, 4, 2)) 93 self.failUnless((bc == 94 N.array(2 * [N.arange(24).reshape(3, 4, 2)])).all())
95 96
97 - def testIds(self):
98 data = N.arange(20).reshape( (10,2) ) 99 bcm = BoxcarMapper([1, 4, 6], 3) 100 trans = bcm(data) 101 102 self.failUnlessEqual(bcm.isValidInId( [1] ), True) 103 self.failUnlessEqual(bcm.isValidInId( [0,1] ), False) 104 105 self.failUnlessEqual(bcm.isValidOutId( [1] ), True) 106 self.failUnlessEqual(bcm.isValidOutId( [3] ), False) 107 self.failUnlessEqual(bcm.isValidOutId( [0,1] ), True) 108 self.failUnlessEqual(bcm.isValidOutId( [0,1,0] ), False)
109 110 111
112 -def suite():
113 return unittest.makeSuite(BoxcarMapperTests)
114 115 116 if __name__ == '__main__': 117 import runner 118