1
2
3
4
5
6
7
8
9 """Unit tests for PyMVPA Parameter class."""
10
11 import unittest, copy
12
13 import numpy as N
14
15 from mvpa.datasets import Dataset
16 from mvpa.misc.state import ClassWithCollections, StateVariable
17 from mvpa.misc.param import Parameter, KernelParameter
18
19 from tests_warehouse_clfs import SameSignClassifier
20
24
29
32
35
40
42
48
50 simple = SimpleClass()
51
52 self.failUnlessEqual(len(simple.params.items), 1)
53 self.failUnlessRaises(AttributeError, simple.__getattribute__, 'dummy')
54 self.failUnlessRaises(IndexError, simple.__getattribute__, '')
55
56 self.failUnlessEqual(simple.C, 1.0)
57 self.failUnlessEqual(simple.params.isSet("C"), False)
58 self.failUnlessEqual(simple.params.isSet(), False)
59 self.failUnlessEqual(simple.params["C"].isDefault, True)
60 self.failUnlessEqual(simple.params["C"].equalDefault, True)
61
62 simple.C = 1.0
63
64 self.failUnlessEqual(simple.params["C"].isDefault, True)
65 self.failUnlessEqual(simple.params["C"].equalDefault, True)
66
67 simple.C = 10.0
68 self.failUnlessEqual(simple.params.isSet("C"), True)
69 self.failUnlessEqual(simple.params.isSet(), True)
70 self.failUnlessEqual(simple.params["C"].isDefault, False)
71 self.failUnlessEqual(simple.params["C"].equalDefault, False)
72
73 self.failUnlessEqual(simple.C, 10.0)
74 simple.params["C"].resetvalue()
75 self.failUnlessEqual(simple.params.isSet("C"), True)
76
77 self.failUnlessEqual(simple.C, 1.0)
78 self.failUnlessRaises(AttributeError, simple.params.__getattribute__, 'B')
79
81 mixed = MixedClass()
82
83 self.failUnlessEqual(len(mixed.params.items), 2)
84 self.failUnlessEqual(len(mixed.states.items), 1)
85 self.failUnlessRaises(AttributeError, mixed.__getattribute__, 'kernel_params')
86
87 self.failUnlessEqual(mixed.C, 1.0)
88 self.failUnlessEqual(mixed.params.isSet("C"), False)
89 self.failUnlessEqual(mixed.params.isSet(), False)
90 mixed.C = 10.0
91 self.failUnlessEqual(mixed.params.isSet("C"), True)
92 self.failUnlessEqual(mixed.params.isSet("D"), False)
93 self.failUnlessEqual(mixed.params.isSet(), True)
94 self.failUnlessEqual(mixed.D, 3.0)
95
96
98 clf = ParametrizedClassifier()
99 self.failUnlessEqual(len(clf.params.items), 3)
100 self.failUnlessEqual(len(clf.kernel_params.items), 1)
101
102 clfe = ParametrizedClassifierExtended()
103 self.failUnlessEqual(len(clfe.params.items), 3)
104 self.failUnlessEqual(len(clfe.kernel_params.items), 2)
105 self.failUnlessEqual(len(clfe.kernel_params.listing), 2)
106
107
108 self.failUnlessEqual(clfe.kp2, 200.0)
109 clfe.kp2 = 201.0
110 self.failUnlessEqual(clfe.kp2, 201.0)
111 self.failUnlessEqual(clfe.kernel_params.isSet("kp2"), True)
112 clfe.train(Dataset(samples=[[0,0]], labels=[1], chunks=[1]))
113 self.failUnlessEqual(clfe.kernel_params.isSet("kp2"), False)
114 self.failUnlessEqual(clfe.kernel_params.isSet(), False)
115 self.failUnlessEqual(clfe.params.isSet(), False)
116
119
120
121 if __name__ == '__main__':
122 import runner
123