def run_mcmc(ntemps=3,nwalkers=100,nsteps=100, graph=False): ndim = 2 pos = [result["x"] + 1e-4*np.random.randn(ndim) for i in range(nwalkers)] pos = np.reshape(np.tile(pos,ntemps),(ntemps,nwalkers,ndim)) sampler = emcee.PTSampler(ntemps, nwalkers, ndim, lnlike, lnprior,loglargs=[x,y,yerr]) sampler.run_mcmc(pos,nsteps) sampler.reset() sampler.run_mcmc(pos, nsteps) chains = sampler.flatchain samples = sampler.chain.reshape(ntemps*10000,2) mmed = np.median(chains[:,:,0]) mstd = np.std(chains[:,:,0]) bmed = np.median(chains[:,:,1]) bstd = np.std(chains[:,:,1]) if graph: xmc = np.linspace(0,10,1000) ymc = mmed*xmc + bmed plt.plot(xmc,ymc) corner.corner(samples, labels=["$m$", "$b$"], truths=[m_true, b_true]) return mstd, bstd, mmed, bmed
def plot_PDFtriangle(self,parameterset, labels): if parameterset=='10pars': figure = corner.corner(self.chain.flatchain, labels= labels, plot_contours=True, plot_datapoints = False, show_titles=True, quantiles=[0.16, 0.50, 0.84]) elif parameterset == 'int_lums': figure = corner.corner(self.int_lums.T, labels= labels, plot_contours=True, plot_datapoints = False, show_titles=True, quantiles=[0.16, 0.50, 0.84]) return figure
def plot_chain(sampler): # Check convergence plt.figure() plt.plot(sampler.lnprobability.T) # Plot maximum likelihood ml_ix = np.unravel_index(np.argmax(sampler.lnprobability), sampler.lnprobability.shape) ml_pos = sampler.chain[ml_ix] bid_pred = np.logspace(-1, 4, 100) plt.figure() plt.errorbar(np.log10(bid_concs), fret_means, yerr=fret_ses, color='k') plt.plot(np.log10(bid_pred), model_func(ml_pos, bid_pred), color='r') # Plot sampling of trajectories parameters plt.figure() num_plot_samples = 2500 num_tot_steps = sampler.flatchain.shape[0] for s_ix in range(num_plot_samples): p_ix = np.random.randint(num_tot_steps) p_samp = sampler.flatchain[p_ix] plt.plot(np.log10(bid_pred), model_func(p_samp, bid_pred), alpha=0.01, color='r') plt.errorbar(np.log10(bid_concs), fret_means, yerr=fret_ses, color='k', linewidth=2) # Triangle plots corner.corner(sampler.flatchain)
def marginal_plots(analyzer, minweight=1e-4, **kwargs): """ Create marginal plots * analyzer: A instance of pymultinest.Analyzer * d: if more than 20 dimensions, by default only 1d marginal distributions are plotted. set d=2 if you want to force a 2d matrix plot """ prefix = analyzer.outputfiles_basename parameters = json.load(open(prefix + 'params.json')) data = analyzer.get_data()[:,2:] weights = analyzer.get_data()[:,0] mask = weights > minweight with warnings.catch_warnings(): warnings.simplefilter("ignore") corner.corner(data[mask,:], weights=weights[mask], labels=parameters, show_titles=True, **kwargs) plt.savefig(prefix + 'corner.pdf') plt.savefig(prefix + 'corner.png') plt.close()
def perform_sample_manips(sampler, ndims, energy): """this function takes the mcmc sampler, grabs the chains from it, and generates plots and percentiles and most likely values from the sampled points""" # retrieve the samples num_samples = (CONFIG["Number of Walkers"] * (CONFIG["Sample Points"] - CONFIG["Burn-in Points"])) samples = sampler.chain[:, CONFIG["Burn-in Points"]:, :].reshape(( num_samples, ndims)) # save the samples to the disk in the sp if CONFIG["Save Chain Data"]: print "Saving MCMC samples for", energy, "MeV" chain_file_name = "" if CONFIG["Chain Directory"][-1] == '/': chain_file_name = CONFIG["Chain Directory"] +\ "A%d_chain_E%4.1f.npz" % (CONFIG["Target A"], energy) else: chain_file_name = CONFIG["Chain Directory"] +\ "/A%d_chain_E%4.1f.npz" % (CONFIG["Target A"], energy) np.savez_compressed(chain_file_name, sampler.chain) print "Done saving MCMC samples for", energy, "MeV" # extract the error bars quantile_list = np.array([(0.5 - CONFIG["Confidence Interval"] / 2.0), 0.5, (0.5 + CONFIG["Confidence Interval"] / 2.0)]) values = calc_param_values(samples, quantile_list, ndims) peak_vals = [param[0] for param in values[1]] # make the probability plots make_prob_plots(samples, energy, peak_vals) # make the corner plot if CONFIG["Generate Corner Plots"]: print "Commencing corner plot creation for", energy, "MeV" lbls = [r"$a_{%d}$" % i for i in range(ndims)] ranges = [(0.00, (0.0001 + samples[:, i].max())) for i in range(ndims)] fig = None if CONFIG["Corner Plot Samples"] >= num_samples: fig = corner.corner(samples, labels=lbls, range=ranges, quantiles=quantile_list, truths=peak_vals, verbose=False) else: # randomize the sample array and then extract the first chunk of it np.random.shuffle(samples) fig = corner.corner(samples[0:CONFIG["Corner Plot Samples"]], labels=lbls, range=ranges, quantiles=quantile_list, truths=peak_vals, verbose=False) # make the corner plot file_name if CONFIG["Corner Plots Directory"][-1] == '/': fig_file_name = CONFIG["Corner Plots Directory"] +\ "A%d_corner_E%4.1f.%s" % (CONFIG["Target A"], energy, CONFIG["Plot Format"]) else: fig_file_name = CONFIG["Corner Plots Directory"] +\ "/A%d_corner_E%4.1f.%s" % (CONFIG["Target A"], energy, CONFIG["Plot Format"]) fig.savefig(fig_file_name, bbox_inches='tight') plt.close(fig) print "Done creating corner plot for", energy, "MeV" # return the point and the errors return values
def doplot(self, name): """ Do some plots """ self.trace = pickle.load( open( name, "rb" ) ) var = np.vstack([self.trace['muCB'][:,0], self.trace['muCB'][:,1], self.trace['sdCB'][:,0], self.trace['sdCB'][:,1]]).T corner.corner(var, labels=['$\mu_C$', '$\mu_B$', '$\sigma_C$','$\sigma_B$'], show_titles=True) pl.show() # pl.savefig('{0}.png'.format(name)) # Just get the first N samples. We shuffle the # arrays and get the subsamples C = self.trace['CB'][:,:,0] np.random.shuffle(C) C_slice = C[0:200,:].flatten() B = self.trace['CB'][:,:,1] np.random.shuffle(B) B_slice = B[0:200,:].flatten() # First option pl.plot(B_slice, C_slice, '.', alpha=0.002) pl.show() # KDE joint plot sns.jointplot(C_slice, B_slice, kind='kde') pl.show()
def plot_chain(sampler): # Check convergence plt.figure() plt.plot(sampler.lnprobability.T) # Plot maximum likelihood plt.figure() ml_ix = np.unravel_index(np.argmax(sampler.lnprobability), sampler.lnprobability.shape) ml_pos = sampler.chain[ml_ix] for d_ix, data_i in enumerate(data): plt.plot(time, data_i) plt.plot(time, fit_func(ml_pos[d_ix*3:(d_ix+1)*3]), color='k') # Plot sampling of trajectories parameters plt.figure() for d_ix, data_i in enumerate(data): plt.plot(time, data_i) num_plot_samples = 100 num_tot_steps = sampler.flatchain.shape[0] for s_ix in range(num_plot_samples): p_ix = np.random.randint(num_tot_steps) p_samp = sampler.flatchain[p_ix] for d_ix in range(len(data)): plt.plot(time, fit_func(p_samp[d_ix*3:(d_ix+1)*3]), alpha=0.1, color='k') # Triangle plots for d_ix in range(len(data)): corner.corner(sampler.flatchain[:,d_ix*3:(d_ix+1)*3]) corner.corner(sampler.flatchain[:,3*len(data):])
def triangle(self, **kws): if self.samples is None: logging.warn('Need to run the fit method first!') return samples = self.samples[self.param_names].as_matrix() corner.corner(samples, labels=self.param_names, **kws) return
def plot_each_spot(self): #fig, ax = plt.subplots(5) n_spots = self.radius.shape[1] burn_in_to_index = int(self.burnin*self.radius.shape[0]) for i in range(n_spots): samples = np.array([self.radius[burn_in_to_index:, i], self.lon[burn_in_to_index:, i]]).T # self.lat[:, i], corner.corner(samples)
def main(N): Nstr = "{:04d}".format(N) print("main: making fake data") np.random.seed(23) data = make_fake_data(N) empiricalmean = np.mean(data) empiricalvar = np.sum((data - empiricalmean) ** 2) / (N - 1) stats = np.array([empiricalmean, empiricalvar]) print(data, stats) print("main: running correct MCMC") pars0 = Truth correct_mcmc_samples = mcmc(pars0, ln_correct_posterior, 2 ** 16, (data, )) accept = correct_mcmc_samples[1:] == correct_mcmc_samples[:-1] print("acceptance ratio", np.mean(accept)) print(correct_mcmc_samples) print("main: plotting correct posterior samples") labels = ["mean", "var"] ranges = [prior_info[0:2], prior_info[2:4]] fig = corner(correct_mcmc_samples, bins=128, labels=labels, range=ranges) pfn = "./correct_{}.png".format(Nstr) fig.savefig(pfn) print(pfn) print("main: computing meanvar and varvar") M = 2 ** 16 print(N, M) means = np.zeros(M) vars = np.zeros(M) for m in range(M): foo = stats[0] + np.sqrt(stats[1]) * np.random.normal(size=N) em = np.mean(foo) ev = np.sum((foo - em) ** 2) / (N - 1) means[m] = em vars[m] = ev variances = np.array([np.var(means), np.var(vars)]) print(variances) print("main: running pseudo MCMC") pars0 = Truth pseudo_mcmc_samples = mcmc(pars0, ln_pseudo_posterior, 2 ** 16, (stats, variances, )) accept = pseudo_mcmc_samples[1:] == pseudo_mcmc_samples[:-1] print("acceptance ratio", np.mean(accept)) print(pseudo_mcmc_samples) print("main: plotting pseudo posterior samples") labels = ["mean", "var"] ranges = [prior_info[0:2], prior_info[2:4]] fig = corner(pseudo_mcmc_samples, bins=128, labels=labels, range=ranges) pfn = "./pseudo_{}.png".format(Nstr) fig.savefig(pfn) print(pfn)
def plot_triangle(samples): import corner if samples.shape[1] == 2: fig = corner.corner(samples, labels=["$t_0$", "$P$"]) if samples.shape[1] == 6: fig = corner.corner(samples, labels=["$t_0$", r"depth", r"duration", r"$b$", "$q_1$", "$q_2$"]) elif samples.shape[1] == 8: fig = corner.corner( samples, labels=["$t_0$", r"depth", r"duration", r"$b$", "$q_1$", "$q_2$", "$q_3$", "$q_4$"] ) plt.show()
def triangle(self, **kws): """ Make a triangle plot from the samples. All keyword arguments are passed to corner.corner() """ if self.samples is None: logging.warn('Need to run the fit method first!') return samples = self.samples[self.param_names].as_matrix() corner.corner(samples, labels=self.param_names, **kws) return
def corner_plot(data,results_dir,burnin,disp,params2sample): """ Plot the corner plot """ ndim=len(data[0,0,:]) #Reshape because samples should be a 2 dimension array to be given to the triangle.corner function samples = data[:, burnin:, :].reshape((-1, ndim)) if ndim == 3: fig = triangle.corner(samples, bins=20,labels=["$gamma_i$","$omi$", "$slopei$"], quantiles=[0.16, 0.5, 0.84], plot_contours = True, plot_datapoints = False, plot_ellipse = False, levels=[0.68,0.95,0.99], range=[1.,1.,1.],color='blue', show_titles=True, title_fmt='.2e',title_kwargs={"fontsize": 12}) elif ndim == 4: if 'zeta' in params2sample: fig = triangle.corner(samples, bins=20,labels=["$gamma_i$","$omi$", "$slopei$","zeta"], quantiles=[0.16, 0.5, 0.84], plot_contours = True, plot_datapoints = False, plot_ellipse = False, levels=[0.68,0.95,0.99], range=[1.,1.,1.,1.],color='blue', show_titles=True, title_fmt='.2e',title_kwargs={"fontsize": 12}) else: fig = triangle.corner(samples, bins=20,labels=["$gamma_i$","$omi$", "$slopei$","n"], quantiles=[0.16, 0.5, 0.84], plot_contours = True, plot_datapoints = False, plot_ellipse = False, levels=[0.68,0.95,0.99], range=[1.,1.,1.,1.],color='blue', show_titles=True, title_fmt='.2e',title_kwargs={"fontsize": 12}) elif ndim==5: fig = triangle.corner(samples, bins=20,labels=["$gamma_i$","$omi$", "$slopei$","n","zeta"], quantiles=[0.16, 0.5, 0.84], plot_contours = True, plot_datapoints = False, plot_ellipse = False, levels=[0.68,0.95,0.99], range=[1.,1.,1.,1.,1.],color='blue', show_titles=True, title_fmt='.2e',title_kwargs={"fontsize": 12}) fig.set_size_inches(12,12) fig.savefig('%s/line-triangle_corr.png' % (results_dir)) if disp: plt.show()
def corner(samples, variables, param_order): # Make the triangle plot. variable_names = [] for p in param_order: if variables[p]['vary']: variable_names.append(variables[p]['texname']) ndim = len(variable_names) fig, axes = plt.subplots(ndim, ndim, figsize=(12.5,9)) triangle.corner(samples, bins=20, labels=variable_names, max_n_ticks=3,plot_contours=True,quantiles=[0.16,0.5,0.84],fig=fig, show_titles=True,verbose=True)
def plot_corner_posteriors(self, savefile=None, labels=["T1", "R1", "Av", "T2", "R2"]): ''' Plots the corner plot of the MCMC results. ''' ndim = len(self.sampler.flatchain[0,:]) chain = self.sampler samples = chain.flatchain samples = samples[:,0:ndim] plt.figure(figsize=(8,8)) fig = corner.corner(samples, labels=labels[0:ndim]) plt.title("MJD: %.2f"%self.mjd) name = self._get_save_path(savefile, "mcmc_posteriors") plt.savefig(name) plt.close("all") plt.figure(figsize=(8,ndim*3)) for n in range(ndim): plt.subplot(ndim,1,n+1) chain = self.sampler.chain[:,:,n] nwalk, nit = chain.shape for i in np.arange(nwalk): plt.plot(chain[i], lw=0.1) plt.ylabel(labels[n]) plt.xlabel("Iteration") name_walkers = self._get_save_path(savefile, "mcmc_walkers") plt.tight_layout() plt.savefig(name_walkers) plt.close("all")
def plotMCMC(chain, modelFunc, modelArgs, modelX, obsY, argName=None, burnin=100, saveDIR=None): """plot MCMC result Keyword Arguments: chain -- mcmc result modelFunc -- model function modelArgs -- model function arguments modelX -- x axis argument for the plot obsY -- observed value argName -- (default None) burnin -- (default 100) burn in steps saveDIR -- (default None) save directory, if None, don't save """ chainShape = chain.shape nDim = chainShape[2] chain = chain[:, burnin:, :].reshape((-1, nDim)) # remove the buring in part # figure 1 figure plot if argName is None: # default arg label is empty argName = [''] * nDim fig1 = corner.corner(chain, labels=argName) # figure 2 fig2 = plt.figure() ax = fig2.add_subplot(111) nRandModel = 50 # plot 50 random model for i in np.random.randint(chain.shape[0], size=nRandModel): ax.plot(modelX, modelFunc(chain[i, :], *modelArgs), color='k', alpha=0.1) ax.plot(modelX, obsY, marker='s', lw=0) if saveDIR is not None: fig1.savefig(path.join(saveDIR, 'cornerPlot.png')) fig2.savefig(path.join(saveDIR, 'modelPlot.png')) return fig1, fig2
def fit_all_single(data_time, data_flux, mask_low, mask_high, initguess_1, burn_1_steps=250, burn_2_steps=250, production_steps=1000, nwalkers=100, best_plot='SAVE', triangle='SAVE', signal_label='6'): mask = np.where((data_time > mask_low) & (data_time < mask_high)) x = data_time[mask] - data_time[mask][0] y = data_flux[mask] for t_1 in read_templates(): t_1_func = make_template_func(t_1) nll = lambda *args: -lnlike_1_no_err(*args) result = op.minimize(nll, initguess_1, args=(x, y, t_1_func), bounds=[(-0.05, 0.05), (0.0, 0.1), (0.0, None), (0.85, 1.15)], method='COBYLA') initguess = result["x"] ndim = 4 pos = [initguess + 1e-4*np.random.randn(ndim) for i in range(nwalkers)] sampler = emcee.EnsembleSampler(nwalkers, ndim, lnprob_1_no_err, args=(x, y, t_1_func)) print 'Running Burn-in 1' p0, prob, state = sampler.run_mcmc(pos, burn_1_steps) sampler.reset() print 'Running Burn-in 2' p = p0[np.argmax(prob)] p0 = [p + 1e-8 * np.random.randn(ndim) for i in xrange(nwalkers)] p0, _, _ = sampler.run_mcmc(p0, burn_2_steps) sampler.reset() print 'Running production' p0, prob, state = sampler.run_mcmc(p0, production_steps) p = p0[np.argmax(prob)] print (-2.0*np.max(prob)/3.0), p0[np.argmax(prob)] model_y = eval_template(p, x, t_1_func) plt.plot(x, y, '.') plt.plot(x, model_y, '-') plotname = 'Best Fit for ' + t_1[:-10] plot_save_name = "best_model_" + signal_label + '_' plot_save_name = plot_save_name + t_1[:-10] + '.png' plt.title(plotname) if best_plot == 'SAVE': plt.savefig(plot_save_name) else: plt.show() plt.clf() plt.close() samples = sampler.chain[:, 500:, :].reshape((-1, ndim)) fig = corner.corner(samples, labels=["$l$", "$w$", "$A$", "Offset"]) save_name = "triangle_" + signal_label + '_' + t_1[:-10] + '.png' trianglename = 'Posteriors for ' + t_1[:-10] plt.suptitle(trianglename) if triangle == 'SAVE': fig.savefig(save_name) else: plt.show() plt.clf() plt.close()
def plot_corner(nfree, nfixed, converted_samples, lnprob, means=False, stds=False, corrs=False, ages=False, weights=False, tstamp='nostamp'): """LEGACY Generate corner plots with dynamically generated parameter list e.g. ONly plotting stds or ages, or weights, or any combinations """ # Checking at least one value is being plotted if not (means or stds or corrs or ages or weights): print("Need to include a boolean flag for desired parameters") return 0 labels = generate_labels(nfree, nfixed) param_mask = generate_param_mask(nfree, nfixed, means, stds, corrs, ages, weights) best_ix = np.argmax(lnprob.flatten()) best_sample = converted_samples[best_ix] # fig = corner.corner(converted_samples[:, np.where(param_mask)], # truths = best_sample[np.where(param_mask)], # labels = labels[np.where(param_mask)] ) fig = corner.corner(converted_samples[:, np.where(param_mask)][:,0], truths = best_sample[np.where(param_mask)], labels = labels[np.where(param_mask)] ) file_name = "plots/{}_corner_{}_{}_{}.png".format(tstamp, nfree, nfixed, lnprob.shape[1]) pdb.set_trace() fig.savefig(file_name) fig.clf() return 0
def make_prob_plots(samples, energy, peak_vals): """this function takes the list of samples and makes histograms of the probability distributions of the parameters using matplotlib and writes those histograms to the specified directory""" ndims = len(samples[0]) lbls = [r"$a_{%d}$" % i for i in range(ndims)] ranges = [(-0.0001, (0.0001 + samples[:, i].max())) for i in range(ndims)] quantile_list = np.array([(0.5 - CONFIG["Confidence Interval"] / 2.0), 0.5, (0.5 + CONFIG["Confidence Interval"] / 2.0)]) for i in range(ndims): temp = samples[:, i] fig = corner.corner(temp, labels=[lbls[i]], range=[ranges[i]], quantiles=quantile_list, truths=[peak_vals[i]], verbose=False, bins=CONFIG["Num Bins"]) # make the probability plot file_name fig_file_name = None if CONFIG["Prob Plots Directory"][-1] == '/': fig_file_name = CONFIG["Prob Plots Directory"] +\ "A%d_prob_en_%4.1f_a%02d.%s" % (CONFIG["Target A"], energy, i, CONFIG["Plot Format"]) else: fig_file_name = CONFIG["Prob Plots Directory"] +\ "/A%d_prob_en_%4.1f_a%02d.%s" % (CONFIG["Target A"], energy, i, CONFIG["Plot Format"]) fig.savefig(fig_file_name, bbox_inches='tight') plt.close(fig)
def mcmc_allstars(v_obs, s_obs, filename=None, nwalkers = 100, ntrials = 1000, method='Nelder-Mead'): vmax, log_smax = dispersion_mean(v_obs, s_obs) smax = np.exp(log_smax) def lnprior_all(vp): if sum(vp[i]>(vmax-5*smax) for i in xrange(len(vp))) and sum(vp[i]<(vmax+5*smax) for i in xrange(len(vp))): return 0.0 else: return -np.inf def lnprob_all(v_sample): lp = lnprior_all(v_sample) if not np.isfinite(lp): return -np.inf return lp - log_p([vmax, log_smax], v_sample, s_obs) #subtracted because recall log_p above is NEGATIVE of log of likelihood #s_obs is measured errors - not sampling anything here ndim = len(v_obs) pos = [v_obs + 0.1*np.random.random(ndim) for i in range(int(nwalkers))] sampler = emcee.EnsembleSampler(int(nwalkers), ndim, lnprob_all) sampler.run_mcmc(pos, int(ntrials)) samples = sampler.chain[:, 50:, :].reshape((-1, ndim)) #removes first 50 TRIALS mean_logstd = [dispersion_mean(samples[row], s_obs, opt_method = method) for row in xrange(len(samples))] mean_std = [np.array([mean_logstd[row][0],np.exp(mean_logstd[row][1])]) for row in xrange(len(mean_logstd))] if filename != None: fig = corner.corner(mean_std, labels=["$v_p$", "$s_p$"], truths=[vmax, smax], truth_color='r', range=((vmax-4,vmax+4),(0,max(3*smax,3)))) fig.savefig(filename) return samples, np.array(mean_std)
def plot_corner(sampler, burnin=100, ndim=3): samples = sampler.chain[:, burnin:, :].reshape((-1, ndim)) fig = corner.corner(samples, labels=["$m$", "$b$", "$\ln\,f$"], ) for ax in fig.axes: ax.minorticks_on() ax.grid() return fig
def triangle(self, keys=None, truths=None, quantiles=[0.16, 0.5, 0.84], title=None, dpi=70, **kwargs): data = [] for k in keys: try: ok *= np.isfinite(self.samples[k]) except UnboundLocalError: ok = np.isfinite(self.samples[k]) for k in keys: data.append(self.samples[k][ok]) data = np.vstack(data).transpose() # make the plot figure = corner.corner(data, labels=keys, truths=truths, quantiles=quantiles, title_args={"fontsize": 8}, levels = 1.0 - np.exp(-0.5 * np.array([1.0,2.0]) ** 2), **kwargs) figure.set_dpi(dpi) plt.draw() if title is not None: figure.gca().annotate(title, xy=(0.5, 1.0), xycoords="figure fraction", xytext=(0, -5), textcoords="offset points", ha="center", va="top") return figure
def _run_corner(nm, pandas=False, N=10000, seed=1234, ndim=3, ret=False, factor=None, **kwargs): print(" .. {0}".format(nm)) if not os.path.exists(FIGURE_PATH): os.makedirs(FIGURE_PATH) np.random.seed(seed) data1 = np.random.randn(ndim*4*N/5.).reshape([4*N/5., ndim]) data2 = (5 * np.random.rand(ndim)[None, :] + np.random.randn(ndim*N/5.).reshape([N/5., ndim])) data = np.vstack([data1, data2]) if factor is not None: data[:, 0] *= factor data[:, 1] /= factor if pandas: data = pd.DataFrame.from_items(zip(map("d{0}".format, range(ndim)), data.T)) fig = corner.corner(data, **kwargs) fig.savefig(os.path.join(FIGURE_PATH, "corner_{0}.png".format(nm))) if ret: return fig else: pl.close(fig)
def plot_thetas(theta , w , t): fig = corner.corner( theta, weights = w.flatten() , truths= data_hod, labels=[r'$\logM_{0}$',r'$\sigma_{\logM}$',r'$\logM_{min}$',r'$\alpha$',r'$\logM_{1}$'], range=plot_range , quantiles=[0.16,0.5,0.84], show_titles=True, title_args={"fontsize": 12},plot_datapoints=True, fill_contours=True, levels=[0.68, 0.95], color = 'b' , bins =20 , smooth = 1.) plt.savefig("/home/mj/public_html/nbar_clustering_Mr20_t"+str(t)+".png") plt.close() fig = corner.corner( theta, truths= data_hod, labels=[r'$\logM_{0}$',r'$\sigma_{\logM}$',r'$\logM_{min}$',r'$\alpha$',r'$\logM_{1}$'], range=plot_range , quantiles=[0.16,0.5,0.84], show_titles=True, title_args={"fontsize": 12},plot_datapoints=True, fill_contours=True, levels=[0.68, 0.95], color = 'b' , bins =20 , smooth = 1.) plt.savefig("/home/mj/public_html/nbar_clustering_Mr20_now_t"+str(t)+".png") plt.close() np.savetxt("/home/mj/public_html/nbar_clustering_Mr20_theta_t"+str(t)+".dat" , theta) np.savetxt("/home/mj/public_html/nbar_clustering_Mr20_w_t"+str(t)+".dat" , w)
def subcorner(sample_results, sps, model, extra_output, flatchain, outname=None, showpars=None, **kwargs): """ Make a corner plot of the (thinned, latter) samples of the posterior parameter space. Optionally make the plot only for a supplied subset of the parameters. """ ### make title font slightly smaller title_kwargs = {'fontsize': 'medium'} ### transform to the "fun" variables! flatchain, parnames = transform_chain(flatchain,model) extents = return_extent(flatchain) fig = corner.corner(flatchain, labels = parnames, levels=[0.68,0.95,0.997], fill_contours=True, color='#0038A8', quantiles=[0.16, 0.5, 0.84], verbose = False, hist_kwargs=dict(color='k'), show_titles = True, plot_datapoints=False, title_kwargs=title_kwargs, **kwargs) fig = add_to_corner(fig, sample_results, extra_output, sps, model, outname=outname, maxprob=True, title_kwargs=title_kwargs) if outname is not None: fig.savefig('{0}.corner.png'.format(outname)) plt.close(fig) else: return fig
def triangle_plot(data, labels, **kwargs): print(kwargs) corner_plt_kwds = {'show_titles': True, 'title_args': {"fontsize": 12}} if kwargs is not None: corner_plt_kwds.update(kwargs) fig = corner.corner(data, labels=labels, **corner_plt_kwds) return fig
def plotABC(run, T, prior='flex'): ''' Corner plots of ABC runs ''' # thetas abcout = ABC.readABC(run, T) theta_med = [UT.median(abcout['theta'][:, i], weights=abcout['w'][:]) for i in range(len(abcout['theta'][0]))] theta_info = ABC.Theta(prior=prior) prior_obj = ABC.Prior(prior) # prior prior_range = [(prior_obj.min[i], prior_obj.max[i]) for i in range(len(prior_obj.min))] # figure name fig = DFM.corner( abcout['theta'], # thetas weights=abcout['w'].flatten(), # weights truths=theta_med, truth_color='#ee6a50', # median theta labels=theta_info['label'], label_kwargs={'fontsize': 25}, range=prior_range, quantiles=[0.16,0.5,0.84], show_titles=True, title_args={"fontsize": 12}, plot_datapoints=True, fill_contours=True, levels=[0.68, 0.95], color='#ee6a50', bins=20, smooth=1.0) fig_name = ''.join([UT.dat_dir(), 'abc/', run, '/', 't', str(T), '.', run , '.png']) fig.savefig(fig_name, bbox_inches="tight") plt.close() return None
def analyze(parameters, datasets): image_path = os.path.join('Data', parameters['sumatra_label']) # Save traces trace_file = str(os.path.join('Data', parameters['sumatra_label'], 'traces.h5')) data_dict = OrderedDict() os.makedirs(os.path.join(image_path, 'acf')) with tables.open_file(trace_file, mode='r') as data: parnames = [x for x in data.root.chain0.PyMCsamples.colnames if not x.startswith('Metropolis') and x != 'deviance'] for param in sorted(parnames): data_dict[param] = np.asarray(data.root.chain0.PyMCsamples.read(field=param), dtype='float') for param, trace in data_dict.items(): figure = plt.figure() figure.gca().plot(autocorr(trace)) figure.gca().set_title(param+' Autocorrelation') figure.savefig(str(os.path.join(image_path, 'acf', param+'.png'))) plt.close(figure) output_files.append(str(os.path.join(parameters['sumatra_label'], 'acf', param+'.png'))) data = np.vstack(list(data_dict.values())).T data_truths = [parameters.as_dict()['parameters'][key].get('compare', None) for key in data_dict.keys()] figure = corner(data, labels=list(data_dict.keys()), quantiles=[0.16, 0.5, 0.84], truths=data_truths, show_titles=True, title_args={"fontsize": 40}, rasterized=True) figure.savefig(str(os.path.join(image_path, 'cornerplot.png'))) output_files.append(str(os.path.join(parameters['sumatra_label'], 'cornerplot.png'))) plt.close(figure) # Write CSV file with parameter summary (should be close to pymc's format) with open(str(os.path.join(image_path, 'parameters.csv')), 'w') as csvfile: fieldnames = ['Parameter', 'Mean', 'SD', 'Lower 95% HPD', 'Upper 95% HPD', 'MC error', 'q2.5', 'q25', 'q50', 'q75', 'q97.5'] writer = csv.DictWriter(csvfile, fieldnames) writer.writeheader() for parname, trace in data_dict.items(): qxx = utils.quantiles(trace, qlist=(2.5, 25, 50, 75, 97.5)) q2d5, q25, q50, q75, q975 = qxx[2.5], qxx[25], qxx[50], qxx[75], qxx[97.5] lower_hpd, upper_hpd = utils.hpd(trace, 0.05) row = { 'Parameter': parname, 'Mean': trace.mean(0), 'SD': trace.std(0), 'Lower 95% HPD': lower_hpd, 'Upper 95% HPD': upper_hpd, 'MC error': batchsd(trace, min(len(trace), 100)), 'q2.5': q2d5, 'q25': q25, 'q50': q50, 'q75': q75, 'q97.5': q975 } writer.writerow(row) output_files.append(str(os.path.join(parameters['sumatra_label'], 'parameters.csv'))) # Generate comparison figures os.makedirs(os.path.join(image_path, 'results')) input_database = Database(parameters['input_database']) compare_databases = {key: Database(value) for key, value in parameters['compare_databases'].items()} idx = 1 for fig in plot_results(input_database, datasets, data_dict, databases=compare_databases): fig.savefig(str(os.path.join(image_path, 'results', 'Figure{}.png'.format(idx)))) output_files.append(str(os.path.join(parameters['sumatra_label'], 'results', 'Figure{}.png'.format(idx)))) plt.close(fig) idx += 1
def plot_corner(self): #fig = plt.figure(figsize=(14, 14)) exclude_columns = np.array([0, 1, 2, 3, 5, 8, 11, 14, 17]) include_columns = np.ones(self.table.shape[1]) include_columns[exclude_columns] = 0 fig = corner.corner(self.table[:, include_columns.astype(bool)])#, fig=fig) return fig
ax.plot(cos2, f * (b * cos2**2 + a * cos2 + 1), color="k", alpha=0.1) ax.plot(cos2, f_true * (b_true * cos2**2 + a_true * cos2 + 1), color="r", lw=2, alpha=0.8) ax.errorbar(cos2, y, yerr=s125_fit_error, fmt=".k") ax.set_xlabel(r'$\log S_{125}$') ax.set_ylabel(r'Intensity') fig.savefig('mcmc_samples.png') fig = plt.figure(figsize=(10, 6)) mpl.rc("font", family="serif", size=fsize) ax = fig.add_subplot(111) ax.grid(True) fig = corner.corner(sample, labels=["$a$", "$b$", "$s38$"], truths=[alpha, beta, a3]) fig.savefig("corner_mcmc.png") a_mcmc, b_mcmc, s38_mcmc = map( lambda v: (v[1], v[2] - v[1], v[1] - v[0]), zip(*np.percentile(sample, [16, 50, 84], axis=0))) #(mid_value, +error, -error) print("a = %f + %f - %f\n" % (a_mcmc[0], a_mcmc[1], a_mcmc[2])) print("b = %f + %f - %f\n" % (b_mcmc[0], b_mcmc[1], b_mcmc[2])) print("s38 = %f + %f - %f\n" % (s38_mcmc[0], s38_mcmc[1], s38_mcmc[2]))
i = 3 else: i = max(0, int(-numpy.floor(numpy.log10(sigma))) + 1) fmt = '%%.%df' % i fmts = '\t'.join([' %-15s' + fmt + " +- " + fmt]) print(fmts % (p, med, sigma)) print('creating marginal plot ...') data = a.get_data()[:, 2:] weights = a.get_data()[:, 0] #mask = weights.cumsum() > 1e-5 mask = weights > 1e-4 corner.corner(data[mask, :], weights=weights[mask], labels=parameters, show_titles=True) if not os.path.exists(prefix[:-1] + "_results"): os.mkdir(prefix[:-1] + "_results") else: for i in os.listdir('.'): shutil.rmtree(prefix[:-1] + "_results") os.mkdir(prefix[:-1] + "_results") plt.savefig(prefix[:-1] + "_results/" + prefix + 'corner.pdf') plt.savefig(prefix[:-1] + "_results/" + prefix + 'corner.png') plt.close() # IMPORTING DATA freq = model_runfile.freq obs_full_signal = model_runfile.signal
Fg_a, uv_d_a, alg_a = 0.010, 15. , 2 Fg_b, uv_d_b, alg_b = 0.2, 100., 6 fg_true = 0.089 uv_true = 27.55 alg_true = 3.7 pos_0 = np.array([0,0,0]) #initial parameters, normal distribution around the true values pos = [pos_0 + np.array([np.random.normal(fg_true,0.02),np.random.normal(uv_true,10),np.random.normal(alg_true,0.2)]) for i in range(nwalkers)] #Plot initial fig = corner.corner(pos, labels=[r"$F_{gauss}$", r"$\sigma$",r"$\alpha_{gauss}$"], extents= [[Fg_a,Fg_b],[uv_d_a,uv_d_b],[alg_a,alg_b]], truths=[fg_true, uv_true,alg_true]) fig.set_size_inches(10,10) plt.show() fig.savefig("triangle_per50_ini_fdadfix.pdf") # Define the posterior PDF # Reminder: post_pdf(theta, data) = likelihood(data, theta) * prior_pdf(theta) # We take the logarithm since emcee needs it. # As prior, we assume an 'uniform' prior (i.e. constant prob. density) def lnprior(theta): Fg0, uv_d0, alg0 = theta if Fg_a < Fg0 < Fg_b and uv_d_a < uv_d0 < uv_d_b and alg_a < alg0 < alg_b : return 0.0
def runMCMC(self, sp, **kwargs): #MCMC the lorenztian fit then measure the EW""" #create fake data # params = kwargs.get('guess', self.parameters) params = np.array(params) #noise=kwargs.get('noise', spectrum.noise.value) ndim = self.nlines * 4 nwalkers = kwargs.get('nwalkers', 10) p0 = [[] for n in range(0, nwalkers)] p0[0] = params for n in range(1, nwalkers): p0[n] = [p + np.random.random(1)[0] * 0.0001 * p for p in params] p0 = np.array(p0) #print 'initial conditions',p0 print("nwalkers", nwalkers) nsamples = kwargs.get('nsamples', 10000) print("samples", nsamples) sampler = emcee.EnsembleSampler(nwalkers, ndim, self.lnprob, args=[sp]) ps, lnps, rstate = sampler.run_mcmc(p0, nsamples) samples = sampler.chain.reshape((-1, ndim)) pmean = [np.mean((samples.T)[i]) for i in range(0, ndim)] #ps_meanunc=[np.std((samples.T)[i]) for i in range(0, ndim)] pf = [np.mean(((samples.T)[i])[-10:]) for i in range(0, ndim)] #print "Fit parameters and uncertainty.........", zip(ps, ps_ers) initial_model = Model(parameters=p0[0], continuum=self.continuum, nlines=self.nlines) avg_model = Model(parameters=pmean, continuum=self.continuum, nlines=self.nlines) final_model = Model(parameters=pf, continuum=self.continuum, nlines=self.nlines) final_model.flux = final_model.lorentzian(sp.wave.value) self.flux = final_model.flux #USE the distribution to measure EQWS eqws = [[np.nan for i in np.arange(self.nlines)] ] #this is just to faciliate appending for p in ps: model = Model(parameters=p, continuum=self.continuum, nlines=self.nlines) model.flux = model.lorentzian(sp.wave.value) ctr, wdth, es = measure_EQW(model, sp.wave.value) eqws = np.append(eqws, [es], axis=0) #remove the first elt eqws = eqws[1:] EWs = [np.mean(x) for x in eqws.T] Unc = [np.std(x) for x in eqws.T] lndata = pd.DataFrame() lndata['Center'] = ctr lndata['Width'] = wdth lndata['EW'] = EWs lndata['EW_unc'] = Unc if kwargs.get('show_corner', False): plt.figure() labels = [[] for i in np.arange(4)] labels[0] = ["$h_" + str(i) + '$' for i in np.arange(ndim / 4)] labels[1] = ["$C_" + str(i) + '$' for i in np.arange(ndim / 4)] labels[2] = ["$\gamma" + str(i) + '$' for i in np.arange(ndim / 4)] labels[3] = ["$S_" + str(i) + '$' for i in np.arange(ndim / 4)] ls = np.concatenate(labels) fig = corner.corner(samples, labels=ls) fig.show() plt.figure() fig, ax = plt.subplots(ndim, sharex=True, figsize=(12, 6)) for i in range(ndim): ax[i].plot(sampler.chain[:, :, i].T, '-k', alpha=0.2) fig.show() if kwargs.get('show_fits', False): plt.figure() plt.plot(sp.wave, sp.flux, 'k', label='SPECTRUM', alpha=0.5) plt.plot(sp.wave.value, initial_model.lorentzian(sp.wave.value), '--r', label='GUESS', alpha=0.5) plt.plot(sp.wave.value, avg_model.lorentzian(sp.wave.value), 'g', label='MEAN FIT') plt.plot(sp.wave.value, final_model.flux, 'b', label='FINAL FIT') plt.legend() plt.show() return lndata
def plot_corner(dbf, trace, fname="corner.png"): param_labels = parameter_labels(dbf, formatted=True) fig = corner.corner(trace.reshape(-1, trace.shape[-1]), labels=param_labels) fig.savefig(fname) return fig
import pickle import corner #!!!Need to update #picklename = 'dump_fitting.pkl' picklename = 'result_vardha/' + 'l24_sersic.pkl' result = pickle.load(open(picklename, 'rb')) [ source_result, image_host, ps_result, image_ps, samples_mcmc, param_mcmc, paras ] = result #%% # here the (non-converged) MCMC chain of the non-linear parameters print 'The original plot:' [source_params_2, ps_param_2, mcmc_new_list, labels_new] = paras plot = corner.corner(mcmc_new_list, labels=labels_new, show_titles=True) plt.show() import astropy.io.fits as pyfits psf = pyfits.getdata('PSF0.fits') # Input the PSF0 to fit. ID = 'l106' agn_image = pyfits.getdata('{0}_cutout.fits'.format(ID)) fit_frame_size = 81 ct = (len(agn_image) - fit_frame_size) / 2 # If want to cut to 81, agn_image[ct:-ct,ct:-ct] agn_image = agn_image[ct:-ct, ct:-ct] kwargs_constraints = {'num_point_source_list': [1]} point_source_list = ['UNLENSED']
### run MCMC sampler ### samples = my_sampling(nbr_parameters, log_posterior) samples_full = np.append( samples, np.expand_dims(1.0 - samples.sum(axis=1), axis=1), axis=1 ) # add last parameter to samples, even though it is determined by the other ones # make histogram corner plot of MCMC result scores = np.array([int(score) for score in reviews]) if configs['show_figs'] or configs['save_figs']: tex_labels = [ r'$p_{' + str(i + 1) + r'}$' for i in range(nbr_parameters + 1) ] fig1 = corner.corner(samples_full, quantiles=[0.16, 0.5, 0.84], bins=configs['nbr_bins'], show_titles=True, title_fmt='.3f', labels=tex_labels) plt.tight_layout() # make plot of probability of rating ratings = samples_full @ scores.T fig2, ax = plt.subplots(1, 1, figsize=(6, 4)) ax.hist(ratings, density=True, bins=configs['nbr_bins']) ax.set_xlabel(r'rating') ax.set_ylabel(r'$p$(rating)') ax.set_xlim([1.0, 5.0]) plt.tight_layout() ### estimate p ### # mean estimate of p
min(ttdata) - 1, objects[1]['P'] + 1 ]) ocdata = ttdata - (epochs * lstats['marginals'][0]['median'] + lstats['marginals'][1]['median']) f = corner.corner( lposteriors[:, 2:], labels=['Period (day)', 'Mid-transit (day)'], bins=int(np.sqrt(lposteriors.shape[0])), range=[ (lstats['marginals'][0]['5sigma'][0], lstats['marginals'][0]['5sigma'][1]), (lstats['marginals'][1]['5sigma'][0], lstats['marginals'][1]['5sigma'][1]), ], #no_fill_contours=True, plot_contours=False, plot_density=False, data_kwargs={ 'c': lposteriors[:, 1], 'vmin': np.percentile(lposteriors[:, 1], 10), 'vmax': np.percentile(lposteriors[:, 1], 50) }, ) plt.show() dude() # estimate priors bounds = []
# in log-log space. MORAL: To fit for the right intercept the area # needs to be normalized under the histogram. hist, bin_edges = np.histogram(data, bins=3) bins = (bin_edges[:-1] + bin_edges[1:]) / 2 hist = hist / integrate.trapz(hist, bins) ndim, nwalkers = 2, 300 sampler = mcmc(ndim, nwalkers, np.log(bins), np.log(hist)) print("Ran the Markov chain! Plotting now..") # f1,ax1 = plt.subplots(1,1) # n = ax1.hist(data,bins=200,label='Histogram of masses drawn') # x = np.linspace(3,15,100); y = x**(-1.35); ynorm = max(n[0])*y/max(y) # ax1.plot(x,ynorm,'r',label='Power law IMF given') # ax1.set_xlabel(r'Mass [$M_{\odot}$]') # ax1.set_ylabel(r'$\xi(m)$') # ax1.legend() # f2,ax2 = plt.subplots(1,1) # ax2.loglog(bins,hist,label='Histogram') # ax2.loglog(x,ynorm,label='Given') # ax2.set_xlabel(r'Mass [$M_{\odot}$]') # ax2.set_ylabel(r'$\xi(m)$') # ax2.legend() f2 = corner.corner(sampler.flatchain, labels=('Mmax', 'alpha'), show_titles=True) plt.show()
sigmax2f = sigmax2.flatten() thetaf = theta.flatten() theta2f = theta2.flatten() sepbf = sepb.flatten() pabf = pab.flatten() sepcf = sepc.flatten() pacf = pac.flatten() # Make corner plot using "corner": import corner minshape = min(xcaf.shape, ycaf.shape, xcbf.shape, ycbf.shape, xccf.shape, yccf.shape, ampaf.shape, ampbf.shape, ampcf.shape, ampratiof.shape, bkgd.shape) ndim, nsamples = 18, minshape data = np.vstack([ xcaf, ycaf, xcbf, ycbf, xccf, yccf, dxf, dyf, ampaf, ampbf, ampcf, ampratiof, bkgdf, sigmaxf, sigmayf, sigmax2f, sigmay2f, thetaf, theta2f, sepbf, pabf, sepcf, pacf ]) data = data.transpose() # Plot it. plt.rcParams['figure.figsize'] = (10.0, 6.0) figure = corner.corner(data, labels=["xca", 'yca', 'xcb', 'ycb', "xcc","ycc",'dx','dy','ampa','ampb','ampc','ampratio','bkgd','sigmax','sigmay',\ 'sigmax2','sigmay2','theta','theta2','sepb','pab','sepc','pac'], show_titles=True, plot_contours=True, title_kwargs={"fontsize": 12}) figure.savefig(input_directory + args.image.split('/')[-1].split('.')[-3] + '_cornerplot', dpi=100) print 'Donezo.'
pl.xlabel("x") pl.ylabel("f(x)") pl.show() ''' niter = 500 pos, tempsigma = run(init_dist, nwalkers, niter, ndim) nptemp = np.array(datatemp).T plt.figure(5) figure = corner.corner(nptemp, bins=50, quantiles=[0.16, 0.5, 0.84], labels=[r"$f_b$", r"$f_0$", r"$R_c$"], label_kwargs={"fontsize": 15}, show_titles=True, title_kwargs={"fontsize": 15}, color='blue') figure.savefig("corner.png") ''' color=cm.rainbow(np.linspace(0,1,nwalkers)) for i,c in zip(range(nwalkers),color): model = pos[-1-i,0]+pos[-1-i,1]/(1+(x/pos[-1-i,2])**2) plt.figure(4) pl.plot(x,model,c=c) ''' plt.figure(4)
h = param[1] model_dist_mod = calc_dist_mod(data["z"], omega_m, h) mtaked = np.matrix(data["mu"] - model_dist_mod) chisq = np.tensordot(np.tensordot(mtaked, cov_inv, axes=1), mtaked) return -0.5*chisq fname = "jla_mub.txt" data = pd.read_table(fname, delimiter=" ", skiprows=1, names=open(fname).readline()[1:].split()) cov = np.matrix(np.loadtxt("jla_mub_covmatrix").reshape(31, 31)) cov_inv = np.linalg.inv(cov) sampler = mcmc_sampler(lnlike, 2) n_samples = 50000 prop_width = 0.01 start_params = np.array([0.3, 0.7]) sampler.run(n_samples, start_params, prop_width=prop_width) corner.corner(sampler.samples[int(n_samples/2):, :], labels=["$\\Omega_\\mathrm{M}$", "$h$"]) plt.savefig("corner_mcmc.pdf", bbox_inches="tight")
ax0.tick_params(axis='y', which='major', labelsize=15) ax0.xaxis.set_visible(False) ax1.tick_params(axis='both', which='major', labelsize=15) #ax0.set_title('lnL = %f'%lnL) ax1.errorbar(time_RVdays, simRV - data_RV, yerr=err_RV, fmt='o', color='green') ax1.plot([time_RVdays.iloc[0], time_RVdays.iloc[-1] + 1], [0, 0], 'k--', lw=2) ax1.set_ylabel('MAP Residuals (m/s)', fontsize=fontsize) ax1.set_xlabel('BJD - 2450000', fontsize=fontsize) if lnL > -290 and jitter2 < 20: plt.savefig("%s.pdf" % name) fig = corner.corner( sim_samples, labels=["x_s", "x_t", "y_s", "y_t", "phi", "jitter2"]) fig.savefig("%s_corner.png" % name) plt.close(fig) os.system("python orbits.py %s.txt" % name) dir = name.split('/') os.system("mv %s* %s/%s/good_ones/." % (name, dir[0], dir[1])) print "lnL=%f, file:%s" % (lnL, name) else: plt.savefig("%s.png" % name) plt.close() print i
import corner samples = out2.samples ndim=3 # This is the empirical mean of the sample: value0 = np.median(samples, axis=0) valueup = np.percentile(samples, 66, axis=0) valuedown = np.percentile(samples, 34, axis=0) print(value0, valueup, valuedown) # Make the base corner plot figure = corner.corner(samples, \ labels=[r"$\rm 12+log(O/H)$", r"$\rm q \ [cm^{-2}]$", \ r"$ \rm E(B-V) \ [mag]$"],\ show_titles=True, title_kwargs={"fontsize": 10},\ label_kwargs={"fontsize": 14},\ data_kwargs={"ms": 0.6}) # Extract the axes axes = np.array(figure.axes).reshape((ndim, ndim)) # Loop over the diagonal for i in range(ndim): ax = axes[i, i] ax.axvline(value0[i], color="r") ax.axvline(valueup[i], color="r", ls='--') ax.axvline(valuedown[i], color="r", ls='--')
def run(self, aposcale, psbin, lmin=None, lmax=None, shift=None, threshold=None, kwargs=dict()): """ ABS routine, 1. run "preprocess", estimate band-powers from given maps. 2. run "analyse", extract measurement-based CMB band-powers with ABS method. 3. run "postprocess", fit CMB band-powers/parameters with cosmic-variance. Returns ------- fitting result """ assert isinstance(aposcale, float) assert isinstance(psbin, int) assert (psbin > 0) assert (aposcale > 0) assert (self._background is not None) # preprocess self.preprocess(aposcale, psbin, lmin, lmax) # method selection self.analyse(shift, threshold) # post analysis result = self.postprocess(kwargs) # visualise data and result bestpar = None bestbp = None if self._solver == 'dynesty': from dynesty import plotting as dyplot fig, ax = dyplot.cornerplot(result, labels=self._paramlist, quantiles=[0.025, 0.5, 0.975], color='midnightblue', title_fmt='.3f', show_titles=1, smooth=0.04) plt.savefig(os.path.join(os.getcwd(), 'posterior.pdf')) bestpar = result.samples[np.where( result['logl'] == max(result['logl']))][0] elif self._solver == 'emcee': import corner fig = corner.corner(result, labels=self._paramlist) plt.savefig(os.path.join(os.getcwd(), 'posterior.pdf')) bestpar = np.median(result, axis=0) elif self._solver == 'minuit': print('params: {}\n bestfit: {}\n stderr: {}'.format( self._paramlist, rslt[0], rslt[1])) bestpar = result[0] else: raise ValueError('unsupported solver: {}'.format(self._solver)) for i in range(len(bestpar)): self._background_obj.reset({self._paramlist[i]: bestpar[i]}) bestbp = self._background_obj.bandpower() self.plot_result(bestbp) self.plot_residule(bestbp) return result
samples[:, :, :ndim] = samp[:, nburn:, :] for i in range(nwalkers): for j in range(nstep): samples[i, j, -1] = samp[i, nburn + j, :].sum() # plot chain pl.figure(figsize=(6, ndim + 1)) for i in range(ndim + 1): pl.subplot(ndim + 1, 1, i + 1) for j in range(nwalkers): pl.plot(samples[j, :, i], 'k-', alpha=0.3) pl.savefig('../plots/rollsRoyce_synth_chain.png') # corner plot samples = samples.reshape(-1, ndim + 1) corner.corner(samples, show_titles=True, title_args={"fontsize": 12}) pl.savefig('../plots/rollsRoyce_synth_triangle.png') # print 16, 50 and 84 percentile values vals = map(lambda v: (v[1], v[2]-v[1], v[1]-v[0]), \ zip(*np.percentile(samples, [16, 50, 84], axis=0))) p = np.zeros(ndim + 1) for i in range(ndim + 1): print 'delta v(%d-0) = %.3f + %.3f -%.3f' % \ (i+1, vals[i][0],vals[i][1],vals[i][2]) p[i] = vals[i][0] t1 = clock() print 'Time taken %d sec' % (t1 - t0) # final shift and plot:
plt.show() # computing Medians for true values medians = np.median(samples, axis=0) a_true, b_true, c_true = np.average(medians,axis=0) # computing the the value of the parameters by averaging over the 50 chains data = np.zeros([4000 * 50, 3]) for i in range(3): data[:, i] = np.hstack(samples[:, :, i]) # We can plot the posterior PDF using the corner library. fig = corner.corner(samples.reshape(40 * 5000, 3), labels=["a", "b", "c"], quantiles=[0.16, 0.5, 0.84], truths=[a_true, b_true, c_true], show_titles=True) plt.show() X = np.linspace(0, 300, 1000) for i in range(200): r = np.random.randint(0, high=4000 * 50) a = data[r, 0] b = data[r, 1] c = data[r, 2] plt.plot(X, a * X ** 2 + b * X + c) x = np.sort(x) plt.plot(X, a_true * X ** 2 + b_true * X + c_true,color = 'black',lw = 2,label = "True Model") plt.errorbar(x, y, yerr=yerr,fmt = 'o',ecolor = 'black',elinewidth = 0.75,lolims = True,uplims = True,label = "Data with error bars") plt.xlabel("x data")
print('\nMinimize Solution (constrained: sum(vec_f) == sum(vec_g)):') solution, V_f_est = sol_mini.fit(constrain_N=True) vec_f_est_mini = solution.x str_0 = 'unregularized:' str_1 = '' for f_i_est, f_i in zip(vec_f_est_mini, vec_f): str_1 += '{0:.2f}\t'.format(f_i_est / f_i) print('{}\t{}'.format(str_0, str_1)) print('\nMinimize Solution (MCMC as seed):') sol_mini.set_x0_and_bounds(x0=vec_f_est_mcmc) solution, V_f_est = sol_mini.fit(constrain_N=False) vec_f_est_mini = solution.x str_0 = 'unregularized:' str_1 = '' for f_i_est, f_i in zip(vec_f_est_mini, vec_f): str_1 += '{0:.2f}\t'.format(f_i_est / f_i) print('{}\t{}'.format(str_0, str_1)) corner.corner(samples, truths=vec_f) plt.savefig('corner_truth.png') print(np.sum(vec_f_est_mcmc)) plt.clf() corner.corner(samples, truths=vec_f_est_mini, truth_color='r') plt.savefig('corner_mini.png') plt.clf() corner.corner(samples, truths=vec_f_est_mcmc, truth_color='springgreen') plt.savefig('corner_mcmc.png')
ax1.semilogx(samples[:, :, 0]) # a values ax1.set_ylabel("a") ax2.semilogx(samples[:, :, 1]) # b values ax2.set_ylabel("b") ax3.semilogx(samples[:, :, 2]) # c values ax3.set_ylabel("c") ax3.set_xlabel("steps") pt.show() # plotting posterior PDFs using corner library a_true = medians[0][0] b_true = medians[0][1] c_true = medians[0][2] flat_samples = sampler.get_chain(flat=True) f2 = corner.corner(flat_samples, labels=labels, truths=[a_true, b_true, c_true]) f2.suptitle("Posterior PDFs") pt.show() # Plotting best fit and 200 models x0 = np.linspace(47, 287, 101) inds = np.random.randint(len(flat_samples), size=200) f3, ax = pt.subplots(nrows=1, ncols=1, tight_layout=True) for i in inds: sample = flat_samples[i] ax.plot(x0, sample[0] * x0 * x0 + sample[1] * x0 + sample[2], 'y-') ax.plot(x0, sample[0] * x0 * x0 + sample[1] * x0 + sample[2], 'y-', label="Model")
def show_corner(data): fig = corner.corner(data) plt.show()
fig.savefig(file) plt.close() else: plt.show() plt.close() chains = chains_to_dict(ftr.fitkeys, sampler) plot_chains(chains, file=ftr.model.PSR.value+"_chains.png") # Make the triangle plot. samples = sampler.chain[:, burnin:, :].reshape((-1, ndim)) try: import corner # Note, I had to turn off plot_contours because I kept getting # errors about how contour levels must be increasing. fig = corner.corner(samples, labels=ftr.fitkeys, bins=20, truths=ftr.maxpost_fitvals, plot_contours=False) fig.savefig(ftr.model.PSR.value+"_triangle.png") plt.close() except: log.warning("Corner plot failed") # Make a phaseogram with the 50th percentile values #ftr.set_params(dict(zip(ftr.fitkeys, np.percentile(samples, 50, axis=0)))) # Make a phaseogram with the best MCMC result ftr.set_params(dict(zip(ftr.fitkeys, ftr.maxpost_fitvals))) ftr.phaseogram(file=ftr.model.PSR.value+"_post.png") plt.close() # Write out the output pulse profile vs, xs = np.histogram(ftr.get_event_phases(), outprof_nbins, \
pymultinest.run(myloglike_inferred, myprior_inferred, n_params, importance_nested_sampling = False, resume = True, verbose = True, sampling_efficiency = 'parameter', n_live_points = n_live_points, outputfiles_basename='%s/2-'%plotDir, evidence_tolerance = evidence_tolerance, multimodal = False, max_iter = max_iter) multifile = "%s/2-post_equal_weights.dat"%plotDir data = np.loadtxt(multifile) tau, nu, delta, zeta, sigma, loglikelihood = data[:,0], data[:,1], data[:,2], data[:,3], data[:,4], data[:,5] idx = np.argmax(loglikelihood) tau_best, nu_best, delta_best, zeta_best, sigma_best = data[idx,0:-1] M = tau_best + nu_best*(mej) + delta_best*(vej) + zeta_best*Xlan plotName = "%s/corner.pdf"%(plotDir) figure = corner.corner(data[:,:-1], labels=labels, quantiles=[0.16, 0.5, 0.84], show_titles=True, title_kwargs={"fontsize": title_fontsize}, label_kwargs={"fontsize": label_fontsize}, title_fmt=".3f", smooth=3) figure.set_size_inches(18.0,18.0) plt.savefig(plotName) plt.close() if "bulla" in opts.analysis_type: phi_unique, theta_unique = np.unique(phi), np.unique(theta) phi_unique = phi_unique[::-1] fig = plt.figure(figsize=(16, 16)) gs = gridspec.GridSpec(len(phi_unique), len(theta_unique)) for ii in range(len(phi_unique)): for jj in range(len(theta_unique)):
ax.set_xlim(0, len(samples)) ax.set_ylabel(labels[i]) axes[-1].set_xlabel("step number") #fig.savefig('C:\\Users\\cvegvari\\Documents\\Python Scripts\\PDfit\\chains_100042BU-gep-0.03.jpeg', dpi=300, bbox_inches='tight') #plt.close(fig) # check autocorrelation tau = sampler.get_autocorr_time() print(tau) # process chain: discard burn-in, thin by half the autocorrelation time, flatten chain flat_samples = sampler.get_chain(discard=100, thin=25, flat=True) print(flat_samples.shape) fig = corner.corner(flat_samples) #fig.savefig('C:\\Users\\cvegvari\\Documents\\Python Scripts\\PDfit\\corner_100042BU-gep-0.03.jpeg', dpi=300, bbox_inches='tight') #plt.close(fig) # get median and 95 percentiles for results r_median = np.median(flat_samples[:,0]) r_95pc = np.percentile(flat_samples[:,0], [5, 95]) K_median = np.median(flat_samples[:,1]) K_95pc = np.percentile(flat_samples[:,1], [5, 95]) f = open('C:\\Users\\cvegvari\\Documents\\Python Scripts\\PDfit\\median_95pc.txt', 'w') print('r median (95 percentile):', r_median, ' (', r_95pc[0], ', ', r_95pc[1], ')', file=f) print('K median (95 percentile):', K_median, ' (', K_95pc[0], ', ', K_95pc[1], ')', file=f) f.close()
smples_c = np.delete(samples, (0), axis=0) smples_c = np.delete(smples_c, (1), axis=0) # print(smples_c) # print(np.mean(s[:,0]),np.mean(s[:,1]),np.mean(s[:,2])) om = np.mean(smples_c[:, 0]) ol = np.mean(smples_c[:, 1]) oh = np.mean(smples_c[:, 2]) print(om, ol, oh) print(chisq((om, ol, oh))) fig = corner.corner(smples_c, labels=["$\\Omega_m$", "$\\Omega_{\\Lambda}$", "$H_0$"]) fig.savefig("triangle.png") plt.show() m_hmc, l_hmc, H0_hmc = map(lambda v: (v[1], v[1] - v[0], v[2] - v[1]), zip(*np.percentile(samples, [16, 50, 84], axis=0))) print(m_hmc) print(l_hmc) print(H0_hmc) # plt.hist(smples_c[:,0],50) # plt.show() # plt.hist(smples_c[:,1],50) # plt.show()
def gp_decor(x,y, yerr=None, ind_in=None, ind_out=None, period=None, epoch=None, width=None, width_2=None, secondary_eclipse=False, systematics_amplitude=None, systematics_timescale=None, mean=1., nwalkers=50, thin_by=50, burn_steps=2500, total_steps=5000, bin_width=None, gp_code='celerite', kernel='Matern32', method='median_posterior', chunk_size=2000, Nsamples_detr=10, Nsamples_plot=10, xlabel='x', ylabel='y', ydetr_label='ydetr', outdir='gp_decor', fname=None, fname_summary=None, multiprocess=False, multiprocess_cores=None, figstretch=1, rasterized=True): ''' Required Input: --------------- x : array of float x-values of the data set y : array of float y-values of the data set Optional Input: --------------- yerr : array of float / float errorbars on y-values of the data set; if None, these are estimated as std(y); this is only needed to set an initial guess for the GP-fit; white noise is fitted as a jitter term period : float period of a potential transit signal if None, no transit region will be masked epoch : float epoch of a potential transit signal if None, no transit region will be masked width : float width of the transit/primary eclipse region that should be masked (should be greater than the signal's width) if None, no transit region will be masked width_2 : float width of the secondary region that should be masked (should be greater than the signal's width) if None, no transit region will be masked secondary_eclipse : bool mask a secondary eclipse (currently assumes a circular orbit) systematics_timescale : float (defaut None) the timescale of the systeamtics must be in the same units as x if None, set to 1. (assuming usually x is in days, 1. day is reasonable) mean : float (default 1.) mean of the data set the default is 1., assuming usually y will be normalized flux nwalkers : int number of MCMC walkers thin_by : int thinning the MCMC chain by how much burn_steps : int how many steps to burn in the MCMC total_steps : int total MCMC steps (including burn_steps) bin_width : float (default None) run the GP on binned data and then evaluate on unbinned data (significant speed up for george) currently a bit buggy gp_code : str (default 'celerite') 'celerite' or 'george' which GP code to use method : str (default 'median_posterior') how to calculate the GP curve that's used for detrending 'mean_curve' : take Nsamples_detr and calculate many curves, detrend by the mean of all of them 'median_posterior' : take the median of the posterior and predict a single curve chunk_size : int (default 5000) calculate gp.predict in chunks of the entire light curve (to not crash memory) Nsamples_detr : float (default 10) only used if method=='mean_curve' how many samples used for detrending Nsampels_plot : float (default 10) only used if method=='mean_curve' how many samples used for plotting xlabel : str x axis label (for plots) ylabel : str y axis label (for plots) ydetr_label : str y_detr axis label (for plots) outdir : str name of the output directory fname : str prefix of the output files (e.g. a planet name) multiprocess : bool (default True) run MCMC on many cores ''' if (gp_code=='celerite') & ('celerite' not in sys.modules): raise ValueError('You are trying to use "celerite", but it is not installed.') elif (gp_code=='george') & ('george' not in sys.modules): raise ValueError('You are trying to use "george", but it is not installed.') #::: make it luser proof and recalculate the true first epoch if not any(v is None for v in [period, epoch, width]): epoch = get_first_epoch(x, epoch, period) #TODO: philosophical question: #use median of the posterior to get 1 "median" GP for detrending? #or use mean (and std) curves of many samples from the GP for detrending? #it definitely is way faster to simply use the "median"... #::: this is ugly, I know; #::: blame the multiprocessing and pickling issues, #::: which demand global variables for efficiency global xx global yy global yyerr global err_norm global GP_CODE global MEAN GP_CODE = gp_code MEAN = mean #::: outdir if not os.path.exists(outdir): os.makedirs(outdir) #::: print function that prints into console and logfile at the same time now = datetime.now().isoformat() def logprint(*text): print(*text) original = sys.stdout with open( os.path.join(outdir,fname+'logfile_'+now+'.log'), 'a' ) as f: sys.stdout = f print(*text) sys.stdout = original #::: fname if fname is not None: fname += '_gp_decor_' else: fname = 'gp_decor_' #::: MCMC plot settings if kernel=='Matern32': keys = ['gp_log_sigma', 'gp_log_rho', 'log_y_err'] names = [r'gp: $\log{\sigma}$', r'gp: $\log{\rho}$', r'$\log{(y_\mathrm{err})}$'] elif kernel=='SHOT': keys = ['gp_log_S0', 'gp_log_Q', 'log_omega0', 'log_y_err'] names = [r'gp: $\log{S_0}$', r'gp: $\log{Q}$', r'gp: $\log{\omega_0}$', r'$\log{(y_\mathrm{err})}$'] celerite.terms.SHOTerm discard = int(1.*burn_steps/thin_by) #::: phase-plot settings dt=1./1000. ferr_type='meansig' ferr_style='sem' sigmaclip=True logprint('\nStarting...') #::: guess yerr if not given if yerr is None: yerr = np.nanstd(y) * np.ones_like(y) #::: mask transit if required #::: if ind_in and ind_out are given, use these #::: otherwise, check if period, epoch and width are given if (ind_in is None) and (ind_out is None): if any(v is None for v in [period, epoch, width]): ind_in = [] ind_out = slice(None) #mark all data points as out of transit (i.e. no transit masked) else: if secondary_eclipse is True: ind_ecl1, ind_ecl2, ind_out = index_eclipses(x, epoch, period, width, width_2) ind_in = list(ind_ecl1)+list(ind_ecl2) else: ind_in, ind_out = index_transits(x, epoch, period, width) xx = x[ind_out] yy = y[ind_out] yyerr = yerr[ind_out] #::: binning if bin_width is not None: bintime_out, bindata_out, bindata_err_out, _ = rebin_err(xx, yy, ferr=yyerr, dt=bin_width, ferr_type='meansig', sigmaclip=True, ferr_style='sem' ) xx = bintime_out yy = bindata_out yyerr = bindata_err_out #::: save settings if not os.path.exists(outdir): os.makedirs(outdir) header = 'period,epoch,width,secondary_eclipse,'+\ 'nwalkers,thin_by,burn_steps,total_steps' X = np.column_stack(( period, epoch, width, secondary_eclipse, nwalkers, thin_by, burn_steps, total_steps )) np.savetxt( os.path.join(outdir,fname+'settings.csv'), X, header=header, delimiter=',', fmt='%s') #::: plot the data fig, ax = plt.subplots(figsize=(6*figstretch,4)) ax.errorbar(x[ind_out], y[ind_out], yerr=yerr[ind_out], fmt=".b", capsize=0, rasterized=rasterized) ax.errorbar(x[ind_in], y[ind_in], yerr=yerr[ind_in], fmt=".", color='skyblue', capsize=0, rasterized=rasterized) ax.set( xlabel=xlabel, ylabel=ylabel, title='Original data' ) fig.savefig( os.path.join(outdir,fname+'data.pdf'), bbox_inches='tight') plt.close(fig) if bin_width is not None: fig, ax = plt.subplots(figsize=(6*figstretch,4)) ax.errorbar(xx, yy, yerr=yyerr, fmt=".b", capsize=0, rasterized=rasterized) ax.set( xlabel=xlabel, ylabel=ylabel, title='Original data (binned)' ) fig.savefig( os.path.join(outdir,fname+'data_binned.pdf'), bbox_inches='tight') plt.close(fig) # err # #::: set up the GP model ## kernel = terms.RealTerm(log_a=1., log_c=1.) + terms.JitterTerm(log_sigma=np.log(yerr)) # kernel = terms.Matern32Term(log_sigma=1., log_rho=1.) + terms.JitterTerm(log_sigma=np.log(yerr)) # gp = celerite.GP(kernel, mean=mean) #log_white_noise=np.log(yerr), # gp.compute(xx, yerr=yerr) ## logprint("Initial log-likelihood: {0}".format(gp.log_likelihood(y))) #::: plot grid t = np.linspace(np.min(x), np.max(x), 2000) # ########################################################################### # #::: MLE fit # ########################################################################### # logprint 'Running MLE fit...' # # #::: define a cost function # def neg_log_like(params, yy, gp): # gp.set_parameter_vector(params) # return -gp.log_likelihood(yy) # # def grad_neg_log_like(params, yy, gp): # gp.set_parameter_vector(params) # return -gp.grad_log_likelihood(yy)[1] # # # #::: run the MLE fit # initial_params = gp.get_parameter_vector() ## logprint initial_params # bounds = gp.get_parameter_bounds() # soln = minimize(neg_log_like, initial_params, jac=grad_neg_log_like, # method="L-BFGS-B", bounds=bounds, args=(yy, gp)) # gp.set_parameter_vector(soln.x) ## logprint("Final log-likelihood: {0}".format(-soln.fun)) ## logprint soln.x # # # #::: evaluate MLE curve # mu, var = gp.predict(yy, t, return_var=True) # std = np.sqrt(var) # # # #::: plot the data and MLE fit # color = 'r' #"#ff7f0e" # fig, ax = plt.subplots() # ax.errorbar(xx, yy, yerr=np.exp(soln.x[2]), fmt="b.", capsize=0) # ax.errorbar(x[ind_in], y[ind_in], yerr=np.exp(soln.x[2]), fmt=".", color='skyblue', capsize=0) # ax.plot(t, mu, color='r', zorder=11) # ax.fill_between(t, mu+std, mu-std, color='r', alpha=0.3, edgecolor="none", zorder=10) # ax.set( xlabel=xlabel, ylabel=ylabel, title="MLE prediction"); # fig.savefig( os.path.join(outdir,fname+'MLE_fit.jpg'), dpi=100, bbox_inches='tight') # # #::: delete that gp instance # del gp ########################################################################### #::: MCMC fit ########################################################################### if multiprocess and not multiprocess_cores: multiprocess_cores = cpu_count()-1 logprint('\nRunning MCMC fit...') if multiprocess: logprint('\tRunning on', multiprocess_cores, 'CPUs.') #::: initial guesses #::: log(sigma) if systematics_amplitude is not None: log_sigma_init = np.log(systematics_amplitude) else: log_sigma_init = np.log(np.nanstd(yy)) #::: log(rho) if systematics_timescale is not None: log_rho_init = np.log(systematics_timescale) else: log_rho_init = np.log(1.) #::: log(yerr) err_norm = np.nanmean(yyerr) err_scale = np.nanmean(yyerr) log_err_scale_init = np.log(err_scale) #::: all initial = np.array([log_sigma_init,log_rho_init,log_err_scale_init]) #::: set up MCMC ndim = len(initial) backend = emcee.backends.HDFBackend(os.path.join(outdir,fname+'mcmc_save.h5')) # Set up a new backend backend.reset(nwalkers, ndim) #::: run MCMC def run_mcmc(sampler): p0 = initial + 1e-8 * np.random.randn(nwalkers, ndim) sampler.run_mcmc(p0, total_steps/thin_by, thin_by=thin_by, progress=True); if multiprocess: with closing(Pool(processes=(multiprocess_cores))) as pool: sampler = emcee.EnsembleSampler(nwalkers, ndim, log_probability, pool=pool, backend=backend) run_mcmc(sampler) else: sampler = emcee.EnsembleSampler(nwalkers, ndim, log_probability, backend=backend) run_mcmc(sampler) logprint('\nAcceptance fractions:') logprint(sampler.acceptance_fraction) tau = sampler.get_autocorr_time(discard=discard, c=5, tol=10, quiet=True)*thin_by logprint('\nAutocorrelation times:') logprint('\t', '{0: <30}'.format('parameter'), '{0: <20}'.format('tau (in steps)'), '{0: <20}'.format('Chain length (in multiples of tau)')) for i, name in enumerate(names): logprint('\t', '{0: <30}'.format(name), '{0: <20}'.format(tau[i]), '{0: <20}'.format((total_steps-burn_steps) / tau[i])) def gp_predict_in_chunks(ybuf, xbuf, quiet=False): #::: predict in chunks of 1000 data points to not crash memory mu = [] var = [] for i in tqdm(range( int(1.*len(xbuf)/chunk_size)+1 ), disable=quiet): m, v = gp.predict(ybuf, xbuf[i*chunk_size:(i+1)*chunk_size], return_var=True) mu += list(m) var += list(v) return np.array(mu), np.array(var) def get_params_from_samples(samples, keys): ''' read MCMC results and update params ''' theta_median = np.percentile(samples, 50, axis=0) theta_ll = np.percentile(samples, 16, axis=0) theta_ul = np.percentile(samples, 84, axis=0) params_median = { n:t for n,t in zip(keys,theta_median) } params_ll = { n:t for n,t in zip(keys,theta_ll) } params_ul = { n:t for n,t in zip(keys,theta_ul) } params_lower_err = {} params_upper_err = {} for key in params_median: params_lower_err[key] = abs(params_median[key]-params_ll[key]) params_upper_err[key] = abs(params_ul[key]-params_median[key]) return params_median, params_lower_err, params_upper_err #::: get the samples, samples = sampler.get_chain(flat=True, discard=discard) #::: get the resulting params dictionaries params_median, params_lower_err, params_upper_err = get_params_from_samples(samples, keys) #::: Save the resulting parameters in a table with open( os.path.join(outdir,fname+'table.csv'), 'w' ) as f: f.write('name,median,lower_err,upper_err\n') for i, key in enumerate(keys): f.write(names[i] + ',' + str(params_median[key]) + ',' + str(params_lower_err[key]) + ',' + str(params_upper_err[key]) + '\n' ) #::: if requested, append a row into the summary file, too if fname_summary is not None: with open( fname_summary, 'a' ) as f: f.write(fname[0:-1] + ',') for i, key in enumerate(keys): f.write(str(params_median[key]) + ',' + str(params_lower_err[key]) + ',' + str(params_upper_err[key])) if i<len(keys)-1: f.write(',') else: f.write('\n') #::: the posterior-median yerr, #::: and calculate the mean GP curve / posterior-median GP curve err_scale = np.exp(np.median(samples[:,2])) yyerr = yyerr/err_norm*err_scale yerr = yerr/err_norm*err_scale #TODO: check this... scale the yerr the same way the OOE / binned data was rescaled # logprint '\nPlot 1' if method=='mean_curve': mu_all_samples = [] std_all_samples = [] for s in tqdm(samples[np.random.randint(len(samples), size=Nsamples_plot)]): gp = call_gp(s) # mu, var = gp.predict(yy, t, return_var=True) mu, var = gp_predict_in_chunks(yy, t, quiet=True) std = np.sqrt(var) mu_all_samples.append( mu ) std_all_samples.append( std ) mu_GP_curve = np.mean(mu_all_samples, axis=0) std_GP_curve = np.mean(std_all_samples, axis=0) elif method=='median_posterior': log_sigma = np.median( samples[:,0] ) log_rho = np.median( samples[:,1] ) log_yerr = np.median( samples[:,2] ) params = [log_sigma, log_rho, log_yerr] gp = call_gp(params) # mu, var = gp.predict(yy, t, return_var=True) mu, var = gp_predict_in_chunks(yy, t) mu_GP_curve = mu std_GP_curve = np.sqrt(var) #::: Plot the data and individual posterior samples # fig, ax = plt.subplots() # ax.errorbar(x, y, yerr=yerr, fmt=".b", capsize=0) # ax.errorbar(x[ind_in], y[ind_in], yerr=yerr, fmt=".", color='skyblue', capsize=0) # for mu, std in zip(mu_all_samples, std_all_samples): # ax.plot(t, mu, color='r', alpha=0.1, zorder=11) # ax.set( xlabel=xlabel, ylabel=ylabel, title="MCMC posterior samples", ylim=[1-0.002, 1.002] ) # fig.savefig( os.path.join(outdir,fname+'MCMC_fit_samples.jpg'), dpi=100, bbox_inches='tight') #::: plot the data and "mean"+"std" GP curve fig, ax = plt.subplots(figsize=(6*figstretch,4)) ax.errorbar(x[ind_out], y[ind_out], yerr=yerr[ind_out], fmt=".b", capsize=0, rasterized=rasterized) ax.errorbar(x[ind_in], y[ind_in], yerr=yerr[ind_in], fmt=".", color='skyblue', capsize=0, rasterized=rasterized) ax.plot(t, mu_GP_curve, color='r', zorder=11) ax.fill_between(t, mu_GP_curve+std_GP_curve, mu_GP_curve-std_GP_curve, color='r', alpha=0.3, edgecolor="none", zorder=10) ax.set( xlabel=xlabel, ylabel=ylabel, title="MCMC posterior predictions" ) fig.savefig( os.path.join(outdir,fname+'mcmc_fit.pdf'), bbox_inches='tight') plt.close(fig) if bin_width is not None: fig, ax = plt.subplots(figsize=(6*figstretch,4)) ax.errorbar(xx, yy, yerr=yyerr, fmt=".b", capsize=0, rasterized=rasterized) ax.plot(t, mu_GP_curve, color='r', zorder=11) ax.fill_between(t, mu_GP_curve+std_GP_curve, mu_GP_curve-std_GP_curve, color='r', alpha=0.3, edgecolor="none", zorder=10) ax.set( xlabel=xlabel, ylabel=ylabel, title="MCMC posterior predictions (binned)" ) fig.savefig( os.path.join(outdir,fname+'mcmc_fit_binned.pdf'), bbox_inches='tight') plt.close(fig) if not any(v is None for v in [period, epoch, width]): Norbits = int((x[-1]-x[0])/period)+1 fig, axes = plt.subplots(1, Norbits, figsize=(4*Norbits,3.8), sharey=True) try: for i in range(Norbits): ax = axes[i] x1 = ( epoch-width+i*period ) x2 = ( epoch+width+i*period ) ind = np.where( (x>x1) & (x<x2) )[0] ax.errorbar(x[ind_out], y[ind_out], yerr=yerr[ind_out], fmt=".b", capsize=0, rasterized=rasterized) ax.errorbar(x[ind_in], y[ind_in], yerr=yerr[ind_in], fmt=".", color='skyblue', capsize=0, rasterized=rasterized) ax.plot(t, mu_GP_curve, color='r', zorder=11) ax.fill_between(t, mu_GP_curve+std_GP_curve, mu_GP_curve-std_GP_curve, color='r', alpha=0.3, edgecolor="none", zorder=10) ax.set( xlim=[x1,x2], xlabel=xlabel, ylabel=ylabel, title="MCMC posterior predictions" ) except: pass fig.savefig( os.path.join(outdir,fname+'mcmc_fit_individual.pdf'), bbox_inches='tight') plt.close(fig) #::: plot chains; format of chain = (nwalkers, nsteps, nparameters) # logprint('Plot chains') fig, axes = plt.subplots(ndim+1, 1, figsize=(6,4*(ndim+1)) ) steps = np.arange(0,total_steps,thin_by) #::: plot the lnprob_values (nwalkers, nsteps) for j in range(nwalkers): axes[0].plot(steps, sampler.get_log_prob()[:,j], '-') axes[0].set( ylabel='lnprob', xlabel='steps' ) #:::plot all chains of parameters for i in range(ndim): ax = axes[i+1] ax.set( ylabel=names[i], xlabel='steps') for j in range(nwalkers): ax.plot(steps, sampler.chain[j,:,i], '-') ax.axvline( burn_steps, color='k', linestyle='--' ) plt.tight_layout() fig.savefig( os.path.join(outdir,fname+'mcmc_chains.pdf'), bbox_inches='tight') plt.close(fig) #::: plot corner fig = corner.corner(samples, labels=names, show_titles=True, title_kwargs={"fontsize": 12}); fig.savefig( os.path.join(outdir,fname+'mcmc_corner.pdf'), bbox_inches='tight') plt.close(fig) #::: Calculate the detrended data logprint('\nRetrieve samples for detrending...') sys.stdout.flush() if method=='mean_curve': mu_all_samples = [] std_all_samples = [] for s in tqdm(samples[np.random.randint(len(samples), size=Nsamples_detr)]): gp = call_gp(s) # mu, var = gp.predict(yy, x, return_var=True) mu, var = gp_predict_in_chunks(yy, x) std = np.sqrt(var) mu_all_samples.append( mu ) std_all_samples.append( std ) mu_GP_curve = np.mean(mu_all_samples, axis=0) std_GP_curve = np.mean(std_all_samples, axis=0) elif method=='median_posterior': log_sigma = np.median( samples[:,0] ) log_rho = np.median( samples[:,1] ) log_yerr = np.median( samples[:,2] ) params = [log_sigma, log_rho, log_yerr] gp = call_gp(params) # mu, var = gp.predict(yy, x, return_var=True) mu, var = gp_predict_in_chunks(yy, x) mu_GP_curve = mu std_GP_curve = np.sqrt(var) logprint('\nCreating output...') #TODO: philosophical question: #does one want to include the std of the GP into the error bars of the detrended y? #this would mean that masked in-transit regions have way bigger error bars than the out-of-transit points #might not be desired... #also, it leads to weirdly large random errorbars at some points... ydetr = y - mu_GP_curve + MEAN ydetr_err = yerr # ydetr_err = ydetr * np.sqrt( (yerr/y)**2 + (std_GP_curve/mu_GP_curve)**2 ) #np.std(buf, axis=0) #::: Save the detrended data as .txt # logprint 'Output results.csv' header = xlabel+','+ydetr_label+','+ydetr_label+'_err' X = np.column_stack(( x, ydetr, ydetr_err )) np.savetxt( os.path.join(outdir,fname+'mcmc_ydetr.csv'), X, header=header, delimiter=',') #::: Save the GP curve as .txt # logprint 'Output results_gp.csv' header = xlabel+',gp_mu,gp_std' X = np.column_stack(( x, mu_GP_curve, std_GP_curve )) np.savetxt( os.path.join(outdir,fname+'mcmc_gp.csv'), X, header=header, delimiter=',') logprint('\nDone. All output files are in '+outdir) #::: plotting helper # def sigma_clip(a, low=3., high=3., iters=5): # for i in range(iters): # ind = np.where( (np.nanmean(a)-np.nanstd(a)*low < a) & (a < np.nanmean(a)+np.nanstd(a)*high ) )[0] # a = a[ind] # return a # # def get_ylim(y,yerr): # y1 = sigma_clip( y-yerr ) # y2 = sigma_clip( y+yerr ) # yrange = np.nanmax(y2) - np.nanmin(y1) # return [ np.nanmin(y1)-0.1*yrange, np.nanmax(y2)+0.1*yrange ] #::: Plot the detrended data # logprint 'Plot 1' fig, ax = plt.subplots(figsize=(6*figstretch,4)) ax.errorbar(x, ydetr, yerr=ydetr_err, fmt='b.', capsize=0, rasterized=rasterized) ax.errorbar(x[ind_in], ydetr[ind_in], yerr=ydetr_err[ind_in], fmt='.', color='skyblue', capsize=0, rasterized=rasterized) ax.set( xlabel=xlabel, ylabel=ylabel, title="Detrended data" ) fig.savefig( os.path.join(outdir,fname+'mcmc_ydetr.pdf'), bbox_inches='tight') plt.close(fig) #::: Plot the detrended data phase-folded if not any(v is None for v in [period, epoch, width]): phase_x, phase_ydetr, phase_ydetr_err, _, phi = phase_fold(x, ydetr, period, epoch, dt = dt, ferr_type=ferr_type, ferr_style=ferr_style, sigmaclip=sigmaclip) # logprint 'Plot 2' fig, ax = plt.subplots(figsize=(6*figstretch,4)) ax.plot(phi, ydetr, marker='.', linestyle='none', color='lightgrey', rasterized=rasterized) ax.errorbar(phase_x, phase_ydetr, yerr=phase_ydetr_err, fmt='b.', capsize=0, zorder=10, rasterized=rasterized) ax.set( xlabel='Phase', ylabel=ylabel, title="Detrended data, phase folded" ) ax.get_yaxis().get_major_formatter().set_useOffset(False) fig.savefig( os.path.join(outdir,fname+'mcmc_ydetr_phase_folded.pdf'), bbox_inches='tight') plt.close(fig) # logprint 'Plot 3' dtime = phase_x*period*24. #from days to hours fig, ax = plt.subplots(figsize=(6*figstretch,4)) ax.plot(phi*period*24., ydetr, marker='.', linestyle='none', color='lightgrey') ax.errorbar(dtime, phase_ydetr, yerr=phase_ydetr_err, fmt='b.', capsize=0, zorder=10, rasterized=rasterized) ax.set( xlim=[-width*24.,width*24.], xlabel=r'$T - T_0 \ (h)$', ylabel=ylabel, title="Detrended data, phase folded, zooom" ) ax.get_yaxis().get_major_formatter().set_useOffset(False) fig.savefig( os.path.join(outdir,fname+'mcmc_ydetr_phase_folded_zoom.pdf'), bbox_inches='tight') plt.close(fig) #::: Plot the detrended data phase-folded per transit fig, ax = plt.subplots(figsize=(6*figstretch,4)) Norbits = int((x[-1]-x[0])/period)+1 for i in range(Norbits): cmap = get_cmap('inferno') color = cmap(1.*i/Norbits) x1 = ( epoch-width+i*period ) x2 = ( epoch+width+i*period ) ind = np.where( (x>x1) & (x<x2) )[0] phase_x, phase_ydetr, phase_ydetr_err, _, phi = phase_fold(x[ind], ydetr[ind], period, epoch, dt = dt, ferr_type=ferr_type, ferr_style=ferr_style, sigmaclip=sigmaclip) dtime = phase_x*period*24. #from days to hours ax.errorbar(dtime, phase_ydetr, yerr=phase_ydetr_err, color=color, marker='.', linestyle='none', capsize=0, zorder=10, rasterized=rasterized) ax.set( xlim=[-width*24.,width*24.], xlabel=r'$T - T_0 \ (h)$', ylabel=ylabel, title="Detrended data, phase folded, zoom, individual" ) ax.get_yaxis().get_major_formatter().set_useOffset(False) fig.savefig( os.path.join(outdir,fname+'mcmc_ydetr_phase_folded_zoom_individual.pdf'), bbox_inches='tight')#, dpi=100, bbox_inches='tight') plt.close(fig)
ax2.plot(n,tstyMAP*1e+6,'mo') ax2.errorbar(n,tstymed*1e+6,tstystd*1e+6,fmt='none',ecolor = 'k') ax2.set_ylabel('UWD NS [$\mu$rad]',fontsize= 12) ax2.set_xlabel('Collapse number',fontsize= 12) ax2.set_xticks(ticks) plt.tight_layout() fig.align_ylabels() plt.savefig('MAPcollapses.pdf') stackmed = np.median(ppc['stack_obs'],axis = 0) stackstd = np.std(ppc['stack_obs'],axis = 0) tstack = tstack/(3600) plt.figure() plt.plot(tstack,stack+180,'b') plt.plot(tstack,stackMAP+180,'m') plt.fill_between(tstack,stackmed-stackstd+180,stackmed+stackstd+180,alpha = 0.5) ax = plt.gca() ax.set_xlabel('Time [Hours]',fontsize= 12) ax.set_ylabel('UWD NS [$\mu rad$]',fontsize = 12) ax.set_xlim([0,24]) plt.tight_layout() plt.savefig('MAPstack.pdf') plt.figure() corner.corner( panda_trace[['xsh_mod','ysh_mod','dsh_mod','pspd_mod','condd_mod','conds_mod','ks_mod','Vs_mod','Vd_mod']],color = 'black', truths =[results['MAP']['xsh_mod'],results['MAP']['ysh_mod'],results['MAP']['dsh_mod'],results['MAP']['pspd_mod']/1e+6,results['MAP']['condd_mod'],results['MAP']['conds_mod'],np.log10(results['MAP']['ks_mod']),np.log10(results['MAP']['Vs_mod']),np.log10(results['MAP']['Vd_mod'])])
def plot_MCMC_corner(sampler): samples = sampler.get_chain( flat=True, discard=int(1. * config.BASEMENT.settings['mcmc_burn_steps'] / config.BASEMENT.settings['mcmc_thin_by'])) params_median, params_ll, params_ul = get_params_from_samples(samples) params_median2, params_ll2, params_ul2 = params_median.copy( ), params_ll.copy(), params_ul.copy() #::: make pretty titles for the corner plot labels, units = [], [] for i, l in enumerate(config.BASEMENT.fitlabels): labels.append(str(config.BASEMENT.fitlabels[i])) units.append(str(config.BASEMENT.fitunits[i])) for companion in config.BASEMENT.settings['companions_all']: if companion + '_epoch' in config.BASEMENT.fitkeys: ind = np.where(config.BASEMENT.fitkeys == companion + '_epoch')[0][0] samples[:, ind] -= int( params_median[companion + '_epoch'] ) #np.round(params_median[companion+'_epoch'],decimals=0) units[ind] = str( units[ind] + '-' + str(int(params_median[companion + '_epoch'])) + 'd' ) #np.format_float_positional(params_median[companion+'_epoch'],0)+'d') config.BASEMENT.fittruths[ind] -= int(params_median[companion + '_epoch']) params_median2[companion + '_epoch'] -= int( params_median[companion + '_epoch']) for i, l in enumerate(labels): if units[i] != '': labels[i] = str(labels[i] + ' (' + units[i] + ')') #::: corner plot fig = corner( samples, labels=labels, range=[0.999] * config.BASEMENT.ndim, quantiles=[0.15865, 0.5, 0.84135], show_titles=False, title_kwargs={"fontsize": 14}, # label_kwargs={"fontsize": 20}, max_n_ticks=3, truths=config.BASEMENT.fittruths) caxes = np.reshape(np.array(fig.axes), (config.BASEMENT.ndim, config.BASEMENT.ndim)) #::: set allesfitter titles for i, key in enumerate(config.BASEMENT.fitkeys): value = round_tex(params_median2[key], params_ll2[key], params_ul2[key]) ctitle = r'' + labels[i] + '\n' + r'$=' + value + '$' if len(config.BASEMENT.fitkeys) > 1: caxes[i, i].set_title(ctitle) for i in range(caxes.shape[0]): for j in range(caxes.shape[1]): caxes[i, j].xaxis.set_label_coords(0.5, -0.5) caxes[i, j].yaxis.set_label_coords(-0.5, 0.5) else: caxes.set_title(ctitle) caxes.xaxis.set_label_coords(0.5, -0.5) caxes.yaxis.set_label_coords(-0.5, 0.5) return fig
def lightcurve_corner(lc, model, sampler_flatchain, model_kwargs=None, num_models_to_plot=100, lcaxis_posn=(0.7, 0.55, 0.2, 0.4), filter_spacing=0.5, tmin=None, tmax=None, t0_offset=None, save_plot_as=''): """ Plot the posterior distributions in a corner (pair) plot, with an inset showing the observed and model light curves. Parameters ---------- lc : lightcurve_fitting.lightcurve.LC Table of broadband photometry including columns "MJD", "mag", "dmag", "filt" model : lightcurve_fitting.models.Model The model that was fit to the light curve. sampler_flatchain : array-like 2D array containing the aggregated MCMC chain histories model_kwargs : dict, optional Keyword arguments to be passed to the model num_models_to_plot : int, optional Number of model realizations to plot in the light curve inset. Default: 100 lcaxis_posn : tuple, optional Light curve inset position and size specification in figure units: (left, bottom, width, height) filter_spacing : float, optional Spacing between filters in the light curve inset, in units determined by the order of magnitude of the luminosities. Default: 0.5 tmin, tmax : float, optional Starting and ending times for which to plot the models in the light curve inset. Default: determined by the time range of the observed light curve. t0_offset : float, optional Reference time on the horizontal axis of the light curve inset. Default: determined by the starting time of the model light curve. save_plot_as : str, optional Filename to which to save the resulting plot Returns ------- fig : matplotlib.pyplot.Figure Figure object containing the plot """ if model_kwargs is None: model_kwargs = {} plt.style.use(resource_filename('lightcurve_fitting', 'serif.mplstyle')) choices = np.random.choice(sampler_flatchain.shape[0], num_models_to_plot) ps = sampler_flatchain[choices].T sampler_flatchain_corner = sampler_flatchain.copy() if 't_0' in model.input_names: i_t0 = model.input_names.index('t_0') if t0_offset is None: t0_offset = np.floor(sampler_flatchain_corner[:, i_t0].min()) if t0_offset != 0.: sampler_flatchain_corner[:, i_t0] -= t0_offset model.axis_labels[i_t0] = '$t_0 - {:.0f}$ (d)'.format(t0_offset) fig = corner.corner(sampler_flatchain_corner, labels=model.axis_labels) corner_axes = np.array(fig.get_axes()).reshape(model.nparams, model.nparams) for ax in np.diag(corner_axes): ax.spines['top'].set_visible(False) ax.spines['left'].set_visible(False) ax.spines['right'].set_visible(False) ax.xaxis.set_ticks_position('bottom') ax.yaxis.set_ticks_position('none') ax = fig.add_axes(lcaxis_posn) if tmin is None: tmin = np.min(lc['MJD']) if tmax is None: tmax = np.max(lc['MJD']) xfit = np.arange(tmin, tmax, 0.1) ufilts = np.unique(lc['filter']) y_fit = model(xfit, ufilts, *ps, **model_kwargs) mjd_offset = np.floor(tmin) if y_fit.max() > 0.: yscale = 10.**np.round(np.log10(y_fit.max())) else: yscale = 1. offset = -len(ufilts) // 2 * filter_spacing for filt, yfit in zip(ufilts, y_fit): offset += filter_spacing lc_filt = lc.where(filter=filt) ax.errorbar(lc_filt['MJD'] - mjd_offset, lc_filt['lum'] / yscale + offset, lc_filt['dlum'] / yscale, ls='none', marker='o', **filt.plotstyle) ax.plot(xfit - mjd_offset, yfit / yscale + offset, color=filt.linecolor, alpha=0.05) txt = '${}{:+.1f}$'.format(filt.name, offset) if offset else filt.name ax.text(1.03, yfit[-1, 0] / yscale + offset, txt, color=filt.textcolor, ha='left', va='center', transform=ax.get_yaxis_transform()) ax.set_xlabel('MJD $-$ {:.0f}'.format(mjd_offset)) ax.set_ylabel( 'Luminosity $L_\\nu$ (10$^{{{:.0f}}}$ erg s$^{{-1}}$ Hz$^{{-1}}$) + Offset' .format(np.log10(yscale) + 7)) # W --> erg / s paramtexts = format_credible_interval(sampler_flatchain, varnames=model.input_names, units=model.units) fig.text(0.45, 0.95, '\n'.join(paramtexts), va='top', ha='center', fontdict={'size': 'large'}) if save_plot_as: fig.savefig(save_plot_as) print('saving figure as ' + save_plot_as) return fig
# system using samples from the posterior distribution mask = approxChain[:,2] >= approxChain[:,3] print("approxposterior P(tsat >= age | data) = %0.3lf" % np.mean(mask)) # Estimate probability that tsat < 1 Gyr mask = approxChain[:,2] <= 1 print("approxposterior P(tsat <= 1Gyr | data) = %0.3lf" % np.mean(mask)) # Plot both true and approxposterior posterior distribution bins = 20 range = [[8.7, 9.06], [-3.4, -2.2], [0, 12], [0, 12], [-2, -0.2]] # approxposterior fig = corner.corner(approxChain, quantiles=[], labels=labels, bins=bins, show_titles=False, title_kwargs={"fontsize": 16}, title_fmt='.2f', verbose=False, hist_kwargs={"linewidth" : 2.5}, plot_contours=True, plot_datapoints=False, plot_density=False, color="royalblue", range=range, no_fill_contours=True) # True fig = corner.corner(trueChain, quantiles=[], labels=labels, show_titles=False, bins=bins, verbose=False, plot_density=True, fig=fig, hist_kwargs={"linewidth" : 2.5}, plot_contours=True, plot_datapoints=False, color="k", range=range, no_fill_contours=True) fig = corner.corner(approxChain, quantiles=[], labels=labels, fig=fig, bins=bins, show_titles=False, title_kwargs={"fontsize": 16}, title_fmt='.2f', verbose=False, hist_kwargs={"linewidth" : 2.5}, plot_contours=False, plot_datapoints=False, plot_density=False, color="royalblue", range=range, no_fill_contours=True)
def make_plot_misscentred_monopole_pcc(file_name,folder): profile = fits.open(folder+file_name) h = profile[1].header p = profile[1].data file_mcmc = 'monopole_pcconly_'+file_name[:-4]+'out' Mhalo = 10**h['lMASS_HALO_mean'] Nmean = h['N_GAL_mean'] Nlens = h['N_LENSES'] Rmean = h['RADIUS_HALO_mean'] ROUT = (2.5*(2.*(Mhalo/2.21e14)**0.75)**(1./3.))/0.7 soff = 0.4*Rmean mcmc = (np.loadtxt(folder+file_mcmc)).T labels = ['M200','pcc'] mout = np.percentile(mcmc[0][1000:], [16, 50, 84]) pcc_out = np.percentile(mcmc[1][1000:], [16, 50, 84]) fig = corner.corner(mcmc.T, labels=labels) plt.savefig(folder+'plots_monopole_misscentred_pcconly/corner'+file_mcmc[:-3]+'png') f, ax = plt.subplots(2, 1, figsize=(6,3)) ax[0].plot(mcmc[0],'k.',alpha=0.3) ax[0].axvline(1000) ax[0].axhline(mout[1]) ax[0].axhline(mout[1] - (mout[1]-mout[0]),ls='--') ax[0].axhline(mout[1] + (mout[2]-mout[1]),ls='--') ax[1].plot(mcmc[1],'k.',alpha=0.3) ax[1].axvline(1000) ax[1].axhline(pcc_out[1]) ax[1].axhline(pcc_out[1] - (pcc_out[1]-pcc_out[0]),ls='--') ax[1].axhline(pcc_out[1] + (pcc_out[2]-pcc_out[1]),ls='--') f.subplots_adjust(hspace=0,wspace=0) plt.savefig(folder+'plots_monopole_misscentred_pcconly/iter'+file_mcmc[:-3]+'png') if h['elM200_NFW'] < 0.: eM200 = 10**h['elM200_NFW'] else: eM200 = h['elM200_NFW'] # e_M200 = (M200*eM200)/np.log(10.) zmean = h['Z_MEAN'] print '####################' print file_name print 'lM200 ',h['lM200_NFW'] try: print h['N_min'],h['N_max']-1 except: print h['N_GAL_mean'] print '####################' M200 = 10**(mout[1]) e_M200 = (10**(mout[1])*np.log(10.)*np.diff(mout)) r = np.logspace(np.log10(0.05),np.log10(5.5),20) multipoles = multipole_shear_parallel(r,M200=10**mout[1], misscentred = True,s_off = soff, ellip=0,z=zmean,components = ['t'], verbose=False,ncores=2) Gt = model_Gamma(multipoles,'t',misscentred=True,pcc=pcc_out[1]) Gtcen = pcc_out[1]*multipoles['Gt0'] Gtmiss = (1-pcc_out[1])*multipoles['Gt_off'] f, ax = plt.subplots(2, 1, figsize=(5,8), sharex=True) f.subplots_adjust(hspace=0,wspace=0) matplotlib.rcParams.update({'font.size': 12}) SN = np.mean(p.DSigma_T/p.error_DSigma_T) ax[0].plot(r,Gt,'C1--') ax[0].plot(r,Gtcen,'C3') ax[0].plot(r,Gtmiss,'C3--') ax[0].scatter(p.Rp,p.DSigma_T,facecolor='none',edgecolors='0.4') ax[0].errorbar(p.Rp,p.DSigma_T,yerr=p.error_DSigma_T,fmt = 'none',ecolor='0.4') ax[0].set_xscale('log') ax[0].set_yscale('log') ax[0].set_xlabel('R [Mpc]') ax[0].set_ylim(1,200) ax[0].set_xlim(0.1,5) ax[0].axvline(ROUT,ls='--',c='C7') ax[0].xaxis.set_ticks([0.1,1,5]) ax[0].set_xticklabels([0.1,1,5]) ax[0].yaxis.set_ticks([0.1,10,100]) ax[0].set_yticklabels([0.1,10,100]) ax[0].set_ylabel(r'$\Delta \Sigma_T [h_{70}M_\odot\,\rm{pc}^{-2}]$') ax[1].plot([0,5],[0,0],'k--') ax[1].scatter(p.Rp,p.DSigma_X,facecolor='none',edgecolors='0.4') ax[1].errorbar(p.Rp,p.DSigma_X,yerr=p.error_DSigma_X,fmt = 'none',ecolor='0.4') ax[1].set_xscale('log') ax[1].set_xlabel('R [mpc]') ax[1].set_ylim(-50,50) ax[1].set_xlim(0.1,5) ax[1].xaxis.set_ticks([0.1,1,5]) ax[1].set_xticklabels([0.1,1,5]) ax[1].yaxis.set_ticks([-25,0,25]) ax[1].set_yticklabels([-25,0,25]) ax[1].set_ylabel(r'$\Delta \Sigma_\times [h_{70}M_\odot\,\rm{pc}^{-2}]$') matplotlib.rcParams.update({'font.size': 12}) plt.savefig(folder+'plots_monopole_misscentred_pcconly/'+file_name[:-5]+'.png') return Mhalo/1.e14, M200/1.e14, e_M200/1.e14, Nmean, Nlens, SN, pcc_out[1], np.diff(pcc_out)
def plot_tics(Y,n_tics,project_name): import corner plt.figure() Y_ = np.vstack(Y)[:,:n_tics] labels = ['tIC{0}'.format(i+1) for i in range(Y_.shape[1])] corner.corner(Y_,labels=labels,bins=50) #plt.title('{0}:\nProjection onto top-{1} tICs'.format(project_name,len(labels))) plt.savefig('{0}_tica_projection.jpg'.format(project_name),dpi=300) plt.close()