コード例 #1
0
    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].output_values[i])
コード例 #2
0
    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].output_values[i])
コード例 #3
0
    def test_one_run_twice(self):
        A = IntegratorMechanism(
            name='A',
            default_variable=[0],
            function=SimpleIntegrator(
                rate=.5,
            )
        )

        p = Process(
            default_variable=[0],
            pathway=[A],
            name='p'
        )

        s = System(
            processes=[p],
            name='s'
        )

        term_conds = {TimeScale.TRIAL: AfterNCalls(A, 2)}
        stim_list = {A: [[1]]}

        s.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.output_values[i])
コード例 #4
0
    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])
コード例 #5
0
 def test_integrator_type_simple_rate_list(self):
     I = IntegratorMechanism(
         name='IntegratorMechanism',
         default_variable=[0, 0, 0],
         function=SimpleIntegrator(rate=[5.0, 5.0, 5.0]))
     # P = Process(pathway=[I])
     val = list(I.execute([10.0, 10.0, 10.0])[0])
     assert val == [50.0, 50.0, 50.0]
コード例 #6
0
 def test_simple_integrator(self):
     I = IntegratorMechanism(function=SimpleIntegrator(
         initializer=10.0,
         rate=5.0,
         offset=10,
     ))
     # P = Process(pathway=[I])
     val = I.execute(1)
     assert val == 25
コード例 #7
0
 def test_integrator_type_simple_rate_list_input_float(self):
     with pytest.raises(ComponentError) as error_text:
         I = IntegratorMechanism(
             name='IntegratorMechanism',
             function=SimpleIntegrator(rate=[5.0, 5.0, 5.0]))
         # P = Process(pathway=[I])
         float(I.execute(10.0))
     assert ("is not compatible with the variable format" in str(error_text)
             and "to which it is being assigned" in str(error_text))
コード例 #8
0
 def test_integrator_input_list(self):
     I = IntegratorMechanism(
         name='IntegratorMechanism',
         function=SimpleIntegrator(
         )
     )
     # P = Process(pathway=[I])
     val = float(I.execute([10.0]))
     assert val == 10.0
コード例 #9
0
 def test_integrator_type_simple_rate_list_input_float(self):
     with pytest.raises(FunctionError) as error_text:
         I = IntegratorMechanism(
             name='IntegratorMechanism',
             function=SimpleIntegrator(rate=[5.0, 5.0, 5.0]))
         # P = Process(pathway=[I])
         float(I.execute(10.0))
     assert ("array specified for the rate parameter" in str(error_text)
             and "must match the length" in str(error_text)
             and "of the default input" in str(error_text))
コード例 #10
0
 def test_integrator_type_simple_rate_float(self):
     I = IntegratorMechanism(
         name='IntegratorMechanism',
         function=SimpleIntegrator(
             rate=5.0
         )
     )
     # P = Process(pathway=[I])
     val = float(I.execute(10.0))
     assert val == 50.0
コード例 #11
0
    def test_integrator_simple_noise_fn_var_list(self):
        I = IntegratorMechanism(
            name='IntegratorMechanism',
            default_variable=[0, 0, 0, 0],
            function=SimpleIntegrator(noise=NormalDist(), ),
        )

        val = I.execute([10, 10, 10, 10])[0]

        np.testing.assert_allclose(
            val, [10.95008842, 9.84864279, 9.89678115, 10.4105985])
コード例 #12
0
    def test_integrator_simple_noise_fn_var_list(self):
        I = IntegratorMechanism(name='IntegratorMechanism',
                                default_variable=[0, 0, 0, 0],
                                function=SimpleIntegrator(
                                    noise=NormalDist().function,
                                    default_variable=[0, 0, 0, 0]),
                                time_scale=TimeScale.TIME_STEP)

        val = I.execute([10, 10, 10, 10])[0]

        np.testing.assert_allclose(
            val, [10.12167502, 10.44386323, 10.33367433, 11.49407907])
コード例 #13
0
    def test_integrator_simple_noise_fn_var_list(self):
        I = IntegratorMechanism(
            name='IntegratorMechanism',
            default_variable=[0, 0, 0, 0],
            function=SimpleIntegrator(
                noise=NormalDist(),
            ),
        )

        val = I.execute([10, 10, 10, 10])[0]

        np.testing.assert_allclose(val, [10.14404357, 11.45427351, 10.76103773, 10.12167502])
コード例 #14
0
    def test_integrator_simple_noise_fn(self):
        I = IntegratorMechanism(
            name='IntegratorMechanism',
            function=SimpleIntegrator(noise=NormalDist()),
        )

        val = float(I.execute(10))

        I.function_object.reinitialize(5.0)

        val2 = float(I.execute(0))

        np.testing.assert_allclose(val, 12.240893199201459)
        np.testing.assert_allclose(val2, 6.867557990149967)
コード例 #15
0
    def test_integrator_simple_noise_fn(self):
        I = IntegratorMechanism(
            name='IntegratorMechanism',
            function=SimpleIntegrator(noise=NormalDist().function),
            time_scale=TimeScale.TIME_STEP)

        val = float(I.execute(10))

        I.function_object.reset_initializer = 5.0

        val2 = float(I.execute(0))

        np.testing.assert_allclose(val, 11.86755799)
        np.testing.assert_allclose(val2, 4.022722120123589)
コード例 #16
0
    def test_integrator_input_list_len_5(self):

        I = IntegratorMechanism(
            name='IntegratorMechanism',
            default_variable=[0, 0, 0, 0, 0],
            function=SimpleIntegrator(
            )
        )
        # P = Process(pathway=[I])
        val = I.execute([10.0, 5.0, 2.0, 1.0, 0.0])[0]
        expected_output = [10.0, 5.0, 2.0, 1.0, 0.0]

        for i in range(len(expected_output)):
            v = val[i]
            e = expected_output[i]
            np.testing.assert_allclose(v, e, atol=1e-08, err_msg='Failed on expected_output[{0}]'.format(i))
コード例 #17
0
    def test_integrator_simple_with_reset_intializer(self):
        I = IntegratorMechanism(name='IntegratorMechanism',
                                function=SimpleIntegrator(),
                                time_scale=TimeScale.TIME_STEP)
        #     # P = Process(pathway=[I])

        #  returns previous_value + rate*variable + noise
        # so in this case, returns 10.0
        val = float(I.execute(10))

        # testing initializer
        I.function_object.reset_initializer = 5.0

        val2 = float(I.execute(0))

        assert [val, val2] == [10.0, 5.0]
コード例 #18
0
    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),
        )

        p = Process(default_variable=[0], pathway=[A, B], name='p')

        s = System(processes=[p],
                   name='s',
                   reinitialize_mechanisms_when=Never())
        term_conds = {TimeScale.TRIAL: AfterNCalls(B, 2)}
        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)

        # 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.output_values[i])

        s.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.output_values[i])
コード例 #19
0
    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])
コード例 #20
0
    def test_Simple_valid(self):
        I = IntegratorMechanism(
            name='IntegratorMechanism',
            function=SimpleIntegrator(
            ),
        )
        I.reinitialize_when = Never()

        #  returns previous_value + rate*variable + noise
        # so in this case, returns 10.0
        I.execute(10)
        assert np.allclose(I.value, 10.0)
        assert np.allclose(I.output_state.value, 10.0)

        # reinitialize function
        I.function_object.reinitialize(5.0)
        assert np.allclose(I.function_object.value, 5.0)
        assert np.allclose(I.value, 10.0)
        assert np.allclose(I.output_states[0].value, 10.0)

        # reinitialize function without value spec
        I.function_object.reinitialize()
        assert np.allclose(I.function_object.value, 0.0)
        assert np.allclose(I.value, 10.0)
        assert np.allclose(I.output_states[0].value, 10.0)

        # reinitialize mechanism
        I.reinitialize(4.0)
        assert np.allclose(I.function_object.value, 4.0)
        assert np.allclose(I.value, 4.0)
        assert np.allclose(I.output_states[0].value, 4.0)

        I.execute(1)
        assert np.allclose(I.value, 5.0)
        assert np.allclose(I.output_states[0].value, 5.0)

        # reinitialize mechanism without value spec
        I.reinitialize()
        assert np.allclose(I.function_object.value, 0.0)
        assert np.allclose(I.value, 0.0)
        assert np.allclose(I.output_states[0].value, 0.0)
コード例 #21
0
    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))

        p = [
            Process(default_variable=[0], pathway=[A, C, E], name='p'),
            Process(default_variable=[0], pathway=[A, C, F], name='p1'),
            Process(default_variable=[0], pathway=[A, D, E], name='p2'),
            Process(default_variable=[0], pathway=[A, D, F], name='p3'),
            Process(default_variable=[0], pathway=[B, C, E], name='q'),
            Process(default_variable=[0], pathway=[B, C, F], name='q1'),
            Process(default_variable=[0], pathway=[B, D, E], name='q2'),
            Process(default_variable=[0], pathway=[B, D, F], name='q3')
        ]

        s = System(processes=p, name='s')

        term_conds = {
            TimeScale.TRIAL: All(AfterNCalls(E, 1), AfterNCalls(F, 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))
        sched.add_condition(E, EveryNCalls(C, 1))
        sched.add_condition(F, EveryNCalls(D, 2))
        s.scheduler_processing = sched

        s.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.output_values[i])