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_invtriangle_2(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, C) comp.add_projection(MappingProjection(), B, C) sched = Scheduler(composition=comp) sched.add_condition(A, EveryNPasses(1)) sched.add_condition(B, EveryNCalls(A, 2)) sched.add_condition(C, All(AfterNCalls(A, 3), AfterNCalls(B, 3))) termination_conds = {} termination_conds[TimeScale.RUN] = AfterNTrials(1) termination_conds[TimeScale.TRIAL] = AfterNCalls(C, 2, time_scale=TimeScale.TRIAL) output = list(sched.run(termination_conds=termination_conds)) expected_output = [ A, set([A, B]), A, set([A, B]), A, set([A, B]), C, A, C ] assert output == pytest.helpers.setify_expected_output(expected_output)
def test_6(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(1) termination_conds[TimeScale.TRIAL] = AfterNCalls(C, 3) output = list(sched.run(termination_conds=termination_conds)) expected_output = [ 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_composite_condition_multi(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_node(m) comp.add_projection(MappingProjection(), A, B) comp.add_projection(MappingProjection(), B, C) sched = Scheduler(composition=comp) sched.add_condition(A, EveryNPasses(1)) sched.add_condition(B, EveryNCalls(A, 2)) sched.add_condition(C, All( Any( AfterPass(6), AfterNCalls(B, 2) ), Any( AfterPass(2), AfterNCalls(B, 3) ) ) ) termination_conds = {} termination_conds[TimeScale.RUN] = AfterNTrials(1) termination_conds[TimeScale.TRIAL] = AfterNCalls(C, 3) output = list(sched.run(termination_conds=termination_conds)) expected_output = [ A, A, B, A, A, B, C, A, C, A, B, C ] assert output == pytest.helpers.setify_expected_output(expected_output)
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)) p = Process(default_variable=[0], pathway=[A, C], name='p') p1 = Process(default_variable=[0], pathway=[A, D], name='p1') q = Process(default_variable=[0], pathway=[B, C], name='q') q1 = Process(default_variable=[0], pathway=[B, D], name='q1') s = System(processes=[p, p1, q, q1], name='s') term_conds = { TimeScale.TRIAL: All(AfterNCalls(C, 1), AfterNCalls(D, 1)) } stim_list = {A: [[1]], B: [[1]]} sched = Scheduler(system=s) sched.add_condition(B, EveryNCalls(A, 2)) sched.add_condition(C, EveryNCalls(A, 1)) sched.add_condition(D, EveryNCalls(B, 1)) s.scheduler_processing = sched s.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(s)[i])
def test_triangle_4b(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(), A, C) sched = Scheduler(composition=comp) sched.add_condition(A, EveryNPasses(1)) sched.add_condition(B, EveryNCalls(A, 2)) sched.add_condition(C, All(WhenFinished(A), AfterNCalls(B, 3))) termination_conds = {} termination_conds[TimeScale.RUN] = AfterNTrials(1) termination_conds[TimeScale.TRIAL] = AfterNCalls(C, 1) output = [] i = 0 for step in sched.run(termination_conds=termination_conds): if i == 10: A._is_finished = True output.append(step) i += 1 expected_output = [A, A, B, A, A, B, A, A, B, A, A, set([B, C])] # pprint.pprint(output) assert output == pytest.helpers.setify_expected_output(expected_output)
def test_multisource_1(self): comp = Composition() A1 = TransferMechanism(function=Linear(slope=5.0, intercept=2.0), name='A1') A2 = TransferMechanism(function=Linear(slope=5.0, intercept=2.0), name='A2') B1 = TransferMechanism(function=Linear(intercept=4.0), name='B1') B2 = TransferMechanism(function=Linear(intercept=4.0), name='B2') B3 = TransferMechanism(function=Linear(intercept=4.0), name='B3') C1 = TransferMechanism(function=Linear(intercept=1.5), name='C1') C2 = TransferMechanism(function=Linear(intercept=.5), name='C2') for m in [A1, A2, B1, B2, B3, C1, C2]: comp.add_node(m) comp.add_projection(MappingProjection(), A1, B1) comp.add_projection(MappingProjection(), A1, B2) comp.add_projection(MappingProjection(), A2, B1) comp.add_projection(MappingProjection(), A2, B2) comp.add_projection(MappingProjection(), A2, B3) comp.add_projection(MappingProjection(), B1, C1) comp.add_projection(MappingProjection(), B2, C1) comp.add_projection(MappingProjection(), B1, C2) comp.add_projection(MappingProjection(), B3, C2) sched = Scheduler(composition=comp) for m in comp.nodes: sched.add_condition(m, Always()) termination_conds = {} termination_conds[TimeScale.RUN] = AfterNTrials(1) termination_conds[TimeScale.TRIAL] = All(AfterNCalls(C1, 1), AfterNCalls(C2, 1)) output = list(sched.run(termination_conds=termination_conds)) expected_output = [ set([A1, A2]), set([B1, B2, B3]), set([C1, C2]) ] 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)) c = Composition(pathways=[A, B]) term_conds = {TimeScale.TRIAL: AfterNCalls(B, 2)} stim_list = {A: [[1]]} sched = Scheduler(composition=c) sched.add_condition(A, Any(AtPass(0), AfterNCalls(B, 2))) sched.add_condition(B, Any(JustRan(A), JustRan(B))) c.scheduler = sched c.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.get_output_values(c)[i])
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_two_AAB(self): A = IntegratorMechanism(name='A', default_variable=[0], function=SimpleIntegrator(rate=.5)) B = TransferMechanism( name='B', default_variable=[0], function=Linear(slope=2.0), ) p = Process(default_variable=[0], pathway=[A, B], name='p') s = System(processes=[p], name='s') term_conds = {TimeScale.TRIAL: AfterNCalls(B, 1)} stim_list = {A: [[1]]} sched = Scheduler(system=s) sched.add_condition(B, EveryNCalls(A, 2)) 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.get_output_values(s)[i])
def test_termination_conditions_reset(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') for m in [A, B]: comp.add_node(m) comp.add_projection(MappingProjection(), A, B) sched = Scheduler(composition=comp) sched.add_condition(B, EveryNCalls(A, 2)) termination_conds = {} termination_conds[TimeScale.RUN] = AfterNTrials(1) termination_conds[TimeScale.TRIAL] = AfterNCalls(B, 2) output = list(sched.run(termination_conds=termination_conds)) expected_output = [A, A, B, A, A, B] assert output == pytest.helpers.setify_expected_output(expected_output) # reset the RUN because schedulers run TRIALs sched.clock._increment_time(TimeScale.RUN) sched._reset_counts_total(TimeScale.RUN) output = list(sched.run()) expected_output = [A, A, B] assert output == pytest.helpers.setify_expected_output(expected_output)
def test_WhenFinishedAll_2(self): comp = Composition() A = TransferMechanism(function=Linear(slope=5.0, intercept=2.0), name='A') A.is_finished_flag = False B = TransferMechanism(function=Linear(intercept=4.0), name='B') B.is_finished_flag = True C = TransferMechanism(function=Linear(intercept=1.5), name='C') for m in [A, B, C]: comp.add_node(m) comp.add_projection(MappingProjection(), A, C) comp.add_projection(MappingProjection(), B, C) sched = Scheduler(composition=comp) sched.add_condition(A, EveryNPasses(1)) sched.add_condition(B, EveryNPasses(1)) sched.add_condition(C, WhenFinishedAll(A, B)) termination_conds = {} termination_conds[TimeScale.RUN] = AfterNTrials(1) termination_conds[TimeScale.TRIAL] = AfterNCalls(A, 5) output = list(sched.run(termination_conds=termination_conds)) expected_output = [ set([A, B]), set([A, B]), set([A, B]), set([A, B]), set([A, B]), ] assert output == pytest.helpers.setify_expected_output(expected_output)
def test_9(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') for m in [A, B]: comp.add_node(m) comp.add_projection(MappingProjection(), A, 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] = AfterNCalls(B, 2) output = [] i = 0 A.is_finished_flag = False for step in sched.run(termination_conds=termination_conds): if i == 3: A.is_finished_flag = True output.append(step) i += 1 expected_output = [A, A, A, A, B, A, B] assert output == pytest.helpers.setify_expected_output(expected_output)
def test_checkmark_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(), B, D) comp.add_projection(MappingProjection(), C, D) sched = Scheduler(composition=comp) sched.add_condition(A, Always()) sched.add_condition(B, Always()) sched.add_condition(C, Always()) sched.add_condition(D, Always()) 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 = [ set([A, C]), B, D ] assert output == pytest.helpers.setify_expected_output(expected_output)
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_7(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') for m in [A, B]: comp.add_node(m) comp.add_projection(MappingProjection(), A, B) sched = Scheduler(composition=comp) sched.add_condition(A, EveryNPasses(1)) sched.add_condition(B, EveryNCalls(A, 2)) termination_conds = {} termination_conds[TimeScale.RUN] = AfterNTrials(1) termination_conds[TimeScale.TRIAL] = Any(AfterNCalls(A, 1), AfterNCalls(B, 1)) output = list(sched.run(termination_conds=termination_conds)) expected_output = [A] assert output == pytest.helpers.setify_expected_output(expected_output)
def test_NWhen_AfterNCalls(self, n, expected_output): 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_node(m) comp.add_projection(MappingProjection(), A, B) sched = Scheduler(composition=comp) sched.add_condition(A, Always()) sched.add_condition(B, NWhen(AfterNCalls(A, 3), n)) termination_conds = {} termination_conds[TimeScale.RUN] = AfterNTrials(1) termination_conds[TimeScale.TRIAL] = AfterNCalls(A, 6) output = list(sched.run(termination_conds=termination_conds)) expected_output = [A if x == 'A' else B for x in expected_output] assert output == pytest.helpers.setify_expected_output(expected_output)
def test_partial_override_composition(self): comp = Composition() A = TransferMechanism(name='scheduler-pytests-A') B = IntegratorMechanism(name='scheduler-pytests-B') for m in [A, B]: comp.add_node(m) comp.add_projection(MappingProjection(), A, B) termination_conds = {TimeScale.TRIAL: AfterNCalls(B, 2)} output = comp.run(inputs={A: 1}, termination_processing=termination_conds) # two executions of B assert output == [.75]
def test_four_ABBCD(self): A = TransferMechanism( name='A', default_variable=[0], function=Linear(slope=2.0), ) B = IntegratorMechanism(name='B', default_variable=[0], function=SimpleIntegrator(rate=.5)) C = IntegratorMechanism(name='C', default_variable=[0], function=SimpleIntegrator(rate=.5)) D = TransferMechanism( name='D', default_variable=[0], function=Linear(slope=1.0), ) p = Process(default_variable=[0], pathway=[A, B, D], name='p') q = Process(default_variable=[0], pathway=[A, C, D], name='q') s = System(processes=[p, q], name='s') term_conds = {TimeScale.TRIAL: AfterNCalls(D, 1)} stim_list = {A: [[1]]} sched = Scheduler(system=s) sched.add_condition(B, EveryNCalls(A, 1)) sched.add_condition(C, EveryNCalls(A, 2)) sched.add_condition(D, Any(EveryNCalls(B, 3), EveryNCalls(C, 3))) s.scheduler_processing = sched s.run(inputs=stim_list, termination_processing=term_conds) terminal_mechs = [D] expected_output = [ [ numpy.array([4.]), ], ] 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(s)[i])
def test_All_end_after_one_finished(self): comp = Composition() A = TransferMechanism(function=Linear(slope=5.0, intercept=2.0), name='A') for m in [A]: comp.add_node(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, 5), AtPass(10)) 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_partial_override_scheduler(self): comp = Composition() A = TransferMechanism(name='scheduler-pytests-A') B = TransferMechanism(name='scheduler-pytests-B') for m in [A, B]: comp.add_node(m) comp.add_projection(MappingProjection(), A, B) sched = Scheduler(composition=comp) sched.add_condition(B, EveryNCalls(A, 2)) termination_conds = {TimeScale.TRIAL: AfterNCalls(B, 2)} output = list(sched.run(termination_conds=termination_conds)) expected_output = [A, A, B, A, A, B] assert output == pytest.helpers.setify_expected_output(expected_output)
def test_three_ABAC_convenience(self): A = IntegratorMechanism(name='A', default_variable=[0], function=SimpleIntegrator(rate=.5)) B = TransferMechanism( name='B', default_variable=[0], function=Linear(slope=2.0), ) C = TransferMechanism( name='C', default_variable=[0], function=Linear(slope=2.0), ) p = Process(default_variable=[0], pathway=[A, B], name='p') q = Process(default_variable=[0], pathway=[A, C], name='q') s = System(processes=[p, q], name='s') term_conds = {TimeScale.TRIAL: AfterNCalls(C, 1)} stim_list = {A: [[1]]} s.scheduler_processing.add_condition( B, Any(AtNCalls(A, 1), EveryNCalls(A, 2))) s.scheduler_processing.add_condition(C, EveryNCalls(A, 2)) s.run(inputs=stim_list, termination_processing=term_conds) terminal_mechs = [B, C] expected_output = [ [ numpy.array([1.]), ], [ numpy.array([2.]), ], ] 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(s)[i])
def test_termination_conditions_reset(self): A = IntegratorMechanism(name='A', default_variable=[0], function=SimpleIntegrator(rate=.5)) B = TransferMechanism( name='B', default_variable=[0], function=Linear(slope=2.0), ) c = Composition(pathways=[[A, B]]) term_conds = {TimeScale.TRIAL: AfterNCalls(B, 2)} stim_list = {A: [[1]]} sched = Scheduler(composition=c) sched.add_condition(B, EveryNCalls(A, 2)) c.scheduler = sched c.run(inputs=stim_list, termination_processing=term_conds) # A should run four times terminal_mech = B expected_output = [ numpy.array([4.]), ] for i in range(len(expected_output)): numpy.testing.assert_allclose( expected_output[i], terminal_mech.get_output_values(c)[i]) c.run(inputs=stim_list, ) # A should run an additional two times terminal_mech = B expected_output = [ numpy.array([6.]), ] for i in range(len(expected_output)): numpy.testing.assert_allclose( expected_output[i], terminal_mech.get_output_values(c)[i])
def test_AfterNCalls(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_node(m) sched = Scheduler(composition=comp) sched.add_condition(A, Always()) sched.add_condition(B, AfterNCalls(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, set([A, B]), set([A, B]), set([A, B])] assert output == pytest.helpers.setify_expected_output(expected_output)
def test_one_run_twice(self): A = IntegratorMechanism(name='A', default_variable=[0], function=SimpleIntegrator(rate=.5, )) c = Composition(pathways=[A]) term_conds = {TimeScale.TRIAL: AfterNCalls(A, 2)} stim_list = {A: [[1]]} c.run(inputs=stim_list, termination_processing=term_conds) terminal_mech = A expected_output = [ numpy.array([1.]), ] for i in range(len(expected_output)): numpy.testing.assert_allclose( expected_output[i], terminal_mech.get_output_values(c)[i])
def test_AtPass_underconstrained(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_node(m) comp.add_projection(MappingProjection(), A, B) comp.add_projection(MappingProjection(), B, C) sched = Scheduler(composition=comp) sched.add_condition(A, AtPass(0)) sched.add_condition(B, Always()) sched.add_condition(C, Always()) termination_conds = {} termination_conds[TimeScale.RUN] = AfterNTrials(1) termination_conds[TimeScale.TRIAL] = AfterNCalls(C, 2) output = list(sched.run(termination_conds=termination_conds)) expected_output = [A, B, C, B, C] assert output == pytest.helpers.setify_expected_output(expected_output)
def test_linear_ABBCC(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, Any(AtPass(0), EveryNCalls(C, 2))) sched.add_condition(B, Any(JustRan(A), JustRan(B))) sched.add_condition(C, Any(EveryNCalls(B, 2), JustRan(C))) 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, B, C, C, A, B, B, C, C] assert output == pytest.helpers.setify_expected_output(expected_output)
def test_six_integrators_threelayer_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)) E = IntegratorMechanism(name='E', default_variable=[0], function=SimpleIntegrator(rate=1)) F = IntegratorMechanism(name='F', default_variable=[0], function=SimpleIntegrator(rate=1)) c = Composition(pathways=[[A, C, E], [A, C, F], [A, D, E], [A, D, F], [B, C, E], [B, C, F], [B, D, E], [B, D, F]]) term_conds = { TimeScale.TRIAL: All(AfterNCalls(E, 1), AfterNCalls(F, 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)) sched.add_condition(E, EveryNCalls(C, 1)) sched.add_condition(F, EveryNCalls(D, 2)) c.scheduler = sched c.run(inputs=stim_list, termination_processing=term_conds) # Intermediate time steps # # 0 1 2 3 # # A 1 2 3 4 # B 1 2 # C 1 4 8 14 # D 3 9 # E 1 8 19 42 # F 23 # expected_output = { A: [ numpy.array([4.]), ], B: [ numpy.array([2.]), ], C: [ numpy.array([14.]), ], D: [ numpy.array([9.]), ], E: [ numpy.array([42.]), ], F: [ numpy.array([23.]), ], } for m in expected_output: for i in range(len(expected_output[m])): numpy.testing.assert_allclose(expected_output[m][i], m.get_output_values(c)[i])
class TestAbsolute: A = TransferMechanism(name='scheduler-pytests-A') B = TransferMechanism(name='scheduler-pytests-B') C = TransferMechanism(name='scheduler-pytests-C') @pytest.mark.parametrize( 'conditions, termination_conds', [ ( { A: TimeInterval(repeat=8), B: TimeInterval(repeat=4), C: TimeInterval(repeat=2) }, { TimeScale.TRIAL: AfterNCalls(A, 2) }, ), ( { A: TimeInterval(repeat=5), B: TimeInterval(repeat=3), C: TimeInterval(repeat=1) }, { TimeScale.TRIAL: AfterNCalls(A, 2) }, ), ( { A: TimeInterval(repeat=3), B: TimeInterval(repeat=2) }, { TimeScale.TRIAL: AfterNCalls(A, 2) }, ), ( { A: TimeInterval(repeat=5), B: TimeInterval(repeat=7) }, { TimeScale.TRIAL: AfterNCalls(B, 2) }, ), ( { A: TimeInterval(repeat=1200), B: TimeInterval(repeat=1000) }, { TimeScale.TRIAL: AfterNCalls(A, 3) }, ), ( { A: TimeInterval(repeat=0.33333), B: TimeInterval(repeat=0.66666) }, { TimeScale.TRIAL: AfterNCalls(B, 3) }, ), # smaller than default units cause floating point issue without mitigation ( { A: TimeInterval(repeat=2 * _unit_registry.us), B: TimeInterval(repeat=4 * _unit_registry.us) }, { TimeScale.TRIAL: AfterNCalls(B, 3) }, ), ]) def test_TimeInterval_linear_everynms(self, conditions, termination_conds): comp = Composition() comp.add_linear_processing_pathway([self.A, self.B, self.C]) comp.scheduler.add_condition_set(conditions) list(comp.scheduler.run(termination_conds=termination_conds)) for node, cond in conditions.items(): executions = [ comp.scheduler.execution_timestamps[ comp.default_execution_id][i].absolute for i in range( len(comp.scheduler.execution_list[ comp.default_execution_id])) if node in comp.scheduler.execution_list[comp.default_execution_id][i] ] for i in range(1, len(executions)): assert (executions[i] - executions[i - 1]) == cond.repeat @pytest.mark.parametrize('conditions, termination_conds', [ ({ A: TimeInterval(repeat=10, start=100), B: TimeInterval(repeat=10, start=300), C: TimeInterval(repeat=10, start=400) }, { TimeScale.TRIAL: TimeInterval(start=500) }), ({ A: TimeInterval(start=100), B: TimeInterval(start=300), C: TimeInterval(start=400) }, { TimeScale.TRIAL: TimeInterval(start=500) }), ({ A: TimeInterval(repeat=2, start=105), B: TimeInterval(repeat=7, start=317), C: TimeInterval(repeat=11, start=431) }, { TimeScale.TRIAL: TimeInterval(start=597) }), ]) def test_TimeInterval_no_dependencies(self, conditions, termination_conds): comp = Composition() comp.add_nodes([self.A, self.B, self.C]) comp.scheduler.add_condition_set(conditions) time_step_abs_value = comp.scheduler._get_absolute_consideration_set_execution_unit( termination_conds) list(comp.scheduler.run(termination_conds=termination_conds)) for node, cond in conditions.items(): executions = [ comp.scheduler.execution_timestamps[ comp.default_execution_id][i].absolute for i in range( len(comp.scheduler.execution_list[ comp.default_execution_id])) if node in comp.scheduler.execution_list[comp.default_execution_id][i] ] for i in range(1, len(executions)): interval = (executions[i] - executions[i - 1]) if cond.repeat is not None: assert interval == cond.repeat else: assert interval == time_step_abs_value if cond.start is not None: if cond.start_inclusive: assert cond.start in executions else: assert cond.start + time_step_abs_value in executions @pytest.mark.parametrize('repeat, unit, expected_repeat', [ (1, None, 1 * _unit_registry.ms), ('1ms', None, 1 * _unit_registry.ms), (1 * _unit_registry.ms, None, 1 * _unit_registry.ms), (1, 'ms', 1 * _unit_registry.ms), (1, _unit_registry.ms, 1 * _unit_registry.ms), ('1', _unit_registry.ms, 1 * _unit_registry.ms), (1 * _unit_registry.ms, _unit_registry.ns, 1 * _unit_registry.ms), (1000 * _unit_registry.ms, None, 1000 * _unit_registry.ms), ]) def test_TimeInterval_time_specs(self, repeat, unit, expected_repeat): if unit is None: c = TimeInterval(repeat=repeat) else: c = TimeInterval(repeat=repeat, unit=unit) assert c.repeat == expected_repeat @pytest.mark.parametrize('repeat, inclusive, last_time', [ (10, True, 10 * _unit_registry.ms), (10, False, 11 * _unit_registry.ms), ]) def test_TimeTermination(self, three_node_linear_composition, repeat, inclusive, last_time): _, comp = three_node_linear_composition comp.scheduler.termination_conds = { TimeScale.TRIAL: TimeTermination(repeat, inclusive) } list(comp.scheduler.run()) assert comp.scheduler.get_clock( comp.scheduler.default_execution_id).time.absolute == last_time