-
Notifications
You must be signed in to change notification settings - Fork 29
/
test_user_defined_func.py
577 lines (526 loc) · 25.6 KB
/
test_user_defined_func.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
573
574
575
576
577
import numpy as np
import pytest
from psyneulink.core.components.functions.transferfunctions import Linear, Logistic
from psyneulink.core.components.functions.userdefinedfunction import UserDefinedFunction
from psyneulink.core.components.mechanisms.processing import ProcessingMechanism
from psyneulink.core.components.mechanisms.processing import TransferMechanism
from psyneulink.core.compositions.composition import Composition
import psyneulink.core.llvm as pnlvm
class TestBinaryOperations:
@pytest.mark.parametrize("param1, param2", [
(1, 2),
(np.ones(2), 2),
(2, np.ones(2)),
(np.ones((2, 2)), 2),
(2, np.ones((2, 2))),
(np.ones(2), np.array([1, 2])),
(np.ones((2, 2)), np.array([[1, 2], [3, 4]])),
], ids=["scalar-scalar", "vec-scalar", "scalar-vec", "mat-scalar", "scalar-mat", "vec-vec", "mat-mat"])
@pytest.mark.benchmark(group="Function UDF")
def test_user_def_func_add(self, param1, param2, func_mode, benchmark):
# default val is same shape as expected output
def myFunction(_, param1, param2):
# we only use param1 and param2 to avoid automatic shape changes of the variable
return param1 + param2
U = UserDefinedFunction(custom_function=myFunction, param1=param1, param2=param2)
if func_mode == 'LLVM':
e = pnlvm.execution.FuncExecution(U).execute
elif func_mode == 'PTX':
e = pnlvm.execution.FuncExecution(U).cuda_execute
else:
e = U
val = benchmark(e, 0)
assert np.allclose(val, param1 + param2)
@pytest.mark.parametrize("param1, param2", [
(1, 2),
(np.ones(2), 2),
(2, np.ones(2)),
(np.ones((2, 2)), 2),
(2, np.ones((2, 2))),
(np.ones(2), np.array([1, 2])),
(np.ones(2), np.array([2.])),
(np.ones((2, 2)), np.array([[1, 2], [3, 4]])),
], ids=["scalar-scalar", "vec-scalar", "scalar-vec", "mat-scalar", "scalar-mat", "vec-vec", "vec-vec-differing", "mat-mat"])
@pytest.mark.benchmark(group="Function UDF")
def test_user_def_func_mul(self, param1, param2, func_mode, benchmark):
# default val is same shape as expected output
def myFunction(_, param1, param2):
# we only use param1 and param2 to avoid automatic shape changes of the variable
return param1 * param2
U = UserDefinedFunction(custom_function=myFunction, param1=param1, param2=param2)
if func_mode == 'LLVM':
e = pnlvm.execution.FuncExecution(U).execute
elif func_mode == 'PTX':
e = pnlvm.execution.FuncExecution(U).cuda_execute
else:
e = U
val = benchmark(e, 0)
assert np.allclose(val, param1 * param2)
@pytest.mark.parametrize("param1, param2", [
(1, 2),
(np.ones(2), 2),
(2, np.ones(2)),
(np.ones((2, 2)), 2),
(2, np.ones((2, 2))),
(np.ones(2), np.array([1, 2])),
(np.ones(2), np.array([2.])),
(np.ones((2, 2)), np.array([[1, 2], [3, 4]])),
], ids=["scalar-scalar", "vec-scalar", "scalar-vec", "mat-scalar", "scalar-mat", "vec-vec", "vec-vec-differing", "mat-mat"])
@pytest.mark.benchmark(group="Function UDF")
def test_user_def_func_div(self, param1, param2, func_mode, benchmark):
# default val is same shape as expected output
def myFunction(_, param1, param2):
# we only use param1 and param2 to avoid automatic shape changes of the variable
return param1 / param2
U = UserDefinedFunction(custom_function=myFunction, param1=param1, param2=param2)
if func_mode == 'LLVM':
e = pnlvm.execution.FuncExecution(U).execute
elif func_mode == 'PTX':
e = pnlvm.execution.FuncExecution(U).cuda_execute
else:
e = U
val = benchmark(e, 0)
assert np.allclose(val, np.divide(param1, param2))
@pytest.mark.parametrize("op", [ # parameter is string since compiled udf doesn't support closures as of present
"AND",
"OR",
])
@pytest.mark.benchmark(group="Function UDF")
def test_user_def_func_boolop(self, op, func_mode, benchmark):
if op == "AND":
def myFunction(variable):
var1 = True
var2 = False
# compiled UDFs don't support python bool type outputs
if var1 and var2:
return 0.0
else:
return 1.0
elif op == "OR":
def myFunction(variable):
var1 = True
var2 = False
# compiled UDFs don't support python bool type outputs
if var1 or var2:
return 1.0
else:
return 0.0
U = UserDefinedFunction(custom_function=myFunction, default_variable=[0])
if func_mode == 'LLVM':
e = pnlvm.execution.FuncExecution(U).execute
elif func_mode == 'PTX':
e = pnlvm.execution.FuncExecution(U).cuda_execute
else:
e = U
val = benchmark(e, [0])
assert val == 1.0
@pytest.mark.parametrize("op,var1,var2,expected", [ # parameter is string since compiled udf doesn't support closures as of present
("Eq", 1.0, 2.0, 0.0),
("NotEq", 1.0, 2.0, 1.0),
("Lt", 1.0, 2.0, 1.0),
("LtE", 1.0, 2.0, 1.0),
("Gt", 1.0, 2.0, 0.0),
("GtE", 1.0, 2.0, 0.0),
])
@pytest.mark.benchmark(group="Function UDF")
def test_user_def_func_cmpop(self, op, var1, var2, expected, func_mode, benchmark):
# we explicitly use np here to ensure that the result is castable to float in the scalar-scalar case
if op == "Eq":
def myFunction(variable, var1, var2):
if var1 == var2:
return 1.0
else:
return 0.0
elif op == "NotEq":
def myFunction(variable, var1, var2):
if var1 != var2:
return 1.0
else:
return 0.0
elif op == "Lt":
def myFunction(variable, var1, var2):
if var1 < var2:
return 1.0
else:
return 0.0
elif op == "LtE":
def myFunction(variable, var1, var2):
if var1 <= var2:
return 1.0
else:
return 0.0
elif op == "Gt":
def myFunction(variable, var1, var2):
if var1 > var2:
return 1.0
else:
return 0.0
elif op == "GtE":
def myFunction(variable, var1, var2):
if var1 >= var2:
return 1.0
else:
return 0.0
U = UserDefinedFunction(custom_function=myFunction, default_variable=[0], var1=var1, var2=var2)
if func_mode == 'LLVM':
e = pnlvm.execution.FuncExecution(U).execute
elif func_mode == 'PTX':
e = pnlvm.execution.FuncExecution(U).cuda_execute
else:
e = U
val = benchmark(e, [0])
assert np.allclose(expected, val)
@pytest.mark.parametrize("op,var1,var2,expected", [ # parameter is string since compiled udf doesn't support closures as of present
("Eq", 1.0, 2.0, 0.0),
("Eq", [1.0, 2.0], [1.0, 2.0], [1.0, 1.0]),
("Eq", 1.0, [1.0, 2.0], [1.0, 0.0]),
("Eq", [2.0, 1.0], 1.0, [0.0, 1.0]),
("Eq", [[1.0, 2.0], [3.0, 4.0]], 1.0, [[1.0, 0.0], [0.0, 0.0]]),
("Eq", 1.0, [[1.0, 2.0], [3.0, 4.0]], [[1.0, 0.0], [0.0, 0.0]]),
("Eq", [[1.0, 2.0], [3.0, 4.0]], [[1.0, 2.0], [3.0, 4.0]], [[1.0, 1.0], [1.0, 1.0]]),
("NotEq", 1.0, 2.0, 1.0),
("NotEq", [1.0, 2.0], [1.0, 2.0], [0.0, 0.0]),
("NotEq", 1.0, [1.0, 2.0], [0.0, 1.0]),
("NotEq", [2.0, 1.0], 1.0, [1.0, 0.0]),
("NotEq", [[1.0, 2.0], [3.0, 4.0]], 1.0, [[0.0, 1.0], [1.0, 1.0]]),
("NotEq", 1.0, [[1.0, 2.0], [3.0, 4.0]], [[0.0, 1.0], [1.0, 1.0]]),
("NotEq", [[1.0, 2.0], [3.0, 4.0]], [[1.0, 2.0], [3.0, 4.0]], [[0.0, 0.0], [0.0, 0.0]]),
("Lt", 1.0, 2.0, 1.0),
("Lt", [1.0, 2.0], [1.0, 2.0], [0.0, 0.0]),
("Lt", 1.0, [1.0, 2.0], [0.0, 1.0]),
("Lt", [2.0, 1.0], 1.0, [0.0, 0.0]),
("Lt", [[1.0, 2.0], [3.0, 4.0]], 1.0, [[0.0, 0.0], [0.0, 0.0]]),
("Lt", 1.0, [[1.0, 2.0], [3.0, 4.0]], [[0.0, 1.0], [1.0, 1.0]]),
("Lt", [[1.0, 2.0], [3.0, 4.0]], [[1.0, 2.0], [3.0, 4.0]], [[0.0, 0.0], [0.0, 0.0]]),
("LtE", 1.0, 2.0, 1.0),
("LtE", [1.0, 2.0], [1.0, 2.0], [1.0, 1.0]),
("LtE", 1.0, [1.0, 2.0], [1.0, 1.0]),
("LtE", [2.0, 1.0], 1.0, [0.0, 1.0]),
("LtE", [[1.0, 2.0], [3.0, 4.0]], 1.0, [[1.0, 0.0], [0.0, 0.0]]),
("LtE", 1.0, [[1.0, 2.0], [3.0, 4.0]], [[1.0, 1.0], [1.0, 1.0]]),
("LtE", [[1.0, 2.0], [3.0, 4.0]], [[1.0, 2.0], [3.0, 4.0]], [[1.0, 1.0], [1.0, 1.0]]),
("Gt", 1.0, 2.0, 0.0),
("Gt", [1.0, 2.0], [1.0, 2.0], [0.0, 0.0]),
("Gt", 1.0, [1.0, 2.0], [0.0, 0.0]),
("Gt", [2.0, 1.0], 1.0, [1.0, 0.0]),
("Gt", [[1.0, 2.0], [3.0, 4.0]], 1.0, [[0.0, 1.0], [1.0, 1.0]]),
("Gt", 1.0, [[1.0, 2.0], [3.0, 4.0]], [[0.0, 0.0], [0.0, 0.0]]),
("Gt", [[1.0, 2.0], [3.0, 4.0]], [[1.0, 2.0], [3.0, 4.0]], [[0.0, 0.0], [0.0, 0.0]]),
("GtE", 1.0, 2.0, 0.0),
("GtE", [1.0, 2.0], [1.0, 2.0], [1.0, 1.0]),
("GtE", 1.0, [1.0, 2.0], [1.0, 0.0]),
("GtE", [2.0, 1.0], 1.0, [1.0, 1.0]),
("GtE", [[1.0, 2.0], [3.0, 4.0]], 1.0, [[1.0, 1.0], [1.0, 1.0]]),
("GtE", 1.0, [[1.0, 2.0], [3.0, 4.0]], [[1.0, 0.0], [0.0, 0.0]]),
("GtE", [[1.0, 2.0], [3.0, 4.0]], [[1.0, 2.0], [3.0, 4.0]], [[1.0, 1.0], [1.0, 1.0]]),
])
@pytest.mark.benchmark(group="Function UDF")
def test_user_def_func_cmpop_numpy(self, op, var1, var2, expected, func_mode, benchmark):
# we explicitly use np here to ensure that the result is castable to float in the scalar-scalar case
if op == "Eq":
def myFunction(variable, var1, var2):
return np.equal(var1, var2).astype(float)
elif op == "NotEq":
def myFunction(variable, var1, var2):
return np.not_equal(var1, var2).astype(float)
elif op == "Lt":
def myFunction(variable, var1, var2):
return np.less(var1, var2).astype(float)
elif op == "LtE":
def myFunction(variable, var1, var2):
return np.less_equal(var1, var2).astype(float)
elif op == "Gt":
def myFunction(variable, var1, var2):
return np.greater(var1, var2).astype(float)
elif op == "GtE":
def myFunction(variable, var1, var2):
return np.greater_equal(var1, var2).astype(float)
U = UserDefinedFunction(custom_function=myFunction, default_variable=[0], var1=var1, var2=var2)
if func_mode == 'LLVM':
e = pnlvm.execution.FuncExecution(U).execute
elif func_mode == 'PTX':
e = pnlvm.execution.FuncExecution(U).cuda_execute
else:
e = U
val = benchmark(e, [0])
assert np.allclose(expected, val)
class TestUserDefFunc:
@pytest.mark.benchmark(group="Function UDF")
def test_user_def_func(self, func_mode, benchmark):
def myFunction(variable, param1, param2):
return variable * 2 + param2
U = UserDefinedFunction(custom_function=myFunction, default_variable=[[0, 0]], param2=3)
if func_mode == 'LLVM':
e = pnlvm.execution.FuncExecution(U).execute
elif func_mode == 'PTX':
e = pnlvm.execution.FuncExecution(U).cuda_execute
else:
e = U
val = benchmark(e, [1, 3])
assert np.allclose(val, [[5, 9]])
@pytest.mark.benchmark(group="Function UDF")
def test_user_def_func_branching(self, func_mode, benchmark):
def myFunction(variable, param1, param2):
if variable[0][0] > 0 and variable[0][1] > 0:
return variable * 2 + param2
else:
return variable * -2 + param2
U = UserDefinedFunction(custom_function=myFunction, default_variable=[[0, 0]], param2=3)
if func_mode == 'LLVM':
e = pnlvm.execution.FuncExecution(U).execute
elif func_mode == 'PTX':
e = pnlvm.execution.FuncExecution(U).cuda_execute
else:
e = U
val = benchmark(e, [[1, 3]])
assert np.allclose(val, [[5, 9]])
val2 = e([[-1, 3]])
assert np.allclose(val2, [[5, -3]])
@pytest.mark.benchmark(group="Function UDF")
def test_user_def_func_variable_index(self, func_mode, benchmark):
def myFunction(variable):
variable[0][0] = variable[0][0] + 5
variable[0][1] = variable[0][1] + 7
return variable
U = UserDefinedFunction(custom_function=myFunction, default_variable=[[0, 0]])
if func_mode == 'LLVM':
e = pnlvm.execution.FuncExecution(U).execute
elif func_mode == 'PTX':
e = pnlvm.execution.FuncExecution(U).cuda_execute
else:
e = U
val = benchmark(e, [[1, 3]])
assert np.allclose(val, [[6, 10]])
@pytest.mark.parametrize("variable", [
(1),
(np.ones((2))),
(np.ones((2, 2)))
], ids=["scalar", "vec-2d", "mat"])
@pytest.mark.benchmark(group="Function UDF")
def test_user_def_func_usub(self, variable, func_mode, benchmark):
def myFunction(variable, param):
return -param
U = UserDefinedFunction(custom_function=myFunction, default_variable=variable, param=variable)
if func_mode == 'LLVM':
e = pnlvm.execution.FuncExecution(U).execute
elif func_mode == 'PTX':
e = pnlvm.execution.FuncExecution(U).cuda_execute
else:
e = U
val = benchmark(e, variable)
assert np.allclose(val, -variable)
@pytest.mark.benchmark(group="Function UDF")
def test_user_def_reward_func(self, func_mode, benchmark):
variable = [[1,2,3,4]]
def myFunction(x,t0=0.48):
return (x[0][0]>0).astype(float) * (x[0][2]>0).astype(float) / (np.max([x[0][1],x[0][3]]) + t0)
U = UserDefinedFunction(custom_function=myFunction, default_variable=variable, param=variable)
if func_mode == 'LLVM':
e = pnlvm.execution.FuncExecution(U).execute
elif func_mode == 'PTX':
e = pnlvm.execution.FuncExecution(U).cuda_execute
else:
e = U
val = benchmark(e, variable)
assert np.allclose(val, 0.2232142857142857)
@pytest.mark.parametrize("dtype, expected", [ # parameter is string since compiled udf doesn't support closures as of present
("SCALAR", 1.0),
("VECTOR", [1,2]),
("MATRIX", [[1,2],[3,4]]),
("BOOL", 1.0),
("TUPLE", (1, 2, 3, 4))
])
@pytest.mark.benchmark(group="Function UDF")
def test_user_def_func_assign(self, dtype, expected, func_mode, benchmark):
if dtype == "SCALAR":
def myFunction(variable):
var = 1.0
return var
elif dtype == "VECTOR":
def myFunction(variable):
var = [1,2]
return var
elif dtype == "MATRIX":
def myFunction(variable):
var = [[1,2],[3,4]]
return var
elif dtype == "BOOL":
def myFunction(variable):
var = True
return 1.0
elif dtype == "TUPLE":
def myFunction(variable):
var = (1, 2, 3, 4)
return var
U = UserDefinedFunction(custom_function=myFunction, default_variable=0)
if func_mode == 'LLVM':
e = pnlvm.execution.FuncExecution(U).execute
elif func_mode == 'PTX':
e = pnlvm.execution.FuncExecution(U).cuda_execute
else:
e = U
val = benchmark(e, 0)
assert np.allclose(val, expected)
@pytest.mark.parametrize("op,variable,expected", [ # parameter is string since compiled udf doesn't support closures as of present
("TANH", [[1, 3]], [0.76159416, 0.99505475]),
("EXP", [[1, 3]], [2.71828183, 20.08553692]),
("SHAPE", [1, 2], [2]),
("SHAPE", [[1, 3]], [1, 2]),
("ASTYPE_FLOAT", [1], [1.0]),
("ASTYPE_INT", [-1.5], [-1.0]),
("NP_MAX", [0.0, 0.0], 0),
("NP_MAX", [1.0, 2.0], 2),
("NP_MAX", [[2.0, 1.0], [6.0, 2.0]], 6),
("FLATTEN", [[1.0, 2.0], [3.0, 4.0]], [1.0, 2.0, 3.0, 4.0])
])
@pytest.mark.benchmark(group="Function UDF")
def test_user_def_func_numpy(self, op, variable, expected, func_mode, benchmark):
if op == "TANH":
def myFunction(variable):
return np.tanh(variable)
elif op == "EXP":
def myFunction(variable):
return np.exp(variable)
elif op == "SHAPE":
def myFunction(variable):
return variable.shape
elif op == "ASTYPE_FLOAT":
def myFunction(variable):
return variable.astype(float)
elif op == "ASTYPE_INT":
# return types cannot be integers, so we cast back to float and check for truncation
def myFunction(variable):
return variable.astype(int).astype(float)
elif op == "NP_MAX":
def myFunction(variable):
return np.max(variable)
elif op == "FLATTEN":
def myFunction(variable):
return variable.flatten()
U = UserDefinedFunction(custom_function=myFunction, default_variable=variable)
if func_mode == 'LLVM':
e = pnlvm.execution.FuncExecution(U).execute
elif func_mode == 'PTX':
e = pnlvm.execution.FuncExecution(U).cuda_execute
else:
e = U
val = benchmark(e, variable)
assert np.allclose(val, expected)
@pytest.mark.benchmark(group="UDF in Mechanism")
def test_udf_in_mechanism(self, mech_mode, benchmark):
def myFunction(variable, param1, param2):
return sum(variable[0]) + 2
myMech = ProcessingMechanism(function=myFunction, size=4, name='myMech')
# assert 'param1' in myMech.parameter_ports.names # <- FIX reinstate when problem with function params is fixed
# assert 'param2' in myMech.parameter_ports.names # <- FIX reinstate when problem with function params is fixed
if mech_mode == 'LLVM':
e = pnlvm.execution.MechExecution(myMech).execute
elif mech_mode == 'PTX':
e = pnlvm.execution.MechExecution(myMech).cuda_execute
else:
e = myMech.execute
val = benchmark(e, [-1, 2, 3, 4])
assert np.allclose(val, [[10]])
@pytest.mark.parametrize("op,variable,expected", [ # parameter is string since compiled udf doesn't support closures as of present
("SUM", [1.0, 3.0], 4),
("SUM", [[1.0], [3.0]], [4.0]),
("LEN", [1.0, 3.0], 2),
("LEN", [[1.0], [3.0]], 2),
("LEN_TUPLE", [0, 0], 2),
("MAX_MULTI", [1,], 6),
("MAX", [1.0, 3.0, 2.0], 3.0),
])
@pytest.mark.benchmark(group="Function UDF")
def test_user_def_func_builtin(self, op, variable, expected, func_mode, benchmark):
if op == "SUM":
def myFunction(variable):
return sum(variable)
elif op == "LEN":
def myFunction(variable):
return len(variable)
elif op == "LEN_TUPLE":
def myFunction(variable):
return len((1,2))
elif op == "MAX":
def myFunction(variable):
return max(variable)
elif op == "MAX_MULTI":
# special cased, since passing in multiple variables without a closure is hard
def myFunction(_):
return max(1, 2, 3, 4, 5, 6, -1, -2)
U = UserDefinedFunction(custom_function=myFunction, default_variable=variable)
if func_mode == 'LLVM':
e = pnlvm.execution.FuncExecution(U).execute
elif func_mode == 'PTX':
e = pnlvm.execution.FuncExecution(U).cuda_execute
else:
e = U
val = benchmark(e, variable)
assert np.allclose(val, expected)
@pytest.mark.benchmark(group="UDF as Composition Origin")
def test_udf_composition_origin(self, comp_mode, benchmark):
def myFunction(variable, context):
return [variable[0][1], variable[0][0]]
myMech = ProcessingMechanism(function=myFunction, size=3, name='myMech')
T = TransferMechanism(size=2, function=Linear)
c = Composition(pathways=[myMech, T])
benchmark(c.run, inputs={myMech: [[1, 3, 5]]}, execution_mode=comp_mode)
assert np.allclose(c.results[0][0], [3, 1])
@pytest.mark.benchmark(group="UDF as Composition Terminal")
def test_udf_composition_terminal(self, comp_mode, benchmark):
def myFunction(variable, context):
return [variable[0][2], variable[0][0]]
myMech = ProcessingMechanism(function=myFunction, size=3, name='myMech')
T2 = TransferMechanism(size=3, function=Linear)
c2 = Composition(pathways=[[T2, myMech]])
benchmark(c2.run, inputs={T2: [[1, 2, 3]]}, execution_mode=comp_mode)
assert(np.allclose(c2.results[0][0], [3, 1]))
def test_udf_with_pnl_func(self):
L = Logistic(gain=2)
def myFunction(variable, context):
return L(variable) + 2
U = UserDefinedFunction(custom_function=myFunction, default_variable=[[0, 0, 0]])
myMech = ProcessingMechanism(function=myFunction, size=3, name='myMech')
val1 = myMech.execute(input=[1, 2, 3])
val2 = U.execute(variable=[[1, 2, 3]])
assert np.allclose(val1, val2)
assert np.allclose(val1, L([1, 2, 3]) + 2)
def test_udf_creates_parameter_ports(self):
def func(input=[[0], [0]], p=0, q=1):
return (p + q) * input
m = ProcessingMechanism(
default_variable=[[0], [0]],
function=UserDefinedFunction(func)
)
assert len(m.parameter_ports) == 2
assert 'p' in m.parameter_ports.names
assert 'q' in m.parameter_ports.names
@pytest.fixture
def mech_with_autogenerated_udf(self):
def func(input=[[0], [0]], p=0, q=1):
return (p + q) * input
m = ProcessingMechanism(
default_variable=[[0], [0]],
function=func
)
return m
def test_mech_autogenerated_udf_execute(self, mech_with_autogenerated_udf):
# Test if execute is working with auto-defined udf's
val1 = mech_with_autogenerated_udf.execute(input=[[1], [1]])
assert np.allclose(val1, np.array([[1], [1]]))
def test_autogenerated_udf(self, mech_with_autogenerated_udf):
assert isinstance(mech_with_autogenerated_udf.function, UserDefinedFunction)
def test_autogenerated_udf_creates_parameter_ports(self, mech_with_autogenerated_udf):
assert len(mech_with_autogenerated_udf.parameter_ports) == 2
assert 'p' in mech_with_autogenerated_udf.parameter_ports.names
assert 'q' in mech_with_autogenerated_udf.parameter_ports.names
def test_autogenerated_udf_creates_parameters(self, mech_with_autogenerated_udf):
assert hasattr(mech_with_autogenerated_udf.function.parameters, 'p')
assert hasattr(mech_with_autogenerated_udf.function.parameters, 'q')
assert mech_with_autogenerated_udf.function.parameters.p.default_value == 0
assert mech_with_autogenerated_udf.function.parameters.q.default_value == 1
assert not hasattr(mech_with_autogenerated_udf.function.class_parameters, 'p')
assert not hasattr(mech_with_autogenerated_udf.function.class_parameters, 'q')
def test_autogenerated_udf_parameters_states_have_source(self, mech_with_autogenerated_udf):
for p in mech_with_autogenerated_udf.parameter_ports:
assert p.source is getattr(mech_with_autogenerated_udf.function.parameters, p.name)