def test_integrator_function_with_default_variable_and_params_of_different_lengths(): with pytest.raises(FunctionError) as error_text: AdaptiveIntegrator(default_variable=[0,0,0], rate=[.1, .2, .3], offset=[.4,.5]) error_msg_a = "The following parameters with len>1 specified for AdaptiveIntegrator Function" error_msg_b = "don't have the same length as its 'default_variable' (3): ['offset']." assert error_msg_a in str(error_text.value) assert error_msg_b in str(error_text.value)
def test_integrator_function_with_params_of_different_lengths(): with pytest.raises(FunctionError) as error_text: AdaptiveIntegrator(rate=[.1, .2, .3], offset=[.4,.5]) error_msg_a = "The parameters with len>1 specified for AdaptiveIntegrator Function" error_msg_b = "(['rate', 'offset']) don't all have the same length" assert error_msg_a in str(error_text.value) assert error_msg_b in str(error_text.value)
def test_integrator_function_default_variable_and_params_len_more_than_1_error(): with pytest.raises(FunctionError) as error_text: AdaptiveIntegrator(default_variable=[0,0], rate=[.1, .2, .3]) error_msg_a = 'The length (3) of the array specified for the rate parameter' error_msg_b = 'must match the length (2) of the default input ([0 0])' assert error_msg_a in str(error_text.value) assert error_msg_b in str(error_text.value)
def test_nested_composition_run_trials_inputs(benchmark, executions, mode): benchmark.group = "Nested Composition mutliple trials/inputs multirun {}".format( executions) # mechanisms A = ProcessingMechanism(name="A", function=AdaptiveIntegrator(rate=0.1)) B = ProcessingMechanism(name="B", function=Logistic) inner_comp = Composition(name="inner_comp") inner_comp.add_linear_processing_pathway([A, B]) inner_comp._analyze_graph() sched = Scheduler(composition=inner_comp) outer_comp = Composition(name="outer_comp") outer_comp.add_node(inner_comp) outer_comp._analyze_graph() sched = Scheduler(composition=outer_comp) # The input dict should assign inputs origin nodes (inner_comp in this case) var = {inner_comp: [[[2.0]], [[3.0]]]} expected = [[[0.549833997312478]], [[0.617747874769249]], [[0.6529428177055896]], [[0.7044959416252289]]] if executions > 1: var = [var for _ in range(executions)] if mode == 'Python': def f(v, num_trials, res=False): results = [] for i in range(executions): outer_comp.run(v[i], execution_id=i, num_trials=num_trials) if res: # copy the results immediately, otherwise it's empty results.append(outer_comp.results.copy()) return results res = f(var, 4, True) if executions > 1 else f([var], 4, True) benchmark(f if executions > 1 else outer_comp.run, var, num_trials=4) elif mode == 'LLVM': e = pnlvm.execution.CompExecution(outer_comp, [None for _ in range(executions)]) res = e.run(var, 4, 2) benchmark(e.run, var, 4, 2) elif mode == 'PTX': e = pnlvm.execution.CompExecution(outer_comp, [None for _ in range(executions)]) res = e.cuda_run(var, 4, 2) benchmark(e.cuda_run, var, 4, 2) assert np.allclose(res, [expected for _ in range(executions)]) assert len(res) == executions or executions == 1
class Parameters(ProcessingMechanism_Base.Parameters): """ Attributes ---------- function see `function <IntegratorMechanism.function>` :default value: `AdaptiveIntegrator`(initializer=numpy.array([0]), rate=0.5) :type: `Function` """ function = Parameter(AdaptiveIntegrator(rate=0.5), stateful=False, loggable=False)
def test_nested_composition_execution(benchmark, executions, mode): benchmark.group = "Nested Composition execution multirun {}".format( executions) # mechanisms A = ProcessingMechanism(name="A", function=AdaptiveIntegrator(rate=0.1)) B = ProcessingMechanism(name="B", function=Logistic) inner_comp = Composition(name="inner_comp") inner_comp.add_linear_processing_pathway([A, B]) inner_comp._analyze_graph() sched = Scheduler(composition=inner_comp) outer_comp = Composition(name="outer_comp") outer_comp.add_node(inner_comp) outer_comp._analyze_graph() sched = Scheduler(composition=outer_comp) # The input dict should assign inputs origin nodes (inner_comp in this case) var = {inner_comp: [[1.0]]} expected = [[0.52497918747894]] if executions > 1: var = [var for _ in range(executions)] if mode == 'Python': f = lambda x: [ outer_comp.execute(x[i], execution_id=i) for i in range(executions) ] res = f(var) if executions > 1 else outer_comp.execute(var) benchmark(f if executions > 1 else outer_comp.execute, var) elif mode == 'LLVM': e = pnlvm.execution.CompExecution(outer_comp, [None for _ in range(executions)]) e.execute(var) res = e.extract_node_output(outer_comp.output_CIM) benchmark(e.execute, var) elif mode == 'PTX': e = pnlvm.execution.CompExecution(outer_comp, [None for _ in range(executions)]) e.cuda_execute(var) res = e.extract_node_output(outer_comp.output_CIM) benchmark(e.cuda_execute, var) assert np.allclose(res, [expected for _ in range(executions)]) assert len(res) == executions
def test_integrator_function_no_default_variable_and_params_len_more_than_1(): I = AdaptiveIntegrator(rate=[.1, .2, .3]) I.defaults.variable = np.array([0,0,0])