def test_get_rhs():
    states = {"x1": {"x1": "m11"}, "x2": {"x1": "m21", "x2": "m22"}}
    parameters = {"m11": -0.5, "m12": 0.1, "m21": 0.6, "m22": -0.3}
    ss = StateSpaceModel(states=states, parameters=parameters)
    y = []
    states_names = ["x1", "x2"]
    with pytest.raises(ValueError, match=r"#states:\d+ must be equal to \d+$"):
        ss.get_rhs(t=[], y=y, states=states_names)
def test_add_state_equation():
    # define a MAK model
    states = {"x1": {"x1": "m11"}, "x2": {"x1": "m21", "x2": "m22"}}
    parameters = {"m11": -0.5, "m12": 0.1, "m21": 0.6, "m22": -0.3}
    ss = StateSpaceModel(states=states, parameters=parameters)

    other_state = {"x3": {"x1": "m11"}}
    ss.add_state_equation(other_state, ss.parameters)

    assert len(ss.state_vector) == len(states) + len(other_state)
def test_from_string():
    """test that the correct State-Space model is built from strings"""
    states = {"x": "sigma*(y-x)", "y": "x*(rho-z)-y", "z": "x*y-beta*z"}
    parameters = {"sigma": 10, "beta": 8 / 3, "rho": 28}
    ss = StateSpaceModel.from_string(states=states, parameters=parameters)
    assert len(ss.state_vector) == len(states)
    assert len(ss.parameters) == len(parameters)
def test_repr():
    """test that the repr of StateSpace model returns"""
    states = {"x": "sigma*(y-x)", "y": "x*(rho-z)-y", "z": "x*y-beta*z"}
    parameters = {"sigma": 10, "beta": 8 / 3, "rho": 28}
    ss = StateSpaceModel.from_string(states=states, parameters=parameters)

    number_of_new_lines = repr(ss).count("\n")
    assert len(ss.state_vector) == number_of_new_lines
    GPSignalPreprocessor,
    RHSEvalSignalPreprocessor,
    SplineSignalPreprocessor,
)
import matplotlib
font = {'size'   : 15}

matplotlib.rc('font', **font)
matplotlib.rc('xtick', labelsize=15)
matplotlib.rc('ytick', labelsize=15)


# define the model
states = {"x": "-y-z", "y": "x+a*y", "z": "b+x*z-z*c"}
parameters = {"a": 0.2, "b": 1.2, "c": 5.0}
ss = StateSpaceModel.from_string(states=states, parameters=parameters)
print("Original Model:")
print(ss)

t_span = [0, 50]
y0 = {"x": 1.0, "y": 0.0, "z": 0.0}
data_points = 500
gp_interpolation_factor = None
noisy_obs = True
SNR = 15

# simulate model
gm = MeasurementsGenerator(
    ss=ss, time_span=t_span, initial_values=y0, data_points=data_points
)
t, y = gm.get_measurements(SNR_db=SNR, fixed_seed=True)
Exemple #6
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