Exemple #1
0
def test_add_dict_fcn():
    # test exponent conversion
    dict_builder = DictionaryBuilder(dict_fcns=[])
    d_f = "x1^2"
    dict_builder.add_dict_fcn(d_f=d_f)
    dict_fcn = dict_builder.dict_fcns.pop()
    d_f_replaced = d_f.replace("^", "**")
    assert str(dict_fcn.symbolic_expression) == d_f_replaced

    # test an undefined function
    d_f = ["f(x1)"]
    dict_builder = DictionaryBuilder(dict_fcns=d_f)
    data = {"x1": [5, 2]}
    with pytest.raises(NameError):
        dict_builder.evaluate_dict(input_data=data)
Exemple #2
0
def test_evaluate_dict():
    d_f = ["x1*x2", "x1**2"]
    dict_builder = DictionaryBuilder(dict_fcns=d_f)
    data_x1 = [1, 5, 10]
    data_x2 = [3, 7, 15]
    data = {"x1": data_x1, "x2": data_x2}
    A = dict_builder.evaluate_dict(input_data=data)
    sym_expr_df1 = parse_expr(s=d_f[0], evaluate=False)
    df1_eval = [
        sym_expr_df1.evalf(subs=dict(x1=x1, x2=x2))
        for x1, x2 in zip(data_x1, data_x2)
    ]
    sym_expr_df2 = parse_expr(s=d_f[1], evaluate=False)
    df2_eval = [
        sym_expr_df2.evalf(subs=dict(x1=x1, x2=x2))
        for x1, x2 in zip(data_x1, data_x2)
    ]
    B = np.array([df1_eval, df2_eval]).T

    assert np.array_equal(A, B)
plt.plot(t_gp, dydt, c='orange', label="GP diff")
plt.fill_between(t_gp, dydt - 2*dydt_std, dydt + 2*dydt_std, alpha=0.2, color='k')
plt.ylabel("Value (AU)")
plt.xlabel("time (s)")
plt.title("Derivative of y")
plt.legend(loc="best")
plt.show()

# step 1 define a dictionary
d_f = ["1", "x", "y", "z", "x*z", "y*x"]
dict_builder = DictionaryBuilder(dict_fcns=d_f)
dict_functions = dict_builder.dict_fcns
# associate variables with data
data = {"x": y[0, :].T, "y": y[1, :].T, "z": y[2, :].T}
#data = {"x": x_samples.T, "y": y_samples.T, "z": z_samples.T}
A = dict_builder.evaluate_dict(input_data=data)

# step 2 define an SBL problem
# with the Lin reg model and solve it
config_dict = {
    "solver": {"name": "ECOS", "show_time": False, "settings": {}},
    "verbose": False,
}
norm_vect = np.linalg.norm(A, axis=0)
A_norm = A / norm_vect

plt.figure(23)
for col in A_norm.T:
    plt.plot(t, col)
plt.ylabel("Value (AU)")
plt.xlabel("Time (s)")
Exemple #4
0
def test_sbl_on_lotka_volterra():
    # define Lotka-Volerra model
    states = {"x1": "alpha*x1-beta*x1*x2", "x2": "delta*x1*x2-gamma*x2"}
    parameters = {"alpha": 2 / 3, "beta": 4 / 3, "delta": 1, "gamma": 1}
    ss = StateSpaceModel.from_string(states=states, parameters=parameters)

    states = ["x1", "x2"]
    t_span = [0, 30]
    x0 = {"x1": 1.2, "x2": 1.0}
    # simulate model
    gm = MeasurementsGenerator(ss=ss, time_span=t_span, initial_values=x0)
    t, y = gm.get_measurements()

    # compute the time derivative of the state variables
    rhs_preprop = RHSEvalSignalPreprocessor(t=t,
                                            y=y,
                                            rhs_function=ss.get_rhs,
                                            states=states)
    rhs_preprop.calculate_time_derivative()
    dydt_rhs = rhs_preprop.dydt
    dx1 = dydt_rhs[0, :]
    dx2 = dydt_rhs[1, :]

    # step 1 define a dictionary
    d_f = ["x1", "x2", "x1*x1", "x1*x2", "x2*x2"]
    dict_builder = DictionaryBuilder(dict_fcns=d_f)
    dict_functions = dict_builder.dict_fcns
    data = {"x1": y[0, :], "x2": y[1, :]}
    A = dict_builder.evaluate_dict(input_data=data)

    # step 2 define an SBL problem and solve it
    lambda_param_x1 = 0.1
    lambda_param_x2 = 0.05
    sbl_x1 = SBL(
        dict_mtx=A,
        data_vec=dx1,
        lambda_param=lambda_param_x1,
        state_name="x1",
        dict_fcns=dict_functions,
    )
    sbl_x1.compute_model_structure()

    sbl_x2 = SBL(
        dict_mtx=A,
        data_vec=dx2,
        lambda_param=lambda_param_x2,
        state_name="x2",
        dict_fcns=dict_functions,
    )
    sbl_x2.compute_model_structure()

    # test SBL results
    zero_th = 1e-8
    x1_results = [
        str(item.symbolic_expression)
        for item in sbl_x1.get_results(zero_th=zero_th)
    ]
    assert len(x1_results) == 2
    assert "x1" in x1_results
    assert "x1*x2" in x1_results

    x2_results = [
        str(item.symbolic_expression)
        for item in sbl_x2.get_results(zero_th=zero_th)
    ]
    assert len(x2_results) == 2
    assert "x2" in x2_results
    assert "x1*x2" in x2_results