예제 #1
0
    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_reinitialize_when(self):
        I1 = IntegratorMechanism()
        I2 = IntegratorMechanism()
        I2.reinitialize_when = AtTrial(2)
        P1 = Process(pathway=[I1])
        P2 = Process(pathway=[I2])
        S = System(processes=[P1, P2],
                   reinitialize_mechanisms_when=AtTrial(3))

        S.run(inputs={I1: [[1.0]],
                      I2: [[1.0]]},
              num_trials=7)

        expected_results = [[np.array([0.5]), np.array([0.5])],
                            [np.array([0.75]), np.array([0.75])],
                            [np.array([0.875]), np.array([0.5])],   # I2 reinitializes at Trial 2
                            [np.array([0.5]), np.array([0.75])],    # I1 reinitializes at Trial 3
                            [np.array([0.75]), np.array([0.875])],
                            [np.array([0.875]), np.array([0.9375])],
                            [np.array([0.9375]), np.array([0.96875])]]

        assert np.allclose(expected_results, S.results)
예제 #3
0
        def test_AtTrial(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] = AtTrial(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]
            assert output == pytest.helpers.setify_expected_output(
                expected_output)
예제 #4
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)
예제 #5
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)
예제 #6
0
    def test_reinitialize_one_mechanism_at_trial_2_condition(self):
        A = TransferMechanism(name='A')
        B = TransferMechanism(name='B',
                              integrator_mode=True,
                              integration_rate=0.5)
        C = TransferMechanism(name='C')

        abc_process = Process(pathway=[A, B, C])
        abc_system = System(processes=[abc_process])

        # Set reinitialization condition
        B.reinitialize_when = AtTrial(2)

        C.log.set_log_conditions('value')

        abc_system.run(inputs={A: [1.0]},
                       reinitialize_values={B: [0.]},
                       num_trials=5)

        # Trial 0: 0.5, Trial 1: 0.75, Trial 2: 0.5, Trial 3: 0.75. Trial 4: 0.875
        assert np.allclose(
            C.log.nparray_dictionary('value')['value'],
            [[np.array([0.5])], [np.array([0.75])], [np.array([0.5])],
             [np.array([0.75])], [np.array([0.875])]])