/
test_accumulator_integrator.py
193 lines (165 loc) · 6.58 KB
/
test_accumulator_integrator.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
from psyneulink.core.components.functions.distributionfunctions import NormalDist
from psyneulink.core.components.functions.statefulfunctions.integratorfunctions import AccumulatorIntegrator
from psyneulink.core.components.mechanisms.processing.processingmechanism import ProcessingMechanism
from psyneulink.core.components.mechanisms.processing.transfermechanism import TransferMechanism
from psyneulink.core.components.projections.pathway.mappingprojection import MappingProjection
from psyneulink.core.compositions.composition import Composition
from psyneulink.core.globals.keywords import MATRIX
class TestAccumulator():
def test_accumulator_standalone(self):
A = AccumulatorIntegrator()
val = A()
assert np.allclose([[0]], val)
def test_accumulator_initializer_len_3(self):
A = AccumulatorIntegrator(default_variable=[[0.0], [1.0], [2.0]],
initializer=[[0.0], [1.0], [2.0]],
increment=1)
assert np.allclose(A(), [[1],[2],[3]])
assert np.allclose(A(), [[2],[3],[4]])
assert np.allclose(A(), [[3],[4],[5]])
# RATE ------------------------------------------------------------
def test_accumulator_standalone_rate_float(self):
A = AccumulatorIntegrator(rate = 0.1, increment=1)
A()
A()
A()
val = A()
assert np.allclose([[1.111]], val)
def test_accumulator_standalone_rate_list(self):
A = AccumulatorIntegrator(rate = [0.1, 0.5, 0.9],
increment=1)
A()
A()
A()
val = A()
assert np.allclose([[1.111, 1.875, 3.439]], val)
def test_accumulator_standalone_rate_ndarray(self):
A = AccumulatorIntegrator(rate = [0.1, 0.5, 0.9],
increment=1)
A()
A()
A()
val = A()
assert np.allclose([[1.111, 1.875, 3.439]], val)
# INCREMENT ------------------------------------------------------------
def test_accumulator_standalone_increment_float(self):
A = AccumulatorIntegrator(increment = 0.1)
A()
A()
A()
val = A()
assert np.allclose([[0.4]], val)
def test_accumulator_standalone_increment_list(self):
A = AccumulatorIntegrator(increment = [0.1, 0.5, 0.9])
A()
A()
A()
val = A()
assert np.allclose([[0.4, 2., 3.6]], val)
def test_accumulator_standalone_increment_ndarray(self):
A = AccumulatorIntegrator(increment = np.array([0.1, 0.5, 0.9]))
A()
A()
A()
val = A()
assert np.allclose([[0.4, 2., 3.6]], val)
# RATE X INCRMEMNT -------------------------------------------------
def test_accumulator_standalone_rate_float_incement_float(self):
A = AccumulatorIntegrator(
rate=2.0,
increment = 3.0)
A()
A()
A()
val = A()
assert np.allclose([[45]], val)
def test_accumulator_standalone_rate_float_incement_list(self):
A = AccumulatorIntegrator(
rate=2.0,
increment = [3.5, 4.0, 5.0])
A()
A()
A()
val = A()
assert np.allclose([[52.5, 60., 75.]], val)
def test_accumulator_standalone_rate_list_incement_float(self):
A = AccumulatorIntegrator(
rate=[2.0, 2.5, -3.0],
increment = -1.4)
A()
A()
A()
val = A()
assert np.allclose([[-21., -35.525, 28.]], val)
def test_accumulator_standalone_rate_list_incement_list(self):
A = AccumulatorIntegrator(
rate=[-.5, 2.0, -3.5],
increment = [-1.0, .5, -2.0])
A()
A()
A()
val = A()
assert np.allclose([[-0.625, 7.5, 66.25]], val)
# NOISE ------------------------------------------------------------
def test_accumulator_standalone_noise_float(self):
A = AccumulatorIntegrator(rate = 1.0, noise=10.0)
A()
A()
A()
val = A()
assert np.allclose([[40]], val)
def test_accumulator_standalone_noise_list(self):
A = AccumulatorIntegrator(rate = 1.0, noise=[10.0, 20.0, 30.0])
A()
A()
A()
val = A()
assert np.allclose([[40., 80., 120.]], val)
def test_accumulator_standalone_noise_ndarray(self):
A = AccumulatorIntegrator(rate = 1.0, noise=[10.0, 20.0, 30.0])
A()
A()
A()
val = A()
assert np.allclose([[40., 80., 120.]], val)
def test_accumulator_standalone_noise_function(self):
A = AccumulatorIntegrator(rate = 1.0, noise=NormalDist(standard_deviation=0.1))
A()
A()
A()
val = A()
assert np.allclose([[-0.34591555]], val)
def test_accumulator_standalone_noise_function_in_array(self):
A = AccumulatorIntegrator(noise=[10, NormalDist(standard_deviation=0.1), 20])
A()
A()
A()
val = A()
expected_val = [[40.0, 0.2480800486427607, 80.0]]
for i in range(len(val)):
for j in range(len(val[i])):
assert np.allclose(expected_val[i][j], val[i][j])
def test_accumulator_as_function_of_processing_mech(self):
P = ProcessingMechanism(function=AccumulatorIntegrator(initializer=[0.0],
rate=.02,
increment=1))
P.execute(1.0)
P.execute(1.0)
val = P.execute(1.0)
assert np.allclose(val, [[1.0204]])
def test_accumulator_as_function_of_matrix_param_of_mapping_projection(self):
# Test that accumulator is function of parameter_port of mapping project,
# and that its increment param works properly (used as modulatory param by LearningProjetion)
T1 = TransferMechanism(size=3)
T2 = TransferMechanism(size=3)
M = MappingProjection(sender=T1, receiver=T2)
C = Composition()
C.add_linear_processing_pathway([T1, M, T2])
C.run(inputs={T1: [1.0, 1.0, 1.0]})
assert np.allclose(M.matrix.base, [[ 1., 0., 0.], [ 0., 1., 0.],[ 0., 0., 1.]])
M.parameter_ports[MATRIX].function.parameters.increment.set(2, C)
C.run(inputs={T1: [1.0, 1.0, 1.0]})
assert np.allclose(M.matrix.base, [[ 3., 2., 2.], [ 2., 3., 2.], [ 2., 2., 3.]])
C.run(inputs={T1: [1.0, 1.0, 1.0]})
assert np.allclose(M.matrix.base, [[ 5., 4., 4.], [ 4., 5., 4.], [ 4., 4., 5.]])