1
2
3
4
5
6
7
8
9 """Unit tests for PyMVPA State parent class"""
10
11 import unittest, copy
12
13 import numpy as N
14
15 from mvpa.base import externals
16
17 from mvpa.misc.state import StateVariable, ClassWithCollections, \
18 ParameterCollection, _def_sep
19 from mvpa.misc.param import *
20 from mvpa.misc.exceptions import UnknownStateError
21
22 if __debug__:
23 from mvpa.base import debug
24
27
33
36
41
42
46
47
57
58
60
62 empty = TestClassEmpty()
63 blank = TestClassBlank()
64 blank2 = TestClassBlank()
65
66 self.failUnlessRaises(AttributeError, empty.__getattribute__, 'states')
67
68 self.failUnlessEqual(blank.states.items, {})
69 self.failUnless(blank.states.enabled == [])
70 self.failUnlessRaises(AttributeError, blank.__getattribute__, 'dummy')
71 self.failUnlessRaises(AttributeError, blank.__getattribute__, '_')
72
73
74
75 return
76
77
78 blank._registerState('state1', False)
79 self.failUnless(blank.states == ['state1'])
80
81 self.failUnless(blank.states.isEnabled('state1') == False)
82 self.failUnless(blank.states.enabled == [])
83 self.failUnlessRaises(UnknownStateError, blank.__getattribute__, 'state1')
84
85
86 blank.state1 = 123
87
88 self.failUnlessRaises(UnknownStateError, blank.__getattribute__, 'state1')
89
90
91 blank.states.enable('state1')
92 blank.state1 = 123
93 self.failUnless(blank.state1 == 123)
94
95
96
97 self.failUnless(blank2.states == [])
98 self.failUnlessRaises(AttributeError, blank2.__getattribute__, 'state1')
99
100
102 proper = TestClassProper()
103 proper2 = TestClassProper(enable_states=['state1'], disable_states=['state2'])
104
105
106 proper3 = TestClassProper(enable_states=['all'], disable_states='all')
107
108 self.failUnlessEqual(len(proper3.states.enabled), 0,
109 msg="disable_states should override anything in enable_states")
110
111 proper.state2 = 1000
112 value = proper.state2
113 self.failUnlessEqual(proper.state2, 1000, msg="Simple assignment/retrieval")
114
115 proper.states.disable('state2')
116 proper.state2 = 10000
117 self.failUnlessEqual(proper.state2, 1000, msg="Simple assignment after being disabled")
118
119 proper4 = copy.deepcopy(proper)
120
121 proper.states.reset('state2')
122 self.failUnlessRaises(UnknownStateError, proper.__getattribute__, 'state2')
123 """Must be blank after being reset"""
124
125 self.failUnlessEqual(proper4.state2, 1000,
126 msg="Simple assignment after being reset in original instance")
127
128
129 proper.states.enable(['state2'])
130 self.failUnlessEqual(set(proper.states.names), set(['state1', 'state2']))
131 if __debug__ and 'ENFORCE_STATES_ENABLED' in debug.active:
132
133 return
134 self.failUnless(proper.states.enabled == ['state2'])
135
136 self.failUnless(set(proper2.states.enabled) == set(['state1']))
137
138 self.failUnlessRaises(AttributeError, proper.__getattribute__, 'state12')
139
140
141 self.failUnlessEqual(proper2.states.listing,
142 ['%sstate1+%s: state1 doc' % (_def_sep, _def_sep),
143 '%sstate2%s: state2 doc' % (_def_sep, _def_sep)])
144
145
146 str_ = str(proper2)
147 self.failUnless(str_.find('2 states:') != -1)
148
149
150 self.failUnless(set(proper2.states.enabled), set(['state1']))
151
152 proper2.states.disable("all")
153 self.failUnlessEqual(set(proper2.states.enabled), set())
154
155 proper2.states.enable("all")
156 self.failUnlessEqual(len(proper2.states.enabled), 2)
157
158 proper2.state1, proper2.state2 = 1,2
159 self.failUnlessEqual(proper2.state1, 1)
160 self.failUnlessEqual(proper2.state2, 2)
161
162
163 proper2.states.reset('all')
164 self.failUnlessRaises(UnknownStateError, proper2.__getattribute__, 'state1')
165 self.failUnlessRaises(UnknownStateError, proper2.__getattribute__, 'state2')
166
167
169 """Check if we can store/restore set of enabled states"""
170
171 if __debug__ and 'ENFORCE_STATES_ENABLED' in debug.active:
172
173 return
174
175 proper = TestClassProper()
176 enabled_states = proper.states.enabled
177 proper.states.enable('state1')
178
179 self.failUnless(enabled_states != proper.states.enabled,
180 msg="New enabled states should differ from previous")
181
182 self.failUnless(set(proper.states.enabled) == set(['state1', 'state2']),
183 msg="Making sure that we enabled all states of interest")
184
185 proper.states.enabled = enabled_states
186 self.failUnless(enabled_states == proper.states.enabled,
187 msg="List of enabled states should return to original one")
188
189
190
191
215
216
218 """
219 Simple test if child gets state variables from the parent as well
220 """
221 proper = TestClassProperChild()
222 self.failUnlessEqual(set(proper.states.names),
223 set(['state1', 'state2', 'state4']))
224
225
232
233
234 class S2(ClassWithCollections):
235 v2 = StateVariable(enabled=True, doc="values12 is ...")
236
237 class S1_(S1):
238 pass
239
240 class S1__(S1_):
241 v1__ = StateVariable(enabled=False)
242
243 class S12(S1__, S2):
244 v12 = StateVariable()
245
246 s1, s2, s1_, s1__, s12 = S1(), S2(), S1_(), S1__(), S12()
247
248 self.failUnlessEqual(s1.states.isEnabled("v1"), True)
249 s1.v1 = 12
250 s12.v1 = 120
251 s2.v2 = 100
252
253 self.failUnlessEqual(len(s2.states.listing), 1)
254
255 self.failUnlessEqual(s1.v1, 12)
256 try:
257 tempvalue = s1__.v1__
258 self.fail("Should have puked since values were not enabled yet")
259 except:
260 pass
261
262
264
265 self.failUnlessRaises(TypeError, TestClassParametrized,
266 p2=34, enable_states=['state1'],
267 msg="Should raise an exception if argument doesn't correspond to"
268 "any parameter")
269 a = TestClassParametrized(p1=123, enable_states=['state1'])
270 self.failUnlessEqual(a.p1, 123, msg="We must have assigned value to instance")
271 self.failUnless('state1' in a.states.enabled,
272 msg="state1 must have been enabled")
273
274 if (__debug__ and 'ID_IN_REPR' in debug.active):
275
276 return
277
278
279 a_str = `a`
280 try:
281 import test_state
282 exec "a2=%s" % a_str
283 except Exception, e:
284 self.fail(msg="Failed to generate an instance out of "
285 "representation %s. Got exception: %s" % (a_str, e))
286
287 a2_str = `a2`
288 self.failUnless(a2_str == a_str,
289 msg="Generated object must have the same repr. Got %s and %s" %
290 (a_str, a2_str))
291
292
293 aparams_str = `a.params`
294 try:
295 import test_state
296 exec "aparams2=%s" % aparams_str
297 except Exception, e:
298 self.fail(msg="Failed to generate an instance out of "
299 "representation %s of params. Got exception: %s" % (aparams_str, e))
300
301
304
305
306 if __name__ == '__main__':
307 import runner
308