def animated_gif(db, output): from pyabc import History from pyabc.visualization import plot_kde_matrix import matplotlib.pyplot as plt import subprocess import tempfile tempdir = tempfile.mkdtemp() print("tmpdir", tempdir) import os h_loaded = History("sqlite:///" + db) limits = dict(log_division_rate=(-3, -1), log_division_depth=(1, 3), log_initial_spheroid_radius=(0, 1.2), log_initial_quiescent_cell_fraction=(-5, 0), log_ecm_production_rate=(-5, 0), log_ecm_degradation_rate=(-5, 0), log_ecm_division_threshold=(-5, 0)) for t in range(h_loaded.n_populations): print("Plot population {t/h_loaded.n_populations}") df, w = h_loaded.get_distribution(m=0, t=t) plot_kde_matrix(df, w, limits=limits) plt.savefig(os.path.join(tempdir, f"{t:0>2}.png")) subprocess.run([ "convert", "-delay", "15", os.path.join(tempdir, "%02d.png"), output + ".gif" ])
# 'delay': DiscreteJumpTransition(domain=delay_domain), # 'n01': DiscreteJumpTransition(domain=n01_domain), # 'n02': DiscreteJumpTransition(domain=n02_domain), 'k': DiscreteJumpTransition(domain=k_domain, p_stay=.8), ('n01', 'n02', 'log_p', 'p_inf'): GridSearchCV() }) id = 'n=3e5_2' db = "sqlite:///" + os.path.join(os.getcwd(), id + ".db") abc = ABCSMC( model, prior, distance, transitions=transition, population_size=200, ) # abc.load(db, int(np.load('run_id_'+id+'.npy')))#, {'cases': data}) abc.load(db, 1) # history = abc.new(db, {'cases': data}) np.save('run_id_' + id + '.npy', abc.history.id) abc.run(max_nr_populations=3) history = abc.history print(history.max_t) df, w = history.get_distribution() # kde = GridSearchCV().fit(df, w) # print(kde.rvs()['log_p']) # df['p'] = 10 ** (-df['log_p']) plot_kde_matrix(df, w) plt.show()
return mean_dict if __name__ == '__main__': # load history h_loaded = History("sqlite:///" + "hft_abm_smc_abc/resultsReal_Data_Small_Test - Smaller Test - eps1_negfix_pop6_pop301597579353.943031.db") # check that the history is not empty print(h_loaded.all_runs()) from pyabc.visualization import plot_kde_matrix df, w = h_loaded.get_distribution(m=0, t=4) plot_kde_matrix(df, w); plt.show() fig, axs = plt.subplots(2, 3) plot_coonvergence(h_loaded, 'mu', MU_MIN, MU_MAX, MU_TRUE, ax=axs[0, 0]) plot_coonvergence(h_loaded, 'lambda0', LAMBDA0_MIN, LAMBDA0_MAX, LAMBDA0_TRUE, ax=axs[0, 1]) plot_coonvergence(h_loaded, 'delta', DELTA_MIN, DELTA_MAX, DELTA_TRUE, ax=axs[0, 2]) plot_coonvergence(h_loaded, 'delta_S', DELTAS_MIN, DELTAS_MAX, DELTA_S_TRUE, ax=axs[1, 0]) plot_coonvergence(h_loaded, 'alpha', ALPHA_MIN, ALPHA_MAX, ALPHA_TRUE, ax=axs[1, 1]) plot_coonvergence(h_loaded, 'C_lambda', C_LAMBDA_MIN, C_LAMBDA_MAX, C_LAMBDA_TRUE, ax=axs[1, 2]) plt.gcf().set_size_inches((12, 8)) plt.gcf().tight_layout() plt.show() _, arr_ax = plt.subplots(1, 2)
# Parameter inference using approximate Bayesian computation (pyABC) limits = dict(Omega=(0, 0.3), Probability=(0, 0.2), Lambda=(0, 1.5), Gamma=(0, 3)) parameter_prior = Distribution( **{key: RV("uniform", a, b - a) for key, (a, b) in limits.items()}) db_path = pyabc.create_sqlite_db_id(file_="glioblatomaLanModel_syn.db") abc = ABCSMC(models = determineTestParameters, \ parameter_priors = parameter_prior, \ distance_function = DistanceAfterBinning, \ population_size = 160, \ sampler = sampler.MulticoreParticleParallelSampler(), \ transitions = transition.LocalTransition(k_fraction=0.3)) abc.new(db_path, expBarFreq) h = abc.run(minimum_epsilon=0.1, max_nr_populations=10) df, w = h.get_distribution(m=0) plot_kde_matrix(df, w, limits=limits) plt.savefig('infer_result.pdf') plt.clf() pickle_out = open('result', 'wb') pickle.dump(h.get_distribution(), pickle_out) pickle_out.close() plot_epsilons(h) plt.savefig('epsilon_plot.pdf') plt.clf()
abc = ABCSMC(models = model, \ parameter_priors = parameter_prior, \ distance_function = DistanceAfterBinning, \ # population_size = 100, \ sampler = sampler.MulticoreParticleParallelSampler(), \ transitions = [transition.LocalTransition(k_fraction=0.3), transition.LocalTransition(k_fraction=0.3)]) abc.new(db_path, expBarFreq) h = abc.run(minimum_epsilon=0.1, max_nr_populations=5) pickle_out = open('model_probability', 'wb') pickle.dump(h.get_model_probabilities(), pickle_out) pickle_out.close() df, w = h.get_distribution(m=0) plot_kde_matrix(df, w, limits=LanPrior) plt.savefig('infer_result_MS_SCH.pdf') plt.clf() df, w = h.get_distribution(m=1) plot_kde_matrix(df, w, limits=QSCPrior) plt.savefig('infer_result_MS_QSC.pdf') plt.clf() #pickle_out = open('history_10G','wb') #pickle.dump(h, pickle_out) #pickle_out.close() #pickle_out = open('result_10G','wb') #pickle.dump(h.get_distribution(), pickle_out) #pickle_out.close()