def compute_log_pys(drug_channel): print drug_channel seed = drugs_channels.index(drug_channel) print "seed =", seed npr.seed(seed) drug, channel = drug_channel num_expts, experiment_numbers, experiments = dr.load_crumb_data( drug, channel) concs = np.array([]) responses = np.array([]) for i in xrange(num_expts): concs = np.concatenate((concs, experiments[i][:, 0])) responses = np.concatenate((responses, experiments[i][:, 1])) print "responses:", responses num_pts = len(responses) pi_bit = dr.compute_pi_bit_of_log_likelihood(responses) n = 40 c = 3 temperatures = (np.arange(n + 1.) / n)**c num_models = 2 for model in xrange(1, num_models + 1): print "model", model log_pys = np.zeros((n + 1, 2)) log_pys[:, 0] = temperatures dr.define_model(model) log_py_file = dr.define_log_py_file(model, drug, channel) with open(log_py_file, "w") as outfile: outfile.write("# temperature, log(p(y|t))\n") for i, temperature in enumerate(temperatures): log_py = approx_log_py(responses, concs, num_pts, temperature, pi_bit, model, drug, channel) log_pys[i, 1] = log_py np.savetxt(log_py_file, log_pys) print "\n", log_pys return None
action='store_true', help='run hierarchical MCMC on all drugs and channels', default=False) parser.add_argument("--plot-data", action='store_true', help='plot data points on top of predicted curves', default=False) parser.add_argument( "--save-pdf", action='store_true', help= 'save dose-response curves figure as pdf (in addition to png), but the file will probably be massive', default=False) args = parser.parse_args() dr.define_model(args.model) temperature = 1 num_params = dr.num_params dr.setup(args.data_file) drugs_to_run, channels_to_run = dr.list_drug_channel_options(args.all) num_x_pts = 50 alpha = 0.002 # this is the lowest value I've found that actually shows anything for drug, channel in it.product(drugs_to_run, channels_to_run): try: num_expts, experiment_numbers, experiments = dr.load_crumb_data( drug, channel) except:
def do_plot(drug_channel): global concs, responses fig = plt.figure(figsize=(5, 8)) axes = {} axes[1] = fig.add_subplot(211) axes[2] = fig.add_subplot(212)#, sharey=axes[1]) fsize = 14 for model in xrange(1, num_models+1): dr.define_model(model) drug, channel = drug_channel num_expts, experiment_numbers, experiments = dr.load_crumb_data(drug, channel) figs_dir = dr.drug_channel_figs_dir(drug, channel) concs = np.array([]) responses = np.array([]) for i in xrange(num_expts): concs = np.concatenate((concs, experiments[i][:, 0])) responses = np.concatenate((responses, experiments[i][:, 1])) if model == 1: x0 = np.ones(2) sigma0 = 0.1 elif model == 2: x0 = np.copy([pic50, hill]) sigma0 = 0.01 #x0[0] = 6.9 opts = cma.CMAOptions() es = cma.CMAEvolutionStrategy(x0, sigma0, opts) while not es.stop(): X = es.ask() f_vals = [sum_of_square_diffs(x, model) for x in X] es.tell(X, f_vals) es.disp() res = es.result() ss = res[1] pic50, hill = res[0] if model == 1: hill = 1 conc_min = np.min(concs) conc_max = np.max(concs) num_pts = 501 x_range = np.logspace(int(np.log10(conc_min))-1, int(np.log10(conc_max))+2, num_pts) predicted = dr.dose_response_model(x_range, hill, dr.pic50_to_ic50(pic50)) #fig = plt.figure(figsize=(5,4)) #ax = fig.add_subplot(111) axes[model].grid() axes[model].set_xscale('log') axes[model].set_ylim(0,100) axes[model].set_ylabel(r"% {} block".format(channel),fontsize=fsize) axes[model].set_xlabel(r"{} concentration ($\mu$M)".format(drug),fontsize=fsize) axes[model].plot(x_range, predicted, color='blue', lw=2, label="Best fit") axes[model].plot(concs, responses, 'o', color='orange', ms=10, label="Expt data") axes[model].legend(loc=2) axes[model].set_title("$M_{}, pIC50 = {}, Hill = {}, SS = {}$".format(model, round(pic50,2), round(hill,2), round(ss,2)),fontsize=fsize) #axes[2].set_yticklabels([]) fig.tight_layout() #fig.savefig(figs_dir+"{}_{}_model_{}_best_fit.png".format(drug,channel,model)) fig.savefig(all_figs_dir+"{}_{}_best_fits.png".format(drug, channel)) fig.savefig(figs_dir+"{}_{}_best_fit.pdf".format(drug,channel)) plt.close()
def do_plot(drug_channel): global concs, responses fig = plt.figure(figsize=(5, 8)) axes = {} axes[1] = fig.add_subplot(211) axes[2] = fig.add_subplot(212) #, sharey=axes[1]) fsize = 14 for model in xrange(1, num_models + 1): dr.define_model(model) drug, channel = drug_channel num_expts, experiment_numbers, experiments = dr.load_crumb_data( drug, channel) figs_dir = dr.drug_channel_figs_dir(drug, channel) concs = np.array([]) responses = np.array([]) for i in xrange(num_expts): concs = np.concatenate((concs, experiments[i][:, 0])) responses = np.concatenate((responses, experiments[i][:, 1])) if model == 1: x0 = np.ones(2) sigma0 = 0.1 elif model == 2: x0 = np.copy([pic50, hill]) sigma0 = 0.01 #x0[0] = 6.9 opts = cma.CMAOptions() es = cma.CMAEvolutionStrategy(x0, sigma0, opts) while not es.stop(): X = es.ask() f_vals = [sum_of_square_diffs(x, model) for x in X] es.tell(X, f_vals) es.disp() res = es.result() ss = res[1] pic50, hill = res[0] if model == 1: hill = 1 conc_min = np.min(concs) conc_max = np.max(concs) num_pts = 501 x_range = np.logspace( int(np.log10(conc_min)) - 1, int(np.log10(conc_max)) + 2, num_pts) predicted = dr.dose_response_model(x_range, hill, dr.pic50_to_ic50(pic50)) #fig = plt.figure(figsize=(5,4)) #ax = fig.add_subplot(111) axes[model].grid() axes[model].set_xscale('log') axes[model].set_ylim(0, 100) axes[model].set_ylabel(r"% {} block".format(channel), fontsize=fsize) axes[model].set_xlabel(r"{} concentration ($\mu$M)".format(drug), fontsize=fsize) axes[model].plot(x_range, predicted, color='blue', lw=2, label="Best fit") axes[model].plot(concs, responses, 'o', color='orange', ms=10, label="Expt data") axes[model].legend(loc=2) axes[model].set_title("$M_{}, pIC50 = {}, Hill = {}, SS = {}$".format( model, round(pic50, 2), round(hill, 2), round(ss, 2)), fontsize=fsize) #axes[2].set_yticklabels([]) fig.tight_layout() #fig.savefig(figs_dir+"{}_{}_model_{}_best_fit.png".format(drug,channel,model)) fig.savefig(all_figs_dir + "{}_{}_best_fits.png".format(drug, channel)) fig.savefig(figs_dir + "{}_{}_best_fit.pdf".format(drug, channel)) plt.close()
#drug = 'Amiodarone' #channel = 'hERG' num_expts, experiment_numbers, experiments = dr.load_crumb_data(drug, channel) concs = np.array([]) responses = np.array([]) for i in xrange(num_expts): concs = np.concatenate((concs, experiments[i][:, 0])) responses = np.concatenate((responses, experiments[i][:, 1])) pi_bit = dr.compute_pi_bit_of_log_likelihood(responses) num_pts = len(responses) for m in xrange(1, num_models + 1): dr.define_model(m) chain_dir = dr.chains_dir(m, drug, channel) chains = np.array(glob(chain_dir + "*_chain.txt")) temps = np.array([float(c.split("_")[-2]) for c in chains]) ordered_indices = np.argsort(temps) temps = temps[ordered_indices] chains = chains[ordered_indices] temps = temps[::2] chains = chains[::2] num_temps = len(temps) log_p_ys = np.zeros(num_temps) for i in xrange(num_temps): print i + 1, "/", num_temps chain = np.loadtxt(chains[i], usecols=range(dr.num_params))
responses = np.concatenate((responses, experiments[i][:, 1])) num_pts = len(responses) pi_bit = dr.compute_pi_bit_of_log_likelihood(responses) #model = 2 #int(sys.argv[1]) n = 40 c = 3 temperatures = (np.arange(n+1.)/n)**c num_models = 2 model = int(sys.argv[1]) dr.define_model(model) #prior_x = np.linspace(0,2,1001) #prior_pdf = st.logistic.pdf(prior_x, loc=dr.mu, scale=dr.s) #prior_pdf = st.fisk.pdf(prior_x, c=dr.beta, scale=dr.alpha, loc=0) for temperature in temperatures: chain_file = dr.define_chain_file(model, drug, channel, temperature) chain = do_mcmc(temperature) np.savetxt(chain_file, chain) """fig = plt.figure() ax = fig.add_subplot(111) ax.hist(chain[:,1],color='blue',edgecolor='blue',normed=True,bins=40,label="t = {}".format(temperature)) ax.plot(prior_x,prior_pdf,color='red',label='Prior',lw=2) ax.legend()
#drug = 'Amiodarone' #channel = 'hERG' num_expts, experiment_numbers, experiments = dr.load_crumb_data(drug, channel) concs = np.array([]) responses = np.array([]) for i in xrange(num_expts): concs = np.concatenate((concs, experiments[i][:, 0])) responses = np.concatenate((responses, experiments[i][:, 1])) pi_bit = dr.compute_pi_bit_of_log_likelihood(responses) num_pts = len(responses) for m in xrange(1, num_models+1): dr.define_model(m) chain_dir = dr.chains_dir(m, drug, channel) chains = np.array(glob(chain_dir+"*_chain.txt")) temps = np.array([float(c.split("_")[-2]) for c in chains]) ordered_indices = np.argsort(temps) temps = temps[ordered_indices] chains = chains[ordered_indices] temps = temps[::2] chains = chains[::2] num_temps = len(temps) log_p_ys = np.zeros(num_temps) for i in xrange(num_temps): print i+1, "/", num_temps chain = np.loadtxt(chains[i], usecols=range(dr.num_params))
import doseresponse as dr import matplotlib.pyplot as plt import os model = 2 dr.define_model(model) fsize = 14 priors_fig_dir = '../output/priors/' if not os.path.exists(priors_fig_dir): os.makedirs(priors_fig_dir) for i in xrange(dr.num_params): fig = plt.figure(figsize=(5, 4)) ax = fig.add_subplot(111) ax.plot(dr.prior_xs[i], dr.prior_pdfs[i], lw=2) xlower, xupper = dr.prior_xs[i][[0, -1]] ax.set_xlim(xlower, xupper) ax.grid() if i == 0: ax.set_xticks(range(int(xlower), int(xupper) + 1, 3)) ax.set_ylabel('Prior pdf', size=fsize) ax.set_xlabel(dr.labels[i], size=fsize) fig.tight_layout() fig.savefig(priors_fig_dir + '{}_prior.pdf'.format(dr.labels[i])) plt.close()
def plot_mcmc_samples(drug_channel): drug, channel = drug_channel fig = plt.figure(figsize=(5, 8)) axes = {} axes[1] = fig.add_subplot(211) axes[2] = fig.add_subplot(212) #drug = "Amiodarone" #channel = "hERG" # drug = "Lopinavir" # channel = "Kir2.1" num_models = 2 for model in xrange(1, num_models+1): dr.define_model(model) chain_file = dr.define_chain_file(model, drug, channel, temperature) num_expts, experiment_numbers, experiments = dr.load_crumb_data(drug, channel) figs_dir = dr.drug_channel_figs_dir(drug, channel) concs = np.array([]) responses = np.array([]) for i in xrange(num_expts): concs = np.concatenate((concs, experiments[i][:, 0])) responses = np.concatenate((responses, experiments[i][:, 1])) how_many_samples_to_plot = 1200 mcmc_samples = np.loadtxt(chain_file, usecols=range(dr.num_params)) saved_its = mcmc_samples.shape[0] sample_indices = npr.randint(0, saved_its, how_many_samples_to_plot) mcmc_samples = mcmc_samples[sample_indices] conc_min = np.min(concs) conc_max = np.max(concs) fsize = 14 num_pts = 101 x_range = np.logspace(int(np.log10(conc_min))-1, int(np.log10(conc_max))+2, num_pts) axes[model].set_xscale('log') axes[model].grid() axes[model].set_ylabel(r"% {} block".format(channel), fontsize=fsize) axes[model].set_xlabel(r"{} concentration ($\mu$M)".format(drug), fontsize=fsize) axes[model].set_ylim(0,100) for i in xrange(how_many_samples_to_plot): if model == 1: pic50 = mcmc_samples[i,0] hill = 1 title = "$M_1$, fixed $Hill=1$, varying $pIC50$" elif model == 2: pic50, hill = mcmc_samples[i,:2] title = "$M_2$, varying $pIC50$ and $Hill$" axes[model].plot(x_range, dr.dose_response_model(x_range, hill, dr.pic50_to_ic50(pic50)),color='black',alpha=0.01) axes[model].plot(concs, responses, 'o', color='orange', ms=10, label="Expt data") axes[model].set_title(title, fontsize = fsize) axes[model].legend(loc=2) #axes[2].set_yticklabels([]) fig.tight_layout() fig.savefig(all_figs_dir+'{}_{}_mcmc_samples.png'.format(drug, channel)) fig.savefig(figs_dir+'{}_{}_mcmc_samples.pdf'.format(drug,channel,model)) plt.close() return None
def plot_mcmc_samples(drug_channel): drug, channel = drug_channel fig = plt.figure(figsize=(5, 8)) axes = {} axes[1] = fig.add_subplot(211) axes[2] = fig.add_subplot(212) #drug = "Amiodarone" #channel = "hERG" # drug = "Lopinavir" # channel = "Kir2.1" num_models = 2 for model in xrange(1, num_models + 1): dr.define_model(model) chain_file = dr.define_chain_file(model, drug, channel, temperature) num_expts, experiment_numbers, experiments = dr.load_crumb_data( drug, channel) figs_dir = dr.drug_channel_figs_dir(drug, channel) concs = np.array([]) responses = np.array([]) for i in xrange(num_expts): concs = np.concatenate((concs, experiments[i][:, 0])) responses = np.concatenate((responses, experiments[i][:, 1])) how_many_samples_to_plot = 1200 mcmc_samples = np.loadtxt(chain_file, usecols=range(dr.num_params)) saved_its = mcmc_samples.shape[0] sample_indices = npr.randint(0, saved_its, how_many_samples_to_plot) mcmc_samples = mcmc_samples[sample_indices] conc_min = np.min(concs) conc_max = np.max(concs) fsize = 14 num_pts = 101 x_range = np.logspace( int(np.log10(conc_min)) - 1, int(np.log10(conc_max)) + 2, num_pts) axes[model].set_xscale('log') axes[model].grid() axes[model].set_ylabel(r"% {} block".format(channel), fontsize=fsize) axes[model].set_xlabel(r"{} concentration ($\mu$M)".format(drug), fontsize=fsize) axes[model].set_ylim(0, 100) for i in xrange(how_many_samples_to_plot): if model == 1: pic50 = mcmc_samples[i, 0] hill = 1 title = "$M_1$, fixed $Hill=1$, varying $pIC50$" elif model == 2: pic50, hill = mcmc_samples[i, :2] title = "$M_2$, varying $pIC50$ and $Hill$" axes[model].plot(x_range, dr.dose_response_model(x_range, hill, dr.pic50_to_ic50(pic50)), color='black', alpha=0.01) axes[model].plot(concs, responses, 'o', color='orange', ms=10, label="Expt data") axes[model].set_title(title, fontsize=fsize) axes[model].legend(loc=2) #axes[2].set_yticklabels([]) fig.tight_layout() fig.savefig(all_figs_dir + '{}_{}_mcmc_samples.png'.format(drug, channel)) fig.savefig(figs_dir + '{}_{}_mcmc_samples.pdf'.format(drug, channel, model)) plt.close() return None