-
Notifications
You must be signed in to change notification settings - Fork 29
/
test_projection_specifications.py
572 lines (516 loc) · 29.1 KB
/
test_projection_specifications.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
import numpy as np
import pytest
import psyneulink as pnl
import psyneulink.core.components.functions.nonstateful.distributionfunctions
import psyneulink.core.components.functions.nonstateful.transferfunctions
import psyneulink.core.components.functions.stateful.integratorfunctions
class TestProjectionSpecificationFormats:
def test_projection_specification_formats(self):
"""Test various matrix and Projection specifications
Also tests assignment of Projections to pathway of Composition using add_linear_processing_pathway:
- Projection explicitly specified in sequence (M1_M2_proj)
- Projection pre-constructed and assigned to Mechanisms, but not specified in pathway(M2_M3_proj)
- Projection specified in pathway that is duplicate one preconstructed and assigned to Mechanisms (M3_M4_proj)
(currently it should be ignored; in the future, if/when Projections between the same sender and receiver
in different Compositions are allowed, then it should be used)
"""
M1 = pnl.ProcessingMechanism(size=2)
M2 = pnl.ProcessingMechanism(size=5)
M3 = pnl.ProcessingMechanism(size=4)
M4 = pnl.ProcessingMechanism(size=3)
M1_M2_matrix = (np.arange(2 * 5).reshape((2, 5)) + 1) / (2 * 5)
M2_M3_matrix = (np.arange(5 * 4).reshape((5, 4)) + 1) / (5 * 4)
M3_M4_matrix_A = (np.arange(4 * 3).reshape((4, 3)) + 1) / (4 * 5)
M3_M4_matrix_B = (np.arange(4 * 3).reshape((4, 3)) + 1) / (4 * 3)
M1_M2_proj = pnl.MappingProjection(matrix=M1_M2_matrix)
M2_M3_proj = pnl.MappingProjection(sender=M2,
receiver=M3,
matrix={pnl.VALUE: M2_M3_matrix,
pnl.FUNCTION: pnl.AccumulatorIntegrator,
pnl.FUNCTION_PARAMS: {pnl.DEFAULT_VARIABLE: M2_M3_matrix,
pnl.INITIALIZER: M2_M3_matrix}})
M3_M4_proj_A = pnl.MappingProjection(sender=M3, receiver=M4, matrix=M3_M4_matrix_A)
c = pnl.Composition()
c.add_linear_processing_pathway(pathway=[M1,
M1_M2_proj,
M2,
M3,
M3_M4_matrix_B,
M4])
assert np.allclose(M2_M3_proj.matrix.base, M2_M3_matrix)
assert M2.efferents[0] is M2_M3_proj
assert np.allclose(M3.efferents[0].matrix.base, M3_M4_matrix_A)
# This is if different Projections are allowed between the same sender and receiver in different Compositions:
# assert np.allclose(M3.efferents[1].matrix, M3_M4_matrix_B)
c.run(inputs={M1:[2, -30]})
# assert np.allclose(c.results, [[-130.19166667, -152.53333333, -174.875]])
assert np.allclose(c.results, [[ -78.115, -91.52 , -104.925]])
@pytest.mark.parametrize('args', [
(pnl.CONTROL, None),
(pnl.MODULATES, None),
(pnl.PROJECTIONS, None),
('mod and ctl', '"Both \'control\' and \'modulates\' arguments are specified in '
'the constructor for \'ControlSignal; Should use just \'control\'."'),
('proj and ctl', 'Both \'control\' and \'projections\' arguments are specified in the constructor for '
'\'ControlSignal; Must use just one or the other.'),
('proj and mod','"Both \'modulates\' and \'projections\' arguments are specified in the constructor for '
'\'ControlSignal; Should use just \'projections\' (or \'control\') "')
])
@pytest.mark.control
def test_control_signal_projections_arg(self, args):
M = pnl.ProcessingMechanism()
control_specs = {pnl.CONTROL: {'control':(pnl.SLOPE, M)},
pnl.MODULATES: {pnl.MODULATES:(pnl.SLOPE, M)},
pnl.PROJECTIONS: {pnl.PROJECTIONS:(pnl.SLOPE, M)},
'mod and ctl': {'control':(pnl.SLOPE, M),
pnl.MODULATES:(pnl.SLOPE, M)},
'proj and ctl': {'control':(pnl.SLOPE, M),
pnl.PROJECTIONS:(pnl.SLOPE, M)},
'proj and mod': {pnl.MODULATES:(pnl.SLOPE, M),
pnl.PROJECTIONS:(pnl.SLOPE, M)}
}
if args[0] in {'mod and ctl', 'proj and ctl', 'proj and mod'}:
from psyneulink.core.components.ports.modulatorysignals.controlsignal import ControlSignalError
with pytest.raises(ControlSignalError) as err:
pnl.ControlSignal(**control_specs[args[0]])
assert args[1] in str(err.value)
else:
ctl_sig = pnl.ControlSignal(**control_specs[args[0]])
assert ctl_sig._init_args[pnl.PROJECTIONS][0][0] == pnl.SLOPE
assert ctl_sig._init_args[pnl.PROJECTIONS][0][1] is M
@pytest.mark.parametrize('args', [
(pnl.GATE, None),
(pnl.MODULATES, None),
(pnl.PROJECTIONS, None),
('mod and gate', '"Both \'gate\' and \'modulates\' arguments are specified in the constructor for '
'\'GatingSignal; Should use just \'gate\'."'),
('proj and gate', 'Both \'gate\' and \'projections\' arguments are specified in the constructor for '
'\'GatingSignal; Must use just one or the other.'),
('proj and mod','"Both \'modulates\' and \'projections\' arguments are specified in the constructor for '
'\'GatingSignal; Should use just \'projections\' (or \'gate\') "')
])
@pytest.mark.control
def test_gating_signal_projections_arg(self, args):
M = pnl.ProcessingMechanism()
gating_specs = {pnl.GATE: {'gate':(pnl.SLOPE, M)},
pnl.MODULATES: {pnl.MODULATES:(pnl.SLOPE, M)},
pnl.PROJECTIONS: {pnl.PROJECTIONS:(pnl.SLOPE, M)},
'mod and gate': {'gate':(pnl.SLOPE, M),
pnl.MODULATES:(pnl.SLOPE, M)},
'proj and gate': {'gate':(pnl.SLOPE, M),
pnl.PROJECTIONS:(pnl.SLOPE, M)},
'proj and mod': {pnl.MODULATES:(pnl.SLOPE, M),
pnl.PROJECTIONS:(pnl.SLOPE, M)}
}
if args[0] in {'mod and gate', 'proj and gate', 'proj and mod'}:
from psyneulink.core.components.ports.modulatorysignals.gatingsignal import GatingSignalError
with pytest.raises(GatingSignalError) as err:
pnl.GatingSignal(**gating_specs[args[0]])
assert args[1] in str(err.value)
else:
gating_sig = pnl.GatingSignal(**gating_specs[args[0]])
assert gating_sig._init_args[pnl.PROJECTIONS][0][0] == pnl.SLOPE
assert gating_sig._init_args[pnl.PROJECTIONS][0][1] is M
@pytest.mark.parametrize("control_spec, gating_spec, extra_spec",
[
[pnl.CONTROL, pnl.GATE, ''],
[pnl.PROJECTIONS, pnl.PROJECTIONS, ''],
[pnl.CONTROL, pnl.GATE, pnl.PROJECTIONS]
]
)
@pytest.mark.control
def test_multiple_modulatory_projection_specs(self, control_spec, gating_spec, extra_spec):
M = pnl.DDM(name='MY DDM')
ctl_sig_spec = {control_spec: [M.parameter_ports[pnl.DRIFT_RATE],
M.parameter_ports[pnl.THRESHOLD]]}
gating_sig_spec = {gating_spec: [M.output_ports[pnl.DECISION_VARIABLE],
M.output_ports[pnl.RESPONSE_TIME]]}
if extra_spec:
ctl_sig_spec.update({extra_spec:[M.parameter_ports[pnl.STARTING_VALUE]]})
gating_sig_spec.update({extra_spec:[M.output_ports[pnl.RESPONSE_TIME]]})
ctl_err_msg = '"Both \'PROJECTIONS\' and \'CONTROL\' entries found in specification dict for ' \
'\'ControlSignal\' of \'ControlMechanism-0\'. Must use only one or the other."'
with pytest.raises(pnl.ControlSignalError) as err:
pnl.ControlMechanism(control_signals=[ctl_sig_spec])
assert ctl_err_msg == str(err.value)
gating_err_msg = '"Both \'PROJECTIONS\' and \'GATE\' entries found in specification dict for ' \
'\'GatingSignal\' of \'GatingMechanism-0\'. Must use only one or the other."'
with pytest.raises(pnl.GatingSignalError) as err:
pnl.GatingMechanism(gating_signals=[gating_sig_spec])
assert gating_err_msg == str(err.value)
else:
# G = pnl.GatingMechanism(gating_signals=[gating_sig_spec])
C = pnl.ControlMechanism(control_signals=[ctl_sig_spec])
G = pnl.GatingMechanism(gating_signals=[gating_sig_spec])
assert len(C.control_signals)==1
assert len(C.control_signals[0].efferents)==2
assert M.parameter_ports[
psyneulink.core.components.functions.nonstateful.distributionfunctions.DRIFT_RATE].mod_afferents[0] == C.control_signals[0].efferents[0]
assert M.parameter_ports[
psyneulink.core.globals.keywords.THRESHOLD].mod_afferents[0] == C.control_signals[0].efferents[1]
assert len(G.gating_signals)==1
assert len(G.gating_signals[0].efferents)==2
assert M.output_ports[pnl.DECISION_VARIABLE].mod_afferents[0]==G.gating_signals[0].efferents[0]
assert M.output_ports[pnl.RESPONSE_TIME].mod_afferents[0]==G.gating_signals[0].efferents[1]
@pytest.mark.control
def test_multiple_modulatory_projections_with_port_Name(self):
M = pnl.DDM(name='MY DDM')
C = pnl.ControlMechanism(control_signals=[{'DECISION_CONTROL':[M.parameter_ports[
psyneulink.core.components.functions.nonstateful.distributionfunctions.DRIFT_RATE],
M.parameter_ports[
psyneulink.core.globals.keywords.THRESHOLD]]}])
G = pnl.GatingMechanism(gating_signals=[{'DDM_OUTPUT_GATE':[M.output_ports[pnl.DECISION_VARIABLE],
M.output_ports[pnl.RESPONSE_TIME]]}])
assert len(C.control_signals)==1
assert C.control_signals[0].name=='DECISION_CONTROL'
assert len(C.control_signals[0].efferents)==2
assert M.parameter_ports[
psyneulink.core.components.functions.nonstateful.distributionfunctions.DRIFT_RATE].mod_afferents[0] == C.control_signals[0].efferents[0]
assert M.parameter_ports[
psyneulink.core.globals.keywords.THRESHOLD].mod_afferents[0] == C.control_signals[0].efferents[1]
assert len(G.gating_signals)==1
assert G.gating_signals[0].name=='DDM_OUTPUT_GATE'
assert len(G.gating_signals[0].efferents)==2
assert M.output_ports[pnl.DECISION_VARIABLE].mod_afferents[0]==G.gating_signals[0].efferents[0]
assert M.output_ports[pnl.RESPONSE_TIME].mod_afferents[0]==G.gating_signals[0].efferents[1]
@pytest.mark.control
def test_multiple_modulatory_projections_with_mech_and_port_Name_specs(self):
M = pnl.DDM(name='MY DDM')
C = pnl.ControlMechanism(control_signals=[{pnl.MECHANISM: M,
pnl.PARAMETER_PORTS: [
psyneulink.core.components.functions.nonstateful.distributionfunctions.DRIFT_RATE,
psyneulink.core.globals.keywords.THRESHOLD]}])
G = pnl.GatingMechanism(gating_signals=[{pnl.MECHANISM: M,
pnl.OUTPUT_PORTS: [pnl.DECISION_VARIABLE, pnl.RESPONSE_TIME]}])
assert len(C.control_signals)==1
assert len(C.control_signals[0].efferents)==2
assert M.parameter_ports[
psyneulink.core.components.functions.nonstateful.distributionfunctions.DRIFT_RATE].mod_afferents[0] == C.control_signals[0].efferents[0]
assert M.parameter_ports[
psyneulink.core.globals.keywords.THRESHOLD].mod_afferents[0] == C.control_signals[0].efferents[1]
assert len(G.gating_signals)==1
assert len(G.gating_signals[0].efferents)==2
assert M.output_ports[pnl.DECISION_VARIABLE].mod_afferents[0]==G.gating_signals[0].efferents[0]
assert M.output_ports[pnl.RESPONSE_TIME].mod_afferents[0]==G.gating_signals[0].efferents[1]
def test_mapping_projection_with_mech_and_port_Name_specs(self):
R1 = pnl.TransferMechanism(output_ports=['OUTPUT_1', 'OUTPUT_2'])
R2 = pnl.TransferMechanism(default_variable=[[0],[0]],
input_ports=['INPUT_1', 'INPUT_2'])
T = pnl.TransferMechanism(input_ports=[{pnl.MECHANISM: R1,
pnl.OUTPUT_PORTS: ['OUTPUT_1', 'OUTPUT_2']}],
output_ports=[{pnl.MECHANISM:R2,
pnl.INPUT_PORTS: ['INPUT_1', 'INPUT_2']}])
assert len(R1.output_ports)==2
assert len(R2.input_ports)==2
assert len(T.input_ports)==1
for input_port in T.input_ports:
for projection in input_port.path_afferents:
assert projection.sender.owner is R1
assert len(T.output_ports)==1
for output_port in T.output_ports:
for projection in output_port.efferents:
assert projection.receiver.owner is R2
def test_mapping_projection_using_2_item_tuple_with_list_of_port_Names(self):
T1 = pnl.TransferMechanism(name='T1', input_ports=[[0,0],[0,0,0]])
T2 = pnl.TransferMechanism(name='T2',
output_ports=[(['InputPort-0','InputPort-1'], T1)])
assert len(T2.output_ports)==1
assert T2.output_ports[0].efferents[0].receiver.name == 'InputPort-0'
assert T2.output_ports[0].efferents[0].matrix.base.shape == (1,2)
assert T2.output_ports[0].efferents[1].receiver.name == 'InputPort-1'
assert T2.output_ports[0].efferents[1].matrix.base.shape == (1,3)
def test_mapping_projection_using_2_item_tuple_and_3_item_tuples_with_index_specs(self):
T1 = pnl.TransferMechanism(name='T1', input_ports=[[0,0],[0,0,0]])
T2 = pnl.TransferMechanism(name='T2',
input_ports=['a','b','c'],
output_ports=[(['InputPort-0','InputPort-1'], T1),
('InputPort-0', (pnl.OWNER_VALUE, 2), T1),
(['InputPort-0','InputPort-1'], 1, T1)])
assert len(T2.output_ports)==3
assert T2.output_ports[0].efferents[0].receiver.name == 'InputPort-0'
assert T2.output_ports[0].efferents[0].matrix.base.shape == (1,2)
assert T2.output_ports[0].efferents[1].receiver.name == 'InputPort-1'
assert T2.output_ports[0].efferents[1].matrix.base.shape == (1,3)
assert T2.output_ports[1].owner_value_index == 2
assert T2.output_ports[2].owner_value_index == 1
def test_2_item_tuple_from_control_signal_to_parameter_port(self):
D = pnl.DDM(name='D')
# Single name
C = pnl.ControlMechanism(control_signals=[(
psyneulink.core.components.functions.nonstateful.distributionfunctions.DRIFT_RATE, D)])
assert C.control_signals[0].name == 'D[drift_rate] ControlSignal'
assert C.control_signals[0].efferents[0].receiver.name == 'drift_rate'
# List of names
C = pnl.ControlMechanism(control_signals=[([
psyneulink.core.components.functions.nonstateful.distributionfunctions.DRIFT_RATE,
psyneulink.core.globals.keywords.THRESHOLD], D)])
assert C.control_signals[0].name == 'D[drift_rate, threshold] ControlSignal'
assert C.control_signals[0].efferents[0].receiver.name == 'drift_rate'
assert C.control_signals[0].efferents[1].receiver.name == 'threshold'
@pytest.mark.control
def test_2_item_tuple_from_parameter_port_to_control_signals(self):
C = pnl.ControlMechanism(control_signals=['a','b'])
D = pnl.DDM(name='D3',
function=psyneulink.core.components.functions.nonstateful.distributionfunctions.DriftDiffusionAnalytical(drift_rate=(3, C),
threshold=(2,C.control_signals['b']))
)
assert D.parameter_ports[
psyneulink.core.components.functions.nonstateful.distributionfunctions.DRIFT_RATE].mod_afferents[0].sender == C.control_signals[0]
assert D.parameter_ports[
psyneulink.core.globals.keywords.THRESHOLD].mod_afferents[0].sender == C.control_signals[1]
@pytest.mark.control
def test_2_item_tuple_from_gating_signal_to_output_ports(self):
D4 = pnl.DDM(name='D4')
# Single name
G = pnl.GatingMechanism(gating_signals=[(pnl.DECISION_VARIABLE, D4)])
assert G.gating_signals[0].name == 'D4[DECISION_VARIABLE] GatingSignal'
assert G.gating_signals[0].efferents[0].receiver.name == 'DECISION_VARIABLE'
# List of names
G = pnl.GatingMechanism(gating_signals=[([pnl.DECISION_VARIABLE, pnl.RESPONSE_TIME], D4)])
assert G.gating_signals[0].name == 'D4[DECISION_VARIABLE, RESPONSE_TIME] GatingSignal'
assert G.gating_signals[0].efferents[0].receiver.name == 'DECISION_VARIABLE'
assert G.gating_signals[0].efferents[1].receiver.name == 'RESPONSE_TIME'
@pytest.mark.control
def test_2_item_tuple_from_input_and_output_ports_to_gating_signals(self):
G = pnl.GatingMechanism(gating_signals=['a','b'])
T = pnl.TransferMechanism(name='T',
input_ports=[(3,G)],
output_ports=[(2,G.gating_signals['b'])]
)
assert T.input_ports[0].mod_afferents[0].sender==G.gating_signals[0]
assert T.output_ports[0].mod_afferents[0].sender==G.gating_signals[1]
control_spec_list = [
pnl.CONTROL,
pnl.CONTROL_SIGNAL,
pnl.CONTROL_PROJECTION,
pnl.ControlSignal,
pnl.ControlSignal(),
pnl.ControlProjection,
"CP_OBJECT",
pnl.ControlMechanism,
pnl.ControlMechanism(),
pnl.ControlMechanism,
(0.3, pnl.CONTROL),
(0.3, pnl.CONTROL_SIGNAL),
(0.3, pnl.CONTROL_PROJECTION),
(0.3, pnl.ControlSignal),
(0.3, pnl.ControlSignal()),
(0.3, pnl.ControlProjection),
(0.3, "CP_OBJECT"),
(0.3, pnl.ControlMechanism),
(0.3, pnl.ControlMechanism()),
(0.3, pnl.ControlMechanism)
]
@pytest.mark.parametrize(
'noise, gain',
[(noise, gain) for noise, gain in [j for j in zip(control_spec_list, reversed(control_spec_list))]]
)
@pytest.mark.control
def test_formats_for_control_specification_for_mechanism_and_function_params(self, noise, gain):
# This shenanigans is to avoid assigning the same instantiated ControlProjection more than once
if noise == 'CP_OBJECT':
noise = pnl.ControlProjection()
elif isinstance(noise, tuple) and noise[1] == 'CP_OBJECT':
noise = (noise[0], pnl.ControlProjection())
if gain == 'CP_OBJECT':
gain = pnl.ControlProjection()
elif isinstance(gain, tuple) and gain[1] == 'CP_OBJECT':
gain = (gain[0], pnl.ControlProjection())
R = pnl.RecurrentTransferMechanism(
# NOTE: fixed name prevents failures due to registry naming
# for parallel test runs
name='R-CONTROL',
noise=noise,
function=psyneulink.core.components.functions.nonstateful.transferfunctions.Logistic(gain=gain)
)
assert R.parameter_ports[pnl.NOISE].mod_afferents[0].name in \
'ControlProjection for R-CONTROL[noise]'
assert R.parameter_ports[pnl.GAIN].mod_afferents[0].name in \
'ControlProjection for R-CONTROL[gain]'
gating_spec_list = [
pnl.GATE,
pnl.CONTROL,
pnl.GATING_SIGNAL,
pnl.CONTROL_SIGNAL,
pnl.GATING_PROJECTION,
pnl.CONTROL_PROJECTION,
pnl.GatingSignal,
pnl.ControlSignal,
pnl.GatingSignal(),
pnl.ControlSignal(),
pnl.GatingProjection,
"GP_OBJECT",
pnl.GatingMechanism,
pnl.ControlMechanism,
pnl.GatingMechanism(),
pnl.ControlMechanism(),
(0.3, pnl.GATE),
(0.3, pnl.CONTROL),
(0.3, pnl.GATING_SIGNAL),
(0.3, pnl.CONTROL_SIGNAL),
(0.3, pnl.GATING_PROJECTION),
(0.3, pnl.CONTROL_PROJECTION),
(0.3, pnl.GatingSignal),
(0.3, pnl.ControlSignal),
(0.3, pnl.GatingSignal()),
(0.3, pnl.ControlSignal()),
(0.3, pnl.GatingProjection),
(0.3, pnl.ControlProjection),
(0.3, "GP_OBJECT"),
(0.3, pnl.GatingMechanism),
(0.3, pnl.ControlMechanism),
(0.3, pnl.GatingMechanism()),
(0.3, pnl.ControlMechanism())
]
@pytest.mark.parametrize(
'input_port, output_port',
[(inp, outp) for inp, outp in [j for j in zip(gating_spec_list, reversed(gating_spec_list))]]
)
@pytest.mark.control
def test_formats_for_gating_specification_of_input_and_output_ports(self, input_port, output_port):
G_IN, G_OUT = input_port, output_port
# This shenanigans is to avoid assigning the same instantiated ControlProjection more than once
if G_IN == 'GP_OBJECT':
G_IN = pnl.GatingProjection()
elif isinstance(G_IN, tuple) and G_IN[1] == 'GP_OBJECT':
G_IN = (G_IN[0], pnl.GatingProjection())
if G_OUT == 'GP_OBJECT':
G_OUT = pnl.GatingProjection()
elif isinstance(G_OUT, tuple) and G_OUT[1] == 'GP_OBJECT':
G_OUT = (G_OUT[0], pnl.GatingProjection())
if isinstance(G_IN, tuple):
IN_NAME = G_IN[1]
else:
IN_NAME = G_IN
# IN_CONTROL = pnl.CONTROL in repr(IN_NAME).split(".")[-1].upper()
IN_CONTROL = 'CONTROL' in repr(IN_NAME).split(".")[-1].upper()
if isinstance(G_OUT, tuple):
OUT_NAME = G_OUT[1]
else:
OUT_NAME = G_OUT
# OUT_CONTROL = pnl.CONTROL in repr(OUT_NAME).split(".")[-1].upper()
OUT_CONTROL = 'CONTROL' in repr(OUT_NAME).split(".")[-1].upper()
T = pnl.TransferMechanism(
name='T-GATING',
input_ports=[G_IN],
output_ports=[G_OUT]
)
if IN_CONTROL:
assert T.input_ports[0].mod_afferents[0].name in \
'ControlProjection for T-GATING[InputPort-0]'
else:
assert T.input_ports[0].mod_afferents[0].name in \
'GatingProjection for T-GATING[InputPort-0]'
if OUT_CONTROL:
assert T.output_ports[0].mod_afferents[0].name in \
'ControlProjection for T-GATING[OutputPort-0]'
else:
assert T.output_ports[0].mod_afferents[0].name in \
'GatingProjection for T-GATING[OutputPort-0]'
# with pytest.raises(pnl.ProjectionError) as error_text:
# T1 = pnl.ProcessingMechanism(name='T1', input_ports=[pnl.ControlMechanism()])
# assert 'Primary OutputPort of ControlMechanism-0 (ControlSignal-0) ' \
# 'cannot be used as a sender of a Projection to InputPort of T1' in error_text.value.args[0]
#
# with pytest.raises(pnl.ProjectionError) as error_text:
# T2 = pnl.ProcessingMechanism(name='T2', output_ports=[pnl.ControlMechanism()])
# assert 'Primary OutputPort of ControlMechanism-1 (ControlSignal-0) ' \
# 'cannot be used as a sender of a Projection to OutputPort of T2' in error_text.value.args[0]
def test_no_warning_when_matrix_specified(self):
with pytest.warns(None) as w:
c = pnl.Composition()
m0 = pnl.ProcessingMechanism(
default_variable=[0, 0, 0, 0]
)
p0 = pnl.MappingProjection(
matrix=[[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]]
)
m1 = pnl.TransferMechanism(
default_variable=[0, 0, 0, 0]
)
c.add_linear_processing_pathway([m0, p0, m1])
for warn in w:
if r'elementwise comparison failed; returning scalar instead' in warn.message.args[0]:
raise
# KDM: this is a good candidate for pytest.parametrize
def test_masked_mapping_projection(self):
t1 = pnl.TransferMechanism(size=2)
t2 = pnl.TransferMechanism(size=2)
proj = pnl.MaskedMappingProjection(sender=t1,
receiver=t2,
matrix=[[1,2],[3,4]],
mask=[[1,0],[0,1]],
mask_operation=pnl.ADD
)
c = pnl.Composition(pathways=[[t1, proj, t2]])
val = c.execute(inputs={t1:[1,2]})
assert np.allclose(val, [[8, 12]])
t1 = pnl.TransferMechanism(size=2)
t2 = pnl.TransferMechanism(size=2)
proj = pnl.MaskedMappingProjection(sender=t1,
receiver=t2,
matrix=[[1,2],[3,4]],
mask=[[1,0],[0,1]],
mask_operation=pnl.MULTIPLY
)
c = pnl.Composition(pathways=[[t1, proj, t2]])
val = c.execute(inputs={t1:[1,2]})
assert np.allclose(val, [[1, 8]])
t1 = pnl.TransferMechanism(size=2)
t2 = pnl.TransferMechanism(size=2)
proj = pnl.MaskedMappingProjection(sender=t1,
receiver=t2,
mask=[[1,2],[3,4]],
mask_operation=pnl.MULTIPLY
)
c = pnl.Composition(pathways=[[t1, proj, t2]])
val = c.execute(inputs={t1:[1,2]})
assert np.allclose(val, [[1, 8]])
def test_masked_mapping_projection_mask_conficts_with_matrix(self):
with pytest.raises(pnl.MaskedMappingProjectionError) as error_text:
t1 = pnl.TransferMechanism(size=2)
t2 = pnl.TransferMechanism(size=2)
pnl.MaskedMappingProjection(sender=t1,
receiver=t2,
mask=[[1,2,3],[4,5,6]],
mask_operation=pnl.MULTIPLY
)
assert "Shape of the 'mask'" in str(error_text.value)
assert "((2, 3)) must be the same as its 'matrix' ((2, 2))" in str(error_text.value)
# FIX 7/22/15 [JDC] - REPLACE WITH MORE ELABORATE TESTS OF DUPLICATE PROJECTIONS:
# SAME FROM OutputPort; SAME TO InputPort
# TEST ERROR MESSAGES GENERATED BY VARIOUS _check_for_duplicates METHODS
# def test_duplicate_projection_detection_and_warning(self):
#
# with pytest.warns(UserWarning) as record:
# T1 = pnl.TransferMechanism(name='T1')
# T2 = pnl.TransferMechanism(name='T2')
# T3 = pnl.TransferMechanism(name='T3')
# T4 = pnl.TransferMechanism(name='T4')
#
# MP1 = pnl.MappingProjection(sender=T1,receiver=T2,name='MP1')
# MP2 = pnl.MappingProjection(sender=T1,receiver=T2,name='MP2')
# pnl.proc(T1,MP1,T2,T3)
# pnl.proc(T1,MP2,T2,T4)
#
# # hack to find a specific warning (other warnings may be generated by the Process construction)
# correct_message_found = False
# for warning in record:
# if "that already has an identical Projection" in str(warning.message):
# correct_message_found = True
# break
#
# assert len(T2.afferents)==1
# assert correct_message_found
def test_duplicate_projection_creation_error(self):
from psyneulink.core.components.projections.projection import DuplicateProjectionError
with pytest.raises(DuplicateProjectionError) as record:
T1 = pnl.TransferMechanism(name='T1')
T2 = pnl.TransferMechanism(name='T2')
pnl.MappingProjection(sender=T1,receiver=T2,name='MP1')
pnl.MappingProjection(sender=T1,receiver=T2,name='MP2')
assert 'Attempt to assign Projection to InputPort-0 of T2 that already has an identical Projection.' \
in record.value.args[0]