1
2
3
4
5
6
7
8
9 """Unit tests for PyMVPA SVD mapper"""
10
11
12 import unittest
13 from mvpa.support.copy import deepcopy
14 import numpy as N
15 from mvpa.datasets import Dataset
16 from mvpa.mappers.svd import SVDMapper
17
18
20
22
23 self.ndlin = Dataset(samples=N.concatenate(
24 [N.arange(40) for i in range(20)]).reshape(20,-1).T, labels=1, chunks=1)
25
26
27
28 self.largefeat = Dataset(samples=N.concatenate(
29 [N.arange(10) for i in range(40)]).reshape(40,-1).T, labels=1, chunks=1)
30
31
33 pm = SVDMapper()
34
35 pm.train(self.ndlin)
36
37 self.failUnlessEqual(pm.proj.shape, (20, 20))
38
39
40 p = pm.forward(self.ndlin.samples)
41
42
43 self.failUnless(pm.sv[:1] > 1.0)
44 self.failUnless((pm.sv[1:] < 0.0001).all())
45
46
47 var = p.var(axis=0)
48
49
50 self.failUnless(var[:1] > 1.0)
51 self.failUnless((var[1:] < 0.0001).all())
52
53
54 pr = pm.reverse(p)
55
56 self.failUnlessEqual(pr.shape, (40,20))
57 self.failUnless(N.abs(pm.reverse(p) - self.ndlin.samples).sum() < 0.0001)
58
59
61 pm = SVDMapper()
62
63 pm.train(self.largefeat)
64
65
66 self.failUnlessEqual(pm.proj.shape, (40, 10))
67
68
69 self.failUnless(pm.sv[:1] > 10)
70 self.failUnless((pm.sv[1:] < 10).all())
71
72
73 p = pm.forward(self.largefeat.samples)
74
75
76 var = p.var(axis=0)
77
78
79 self.failUnless(var[:1] > 1.0)
80 self.failUnless((var[1:] < 0.0001).all())
81
82
83 rp = pm.reverse(p)
84 self.failUnlessEqual(rp.shape, self.largefeat.samples.shape)
85 self.failUnless((N.round(rp) == self.largefeat.samples).all())
86
87 self.failUnlessEqual(pm.getInSize(), 40)
88 self.failUnlessEqual(pm.getOutSize(), 10)
89
90
91 pm2 = deepcopy(pm)
92
93
94 pm2.selectOut([0,1])
95
96
97 self.failUnlessEqual(pm2.getInSize(), 40)
98 self.failUnlessEqual(pm2.getOutSize(), 2)
99
100
101 self.failUnlessEqual(pm.getInSize(), 40)
102 self.failUnlessEqual(pm.getOutSize(), 10)
103
104
105 rp2 = pm2.reverse(p[:,[0,1]])
106 self.failUnlessEqual(rp2.shape, self.largefeat.samples.shape)
107 self.failUnless(N.abs(rp2 - self.largefeat.samples).sum() < 0.0001)
108
109
110
111 data = N.random.normal(size=(98,40))
112 data_f = pm.forward(data)
113
114 self.failUnlessEqual(data_f.shape, (98,10))
115
116 data_r = pm.reverse(data_f)
117 self.failUnlessEqual(data_r.shape, (98,40))
118
119
120
123
124
125 if __name__ == '__main__':
126 import runner
127