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.get_output_values(s)[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))

        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])
Example #3
0
    def test_kwta_size_10_k_3_threshold_1(self):
        K = KWTAMechanism(
            name='K',
            size=10,
            k_value=3,
            threshold=1,
        )
        p = Process(pathway=[K], prefs=TestKWTALongTerm.simple_prefs)
        s = System(processes=[p], prefs=TestKWTALongTerm.simple_prefs)
        kwta_input = {K: [[-1, -.5, 0, 0, 0, 1, 1, 2, 3, 3]]}
        print("")
        for i in range(20):
            s.run(inputs=kwta_input)
            print('\ntrial number', i)
            print('K.parameters.value.get(s): ', K.parameters.value.get(s))
        assert np.allclose(K.parameters.value.get(s), [[0.012938850123312412, 0.022127587008877226, 0.039010157367582114,
                                     0.039010157367582114, 0.039010157367582114, 0.19055156271846602,

                                     0.19055156271846602, 0.969124504436019, 0.9895271824560731, 0.9895271824560731]])
        kwta_input2 = {K: [0] * 10}

        print('\n\nturning to zero-inputs now:')
        for i in range(20):
            s.run(inputs=kwta_input2)
            print('\ntrial number', i)
            print('K.parameters.value.get(s): ', K.parameters.value.get(s))
        assert np.allclose(K.parameters.value.get(s), [[0.13127237999481228, 0.13130057846907178, 0.1313653354768465, 0.1313653354768465,
                                     0.1313653354768465, 0.5863768938723602, 0.5863768938723602, 0.8390251365605804,
                                     0.8390251603214743, 0.8390251603214743]])
    def test_not_all_output_port_values_have_label(self):
        input_labels_dict = {
            "red": [1.0, 0.0],
            "green": [0.0, 1.0],
            "blue": [2.0, 2.0]
        }
        output_labels_dict = {"red": [1.0, 0.0], "green": [0.0, 1.0]}
        M = ProcessingMechanism(size=2,
                                params={
                                    INPUT_LABELS_DICT: input_labels_dict,
                                    OUTPUT_LABELS_DICT: output_labels_dict
                                })
        P = Process(pathway=[M])
        S = System(processes=[P])

        store_output_labels = []

        def call_after_trial():
            store_output_labels.append(M.get_output_labels(S))

        S.run(inputs=['red', 'blue', 'green', 'blue'],
              call_after_trial=call_after_trial)
        assert np.allclose(
            S.results,
            [[[1.0, 0.0]], [[2.0, 2.0]], [[0.0, 1.0]], [[2.0, 2.0]]])

        assert store_output_labels[0] == ['red']
        assert np.allclose(store_output_labels[1], [[2.0, 2.0]])
        assert store_output_labels[2] == ['green']
        assert np.allclose(store_output_labels[3], [[2.0, 2.0]])
Example #5
0
    def test_system_run_with_sticky_condition(self):

        # Construction
        T = TransferMechanism()
        P = Process(pathway=[T])
        S = System(processes=[P])
        assert T.noise == 0.0
        assert T.parameter_states['noise'].value == 0.0

        # 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, AfterTrial(1))
              }},
              num_trials=4)

        # Runtime param NOT used for noise
        S.run(inputs={T: 2.0})

        assert np.allclose(
            S.results,
            [
                [np.array([2.])],  # Trial 0 - condition not satisfied yet
                [np.array([2.])],  # Trial 1 - condition not satisfied yet
                [np.array([12.])],  # Trial 2 - condition satisfied
                [np.array([12.])],  # Trial 3 - condition satisfied (sticky)
                [np.array([2.])]
            ])  # New run (runtime param no longer applies)
Example #6
0
    def test_recurrent_transfer_origin(self):
        R = RecurrentTransferMechanism(has_recurrent_input_state=True)
        P = Process(pathway=[R])
        S = System(processes=[P])

        S.run(inputs={R: [[1.0], [2.0], [3.0]]})
        print(S.results)
Example #7
0
    def test_convergent(self):
        a = TransferMechanism(name='a', default_variable=[0, 0])
        b = TransferMechanism(name='b')
        c = TransferMechanism(name='c')
        c = TransferMechanism(name='c', default_variable=[0])
        d = TransferMechanism(name='d')
        e = TransferMechanism(name='e')

        p1 = Process(pathway=[a, b, e], name='p1')
        p2 = Process(pathway=[c, d, e], name='p2')

        s = System(
            processes=[p1, p2],
            name='Convergent System',
            initial_values={a: [1, 1]},
        )

        inputs = {a: [[2, 2]], c: [[0]]}
        s.run(inputs=inputs)

        assert set([a, c]) == set(s.origin_mechanisms.mechanisms)
        assert [e] == s.terminal_mechanisms.mechanisms

        assert a.systems[s] == ORIGIN
        assert b.systems[s] == INTERNAL
        assert c.systems[s] == ORIGIN
        assert d.systems[s] == INTERNAL
        assert e.systems[s] == TERMINAL
Example #8
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')[
                abc_system.default_execution_id]['value'],
            [[np.array([0.5])], [np.array([0.75])], [np.array([0.5])],
             [np.array([0.75])], [np.array([0.875])]])
Example #9
0
    def cyclic_extended_loop(self):
        a = TransferMechanism(name='a', default_variable=[0, 0])
        b = TransferMechanism(name='b')
        c = TransferMechanism(name='c')
        d = TransferMechanism(name='d')
        e = TransferMechanism(name='e', default_variable=[0])
        f = TransferMechanism(name='f')

        p1 = Process(pathway=[a, b, c, d], name='p1')
        p2 = Process(pathway=[e, c, f, b, d], name='p2')

        s = System(
            processes=[p1, p2],
            name='Cyclic System with Extended Loop',
            initial_values={a: [1, 1]},
        )

        inputs = {a: [2, 2], e: [0]}
        s.run(inputs=inputs)

        assert set([a, c]) == set(s.origin_mechanisms.mechanisms)
        assert [d] == s.terminal_mechanisms.mechanisms

        assert a.systems[s] == ORIGIN
        assert b.systems[s] == CYCLE
        assert c.systems[s] == INTERNAL
        assert d.systems[s] == TERMINAL
        assert e.systems[s] == ORIGIN
        assert f.systems[s] == INITIALIZE_CYCLE
Example #10
0
    def test_initialize_mechanisms(self):
        A = TransferMechanism(name='A')
        B = TransferMechanism(name='B')
        C = RecurrentTransferMechanism(name='C', auto=1.0)

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

        abc_system = System(processes=[abc_process])

        C.log.set_log_conditions('value')

        abc_system.run(inputs={A: [1.0, 2.0, 3.0]},
                       initial_values={
                           A: 1.0,
                           B: 1.5,
                           C: 2.0
                       },
                       initialize=True)

        abc_system.run(inputs={A: [1.0, 2.0, 3.0]},
                       initial_values={
                           A: 1.0,
                           B: 1.5,
                           C: 2.0
                       },
                       initialize=False)

        # Run 1 --> Execution 1: 1 + 2 = 3    |    Execution 2: 3 + 2 = 5    |    Execution 3: 5 + 3 = 8
        # Run 2 --> Execution 1: 8 + 1 = 9    |    Execution 2: 9 + 2 = 11    |    Execution 3: 11 + 3 = 14
        assert np.allclose(
            C.log.nparray_dictionary('value')[abc_system.default_execution_id]
            ['value'], [[[3]], [[5]], [[8]], [[9]], [[11]], [[14]]])
Example #11
0
    def test_recurrent_mech_change_learning_rate(self):
        R = RecurrentTransferMechanism(size=4,
                                       function=Linear,
                                       enable_learning=True,
                                       learning_rate=0.1
                                       )

        p = Process(pathway=[R])
        s = System(processes=[p])
        assert R.learning_rate == 0.1
        assert R.learning_mechanism.learning_rate == 0.1
        # assert R.learning_mechanism.function.learning_rate == 0.1

        s.run(inputs=[[1.0, 1.0, 1.0, 1.0]])
        matrix_1 = [[0., 1.1, 1.1, 1.1],
                    [1.1, 0., 1.1, 1.1],
                    [1.1, 1.1, 0., 1.1],
                    [1.1, 1.1, 1.1, 0.]]
        assert np.allclose(R.recurrent_projection.mod_matrix, matrix_1)
        print(R.recurrent_projection.mod_matrix)
        R.learning_rate = 0.9

        assert R.learning_rate == 0.9
        assert R.learning_mechanism.learning_rate == 0.9
        # assert R.learning_mechanism.function.learning_rate == 0.9
        s.run(inputs=[[1.0, 1.0, 1.0, 1.0]])
        matrix_2 = [[0., 1.911125, 1.911125, 1.911125],
                    [1.911125, 0., 1.911125, 1.911125],
                    [1.911125, 1.911125, 0., 1.911125],
                    [1.911125, 1.911125, 1.911125, 0.]]
        # assert np.allclose(R.recurrent_projection.mod_matrix, matrix_2)
        print(R.recurrent_projection.mod_matrix)
    def test_dict_of_arrays(self):
        input_labels_dict = {"red": [1.0, 0.0], "green": [0.0, 1.0]}
        output_labels_dict = {"red": [1.0, 0.0], "green": [0.0, 1.0]}
        M = ProcessingMechanism(size=2,
                                params={
                                    INPUT_LABELS_DICT: input_labels_dict,
                                    OUTPUT_LABELS_DICT: output_labels_dict
                                })
        P = Process(pathway=[M])
        S = System(processes=[P])

        store_output_labels = []

        def call_after_trial():
            store_output_labels.append(M.get_output_labels(S))

        S.run(inputs=['red', 'green', 'green', 'red'],
              call_after_trial=call_after_trial)
        assert np.allclose(
            S.results,
            [[[1.0, 0.0]], [[0.0, 1.0]], [[0.0, 1.0]], [[1.0, 0.0]]])
        assert store_output_labels == [['red'], ['green'], ['green'], ['red']]

        store_output_labels = []
        S.run(inputs=[[1.0, 0.0], 'green', [0.0, 1.0], 'red'],
              call_after_trial=call_after_trial)
        assert np.allclose(
            S.results,
            [[[1.0, 0.0]], [[0.0, 1.0]], [[0.0, 1.0]], [[1.0, 0.0]],
             [[1.0, 0.0]], [[0.0, 1.0]], [[0.0, 1.0]], [[1.0, 0.0]]])
        assert store_output_labels == [['red'], ['green'], ['green'], ['red']]
Example #13
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_dict_of_subdicts(self):
        input_labels_dict_M1 = {"red": [1, 1], "green": [0, 0]}
        output_labels_dict_M2 = {0: {"red": [0, 0], "green": [1, 1]}}

        M1 = ProcessingMechanism(
            size=2, params={INPUT_LABELS_DICT: input_labels_dict_M1})
        M2 = ProcessingMechanism(
            size=2, params={OUTPUT_LABELS_DICT: output_labels_dict_M2})
        P = Process(pathway=[M1, M2], learning=ENABLED, learning_rate=0.25)
        S = System(processes=[P])

        learned_matrix = []
        count = []

        def record_matrix_after_trial():
            learned_matrix.append(M2.path_afferents[0].get_mod_matrix(S))
            count.append(1)

        S.run(inputs=['red', 'green', 'green', 'red'],
              targets=['red', 'green', 'green', 'red'],
              call_after_trial=record_matrix_after_trial)

        assert np.allclose(S.results,
                           [[[1, 1]], [[0., 0.]], [[0., 0.]], [[0.5, 0.5]]])
        assert np.allclose(learned_matrix, [
            np.array([[0.75, -0.25], [-0.25, 0.75]]),
            np.array([[0.75, -0.25], [-0.25, 0.75]]),
            np.array([[0.75, -0.25], [-0.25, 0.75]]),
            np.array([[0.625, -0.375], [-0.375, 0.625]])
        ])
    def test_3_input_ports_2_label_dicts(self):
        input_labels_dict = {
            0: {
                "red": [1, 0],
                "green": [0, 1]
            },
            2: {
                "red": [0, 1],
                "green": [1, 0]
            }
        }

        M = TransferMechanism(default_variable=[[0, 0], [0, 0], [0, 0]],
                              params={INPUT_LABELS_DICT: input_labels_dict})
        P = Process(pathway=[M])
        S = System(processes=[P])

        S.run(inputs=[['red', [0, 0], 'green'], ['green', [1, 1], 'red'],
                      ['green', [2, 2], 'green']])
        assert np.allclose(S.results,
                           [[[1, 0], [0, 0], [1, 0]], [[0, 1], [1, 1], [0, 1]],
                            [[0, 1], [2, 2], [1, 0]]])

        S.run(inputs=[['red', [0, 0], [1, 0]], ['green', [1, 1], 'red'],
                      [[0, 1], [2, 2], 'green']])
        assert np.allclose(
            S.results, [[[1, 0], [0, 0], [1, 0]], [[0, 1], [1, 1], [0, 1]],
                        [[0, 1], [2, 2], [1, 0]], [[1, 0], [0, 0], [1, 0]],
                        [[0, 1], [1, 1], [0, 1]], [[0, 1], [2, 2], [1, 0]]])
Example #16
0
    def test_change_termination_condition(self):
        D = DDM(function=DriftDiffusionIntegrator(threshold=10))
        P = Process(pathway=[D])
        S = System(processes=[P])

        D.set_log_conditions(VALUE)

        def change_termination_processing():
            if S.termination_processing is None:
                S.scheduler_processing.termination_conds = {TimeScale.TRIAL: WhenFinished(D)}
                S.termination_processing = {TimeScale.TRIAL: WhenFinished(D)}
            elif isinstance(S.termination_processing[TimeScale.TRIAL], AllHaveRun):
                S.scheduler_processing.termination_conds = {TimeScale.TRIAL: WhenFinished(D)}
                S.termination_processing = {TimeScale.TRIAL: WhenFinished(D)}
            else:
                S.scheduler_processing.termination_conds = {TimeScale.TRIAL: AllHaveRun()}
                S.termination_processing = {TimeScale.TRIAL: AllHaveRun()}

        change_termination_processing()
        S.run(inputs={D: [[1.0], [2.0]]},
              # termination_processing={TimeScale.TRIAL: WhenFinished(D)},
              call_after_trial=change_termination_processing,
              num_trials=4)
        # Trial 0:
        # input = 1.0, termination condition = WhenFinished
        # 10 passes (value = 1.0, 2.0 ... 9.0, 10.0)
        # Trial 1:
        # input = 2.0, termination condition = AllHaveRun
        # 1 pass (value = 2.0)
        expected_results = [[np.array([[10.]]), np.array([[10.]])],
                            [np.array([[2.]]), np.array([[1.]])],
                            [np.array([[10.]]), np.array([[10.]])],
                            [np.array([[2.]]), np.array([[1.]])]]
        assert np.allclose(expected_results, S.results)
Example #17
0
    def test_bypass(self):
        a = TransferMechanism(name='a', default_variable=[0, 0])
        b = TransferMechanism(name='b', default_variable=[0, 0])
        c = TransferMechanism(name='c')
        d = TransferMechanism(name='d')

        p1 = Process(pathway=[a, b, c, d], name='p1')
        p2 = Process(pathway=[a, b, d], name='p2')

        s = System(
            processes=[p1, p2],
            name='Bypass System',
            initial_values={a: [1, 1]},
        )

        inputs = {a: [[2, 2], [0, 0]]}
        s.run(inputs=inputs)

        assert [a] == s.origin_mechanisms.mechanisms
        assert [d] == s.terminal_mechanisms.mechanisms

        assert a.systems[s] == ORIGIN
        assert b.systems[s] == INTERNAL
        assert c.systems[s] == INTERNAL
        assert d.systems[s] == TERMINAL
Example #18
0
    def test_2_target_mechanisms_fn_spec(self):
        A = TransferMechanism(name="learning-process-mech-A")
        B = TransferMechanism(name="learning-process-mech-B")
        C = TransferMechanism(name="learning-process-mech-C")

        LP = Process(name="learning-process", pathway=[A, B], learning=ENABLED)
        LP2 = Process(name="learning-process2",
                      pathway=[A, C],
                      learning=ENABLED)

        S = System(
            name="learning-system",
            processes=[LP, LP2],
        )

        def target_function():
            val_1 = NormalDist(mean=3.0)()
            val_2 = NormalDist(mean=3.0)()
            return [val_1, val_2]

        with pytest.raises(RunError) as error_text:

            S.run(inputs={A: [[[1.0]]]}, targets=target_function)

        assert 'Target values for' in str(error_text.value) and \
               'must be specified in a dictionary' in str(error_text.value)
Example #19
0
    def test_reinitialize_run(self):

        L = LCAMechanism(name="L",
                         function=Linear,
                         initial_value=0.5,
                         integrator_mode=True,
                         leak=0.1,
                         competition=0,
                         self_excitation=1.0,
                         time_step_size=1.0,
                         noise=0.0)
        P = Process(name="P", pathway=[L])
        S = System(name="S", processes=[P])

        L.reinitialize_when = Never()
        assert np.allclose(L.integrator_function.previous_value, 0.5)
        assert np.allclose(L.initial_value, 0.5)
        assert np.allclose(L.integrator_function.initializer, 0.5)

        S.run(inputs={L: 1.0},
              num_trials=2,
              initialize=True,
              initial_values={L: 0.0})

        # IntegratorFunction fn: previous_value + (rate*previous_value + new_value)*time_step_size + noise*(time_step_size**0.5)

        # Trial 1    |   variable = 1.0 + 0.0
        # integration: 0.5 + (0.1*0.5 + 1.0)*1.0 + 0.0 = 1.55
        # linear fn: 1.55*1.0 = 1.55
        # Trial 2    |   variable = 1.0 + 1.55
        # integration: 1.55 + (0.1*1.55 + 2.55)*1.0 + 0.0 = 4.255
        #  linear fn: 4.255*1.0 = 4.255
        assert np.allclose(
            L.integrator_function.parameters.previous_value.get(S), 4.255)

        L.integrator_function.reinitialize(0.9, execution_context=S)

        assert np.allclose(
            L.integrator_function.parameters.previous_value.get(S), 0.9)
        assert np.allclose(L.parameters.value.get(S), 4.255)

        L.reinitialize(0.5, execution_context=S)

        assert np.allclose(
            L.integrator_function.parameters.previous_value.get(S), 0.5)
        assert np.allclose(L.parameters.value.get(S), 0.5)

        S.run(inputs={L: 1.0}, num_trials=2)
        # Trial 3    |   variable = 1.0 + 0.5
        # integration: 0.5 + (0.1*0.5 + 1.5)*1.0 + 0.0 = 2.05
        # linear fn: 2.05*1.0 = 2.05
        # Trial 4    |   variable = 1.0 + 2.05
        # integration: 2.05 + (0.1*2.05 + 3.05)*1.0 + 0.0 = 5.305
        #  linear fn: 5.305*1.0 = 5.305
        assert np.allclose(
            L.integrator_function.parameters.previous_value.get(S), 5.305)
        assert np.allclose(L.initial_value, 0.5)
        assert np.allclose(L.integrator_function.initializer, 0.5)
 def test_udf_system_terminal(self):
     def myFunction(variable, params, context):
         return [variable[0][2], variable[0][0]]
     myMech = ProcessingMechanism(function=myFunction, size=3, name='myMech')
     T2 = TransferMechanism(size=3, function=Linear)
     p2 = Process(pathway=[T2, myMech])
     s2 = System(processes=[p2])
     s2.run(inputs = {T2: [[1, 2, 3]]})
     assert(np.allclose(s2.results[0][0], [3, 1]))
 def test_udf_system_origin(self):
     def myFunction(variable, params, context):
         return [variable[0][1], variable[0][0]]
     myMech = ProcessingMechanism(function=myFunction, size=3, name='myMech')
     T = TransferMechanism(size=2, function=Linear)
     p = Process(pathway=[myMech, T])
     s = System(processes=[p])
     s.run(inputs = {myMech: [[1, 3, 5]]})
     assert np.allclose(s.results[0][0], [3, 1])
Example #22
0
def run_twice_in_system(mech, input1, input2=None):
    if input2 is None:
        input2 = input1
    simple_prefs = {REPORT_OUTPUT_PREF: False, VERBOSE_PREF: False}
    simple_process = Process(size=mech.size[0], pathway=[mech], name='simple_process')
    simple_system = System(processes=[simple_process], name='simple_system', prefs=simple_prefs)

    first_output = simple_system.run(inputs={mech: [input1]})
    second_output = simple_system.run(inputs={mech: [input2]})
    return second_output[1][0]
Example #23
0
    def test_kwta_threshold_float(self):
        K = KWTAMechanism(name='K', size=4, threshold=0.5)
        p = Process(pathway=[K], prefs=TestKWTARatio.simple_prefs)
        s = System(processes=[p], prefs=TestKWTARatio.simple_prefs)

        s.run(inputs={K: [1, 2, 3, 3]})
        assert np.allclose(
            K.parameters.value.get(s),
            [[0.2689414213699951, 0.5, 0.7310585786300049, 0.7310585786300049]
             ])
Example #24
0
    def test_kwta_threshold_int(self):
        K = KWTAMechanism(name='K', size=4, threshold=-1)
        p = Process(pathway=[K], prefs=TestKWTAThreshold.simple_prefs)
        s = System(processes=[p], prefs=TestKWTAThreshold.simple_prefs)

        s.run(inputs={K: [1, 2, 3, 4]})
        assert np.allclose(K.parameters.value.get(s), [[
            0.07585818002124355, 0.18242552380635635, 0.3775406687981454,
            0.6224593312018546
        ]])
Example #25
0
    def test_is_finished_stops_system(self):
        D = DDM(name='DDM', function=DriftDiffusionIntegrator(threshold=10.0))
        P = Process(pathway=[D])
        S = System(processes=[P], reinitialize_mechanisms_when=Never())
        S.run(inputs={D: 2.0},
              termination_processing={TimeScale.TRIAL: WhenFinished(D)})

        # decision variable's value should match threshold
        assert D.parameters.value.get(S)[0] == 10.0
        # it should have taken 5 executions (and time_step_size = 1.0)
        assert D.parameters.value.get(S)[1] == 5.0
Example #26
0
    def test_kwta_k_value_empty_size_4(self):
        K = KWTAMechanism(
            name='K',
            size=4
        )
        assert K.k_value == 0.5
        p = Process(pathway=[K], prefs=TestKWTARatio.simple_prefs)
        s = System(processes=[p], prefs=TestKWTARatio.simple_prefs)

        s.run(inputs={K: [1, 2, 3, 4]})
        assert np.allclose(K.parameters.value.get(s), [[0.18242552380635635, 0.3775406687981454, 0.6224593312018546, 0.8175744761936437]])
Example #27
0
    def test_heterogeneous_variables(self):
        # from psyneulink.core.components.mechanisms.processing.objectivemechanism import ObjectiveMechanism
        a = TransferMechanism(name='a', default_variable=[[0.0], [0.0, 0.0]])

        p1 = Process(pathway=[a])

        s = System(processes=[p1])

        inputs = {a: [[[1.1], [2.1, 2.1]], [[1.2], [2.2, 2.2]]]}

        s.run(inputs)
Example #28
0
 def test_kwta_average_k_1(self):
     K = KWTAMechanism(name='K',
                       size=4,
                       k_value=1,
                       threshold=0,
                       function=Linear,
                       average_based=True)
     p = Process(pathway=[K], prefs=TestKWTAAverageBased.simple_prefs)
     s = System(processes=[p], prefs=TestKWTAAverageBased.simple_prefs)
     kwta_input = {K: [[1, 2, 3, 4]]}
     s.run(inputs=kwta_input)
     assert np.allclose(K.parameters.value.get(s), [[-2, -1, 0, 1]])
Example #29
0
    def test_kwta_ratio_empty(self):
        K = KWTAMechanism(
            name='K',
            size=4
        )
        p = Process(pathway = [K], prefs = TestKWTARatio.simple_prefs)
        s = System(processes=[p], prefs = TestKWTARatio.simple_prefs)

        s.run(inputs = {K: [2, 4, 1, 6]})
        assert np.allclose(K.parameters.value.get(s), [[0.2689414213699951, 0.7310585786300049, 0.11920292202211755, 0.9525741268224334]])
        s.run(inputs = {K: [1, 2, 3, 4]})
        assert np.allclose(K.parameters.value.get(s), [[0.09271329298112314, 0.7368459299092773, 0.2631540700907225, 0.9842837170829899]])
Example #30
0
    def test_dict_target_spec_length2(self):
        A = TransferMechanism(name="learning-process-mech-A")
        B = TransferMechanism(name="learning-process-mech-B",
                              default_variable=[[0.0, 0.0]])

        LP = Process(name="learning-process", pathway=[A, B], learning=ENABLED)

        S = System(name="learning-system", processes=[LP])

        S.run(inputs={A: 1.0}, targets={B: [2.0, 3.0]})

        S.run(inputs={A: 1.0}, targets={B: [[2.0, 3.0]]})