예제 #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].get_output_values(s)[i])
예제 #2
0
    def test_reset_state_integrator_mechanism(self):
        A = IntegratorMechanism(name='A', function=DriftDiffusionIntegrator())

        # Execute A twice
        #  [0] saves decision variable only (not time)
        original_output = [A.execute(1.0)[0], A.execute(1.0)[0]]

        # SAVING STATE  - - - - - - - - - - - - - - - - - - - - - - - - -
        reinitialize_values = []
        for attr in A.function.stateful_attributes:
            reinitialize_values.append(getattr(A.function, attr))

        # Execute A twice AFTER saving the state so that it continues accumulating.
        # We expect the next two outputs to repeat once we reset the state b/c we will return it to the current state
        output_after_saving_state = [A.execute(1.0)[0], A.execute(1.0)[0]]

        # RESETTING STATE - - - - - - - - - - - - - - - - - - - - - - - -
        A.reinitialize(*reinitialize_values)

        # We expect these results to match the results from immediately after saving the state
        output_after_reinitialization = [A.execute(1.0)[0], A.execute(1.0)[0]]

        assert np.allclose(output_after_saving_state,
                           output_after_reinitialization)
        assert np.allclose(
            original_output,
            [np.array([[1.0]]), np.array([[2.0]])])
        assert np.allclose(
            output_after_reinitialization,
            [np.array([[3.0]]), np.array([[4.0]])])
예제 #3
0
    def test_fitzHughNagumo_gilzenrat_figure_2(self):
        # Isolate the FitzHughNagumo mechanism for testing and recreate figure 2 from the gilzenrat paper

        initial_v = 0.2
        initial_w = 0.0

        F = IntegratorMechanism(name='IntegratorMech-FitzHughNagumoFunction',
                                function=FitzHughNagumoIntegrator(
                                    initial_v=initial_v,
                                    initial_w=initial_w,
                                    time_step_size=0.01,
                                    time_constant_w=1.0,
                                    time_constant_v=0.01,
                                    a_v=-1.0,
                                    b_v=1.0,
                                    c_v=1.0,
                                    d_v=0.0,
                                    e_v=-1.0,
                                    f_v=1.0,
                                    threshold=0.5,
                                    mode=1.0,
                                    uncorrelated_activity=0.0,
                                    a_w=1.0,
                                    b_w=-1.0,
                                    c_w=0.0))
        plot_v_list = [initial_v]
        plot_w_list = [initial_w]

        # found this stimulus by guess and check b/c one was not provided with Figure 2 params
        stimulus = 0.073
        # increase range to 200 to match Figure 2 in Gilzenrat
        for i in range(10):
            results = F.execute(stimulus)
            plot_v_list.append(results[0][0][0])
            plot_w_list.append(results[1][0][0])

        # ** uncomment the lines below if you want to view the plot:
        # from matplotlib import pyplot as plt
        # plt.plot(plot_v_list)
        # plt.plot(plot_w_list)
        # plt.show()

        np.testing.assert_allclose(plot_v_list, [
            0.2, 0.22493312915681499, 0.24840327807265583, 0.27101619694032797,
            0.29325863380332173, 0.31556552465130933, 0.33836727470568129,
            0.36212868305470697, 0.38738542852040492, 0.41478016676749552,
            0.44509530539552955
        ])
        print(plot_w_list)
        np.testing.assert_allclose(plot_w_list, [
            0.0, 0.0019900332500000003, 0.0042083541185625045,
            0.0066381342093118408, 0.009268739886338381, 0.012094486544132229,
            0.015114073825358726, 0.018330496914962583, 0.021751346023501487,
            0.025389465931011893, 0.029263968140538919
        ])
예제 #4
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))

        c = Composition(pathways=[[A, C], [A, D], [B, C], [B, D]])

        term_conds = {
            TimeScale.TRIAL: All(AfterNCalls(C, 1), AfterNCalls(D, 1))
        }
        stim_list = {A: [[1]], B: [[1]]}

        sched = Scheduler(composition=c)
        sched.add_condition(B, EveryNCalls(A, 2))
        sched.add_condition(C, EveryNCalls(A, 1))
        sched.add_condition(D, EveryNCalls(B, 1))
        c.scheduler = sched

        c.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(c)[i])
예제 #5
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].get_output_values(s)[i])
예제 #6
0
    def test_two_AAB(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 = Composition(pathways=[A, B])

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

        sched = Scheduler(composition=c)
        sched.add_condition(B, EveryNCalls(A, 2))
        c.scheduler = sched

        c.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(c)[i])
예제 #7
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.get_output_values(s)[i])
예제 #8
0
    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),
        )

        c = Composition(pathways=[[A, C, D], [B, C, E]])

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

        sched = Scheduler(composition=c)
        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))
        c.scheduler = sched

        c.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].get_output_values(c)[i])
예제 #9
0
    def test_partial_override_composition(self):
        comp = Composition()
        A = TransferMechanism(name='scheduler-pytests-A')
        B = IntegratorMechanism(name='scheduler-pytests-B')
        for m in [A, B]:
            comp.add_node(m)
        comp.add_projection(MappingProjection(), A, B)

        termination_conds = {TimeScale.TRIAL: AfterNCalls(B, 2)}

        output = comp.run(inputs={A: 1}, termination_processing=termination_conds)
        # two executions of B
        assert output == [.75]
예제 #10
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.get_output_values(s)[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.get_output_values(s)[i])
예제 #11
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].get_output_values(s)[i])
예제 #12
0
    def test_one_run_twice(self):
        A = IntegratorMechanism(name='A',
                                default_variable=[0],
                                function=SimpleIntegrator(rate=.5, ))

        c = Composition(pathways=[A])

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

        c.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.get_output_values(c)[i])
예제 #13
0
def test_debug_comp(mode, debug_env):
    # save old debug env var
    old_env = os.environ.get("PNL_LLVM_DEBUG")
    if debug_env is not None:
        os.environ["PNL_LLVM_DEBUG"] = debug_env
        pnlvm.debug._update()

    comp = Composition()
    A = IntegratorMechanism(default_variable=1.0, function=Linear(slope=5.0))
    B = TransferMechanism(function=Linear(slope=5.0), integrator_mode=True)
    comp.add_linear_processing_pathway([A, B])

    inputs_dict = {A: [5]}
    output1 = comp.run(inputs=inputs_dict, execution_mode=mode)
    output2 = comp.run(inputs=inputs_dict, execution_mode=mode)
    # restore old debug env var and cleanup the debug configuration
    if old_env is None:
        del os.environ["PNL_LLVM_DEBUG"]
    else:
        os.environ["PNL_LLVM_DEBUG"] = old_env
    pnlvm.debug._update()

    assert len(comp.results) == 2

    if "const_input=" in debug_env:
        expected1 = 87.5
        expected2 = 131.25
    elif "const_input" in debug_env:
        expected1 = 12.5
        expected2 = 18.75
    else:
        expected1 = 62.5
        expected2 = 93.75

    if "const_state" in debug_env:
        expected2 = expected1

    assert np.allclose(expected1, output1[0][0])
    assert np.allclose(expected2, output2[0][0])
def test_debug_comp(mode, debug_env):
    # save old debug env var
    old_env = os.environ.get("PNL_LLVM_DEBUG")
    if debug_env is not None:
        os.environ["PNL_LLVM_DEBUG"] = debug_env
        pnlvm.debug._update()

    comp = Composition()
    A = IntegratorMechanism(default_variable=1.0, function=Linear(slope=5.0))
    B = TransferMechanism(function=Linear(slope=5.0), integrator_mode=True)
    comp.add_node(A)
    comp.add_node(B)
    comp.add_projection(MappingProjection(sender=A, receiver=B), A, B)
    sched = Scheduler(composition=comp)

    inputs_dict = {A: [5]}
    output1 = comp.run(inputs=inputs_dict, scheduler=sched, bin_execute=mode)
    output2 = comp.run(inputs=inputs_dict, scheduler=sched, bin_execute=mode)
    # restore old debug env var and cleanup the debug configuration
    if old_env is None:
        del os.environ["PNL_LLVM_DEBUG"]
    else:
        os.environ["PNL_LLVM_DEBUG"] = old_env
    pnlvm.debug._update()

    assert len(comp.results) == 2

    if "const_input" in debug_env:
        expected1 = 87.5
        expected2 = 131.25
    else:
        expected1 = 62.5
        expected2 = 93.75

    if "const_state" in debug_env:
        expected2 = expected1

    assert np.allclose(expected1, output1[0][0])
    assert np.allclose(expected2, output2[0][0])
예제 #15
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))

        c = Composition(pathways=[[A, C, E], [A, C, F], [A, D, E], [A, D, F],
                                  [B, C, E], [B, C, F], [B, D, E], [B, D, F]])

        term_conds = {
            TimeScale.TRIAL: All(AfterNCalls(E, 1), AfterNCalls(F, 1))
        }
        stim_list = {A: [[1]], B: [[1]]}

        sched = Scheduler(composition=c)
        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))
        c.scheduler = sched

        c.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.get_output_values(c)[i])
예제 #16
0
    def test_save_state_before_simulations(self):

        A = TransferMechanism(name='A',
                              integrator_mode=True,
                              integration_rate=0.2)

        B = IntegratorMechanism(name='B',
                                function=DriftDiffusionIntegrator(rate=0.1))
        C = TransferMechanism(name='C')

        P = Process(pathway=[A, B, C])
        S = System(processes=[P], reinitialize_mechanisms_when=Never())

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

        run_1_values = [
            A.parameters.value.get(S),
            B.parameters.value.get(S)[0],
            C.parameters.value.get(S)
        ]

        # "Save state" code from EVCaux

        # Get any values that need to be reinitialized for each run
        reinitialization_values = {}
        for mechanism in S.stateful_mechanisms:
            # "save" the current state of each stateful mechanism by storing the values of each of its stateful
            # attributes in the reinitialization_values dictionary; this gets passed into run and used to call
            # the reinitialize method on each stateful mechanism.
            reinitialization_value = []

            if isinstance(mechanism.function, IntegratorFunction):
                for attr in mechanism.function.stateful_attributes:
                    reinitialization_value.append(
                        getattr(mechanism.function.parameters, attr).get(S))
            elif hasattr(mechanism, "integrator_function"):
                if isinstance(mechanism.integrator_function,
                              IntegratorFunction):
                    for attr in mechanism.integrator_function.stateful_attributes:
                        reinitialization_value.append(
                            getattr(mechanism.integrator_function.parameters,
                                    attr).get(S))

            reinitialization_values[mechanism] = reinitialization_value

        # Allow values to continue accumulating so that we can set them back to the saved state
        S.run(inputs={A: [[1.0], [1.0]]})

        run_2_values = [
            A.parameters.value.get(S),
            B.parameters.value.get(S)[0],
            C.parameters.value.get(S)
        ]

        S.run(inputs={A: [[1.0], [1.0]]},
              reinitialize_values=reinitialization_values)

        run_3_values = [
            A.parameters.value.get(S),
            B.parameters.value.get(S)[0],
            C.parameters.value.get(S)
        ]

        assert np.allclose(run_2_values, run_3_values)
        assert np.allclose(
            run_1_values,
            [np.array([[0.36]]),
             np.array([[0.056]]),
             np.array([[0.056]])])
        assert np.allclose(run_2_values, [
            np.array([[0.5904]]),
            np.array([[0.16384]]),
            np.array([[0.16384]])
        ])