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

Source Code for Module mvpa.tests.test_support

  1  #emacs: -*- mode: python-mode; py-indent-offset: 4; indent-tabs-mode: nil -*- 
  2  #ex: set 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 serial feature inclusion algorithm""" 
 10   
 11  from mvpa.misc.support import * 
 12  from mvpa.datasets.splitters import NFoldSplitter 
 13  from mvpa.clfs.transerror import TransferError 
 14  from tests_warehouse import * 
 15  from tests_warehouse import getMVPattern 
 16  from tests_warehouse_clfs import * 
 17  from mvpa.clfs.distance import oneMinusCorrelation 
 18   
 19  from mvpa.support.copy import deepcopy 
 20   
21 -class SupportFxTests(unittest.TestCase):
22
23 - def testTransformWithBoxcar(self):
24 data = N.arange(10) 25 sp = N.arange(10) 26 27 # check if stupid thing don't work 28 self.failUnlessRaises(ValueError, 29 transformWithBoxcar, 30 data, 31 sp, 32 0 ) 33 34 # now do an identity transformation 35 trans = transformWithBoxcar(data, sp, 1) 36 self.failUnless( (trans == data).all() ) 37 38 # now check for illegal boxes 39 self.failUnlessRaises(ValueError, 40 transformWithBoxcar, 41 data, 42 sp, 43 2) 44 45 # now something that should work 46 sp = N.arange(9) 47 trans = transformWithBoxcar( data, sp, 2) 48 self.failUnless( ( trans == \ 49 [0.5,1.5,2.5,3.5,4.5,5.5,6.5,7.5,8.5] ).all() ) 50 51 52 # now test for proper data shape 53 data = N.ones((10,3,4,2)) 54 sp = [ 2, 4, 3, 5 ] 55 trans = transformWithBoxcar( data, sp, 4) 56 self.failUnless( trans.shape == (4,3,4,2) )
57 58 59
60 - def testEvent(self):
61 self.failUnlessRaises(ValueError, Event) 62 ev = Event(onset=2.5) 63 64 # all there? 65 self.failUnless(ev.items() == [('onset', 2.5)]) 66 67 # conversion 68 self.failUnless(ev.asDescreteTime(dt=2).items() == [('onset', 1)]) 69 evc = ev.asDescreteTime(dt=2, storeoffset=True) 70 self.failUnless(evc.has_key('features')) 71 self.failUnless(evc['features'] == [0.5]) 72 73 # same with duration included 74 evc = Event(onset=2.5, duration=3.55).asDescreteTime(dt=2) 75 self.failUnless(evc['duration'] == 3)
76 77
78 - def testMofNCombinations(self):
79 self.failUnlessEqual( 80 getUniqueLengthNCombinations( range(3), 1 ), [[0],[1],[2]] ) 81 self.failUnlessEqual( 82 getUniqueLengthNCombinations( 83 range(4), 2 ), 84 [[0, 1], [0, 2], [0, 3], [1, 2], [1, 3], [2, 3]] 85 ) 86 self.failUnlessEqual( 87 getUniqueLengthNCombinations( 88 range(4), 3 ), [[0, 1, 2], [0, 1, 3], [0, 2, 3]] )
89 90
91 - def testBreakPoints(self):
92 items_cont = [0, 0, 0, 1, 1, 1, 3, 3, 2] 93 items_noncont = [0, 0, 1, 1, 0, 3, 2] 94 self.failUnlessRaises(ValueError, getBreakPoints, items_noncont) 95 self.failUnlessEqual(getBreakPoints(items_noncont, contiguous=False), 96 [0, 2, 4, 5, 6]) 97 self.failUnlessEqual(getBreakPoints(items_cont), [0, 3, 6, 8]) 98 self.failUnlessEqual(getBreakPoints(items_cont, contiguous=False), 99 [0, 3, 6, 8])
100 101
102 - def testMapOverlap(self):
103 mo = MapOverlap() 104 105 maps = [[1,0,1,0], 106 [1,0,0,1], 107 [1,0,1,0]] 108 109 overlap = mo(maps) 110 111 self.failUnlessEqual(overlap, 1./len(maps[0])) 112 self.failUnless((mo.overlap_map == [1,0,0,0]).all()) 113 self.failUnless((mo.spread_map == [0,0,1,1]).all()) 114 self.failUnless((mo.ovstats_map == [1,0,2./3,1./3]).all()) 115 116 mo = MapOverlap(overlap_threshold=0.5) 117 overlap = mo(maps) 118 self.failUnlessEqual(overlap, 2./len(maps[0])) 119 self.failUnless((mo.overlap_map == [1,0,1,0]).all()) 120 self.failUnless((mo.spread_map == [0,0,0,1]).all()) 121 self.failUnless((mo.ovstats_map == [1,0,2./3,1./3]).all())
122 123
124 - def testHarvester(self):
125 # do very simple list comprehension 126 self.failUnlessEqual( 127 [(-1)*i for i in range(5)], 128 Harvester(xrange, 129 [HarvesterCall(lambda x: (-1)*x, expand_args=False)]) 130 (5)) 131 132 133 # do clf cross-validation on a dataset with a very high SNR 134 cv = Harvester(NFoldSplitter(cvtype=1), 135 [HarvesterCall(TransferError(sample_clf_nl), argfilter=[1,0])]) 136 data = getMVPattern(10) 137 err = N.array(cv(data)) 138 139 # has to be perfect 140 self.failUnless((err < 0.1).all()) 141 self.failUnlessEqual(err.shape, (len(data.uniquechunks),)) 142 143 # now same stuff but two classifiers at once 144 cv = Harvester(NFoldSplitter(cvtype=1), 145 [HarvesterCall(TransferError(sample_clf_nl), argfilter=[1,0]), 146 HarvesterCall(TransferError(sample_clf_nl), argfilter=[1,0])]) 147 err = N.array(cv(data)) 148 self.failUnlessEqual(err.shape, (2,len(data.uniquechunks))) 149 150 # only one again, but this time remember confusion matrix 151 cv = Harvester(NFoldSplitter(cvtype=1), 152 [HarvesterCall(TransferError(sample_clf_nl, 153 enable_states=['confusion']), 154 argfilter=[1,0], attribs=['confusion'])]) 155 res = cv(data) 156 157 self.failUnless(isinstance(res, dict)) 158 self.failUnless(res.has_key('confusion') and res.has_key('result')) 159 self.failUnless(len(res['result']) == len(data.uniquechunks))
160 161 162 @sweepargs(pair=[(N.random.normal(size=(10,20)), N.random.normal(size=(10,20))), 163 ([1,2,3,0], [1,3,2,0]), 164 ((1,2,3,1), (1,3,2,1))])
165 - def testIdHash(self, pair):
166 a, b = pair 167 a1 = deepcopy(a) 168 a_1 = idhash(a) 169 self.failUnless(a_1 == idhash(a), msg="Must be of the same idhash") 170 self.failUnless(a_1 != idhash(b), msg="Must be of different idhash") 171 if isinstance(a, N.ndarray): 172 self.failUnless(a_1 != idhash(a.T), msg=".T must be of different idhash") 173 if not isinstance(a, tuple): 174 self.failUnless(a_1 != idhash(a1), msg="Must be of different idhash") 175 a[2] += 1; a_2 = idhash(a) 176 self.failUnless(a_1 != a_2, msg="Idhash must change") 177 else: 178 a_2 = a_1 179 a = a[2:]; a_3 = idhash(a) 180 self.failUnless(a_2 != a_3, msg="Idhash must change after slicing")
181 182
183 - def testCorrelation(self):
184 # data: 20 samples, 80 features 185 X = N.random.rand(20,80) 186 187 C = 1 - oneMinusCorrelation(X, X) 188 189 # get nsample x nssample correlation matrix 190 self.failUnless(C.shape == (20, 20)) 191 # diagonal is 1 192 self.failUnless((N.abs(N.diag(C) - 1).mean() < 0.00001).all()) 193 194 # now two different 195 Y = N.random.rand(5,80) 196 C2 = 1 - oneMinusCorrelation(X, Y) 197 # get nsample x nssample correlation matrix 198 self.failUnless(C2.shape == (20, 5)) 199 # external validity check -- we are dealing with correlations 200 self.failUnless(C2[10,2] - N.corrcoef(X[10], Y[2])[0,1] < 0.000001)
201 202
203 -def suite():
204 return unittest.makeSuite(SupportFxTests)
205 206 207 if __name__ == '__main__': 208 import runner 209