1
2
3
4
5
6
7
8
9 """Unit tests for PyMVPA miscelaneouse functions operating on datasets"""
10
11 import unittest
12
13 import numpy as N
14
15 from mvpa.base import externals
16 from mvpa.datasets import Dataset
17 from mvpa.datasets.miscfx import removeInvariantFeatures, coarsenChunks, \
18 SequenceStats
19
20 from mvpa.misc.data_generators import normalFeatureDataset
21
23
35
36
38 """Just basic testing for now"""
39 chunks = [1,1,2,2,3,3,4,4]
40 ds = Dataset(samples=N.arange(len(chunks)).reshape(
41 (len(chunks),1)), labels=[1]*8, chunks=chunks)
42 coarsenChunks(ds, nchunks=2)
43 chunks1 = coarsenChunks(chunks, nchunks=2)
44 self.failUnless((chunks1 == ds.chunks).all())
45 self.failUnless((chunks1 == N.asarray([0,0,0,0,1,1,1,1])).all())
46
47 ds2 = Dataset(samples=N.arange(len(chunks)).reshape(
48 (len(chunks),1)), labels=[1]*8)
49 coarsenChunks(ds2, nchunks=2)
50 self.failUnless((chunks1 == ds.chunks).all())
51
53 ds = normalFeatureDataset()
54 ds_data = ds.samples.copy()
55 ds_chunks = ds.chunks.copy()
56 self.failUnless(N.all(ds.samples == ds_data))
57
58 funcs = ['zscore', 'coarsenChunks']
59 if externals.exists('scipy'):
60 funcs.append('detrend')
61
62 for f in funcs:
63 eval('ds.%s()' % f)
64 self.failUnless(N.any(ds.samples != ds_data) or
65 N.any(ds.chunks != ds_chunks),
66 msg="We should have modified original dataset with %s" % f)
67 ds.samples = ds_data.copy()
68 ds.chunks = ds_chunks.copy()
69
70
71 for f in ['aggregateFeatures', 'removeInvariantFeatures',
72 'getSamplesPerChunkLabel']:
73 res = eval('ds.%s()' % f)
74 self.failUnless(res is not None,
75 msg='We should have got result from function %s' % f)
76 self.failUnless(N.all(ds.samples == ds_data),
77 msg="Function %s should have not modified original dataset" % f)
78
80 """Test sequence statistics
81 """
82 order = 3
83
84 sp = N.array([-1, 1, 1, -1, 1, -1, -1, 1, -1, -1, -1,
85 -1, 1, -1, 1, -1, 1, 1, 1, -1, 1, 1,
86 -1, -1, -1, 1, 1, 1, 1, 1, -1], dtype=int)
87 rp = SequenceStats(sp, order=order)
88 self.failUnlessAlmostEqual(rp['sumabscorr'], 1.0)
89 self.failUnlessAlmostEqual(N.max(rp['corrcoef'] * (len(sp)-1) + 1.0), 0.0)
90
91
92 sb = (N.random.random_sample((1000,)) >= 0.5)
93 rb = SequenceStats(sb, order=order)
94
95
96 s5 = N.random.permutation(['f', 'bu', 'd', 0, 'zz']*200)
97 r5 = SequenceStats(s5, order=order)
98
99
100 s1 = ['aaa']*100
101 r1 = SequenceStats(s1, order=order)
102
103
104 for r in (rp, rb, r5, r1):
105 ulabels = r['ulabels']
106 nlabels = len(r['ulabels'])
107 cbcounts = r['cbcounts']
108 self.failUnlessEqual(len(cbcounts), order)
109 for cb in cbcounts:
110 self.failUnlessEqual(N.asarray(cb).shape, (nlabels, nlabels))
111
112 sr = str(r)
113
114
117
118
119 if __name__ == '__main__':
120 import runner
121