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 == 12.641125838188323
def test_DDM_size_int_inputs_(): T = DDM(name='DDM', size=1.0, function=DriftDiffusionIntegrator(noise=0.0, rate=-5.0, time_step_size=1.0), time_scale=TimeScale.TIME_STEP) val = T.execute([.4]).tolist() assert val == [[-2.0], [1.0]]
def test_DDM_Integrator(): stim = 10 T = DDM(name='DDM', function=DriftDiffusionIntegrator(initializer=20.0), time_scale=TimeScale.TIME_STEP) val = float(T.execute(stim)[0]) T.function_object.initializer = 30.0 val2 = float(T.execute(stim)[0])
def test_DDM_zero_noise(): stim = 10 T = DDM(name='DDM', function=DriftDiffusionIntegrator(noise=0.0, rate=1.0, time_step_size=1.0), time_scale=TimeScale.TIME_STEP) 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 == 11.320562919094161
def test_DDM_input_list_len_1(): stim = [10] 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_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), time_scale=TimeScale.TIME_STEP) val = float(T.execute(stim)[0]) assert val == -50
def test_DDM_input_fn(): with pytest.raises(TypeError) as error_text: stim = NormalDist().function T = DDM(name='DDM', function=DriftDiffusionIntegrator(noise=0.0, rate=1.0, time_step_size=1.0), time_scale=TimeScale.TIME_STEP) float(T.execute(stim)) assert "not supported for the input types" in str(error_text.value)
def test_DDM_rate_float(): 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_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), time_scale=TimeScale.TIME_STEP) 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), time_scale=TimeScale.TIME_STEP) float(T.execute(stim)[0]) assert "must have only a single numeric item" in str(error_text.value)
def test_DDM_noise_fn(): with pytest.raises(FunctionError) as error_text: stim = 10 T = DDM(name='DDM', function=DriftDiffusionIntegrator(noise=NormalDist().function, rate=1.0, time_step_size=1.0), time_scale=TimeScale.TIME_STEP) float(T.execute(stim)[0]) assert "DriftDiffusionIntegrator requires noise parameter to be a float" in str( error_text.value)
def test_DDM_noise_int(): with pytest.raises(FunctionError) as error_text: stim = 10 T = DDM( name='DDM', function=DriftDiffusionIntegrator(noise=2, 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_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_threshold_stops_accumulation(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])
def test_DDM(): myMechanism = DDM( function=BogaczEtAl( drift_rate=(1.0), threshold=(10.0), starting_point=0.0, ), name='My_DDM', ) myMechanism_2 = DDM(function=BogaczEtAl(drift_rate=2.0, threshold=20.0), name='My_DDM_2') myMechanism_3 = DDM( function=BogaczEtAl(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].value, np.array([40.])), (myMechanism.output_states[0].value, np.array([10.])), (myMechanism_2.input_states[0].value, np.array([10.])), (myMechanism_2.output_states[0].value, np.array([20.])), (myMechanism_3.input_states[0].value, np.array([20.])), (myMechanism_3.output_states[0].value, 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))
def test_threshold_param(self): D = DDM(name='DDM', function=DriftDiffusionIntegrator(threshold=10.0)) assert D.function_object.threshold == 10.0 D.function_object.threshold = 5.0 assert D.function_object._threshold == 5.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_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_int_check_var(): T = DDM(name='DDM', size=1, function=DriftDiffusionIntegrator(noise=0.0, rate=-5.0, time_step_size=1.0), time_scale=TimeScale.TIME_STEP) assert len(T.instance_defaults.variable ) == 1 and T.instance_defaults.variable[0][0] == 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), time_scale=TimeScale.TIME_STEP) 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), time_scale=TimeScale.TIME_STEP) assert "must have only a single numeric item" in str(error_text.value)
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), time_scale=TimeScale.TIME_STEP) assert "is greater than 1, implying there are" in str(error_text.value)
def test_is_finished_stops_system(self): D = DDM(name='DDM', function=DriftDiffusionIntegrator(threshold=10.0)) P = Process(pathway=[D]) S = System(processes=[P]) S.run(inputs={D: 2.0}, termination_processing={TimeScale.TRIAL: WhenFinished(D)}) # decision variable's value should match threshold assert D.value[0] == 10.0 # it should have taken 5 executions (and time_step_size = 1.0) assert D.value[1] == 5.0
def test_DDM_time(): D = DDM(name='DDM', function=DriftDiffusionIntegrator(noise=0.0, rate=-5.0, time_step_size=0.2, t0=0.5)) time_0 = D.function_object.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_laming_validation_specify_control_signals(): # Mechanisms: Input = TransferMechanism(name='Input') Reward = TransferMechanism(name='Reward', output_states=[RESULT, MEAN, VARIANCE]) Decision = DDM(function=BogaczEtAl(drift_rate=1.0, threshold=1.0, noise=0.5, starting_point=0, t0=0.45), output_states=[ DECISION_VARIABLE, RESPONSE_TIME, PROBABILITY_UPPER_THRESHOLD ], name='Decision') # Processes: TaskExecutionProcess = Process(default_variable=[0], pathway=[Input, IDENTITY_MATRIX, Decision], name='TaskExecutionProcess') RewardProcess = Process(default_variable=[0], pathway=[Reward], name='RewardProcess') # System: mySystem = System(processes=[TaskExecutionProcess, RewardProcess], controller=EVCControlMechanism, enable_controller=True, monitor_for_control=[ Reward, Decision.PROBABILITY_UPPER_THRESHOLD, (Decision.RESPONSE_TIME, -1, 1) ], control_signals=[(DRIFT_RATE, Decision), (THRESHOLD, Decision)], name='EVC Test System') # Stimulus stim_list_dict = {Input: [0.5, 0.123], Reward: [20, 20]} # Run system: mySystem.run(inputs=stim_list_dict) RewardPrediction = mySystem.execution_list[3] InputPrediction = mySystem.execution_list[4] # rearranging mySystem.results into a format that we can compare with pytest results_array = [] for elem in mySystem.results: elem_array = [] for inner_elem in elem: elem_array.append(float(inner_elem)) results_array.append(elem_array) # mySystem.results expected output properly formatted expected_results_array = [[10., 10.0, 0.0, -0.1, 0.48999867, 0.50499983], [10., 10.0, 0.0, -0.4, 1.08965888, 0.51998934], [10., 10.0, 0.0, 0.7, 2.40680493, 0.53494295], [10., 10.0, 0.0, -1., 4.43671978, 0.549834], [10., 10.0, 0.0, 0.1, 0.48997868, 0.51998934], [10., 10.0, 0.0, -0.4, 1.08459402, 0.57932425], [10., 10.0, 0.0, 0.7, 2.36033556, 0.63645254], [10., 10.0, 0.0, 1., 4.24948962, 0.68997448], [10., 10.0, 0.0, 0.1, 0.48993479, 0.53494295], [10., 10.0, 0.0, 0.4, 1.07378304, 0.63645254], [10., 10.0, 0.0, 0.7, 2.26686573, 0.72710822], [10., 10.0, 0.0, 1., 3.90353015, 0.80218389], [10., 10.0, 0.0, 0.1, 0.4898672, 0.549834], [10., 10.0, 0.0, -0.4, 1.05791834, 0.68997448], [10., 10.0, 0.0, 0.7, 2.14222978, 0.80218389], [10., 10.0, 0.0, 1., 3.49637662, 0.88079708], [10., 10.0, 0.0, 1., 3.49637662, 0.88079708], [15., 15.0, 0.0, 0.1, 0.48999926, 0.50372993], [15., 15.0, 0.0, -0.4, 1.08981011, 0.51491557], [15., 15.0, 0.0, 0.7, 2.40822035, 0.52608629], [15., 15.0, 0.0, 1., 4.44259627, 0.53723096], [15., 15.0, 0.0, 0.1, 0.48998813, 0.51491557], [15., 15.0, 0.0, 0.4, 1.0869779, 0.55939819], [15., 15.0, 0.0, -0.7, 2.38198336, 0.60294711], [15., 15.0, 0.0, 1., 4.33535807, 0.64492386], [15., 15.0, 0.0, 0.1, 0.48996368, 0.52608629], [15., 15.0, 0.0, 0.4, 1.08085171, 0.60294711], [15., 15.0, 0.0, 0.7, 2.32712843, 0.67504223], [15., 15.0, 0.0, 1., 4.1221271, 0.7396981], [15., 15.0, 0.0, 0.1, 0.48992596, 0.53723096], [15., 15.0, 0.0, -0.4, 1.07165729, 0.64492386], [15., 15.0, 0.0, 0.7, 2.24934228, 0.7396981], [15., 15.0, 0.0, 1., 3.84279648, 0.81637827], [15., 15.0, 0.0, 1., 3.84279648, 0.81637827]] expected_output = [ # Decision Output | Second Trial (Decision.output_states[0].value, np.array(1.0)), # Input Prediction Output | Second Trial (InputPrediction.output_states[0].value, np.array(0.1865)), # RewardPrediction Output | Second Trial (RewardPrediction.output_states[0].value, np.array(15.0)), # --- Decision Mechanism --- # ControlSignal Values # drift rate # ALT: float(Decision._parameter_states[DRIFT_RATE].value # (mySystem.controller.control_signals[0].value, np.array(1.0)), # # threshold # # # ALT: float(Decision._parameter_states[THRESHOLD].value # (mySystem.controller.control_signals[1].value, np.array(1.0)), # Output State Values # decision variable (Decision.output_states[DECISION_VARIABLE].value, np.array([1.0])), # response time (Decision.output_states[RESPONSE_TIME].value, np.array([3.84279648])), # upper bound (Decision.output_states[PROBABILITY_UPPER_THRESHOLD].value, np.array([0.81637827])), # lower bound # (round(float(Decision.output_states['DDM_probability_lowerBound'].value),3), 0.184), # --- Reward Mechanism --- # Output State Values # transfer mean (Reward.output_states[RESULT].value, np.array([15.])), # transfer_result (Reward.output_states[MEAN].value, np.array(15.0)), # transfer variance (Reward.output_states[VARIANCE].value, np.array(0.0)), # System Results Array # (all intermediate output values of system) (results_array, expected_results_array) ] for i in range(len(expected_output)): val, expected = expected_output[i] np.testing.assert_allclose( val, expected, atol=1e-08, err_msg='Failed on expected_output[{0}]'.format(i)) np.testing.assert_almost_equal( Decision._parameter_states[DRIFT_RATE].value, Decision._parameter_states[DRIFT_RATE].mod_afferents[0].value * Decision._parameter_states[DRIFT_RATE].function_object.value) np.testing.assert_almost_equal( Decision._parameter_states[THRESHOLD].value, Decision._parameter_states[THRESHOLD].mod_afferents[0].value * Decision._parameter_states[THRESHOLD].function_object.value)
def test_danglingControlledMech(): # # first section is from Stroop Demo # Color_Input = TransferMechanism(name='Color Input', function=Linear(slope=0.2995)) Word_Input = TransferMechanism(name='Word Input', function=Linear(slope=0.2995)) # Processing Mechanisms (Control) Color_Hidden = TransferMechanism( name='Colors Hidden', function=Logistic(gain=(1.0, ControlProjection)), ) Word_Hidden = TransferMechanism( name='Words Hidden', function=Logistic(gain=(1.0, ControlProjection)), ) Output = TransferMechanism( name='Output', function=Logistic(gain=(1.0, ControlProjection)), ) # Decision Mechanisms Decision = DDM( function=BogaczEtAl( drift_rate=(1.0), threshold=(0.1654), noise=(0.5), starting_point=(0), t0=0.25, ), name='Decision', ) # Outcome Mechanisms: Reward = TransferMechanism(name='Reward') # Processes: ColorNamingProcess = Process( default_variable=[0], pathway=[Color_Input, Color_Hidden, Output, Decision], name='Color Naming Process', ) WordReadingProcess = Process( default_variable=[0], pathway=[Word_Input, Word_Hidden, Output, Decision], name='Word Reading Process', ) RewardProcess = Process( default_variable=[0], pathway=[Reward], name='RewardProcess', ) # add another DDM but do not add to system second_DDM = DDM( function=BogaczEtAl( drift_rate=( 1.0, ControlProjection( function=Linear, control_signal_params={ ALLOCATION_SAMPLES: np.arange(0.1, 1.01, 0.3) }, ), ), threshold=( 1.0, ControlProjection( function=Linear, control_signal_params={ ALLOCATION_SAMPLES: np.arange(0.1, 1.01, 0.3) }, ), ), noise=(0.5), starting_point=(0), t0=0.45 ), name='second_DDM', ) # System: mySystem = System( processes=[ColorNamingProcess, WordReadingProcess, RewardProcess], controller=EVCControlMechanism, enable_controller=True, # monitor_for_control=[Reward, (DDM_PROBABILITY_UPPER_THRESHOLD, 1, -1)], name='EVC Gratton System', )
def test_DDM_Integrator_Bogacz(): stim = 10 T = DDM(name='DDM', function=BogaczEtAl()) val = float(T.execute(stim)[0]) assert val == 1.0
def test_valid(self): D = DDM( name='DDM', function=DriftDiffusionIntegrator(), ) # 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(D.value, [[1.0], [1.0]]) assert np.allclose(D.output_states[0].value, 1.0) assert np.allclose(D.output_states[1].value, 1.0) # reinitialize function D.function_object.reinitialize(2.0, 0.1) assert np.allclose(D.function_object.value, 2.0) assert np.allclose(D.function_object.previous_value, 2.0) assert np.allclose(D.function_object.previous_time, 0.1) assert np.allclose(D.value, [[1.0], [1.0]]) assert np.allclose(D.output_states[0].value, 1.0) assert np.allclose(D.output_states[1].value, 1.0) # reinitialize function without value spec D.function_object.reinitialize() assert np.allclose(D.function_object.value, 0.0) assert np.allclose(D.function_object.previous_value, 0.0) assert np.allclose(D.function_object.previous_time, 0.0) assert np.allclose(D.value, [[1.0], [1.0]]) assert np.allclose(D.output_states[0].value, 1.0) assert np.allclose(D.output_states[1].value, 1.0) # reinitialize mechanism D.reinitialize(2.0, 0.1) assert np.allclose(D.function_object.value, 2.0) assert np.allclose(D.function_object.previous_value, 2.0) assert np.allclose(D.function_object.previous_time, 0.1) assert np.allclose(D.value, [[2.0], [0.1]]) assert np.allclose(D.output_states[0].value, 2.0) assert np.allclose(D.output_states[1].value, 0.1) D.execute(1.0) # 2.0 + 1.0 = 3.0 ; 0.1 + 1.0 = 1.1 assert np.allclose(D.value, [[[3.0]], [[1.1]]]) assert np.allclose(D.output_states[0].value, 3.0) assert np.allclose(D.output_states[1].value, 1.1) # reinitialize mechanism without value spec D.reinitialize() assert np.allclose(D.function_object.value, 0.0) assert np.allclose(D.function_object.previous_value, 0.0) assert np.allclose(D.function_object.previous_time, 0.0) assert np.allclose(D.output_states[0].value[0], 0.0) assert np.allclose(D.output_states[1].value[0], 0.0) # reinitialize only decision variable D.reinitialize(1.0) assert np.allclose(D.function_object.value, 1.0) assert np.allclose(D.function_object.previous_value, 1.0) assert np.allclose(D.function_object.previous_time, 0.0) assert np.allclose(D.output_states[0].value[0], 1.0) assert np.allclose(D.output_states[1].value[0], 0.0)
def test_create_scheduler_from_system_StroopDemo(self): Color_Input = TransferMechanism(name='Color Input', function=Linear(slope=0.2995)) Word_Input = TransferMechanism(name='Word Input', function=Linear(slope=0.2995)) # Processing Mechanisms (Control) Color_Hidden = TransferMechanism( name='Colors Hidden', function=Logistic(gain=(1.0, ControlProjection)), ) Word_Hidden = TransferMechanism( name='Words Hidden', function=Logistic(gain=(1.0, ControlProjection)), ) Output = TransferMechanism( name='Output', function=Logistic(gain=(1.0, ControlProjection)), ) # Decision Mechanisms Decision = DDM( function=BogaczEtAl( drift_rate=(1.0), threshold=(0.1654), noise=(0.5), starting_point=(0), t0=0.25, ), name='Decision', ) # Outcome Mechanisms: Reward = TransferMechanism(name='Reward') # Processes: ColorNamingProcess = Process( default_variable=[0], pathway=[Color_Input, Color_Hidden, Output, Decision], name='Color Naming Process', ) WordReadingProcess = Process( default_variable=[0], pathway=[Word_Input, Word_Hidden, Output, Decision], name='Word Reading Process', ) RewardProcess = Process( default_variable=[0], pathway=[Reward], name='RewardProcess', ) # System: mySystem = System( processes=[ColorNamingProcess, WordReadingProcess, RewardProcess], controller=EVCControlMechanism, enable_controller=True, # monitor_for_control=[Reward, (PROBABILITY_UPPER_THRESHOLD, 1, -1)], name='EVC Gratton System', ) sched = Scheduler(system=mySystem) integrator_ColorInputPrediction = mySystem.execution_list[7] integrator_RewardPrediction = mySystem.execution_list[8] integrator_WordInputPrediction = mySystem.execution_list[9] objective_EVC_mech = mySystem.execution_list[10] expected_consideration_queue = [ { Color_Input, Word_Input, Reward, integrator_ColorInputPrediction, integrator_WordInputPrediction, integrator_RewardPrediction }, {Color_Hidden, Word_Hidden}, {Output}, {Decision}, {objective_EVC_mech}, ] assert sched.consideration_queue == expected_consideration_queue