def test_constant_integrator(self):
        I = IntegratorMechanism(
            function=ConstantIntegrator(
                initializer=10.0,
                rate=5.0,
                offset=10
            )
        )
        # P = Process(pathway=[I])
        # constant integrator does not use input value (variable)

        # step 1:
        val = I.execute(20000)
        # value = 10 + 5
        # adjusted_value = 15 + 10
        # previous_value = 25
        # RETURN 25

        # step 2:
        val2 = I.execute(70000)
        # value = 25 + 5
        # adjusted_value = 30 + 10
        # previous_value = 30
        # RETURN 40
        assert (val, val2) == (25, 40)
    def test_ornstein_uhlenbeck_integrator(self):
        I = IntegratorMechanism(
            function=OrnsteinUhlenbeckIntegrator(
                decay=0.5,
                initializer=10.0,
                rate=0.25,
                time_step_size=0.5,
                noise = 0.0,
                offset= 1.0
            )
        )
        # P = Process(pathway=[I])
        # value = previous_value + decay * (previous_value -  rate * new_value) * time_step_size + np.sqrt(
        # time_step_size * noise) * np.random.normal()
        # step 1:

        val = I.execute(1)
        # value = 10 + 0.5 * ( 10.0 - 0.25*1.0) * 0.5 + sqrt(0.25*0)*random_sample
        #       = 10 + 0.5*9.75*0.5
        #       = 12.4375
        # adjusted_value = 12.4375 + 1.0
        # previous_value = 13.4375
        # RETURN 13.4375

        # step 2:
        val2 = I.execute(1)
    def test_reinitialize_not_integrator(self):

        with pytest.raises(MechanismError) as err_txt:
            I_not_integrator = IntegratorMechanism(function=Linear)
            I_not_integrator.execute(1.0)
            I_not_integrator.reinitialize(0.0)
        assert "not allowed because this Mechanism is not stateful." in str(err_txt) \
               and "(It does not have an accumulator to reinitialize)" in str(err_txt)
    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_diffusion_with_reset_intializer(self):
        I = IntegratorMechanism(name='IntegratorMechanism',
                                function=DriftDiffusionIntegrator(),
                                time_scale=TimeScale.TIME_STEP)
        # val = float(I.execute(10)[0])
        # P = Process(pathway=[I])
        val = float(I.execute(10))

        # testing initializer
        I.function_object.reset_initializer = 1.0
        val2 = float(I.execute(0))

        assert [val, val2] == [10.0, 1.0]
    def test_integrator_simple_noise_fn(self):
        I = IntegratorMechanism(
            name='IntegratorMechanism',
            function=SimpleIntegrator(noise=NormalDist()),
        )

        val = float(I.execute(10))

        I.function_object.reinitialize(5.0)

        val2 = float(I.execute(0))

        np.testing.assert_allclose(val, 12.240893199201459)
        np.testing.assert_allclose(val2, 6.867557990149967)
    def test_integrator_simple_noise_fn(self):
        I = IntegratorMechanism(
            name='IntegratorMechanism',
            function=SimpleIntegrator(noise=NormalDist().function),
            time_scale=TimeScale.TIME_STEP)

        val = float(I.execute(10))

        I.function_object.reset_initializer = 5.0

        val2 = float(I.execute(0))

        np.testing.assert_allclose(val, 11.86755799)
        np.testing.assert_allclose(val2, 4.022722120123589)
    def test_integrator_constant_with_reset_intializer(self):
        I = IntegratorMechanism(name='IntegratorMechanism',
                                function=ConstantIntegrator(rate=1.0),
                                time_scale=TimeScale.TIME_STEP)
        # val = float(I.execute(10)[0])
        # P = Process(pathway=[I])
        val = float(I.execute())
        # returns previous_value + rate + noise
        # rate = 1.0, noise = 0, so in this case returns 1.0

        # testing initializer
        I.function_object.reset_initializer = 10.0
        val2 = float(I.execute())

        assert [val, val2] == [1.0, 11.0]
    def test_integrator_adaptive_with_reset_intializer(self):
        I = IntegratorMechanism(name='IntegratorMechanism',
                                function=AdaptiveIntegrator(rate=0.5),
                                time_scale=TimeScale.TIME_STEP)
        # val = float(I.execute(10)[0])
        # P = Process(pathway=[I])
        val = float(I.execute(10))
        # returns (rate)*variable + (1-rate*previous_value) + noise
        # rate = 1, noise = 0, so in this case, returns 10.0

        # testing initializer
        I.function_object.reset_initializer = 1.0
        val2 = float(I.execute(1))

        assert [val, val2] == [5.0, 1.0]
    def test_integrator_simple_with_reset_intializer(self):
        I = IntegratorMechanism(name='IntegratorMechanism',
                                function=SimpleIntegrator(),
                                time_scale=TimeScale.TIME_STEP)
        #     # P = Process(pathway=[I])

        #  returns previous_value + rate*variable + noise
        # so in this case, returns 10.0
        val = float(I.execute(10))

        # testing initializer
        I.function_object.reset_initializer = 5.0

        val2 = float(I.execute(0))

        assert [val, val2] == [10.0, 5.0]
 def test_integrator_type_adaptive_rate_float_input_list(self):
     I = IntegratorMechanism(default_variable=[0, 0, 0],
                             name='IntegratorMechanism',
                             function=AdaptiveIntegrator(rate=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_utility_integrator_short_plus_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_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_constant_rate_list(self):
     I = IntegratorMechanism(
         default_variable=[0, 0, 0],
         name='IntegratorMechanism',
         function=ConstantIntegrator(rate=[5.0, 5.0, 5.0]))
     # 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_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, ),
        )

        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_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_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_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_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_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_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_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_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_ornstein_uhlenbeck_integrator_time(self):
        OU = IntegratorMechanism(function=OrnsteinUhlenbeckIntegrator(
            initializer=10.0,
            rate=10,
            time_step_size=0.2,
            t0=0.5,
            decay=0.1,
            offset=10,
        ))
        time_0 = OU.function_object.previous_time  # t_0  = 0.5
        # np.testing.assert_allclose(time_0, [0.5], atol=1e-08)

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

        for i in range(11):  # t_11 = 0.7 + 10*0.2 = 2.7
            OU.execute(10)
        time_12 = OU.function_object.previous_time  # t_12 = 2.7 + 0.2 = 2.9
 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_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]