1
2
3
4
5
6
7
8
9 """Unit tests for PyMVPA pattern handling"""
10
11 from mvpa.datasets.masked import *
12 from mvpa.misc.exceptions import DatasetError
13
14 import unittest
15 import numpy as N
16 import random
17
19
21 data = MaskedDataset(samples=[range(5)], labels=1,
22 chunks=1)
23
24 self.failUnlessEqual( data.nsamples, 1)
25
26 self.failUnless(
27 (data.samples == N.array([[0, 1, 2, 3, 4]])).all() )
28
29
30 self.failUnless( (data.labels == N.array([1])).all() )
31 self.failUnless( (data.chunks == N.array([1])).all() )
32
33
34 self.failUnlessRaises(DatasetError,
35 data.__iadd__,
36 MaskedDataset(samples=N.ones((2,3)), labels=1,
37 chunks=1))
38
39
40 data += MaskedDataset(samples=N.random.standard_normal((2,5)),
41 labels=2, chunks=2)
42 self.failUnlessEqual( data.nsamples, 3 )
43 self.failUnless( (data.labels == N.array([1,2,2]) ).all() )
44 self.failUnless( (data.chunks == N.array([1,2,2]) ).all() )
45
46
47
48 data += MaskedDataset(samples=N.random.standard_normal((2,5)),
49 labels=3)
50 self.failUnless( (data.uniquelabels == N.array([1,2,3]) ).all() )
51
52
53 self.failUnlessRaises(DatasetError,
54 MaskedDataset,
55 samples=N.random.standard_normal((4,2,3,4)),
56 labels=[1, 2, 3],
57 chunks=2)
58
59
60 self.failUnlessRaises( DatasetError,
61 MaskedDataset,
62 samples=N.random.standard_normal((4,2,3,4)),
63 labels=[1, 2, 3, 4],
64 chunks=[2, 2, 2])
65
66
74
75
79
80
82 origdata = N.random.standard_normal((10,2,4,3,5))
83 data = MaskedDataset( samples=origdata, labels=2, chunks=2 )
84
85 def randomCoord(shape):
86 return [ random.sample(range(size),1)[0] for size in shape ]
87
88
89 for i in xrange(100):
90
91 c = randomCoord((2,4,3,5))
92
93 id = data.mapper.getOutId(c)
94
95
96
97 orig = origdata[:,c[0],c[1],c[2],c[3]]
98 pat = data.samples[:, id]
99
100 self.failUnless((orig == pat).all())
101
102
104 origdata = N.random.standard_normal((10,2,4,3,5))
105 data = MaskedDataset(samples=origdata, labels=2, chunks=2)
106
107 def randomCoord(shape):
108 return [ random.sample(range(size),1)[0] for size in shape ]
109
110 for id in xrange(data.nfeatures):
111
112 c = data.mapper.getInId(id)
113 self.failUnlessEqual(len(c), 4)
114
115
116
117 orig = origdata[:,c[0],c[1],c[2],c[3]]
118 pat = data.samples[:, id]
119
120 self.failUnless((orig == pat).all())
121
122
124 origdata = N.random.standard_normal((10,2,4,3,5))
125 data = MaskedDataset(samples=origdata, labels=2, chunks=2)
126
127 unmasked = data.samples.copy()
128
129
130 self.failUnless( data.nfeatures == 120 )
131 self.failUnless(data.mapper.getOutSize() == 120)
132
133
134 sel = data.selectFeaturesByMask( N.ones((2,4,3,5)) )
135 self.failUnless( sel.nfeatures == data.samples.shape[1] )
136 self.failUnless( data.nfeatures == 120 )
137
138
139 partial_mask = N.zeros((2,4,3,5), dtype='uint')
140 partial_mask[0,0,2,2] = 1
141 partial_mask[1,2,2,0] = 1
142
143 sel = data.selectFeaturesByMask( partial_mask )
144 self.failUnless( sel.nfeatures == 2 )
145 self.failUnless( sel.mapper.getMask().shape == (2,4,3,5))
146
147
148 self.failUnless(data.nfeatures == 120)
149 self.failUnlessEqual(data.mapper.getOutSize(), 120)
150
151
152 sel = data.selectFeatures([0,37,119])
153 self.failUnless(sel.nfeatures == 3)
154
155
156 self.failUnless( sel.samples.shape == (10,3) )
157
158
159 self.failUnless( (unmasked[:,[0,37,119]]==sel.samples).all() )
160
161
181
194
195
206
207
208
210 data1 = MaskedDataset(samples=N.ones((5,5,1)), labels=1, chunks=1)
211 data2 = MaskedDataset(samples=N.ones((3,5,1)), labels=2, chunks=1)
212
213 merged = data1 + data2
214
215 self.failUnless(merged.nsamples == 8 )
216 self.failUnless((merged.labels == [ 1,1,1,1,1,2,2,2]).all())
217 self.failUnless((merged.chunks == [ 1,1,1,1,1,1,1,1]).all())
218
219 data1 += data2
220
221 self.failUnless(data1.nsamples == 8 )
222 self.failUnless((data1.labels == [ 1,1,1,1,1,2,2,2]).all())
223 self.failUnless((data1.chunks == [ 1,1,1,1,1,1,1,1]).all())
224
225
227 data = MaskedDataset(samples=N.ones((5,1)), labels=range(5), chunks=1)
228 data += MaskedDataset(samples=N.ones((5,1))+1, labels=range(5), chunks=2)
229 data += MaskedDataset(samples=N.ones((5,1))+2, labels=range(5), chunks=3)
230 data += MaskedDataset(samples=N.ones((5,1))+3, labels=range(5), chunks=4)
231 data += MaskedDataset(samples=N.ones((5,1))+4, labels=range(5), chunks=5)
232 self.failUnless( data.samplesperlabel == {0:5, 1:5, 2:5, 3:5, 4:5} )
233
234 sample = data.getRandomSamples( 2 )
235 self.failUnless( sample.samplesperlabel.values() == [ 2,2,2,2,2 ] )
236
237 self.failUnless( (data.uniquechunks == range(1,6)).all() )
238
239
240 origlabels = data.labels.copy()
241
242 data.permuteLabels(True)
243
244 self.failIf( (data.labels == origlabels).all() )
245
246 data.permuteLabels(False)
247
248 self.failUnless( (data.labels == origlabels).all() )
249
250
251 data2 = MaskedDataset(samples=data.samples, labels=data.labels,
252 chunks=data.chunks )
253
254
255 self.failUnless( (data2.labels == origlabels).all() )
256
257
258 data2.permuteLabels( True )
259
260
261 self.failUnless( (data.labels == origlabels).all() )
262
263 self.failIf( (data2.labels == origlabels).all() )
264
265
267 mask = N.zeros((5,3),dtype='bool')
268 mask[2,1] = True; mask[4,0] = True
269 data = MaskedDataset(
270 samples=N.arange( 60 ).reshape( (4,5,3) ), labels=1, chunks=1,
271 mask=mask)
272
273
274 self.failUnless( data.nfeatures == 2 )
275 self.failUnless( data.mapper.getOutId( (2,1) ) == 0
276 and data.mapper.getOutId( (4,0) ) == 1 )
277 self.failUnlessRaises( ValueError, data.mapper.getOutId, (2,3) )
278 self.failUnless( data.mapper.getMask().shape == (5,3) )
279 self.failUnless( tuple(data.mapper.getInId( 1 )) == (4,0) )
280
281
282 self.failUnless(data.selectFeaturesByMask( mask ).nfeatures == data.nfeatures )
283
284 self.failUnless( (data.selectFeatures([1]).samples[:,0] \
285 == N.array([12, 27, 42, 57]) ).all() )
286 self.failUnless(tuple( data.selectFeatures([1]).mapper.getInId(0) ) == (4,0) )
287 self.failUnless( data.selectFeatures([1]).mapper.getMask().sum() == 1 )
288
289
290
291 self.failUnless(N.all(data.I == data.origids))
292 self.failUnless(N.all(data.C == data.chunks))
293 self.failUnless(N.all(data.L == data.labels))
294 self.failUnless(N.all(data.S == data.samples))
295 self.failUnless(N.all(data.O == data.mapper.reverse(data.samples)))
296
297
298 self.failUnless(N.all(data.UC == data.uniquechunks))
299 self.failUnless(N.all(data.UL == data.uniquelabels))
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
331
332
333 if __name__ == '__main__':
334 import runner
335