-
Notifications
You must be signed in to change notification settings - Fork 29
/
test_mechanisms.py
193 lines (163 loc) · 7.48 KB
/
test_mechanisms.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import numpy as np
import psyneulink as pnl
import pytest
class TestMechanism:
@pytest.mark.mechanism
@pytest.mark.parametrize(
'mechanism, default_variable, result_variable',
[
(pnl.TransferMechanism, [0], np.array([[0]])),
(pnl.IntegratorMechanism, [0], np.array([[0]])),
]
)
def test_transfer_mech_instantiation(self, mechanism, default_variable, result_variable):
T = mechanism(default_variable=default_variable)
assert T.defaults.variable == result_variable
assert T.defaults.value == result_variable
assert T.function.defaults.variable == result_variable
assert T.function.defaults.value == result_variable
assert T.input_port.defaults.variable == result_variable[0]
assert T.input_port.defaults.value == result_variable[0]
assert T.input_port.function.defaults.variable == result_variable[0]
assert T.input_port.function.defaults.value == result_variable[0]
@pytest.mark.mechanism
@pytest.mark.parametrize(
'mechanism_type, default_variable, mechanism_value, function_value',
[
(pnl.ObjectiveMechanism, [0, 0, 0], np.array([[0, 0, 0]]), np.array([[0, 0, 0]]))
]
)
def test_value_shapes(self, mechanism_type, default_variable, mechanism_value, function_value):
M = mechanism_type(default_variable=default_variable)
assert M.defaults.value.shape == mechanism_value.shape
assert M.function.defaults.value.shape == function_value.shape
class TestMechanismFunctionParameters:
f = pnl.Linear()
i = pnl.SimpleIntegrator()
mech_1 = pnl.TransferMechanism(function=f, integrator_function=i)
mech_2 = pnl.TransferMechanism(function=f, integrator_function=i)
integrator_mechanism = pnl.IntegratorMechanism(function=i)
@pytest.mark.parametrize(
"f, g",
[
pytest.param(
mech_1.defaults.function,
mech_2.defaults.function,
id="function_defaults",
),
pytest.param(
mech_1.defaults.function,
mech_1.parameters.function.get(),
id="function_default-and-value",
),
pytest.param(
mech_1.defaults.function,
mech_2.parameters.function.get(),
id="function_default-and-other-value",
),
pytest.param(
mech_1.defaults.integrator_function,
mech_2.defaults.integrator_function,
id="integrator_function_defaults",
),
pytest.param(
mech_1.defaults.integrator_function,
mech_1.parameters.integrator_function.get(),
id="integrator_function_default-and-value",
),
pytest.param(
mech_1.defaults.integrator_function,
mech_2.parameters.integrator_function.get(),
id="integrator_function_default-and-other-value",
),
],
)
def test_function_parameter_distinctness(self, f, g):
assert f is not g
@pytest.mark.parametrize(
"f, owner",
[
pytest.param(
mech_1.parameters.function.get(),
mech_1,
id='function'
),
pytest.param(
integrator_mechanism.class_defaults.function,
integrator_mechanism.class_parameters.function,
id="class_default_function"
),
pytest.param(
mech_1.defaults.function,
mech_1.parameters.function,
id="default_function"
),
pytest.param(
mech_1.parameters.termination_measure.get(),
mech_1,
id='termination_measure'
),
pytest.param(
mech_1.class_defaults.termination_measure,
mech_1.class_parameters.termination_measure,
id="class_default_termination_measure"
),
pytest.param(
mech_1.defaults.termination_measure,
mech_1.parameters.termination_measure,
id="default_termination_measure"
),
]
)
def test_function_parameter_ownership(self, f, owner):
assert f.owner is owner
@pytest.mark.parametrize(
'param_name, function',
[
('function', f),
('integrator_function', i),
]
)
def test_function_parameter_assignment(self, param_name, function):
# mech_1 should use the exact instances, mech_2 should have copies
assert getattr(self.mech_1.parameters, param_name).get() is function
assert getattr(self.mech_2.parameters, param_name).get() is not function
class TestResetValues:
def test_reset_state_integrator_mechanism(self):
A = pnl.IntegratorMechanism(name='A', function=pnl.DriftDiffusionIntegrator())
# Execute A twice
# [0] saves decision variable only (not time)
original_output = [A.execute(1.0)[0], A.execute(1.0)[0]]
# SAVING STATE - - - - - - - - - - - - - - - - - - - - - - - - -
reset_stateful_functions_to = {}
for attr in A.function.stateful_attributes:
reset_stateful_functions_to[attr] = getattr(A.function, attr)
print(reset_stateful_functions_to)
# Execute A twice AFTER saving the state so that it continues accumulating.
# We expect the next two outputs to repeat once we reset the state b/c we will return it to the current state
output_after_saving_state = [A.execute(1.0)[0], A.execute(1.0)[0]]
# RESETTING STATE - - - - - - - - - - - - - - - - - - - - - - - -
A.reset(**reset_stateful_functions_to)
# We expect these results to match the results from immediately after saving the state
output_after_reinitialization = [A.execute(1.0)[0], A.execute(1.0)[0]]
assert np.allclose(output_after_saving_state, output_after_reinitialization)
assert np.allclose(original_output, [np.array([[1.0]]), np.array([[2.0]])])
assert np.allclose(output_after_reinitialization, [np.array([[3.0]]), np.array([[4.0]])])
def test_reset_state_transfer_mechanism(self):
A = pnl.TransferMechanism(name='A', integrator_mode=True)
# Execute A twice
original_output = [A.execute(1.0), A.execute(1.0)]
# SAVING STATE - - - - - - - - - - - - - - - - - - - - - - - - -
reset_stateful_functions_to = {}
for attr in A.integrator_function.stateful_attributes:
reset_stateful_functions_to[attr] = getattr(A.integrator_function, attr)
# Execute A twice AFTER saving the state so that it continues accumulating.
# We expect the next two outputs to repeat once we reset the state b/c we will return it to the current state
output_after_saving_state = [A.execute(1.0), A.execute(1.0)]
# RESETTING STATE - - - - - - - - - - - - - - - - - - - - - - - -
A.reset(**reset_stateful_functions_to)
# We expect these results to match the results from immediately after saving the state
output_after_reinitialization = [A.execute(1.0), A.execute(1.0)]
assert np.allclose(output_after_saving_state, output_after_reinitialization)
assert np.allclose(original_output, [np.array([[0.5]]), np.array([[0.75]])])
assert np.allclose(output_after_reinitialization, [np.array([[0.875]]), np.array([[0.9375]])])