def test_threshold_param(self): D = DDM(name='DDM', function=DriftDiffusionIntegrator(threshold=10.0)) assert D.function.threshold.base == 10.0 D.function.threshold.base = 5.0 assert D.function.threshold.base == 5.0
def test_DDMMechanism_LCA_equivalent(comp_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]}, execution_mode=comp_mode) assert np.allclose(np.asfarray(result2[0]), [0.1]) assert np.allclose(np.asfarray(result2[1]), [0.1])
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_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
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()
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_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_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)
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_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])
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_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_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)
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_ddm_is_finished(comp_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], execution_mode=comp_mode) results = [ x for x in np.array(results).flatten() ] #HACK: The result is an object dtype in Python comp_mode for some reason? assert np.allclose(results, np.array(expected_results).flatten())
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
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)
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.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, 2.9, atol=1e-08)
def test_threshold_stops_accumulation(self, mech_mode, variable, expected, benchmark): D = DDM(name='DDM', function=DriftDiffusionIntegrator(threshold=5.0)) ex = pytest.helpers.get_mech_execution(D, mech_mode) decision_variables = [] time_points = [] for i in range(5): output = ex([variable]) decision_variables.append(output[0][0]) time_points.append(output[1][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]) if benchmark.enabled: benchmark(ex, [variable])
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], 1.0) assert np.allclose(D.output_ports[1].value[0], 1.0) # reset function D.function.reset(2.0, 0.1) 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], 1.0) assert np.allclose(D.output_ports[1].value[0], 1.0) # reset function without value spec D.function.reset() 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], 1.0) assert np.allclose(D.output_ports[1].value[0], 1.0) # reset mechanism D.reset(2.0, 0.1) 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], 3.0) assert np.allclose(D.output_ports[1].value[0], 1.1) # reset mechanism without value spec D.reset() 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) # reset only decision variable D.function.initializer = 1.0 D.function.starting_point = 0.0 D.reset() 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)