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