'kNB': 2.359199465995228, 'lambdaM': 2.213837884117815, 'lambdaN': 7.260925726829641, 'muA': 18.94626522780349, 'muB': 2.092860392215201, 'muM': 0.17722330053184654, 'muN': 0.0023917569160019844, 'sAM': 10.228522400429998, 'sBN': 4.034313992927392, 'vNM': 0.3091883041193706} # Using default time points solver = ODESolver() obs_data_noisy = solver.ode_model(para_true, flatten=True, add_noise=True) obs_data_raw = solver.ode_model(para_true, flatten=True, add_noise=False) obs_data_noisy_s = solver.ode_model(para_true, flatten=False, add_noise=True) obs_data_raw_s = solver.ode_model(para_true, flatten=False, add_noise=False) print("Target data") print(obs_data_noisy_s) # %% Calculate data range as factors: # Use data range as factors range_N = obs_data_raw_s['N'].max() - obs_data_raw_s['N'].min() range_M = obs_data_raw_s['M'].max() - obs_data_raw_s['M'].min() range_B = obs_data_raw_s['B'].max() - obs_data_raw_s['B'].min() range_A = obs_data_raw_s['A'].max() - obs_data_raw_s['A'].min()
# %% Load database # change database name db_path = "sqlite:///db/model5_24_more.db" history = pyabc.History(db_path) print("ID: %d, generations: %d" % (history.id, history.max_t)) # %% Plot solver = ODESolver() # change model name solver.ode_model = solver.ode_model5 result_data(history, solver, nr_population=history.max_t, savefig=True) # change prior name result_plot(history, None, para_prior5, history.max_t - 5, savefig=False) df, w = history.get_distribution(t=history.max_t - 5) pyabc.visualization.plot_kde_matrix(df, w) plt.show() pyabc.visualization.plot_epsilons(history) plt.show() # %% Compare
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) print("ID: %d, generations: %d" % (history.id, history.max_t)) # %% Plot raw_data_path = os.path.abspath(os.curdir) + "/data/rawData.csv"
def result_data_old(history, solver: ODESolver, compare_data=exp_data_s, nr_population=1, sample_size=500, savefig='False'): """ Visualise SMC population from infer-back populations :param solver: ODE solver object :param savefig: Filename of output figure. If 'False' then no figure will be saved :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) # 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=(6, 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') axs[kk].plot(solver.time_point, df_mean.iloc[:, kk], 'b', label="Mean", alpha=0.6) axs[kk].scatter(solver.time_point_default, compare_data[index_cov[kk]], alpha=0.7, marker='^', color='black') # 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]) # fig.tight_layout(pad=5.0) if savefig != 'False': plt.savefig(savefig + ".png", dpi=200) plt.show()
# 'lambdaM': 2.213837884117815, # 'lambdaN': 7.260925726829641, # 'muA': 18.94626522780349, # 'muB': 2.092860392215201, # 'muM': 0.17722330053184654, # 'muN': 0.0023917569160019844, # 'sAM': 10.228522400429998, # 'sBN': 4.034313992927392, # 'vNM': 0.3091883041193706} # Using default time points solver = ODESolver() solver.time_point = solver.time_point_default # obs_data_noisy = solver.ode_model(para_true, flatten=True, add_noise=True) obs_data_raw = solver.ode_model(para_true1, flatten=True, add_noise=False) # obs_data_noisy_s = solver.ode_model(para_true1, flatten=False, add_noise=True) # obs_data_raw_s = solver.ode_model(para_true1, flatten=False, add_noise=False) print("Target data") print(obs_data_raw) # %% Calculate data range as factors: # range_N = obs_data_raw_s['N'].max() - obs_data_raw_s['N'].min() # range_M = obs_data_raw_s['M'].max() - obs_data_raw_s['M'].min() # range_B = obs_data_raw_s['B'].max() - obs_data_raw_s['B'].min() # range_A = obs_data_raw_s['A'].max() - obs_data_raw_s['A'].min() # factors = {}
rawData_path = os.path.abspath(os.curdir) + "/data/rawData.csv" rawData = pd.read_csv(rawData_path).astype("float32") # normalise_data(expData) print("Target data") print(exp_data) # Run a rough inference on the raw data solver = ODESolver() # Reload the timepoints to be calculated in ODE solver solver.time_point = solver.time_point_exp paraGuess = [1] * 12 distanceP2 = pyabc.PNormDistance(p=2) tmpData = solver.ode_model(para_list_to_dict(paraGuess)) def residual_ls(para: list): """ Define the residual :param para: parameter values :return: value of residual """ para_dict = para_list_to_dict(para) simulation_data = solver.ode_model(para_dict) # print(x) # print(simulation_data) # return distanceP2(simulation_data, expData) ans = [abs(np.nan_to_num((simulation_data[i] - exp_data[i]))) for i in range(48)] return ans