Example #1
0
def test_DDMMechanism_LCA_equivalent(mode):
    ddm = DDM(default_variable=[0], function=DriftDiffusionIntegrator(rate=1, time_step_size=0.1))
    comp2 = Composition()
    comp2.add_node(ddm)
    result2 = comp2.run(inputs={ddm:[1]}, bin_execute=mode)
    assert np.allclose(np.asfarray(result2[0]), [0.1])
    assert np.allclose(np.asfarray(result2[1]), [0.1])
Example #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]])])
Example #3
0
    def test_threshold_param(self):
        D = DDM(name='DDM', function=DriftDiffusionIntegrator(threshold=10.0))

        assert D.function.threshold == 10.0

        D.function.threshold = 5.0
        assert D.function.threshold == 5.0
Example #4
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)
def test_DDM_noise_2_0():
    stim = 10
    T = DDM(name='DDM',
            function=DriftDiffusionIntegrator(noise=2.0,
                                              rate=1.0,
                                              time_step_size=1.0))
    val = float(T.execute(stim)[0])
    assert val == 11.34362792551828
Example #6
0
def test_DDM_input(stim):
    T = DDM(
        name='DDM',
        function=DriftDiffusionIntegrator(noise=0.0,
                                          rate=1.0,
                                          time_step_size=1.0),
    )
    val = float(T.execute(stim)[0])
    assert val == 10
Example #7
0
def test_DDM_size_int_check_var():
    T = DDM(
        name='DDM',
        size=1,
        function=DriftDiffusionIntegrator(noise=0.0,
                                          rate=-5.0,
                                          time_step_size=1.0),
    )
    assert len(T.defaults.variable) == 1 and T.defaults.variable[0][0] == 0
Example #8
0
    def test_is_finished_stops_composition(self):
        D = DDM(name='DDM', function=DriftDiffusionIntegrator(threshold=10.0))
        C = Composition(pathways=[D], reset_stateful_function_when=Never())
        C.run(inputs={D: 2.0},
              termination_processing={TimeScale.TRIAL: WhenFinished(D)})

        # decision variable's value should match threshold
        assert D.parameters.value.get(C)[0] == 10.0
        # it should have taken 5 executions (and time_step_size = 1.0)
        assert D.parameters.value.get(C)[1] == 5.0
def test_DDM_rate_list_len_1():
    stim = 10
    T = DDM(
        name='DDM',
        function=DriftDiffusionIntegrator(noise=0.0,
                                          rate=[5],
                                          time_step_size=1.0),
    )
    val = float(T.execute(stim)[0])
    assert val == 50
def test_DDM_noise_0_5():
    stim = 10
    T = DDM(name='DDM',
            function=DriftDiffusionIntegrator(noise=0.5,
                                              rate=1.0,
                                              time_step_size=1.0))

    val = float(T.execute(stim)[0])

    assert val == 10.67181396275914
Example #11
0
    def test_threshold_sets_is_finished(self):
        D = DDM(name='DDM', function=DriftDiffusionIntegrator(threshold=5.0))
        D.execute(2.0)  # 2.0 < 5.0
        assert not D.is_finished()

        D.execute(2.0)  # 4.0 < 5.0
        assert not D.is_finished()

        D.execute(2.0)  # 5.0 = threshold
        assert D.is_finished()
Example #12
0
def test_ddm_is_finished(mode, noise, threshold, expected_results):
    comp = Composition()
    ddm = DDM(execute_until_finished=True,
                function=DriftDiffusionIntegrator(threshold=threshold, noise=noise))
    comp.add_node(ddm)

    results = comp.run([0], bin_execute=mode)

    results = [x for x in np.array(results).flatten()] #HACK: The result is an object dtype in Python mode for some reason?
    assert np.allclose(results, np.array(expected_results).flatten())
Example #13
0
def test_DDM_size_too_large():
    with pytest.raises(DDMError) as error_text:
        T = DDM(
            name='DDM',
            size=3.0,
            function=DriftDiffusionIntegrator(noise=0.0,
                                              rate=-5.0,
                                              time_step_size=1.0),
        )
    assert "single numeric item" in str(error_text.value)
Example #14
0
def test_DDM_mech_size_negative_one():
    with pytest.raises(ComponentError) as error_text:
        T = DDM(
            name='DDM',
            size=-1.0,
            function=DriftDiffusionIntegrator(noise=0.0,
                                              rate=-5.0,
                                              time_step_size=1.0),
        )
    assert "is not a positive number" in str(error_text.value)
Example #15
0
def test_DDM_size_too_long():
    with pytest.raises(DDMError) as error_text:
        T = DDM(
            name='DDM',
            size=[1, 1],
            function=DriftDiffusionIntegrator(noise=0.0,
                                              rate=-5.0,
                                              time_step_size=1.0),
        )
    assert "is greater than 1, implying there are" in str(error_text.value)
def test_DDM_input_rate_negative():
    stim = [10]
    T = DDM(
        name='DDM',
        default_variable=[0],
        function=DriftDiffusionIntegrator(noise=0.0,
                                          rate=-5.0,
                                          time_step_size=1.0),
    )
    val = float(T.execute(stim)[0])
    assert val == -50
Example #17
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 #18
0
def test_DDM_input_fn():
    with pytest.raises(TypeError) as error_text:
        stim = NormalDist()
        T = DDM(
            name='DDM',
            function=DriftDiffusionIntegrator(noise=0.0,
                                              rate=1.0,
                                              time_step_size=1.0),
        )
        float(T.execute(stim))
    assert "not supported for the input types" in str(error_text.value)
Example #19
0
def test_DDM_input_list_len_2():
    with pytest.raises(DDMError) as error_text:
        stim = [10, 10]
        T = DDM(
            name='DDM',
            default_variable=[0, 0],
            function=DriftDiffusionIntegrator(noise=0.0,
                                              rate=1.0,
                                              time_step_size=1.0),
        )
        float(T.execute(stim)[0])
    assert "single numeric item" in str(error_text.value)
Example #20
0
def test_DDM_noise_invalid(noise):
    with pytest.raises(FunctionError) as error_text:
        stim = 10
        T = DDM(
            name='DDM',
            function=DriftDiffusionIntegrator(noise=noise,
                                              rate=1.0,
                                              time_step_size=1.0),
        )
        float(T.execute(stim)[0])
    assert "DriftDiffusionIntegrator requires noise parameter to be a float" in str(
        error_text.value)
Example #21
0
def test_DDM_rate_fn():
    with pytest.raises(typecheck.framework.InputParameterError) as error_text:
        stim = [10]
        T = DDM(
            name='DDM',
            default_variable=[0],
            function=DriftDiffusionIntegrator(noise=0.0,
                                              rate=NormalDist().function,
                                              time_step_size=1.0),
        )
        float(T.execute(stim)[0])
    assert "incompatible value" in str(error_text.value)
    def test_threshold_stops_accumulation_negative(self):
        D = DDM(name='DDM', function=DriftDiffusionIntegrator(threshold=5.0))
        decision_variables = []
        time_points = []
        for i in range(5):
            output = D.execute(-2.0)
            decision_variables.append(output[0][0][0])
            time_points.append(output[1][0][0])

        # decision variable accumulation stops
        assert np.allclose(decision_variables, [-2.0, -4.0, -5.0, -5.0, -5.0])

        # time accumulation does not stop
        assert np.allclose(time_points, [1.0, 2.0, 3.0, 4.0, 5.0])
Example #23
0
def test_DDM_size_int_inputs():

    T = DDM(
        name='DDM',
        size=1,
        function=DriftDiffusionIntegrator(noise=0.0,
                                          rate=-5.0,
                                          time_step_size=1.0),
    )
    val = T.execute([.4])
    decision_variable = val[0][0]
    time = val[1][0]
    assert decision_variable == -2.0
    assert time == 1.0
Example #24
0
def test_DDM_noise(mode, benchmark, noise, expected):
    T = DDM(name='DDM',
            function=DriftDiffusionIntegrator(noise=0.5,
                                              rate=1.0,
                                              time_step_size=1.0))
    if mode == "Python":
        ex = T.execute
    elif mode == "LLVM":
        ex = pnlvm.execution.MechExecution(T).execute
    elif mode == "PTX":
        ex = pnlvm.execution.MechExecution(T).cuda_execute

    val = ex([10])
    assert np.allclose(val[0][0][0], 8.194383551861414)
    benchmark(ex, [10])
Example #25
0
def test_DDM_noise(mode, benchmark, noise, expected):
    T = DDM(name='DDM',
            function=DriftDiffusionIntegrator(noise=noise,
                                              rate=1.0,
                                              time_step_size=1.0))
    if mode == 'Python':
        ex = T.execute
    elif mode == 'LLVM':
        ex = pnlvm.execution.MechExecution(T).execute
    elif mode == 'PTX':
        ex = pnlvm.execution.MechExecution(T).cuda_execute

    val = ex([10])
    assert np.allclose(val[0][0][0], expected)
    if benchmark.enabled:
        benchmark(ex, [10])
Example #26
0
def test_DDM_rate(benchmark, rate, expected, mode):
    stim = [10]
    T = DDM(
        name='DDM',
        function=DriftDiffusionIntegrator(noise=0.0,
                                          rate=rate,
                                          time_step_size=1.0),
    )
    if mode == "Python":
        ex = T.execute
    elif mode == "LLVM":
        ex = pnlvm.execution.MechExecution(T).execute
    elif mode == "PTX":
        ex = pnlvm.execution.MechExecution(T).cuda_execute
    val = float(ex(stim)[0][0][0])
    assert val == expected
    benchmark(ex, stim)
Example #27
0
def test_DDM_time():

    D = DDM(name='DDM',
            function=DriftDiffusionIntegrator(noise=0.0,
                                              rate=-5.0,
                                              time_step_size=0.2,
                                              starting_point=0.5))

    time_0 = D.function.previous_time  # t_0  = 0.5
    np.testing.assert_allclose(time_0, 0.5, atol=1e-08)

    time_1 = D.execute(10)[1][0]  # t_1  = 0.5 + 0.2 = 0.7
    np.testing.assert_allclose(time_1[0], 0.7, atol=1e-08)

    for i in range(10):  # t_11 = 0.7 + 10*0.2 = 2.7
        D.execute(10)
    time_12 = D.execute(10)[1][0]  # t_12 = 2.7 + 0.2 = 2.9
    np.testing.assert_allclose(time_12[0], 2.9, atol=1e-08)
Example #28
0
    def test_threshold_stops_accumulation(self, mode, variable, expected,
                                          benchmark):
        D = DDM(name='DDM', function=DriftDiffusionIntegrator(threshold=5.0))
        if mode == "Python":
            ex = D.execute
        elif mode == "LLVM":
            ex = pnlvm.execution.MechExecution(D).execute
        elif mode == "PTX":
            ex = pnlvm.execution.MechExecution(D).cuda_execute

        decision_variables = []
        time_points = []
        for i in range(5):
            output = ex([variable])
            decision_variables.append(output[0][0][0])
            time_points.append(output[1][0][0])

        # decision variable accumulation stops
        assert np.allclose(decision_variables, expected)

        # time accumulation does not stop
        assert np.allclose(time_points, [1.0, 2.0, 3.0, 4.0, 5.0])
        benchmark(ex, [variable])
Example #29
0
    def test_valid(self):
        D = DDM(
            name='DDM',
            function=DriftDiffusionIntegrator(seed=0),
        )

        #  returns previous_value + rate * variable * time_step_size  + noise
        #  0.0 + 1.0 * 1.0 * 1.0 + 0.0
        D.execute(1.0)
        assert np.allclose(np.asfarray(D.value), [[1.0], [1.0]])
        assert np.allclose(D.output_ports[0].value[0][0], 1.0)
        assert np.allclose(D.output_ports[1].value[0][0], 1.0)

        # reinitialize function
        D.function.reinitialize(2.0, 0.1, 0)
        assert np.allclose(D.function.value[0], 2.0)
        assert np.allclose(D.function.previous_value, 2.0)
        assert np.allclose(D.function.previous_time, 0.1)
        assert np.allclose(np.asfarray(D.value), [[1.0], [1.0]])
        assert np.allclose(D.output_ports[0].value[0][0], 1.0)
        assert np.allclose(D.output_ports[1].value[0][0], 1.0)

        # reinitialize function without value spec
        D.function.reinitialize()
        assert np.allclose(D.function.value[0], 0.0)
        assert np.allclose(D.function.previous_value, 0.0)
        assert np.allclose(D.function.previous_time, 0.0)
        assert np.allclose(np.asfarray(D.value), [[1.0], [1.0]])
        assert np.allclose(D.output_ports[0].value[0][0], 1.0)
        assert np.allclose(D.output_ports[1].value[0][0], 1.0)

        # reinitialize mechanism
        D.reinitialize(2.0, 0.1, 0)
        assert np.allclose(D.function.value[0], 2.0)
        assert np.allclose(D.function.previous_value, 2.0)
        assert np.allclose(D.function.previous_time, 0.1)
        assert np.allclose(np.asfarray(D.value), [[2.0], [0.1]])
        assert np.allclose(D.output_ports[0].value, 2.0)
        assert np.allclose(D.output_ports[1].value, 0.1)

        D.execute(1.0)
        #  2.0 + 1.0 = 3.0 ; 0.1 + 1.0 = 1.1
        assert np.allclose(np.asfarray(D.value), [[[3.0]], [[1.1]]])
        assert np.allclose(D.output_ports[0].value[0][0], 3.0)
        assert np.allclose(D.output_ports[1].value[0][0], 1.1)

        # reinitialize mechanism without value spec
        D.reinitialize()
        assert np.allclose(D.function.value[0], 0.0)
        assert np.allclose(D.function.previous_value, 0.0)
        assert np.allclose(D.function.previous_time, 0.0)
        assert np.allclose(D.output_ports[0].value[0], 0.0)
        assert np.allclose(D.output_ports[1].value[0], 0.0)

        # reinitialize only decision variable
        D.function.initializer = 1.0
        D.function.starting_point = 0.0
        D.reinitialize()
        assert np.allclose(D.function.value[0], 1.0)
        assert np.allclose(D.function.previous_value, 1.0)
        assert np.allclose(D.function.previous_time, 0.0)
        assert np.allclose(D.output_ports[0].value[0], 1.0)
        assert np.allclose(D.output_ports[1].value[0], 0.0)
Example #30
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]])
        ])