示例#1
0
from pyABC_study.ODE import ODESolver, PriorLimits, para_true1, para_prior

print("\n\n\n Base\n Median eps, 2000 particles, 20 generations\n\n\n")

# %% Set path

db_path = "sqlite:///dbfiles/ib_wide.db"

# %% Generate synthetic data


# Using default time points

solver = ODESolver()
solver.time_point = solver.time_point_default

obs_data_raw = solver.ode_model1(para_true1)

print("Target data")
print(obs_data_raw)

# Set factors
# print("Factors applied: 75/25 factor")

# factors = {}

# time_length: int = len(solver.time_point) * 4

# # for i in range(0, time_length, 4):
# #     factors[i] = 1/26.520
示例#2
0
def result_data(history,
                solver: ODESolver,
                compare_data=exp_data_s,
                nr_population=1,
                sample_size=500,
                savefig=False):
    """
Visualise the simulated trajectories from the inferred posterior
    :param solver: ODE solver object
    :param savefig: Decide to save the plot as file or not
    :param history: abc.history object
    :param compare_data: target data
    :param nr_population: the id of population to be visualised
    :param sample_size: sampling size of the selected population
    """
    df, w = history.get_distribution(t=nr_population)
    # if is_old:
    #     df.columns = ['i_beta_phi', 'k_phi_beta', 'k_n_beta', 'lambda_phi', 'lambda_n', 'mu_alpha', 'mu_beta', 'mu_phi',
    #                   'mu_n', 's_alpha_phi', 's_beta_n', 'v_n_phi']
    df_sample = df.sample(sample_size, replace=False)
    df_all_sim_data = pd.DataFrame(columns=['N', 'M', 'B', 'A'])

    solver.time_point = solver.time_point_default
    for ii in range(sample_size):
        temp_dict = df_sample.iloc[ii].to_dict()
        sim_data = solver.ode_model(temp_dict, flatten=False)
        # print(sim_data)
        sim_data = pd.DataFrame.from_dict(sim_data)
        df_all_sim_data = pd.concat([df_all_sim_data, sim_data])

    # plt.ylim(0, 2) # make lim a function parameter
    # plt.show()

    df_mean = pd.DataFrame(columns=['N', 'M', 'B', 'A'])
    for jj in range(solver.time_point.__len__()):
        # print(df_all_sim_data.loc[jj].mean())
        df_mean = df_mean.append(df_all_sim_data.loc[jj].mean(),
                                 ignore_index=True)

    df_median = quantile_calculate(df_all_sim_data,
                                   solver.time_point.__len__(), 0.5)
    df_75 = quantile_calculate(df_all_sim_data, solver.time_point.__len__(),
                               0.75)
    df_25 = quantile_calculate(df_all_sim_data, solver.time_point.__len__(),
                               0.25)

    fig, axs = plt.subplots(4, 1, figsize=(4, 12))
    # plt.subplots_adjust(hspace=0.5)
    index_cov = ['N', 'M', 'B', 'A']
    titles = ["N", "Φ", "β", "α"]
    for kk in range(4):
        seq_mask = np.isfinite(compare_data[index_cov[kk]])
        # axs[kk].plot(solver.timePoint, df_25.iloc[:, kk], 'b--')
        # axs[kk].plot(solver.timePoint, df_75.iloc[:, kk], 'b--')
        axs[kk].fill_between(solver.time_point,
                             df_25.iloc[:, kk],
                             df_75.iloc[:, kk],
                             alpha=0.9,
                             color='lightgrey',
                             label='25% – 75% quantile range')
        axs[kk].plot(solver.time_point,
                     df_mean.iloc[:, kk],
                     'b',
                     label="Mean",
                     alpha=0.6)
        axs[kk].plot(solver.time_point_exp[seq_mask],
                     compare_data[index_cov[kk]][seq_mask],
                     alpha=0.7,
                     marker='^',
                     color='black',
                     label='Observed')
        axs[kk].errorbar(solver.time_point_exp,
                         compare_data[index_cov[kk]],
                         yerr=[[0.5 * x for x in exp_data_SEM[index_cov[kk]]],
                               [0.5 * x for x in exp_data_SEM[index_cov[kk]]]],
                         fmt='none',
                         ecolor='grey',
                         elinewidth=2,
                         alpha=0.6)
        # axs[kk].legend(['Mean', '25% – 75% quantile range', 'Observed'])
        axs[kk].set_title(titles[kk])
    # handles, labels = axs[0].get_legend_handles_labels()
    # fig.legend(handles, labels, loc='upper center')
    # fig.tight_layout(pad=5.0)
    if savefig:
        plt.savefig("resultCurve.png", dpi=200)
    # plt.subplots_adjust(hspace=5.5)
    plt.show()
from pyABC_study.ODE import ODESolver, PriorLimits, arr2d_to_dict, para_true1, para_prior
from pyABC_study.dataPlot import result_data_old, result_plot, result_data

# %% Settings

lim = PriorLimits(1e-6, 50)

prior_distribution = "uniform"

print(prior_distribution)

para_prior1 = para_prior(lim, prior_distribution, 1)

solver = ODESolver()
solver.time_point = solver.time_point_default

obs_data_raw_s = solver.ode_model1(para_true1, flatten=False, add_noise=False)

solver.time_point = solver.time_point_exp
obs_data_raw_s_less = solver.ode_model(para_true1, flatten=False, add_noise=False)

# print("Target data")
# print(obs_data_noisy_s)

# %% Load database

db_path = "sqlite:///db/abcsmc_test.db"

history = pyabc.History(db_path)
示例#4
0
import pyabc

from pyABC_study.ODE import ODESolver, PriorLimits, para_true1, para_prior

print("\n\n\n Base\n Median eps, 2000 particles, 20 generations\n\n\n")

# %% Set path

db_path = "sqlite:///dbfiles/ib_less.db"

# %% Generate synthetic data

# Using default time points

solver = ODESolver()
solver.time_point = solver.time_point_exp

obs_data_raw = solver.ode_model1(para_true1)

print("Target data")
print(obs_data_raw)

# Set factors
# print("Factors applied: 75/25 factor")

# factors = {}

# time_length: int = len(solver.time_point) * 4

# # for i in range(0, time_length, 4):
# #     factors[i] = 1/26.520