1
2
3
4
5
6
7
8
9 """Unit tests for PyMVPA SOM mapper"""
10
11
12 import unittest
13 import numpy as N
14 from mvpa import cfg
15 from mvpa.mappers.som import SimpleSOMMapper
16 from mvpa.datasets import Dataset
17
19
21 colors = [[0., 0., 0.], [0., 0., 1.], [0., 1., 0.],
22 [1., 0., 0.], [0., 1., 1.], [1., 0., 1.],
23 [1., 1., 0.], [1., 1., 1.]]
24 ds = Dataset(samples=colors, labels=1)
25
26
27 som = SimpleSOMMapper((10, 5), 200, learning_rate=0.05)
28
29
30 self.failUnlessRaises(RuntimeError, som._accessKohonen)
31 self.failUnlessRaises(RuntimeError, som.getInSize)
32 self.failUnlessRaises(RuntimeError, som.getOutSize)
33
34 som.train(ds)
35
36 self.failUnless(som.getInSize() == 3)
37 self.failUnless(som.getOutSize() == (10,5))
38
39 fmapped = som(colors)
40 self.failUnless(fmapped.shape == (8, 2))
41 for fm in fmapped:
42 self.failUnless(som.isValidOutId(fm))
43
44
45 rmapped = som.reverse(fmapped)
46
47 if cfg.getboolean('tests', 'labile', default='yes'):
48
49
50 self.failUnless((N.round(rmapped) == ds.samples).all())
51
52
55
56
57 if __name__ == '__main__':
58 import runner
59