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_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_multisource_2(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_mechanism(m) comp.add_projection(A1, MappingProjection(), B1) comp.add_projection(A1, MappingProjection(), B2) comp.add_projection(A2, MappingProjection(), B1) comp.add_projection(A2, MappingProjection(), B2) comp.add_projection(A2, MappingProjection(), B3) comp.add_projection(B1, MappingProjection(), C1) comp.add_projection(B2, MappingProjection(), C1) comp.add_projection(B1, MappingProjection(), C2) comp.add_projection(B3, MappingProjection(), C2) sched = Scheduler(composition=comp) sched.add_condition_set({ A1: Always(), A2: Always(), B1: EveryNCalls(A1, 2), B3: EveryNCalls(A2, 2), B2: All(EveryNCalls(A1, 4), EveryNCalls(A2, 4)), C1: Any(AfterNCalls(B1, 2), AfterNCalls(B2, 2)), C2: Any(AfterNCalls(B2, 2), AfterNCalls(B3, 2)), }) 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([A1, A2]), set([B1, B3]), set([A1, A2]), set([A1, A2]), set([B1, B2, B3]), set([C1, C2]) ] 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), ) p = Process(default_variable=[0], pathway=[A, C, D], name='p') q = Process(default_variable=[0], pathway=[B, C, E], name='q') s = System(processes=[p, q], name='s') term_conds = {TimeScale.TRIAL: AfterNCalls(E, 1)} stim_list = {A: [[1]], B: [[2]]} sched = Scheduler(system=s) 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)) s.scheduler_processing = sched s.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].output_values[i])
def test_invtriangle_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') for m in [A, B, C]: comp.add_mechanism(m) comp.add_projection(A, MappingProjection(), C) comp.add_projection(B, MappingProjection(), C) sched = Scheduler(composition=comp) sched.add_condition(A, EveryNPasses(1)) sched.add_condition(B, EveryNCalls(A, 2)) sched.add_condition(C, Any(AfterNCalls(A, 3), AfterNCalls(B, 3))) 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, set([A, B]), A, C, set([A, B]), C, A, C, set([A, B]), C ] # pprint.pprint(output) assert output == pytest.helpers.setify_expected_output(expected_output)
def test_system_run_with_combined_condition(self): # Construction T = TransferMechanism() P = Process(pathway=[T]) S = System(processes=[P]) # Runtime param used for noise # ONLY mechanism value should reflect runtime param -- attr should be changed back by the time we inspect it S.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 S.run(inputs={T: 2.0}) assert np.allclose( S.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_three_integrators(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)) p = Process(default_variable=[0], pathway=[A, C], name='p') q = Process(default_variable=[0], pathway=[B, C], name='q') s = System(processes=[p, q], name='s') term_conds = {TimeScale.TRIAL: AfterNCalls(C, 2)} stim_list = {A: [[1]], B: [[1]]} sched = Scheduler(system=s) sched.add_condition(B, EveryNCalls(A, 2)) sched.add_condition(C, Any(EveryNCalls(A, 1), EveryNCalls(B, 1))) s.scheduler_processing = sched s.run(inputs=stim_list, termination_processing=term_conds) mechs = [A, B, C] expected_output = [ [ numpy.array([2.]), ], [ numpy.array([1.]), ], [ numpy.array([4.]), ], ] for m in range(len(mechs)): for i in range(len(expected_output[m])): numpy.testing.assert_allclose(expected_output[m][i], mechs[m].output_values[i])
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].output_values[i])
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_7(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) comp.add_projection(A, MappingProjection(), 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_composition_run_with_combined_condition(self): # Construction T = TransferMechanism() C = Composition() C.add_mechanism(T) results = [] def call_after_trial(): results.append(T.output_state.value) # 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, call_after_trial=call_after_trial) # Runtime param NOT used for noise C.run(inputs={T: 2.0}, call_after_trial=call_after_trial) assert np.allclose( 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_10b(self): comp = Composition() A = TransferMechanism(function=Linear(slope=5.0, intercept=2.0), name='scheduler-pytests-A') A.is_finished = False B = TransferMechanism(function=Linear(intercept=4.0), name='scheduler-pytests-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, Any(WhenFinished(A), AfterNCalls(A, 3))) termination_conds = {} termination_conds[TimeScale.RUN] = AfterNTrials(1) termination_conds[TimeScale.TRIAL] = AfterNCalls(B, 4) output = list(sched.run(termination_conds=termination_conds)) expected_output = [A, A, A, B, A, B, A, B, A, B] assert output == pytest.helpers.setify_expected_output(expected_output)