1
2
3
4
5
6
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
21
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
31 self.failUnlessRaises(ValueError,
32 BoxcarMapper,
33 sp,
34 0 )
35
36
37 bcm = BoxcarMapper(sp, 1)
38 trans = bcm(data)
39
40 self.failUnless( (trans[:,0] == data).all() )
41
42
43 self.failUnlessRaises(ValueError,
44 BoxcarMapper(sp, 2),
45 data)
46
47
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
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
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
69 mr = m.reverse(mp)
70
71 self.failUnless(mr.shape == data.shape)
72
73
74 self.failUnless(N.sum(mr[:2]) == 0)
75
76 self.failUnless(N.sum(mr[7:]) == 0)
77
78
79 self.failUnless((mr[2].ravel() == N.arange(48, 72)).all())
80
81
82
83 self.failUnless((mr[3].ravel() == N.arange(72, 96)).all())
84
85
86 singlesample = N.arange(48).reshape(2, 3, 4, 2)
87 self.failUnless((singlesample == m.reverse(singlesample)).all())
88
89 self.failUnlessRaises(ValueError, m.reverse, singlesample[0])
90
91
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
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
114
115
116 if __name__ == '__main__':
117 import runner
118