1
2
3
4
5
6
7
8
9 """Unit tests for PyMVPA dense array mapper"""
10
11
12 from mvpa.mappers.array import DenseArrayMapper
13 from mvpa.mappers.metric import *
14 import unittest
15 import numpy as N
16
18
20 mask = N.ones((3,2))
21 map_ = DenseArrayMapper(mask)
22
23
24 self.failUnless( map_.nfeatures == 6 )
25
26
27 self.failUnless( ( map_.forward( N.arange(6).reshape(3,2) ) \
28 == [0,1,2,3,4,5]).all() )
29
30
31 foursample = map_.forward( N.arange(24).reshape(4,3,2))
32 self.failUnless( ( foursample \
33 == [[0,1,2,3,4,5],
34 [6,7,8,9,10,11],
35 [12,13,14,15,16,17],
36 [18,19,20,21,22,23]]).all() )
37
38
39 mask[1,1] = 0
40 map_ = DenseArrayMapper(mask)
41 self.failUnless( map_.nfeatures == 5 )
42 self.failUnless( ( map_.forward( N.arange(6).reshape(3,2) ) \
43 == [0,1,2,4,5]).all() )
44
45
46 self.failUnlessRaises( ValueError,
47 map_.forward,
48 N.arange(4).reshape(2,2) )
49
50
51 self.failUnlessRaises(ValueError, DenseArrayMapper)
52
53
54 m = DenseArrayMapper(shape=(2, 3, 4))
55 mp = m.forward(N.arange(24).reshape(2, 3, 4))
56 self.failUnless((mp == N.arange(24)).all())
57
58
60 mask = N.ones((3,2))
61 mask[1,1] = 0
62 map_ = DenseArrayMapper(mask)
63
64 rmapped = map_.reverse(N.arange(1,6))
65 self.failUnless( rmapped.shape == (3,2) )
66 self.failUnless( rmapped[1,1] == 0 )
67 self.failUnless( rmapped[2,1] == 5 )
68
69
70
71 self.failUnlessRaises( ValueError,
72 map_,
73 N.arange(6))
74
75 rmapped2 = map_.reverse(N.arange(1,11).reshape(2,5))
76 self.failUnless( rmapped2.shape == (2,3,2) )
77 self.failUnless( rmapped2[0,1,1] == 0 )
78 self.failUnless( rmapped2[1,1,1] == 0 )
79 self.failUnless( rmapped2[0,2,1] == 5 )
80 self.failUnless( rmapped2[1,2,1] == 10 )
81
82
84 """ Test DenseArrayMapperMetric
85 """
86 mask = N.ones((3,2))
87 mask[1,1] = 0
88
89
90 neighborFinder = DescreteMetric([0.5, 2])
91 map_ = DenseArrayMapper(mask, neighborFinder)
92
93
94
95
96
97
98
99
100
101 target = [0,1,2,3]
102 result = map_.getNeighbors(0, 2.1)
103 self.failUnless( result == target )
104
105 map__ = DenseArrayMapper(mask, elementsize=[0.5, 2])
106 self.failUnless( map__.getNeighbors(0, 2.1) == target,
107 msg="DenseArrayMapper must accept elementsize parameter and set" +
108 " DescreteMetric accordingly")
109
110 self.failUnlessRaises(ValueError, DenseArrayMapper,
111 mask, elementsize=[0.5]*3)
112 """DenseArrayMapper must raise exception when not appropriatly sized
113 elementsize was provided"""
114
115
116
118 mm=DenseArrayMapper(N.ones((3,4,2)))
119
120
121 self.failUnless((mm(N.ones((3,4,2))) \
122 == mm.forward(N.ones((3,4,2)))).all())
123
124
126 mask=N.zeros((3,4,2))
127 mask[0,0,1]=1
128 mask[2,1,0]=1
129 mask[0,3,1]=1
130
131 mm=DenseArrayMapper(mask)
132
133 self.failUnless(mm.nfeatures==3)
134
135
136 self.failUnless((mm.getInIds() \
137 == N.array([[0, 0, 1],[0, 3, 1],[2, 1, 0]])).all())
138 self.failUnless((mm.getInId(1) == [0,3,1]).all())
139
140 self.failUnless((mm.getInId(range(mm.nfeatures)) \
141 == mm.getInIds().T).all())
142
143
144 self.failUnlessRaises( ValueError,
145 mm.getOutId,
146 (0,0,0))
147 self.failUnless(mm.getOutId((0,0,1)) == 0
148 and mm.getOutId((0,3,1)) == 1
149 and mm.getOutId((2,1,0)) == 2)
150
151
153 mask = N.ones((3,2))
154 mask[1,1] = 0
155 mask0 = mask.copy()
156 data = N.arange(6).reshape(mask.shape)
157 map_ = DenseArrayMapper(mask)
158
159
160
161 self.failUnlessRaises(IndexError, map_.selectOut, [0,1,2,6])
162
163
164 map_.selectOut([0,3,4])
165 self.failUnless((map_.forward(data)==[0, 4, 5]).all())
166
167 map_.selectOut([0,2])
168 self.failUnless((map_.forward(data)==[0, 5]).all())
169
170
171 self.failUnless((mask == mask0).all())
172
173
174 map_ = DenseArrayMapper(mask)
175 map_.discardOut([1,2])
176 self.failUnless((map_.forward(data)==[0, 4, 5]).all())
177 map_.discardOut([1])
178 self.failUnless((map_.forward(data)==[0, 5]).all())
179
180
181 self.failUnless((mask == mask0).all())
182
183
185 """
186 Test is desabled for now since if order is incorrect in
187 __debug__ we just spit out a warning - no exception
188 """
189 mask = N.ones((3,3))
190 mask[1,1] = 0
191
192 data = N.arange(9).reshape(mask.shape)
193 map_ = DenseArrayMapper(mask)
194 oldneighbors = map_.forward(data)[map_.getNeighbors(0, radius=2)]
195
196
197
198
199 map_.selectOut([7, 1, 2, 3, 4, 5, 6, 0])
200
201 newneighbors = map_.forward(data)[map_.getNeighbors(7, radius=2)]
202 self.failUnless( (oldneighbors != newneighbors ).any())
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
227
228
229 if __name__ == '__main__':
230 import runner
231