def plot_model_res(ax, t_epoch, t_model, rv_model, sim_rv, n_rvset, labels, colors, markers): t_rv = sim_rv[:, 0] - t_epoch rv_obs = sim_rv[:, 1] - sim_rv[:, 5] rv_sim = sim_rv[:, 3] res_rv = rv_obs - rv_sim xlim1, xlim2 = anc.compute_limits(t_rv, 0.05) miny = np.min(res_rv - sim_rv[:, 2]) maxy = np.max(res_rv + sim_rv[:, 2]) ylim1, ylim2 = anc.compute_limits(np.array([miny, maxy]), 0.05) ax.axhline(0., color='gray', ls='-') for iset in range(0, n_rvset): sel = sim_rv[:, 7].astype(int) == iset + 1 ax.errorbar(t_rv[sel], res_rv[sel], yerr=sim_rv[sel, 2], fmt=markers[iset], mfc=colors[iset], mec='black', ecolor=colors[iset], capsize=0, label=labels[iset]) ax.set_xlabel('BJD-%.3f' % (t_epoch)) ax.set_xlim([xlim1, xlim2]) ax.set_ylim([ylim1, ylim2]) ax.set_ylabel('res [m/s]') plt.draw() return ax
def plot_model_data(ax, t_epoch, t_model, rv_model, sim_rv, n_rvset, labels, colors, markers): t_rv = sim_rv[:,0] - t_epoch rv_obs = sim_rv[:,1] - sim_rv[:,5] rv_sim = sim_rv[:,3] xlim1, xlim2 = anc.compute_limits(t_rv, 0.05) miny = min(np.min(rv_obs-sim_rv[:,2]), np.min(rv_sim), np.min(rv_model)) maxy = max(np.max(rv_obs+sim_rv[:,2]), np.min(rv_sim), np.min(rv_model)) ylim1, ylim2 = anc.compute_limits(np.array([miny, maxy]), 0.05) ax.axhline(0., color='gray', ls='-') ax.plot(t_model-t_epoch, rv_model, marker='None', ls=':', lw=0.5, color='darkgray', label='model $N$-body') for iset in range(0, n_rvset): sel = sim_rv[:,7].astype(int) == iset+1 ax.errorbar(t_rv[sel], rv_obs[sel], yerr=sim_rv[sel,2], fmt=markers[iset], mfc=colors[iset], mec='black', ecolor=colors[iset], capsize=0, label=labels[iset]) ax.plot(t_rv[sel], rv_sim[sel], marker=markers[iset], mfc='None', mec=colors[iset], mew=1., ms=8, ls='', label='%s (TRADES)' %(labels[iset])) ax.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3,\ ncol=2*(n_rvset)+1, mode="expand", \ borderaxespad=0., numpoints=1,\ fontsize=9) ax.set_xlim([xlim1, xlim2]) ax.set_xticklabels([]) ax.set_ylim([ylim1, ylim2]) ax.set_ylabel('RV [m/s]') plt.draw() return ax
def plot_model_data(ax, t_epoch, t_model, rv_model, sim_rv, n_rvset, labels, colors, markers): t_rv = sim_rv[:, 0] - t_epoch rv_obs = sim_rv[:, 1] - sim_rv[:, 5] rv_sim = sim_rv[:, 3] xlim1, xlim2 = anc.compute_limits(t_rv, 0.05) miny = min(np.min(rv_obs - sim_rv[:, 2]), np.min(rv_sim), np.min(rv_model)) maxy = max(np.max(rv_obs + sim_rv[:, 2]), np.min(rv_sim), np.min(rv_model)) ylim1, ylim2 = anc.compute_limits(np.array([miny, maxy]), 0.05) ax.axhline(0., color='gray', ls='-') ax.plot(t_model - t_epoch, rv_model, marker='None', ls=':', lw=0.5, color='darkgray', label='model $N$-body') for iset in range(0, n_rvset): sel = sim_rv[:, 7].astype(int) == iset + 1 ax.errorbar(t_rv[sel], rv_obs[sel], yerr=sim_rv[sel, 2], fmt=markers[iset], mfc=colors[iset], mec='black', ecolor=colors[iset], capsize=0, label=labels[iset]) ax.plot(t_rv[sel], rv_sim[sel], marker=markers[iset], mfc='None', mec=colors[iset], mew=1., ms=8, ls='', label='%s (TRADES)' % (labels[iset])) ax.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3,\ ncol=2*(n_rvset)+1, mode="expand", \ borderaxespad=0., numpoints=1,\ fontsize=9) ax.set_xlim([xlim1, xlim2]) ax.set_xticklabels([]) ax.set_ylim([ylim1, ylim2]) ax.set_ylabel('RV [m/s]') plt.draw() return ax
def plot_phase_res(ax, phi_rv, rv_opl, rv_spl, res_rv, sim_rv, rv_kep, n_rvset, labels, colors, markers, pl_id): res_okep = rv_opl - rv_kep res_skep = rv_spl - rv_kep #xlim1, xlim2 = anc.compute_limits(phi_rv, 0.05) xlim1, xlim2 = 0., 1. #miny = np.min(res_rv-sim_rv[:,2]) #maxy = np.max(res_rv+sim_rv[:,2]) miny = min(np.min(res_okep-sim_rv[:,2]), np.min(res_skep-sim_rv[:,2])) maxy = max(np.max(res_okep+sim_rv[:,2]), np.max(res_skep+sim_rv[:,2])) ylim1, ylim2 = anc.compute_limits(np.array([miny, maxy]), 0.05) ax.axhline(0., color='gray', ls='-') for iset in range(0, n_rvset): sel = sim_rv[:,7].astype(int) == iset+1 ax.errorbar(phi_rv[sel], res_okep[sel], yerr=sim_rv[sel,2], fmt=markers[iset], mfc=colors[iset], mec='black', ecolor=colors[iset], capsize=0, label=labels[iset]) ax.errorbar(phi_rv[sel], res_skep[sel], yerr=sim_rv[sel,2], fmt=markers[iset], ms= 8, mfc='None', mec=colors[iset], mew=1., ecolor=colors[iset], capsize=0, label=labels[iset]) #ax.errorbar(phi_rv[sel], res_rv[sel], yerr=sim_rv[sel,2], fmt=markers[iset], mfc=colors[iset], mec='black', ecolor=colors[iset], capsize=0, label=labels[iset]) ax.set_xlabel('$\phi$ [phase planet %s]' %(pl_id)) ax.set_xlim([xlim1, xlim2]) ax.set_ylim([ylim1, ylim2]) ax.set_ylabel('res [m/s]') plt.draw() return ax
def plot_model_res(ax, t_epoch, t_model, rv_model, sim_rv, n_rvset, labels, colors, markers): t_rv = sim_rv[:,0] - t_epoch rv_obs = sim_rv[:,1] - sim_rv[:,5] rv_sim = sim_rv[:,3] res_rv = rv_obs - rv_sim xlim1, xlim2 = anc.compute_limits(t_rv, 0.05) miny = np.min(res_rv-sim_rv[:,2]) maxy = np.max(res_rv+sim_rv[:,2]) ylim1, ylim2 = anc.compute_limits(np.array([miny, maxy]), 0.05) ax.axhline(0., color='gray', ls='-') for iset in range(0, n_rvset): sel = sim_rv[:,7].astype(int) == iset+1 ax.errorbar(t_rv[sel], res_rv[sel], yerr=sim_rv[sel,2], fmt=markers[iset], mfc=colors[iset], mec='black', ecolor=colors[iset], capsize=0, label=labels[iset]) ax.set_xlabel('BJD-%.3f' %(t_epoch)) ax.set_xlim([xlim1, xlim2]) ax.set_ylim([ylim1, ylim2]) ax.set_ylabel('res [m/s]') plt.draw() return ax
def plot_phase_res(ax, phi_rv, rv_opl, rv_spl, res_rv, sim_rv, rv_kep, n_rvset, labels, colors, markers, pl_id): res_okep = rv_opl - rv_kep res_skep = rv_spl - rv_kep #xlim1, xlim2 = anc.compute_limits(phi_rv, 0.05) xlim1, xlim2 = 0., 1. #miny = np.min(res_rv-sim_rv[:,2]) #maxy = np.max(res_rv+sim_rv[:,2]) miny = min(np.min(res_okep - sim_rv[:, 2]), np.min(res_skep - sim_rv[:, 2])) maxy = max(np.max(res_okep + sim_rv[:, 2]), np.max(res_skep + sim_rv[:, 2])) ylim1, ylim2 = anc.compute_limits(np.array([miny, maxy]), 0.05) ax.axhline(0., color='gray', ls='-') for iset in range(0, n_rvset): sel = sim_rv[:, 7].astype(int) == iset + 1 ax.errorbar(phi_rv[sel], res_okep[sel], yerr=sim_rv[sel, 2], fmt=markers[iset], mfc=colors[iset], mec='black', ecolor=colors[iset], capsize=0, label=labels[iset]) ax.errorbar(phi_rv[sel], res_skep[sel], yerr=sim_rv[sel, 2], fmt=markers[iset], ms=8, mfc='None', mec=colors[iset], mew=1., ecolor=colors[iset], capsize=0, label=labels[iset]) #ax.errorbar(phi_rv[sel], res_rv[sel], yerr=sim_rv[sel,2], fmt=markers[iset], mfc=colors[iset], mec='black', ecolor=colors[iset], capsize=0, label=labels[iset]) ax.set_xlabel('$\phi$ [phase planet %s]' % (pl_id)) ax.set_xlim([xlim1, xlim2]) ax.set_ylim([ylim1, ylim2]) ax.set_ylabel('res [m/s]') plt.draw() return ax
def plot_phase_rv(ax, phi_model, rv_model, phi_rv, rv_opl, rv_spl, sim_rv, n_rvset, labels, colors, markers): #xlim1, xlim2 = anc.compute_limits(phi_rv, 0.05) xlim1, xlim2 = 0., 1. miny = np.min(rv_opl - sim_rv[:, 2]) maxy = np.max(rv_opl + sim_rv[:, 2]) ylim1, ylim2 = anc.compute_limits(np.array([miny, maxy]), 0.05) ax.axhline(0., color='gray', ls='-') ax.plot(phi_model, rv_model, marker='None', ls='--', lw=0.5, color='darkgray', label='model Keplerian') for iset in range(0, n_rvset): sel = sim_rv[:, 7].astype(int) == iset + 1 ax.errorbar(phi_rv[sel], rv_opl[sel], yerr=sim_rv[sel, 2], fmt=markers[iset], mfc=colors[iset], mec='black', ecolor=colors[iset], capsize=0) ax.plot(phi_rv[sel], rv_spl[sel], marker=markers[iset], mfc='None', mec=colors[iset], mew=1., ms=8, ls='') # skip simulated ax.legend(loc=0, fontsize=9, numpoints=1) ax.set_xlim([xlim1, xlim2]) ax.set_xticklabels([]) ax.set_ylim([ylim1, ylim2]) ax.set_ylabel('RV [m/s]') plt.draw() return ax
def plot_phase_rv(ax, phi_model, rv_model, phi_rv, rv_opl, rv_spl, sim_rv, n_rvset, labels, colors, markers): #xlim1, xlim2 = anc.compute_limits(phi_rv, 0.05) xlim1, xlim2 = 0., 1. miny = np.min(rv_opl-sim_rv[:,2]) maxy = np.max(rv_opl+sim_rv[:,2]) ylim1, ylim2 = anc.compute_limits(np.array([miny, maxy]), 0.05) ax.axhline(0., color='gray', ls='-') ax.plot(phi_model, rv_model, marker='None', ls='--', lw=0.5, color='darkgray', label='model Keplerian') for iset in range(0, n_rvset): sel = sim_rv[:,7].astype(int) == iset+1 ax.errorbar(phi_rv[sel], rv_opl[sel], yerr=sim_rv[sel,2], fmt=markers[iset], mfc=colors[iset], mec='black', ecolor=colors[iset], capsize=0) ax.plot(phi_rv[sel], rv_spl[sel], marker=markers[iset], mfc='None', mec=colors[iset], mew=1., ms=8, ls='') # skip simulated ax.legend(loc=0, fontsize=9, numpoints=1) ax.set_xlim([xlim1, xlim2]) ax.set_xticklabels([]) ax.set_ylim([ylim1, ylim2]) ax.set_ylabel('RV [m/s]') plt.draw() return ax
def main(): cli = anc.get_args() # read derived posterior file derived_file = os.path.join(cli.full_path, 'derived_posterior.hdf5') h5f = h5py.File(derived_file, 'r') derived_names = np.array(h5f['derived_names'], dtype='S10') derived_posterior_in = np.array(h5f['derived_posterior'], dtype=np.float64) h5f.close() n_der = derived_names.shape[0] n_flatchain = derived_posterior_in.shape[0] derived_posterior = anc.derived_posterior_check(derived_names, derived_posterior_in) label_separation = -0.90 # if uses this, comment ax.xyaxis.labelpad = label_pad label_pad = 12 # it uses this, comment ax.xyaxis.set_label_coords()... label_size = 8 ticklabel_size = 4 if (n_der > 2): #label_separation = -0.1 - ( 0.075 * (n_der-2) ) label_separation = -0.15 - (0.125 * (n_der - 2)) #else: #label_separation = -0.15 #label_size = label_size - 1 * int(n_der / 10.) #label_size = label_size - 1 * int(n_der / 5.) label_size = label_size - 1 * int(n_der / 2.5) labels_list = anc.derived_labels(derived_names, cli.m_type) k = anc.get_bins(derived_posterior, rule='doane') if (cli.overplot is not None): ## OPEN summary_parameters.hdf5 FILE s_h5f = h5py.File( os.path.join(cli.full_path, 'summary_parameters.hdf5'), 'r') # take only the selected sample s_overplot = '%04d' % (cli.overplot) #overp_der = s_h5f['parameters/%s/derived/parameters' %(s_overplot)][...] read_der = s_h5f['parameters/%s/derived/parameters' % (s_overplot)][...] s_h5f.close() overp_der = anc.derived_parameters_check(derived_names, read_der, derived_posterior) #fig = plt.figure(figsize=(12,12)) fig = plt.figure(figsize=(6, 6)) fig.subplots_adjust(hspace=0.05, wspace=0.05) for ix in range(0, n_der): x_data = derived_posterior[:, ix] x_min, x_max = anc.compute_limits(x_data, 0.05) if (x_min == x_max): x_min = x_min - 1. x_max = x_max + 1. for iy in range(0, n_der): y_data = derived_posterior[:, iy] y_min, y_max = anc.compute_limits(y_data, 0.05) if (y_min == y_max): y_min = y_min - 1. y_max = y_max + 1. if (iy > ix): # correlation plot anc.print_both('correlation %s vs %s' % (derived_names[ix], derived_names[iy])) ax = plt.subplot2grid((n_der + 1, n_der), (iy, ix)) hist2d_counts, xedges, yedges, image2d = ax.hist2d(\ x_data, y_data, bins=k, range=[[x_data.min(), x_data.max()],[y_data.min(), y_data.max()]], cmap=cm.gray_r, #normed=True normed=False ) #new_k = int(k/3) new_k = k hist2d_counts_2, xedges_2, yedges_2 = np.histogram2d(\ x_data, y_data, bins=new_k, range=[[x_data.min(), x_data.max()],[y_data.min(), y_data.max()]], #normed=True density=False ) x_bins = [ 0.5 * (xedges_2[i] + xedges_2[i + 1]) for i in range(0, new_k) ] y_bins = [ 0.5 * (yedges_2[i] + yedges_2[i + 1]) for i in range(0, new_k) ] nl = 5 levels = [1. - np.exp(-0.5 * ii) for ii in range(0, nl) ] # 2D sigmas: 0sigma, 1sigma, 2sigma, 3sigma, .. ax.contour( x_bins, y_bins, hist2d_counts_2.T, nl, cmap=cm.viridis, linestyles='solid', linewidths=0.5, #normed=True ) if (cli.overplot is not None): # plot selected overplot sample # check angle and plot %360 and %-360... if ('w' in derived_names[ix] or 'lN' in derived_names[ix] or 'mA' in derived_names[ix]): ax.axvline(overp_der[ix] % 360., color='C0', ls='--', lw=1.1, alpha=0.7) ax.axvline(overp_der[ix] % -360., color='C0', ls='--', lw=1.1, alpha=0.7) else: ax.axvline(overp_der[ix], color='C0', ls='--', lw=1.1, alpha=0.7) if ('w' in derived_names[iy] or 'lN' in derived_names[iy] or 'mA' in derived_names[iy]): ax.axhline(overp_der[iy] % 360., color='C0', ls='--', lw=1.1, alpha=0.7) ax.axhline(overp_der[iy] % -360., color='C0', ls='--', lw=1.1, alpha=0.7) else: ax.axhline(overp_der[iy], color='C0', ls='--', lw=1.1, alpha=0.7) ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False) if (iy == n_der - 1): set_xaxis(ax, label_size, label_separation, label_pad, ticklabel_size, labels_list[ix], [xedges[0], xedges[-1], 4]) if (ix == 0): set_yaxis(ax, label_size, label_separation, label_pad, ticklabel_size, labels_list[iy], [yedges[0], yedges[-1], 5]) ax.set_ylim([y_min, y_max]) ax.set_xlim([x_min, x_max]) plt.draw() elif (iy == ix): # distribution plot anc.print_both('%s histogram' % (derived_names[ix])) ax = plt.subplot2grid((n_der + 1, n_der), (ix, ix)) if (ix == n_der - 1): hist_orientation = 'horizontal' else: hist_orientation = 'vertical' idx = np.argsort(x_data) if (not cli.cumulative): # HISTOGRAM hist_counts, edges, patces = ax.hist( x_data, bins=k, range=[x_data.min(), x_data.max()], histtype='stepfilled', color='darkgrey', #edgecolor='lightgray', edgecolor='None', align='mid', orientation=hist_orientation, #normed=True, density=True, stacked=True) else: # CUMULATIVE HISTOGRAM hist_counts, edges, patces = ax.hist( x_data, bins=k, range=[x_data.min(), x_data.max()], histtype='stepfilled', color='darkgrey', #edgecolor='lightgray', edgecolor='None', align='mid', orientation=hist_orientation, density=True, stacked=True, cumulative=True) #print parameter_names_emcee[ix], overp_der[ix] if (ix == n_der - 1): if (cli.overplot is not None): # check angle and plot %360 and %-360... if ('w' in derived_names[ix] or 'lN' in derived_names[ix] or 'mA' in derived_names[ix]): ax.axhline(overp_der[ix] % 360., color='C0', ls='--', lw=1.1, alpha=0.7) ax.axhline(overp_der[ix] % -360., color='C0', ls='--', lw=1.1, alpha=0.7) else: # plot selected overplot sample ax.axhline(overp_der[ix], color='C0', ls='--', lw=1.1, alpha=0.7) ax.set_ylim([y_min, y_max]) else: if (cli.overplot is not None): if ('w' in derived_names[ix] or 'lN' in derived_names[ix] or 'mA' in derived_names[ix]): ax.axvline(overp_der[ix] % 360., color='C0', ls='--', lw=1.1, alpha=0.7) ax.axvline(overp_der[ix] % -360., color='C0', ls='--', lw=1.1, alpha=0.7) else: # plot selected overplot sample ax.axvline(overp_der[ix], color='C0', ls='--', lw=1.1, alpha=0.7) ax.set_xlim([x_min, x_max]) if (cli.overplot is not None): print derived_names[ix], ' overplot val = ', overp_der[ ix], ' min = ', x_data.min(), ' max = ', x_data.max() ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False) ax.set_title(labels_list[ix], fontsize=label_size) plt.draw() plot_folder = os.path.join(cli.full_path, 'plots') if (not os.path.isdir(plot_folder)): os.makedirs(plot_folder) correlation_file = os.path.join(plot_folder, 'derived_triangle.png') fig.savefig(correlation_file, bbox_inches='tight', dpi=300) anc.print_both('png done') correlation_file = os.path.join(plot_folder, 'derived_triangle.pdf') fig.savefig(correlation_file, bbox_inches='tight', dpi=96) anc.print_both('pdf done') plt.close(fig) return
def main(): # --- # initialize logger logger = logging.getLogger("Main_log") logger.setLevel(logging.DEBUG) formatter = logging.Formatter("%(asctime)s - %(message)s") # global variables label_separation=-0.90 # if uses this, comment ax.xyaxis.labelpad = label_pad label_pad = 12 # it uses this, comment ax.xyaxis.set_label_coords()... label_size = 8 ticklabel_size = 4 def set_xaxis(ax, label_size, label_separation, label_pad, ticklabel_size, kel_label, ticks_formatter, tick_fmt='%.4f'): ax.get_xaxis().set_visible(True) ax.xaxis.set_tick_params(labelsize=ticklabel_size) ax.xaxis.set_label_coords(0.5, label_separation) #ax.ticklabel_format(style='plain', axis='both', useOffset=False) plt.setp(ax.xaxis.get_majorticklabels(), rotation=70 ) #ax.xaxis.labelpad = label_pad ax.set_xlabel(kel_label, fontsize=label_size) tick_step = (ticks_formatter[1] - ticks_formatter[0]) / ticks_formatter[2] ax.xaxis.set_ticks(np.arange(ticks_formatter[0], ticks_formatter[1], tick_step)) tick_formatter = FormatStrFormatter(tick_fmt) ax.xaxis.set_major_formatter(tick_formatter) return def set_yaxis(ax, label_size, label_separation, label_pad, ticklabel_size, kel_label, ticks_formatter, tick_fmt='%.4f'): ax.get_yaxis().set_visible(True) ax.yaxis.set_tick_params(labelsize=ticklabel_size) ax.yaxis.set_label_coords(label_separation,0.5) #ax.ticklabel_format(style='plain', axis='both', useOffset=False) #ax.yaxis.labelpad = label_pad ax.set_ylabel(kel_label, fontsize=label_size) tick_step = (ticks_formatter[1] - ticks_formatter[0]) / ticks_formatter[2] ax.yaxis.set_ticks(np.arange(ticks_formatter[0], ticks_formatter[1], tick_step)) tick_formatter = FormatStrFormatter(tick_fmt) ax.yaxis.set_major_formatter(tick_formatter) return print print ' ================== ' print ' CORRELATION PLOTS' print ' ================== ' print # read cli arguments cli = anc.get_args() #plot_folder = prepare_plot_folder(working_path) emcee_plots = anc.prepare_emcee_plot_folder(cli.full_path) log_file = os.path.join(emcee_plots, 'emcee_triangle_log.txt') flog = logging.FileHandler(log_file, 'w') flog.setLevel(logging.DEBUG) flog.setFormatter(formatter) logger.addHandler(flog) # log screen slog = logging.StreamHandler() slog.setLevel(logging.DEBUG) slog.setFormatter(formatter) logger.addHandler(slog) # computes mass conversion factor #m_factor = anc.mass_conversion_factor(cli.m_type) m_factor, m_unit = anc.mass_type_factor(1., cli.m_type, False) # set emcee and trades folder emcee_folder = cli.full_path trades_folder = os.path.join(os.path.dirname(cli.full_path), '') # and best folder emcee_file, emcee_best, folder_best = anc.get_emcee_file_and_best(emcee_folder, cli.temp_status) # get data from the hdf5 file parameter_names_emcee, parameter_boundaries, chains, acceptance_fraction, autocor_time, lnprobability, ln_err_const, completed_steps = anc.get_data(emcee_file, cli.temp_status) # print Memory occupation of ... anc.print_memory_usage(chains) nfit, nwalkers, nruns, nburnin, nruns_sel = anc.get_emcee_parameters(chains, cli.temp_status, cli.nburnin, completed_steps) logger.info('nfit(%d), nwalkers(%d), nruns(%d), nburnin(%d), nruns_sel(%d)' %(nfit, nwalkers, nruns, nburnin, nruns_sel)) # test label_separation #if (nfit <= 3): label_separation = -0.1 if(nfit > 2): #label_separation = -0.1 - ( 0.075 * (nfit-2) ) # good for figsize=(12,12) label_separation = -0.15 - ( 0.125 * (nfit-2) ) # testing #else: #label_separation = -0.15 #label_size = label_size - 1 * int(nfit / 5.) label_size = label_size - 1 * int(nfit / 2.5) # set label and legend names kel_plot_labels = anc.keplerian_legend(parameter_names_emcee, cli.m_type) chains_T_full, parameter_boundaries = anc.select_transpose_convert_chains(nfit, nwalkers, nburnin, nruns, nruns_sel, m_factor, parameter_names_emcee, parameter_boundaries, chains) chains_T, flatchain_posterior_0, lnprob_burnin, thin_steps = anc.thin_the_chains(cli.use_thin, nburnin, nruns, nruns_sel, autocor_time, chains_T_full, lnprobability, burnin_done=False) flatchain_posterior_0 = anc.fix_lambda(flatchain_posterior_0, parameter_names_emcee ) if(cli.boot_id > 0): flatchain_posterior_msun = anc.posterior_back_to_msun(m_factor,parameter_names_emcee,flatchain_posterior_0) boot_file = anc.save_bootstrap_like(emcee_folder, cli.boot_id, parameter_names_emcee, flatchain_posterior_msun) logger.info('saved bootstrap like file: %s' %(boot_file)) del flatchain_posterior_msun k = anc.get_auto_bins(flatchain_posterior_0) if(cli.overplot is not None): if(cli.adhoc is not None): overp_names, read_par = anc.read_fitted_file(cli.adhoc) cli.overplot = 777 else: ## OPEN summary_parameters.hdf5 FILE s_h5f = h5py.File(os.path.join(cli.full_path, 'summary_parameters.hdf5'), 'r') # take only the selected sample s_overplot = '%04d' %(cli.overplot) read_par = s_h5f['parameters/%s/fitted/parameters' %(s_overplot)][...] s_h5f.close() # fitted parameters has always Mp/Ms in Msun/Mstar, so it is needed to rescale it properly overp_par = read_par.copy() for ii in range(0, nfit): if('Ms' in parameter_names_emcee[ii]): #if('Ms' in overp_names[ii]): overp_par[ii] = overp_par[ii]*m_factor #fig, ax = plt.subplots(nrows = nfit-1, ncols=nfit, figsize=(12,12)) #fig = plt.figure(figsize=(12,12)) fig = plt.figure(figsize=(6,6)) fig.subplots_adjust(hspace=0.05, wspace=0.05) for ix in range(0, nfit, 1): x_data = flatchain_posterior_0[:, ix] ##x_med = median_parameters[ix] x_min, x_max = anc.compute_limits(x_data, 0.05) if(x_min == x_max): x_min = parameter_boundaries[ix,0] x_max = parameter_boundaries[ix,1] #x_max_mean = mode_parameters[ix] for iy in range(nfit-1, -1, -1): y_data = flatchain_posterior_0[:, iy] y_min, y_max = anc.compute_limits(y_data, 0.05) if(y_min == y_max): y_min = parameter_boundaries[iy,0] y_max = parameter_boundaries[iy,1] #y_max_mean = mode_parameters[iy] if(iy > ix): # correlation plot logger.info('%s vs %s' %(parameter_names_emcee[ix], parameter_names_emcee[iy])) ax = plt.subplot2grid((nfit+1, nfit), (iy,ix)) #hist2d_counts, xedges, yedges, image2d = ax.hist2d(x_data, y_data, bins=k, range=[[x_data.min(), x_data.max()],[y_data.min(), y_data.max()]], cmap=cm.gray_r, normed=True) hist2d_counts, xedges, yedges, image2d = ax.hist2d(\ x_data, y_data, bins=k, range=[[x_data.min(), x_data.max()],[y_data.min(), y_data.max()]], cmap=cm.gray_r, normed=False #density=False ) #new_k = int(k/3) new_k = k hist2d_counts_2, xedges_2, yedges_2 = np.histogram2d(\ x_data, y_data, bins=new_k, range=[[x_data.min(), x_data.max()],[y_data.min(), y_data.max()]], #normed=True density=False ) x_bins = [0.5*(xedges_2[i]+xedges_2[i+1]) for i in range(0, new_k)] y_bins = [0.5*(yedges_2[i]+yedges_2[i+1]) for i in range(0, new_k)] #ax.contour(x_bins, y_bins, hist2d_counts_2.T, 3, cmap=cm.gray, linestyle='solid', linewidths=(0.7, 0.7, 0.7)) nl = 5 levels = [1.-np.exp(-0.5*ii) for ii in range(0,nl)] # 2D sigmas: 0sigma, 1sigma, 2sigma, 3sigma, .. #ax.contour(x_bins, y_bins, hist2d_counts_2.T, levels, cmap=cm.gray, linestyle='solid', linewidths=(0.7, 0.7, 0.7)) #ax.contour(x_bins, y_bins, hist2d_counts_2.T, levels, cmap=cm.viridis, linestyle='solid', linewidths=1.) #ax.contour(x_bins, y_bins, hist2d_counts_2.T, cmap=cm.viridis, linestyle='solid', linewidths=0.7) #ax.contour(x_bins, y_bins, hist2d_counts_2.T, levels, cmap=cm.viridis, linestyle='solid', linewidths=0.7, normed=True) ax.contour(x_bins, y_bins, hist2d_counts_2.T, nl, cmap=cm.viridis, linestyles='solid', linewidths=0.5, #normed=True ) if(cli.overplot is not None): # plot selected overplot sample ax.axvline(overp_par[ix], color='C0', ls='--', lw=1.1, alpha=0.5) ax.axhline(overp_par[iy], color='C0', ls='--', lw=1.1, alpha=0.5) ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False) if(iy == nfit-1): set_xaxis(ax, label_size, label_separation, label_pad, ticklabel_size, kel_plot_labels[ix], [xedges[0], xedges[-1], 4]) if(ix == 0): set_yaxis(ax, label_size, label_separation, label_pad, ticklabel_size, kel_plot_labels[iy], [yedges[0], yedges[-1], 5]) ax.set_ylim([y_min, y_max]) ax.set_xlim([x_min, x_max]) plt.draw() elif(iy == ix): # distribution plot logger.info('%s histogram' %(parameter_names_emcee[ix])) ax = plt.subplot2grid((nfit+1, nfit), (ix,ix)) if (ix == nfit-1): hist_orientation='horizontal' else: hist_orientation='vertical' idx = np.argsort(x_data) if(not cli.cumulative): # HISTOGRAM hist_counts, edges, patces = ax.hist(x_data, bins=k, range=[x_data.min(), x_data.max()], histtype='stepfilled', color='darkgrey', #edgecolor='lightgray', edgecolor='None', align='mid', orientation=hist_orientation, #normed=True, density=True, stacked=True ) else: # CUMULATIVE HISTOGRAM hist_counts, edges, patces = ax.hist(x_data, bins=k, range=[x_data.min(), x_data.max()], histtype='stepfilled', color='darkgrey', #edgecolor='lightgray', edgecolor='None', align='mid', orientation=hist_orientation, density=True, stacked=True, cumulative=True ) if (ix == nfit-1): ax.set_ylim([y_min, y_max]) if(cli.overplot is not None): # plot selected overplot sample ax.axhline(overp_par[ix], color='C0', ls='--', lw=1.1, alpha=0.5) else: ax.set_xlim([x_min, x_max]) if(cli.overplot is not None): # plot selected overplot sample ax.axvline(overp_par[ix], color='C0', ls='--', lw=1.1, alpha=0.5) ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False) ax.set_title(kel_plot_labels[ix], fontsize=label_size) plt.draw() logger.info('saving plot') emcee_fig_file = os.path.join(emcee_plots, 'emcee_triangle.png') fig.savefig(emcee_fig_file, bbox_inches='tight', dpi=300) logger.info('png done') emcee_fig_file = os.path.join(emcee_plots, 'emcee_triangle.pdf') fig.savefig(emcee_fig_file, bbox_inches='tight', dpi=96) logger.info('pdf done') plt.close(fig) logger.info('') return
def main(): print print ' ======================== ' print ' TRADES+EMCEE CHAIN PLOTS' print ' ======================== ' print # read cli arguments cli = anc.get_args() # computes mass conversion factor #m_factor, m_unit = anc.mass_conversion_factor_and_unit(cli.m_type) m_factor, m_unit = anc.mass_type_factor(1., cli.m_type, False) # set emcee and trades folder emcee_folder = cli.full_path trades_folder = os.path.join(os.path.dirname(cli.full_path), '') # and best folder emcee_file, emcee_best, folder_best = anc.get_emcee_file_and_best(emcee_folder, cli.temp_status) parameter_names_emcee, parameter_boundaries, chains, acceptance_fraction, autocor_time, lnprobability, ln_err_const, completed_steps = anc.get_data(emcee_file, cli.temp_status) # set label and legend names kel_labels = anc.keplerian_legend(parameter_names_emcee, cli.m_type) nfit, nwalkers, nruns, nburnin, nruns_sel = anc.get_emcee_parameters(chains, cli.temp_status, cli.nburnin, completed_steps) anc.print_memory_usage(chains) chains_T_full, parameter_boundaries = anc.select_transpose_convert_chains(nfit, nwalkers, nburnin, nruns, nruns_sel, m_factor, parameter_names_emcee, parameter_boundaries, chains) if(cli.use_thin or cli.use_thin > 0): chains_T, flatchain_posterior_0, lnprob_burnin, thin_steps, chains_T_full_thinned = anc.thin_the_chains(cli.use_thin, nburnin, nruns, nruns_sel, autocor_time, chains_T_full, lnprobability, burnin_done=False, full_chains_thinned=True) nburnin_plt = np.rint(nburnin / thin_steps).astype(int) nend = np.rint(nruns / thin_steps).astype(int) else: chains_T, flatchain_posterior_0, lnprob_burnin, thin_steps = anc.thin_the_chains(cli.use_thin, nburnin, nruns, nruns_sel, autocor_time, chains_T_full, lnprobability, burnin_done=False, full_chains_thinned=False) nburnin_plt = nburnin nend = nruns #name_par, name_excluded = anc.get_sample_list(cli.sample_str, parameter_names_emcee) #sample_parameters, idx_sample = anc.pick_sample_parameters(flatchain_posterior_0, parameter_names_emcee, name_par = name_par, name_excluded = name_excluded) #flatchain_posterior_1 = flatchain_posterior_0 # fix lambda? #flatchain_posterior_0 = anc.fix_lambda(flatchain_posterior_0, #parameter_names_emcee #) if(cli.boot_id > 0): flatchain_posterior_msun = anc.posterior_back_to_msun(m_factor,parameter_names_emcee,flatchain_posterior_0) boot_file = anc.save_bootstrap_like(emcee_folder, cli.boot_id, parameter_names_emcee, flatchain_posterior_msun) logger.info('saved bootstrap like file: %s' %(boot_file)) del flatchain_posterior_msun k = anc.get_auto_bins(flatchain_posterior_0) try: overplot = int(cli.overplot) except: overplot = None ## OPEN summary_parameters.hdf5 FILE s_h5f = h5py.File(os.path.join(cli.full_path, 'summary_parameters.hdf5'), 'r') if(overplot is not None): # sample_parameters ci_fitted = s_h5f['confidence_intervals/fitted/ci'][...] sample_parameters = s_h5f['parameters/0666/fitted/parameters'][...] sample_lgllhd = s_h5f['parameters/0666'].attrs['lgllhd'] try: sample2_parameters = s_h5f['parameters/0667/fitted/parameters'][...] sample2_lgllhd = s_h5f['parameters/0667'].attrs['lgllhd'] except: sample2_parameters = None sample2_lgllhd = None try: sample3_parameters = s_h5f['parameters/0668/fitted/parameters'][...] sample3_lgllhd = s_h5f['parameters/0668'].attrs['lgllhd'] except: sample3_parameters = None sample3_lgllhd = None median_parameters = s_h5f['parameters/1051/fitted/parameters'][...] median_lgllhd = s_h5f['parameters/1051'].attrs['lgllhd'] max_lnprob_parameters = s_h5f['parameters/2050/fitted/parameters'][...] max_lgllhd = s_h5f['parameters/2050'].attrs['lgllhd'] try: mode_parameters = s_h5f['parameters/3051/fitted/parameters'][...] mode_lgllhd = s_h5f['parameters/3051'].attrs['lgllhd'] except: mode_parameters = None mode_lgllhd = None overp_par = s_h5f['parameters/%04d/fitted/parameters' %(overplot)][...] overp_lgllhd = s_h5f['parameters/%04d' %(overplot)].attrs['lgllhd'] #nfit = s_h5f['confidence_intervals/fitted'].attrs['nfit'] ndata = s_h5f['confidence_intervals/fitted'].attrs['ndata'] dof = s_h5f['confidence_intervals/fitted'].attrs['dof'] s_h5f.close() emcee_plots = os.path.join(cli.full_path,'plots') if (not os.path.isdir(emcee_plots)): os.makedirs(emcee_plots) for i in range(0, nfit): if('Ms' in parameter_names_emcee[i]): conv_plot = m_factor else: conv_plot = 1. emcee_fig_file = os.path.join(emcee_plots, 'chain_%03d_%s.png' %(i+1, parameter_names_emcee[i].strip())) print ' %s' %(emcee_fig_file), #fig, (axChain, axHist) = plt.subplots(nrows=1, ncols=2, figsize=(12,12)) fig, (axChain, axHist) = plt.subplots(nrows=1, ncols=2, figsize=(6,6)) (counts, bins_val, patches) = axHist.hist(flatchain_posterior_0[:,i], bins=k, range=(flatchain_posterior_0[:,i].min(), flatchain_posterior_0[:,i].max()), orientation='horizontal', density=True, stacked=True, histtype='stepfilled', color='darkgrey', edgecolor='lightgray', align='mid' ) xpdf = scipy_norm.pdf(flatchain_posterior_0[:,i], loc = flatchain_posterior_0[:,i].mean(), scale = flatchain_posterior_0[:,i].std()) idx = np.argsort(flatchain_posterior_0[:,i]) axHist.plot(xpdf[idx], flatchain_posterior_0[idx,i], color='black', marker='None', ls='-.', lw=1.5, label='pdf') # chains after burn-in #axChain.plot(chains_T[:,:,i], '-', alpha=0.3) # chains with the burn-in if(cli.use_thin): axChain.plot(chains_T_full_thinned[:,:,i], '-', alpha=0.3) else: axChain.plot(chains_T_full[:,:,i], '-', alpha=0.3) axChain.axvspan(0, nburnin_plt, color='gray', alpha=0.45) axChain.axvline(nburnin_plt, color='gray', ls='-', lw=1.5) if(overplot is not None): if(mode_parameters is not None): # plot of mode (mean of higher peak/bin) axChain.axhline(mode_parameters[i]*conv_plot, color='red', ls='-', lw=2.1, alpha=1, label='mode') # plot of median axChain.axhline(median_parameters[i]*conv_plot, marker='None', c='blue',ls='-', lw=2.1, alpha=1.0, label='median fit') # plot of max_lnprob axChain.axhline(max_lnprob_parameters[i]*conv_plot, marker='None', c='black',ls='-', lw=1.1, alpha=1.0, label='max lnprob') if(sample_parameters is not None): # plot of sample_parameters axChain.axhline(sample_parameters[i]*conv_plot, marker='None', c='orange',ls='--', lw=2.3, alpha=0.77, label='picked: %12.7f' %(sample_parameters[i])) if(sample2_parameters is not None): # plot of sample2_parameters axChain.axhline(sample2_parameters[i]*conv_plot, marker='None', c='cyan',ls=':', lw=2.7, alpha=0.77, label='close lgllhd: %12.7f' %(sample2_parameters[i])) if(sample3_parameters is not None): # plot of sample3_parameters axChain.axhline(sample3_parameters[i]*conv_plot, marker='None', c='yellow',ls='-', lw=3.1, alpha=0.66, label='close lgllhd: %12.7f' %(sample3_parameters[i])) if(overplot not in [1050, 1051, 2050, 3050, 3051]): axChain.axhline(overp_par[i]*conv_plot, marker='None', c='black',ls='--', lw=2.5, alpha=0.6, label='overplot %d' %(overplot)) # plot ci axChain.axhline(ci_fitted[i,0]*conv_plot, marker='None', c='forestgreen',ls='-', lw=2.1, alpha=1.0, label='CI 15.865th (%.5f)' %(ci_fitted[i,0]*conv_plot)) axChain.axhline(ci_fitted[i,1]*conv_plot, marker='None', c='forestgreen',ls='-', lw=2.1, alpha=1.0, label='CI 84.135th (%.5f)' %(ci_fitted[i,1]*conv_plot)) axChain.ticklabel_format(useOffset=False) xlabel = '$N_\mathrm{steps}$' if(cli.use_thin): xlabel = '$N_\mathrm{steps} \\times %d$' %(thin_steps) axChain.set_xlabel(xlabel) axChain.set_xlim([0, nend]) axChain.set_ylabel(kel_labels[i]) y_min = flatchain_posterior_0[:,i].min() y_max = flatchain_posterior_0[:,i].max() axChain.set_ylim([y_min, y_max]) axChain.set_title('Full chain %s:=[%.3f , %.3f]' %(kel_labels[i], parameter_boundaries[i,0], parameter_boundaries[i,1])) plt.draw() axHist.ticklabel_format(useOffset=False) axHist.tick_params(direction='inout', labelleft=False) axHist.set_ylim([y_min, y_max]) if(overplot is not None): if(mode_parameters is not None): # plot mode axHist.axhline(mode_parameters[i]*conv_plot, color='red', ls='-', lw=2.1, alpha=1, label='mode') # plot median axHist.axhline(median_parameters[i]*conv_plot, marker='None', c='blue',ls='-', lw=2.1, alpha=1.0, label='median fit') # plot of max_lnprob axHist.axhline(max_lnprob_parameters[i]*conv_plot, marker='None', c='black',ls='-', lw=1.1, alpha=1.0, label='max lnprob') if(sample_parameters is not None): # plot of sample_parameters axHist.axhline(sample_parameters[i]*conv_plot, marker='None', c='orange',ls='--', lw=2.3, alpha=0.77, label='picked: %12.7f' %(sample_parameters[i]*conv_plot)) if(sample2_parameters is not None): # plot of sample2_parameters axHist.axhline(sample2_parameters[i]*conv_plot, marker='None', c='cyan',ls=':', lw=2.7, alpha=0.77, label='close lgllhd: %12.7f' %(sample2_parameters[i])) if(sample3_parameters is not None): # plot of sample3_parameters axHist.axhline(sample3_parameters[i]*conv_plot, marker='None', c='yellow',ls='-', lw=3.1, alpha=0.66, label='close lgllhd: %12.7f' %(sample3_parameters[i])) if(overplot not in [1050, 1051, 2050, 3050, 3051]): axHist.axhline(overp_par[i]*conv_plot, marker='None', c='black',ls='--', lw=2.5, alpha=0.8, label='overplot %d' %(overplot)) # plot ci axHist.axhline(ci_fitted[i,0]*conv_plot, marker='None', c='forestgreen',ls='-', lw=2.1, alpha=1.0, label='CI 15.865th (%.5f)' %(ci_fitted[i,0]*conv_plot)) axHist.axhline(ci_fitted[i,1]*conv_plot, marker='None', c='forestgreen',ls='-', lw=2.1, alpha=1.0, label='CI 84.135th (%.5f)' %(ci_fitted[i,1]*conv_plot)) axHist.set_title('Distribution of posterior chain') axHist.legend(loc='center left', fontsize=9, bbox_to_anchor=(1, 0.5)) plt.draw() fig.savefig(emcee_fig_file, bbox_inches='tight', dpi=150) print ' saved' print #fig = plt.figure(figsize=(12,12)) fig = plt.figure(figsize=(6,6)) # lnprob xlabel = '$N_\mathrm{steps}$' if(cli.use_thin): xlabel = '$N_\mathrm{steps} \\times %d$' %(thin_steps) ax = plt.subplot2grid((2,1), (0,0)) ax.plot(lnprob_burnin.T, '-', alpha=0.3) if(overplot is not None): posterior_msun = anc.posterior_back_to_msun(m_factor,parameter_names_emcee,flatchain_posterior_0) post_sel, lnprob_sel = anc.select_within_all_ci(posterior_msun, ci_fitted[:,0:2], lnprob_burnin.T.reshape(-1)) #lnprob_sel = lnprob_burnin.T.reshape((-1)) lgllhd_med = np.percentile(lnprob_burnin.T.reshape(-1), 50., interpolation='midpoint') abs_dlg = np.abs(lnprob_sel - lgllhd_med) lgllhd_mad = np.percentile(abs_dlg, 50., interpolation='midpoint') #lnp_min = np.min(lnprob_sel) #lnp_max = np.max(lnprob_sel) lnp_min = lgllhd_med - lgllhd_mad lnp_max = lgllhd_med + lgllhd_mad print ' lgllhd_med & mad = ',lgllhd_med, lgllhd_mad print ' lnp_min = ',lnp_min, ' lnp_max = ',lnp_max print ' lnl_668 = ',sample3_lgllhd ax.axhline(lgllhd_med, color='black', ls='-', lw=1.6, alpha=0.77) #if(sample2_lgllhd is not None): #ax.axhline(sample2_lgllhd, marker='None', c='cyan',ls=':', lw=2.7, alpha=0.9) if(sample3_lgllhd is not None): ax.axhline(sample3_lgllhd, marker='None', c='yellow',ls='-', lw=3.1, alpha=0.9) ax.axhspan(lnp_min, lnp_max, color='gray', alpha=0.77) ax.axhline(lnp_min, color='black', ls='--', lw=1.6, alpha=0.77) ax.axhline(lnp_max, color='black', ls='--', lw=1.6, alpha=0.77) min_lnp = np.min(lnprob_burnin.T, axis=0).min() max_lnp = np.max(lnprob_burnin.T, axis=0).max() y_min, y_max = anc.compute_limits(np.asarray([min_lnp, max_lnp]), 0.05) ax.set_ylim((y_min, y_max)) ax.set_ylabel('lnprob') #ax.get_xaxis().set_visible(False) ax.set_xlabel(xlabel) # chi2r chi2r = -2.*(lnprob_burnin.T-ln_err_const)/np.float64(dof) ax = plt.subplot2grid((2,1), (1,0)) ax.axhline(1.0, color='gray', ls='-') ax.plot(chi2r, '-', alpha=0.3) if(overplot is not None): c2r_med = -(2.*(lgllhd_med - ln_err_const))/np.float64(dof) c2r_smax = -(2.*(lnp_min - ln_err_const))/np.float64(dof) c2r_smin = -(2.*(lnp_max - ln_err_const))/np.float64(dof) print ' c2r_med = ',c2r_med print ' c2r_smin = ',c2r_smin, ' c2r_smax = ', c2r_smax ax.axhline(c2r_med, color='black', ls='-', lw=1.6, alpha=0.77) ax.axhspan(c2r_smin, c2r_smax, color='gray', alpha=0.77) ax.axhline(c2r_smin, color='black', ls='--', lw=1.6, alpha=0.77) ax.axhline(c2r_smax, color='black', ls='--', lw=1.6, alpha=0.77) #if(sample2_lgllhd is not None): #c2r_sample2 = -2.*(sample2_lgllhd - ln_err_const)/np.float64(dof) #ax.axhline(c2r_sample2, marker='None', c='cyan',ls=':', lw=2.7, alpha=0.9) if(sample3_lgllhd is not None): c2r_sample3 = -2.*(sample3_lgllhd - ln_err_const)/np.float64(dof) ax.axhline(c2r_sample3, marker='None', c='yellow',ls='-', lw=3.1, alpha=0.9) c2r_min = -2.*(y_max - ln_err_const)/np.float64(dof) c2r_max = -2.*(y_min - ln_err_const)/np.float64(dof) ax.set_ylim((c2r_min, c2r_max)) ax.set_ylabel('$\chi^{2}/\mathrm{dof}$') #ax.get_xaxis().set_visible(True) ax.set_xlabel(xlabel) fig.savefig(os.path.join(emcee_plots, 'emcee_lnprobability.png'), bbox_inches='tight', dpi=150) print ' %s saved' %(os.path.join(emcee_plots, 'emcee_lnprobability.png')) return
def main(): cli = anc.get_args() # read derived posterior file derived_file = os.path.join(cli.full_path, 'derived_posterior.hdf5') h5f = h5py.File(derived_file, 'r') derived_names = np.array(h5f['derived_names'], dtype='S10') derived_posterior_in = np.array(h5f['derived_posterior'], dtype=np.float64) h5f.close() n_der = derived_names.shape[0] n_flatchain = derived_posterior_in.shape[0] derived_posterior = anc.derived_posterior_check(derived_names, derived_posterior_in) label_separation=-0.90 # if uses this, comment ax.xyaxis.labelpad = label_pad label_pad = 12 # it uses this, comment ax.xyaxis.set_label_coords()... label_size = 8 ticklabel_size = 4 if(n_der > 2): #label_separation = -0.1 - ( 0.075 * (n_der-2) ) label_separation = -0.15 - ( 0.125 * (n_der-2) ) #else: #label_separation = -0.15 #label_size = label_size - 1 * int(n_der / 10.) #label_size = label_size - 1 * int(n_der / 5.) label_size = label_size - 1 * int(n_der / 2.5) labels_list = anc.derived_labels(derived_names, cli.m_type) k = anc.get_bins(derived_posterior, rule='doane') if(cli.overplot is not None): ## OPEN summary_parameters.hdf5 FILE s_h5f = h5py.File(os.path.join(cli.full_path, 'summary_parameters.hdf5'), 'r') # take only the selected sample s_overplot = '%04d' %(cli.overplot) #overp_der = s_h5f['parameters/%s/derived/parameters' %(s_overplot)][...] read_der = s_h5f['parameters/%s/derived/parameters' %(s_overplot)][...] s_h5f.close() overp_der = anc.derived_parameters_check(derived_names, read_der, derived_posterior) #fig = plt.figure(figsize=(12,12)) fig = plt.figure(figsize=(6,6)) fig.subplots_adjust(hspace=0.05, wspace=0.05) for ix in range(0, n_der): x_data = derived_posterior[:,ix] x_min, x_max = anc.compute_limits(x_data, 0.05) if(x_min == x_max): x_min = x_min - 1. x_max = x_max + 1. for iy in range(0, n_der): y_data = derived_posterior[:,iy] y_min, y_max = anc.compute_limits(y_data, 0.05) if(y_min == y_max): y_min = y_min - 1. y_max = y_max + 1. if(iy > ix): # correlation plot anc.print_both('correlation %s vs %s' %(derived_names[ix], derived_names[iy]) ) ax = plt.subplot2grid((n_der+1, n_der), (iy,ix)) hist2d_counts, xedges, yedges, image2d = ax.hist2d(\ x_data, y_data, bins=k, range=[[x_data.min(), x_data.max()],[y_data.min(), y_data.max()]], cmap=cm.gray_r, #normed=True normed=False ) #new_k = int(k/3) new_k = k hist2d_counts_2, xedges_2, yedges_2 = np.histogram2d(\ x_data, y_data, bins=new_k, range=[[x_data.min(), x_data.max()],[y_data.min(), y_data.max()]], #normed=True density=False ) x_bins = [0.5*(xedges_2[i]+xedges_2[i+1]) for i in range(0, new_k)] y_bins = [0.5*(yedges_2[i]+yedges_2[i+1]) for i in range(0, new_k)] nl = 5 levels = [1.-np.exp(-0.5*ii) for ii in range(0,nl)] # 2D sigmas: 0sigma, 1sigma, 2sigma, 3sigma, .. ax.contour(x_bins, y_bins, hist2d_counts_2.T, nl, cmap=cm.viridis, linestyles='solid', linewidths=0.5, #normed=True ) if(cli.overplot is not None): # plot selected overplot sample # check angle and plot %360 and %-360... if('w' in derived_names[ix] or 'lN' in derived_names[ix] or 'mA' in derived_names[ix]): ax.axvline(overp_der[ix]%360., color='C0', ls='--', lw=1.1, alpha=0.7) ax.axvline(overp_der[ix]%-360., color='C0', ls='--', lw=1.1, alpha=0.7) else: ax.axvline(overp_der[ix], color='C0', ls='--', lw=1.1, alpha=0.7) if('w' in derived_names[iy] or 'lN' in derived_names[iy] or 'mA' in derived_names[iy]): ax.axhline(overp_der[iy]%360., color='C0', ls='--', lw=1.1, alpha=0.7) ax.axhline(overp_der[iy]%-360., color='C0', ls='--', lw=1.1, alpha=0.7) else: ax.axhline(overp_der[iy], color='C0', ls='--', lw=1.1, alpha=0.7) ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False) if(iy == n_der-1): set_xaxis(ax, label_size, label_separation, label_pad, ticklabel_size, labels_list[ix], [xedges[0], xedges[-1], 4]) if(ix == 0): set_yaxis(ax, label_size, label_separation, label_pad, ticklabel_size, labels_list[iy], [yedges[0], yedges[-1], 5]) ax.set_ylim([y_min, y_max]) ax.set_xlim([x_min, x_max]) plt.draw() elif(iy == ix): # distribution plot anc.print_both('%s histogram' %(derived_names[ix])) ax = plt.subplot2grid((n_der+1, n_der), (ix,ix)) if (ix == n_der-1): hist_orientation='horizontal' else: hist_orientation='vertical' idx = np.argsort(x_data) if(not cli.cumulative): # HISTOGRAM hist_counts, edges, patces = ax.hist(x_data, bins=k, range=[x_data.min(), x_data.max()], histtype='stepfilled', color='darkgrey', #edgecolor='lightgray', edgecolor='None', align='mid', orientation=hist_orientation, #normed=True, density=True, stacked=True ) else: # CUMULATIVE HISTOGRAM hist_counts, edges, patces = ax.hist(x_data, bins=k, range=[x_data.min(), x_data.max()], histtype='stepfilled', color='darkgrey', #edgecolor='lightgray', edgecolor='None', align='mid', orientation=hist_orientation, density=True, stacked=True, cumulative=True ) #print parameter_names_emcee[ix], overp_der[ix] if (ix == n_der-1): if(cli.overplot is not None): # check angle and plot %360 and %-360... if('w' in derived_names[ix] or 'lN' in derived_names[ix] or 'mA' in derived_names[ix]): ax.axhline(overp_der[ix]%360., color='C0', ls='--', lw=1.1, alpha=0.7) ax.axhline(overp_der[ix]%-360., color='C0', ls='--', lw=1.1, alpha=0.7) else: # plot selected overplot sample ax.axhline(overp_der[ix], color='C0', ls='--', lw=1.1, alpha=0.7) ax.set_ylim([y_min, y_max]) else: if(cli.overplot is not None): if('w' in derived_names[ix] or 'lN' in derived_names[ix] or 'mA' in derived_names[ix]): ax.axvline(overp_der[ix]%360., color='C0', ls='--', lw=1.1, alpha=0.7) ax.axvline(overp_der[ix]%-360., color='C0', ls='--', lw=1.1, alpha=0.7) else: # plot selected overplot sample ax.axvline(overp_der[ix], color='C0', ls='--', lw=1.1, alpha=0.7) ax.set_xlim([x_min, x_max]) if(cli.overplot is not None): print derived_names[ix], ' overplot val = ', overp_der[ix], ' min = ', x_data.min(), ' max = ', x_data.max() ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False) ax.set_title(labels_list[ix], fontsize=label_size) plt.draw() plot_folder = os.path.join(cli.full_path, 'plots') if (not os.path.isdir(plot_folder)): os.makedirs(plot_folder) correlation_file = os.path.join(plot_folder, 'derived_triangle.png') fig.savefig(correlation_file, bbox_inches='tight', dpi=300) anc.print_both('png done') correlation_file = os.path.join(plot_folder, 'derived_triangle.pdf') fig.savefig(correlation_file, bbox_inches='tight', dpi=96) anc.print_both('pdf done') plt.close(fig) return
def main(): # --- # initialize logger logger = logging.getLogger("Main_log") logger.setLevel(logging.DEBUG) formatter = logging.Formatter("%(asctime)s - %(message)s") # global variables label_separation = -0.90 # if uses this, comment ax.xyaxis.labelpad = label_pad label_pad = 12 # it uses this, comment ax.xyaxis.set_label_coords()... label_size = 8 ticklabel_size = 4 def set_xaxis(ax, label_size, label_separation, label_pad, ticklabel_size, kel_label, ticks_formatter, tick_fmt='%.4f'): ax.get_xaxis().set_visible(True) ax.xaxis.set_tick_params(labelsize=ticklabel_size) ax.xaxis.set_label_coords(0.5, label_separation) #ax.ticklabel_format(style='plain', axis='both', useOffset=False) plt.setp(ax.xaxis.get_majorticklabels(), rotation=70) #ax.xaxis.labelpad = label_pad ax.set_xlabel(kel_label, fontsize=label_size) tick_step = (ticks_formatter[1] - ticks_formatter[0]) / ticks_formatter[2] ax.xaxis.set_ticks( np.arange(ticks_formatter[0], ticks_formatter[1], tick_step)) tick_formatter = FormatStrFormatter(tick_fmt) ax.xaxis.set_major_formatter(tick_formatter) return def set_yaxis(ax, label_size, label_separation, label_pad, ticklabel_size, kel_label, ticks_formatter, tick_fmt='%.4f'): ax.get_yaxis().set_visible(True) ax.yaxis.set_tick_params(labelsize=ticklabel_size) ax.yaxis.set_label_coords(label_separation, 0.5) #ax.ticklabel_format(style='plain', axis='both', useOffset=False) #ax.yaxis.labelpad = label_pad ax.set_ylabel(kel_label, fontsize=label_size) tick_step = (ticks_formatter[1] - ticks_formatter[0]) / ticks_formatter[2] ax.yaxis.set_ticks( np.arange(ticks_formatter[0], ticks_formatter[1], tick_step)) tick_formatter = FormatStrFormatter(tick_fmt) ax.yaxis.set_major_formatter(tick_formatter) return print print ' ================== ' print ' CORRELATION PLOTS' print ' ================== ' print # read cli arguments cli = anc.get_args() #plot_folder = prepare_plot_folder(working_path) emcee_plots = anc.prepare_emcee_plot_folder(cli.full_path) log_file = os.path.join(emcee_plots, 'emcee_triangle_log.txt') flog = logging.FileHandler(log_file, 'w') flog.setLevel(logging.DEBUG) flog.setFormatter(formatter) logger.addHandler(flog) # log screen slog = logging.StreamHandler() slog.setLevel(logging.DEBUG) slog.setFormatter(formatter) logger.addHandler(slog) # computes mass conversion factor #m_factor = anc.mass_conversion_factor(cli.m_type) m_factor, m_unit = anc.mass_type_factor(1., cli.m_type, False) # set emcee and trades folder emcee_folder = cli.full_path trades_folder = os.path.join(os.path.dirname(cli.full_path), '') # and best folder emcee_file, emcee_best, folder_best = anc.get_emcee_file_and_best( emcee_folder, cli.temp_status) # get data from the hdf5 file parameter_names_emcee, parameter_boundaries, chains, acceptance_fraction, autocor_time, lnprobability, ln_err_const, completed_steps = anc.get_data( emcee_file, cli.temp_status) # print Memory occupation of ... anc.print_memory_usage(chains) nfit, nwalkers, nruns, nburnin, nruns_sel = anc.get_emcee_parameters( chains, cli.temp_status, cli.nburnin, completed_steps) logger.info( 'nfit(%d), nwalkers(%d), nruns(%d), nburnin(%d), nruns_sel(%d)' % (nfit, nwalkers, nruns, nburnin, nruns_sel)) # test label_separation #if (nfit <= 3): label_separation = -0.1 if (nfit > 2): #label_separation = -0.1 - ( 0.075 * (nfit-2) ) # good for figsize=(12,12) label_separation = -0.15 - (0.125 * (nfit - 2)) # testing #else: #label_separation = -0.15 #label_size = label_size - 1 * int(nfit / 5.) label_size = label_size - 1 * int(nfit / 2.5) # set label and legend names kel_plot_labels = anc.keplerian_legend(parameter_names_emcee, cli.m_type) chains_T_full, parameter_boundaries = anc.select_transpose_convert_chains( nfit, nwalkers, nburnin, nruns, nruns_sel, m_factor, parameter_names_emcee, parameter_boundaries, chains) chains_T, flatchain_posterior_0, lnprob_burnin, thin_steps = anc.thin_the_chains( cli.use_thin, nburnin, nruns, nruns_sel, autocor_time, chains_T_full, lnprobability, burnin_done=False) flatchain_posterior_0 = anc.fix_lambda(flatchain_posterior_0, parameter_names_emcee) if (cli.boot_id > 0): flatchain_posterior_msun = anc.posterior_back_to_msun( m_factor, parameter_names_emcee, flatchain_posterior_0) boot_file = anc.save_bootstrap_like(emcee_folder, cli.boot_id, parameter_names_emcee, flatchain_posterior_msun) logger.info('saved bootstrap like file: %s' % (boot_file)) del flatchain_posterior_msun k = anc.get_auto_bins(flatchain_posterior_0) if (cli.overplot is not None): if (cli.adhoc is not None): overp_names, read_par = anc.read_fitted_file(cli.adhoc) cli.overplot = 777 else: ## OPEN summary_parameters.hdf5 FILE s_h5f = h5py.File( os.path.join(cli.full_path, 'summary_parameters.hdf5'), 'r') # take only the selected sample s_overplot = '%04d' % (cli.overplot) read_par = s_h5f['parameters/%s/fitted/parameters' % (s_overplot)][...] s_h5f.close() # fitted parameters has always Mp/Ms in Msun/Mstar, so it is needed to rescale it properly overp_par = read_par.copy() for ii in range(0, nfit): if ('Ms' in parameter_names_emcee[ii]): #if('Ms' in overp_names[ii]): overp_par[ii] = overp_par[ii] * m_factor #fig, ax = plt.subplots(nrows = nfit-1, ncols=nfit, figsize=(12,12)) #fig = plt.figure(figsize=(12,12)) fig = plt.figure(figsize=(6, 6)) fig.subplots_adjust(hspace=0.05, wspace=0.05) for ix in range(0, nfit, 1): x_data = flatchain_posterior_0[:, ix] ##x_med = median_parameters[ix] x_min, x_max = anc.compute_limits(x_data, 0.05) if (x_min == x_max): x_min = parameter_boundaries[ix, 0] x_max = parameter_boundaries[ix, 1] #x_max_mean = mode_parameters[ix] for iy in range(nfit - 1, -1, -1): y_data = flatchain_posterior_0[:, iy] y_min, y_max = anc.compute_limits(y_data, 0.05) if (y_min == y_max): y_min = parameter_boundaries[iy, 0] y_max = parameter_boundaries[iy, 1] #y_max_mean = mode_parameters[iy] if (iy > ix): # correlation plot logger.info( '%s vs %s' % (parameter_names_emcee[ix], parameter_names_emcee[iy])) ax = plt.subplot2grid((nfit + 1, nfit), (iy, ix)) #hist2d_counts, xedges, yedges, image2d = ax.hist2d(x_data, y_data, bins=k, range=[[x_data.min(), x_data.max()],[y_data.min(), y_data.max()]], cmap=cm.gray_r, normed=True) hist2d_counts, xedges, yedges, image2d = ax.hist2d(\ x_data, y_data, bins=k, range=[[x_data.min(), x_data.max()],[y_data.min(), y_data.max()]], cmap=cm.gray_r, normed=False #density=False ) #new_k = int(k/3) new_k = k hist2d_counts_2, xedges_2, yedges_2 = np.histogram2d(\ x_data, y_data, bins=new_k, range=[[x_data.min(), x_data.max()],[y_data.min(), y_data.max()]], #normed=True density=False ) x_bins = [ 0.5 * (xedges_2[i] + xedges_2[i + 1]) for i in range(0, new_k) ] y_bins = [ 0.5 * (yedges_2[i] + yedges_2[i + 1]) for i in range(0, new_k) ] #ax.contour(x_bins, y_bins, hist2d_counts_2.T, 3, cmap=cm.gray, linestyle='solid', linewidths=(0.7, 0.7, 0.7)) nl = 5 levels = [1. - np.exp(-0.5 * ii) for ii in range(0, nl) ] # 2D sigmas: 0sigma, 1sigma, 2sigma, 3sigma, .. #ax.contour(x_bins, y_bins, hist2d_counts_2.T, levels, cmap=cm.gray, linestyle='solid', linewidths=(0.7, 0.7, 0.7)) #ax.contour(x_bins, y_bins, hist2d_counts_2.T, levels, cmap=cm.viridis, linestyle='solid', linewidths=1.) #ax.contour(x_bins, y_bins, hist2d_counts_2.T, cmap=cm.viridis, linestyle='solid', linewidths=0.7) #ax.contour(x_bins, y_bins, hist2d_counts_2.T, levels, cmap=cm.viridis, linestyle='solid', linewidths=0.7, normed=True) ax.contour( x_bins, y_bins, hist2d_counts_2.T, nl, cmap=cm.viridis, linestyles='solid', linewidths=0.5, #normed=True ) if (cli.overplot is not None): # plot selected overplot sample ax.axvline(overp_par[ix], color='C0', ls='--', lw=1.1, alpha=0.5) ax.axhline(overp_par[iy], color='C0', ls='--', lw=1.1, alpha=0.5) ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False) if (iy == nfit - 1): set_xaxis(ax, label_size, label_separation, label_pad, ticklabel_size, kel_plot_labels[ix], [xedges[0], xedges[-1], 4]) if (ix == 0): set_yaxis(ax, label_size, label_separation, label_pad, ticklabel_size, kel_plot_labels[iy], [yedges[0], yedges[-1], 5]) ax.set_ylim([y_min, y_max]) ax.set_xlim([x_min, x_max]) plt.draw() elif (iy == ix): # distribution plot logger.info('%s histogram' % (parameter_names_emcee[ix])) ax = plt.subplot2grid((nfit + 1, nfit), (ix, ix)) if (ix == nfit - 1): hist_orientation = 'horizontal' else: hist_orientation = 'vertical' idx = np.argsort(x_data) if (not cli.cumulative): # HISTOGRAM hist_counts, edges, patces = ax.hist( x_data, bins=k, range=[x_data.min(), x_data.max()], histtype='stepfilled', color='darkgrey', #edgecolor='lightgray', edgecolor='None', align='mid', orientation=hist_orientation, #normed=True, density=True, stacked=True) else: # CUMULATIVE HISTOGRAM hist_counts, edges, patces = ax.hist( x_data, bins=k, range=[x_data.min(), x_data.max()], histtype='stepfilled', color='darkgrey', #edgecolor='lightgray', edgecolor='None', align='mid', orientation=hist_orientation, density=True, stacked=True, cumulative=True) if (ix == nfit - 1): ax.set_ylim([y_min, y_max]) if (cli.overplot is not None): # plot selected overplot sample ax.axhline(overp_par[ix], color='C0', ls='--', lw=1.1, alpha=0.5) else: ax.set_xlim([x_min, x_max]) if (cli.overplot is not None): # plot selected overplot sample ax.axvline(overp_par[ix], color='C0', ls='--', lw=1.1, alpha=0.5) ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False) ax.set_title(kel_plot_labels[ix], fontsize=label_size) plt.draw() logger.info('saving plot') emcee_fig_file = os.path.join(emcee_plots, 'emcee_triangle.png') fig.savefig(emcee_fig_file, bbox_inches='tight', dpi=300) logger.info('png done') emcee_fig_file = os.path.join(emcee_plots, 'emcee_triangle.pdf') fig.savefig(emcee_fig_file, bbox_inches='tight', dpi=96) logger.info('pdf done') plt.close(fig) logger.info('') return
def main(): print print ' ======================== ' print ' TRADES+EMCEE CHAIN PLOTS' print ' ======================== ' print # read cli arguments cli = anc.get_args() # computes mass conversion factor #m_factor, m_unit = anc.mass_conversion_factor_and_unit(cli.m_type) m_factor, m_unit = anc.mass_type_factor(1., cli.m_type, False) # set emcee and trades folder emcee_folder = cli.full_path trades_folder = os.path.join(os.path.dirname(cli.full_path), '') # and best folder emcee_file, emcee_best, folder_best = anc.get_emcee_file_and_best( emcee_folder, cli.temp_status) parameter_names_emcee, parameter_boundaries, chains, acceptance_fraction, autocor_time, lnprobability, ln_err_const, completed_steps = anc.get_data( emcee_file, cli.temp_status) # set label and legend names kel_labels = anc.keplerian_legend(parameter_names_emcee, cli.m_type) nfit, nwalkers, nruns, nburnin, nruns_sel = anc.get_emcee_parameters( chains, cli.temp_status, cli.nburnin, completed_steps) anc.print_memory_usage(chains) chains_T_full, parameter_boundaries = anc.select_transpose_convert_chains( nfit, nwalkers, nburnin, nruns, nruns_sel, m_factor, parameter_names_emcee, parameter_boundaries, chains) if (cli.use_thin or cli.use_thin > 0): chains_T, flatchain_posterior_0, lnprob_burnin, thin_steps, chains_T_full_thinned = anc.thin_the_chains( cli.use_thin, nburnin, nruns, nruns_sel, autocor_time, chains_T_full, lnprobability, burnin_done=False, full_chains_thinned=True) nburnin_plt = np.rint(nburnin / thin_steps).astype(int) nend = np.rint(nruns / thin_steps).astype(int) else: chains_T, flatchain_posterior_0, lnprob_burnin, thin_steps = anc.thin_the_chains( cli.use_thin, nburnin, nruns, nruns_sel, autocor_time, chains_T_full, lnprobability, burnin_done=False, full_chains_thinned=False) nburnin_plt = nburnin nend = nruns #name_par, name_excluded = anc.get_sample_list(cli.sample_str, parameter_names_emcee) #sample_parameters, idx_sample = anc.pick_sample_parameters(flatchain_posterior_0, parameter_names_emcee, name_par = name_par, name_excluded = name_excluded) #flatchain_posterior_1 = flatchain_posterior_0 # fix lambda? #flatchain_posterior_0 = anc.fix_lambda(flatchain_posterior_0, #parameter_names_emcee #) if (cli.boot_id > 0): flatchain_posterior_msun = anc.posterior_back_to_msun( m_factor, parameter_names_emcee, flatchain_posterior_0) boot_file = anc.save_bootstrap_like(emcee_folder, cli.boot_id, parameter_names_emcee, flatchain_posterior_msun) logger.info('saved bootstrap like file: %s' % (boot_file)) del flatchain_posterior_msun k = anc.get_auto_bins(flatchain_posterior_0) try: overplot = int(cli.overplot) except: overplot = None ## OPEN summary_parameters.hdf5 FILE s_h5f = h5py.File(os.path.join(cli.full_path, 'summary_parameters.hdf5'), 'r') if (overplot is not None): # sample_parameters ci_fitted = s_h5f['confidence_intervals/fitted/ci'][...] sample_parameters = s_h5f['parameters/0666/fitted/parameters'][...] sample_lgllhd = s_h5f['parameters/0666'].attrs['lgllhd'] try: sample2_parameters = s_h5f['parameters/0667/fitted/parameters'][ ...] sample2_lgllhd = s_h5f['parameters/0667'].attrs['lgllhd'] except: sample2_parameters = None sample2_lgllhd = None try: sample3_parameters = s_h5f['parameters/0668/fitted/parameters'][ ...] sample3_lgllhd = s_h5f['parameters/0668'].attrs['lgllhd'] except: sample3_parameters = None sample3_lgllhd = None median_parameters = s_h5f['parameters/1051/fitted/parameters'][...] median_lgllhd = s_h5f['parameters/1051'].attrs['lgllhd'] max_lnprob_parameters = s_h5f['parameters/2050/fitted/parameters'][...] max_lgllhd = s_h5f['parameters/2050'].attrs['lgllhd'] try: mode_parameters = s_h5f['parameters/3051/fitted/parameters'][...] mode_lgllhd = s_h5f['parameters/3051'].attrs['lgllhd'] except: mode_parameters = None mode_lgllhd = None overp_par = s_h5f['parameters/%04d/fitted/parameters' % (overplot)][...] overp_lgllhd = s_h5f['parameters/%04d' % (overplot)].attrs['lgllhd'] #nfit = s_h5f['confidence_intervals/fitted'].attrs['nfit'] ndata = s_h5f['confidence_intervals/fitted'].attrs['ndata'] dof = s_h5f['confidence_intervals/fitted'].attrs['dof'] s_h5f.close() emcee_plots = os.path.join(cli.full_path, 'plots') if (not os.path.isdir(emcee_plots)): os.makedirs(emcee_plots) for i in range(0, nfit): if ('Ms' in parameter_names_emcee[i]): conv_plot = m_factor else: conv_plot = 1. emcee_fig_file = os.path.join( emcee_plots, 'chain_%03d_%s.png' % (i + 1, parameter_names_emcee[i].strip())) print ' %s' % (emcee_fig_file), #fig, (axChain, axHist) = plt.subplots(nrows=1, ncols=2, figsize=(12,12)) fig, (axChain, axHist) = plt.subplots(nrows=1, ncols=2, figsize=(6, 6)) (counts, bins_val, patches) = axHist.hist(flatchain_posterior_0[:, i], bins=k, range=(flatchain_posterior_0[:, i].min(), flatchain_posterior_0[:, i].max()), orientation='horizontal', density=True, stacked=True, histtype='stepfilled', color='darkgrey', edgecolor='lightgray', align='mid') xpdf = scipy_norm.pdf(flatchain_posterior_0[:, i], loc=flatchain_posterior_0[:, i].mean(), scale=flatchain_posterior_0[:, i].std()) idx = np.argsort(flatchain_posterior_0[:, i]) axHist.plot(xpdf[idx], flatchain_posterior_0[idx, i], color='black', marker='None', ls='-.', lw=1.5, label='pdf') # chains after burn-in #axChain.plot(chains_T[:,:,i], '-', alpha=0.3) # chains with the burn-in if (cli.use_thin): axChain.plot(chains_T_full_thinned[:, :, i], '-', alpha=0.3) else: axChain.plot(chains_T_full[:, :, i], '-', alpha=0.3) axChain.axvspan(0, nburnin_plt, color='gray', alpha=0.45) axChain.axvline(nburnin_plt, color='gray', ls='-', lw=1.5) if (overplot is not None): if (mode_parameters is not None): # plot of mode (mean of higher peak/bin) axChain.axhline(mode_parameters[i] * conv_plot, color='red', ls='-', lw=2.1, alpha=1, label='mode') # plot of median axChain.axhline(median_parameters[i] * conv_plot, marker='None', c='blue', ls='-', lw=2.1, alpha=1.0, label='median fit') # plot of max_lnprob axChain.axhline(max_lnprob_parameters[i] * conv_plot, marker='None', c='black', ls='-', lw=1.1, alpha=1.0, label='max lnprob') if (sample_parameters is not None): # plot of sample_parameters axChain.axhline(sample_parameters[i] * conv_plot, marker='None', c='orange', ls='--', lw=2.3, alpha=0.77, label='picked: %12.7f' % (sample_parameters[i])) if (sample2_parameters is not None): # plot of sample2_parameters axChain.axhline(sample2_parameters[i] * conv_plot, marker='None', c='cyan', ls=':', lw=2.7, alpha=0.77, label='close lgllhd: %12.7f' % (sample2_parameters[i])) if (sample3_parameters is not None): # plot of sample3_parameters axChain.axhline(sample3_parameters[i] * conv_plot, marker='None', c='yellow', ls='-', lw=3.1, alpha=0.66, label='close lgllhd: %12.7f' % (sample3_parameters[i])) if (overplot not in [1050, 1051, 2050, 3050, 3051]): axChain.axhline(overp_par[i] * conv_plot, marker='None', c='black', ls='--', lw=2.5, alpha=0.6, label='overplot %d' % (overplot)) # plot ci axChain.axhline(ci_fitted[i, 0] * conv_plot, marker='None', c='forestgreen', ls='-', lw=2.1, alpha=1.0, label='CI 15.865th (%.5f)' % (ci_fitted[i, 0] * conv_plot)) axChain.axhline(ci_fitted[i, 1] * conv_plot, marker='None', c='forestgreen', ls='-', lw=2.1, alpha=1.0, label='CI 84.135th (%.5f)' % (ci_fitted[i, 1] * conv_plot)) axChain.ticklabel_format(useOffset=False) xlabel = '$N_\mathrm{steps}$' if (cli.use_thin): xlabel = '$N_\mathrm{steps} \\times %d$' % (thin_steps) axChain.set_xlabel(xlabel) axChain.set_xlim([0, nend]) axChain.set_ylabel(kel_labels[i]) y_min = flatchain_posterior_0[:, i].min() y_max = flatchain_posterior_0[:, i].max() axChain.set_ylim([y_min, y_max]) axChain.set_title('Full chain %s:=[%.3f , %.3f]' % (kel_labels[i], parameter_boundaries[i, 0], parameter_boundaries[i, 1])) plt.draw() axHist.ticklabel_format(useOffset=False) axHist.tick_params(direction='inout', labelleft=False) axHist.set_ylim([y_min, y_max]) if (overplot is not None): if (mode_parameters is not None): # plot mode axHist.axhline(mode_parameters[i] * conv_plot, color='red', ls='-', lw=2.1, alpha=1, label='mode') # plot median axHist.axhline(median_parameters[i] * conv_plot, marker='None', c='blue', ls='-', lw=2.1, alpha=1.0, label='median fit') # plot of max_lnprob axHist.axhline(max_lnprob_parameters[i] * conv_plot, marker='None', c='black', ls='-', lw=1.1, alpha=1.0, label='max lnprob') if (sample_parameters is not None): # plot of sample_parameters axHist.axhline(sample_parameters[i] * conv_plot, marker='None', c='orange', ls='--', lw=2.3, alpha=0.77, label='picked: %12.7f' % (sample_parameters[i] * conv_plot)) if (sample2_parameters is not None): # plot of sample2_parameters axHist.axhline(sample2_parameters[i] * conv_plot, marker='None', c='cyan', ls=':', lw=2.7, alpha=0.77, label='close lgllhd: %12.7f' % (sample2_parameters[i])) if (sample3_parameters is not None): # plot of sample3_parameters axHist.axhline(sample3_parameters[i] * conv_plot, marker='None', c='yellow', ls='-', lw=3.1, alpha=0.66, label='close lgllhd: %12.7f' % (sample3_parameters[i])) if (overplot not in [1050, 1051, 2050, 3050, 3051]): axHist.axhline(overp_par[i] * conv_plot, marker='None', c='black', ls='--', lw=2.5, alpha=0.8, label='overplot %d' % (overplot)) # plot ci axHist.axhline(ci_fitted[i, 0] * conv_plot, marker='None', c='forestgreen', ls='-', lw=2.1, alpha=1.0, label='CI 15.865th (%.5f)' % (ci_fitted[i, 0] * conv_plot)) axHist.axhline(ci_fitted[i, 1] * conv_plot, marker='None', c='forestgreen', ls='-', lw=2.1, alpha=1.0, label='CI 84.135th (%.5f)' % (ci_fitted[i, 1] * conv_plot)) axHist.set_title('Distribution of posterior chain') axHist.legend(loc='center left', fontsize=9, bbox_to_anchor=(1, 0.5)) plt.draw() fig.savefig(emcee_fig_file, bbox_inches='tight', dpi=150) print ' saved' print #fig = plt.figure(figsize=(12,12)) fig = plt.figure(figsize=(6, 6)) # lnprob xlabel = '$N_\mathrm{steps}$' if (cli.use_thin): xlabel = '$N_\mathrm{steps} \\times %d$' % (thin_steps) ax = plt.subplot2grid((2, 1), (0, 0)) ax.plot(lnprob_burnin.T, '-', alpha=0.3) if (overplot is not None): posterior_msun = anc.posterior_back_to_msun(m_factor, parameter_names_emcee, flatchain_posterior_0) post_sel, lnprob_sel = anc.select_within_all_ci( posterior_msun, ci_fitted[:, 0:2], lnprob_burnin.T.reshape(-1)) #lnprob_sel = lnprob_burnin.T.reshape((-1)) lgllhd_med = np.percentile(lnprob_burnin.T.reshape(-1), 50., interpolation='midpoint') abs_dlg = np.abs(lnprob_sel - lgllhd_med) lgllhd_mad = np.percentile(abs_dlg, 50., interpolation='midpoint') #lnp_min = np.min(lnprob_sel) #lnp_max = np.max(lnprob_sel) lnp_min = lgllhd_med - lgllhd_mad lnp_max = lgllhd_med + lgllhd_mad print ' lgllhd_med & mad = ', lgllhd_med, lgllhd_mad print ' lnp_min = ', lnp_min, ' lnp_max = ', lnp_max print ' lnl_668 = ', sample3_lgllhd ax.axhline(lgllhd_med, color='black', ls='-', lw=1.6, alpha=0.77) #if(sample2_lgllhd is not None): #ax.axhline(sample2_lgllhd, marker='None', c='cyan',ls=':', lw=2.7, alpha=0.9) if (sample3_lgllhd is not None): ax.axhline(sample3_lgllhd, marker='None', c='yellow', ls='-', lw=3.1, alpha=0.9) ax.axhspan(lnp_min, lnp_max, color='gray', alpha=0.77) ax.axhline(lnp_min, color='black', ls='--', lw=1.6, alpha=0.77) ax.axhline(lnp_max, color='black', ls='--', lw=1.6, alpha=0.77) min_lnp = np.min(lnprob_burnin.T, axis=0).min() max_lnp = np.max(lnprob_burnin.T, axis=0).max() y_min, y_max = anc.compute_limits(np.asarray([min_lnp, max_lnp]), 0.05) ax.set_ylim((y_min, y_max)) ax.set_ylabel('lnprob') #ax.get_xaxis().set_visible(False) ax.set_xlabel(xlabel) # chi2r chi2r = -2. * (lnprob_burnin.T - ln_err_const) / np.float64(dof) ax = plt.subplot2grid((2, 1), (1, 0)) ax.axhline(1.0, color='gray', ls='-') ax.plot(chi2r, '-', alpha=0.3) if (overplot is not None): c2r_med = -(2. * (lgllhd_med - ln_err_const)) / np.float64(dof) c2r_smax = -(2. * (lnp_min - ln_err_const)) / np.float64(dof) c2r_smin = -(2. * (lnp_max - ln_err_const)) / np.float64(dof) print ' c2r_med = ', c2r_med print ' c2r_smin = ', c2r_smin, ' c2r_smax = ', c2r_smax ax.axhline(c2r_med, color='black', ls='-', lw=1.6, alpha=0.77) ax.axhspan(c2r_smin, c2r_smax, color='gray', alpha=0.77) ax.axhline(c2r_smin, color='black', ls='--', lw=1.6, alpha=0.77) ax.axhline(c2r_smax, color='black', ls='--', lw=1.6, alpha=0.77) #if(sample2_lgllhd is not None): #c2r_sample2 = -2.*(sample2_lgllhd - ln_err_const)/np.float64(dof) #ax.axhline(c2r_sample2, marker='None', c='cyan',ls=':', lw=2.7, alpha=0.9) if (sample3_lgllhd is not None): c2r_sample3 = -2. * (sample3_lgllhd - ln_err_const) / np.float64(dof) ax.axhline(c2r_sample3, marker='None', c='yellow', ls='-', lw=3.1, alpha=0.9) c2r_min = -2. * (y_max - ln_err_const) / np.float64(dof) c2r_max = -2. * (y_min - ln_err_const) / np.float64(dof) ax.set_ylim((c2r_min, c2r_max)) ax.set_ylabel('$\chi^{2}/\mathrm{dof}$') #ax.get_xaxis().set_visible(True) ax.set_xlabel(xlabel) fig.savefig(os.path.join(emcee_plots, 'emcee_lnprobability.png'), bbox_inches='tight', dpi=150) print ' %s saved' % (os.path.join(emcee_plots, 'emcee_lnprobability.png')) return