-
Notifications
You must be signed in to change notification settings - Fork 29
/
test_input_output_labels.py
321 lines (319 loc) · 15 KB
/
test_input_output_labels.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
import numpy as np
import pytest
from psyneulink.core.compositions.composition import Composition
from psyneulink.core.components.mechanisms.processing.processingmechanism import ProcessingMechanism
from psyneulink.core.components.mechanisms.processing.transfermechanism import TransferMechanism
from psyneulink.core.globals.keywords import ENABLED, INPUT_LABELS_DICT, OUTPUT_LABELS_DICT
# FIX 5/8/20 ELIMINATE SYSTEM [JDC] -- CONVERTED TO COMPOSITION, BUT REQUIRE REFACTORING OF LABEL HANDLING
# class TestMechanismInputLabels:
# def test_dict_of_floats(self):
# input_labels_dict = {"red": 1,
# "green":0}
#
# M = ProcessingMechanism(params={INPUT_LABELS_DICT: input_labels_dict})
# C = Composition(pathways=[M])
#
# store_input_labels = []
#
# def call_after_trial():
# store_input_labels.append(M.get_input_labels(C))
#
# C.run(inputs={M:['red', 'green', 'green', 'red']},
# call_after_trial=call_after_trial)
# assert np.allclose(C.results, [[[1.]], [[0.]], [[0.]], [[1.]]])
# assert store_input_labels == [['red'], ['green'], ['green'], ['red']]
# C.run(inputs={M:[1, 'green', 0, 'red']})
# assert np.allclose(C.results, [[[1.]], [[0.]], [[0.]], [[1.]], [[1.]], [[0.]], [[0.]], [[1.]]])
#
# def test_dict_of_arrays(self):
# input_labels_dict = {"red": [1, 0, 0],
# "green": [0, 1, 0],
# "blue": [0, 0, 1]}
# M = ProcessingMechanism(default_variable=[[0, 0, 0]],
# params={INPUT_LABELS_DICT: input_labels_dict})
# C = Composition(pathways=[M])
#
# store_input_labels = []
#
# def call_after_trial():
# store_input_labels.append(M.get_input_labels(C))
#
# C.run(inputs={M:['red', 'green', 'blue', 'red']},
# call_after_trial=call_after_trial)
# assert np.allclose(C.results, [[[1, 0, 0]], [[0, 1, 0]], [[0, 0, 1]], [[1, 0, 0]]])
# assert store_input_labels == [['red'], ['green'], ['blue'], ['red']]
#
# C.run(inputs={M:'red'})
# assert np.allclose(C.results, [[[1, 0, 0]], [[0, 1, 0]], [[0, 0, 1]], [[1, 0, 0]], [[1, 0, 0]]])
#
# C.run(inputs={M:['red']})
# assert np.allclose(C.results, [[[1, 0, 0]], [[0, 1, 0]], [[0, 0, 1]], [[1, 0, 0]], [[1, 0, 0]], [[1, 0, 0]]])
#
# # def test_dict_of_arrays_2_input_ports(self):
# # input_labels_dict = {"red": [0],
# # "green": [1]}
# #
# # M = ProcessingMechanism(default_variable=[[0], [0]],
# # params={INPUT_LABELS_DICT: input_labels_dict})
# # P = Process(pathway=[M])
# # S = System(processes=[P])
# #
# # M_output = []
# # store_input_labels = []
# #
# # def call_after_trial():
# # M_output.append(M.value)
# # store_input_labels.append(M.get_input_labels(S))
# #
# # S.run(inputs=[['red', 'green'], ['green', 'red']],
# # call_after_trial=call_after_trial)
# #
# # assert np.allclose(M_output, [[[0], [1]], [[1], [0]]])
# # assert store_input_labels == [['red', 'green'], ['green', 'red']]
# #
# # S.run(inputs=[[[0], 'green'], [[1], 'red']],
# # call_after_trial=call_after_trial)
# #
# # assert np.allclose(M_output, [[[0], [1]], [[1], [0]], [[0], [1]], [[1], [0]]])
#
# # no longer valid:
# # def test_dict_of_2d_arrays(self):
# # input_labels_dict = {"red": [[1, 0], [1, 0]],
# # "green": [[0, 1], [0, 1]],
# # "blue": [[0, 1], [1, 0]]}
# # M = TransferMechanism(default_variable=[[0, 0], [0, 0]],
# # params={INPUT_LABELS_DICT: input_labels_dict})
# # P = Process(pathway=[M])
# # S = System(processes=[P])
# #
# # store_input_labels = []
# #
# # def call_after_trial():
# # store_input_labels.append(M.get_input_labels(S))
# #
# # S.run(inputs=['red', 'green', 'blue'],
# # call_after_trial=call_after_trial)
# # assert np.allclose(S.results, [[[1, 0], [1, 0]], [[0, 1], [0, 1]], [[0, 1], [1, 0]]])
# # assert store_input_labels == ['red', 'green', 'blue']
# #
# # S.run(inputs='red')
# # assert np.allclose(S.results, [[[1, 0], [1, 0]], [[0, 1], [0, 1]], [[0, 1], [1, 0]], [[1, 0], [1, 0]]])
#
# def test_dict_of_dicts_1_input_port(self):
# input_labels_dict = {0: {"red": [1, 0],
# "green": [0, 1]}}
#
# M = TransferMechanism(default_variable=[[0, 0]],
# params={INPUT_LABELS_DICT: input_labels_dict})
# C = Composition(pathways=[M])
#
# store_input_labels = []
#
# def call_after_trial():
# store_input_labels.append(M.get_input_labels(C))
#
# C.run(inputs={M:[['red'], ['green'], ['green']]},
# call_after_trial=call_after_trial)
# assert np.allclose(C.results, [[[1, 0]], [[0, 1]], [[0, 1]]])
# assert [['red'], ['green'], ['green']] == store_input_labels
#
# C.run(inputs={M:'red'})
# assert np.allclose(C.results, [[[1, 0]], [[0, 1]], [[0, 1]], [[1, 0]]])
#
# C.run(inputs={M:['red']})
# assert np.allclose(C.results, [[[1, 0]], [[0, 1]], [[0, 1]], [[1, 0]], [[1, 0]]])
#
# def test_dict_of_dicts(self):
# input_labels_dict = {0: {"red": [1, 0],
# "green": [0, 1]},
# 1: {"red": [0, 1],
# "green": [1, 0]}}
#
#
# M = TransferMechanism(default_variable=[[0, 0], [0, 0]],
# params={INPUT_LABELS_DICT: input_labels_dict})
# C = Composition(pathways=[M])
#
# C.run(inputs={M:[['red', 'green'], ['green', 'red'], ['green', 'green']]})
# assert np.allclose(C.results, [[[1, 0], [1, 0]], [[0, 1], [0, 1]], [[0, 1], [1, 0]]])
#
# C.run(inputs={M:[['red', [1, 0]], ['green', 'red'], [[0,1], 'green']]})
# assert np.allclose(C.results, [[[1, 0], [1, 0]], [[0, 1], [0, 1]], [[0, 1], [1, 0]], [[1, 0], [1, 0]], [[0, 1], [0, 1]], [[0, 1], [1, 0]]])
#
# def test_3_input_ports_2_label_dicts(self):
# input_labels_dict = {0: {"red": [1, 0],
# "green": [0, 1]},
# 2: {"red": [0, 1],
# "green": [1, 0]}}
#
#
# M = TransferMechanism(default_variable=[[0, 0], [0, 0], [0, 0]],
# params={INPUT_LABELS_DICT: input_labels_dict})
# C = Composition(pathways=[M])
#
# C.run(inputs={M:[['red', [0, 0], 'green'], ['green', [1, 1], 'red'], ['green', [2, 2], 'green']]})
# assert np.allclose(C.results, [[[1, 0], [0, 0], [1, 0]], [[0, 1], [1, 1], [0, 1]], [[0, 1], [2, 2], [1, 0]]])
#
# C.run(inputs={M:[['red', [0, 0], [1, 0]], ['green', [1, 1], 'red'], [[0,1], [2, 2], 'green']]})
# assert np.allclose(C.results, [[[1, 0], [0, 0], [1, 0]], [[0, 1], [1, 1], [0, 1]], [[0, 1], [2, 2], [1, 0]], [[1, 0], [0, 0], [1, 0]], [[0, 1], [1, 1], [0, 1]], [[0, 1], [2, 2], [1, 0]]])
#
# class TestMechanismTargetLabels:
# def test_dict_of_floats(self):
# input_labels_dict_M1 = {"red": 1,
# "green": 0}
# output_labels_dict_M2 = {"red": 0,
# "green": 1}
# M1 = ProcessingMechanism(params={INPUT_LABELS_DICT: input_labels_dict_M1})
# M2 = ProcessingMechanism(params={OUTPUT_LABELS_DICT: output_labels_dict_M2})
# C = Composition()
# learning_pathway = C.add_backpropagation_learning_pathway(pathway=[M1, M2], learning_rate=0.25)
# target = learning_pathway.target
# learned_matrix = []
#
# def record_matrix_after_trial():
# learned_matrix.append(M2.path_afferents[0].get_mod_matrix(C))
#
# C.learn(inputs={M1: ['red', 'green', 'green', 'red'],
# target:['red', 'green', 'green', 'red']},
# call_after_trial=record_matrix_after_trial)
#
# assert np.allclose(C.results, [[[1.]], [[0.]], [[0.]], [[0.75]]])
# assert np.allclose(learned_matrix, [[[0.75]], [[0.75]], [[0.75]], [[0.5625]]])
#
# def test_dict_of_arrays(self):
# input_labels_dict_M1 = {"red": [1, 1],
# "green": [0, 0]}
# output_labels_dict_M2 = {"red": [0, 0],
# "green": [1, 1]}
# M1 = ProcessingMechanism(size=2,
# params={INPUT_LABELS_DICT: input_labels_dict_M1})
# M2 = ProcessingMechanism(size=2,
# params={OUTPUT_LABELS_DICT: output_labels_dict_M2})
# C = Composition()
# learning_pathway = C.add_backpropagation_learning_pathway(pathway=[M1, M2], learning_rate=0.25)
# target = learning_pathway.target
# learned_matrix = []
# count = []
#
# def record_matrix_after_trial():
# learned_matrix.append(M2.path_afferents[0].get_mod_matrix(C))
# count.append(1)
#
# C.learn(inputs={M1: ['red', 'green', 'green', 'red'],
# target: ['red', 'green', 'green', 'red']},
# call_after_trial=record_matrix_after_trial)
# assert np.allclose(C.results, [[[1, 1]], [[0., 0.]], [[0., 0.]], [[0.5, 0.5]]])
# assert np.allclose(learned_matrix, [np.array([[0.75, -0.25], [-0.25, 0.75]]),
# np.array([[0.75, -0.25], [-0.25, 0.75]]),
# np.array([[0.75, -0.25], [-0.25, 0.75]]),
# np.array([[0.625, -0.375], [-0.375, 0.625]])])
#
# def test_dict_of_subdicts(self):
# input_labels_dict_M1 = {"red": [1, 1],
# "green": [0, 0]}
# output_labels_dict_M2 = {0: {"red": [0, 0],
# "green": [1, 1]}
# }
# M1 = ProcessingMechanism(size=2,
# params={INPUT_LABELS_DICT: input_labels_dict_M1})
# M2 = ProcessingMechanism(size=2,
# params={OUTPUT_LABELS_DICT: output_labels_dict_M2})
# C = Composition()
#
# learning_pathway = C.add_backpropagation_learning_pathway(pathway=[M1, M2], learning_rate=0.25)
# target = learning_pathway.target
# learned_matrix = []
# count = []
#
# def record_matrix_after_trial():
# learned_matrix.append(M2.path_afferents[0].get_mod_matrix(C))
# count.append(1)
#
# C.learn(inputs={M1: ['red', 'green', 'green', 'red'],
# target: ['red', 'green', 'green', 'red']},
# call_after_trial=record_matrix_after_trial)
# assert np.allclose(C.results, [[[1, 1]], [[0., 0.]], [[0., 0.]], [[0.5, 0.5]]])
# assert np.allclose(learned_matrix, [np.array([[0.75, -0.25], [-0.25, 0.75]]),
# np.array([[0.75, -0.25], [-0.25, 0.75]]),
# np.array([[0.75, -0.25], [-0.25, 0.75]]),
# np.array([[0.625, -0.375], [-0.375, 0.625]])])
#
#
# class TestMechanismOutputLabels:
#
# def test_dict_of_floats(self):
# input_labels_dict = {"red": 1,
# "green": 0}
# output_labels_dict = {"red": 1,
# "green":0}
# M = ProcessingMechanism(params={INPUT_LABELS_DICT: input_labels_dict,
# OUTPUT_LABELS_DICT: output_labels_dict})
# C = Composition(pathways=[M])
#
# store_output_labels = []
#
# def call_after_trial():
# store_output_labels.append(M.get_output_labels(C))
#
# C.run(inputs={M: ['red', 'green', 'green', 'red']},
# call_after_trial=call_after_trial)
# assert np.allclose(C.results, [[[1.]], [[0.]], [[0.]], [[1.]]])
# assert store_output_labels == [['red'], ['green'], ['green'], ['red']]
#
# store_output_labels = []
# C.run(inputs={M: [1, 'green', 0, 'red']},
# call_after_trial=call_after_trial)
# assert np.allclose(C.results, [[[1.]], [[0.]], [[0.]], [[1.]], [[1.]], [[0.]], [[0.]], [[1.]]])
# assert store_output_labels == [['red'], ['green'], ['green'], ['red']]
#
# def test_dict_of_arrays(self):
# input_labels_dict = {"red": [1.0, 0.0],
# "green": [0.0, 1.0]}
# output_labels_dict = {"red": [1.0, 0.0],
# "green": [0.0, 1.0]}
# M = ProcessingMechanism(size=2,
# params={INPUT_LABELS_DICT: input_labels_dict,
# OUTPUT_LABELS_DICT: output_labels_dict})
# C = Composition(pathways=[M])
#
# store_output_labels = []
#
# def call_after_trial():
# store_output_labels.append(M.get_output_labels(S))
#
# C.run(inputs={M:['red', 'green', 'green', 'red']},
# call_after_trial=call_after_trial)
# assert np.allclose(C.results, [[[1.0, 0.0]], [[0.0, 1.0]], [[0.0, 1.0]], [[1.0, 0.0]]])
# assert store_output_labels == [['red'], ['green'], ['green'], ['red']]
#
# store_output_labels = []
# C.run(inputs={M: [[1.0, 0.0], 'green', [0.0, 1.0], 'red']},
# call_after_trial=call_after_trial)
# assert np.allclose(C.results, [[[1.0, 0.0]], [[0.0, 1.0]], [[0.0, 1.0]], [[1.0, 0.0]], [[1.0, 0.0]], [[0.0, 1.0]], [[0.0, 1.0]], [[1.0, 0.0]]])
# assert store_output_labels == [['red'], ['green'], ['green'], ['red']]
# # S.show_graph(show_mechanism_structure="labels")
#
# def test_not_all_output_port_values_have_label(self):
# input_labels_dict = {"red": [1.0, 0.0],
# "green": [0.0, 1.0],
# "blue": [2.0, 2.0]}
# output_labels_dict = {"red": [1.0, 0.0],
# "green": [0.0, 1.0]}
# M = ProcessingMechanism(size=2,
# params={INPUT_LABELS_DICT: input_labels_dict,
# OUTPUT_LABELS_DICT: output_labels_dict})
# C = Composition(pathways=[M])
#
# store_output_labels = []
#
# def call_after_trial():
# store_output_labels.append(M.get_output_labels(C))
#
# C.run(inputs={M: ['red', 'blue', 'green', 'blue']},
# call_after_trial=call_after_trial)
# assert np.allclose(C.results, [[[1.0, 0.0]], [[2.0, 2.0]], [[0.0, 1.0]], [[2.0, 2.0]]])
#
# assert store_output_labels[0] == ['red']
# assert np.allclose(store_output_labels[1], [[2.0, 2.0]])
# assert store_output_labels[2] == ['green']
# assert np.allclose(store_output_labels[3], [[2.0, 2.0]])