Пример #1
0
    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_mechanism(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)
Пример #2
0
    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_mechanism(m)
        comp.add_projection(A, MappingProjection(), B)
        comp.add_projection(A, MappingProjection(), D)
        comp.add_projection(B, MappingProjection(), D)
        comp.add_projection(C, MappingProjection(), 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)
Пример #3
0
    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_mechanism(m)
        comp.add_projection(A, MappingProjection(), B)
        comp.add_projection(B, MappingProjection(), 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._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)
Пример #4
0
    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_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(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)
Пример #5
0
    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)
Пример #6
0
    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_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] = AfterNCalls(B, 2)

        output = []
        i = 0
        for step in sched.run(termination_conds=termination_conds):
            if i == 3:
                A.is_finished = True
            output.append(step)
            i += 1

        expected_output = [A, A, A, A, B, A, B]
        assert output == pytest.helpers.setify_expected_output(expected_output)
Пример #7
0
    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_mechanism(m)
        comp.add_projection(A, MappingProjection(), B)
        comp.add_projection(B, MappingProjection(), 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)
Пример #8
0
    def test_no_termination_conds(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(), B)
        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, EveryNCalls(B, 3))

        output = list(sched.run())

        expected_output = [
            A,
            A,
            B,
            A,
            A,
            B,
            A,
            A,
            B,
            C,
        ]
        # pprint.pprint(output)
        assert output == pytest.helpers.setify_expected_output(expected_output)
Пример #9
0
    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_mechanism(m)
        comp.add_projection(A, MappingProjection(), 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)
Пример #10
0
    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_mechanism(m)
        comp.add_projection(A, MappingProjection(), B)
        comp.add_projection(A, MappingProjection(), 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)
Пример #11
0
    def test_create_multiple_contexts(self):
        comp = Composition()
        A = TransferMechanism(function=Linear(slope=5.0, intercept=2.0),
                              name='scheduler-pytests-A')
        comp.add_mechanism(A)

        comp.scheduler_processing.clock._increment_time(TimeScale.TRIAL)

        eid2 = uuid.uuid4()
        eid3 = uuid.uuid4()
        comp.scheduler_processing._init_counts(execution_id=eid2)

        assert comp.scheduler_processing.clocks[eid2].time.trial == 0

        comp.scheduler_processing.clock._increment_time(TimeScale.TRIAL)

        assert comp.scheduler_processing.clocks[eid2].time.trial == 0

        comp.scheduler_processing._init_counts(
            execution_id=eid3,
            base_execution_id=comp.scheduler_processing.default_execution_id)

        assert comp.scheduler_processing.clocks[eid3].time.trial == 2
Пример #12
0
    def test_composition_run_with_condition(self):

        # Construction
        T = TransferMechanism()
        C = Composition()
        # S = Scheduler(composition=C)
        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, AtTrial(1))
            }},
            # scheduler_processing=S,
            num_trials=4,
            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 - condition not satisfied yet
                np.array([12.]),  # Trial 1 - condition satisfied
                np.array([
                    2.
                ]),  # Trial 2 - condition no longer satisfied (not sticky)
                np.array([
                    2.
                ]),  # Trial 3 - condition no longer satisfied (not sticky)
                np.array([2.])
            ])  # New run (runtime param no longer applies)
Пример #13
0
    def test_composition_run_function_param_no_condition(self):

        # Construction
        T = TransferMechanism()
        C = Composition()
        C.add_mechanism(T)

        assert T.function_object.slope == 1.0
        assert T.parameter_states['slope'].value == 1.0

        # Runtime param used for slope
        # 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: {"slope": 10.0}})
        assert T.function_object.slope == 1.0
        assert T.parameter_states['slope'].value == 1.0
        assert T.value == 20.0

        # Runtime param NOT used for slope
        C.run(inputs={T: 2.0})
        assert T.function_object.slope == 1.0
        assert T.parameter_states['slope'].value == 1.0
        assert T.value == 2.0
Пример #14
0
    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)
Пример #15
0
    def test_9b(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, 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)
Пример #16
0
    def test_linear_AAB(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_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] = AfterNCalls(
            B, 2, time_scale=TimeScale.RUN)
        termination_conds[TimeScale.TRIAL] = AfterNCalls(
            B, 2, time_scale=TimeScale.TRIAL)
        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)
Пример #17
0
    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)