def test_LCAMechanism_threshold_with_max_vs_next(self): lca = LCAMechanism(size=3, threshold=0.1, threshold_criterion=MAX_VS_NEXT) comp = Composition() comp.add_node(lca) result = comp.run(inputs={lca: [1, 0.5, 0]}) assert np.allclose(result, [[0.52200799, 0.41310248, 0.31228985]])
def test_DDMMechanism_LCA_equivalent(comp_mode): ddm = DDM(default_variable=[0], function=DriftDiffusionIntegrator(rate=1, time_step_size=0.1)) comp2 = Composition() comp2.add_node(ddm) result2 = comp2.run(inputs={ddm: [1]}, execution_mode=comp_mode) assert np.allclose(np.asfarray(result2[0]), [0.1]) assert np.allclose(np.asfarray(result2[1]), [0.1])
def test_LCAMechanism_threshold_with_max_vs_avg(self): lca = LCAMechanism(size=3, threshold=0.1, threshold_criterion=MAX_VS_AVG) comp = Composition() comp.add_node(lca) result = comp.run(inputs={lca: [1, 0.5, 0]}) assert np.allclose(result, [[0.5100369, 0.43776452, 0.36808511]])
def test_LCAMechanism_threshold(self, benchmark, comp_mode): lca = LCAMechanism(size=2, leak=0.5, threshold=0.7) comp = Composition() comp.add_node(lca) result = comp.run(inputs={lca: [1, 0]}, execution_mode=comp_mode) assert np.allclose(result, [0.70005431, 0.29994569]) if benchmark.enabled: benchmark(comp.run, inputs={lca: [1, 0]}, execution_mode=comp_mode)
def test_five_ABABCDE(self): A = TransferMechanism( name='A', default_variable=[0], function=Linear(slope=2.0), ) B = TransferMechanism( name='B', default_variable=[0], function=Linear(slope=2.0), ) C = IntegratorMechanism(name='C', default_variable=[0], function=SimpleIntegrator(rate=.5)) D = TransferMechanism( name='D', default_variable=[0], function=Linear(slope=1.0), ) E = TransferMechanism( name='E', default_variable=[0], function=Linear(slope=2.0), ) c = Composition(pathways=[[A, C, D], [B, C, E]]) term_conds = {TimeScale.TRIAL: AfterNCalls(E, 1)} stim_list = {A: [[1]], B: [[2]]} sched = Scheduler(composition=c) sched.add_condition(C, Any(EveryNCalls(A, 1), EveryNCalls(B, 1))) sched.add_condition(D, EveryNCalls(C, 1)) sched.add_condition(E, EveryNCalls(C, 1)) c.scheduler = sched c.run(inputs=stim_list, termination_processing=term_conds) terminal_mechs = [D, E] expected_output = [ [ numpy.array([3.]), ], [ numpy.array([6.]), ], ] for m in range(len(terminal_mechs)): for i in range(len(expected_output[m])): numpy.testing.assert_allclose( expected_output[m][i], terminal_mechs[m].get_output_values(c)[i])
def test_LCAMechanism_threshold_with_max_vs_next(self): lca = LCAMechanism(size=3, leak=0.5, threshold=0.1, threshold_criterion=MAX_VS_NEXT) comp = Composition() comp.add_node(lca) result = comp.run(inputs={lca: [1, 0.5, 0]}) assert np.allclose(result, [[0.52490032, 0.42367594, 0.32874867]])
def test_kwta_threshold_int(self): K = KWTAMechanism(name='K', size=4, threshold=-1) c = Composition(pathways=[K], prefs=TestKWTARatio.simple_prefs) c.run(inputs={K: [1, 2, 3, 4]}) assert np.allclose(K.parameters.value.get(c), [[ 0.07585818002124355, 0.18242552380635635, 0.3775406687981454, 0.6224593312018546 ]])
def test_LCAMechanism_threshold_with_convergence(self): lca = LCAMechanism(size=3, threshold=0.01, threshold_criterion=CONVERGENCE) comp = Composition() comp.add_node(lca) result = comp.run(inputs={lca: [0, 1, 2]}) assert np.allclose(result, [[0.02377001, 0.5, 0.97622999]]) assert lca.num_executions_before_finished == 19
def test_kwta_threshold_float(self): K = KWTAMechanism(name='K', size=4, threshold=0.5) c = Composition(pathways=[K], prefs=TestKWTARatio.simple_prefs) c.run(inputs={K: [1, 2, 3, 3]}) assert np.allclose( K.parameters.value.get(c), [[0.2689414213699951, 0.5, 0.7310585786300049, 0.7310585786300049] ])
def test_LCAMechanism_threshold_with_max_vs_avg(self): lca = LCAMechanism(size=3, leak=0.5, threshold=0.1, threshold_criterion=MAX_VS_AVG) comp = Composition() comp.add_node(lca) result = comp.run(inputs={lca: [1, 0.5, 0]}) assert np.allclose(result, [[0.51180475, 0.44161738, 0.37374946]])
def test_LCAMechanism_threshold_with_convergence(self, benchmark, mode): lca = LCAMechanism(size=3, leak=0.5, threshold=0.01, threshold_criterion=CONVERGENCE) comp = Composition() comp.add_node(lca) result = comp.run(inputs={lca:[0,1,2]}, bin_execute=mode) assert np.allclose(result, [[0.19153799, 0.5, 0.80846201]]) if mode == 'Python': assert lca.num_executions_before_finished == 18 if benchmark.enabled: benchmark(comp.run, inputs={lca:[0,1,2]}, bin_execute=mode)
def test_is_finished_stops_composition(self): D = DDM(name='DDM', function=DriftDiffusionIntegrator(threshold=10.0)) C = Composition(pathways=[D], reset_stateful_function_when=Never()) C.run(inputs={D: 2.0}, termination_processing={TimeScale.TRIAL: WhenFinished(D)}) # decision variable's value should match threshold assert D.parameters.value.get(C)[0] == 10.0 # it should have taken 5 executions (and time_step_size = 1.0) assert D.parameters.value.get(C)[1] == 5.0
def test_ddm_is_finished(mode, noise, threshold, expected_results): comp = Composition() ddm = DDM(execute_until_finished=True, function=DriftDiffusionIntegrator(threshold=threshold, noise=noise)) comp.add_node(ddm) results = comp.run([0], bin_execute=mode) results = [x for x in np.array(results).flatten()] #HACK: The result is an object dtype in Python mode for some reason? assert np.allclose(results, np.array(expected_results).flatten())
def test_kwta_k_value_empty_size_6(self): K = KWTAMechanism(name='K', size=6) assert K.k_value.base == 0.5 c = Composition(pathways=[K], prefs=TestKWTARatio.simple_prefs) c.run(inputs={K: [1, 2, 2, 3, 3, 4]}) assert np.allclose(K.parameters.value.get(c), [[ 0.18242552380635635, 0.3775406687981454, 0.3775406687981454, 0.6224593312018546, 0.6224593312018546, 0.8175744761936437 ]])
def test_one_composition_two_contexts(self): comp = Composition() A = TransferMechanism(function=Linear(slope=5.0, intercept=2.0), name='scheduler-pytests-A') comp.add_node(A) sched = Scheduler(composition=comp) sched.add_condition(A, BeforeNCalls(A, 5, time_scale=TimeScale.LIFE)) termination_conds = {} termination_conds[TimeScale.RUN] = AfterNTrials(6) termination_conds[TimeScale.TRIAL] = AfterNPasses(1) eid = uuid.uuid4() comp.run( inputs={A: [[0], [1], [2], [3], [4], [5]]}, scheduler_processing=sched, termination_processing=termination_conds, execution_id=eid, ) output = sched.execution_list[eid] expected_output = [ A, A, A, A, A, set() ] # pprint.pprint(output) assert output == pytest.helpers.setify_expected_output(expected_output) comp.run( inputs={A: [[0], [1], [2], [3], [4], [5]]}, scheduler_processing=sched, termination_processing=termination_conds, execution_id=eid, ) output = sched.execution_list[eid] expected_output = [ A, A, A, A, A, set(), set(), set(), set(), set(), set(), set() ] # pprint.pprint(output) assert output == pytest.helpers.setify_expected_output(expected_output) eid2 = uuid.uuid4() comp.run( inputs={A: [[0], [1], [2], [3], [4], [5]]}, scheduler_processing=sched, termination_processing=termination_conds, execution_id=eid2, ) output = sched.execution_list[eid2] expected_output = [ A, A, A, A, A, set() ] # pprint.pprint(output) assert output == pytest.helpers.setify_expected_output(expected_output)
def test_two_input_ports_two_output_ports(self): comp = Composition() A = TransferMechanism(name="A", default_variable=[[0.0], [0.0]], function=Linear(slope=2.0)) B = TransferMechanism(name="B", default_variable=[[0.0], [0.0]], function=Linear(slope=3.0)) comp.add_node(A) comp.add_node(B) comp.add_projection(MappingProjection(sender=A, receiver=B), A, B) comp.add_projection( MappingProjection(sender=A.output_ports[1], receiver=B.input_ports[1]), A, B) inputs_dict = { A: [[5.], [6.]], } sched = Scheduler(composition=comp) output = comp.run(inputs=inputs_dict, scheduler=sched) assert np.allclose([[30.], [36.]], output)
def test_kwta_average_k_1(self): K = KWTAMechanism(name='K', size=4, k_value=1, threshold=0, function=Linear, average_based=True) c = Composition(pathways=[K], prefs=TestKWTARatio.simple_prefs) kwta_input = {K: [[1, 2, 3, 4]]} c.run(inputs=kwta_input) assert np.allclose(K.parameters.value.get(c), [[-2, -1, 0, 1]])
def test_sequence_of_DDM_mechs_in_Composition_Pathway(): myMechanism = DDM( function=DriftDiffusionAnalytical( drift_rate=(1.0), threshold=(10.0), starting_point=0.0, ), name='My_DDM', ) myMechanism_2 = DDM( function=DriftDiffusionAnalytical( drift_rate=2.0, threshold=20.0), name='My_DDM_2' ) myMechanism_3 = DDM( function=DriftDiffusionAnalytical( drift_rate=3.0, threshold=30.0 ), name='My_DDM_3', ) z = Composition( # default_variable=[[30], [10]], pathways=[[ myMechanism, (IDENTITY_MATRIX), myMechanism_2, (FULL_CONNECTIVITY_MATRIX), myMechanism_3 ]], ) result = z.execute(inputs={myMechanism:[40]}) expected_output = [ (myMechanism.input_ports[0].parameters.value.get(z), np.array([40.])), (myMechanism.output_ports[0].parameters.value.get(z), np.array([10.])), (myMechanism_2.input_ports[0].parameters.value.get(z), np.array([10.])), (myMechanism_2.output_ports[0].parameters.value.get(z), np.array([20.])), (myMechanism_3.input_ports[0].parameters.value.get(z), np.array([20.])), (myMechanism_3.output_ports[0].parameters.value.get(z), np.array([30.])), (result[0], np.array([30.])), ] for i in range(len(expected_output)): val, expected = expected_output[i] # setting absolute tolerance to be in accordance with reference_output precision # if you do not specify, assert_allcose will use a relative tolerance of 1e-07, # which WILL FAIL unless you gather higher precision values to use as reference np.testing.assert_allclose(val, expected, atol=1e-08, err_msg='Failed on expected_output[{0}]'.format(i))
def test_LCAMechanism_DDM_equivalent(self, comp_mode): lca = LCAMechanism(size=2, leak=0., threshold=1, auto=0, hetero=0, initial_value=[0, 0], execute_until_finished=False) comp1 = Composition() comp1.add_node(lca) result1 = comp1.run(inputs={lca: [1, -1]}, execution_mode=comp_mode) assert np.allclose(result1, [[0.52497918747894, 0.47502081252106]])
def test_four_integrators_mixed(self): A = IntegratorMechanism(name='A', default_variable=[0], function=SimpleIntegrator(rate=1)) B = IntegratorMechanism(name='B', default_variable=[0], function=SimpleIntegrator(rate=1)) C = IntegratorMechanism(name='C', default_variable=[0], function=SimpleIntegrator(rate=1)) D = IntegratorMechanism(name='D', default_variable=[0], function=SimpleIntegrator(rate=1)) c = Composition(pathways=[[A, C], [A, D], [B, C], [B, D]]) term_conds = { TimeScale.TRIAL: All(AfterNCalls(C, 1), AfterNCalls(D, 1)) } stim_list = {A: [[1]], B: [[1]]} sched = Scheduler(composition=c) sched.add_condition(B, EveryNCalls(A, 2)) sched.add_condition(C, EveryNCalls(A, 1)) sched.add_condition(D, EveryNCalls(B, 1)) c.scheduler = sched c.run(inputs=stim_list, termination_processing=term_conds) mechs = [A, B, C, D] expected_output = [ [ numpy.array([2.]), ], [ numpy.array([1.]), ], [ numpy.array([4.]), ], [ numpy.array([3.]), ], ] for m in range(len(mechs)): for i in range(len(expected_output[m])): numpy.testing.assert_allclose(expected_output[m][i], mechs[m].get_output_values(c)[i])
def test_checkmark2_1(self): comp = Composition() A = TransferMechanism(function=Linear(slope=5.0, intercept=2.0), name='scheduler-pytests-A') B = TransferMechanism(function=Linear(intercept=4.0), name='scheduler-pytests-B') C = TransferMechanism(function=Linear(intercept=1.5), name='scheduler-pytests-C') D = TransferMechanism(function=Linear(intercept=.5), name='scheduler-pytests-D') for m in [A, B, C, D]: comp.add_node(m) comp.add_projection(MappingProjection(), A, B) comp.add_projection(MappingProjection(), A, D) comp.add_projection(MappingProjection(), B, D) comp.add_projection(MappingProjection(), C, D) sched = Scheduler(composition=comp) sched.add_condition(A, EveryNPasses(1)) sched.add_condition(B, EveryNCalls(A, 2)) sched.add_condition(C, EveryNCalls(A, 2)) sched.add_condition(D, All(EveryNCalls(B, 2), EveryNCalls(C, 2))) termination_conds = {} termination_conds[TimeScale.RUN] = AfterNTrials(1) termination_conds[TimeScale.TRIAL] = AfterNCalls(D, 1, time_scale=TimeScale.TRIAL) output = list(sched.run(termination_conds=termination_conds)) expected_output = [ A, set([A, C]), B, A, set([A, C]), B, D ] assert output == pytest.helpers.setify_expected_output(expected_output)
def test_composition_run_with_sticky_condition(self): # Construction T = TransferMechanism() C = Composition() C.add_node(T) assert T.noise == 0.0 assert T.parameter_ports['noise'].value == 0.0 # Runtime param used for noise # ONLY mechanism value should reflect runtime param -- attr should be changed back by the time we inspect it C.run(inputs={T: 2.0}, runtime_params={T: { "noise": (10.0, AfterTrial(1)) }}, num_trials=4) # Runtime param NOT used for noise C.run(inputs={T: 2.0}) assert np.allclose( C.results, [ np.array([[2.]]), # Trial 0 - condition not satisfied yet np.array([[2.]]), # Trial 1 - condition not satisfied yet np.array([[12.]]), # Trial 2 - condition satisfied np.array([[12.]]), # Trial 3 - condition satisfied (sticky) np.array([[2.]]) ]) # New run (runtime param no longer applies)
def test_composition_run_with_combined_condition(self): # Construction T = TransferMechanism() C = Composition() C.add_node(T) # Runtime param used for noise # ONLY mechanism value should reflect runtime param -- attr should be changed back by the time we inspect it C.run(inputs={T: 2.0}, runtime_params={ T: { "noise": (10.0, Any(AtTrial(1), AfterTrial(2))) } }, num_trials=5) # Runtime param NOT used for noise C.run(inputs={T: 2.0}) assert np.allclose( C.results, [ np.array([[2.]]), # Trial 0 - NOT condition 0, NOT condition 1 np.array([[12.]]), # Trial 1 - condition 0, NOT condition 1 np.array([[2.]]), # Trial 2 - NOT condition 0, NOT condition 1 np.array([[12.]]), # Trial 3 - NOT condition 0, condition 1 np.array([[12.]]), # Trial 4 - NOT condition 0, condition 1 np.array([[2.]]) ]) # New run (runtime param no longer applies)
def test_function_params_with_different_but_overlapping_conditions(self): T = TransferMechanism() C = Composition() C.add_node(T) assert T.function.slope.base == 1.0 assert T.parameter_ports['slope'].value == 1.0 # run with runtime param used for slope only on trial 1 and after 2 (i.e., 3 and 4) C.run(inputs={T: 2.0}, runtime_params={T: {"slope": (10.0, Any(AtTrial(1), AfterTrial(2))), "intercept": (1.0, AfterTrial(1))}}, num_trials=4) # slope restored to default assert T.function.slope.base == 1.0 assert T.parameter_ports['slope'].value == 1.0 assert T.function.intercept.base == 0.0 assert T.parameter_ports['intercept'].value == 0.0 # run again to insure restored default for slope after last run C.run(inputs={T: 2.0}) # results reflect runtime_param used for slope only on trials 1, 3 and 4 assert np.allclose(C.results,[np.array([[2.]]), # Trial 0 - neither condition met np.array([[20.]]), # Trial 1 - slope condition met, intercept not met np.array([[3.]]), # Trial 2 - slope condition not met, intercept met np.array([[21.]]), # Trial 3 - both conditions met np.array([[2.]])]) # New run (runtime param no longer applies)
def test_function_param_with_combined_condition(self): T = TransferMechanism() C = Composition() C.add_node(T) assert T.function.slope.base == 1.0 assert T.parameter_ports['slope'].value == 1.0 # run with runtime param used for slope only on trial 1 and after 2 (i.e., 3 and 4) C.run(inputs={T: 2.0}, runtime_params={T: {"slope": (10.0, Any(AtTrial(1), AfterTrial(2)))}}, num_trials=5) # slope restored to default assert T.function.slope.base == 1.0 assert T.parameter_ports['slope'].value == 1.0 # run again to insure restored default for slope after last run C.run(inputs={T: 2.0}) # results reflect runtime_param used for slope only on trials 1, 3 and 4 assert np.allclose(C.results,[np.array([[2.]]), # Trial 0 - NOT condition 0, NOT condition 1 np.array([[20.]]), # Trial 1 - condition 0, NOT condition 1 np.array([[2.]]), # Trial 2 - NOT condition 0, NOT condition 1 np.array([[20.]]), # Trial 3 - NOT condition 0, condition 1 np.array([[20.]]), # Trial 4 - NOT condition 0, condition 1 np.array([[2.]])]) # New run (runtime param no longer applies)
def test_mechanism_param_with_AfterTrial_condition(self): T = TransferMechanism() C = Composition() C.add_node(T) assert T.noise.base == 0.0 assert T.parameter_ports['noise'].value == 0.0 # run with runtime param used for noise after trial 1 (i.e., trials 2 and 3) C.run(inputs={T: 2.0}, runtime_params={T: {"noise": (10.0, AfterTrial(1))}}, num_trials=4) # noise restored to default assert T.noise.base == 0.0 assert T.parameter_ports['noise'].parameters.value.get(C) == 0.0 # run again to insure restored default for noise after last run C.run(inputs={T: 2.0}) # results reflect runtime_param used for noise only on trials 2 and 3 assert np.allclose(C.results, [np.array([[2.]]), # Trial 0 - condition not satisfied yet np.array([[2.]]), # Trial 1 - condition not satisfied yet np.array([[12.]]), # Trial 2 - condition satisfied np.array([[12.]]), # Trial 3 - condition satisfied (sticky) np.array([[2.]])]) # New run (runtime param no longer applies)
def test_6_two_trials(self): comp = Composition() A = TransferMechanism(function=Linear(slope=5.0, intercept=2.0), name='scheduler-pytests-A') B = TransferMechanism(function=Linear(intercept=4.0), name='scheduler-pytests-B') C = TransferMechanism(function=Linear(intercept=1.5), name='scheduler-pytests-C') for m in [A, B, C]: comp.add_node(m) comp.add_projection(MappingProjection(), A, B) comp.add_projection(MappingProjection(), B, C) sched = Scheduler(composition=comp) sched.add_condition(A, BeforePass(5)) sched.add_condition(B, AfterNCalls(A, 5)) sched.add_condition(C, AfterNCalls(B, 1)) termination_conds = {} termination_conds[TimeScale.RUN] = AfterNTrials(2) termination_conds[TimeScale.TRIAL] = AfterNCalls(C, 3) comp.run( inputs={A: [[0], [1], [2], [3], [4], [5]]}, scheduler_processing=sched, termination_processing=termination_conds ) output = sched.execution_list[comp.default_execution_id] expected_output = [ A, A, A, A, A, B, C, B, C, B, C, A, A, A, A, A, B, C, B, C, B, C ] # pprint.pprint(output) assert output == pytest.helpers.setify_expected_output(expected_output)
def test_WhileNot_AtPass_in_middle(self): comp = Composition() A = TransferMechanism(function=Linear(slope=5.0, intercept=2.0), name='A') comp.add_node(A) sched = Scheduler(composition=comp) sched.add_condition(A, WhileNot(lambda sched: sched.clock.get_total_times_relative(TimeScale.PASS, TimeScale.TRIAL) == 2, sched)) termination_conds = {} termination_conds[TimeScale.RUN] = AfterNTrials(1) termination_conds[TimeScale.TRIAL] = AtPass(5) output = list(sched.run(termination_conds=termination_conds)) expected_output = [A, A, set(), A, A] assert output == pytest.helpers.setify_expected_output(expected_output)
def test_BeforeNCalls(self): comp = Composition() A = TransferMechanism(function=Linear(slope=5.0, intercept=2.0), name='A') comp.add_node(A) sched = Scheduler(composition=comp) sched.add_condition(A, BeforeNCalls(A, 3)) termination_conds = {} termination_conds[TimeScale.RUN] = AfterNTrials(1) termination_conds[TimeScale.TRIAL] = AtPass(5) output = list(sched.run(termination_conds=termination_conds)) expected_output = [A, A, A, set(), set()] assert output == pytest.helpers.setify_expected_output(expected_output)
def test_AtTrialStart(self): comp = Composition() A = TransferMechanism(name='A') B = TransferMechanism(name='B') comp.add_linear_processing_pathway([A, B]) sched = Scheduler(composition=comp) sched.add_condition(B, AtTrialStart()) termination_conds = {TimeScale.TRIAL: AtPass(3)} output = list(sched.run(termination_conds=termination_conds)) expected_output = [A, B, A, A] assert output == pytest.helpers.setify_expected_output( expected_output)