def test_linear_ABCBC(self): comp = Composition() A = TransferMechanism(function=Linear(slope=5.0, intercept=2.0), name='A') B = TransferMechanism(function=Linear(intercept=4.0), name='B') C = TransferMechanism(function=Linear(intercept=1.5), name='C') for m in [A, B, C]: comp.add_mechanism(m) comp.add_projection(A, MappingProjection(), B) comp.add_projection(B, MappingProjection(), C) sched = Scheduler(composition=comp) sched.add_condition(A, Any(AtPass(0), EveryNCalls(C, 2))) sched.add_condition(B, Any(EveryNCalls(A, 1), EveryNCalls(C, 1))) sched.add_condition(C, EveryNCalls(B, 1)) termination_conds = {} termination_conds[TimeScale.RUN] = AfterNTrials(1) termination_conds[TimeScale.TRIAL] = AfterNCalls( C, 4, time_scale=TimeScale.TRIAL) output = list(sched.run(termination_conds=termination_conds)) expected_output = [A, B, C, B, C, A, B, C, B, C] assert output == pytest.helpers.setify_expected_output(expected_output)
def test_two_ABB(self): A = TransferMechanism( name='A', default_variable=[0], function=Linear(slope=2.0), ) B = IntegratorMechanism(name='B', default_variable=[0], function=SimpleIntegrator(rate=.5)) p = Process(default_variable=[0], pathway=[A, B], name='p') s = System(processes=[p], name='s') term_conds = {TimeScale.TRIAL: AfterNCalls(B, 2)} stim_list = {A: [[1]]} sched = Scheduler(system=s) sched.add_condition(A, Any(AtPass(0), AfterNCalls(B, 2))) sched.add_condition(B, Any(JustRan(A), JustRan(B))) s.scheduler_processing = sched s.run(inputs=stim_list, termination_processing=term_conds) terminal_mech = B expected_output = [ numpy.array([2.]), ] for i in range(len(expected_output)): numpy.testing.assert_allclose(expected_output[i], terminal_mech.output_values[i])
def test_AtPass_after_end(self): comp = Composition() A = TransferMechanism(function=Linear(slope=5.0, intercept=2.0), name='A') comp.add_mechanism(A) sched = Scheduler(composition=comp) sched.add_condition(A, AtPass(6)) termination_conds = {} termination_conds[TimeScale.RUN] = AfterNTrials(1) termination_conds[TimeScale.TRIAL] = AtPass(5) output = list(sched.run(termination_conds=termination_conds)) expected_output = [set(), set(), set(), set(), set()] assert output == pytest.helpers.setify_expected_output( expected_output)
def test_Any_end_before_one_finished(self): comp = Composition() A = TransferMechanism(function=Linear(slope=5.0, intercept=2.0), name='A') for m in [A]: comp.add_mechanism(m) sched = Scheduler(composition=comp) sched.add_condition(A, EveryNPasses(1)) termination_conds = {} termination_conds[TimeScale.RUN] = AfterNTrials(1) termination_conds[TimeScale.TRIAL] = Any(AfterNCalls(A, 10), AtPass(5)) output = list(sched.run(termination_conds=termination_conds)) expected_output = [A for _ in range(5)] assert output == pytest.helpers.setify_expected_output( expected_output)
def test_AfterCall(self): comp = Composition() A = TransferMechanism(function=Linear(slope=5.0, intercept=2.0), name='A') B = TransferMechanism(function=Linear(intercept=4.0), name='B') for m in [A, B]: comp.add_mechanism(m) sched = Scheduler(composition=comp) sched.add_condition(B, AfterCall(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([A, B]), set([A, B])] assert output == pytest.helpers.setify_expected_output( expected_output)
def test_AfterTrial(self): comp = Composition() A = TransferMechanism(function=Linear(slope=5.0, intercept=2.0), name='A') comp.add_mechanism(A) sched = Scheduler(composition=comp) sched.add_condition(A, Always()) termination_conds = {} termination_conds[TimeScale.RUN] = AfterTrial(4) termination_conds[TimeScale.TRIAL] = AtPass(1) comp.run(inputs={A: range(6)}, scheduler_processing=sched, termination_processing=termination_conds) output = sched.execution_list expected_output = [A, A, A, A, A] 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_mechanism(A) sched = Scheduler(composition=comp) sched.add_condition( A, WhileNot( lambda sched: sched.times[TimeScale.RUN][TimeScale.PASS] == 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_9b(self): comp = Composition() A = TransferMechanism(function=Linear(slope=5.0, intercept=2.0), name='A') A.is_finished = False B = TransferMechanism(function=Linear(intercept=4.0), name='B') for m in [A, B]: comp.add_mechanism(m) comp.add_projection(A, MappingProjection(), B) sched = Scheduler(composition=comp) sched.add_condition(A, EveryNPasses(1)) sched.add_condition(B, WhenFinished(A)) 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, A, A] assert output == pytest.helpers.setify_expected_output(expected_output)