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_object.stateful_attributes: reinitialize_values.append(getattr(A.function_object, 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]])])
def test_utility_integrator_short_minus_long(self): # default params: # initial_short_term_utility = 0.0 # initial_long_term_utility = 0.0 # short_term_rate = 1.0 # long_term_rate = 1.0 U = IntegratorMechanism( name = "AGTUtilityIntegrator", function=AGTUtilityIntegrator( operation="s-l" ) ) engagement = [] short_term_util = [] long_term_util = [] for i in range(50): engagement.append(U.execute([1])[0][0]) short_term_util.append(U.function_object.short_term_utility_logistic[0]) long_term_util.append(U.function_object.long_term_utility_logistic[0]) print("engagement = ", engagement) print("short_term_util = ", short_term_util) print("long_term_util = ", long_term_util)
def test_integrator_type_simple_rate_list(self): I = IntegratorMechanism( name='IntegratorMechanism', default_variable=[0, 0, 0], function=SimpleIntegrator(rate=[5.0, 5.0, 5.0])) # P = Process(pathway=[I]) val = list(I.execute([10.0, 10.0, 10.0])[0]) assert val == [50.0, 50.0, 50.0]
def test_integrator_drift_diffusion_noise_val(self): I = IntegratorMechanism(name='IntegratorMechanism', function=DriftDiffusionIntegrator(noise=5.0, ), time_scale=TimeScale.TIME_STEP) val = float(I.execute(10)) np.testing.assert_allclose(val, 15.010789523731438)
def test_integrator_type_diffusion_rate_list(self): I = IntegratorMechanism( default_variable=[0, 0, 0], name='IntegratorMechanism', function=DriftDiffusionIntegrator(rate=[5.0, 5.0, 5.0])) # P = Process(pathway=[I]) val = list(I.execute([10.0, 10.0, 10.0])[0]) assert val == [50.0, 50.0, 50.0]
def test_integrator_input_array_less_than_default(self): with pytest.raises(MechanismError) as error_text: I = IntegratorMechanism(name='IntegratorMechanism', default_variable=[0, 0, 0, 0, 0]) # P = Process(pathway=[I]) I.execute([10.0, 5.0, 2.0]) assert "does not match required length" in str(error_text)
def test_integrator_type_adaptive_rate_list(self): I = IntegratorMechanism( default_variable=[0, 0, 0], name='IntegratorMechanism', function=AdaptiveIntegrator(rate=[0.5, 0.5, 0.5])) # P = Process(pathway=[I]) val = list(I.execute([10.0, 10.0, 10.0])[0]) assert val == [5.0, 5.0, 5.0]
def test_integrator_type_constant_rate_list_input_float(self): with pytest.raises(ComponentError) as error_text: I = IntegratorMechanism( name='IntegratorMechanism', function=ConstantIntegrator(rate=[5.0, 5.0, 5.0])) # P = Process(pathway=[I]) float(I.execute(10.0)) assert ("is not compatible with the variable format" in str(error_text) and "to which it is being assigned" in str(error_text))
def test_simple_integrator(self): I = IntegratorMechanism(function=SimpleIntegrator( initializer=10.0, rate=5.0, offset=10, )) # P = Process(pathway=[I]) val = I.execute(1) assert val == 25
def test_integrator_input_list(self): I = IntegratorMechanism( name='IntegratorMechanism', function=SimpleIntegrator( ) ) # P = Process(pathway=[I]) val = float(I.execute([10.0])) assert val == 10.0
def test_integrator_adaptive_noise_fn(self): I = IntegratorMechanism( name='IntegratorMechanism', function=AdaptiveIntegrator(noise=NormalDist().function), time_scale=TimeScale.TIME_STEP) val = float(I.execute(10)) np.testing.assert_allclose(val, 11.86755799)
def test_integrator_adaptive_noise_fn(self): I = IntegratorMechanism( name='IntegratorMechanism', function=AdaptiveIntegrator(noise=NormalDist()), ) val = float(I.execute(10)) np.testing.assert_allclose(val, 12.240893199201459)
def test_integrator_drift_diffusion_noise_val(self): I = IntegratorMechanism( name='IntegratorMechanism', function=DriftDiffusionIntegrator(noise=5.0, ), ) val = float(I.execute(10)) np.testing.assert_allclose(val, 12.188524664621541)
def test_integrator_constant_noise_fn(self): I = IntegratorMechanism( name='IntegratorMechanism', function=ConstantIntegrator(noise=NormalDist()), ) val = float(I.execute(10)) np.testing.assert_allclose(val, 1.8675579901499675)
def test_adaptive_integrator(self): I = IntegratorMechanism(function=AdaptiveIntegrator( initializer=10.0, rate=0.5, offset=10, )) # P = Process(pathway=[I]) # 10*0.5 + 1*0.5 + 10 val = I.execute(1) assert val == 15.5
def test_integrator_accumulator_noise_fn_var_list(self): I = IntegratorMechanism( name='IntegratorMechanism', default_variable=[0, 0, 0, 0], function=AccumulatorIntegrator(noise=NormalDist(), ), ) val = I.execute([10, 10, 10, 10])[0] np.testing.assert_allclose( val, [0.95008842, -0.15135721, -0.10321885, 0.4105985])
def test_integrator_type_diffusion_rate_float(self): I = IntegratorMechanism( name='IntegratorMechanism', function=DriftDiffusionIntegrator( rate=5.0 ) ) # P = Process(pathway=[I]) val = I.execute(10.0) assert np.allclose([[[50.0]], [[1.0]]], val)
def test_integrator_drift_diffusion_noise_val(self): I = IntegratorMechanism( name='IntegratorMechanism', function=DriftDiffusionIntegrator( noise=5.0, ), ) val = I.execute(10.0) assert np.allclose(val, [[[15.01078952]], [[1.]]])
def test_integrator_type_adaptive_rate_float(self): I = IntegratorMechanism( name='IntegratorMechanism', function=AdaptiveIntegrator( rate=0.5 ) ) # P = Process(pathway=[I]) val = list(I.execute(10.0)) assert val == [5.0]
def test_integrator_type_diffusion_rate_list_input_float(self): with pytest.raises(FunctionError) as error_text: I = IntegratorMechanism( name='IntegratorMechanism', function=DriftDiffusionIntegrator(rate=[5.0, 5.0, 5.0])) # P = Process(pathway=[I]) float(I.execute(10.0)) assert ("array specified for the rate parameter" in str(error_text) and "must match the length" in str(error_text) and "of the default input" in str(error_text))
def test_integrator_type_constant_rate_float(self): I = IntegratorMechanism( name='IntegratorMechanism', function=ConstantIntegrator( rate=5.0 ) ) # P = Process(pathway=[I]) val = float(I.execute(10.0)) assert val == 5.0
def test_fhn_gilzenrat_figure_2(self): # Isolate the FHN mechanism for testing and recreate figure 2 from the gilzenrat paper initial_v = 0.2 initial_w = 0.0 F = IntegratorMechanism(name='IntegratorMech-FHNFunction', function=FHNIntegrator( 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 ])
def test_integrator_adaptive_noise_fn_var_list(self): I = IntegratorMechanism( name='IntegratorMechanism', default_variable=[0, 0, 0, 0], function=AdaptiveIntegrator(noise=NormalDist(), ), ) val = I.execute([10, 10, 10, 10])[0] np.testing.assert_allclose( val, [10.95008842, 9.84864279, 9.89678115, 10.4105985])
def test_integrator_accumulator_noise_fn_var_list(self): I = IntegratorMechanism( name='IntegratorMechanism', default_variable=[0, 0, 0, 0], function=AccumulatorIntegrator( noise=NormalDist(), ), ) val = I.execute([10, 10, 10, 10])[0] np.testing.assert_allclose(val, [0.14404357, 1.45427351, 0.76103773, 0.12167502])
def test_integrator_constant_noise_fn_var_list(self): I = IntegratorMechanism( name='IntegratorMechanism', default_variable=[0, 0, 0, 0], function=ConstantIntegrator(noise=NormalDist(), ), ) val = I.execute([10, 10, 10, 10])[0] np.testing.assert_allclose( val, [-0.15135721, -0.10321885, 0.4105985, 0.14404357])
def test_fhn_gilzenrat_figure_2(self): # Isolate the FHN mechanism for testing and recreate figure 2 from the gilzenrat paper initial_v = 0.2 initial_w = 0.0 F = IntegratorMechanism(name='IntegratorMech-FHNFunction', function=FHNIntegrator( 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.24844236992931412, 0.27113468959297515, 0.29350254152625221, 0.31599112332052792, 0.33904651470437225, 0.36315614063656521, 0.38888742632665502, 0.41692645840176923, 0.44811281741549686 ]) np.testing.assert_allclose(plot_w_list, [ 0.0, 0.0019518690642000148, 0.0041351416812363193, 0.0065323063637677276, 0.0091322677555586273, 0.011929028036111457, 0.014921084302726394, 0.018111324713170868, 0.021507331976846619, 0.025122069034563425, 0.028974949616469712 ])
def test_integrator_accumulator_noise_fn_var_list(self): I = IntegratorMechanism(name='IntegratorMechanism', default_variable=[0, 0, 0, 0], function=AccumulatorIntegrator( noise=NormalDist().function, default_variable=[0, 0, 0, 0]), time_scale=TimeScale.TIME_STEP) val = I.execute([10, 10, 10, 10])[0] np.testing.assert_allclose( val, [0.12167502, 0.44386323, 0.33367433, 1.49407907])
def test_drift_diffusion_integrator(self): I = IntegratorMechanism(function=DriftDiffusionIntegrator( initializer=10.0, rate=10, time_step_size=0.5, offset=10, )) # P = Process(pathway=[I]) # 10 + 10*0.5 + 0 + 10 = 25 val = I.execute(1) assert val == 25
def test_integrator_ornstein_uhlenbeck_noise_val(self): I = IntegratorMechanism( name='IntegratorMechanism', function=OrnsteinUhlenbeckIntegrator(noise=2.0, decay=0.5, initializer=1.0, rate=0.25), ) # val = 1.0 + 0.5 * (1.0 - 0.25 * 2.5) * 1.0 + np.sqrt(1.0 * 2.0) * np.random.normal() val = float(I.execute(2.5))
def test_integrator_constant_noise_fn_var_list(self): I = IntegratorMechanism( name='IntegratorMechanism', default_variable=[0, 0, 0, 0], function=ConstantIntegrator( noise=NormalDist(), ), ) val = I.execute([10, 10, 10, 10])[0] np.testing.assert_allclose(val, [1.45427351, 0.76103773, 0.12167502, 0.44386323])