Beispiel #1
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
Beispiel #3
0
 def test_selected_input_array(self):
     action_selection = DDM(
         input_format=ARRAY,
         function=DriftDiffusionAnalytical(
         ),
         output_ports=[SELECTED_INPUT_ARRAY],
         name='DDM'
     )
     action_selection.execute([1.0])
Beispiel #4
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
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
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
Beispiel #7
0
def test_DDM():
    myMechanism = DDM(
        function=DriftDiffusionAnalytical(
            drift_rate=(1.0),
            threshold=(10.0),
            starting_point=0.0,
        ),
        name='My_DDM',
    )

    myMechanism_2 = DDM(function=DriftDiffusionAnalytical(drift_rate=2.0,
                                                          threshold=20.0),
                        name='My_DDM_2')

    myMechanism_3 = DDM(
        function=DriftDiffusionAnalytical(drift_rate=3.0, threshold=30.0),
        name='My_DDM_3',
    )

    z = Process(
        default_variable=[[30], [10]],
        pathway=[
            myMechanism, (IDENTITY_MATRIX), myMechanism_2,
            (FULL_CONNECTIVITY_MATRIX), myMechanism_3
        ],
    )

    result = z.execute([[30], [10]])

    expected_output = [
        (myMechanism.input_states[0].parameters.value.get(z), np.array([40.])),
        (myMechanism.output_states[0].parameters.value.get(z), np.array([10.
                                                                         ])),
        (myMechanism_2.input_states[0].parameters.value.get(z), np.array([10.
                                                                          ])),
        (myMechanism_2.output_states[0].parameters.value.get(z),
         np.array([20.])),
        (myMechanism_3.input_states[0].parameters.value.get(z), np.array([20.
                                                                          ])),
        (myMechanism_3.output_states[0].parameters.value.get(z),
         np.array([30.])),
        (result, np.array([30.])),
    ]

    for i in range(len(expected_output)):
        val, expected = expected_output[i]
        # setting absolute tolerance to be in accordance with reference_output precision
        # if you do not specify, assert_allcose will use a relative tolerance of 1e-07,
        # which WILL FAIL unless you gather higher precision values to use as reference
        np.testing.assert_allclose(
            val,
            expected,
            atol=1e-08,
            err_msg='Failed on expected_output[{0}]'.format(i))
Beispiel #8
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)
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
Beispiel #10
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)
Beispiel #11
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)
Beispiel #12
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)
    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])
Beispiel #14
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
Beispiel #15
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])
Beispiel #16
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
def test_DDM_Integrator_Bogacz(benchmark, mech_mode):
    stim = 10
    T = DDM(name='DDM', function=DriftDiffusionAnalytical())
    ex = pytest.helpers.get_mech_execution(T, mech_mode)

    val = ex(stim)[0]
    assert np.allclose(val, [1.0])
    if benchmark.enabled:
        benchmark(ex, stim)
Beispiel #18
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
Beispiel #19
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)
Beispiel #20
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)
Beispiel #21
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)
    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
Beispiel #23
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())
Beispiel #24
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
def test_DDM_noise(mech_mode, benchmark, noise, expected):
    T = DDM(name='DDM',
            function=DriftDiffusionIntegrator(noise=noise,
                                              rate=1.0,
                                              time_step_size=1.0))
    ex = pytest.helpers.get_mech_execution(T, mech_mode)

    val = ex([10])
    assert np.allclose(val[0][0], expected)
    if benchmark.enabled:
        benchmark(ex, [10])
Beispiel #26
0
def test_drift_difussion_analytical_shenhav_compat_mode():

    # Create a DriftDiffusionAnalytical Function, make sure to set shenav_et_al_compat_mode=True to get exact behavior
    # of old MATLAB code (Matlab/DDMFunctions/ddmSimFRG.m)
    B = DDM(name='DDM',
            function=DriftDiffusionAnalytical(shenhav_et_al_compat_mode=True))

    # Load a CSV containing random sampled test values
    data = np.loadtxt(
        os.path.join(__location__, 'matlab_ddm_code_ground_truth.csv'))

    check_drift_diffusion_analytical(B, data, degenerate_cases=True)
Beispiel #27
0
def test_drift_difussion_analytical():

    # Create a DriftDiffusionAnalytical Function, setting shenav_et_al_compat_mode=False (the defualt) should only
    # really change degenerate input cases, this test tests non-degenerate cases.
    B = DDM(name='DDM', function=DriftDiffusionAnalytical())

    # Load a CSV containing random sampled test values
    data = np.loadtxt(
        os.path.join(__location__,
                     'matlab_ddm_code_ground_truth_non_degenerate.csv'))

    check_drift_diffusion_analytical(B, data, degenerate_cases=True)
Beispiel #28
0
def test_DDM_Integrator_Bogacz(benchmark, mode):
    stim = 10
    T = DDM(name='DDM', function=DriftDiffusionAnalytical())
    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(stim)[0]
    assert np.allclose(val, [1.0])
    benchmark(ex, stim)
def test_DDM_rate(benchmark, rate, expected, mech_mode):
    stim = [10]
    T = DDM(
        name='DDM',
        function=DriftDiffusionIntegrator(noise=0.0,
                                          rate=rate,
                                          time_step_size=1.0),
    )
    ex = pytest.helpers.get_mech_execution(T, mech_mode)

    val = float(ex(stim)[0][0])
    assert val == expected
    if benchmark.enabled:
        benchmark(ex, stim)
Beispiel #30
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])