def compare_plots_one_param_line_hist_cum(list_of_pos_by_name,param,cl,color_by_name,cl_lines_flag=True,analyticCDF=None,legend='auto'): """ Plots a gaussian kernel density estimate for a set of Posteriors onto the same axis. @param list_of_pos: a list of Posterior class instances. @param plot1DParams: a dict; {paramName:Nbins} """ from scipy import seterr as sp_seterr #Create common figure myfig=plt.figure(figsize=(6,4.5),dpi=150) myfig.add_axes([0.15,0.15,0.6,0.76]) list_of_pos=list_of_pos_by_name.values() list_of_pos_names=list_of_pos_by_name.keys() injvals=[] allmins=map(lambda a: np.min(a[param].samples), list_of_pos) allmaxes=map(lambda a: np.max(a[param].samples), list_of_pos) min_pos=np.min(allmins) max_pos=np.max(allmaxes) patch_list=[] max_y=1. posbins=np.linspace(min_pos,max_pos,50) for name,posterior in list_of_pos_by_name.items(): colour=color_by_name[name] #myfig.gca(autoscale_on=True) if posterior[param].injval: injvals.append(posterior[param].injval) try: n,bins=np.histogram(posterior[param].samples,bins=posbins,normed=True,new=True) except: n,bins=np.histogram(posterior[param].samples,bins=posbins,normed=True) if min(bins)==max(bins): print 'Skipping '+param continue (n, bins, patches)=plt.hist(posterior[param].samples,bins=bins,histtype='step',label=name,normed=True,hold=True,color=color_by_name[name],cumulative='True')#range=(min_pos,max_pos) patch_list.append(patches[0]) top_cl_intervals_list={} pos_names=list_of_pos_by_name.keys() for name,posterior in list_of_pos_by_name.items(): #toppoints,injectionconfidence,reses,injection_area,cl_intervals=bppu.greedy_bin_one_param(posterior,{param:greedyBinSizes[param]},[cl]) cl_intervals=posterior[param].prob_interval([cl]) colour=color_by_name[name] if cl_intervals[0] is not None and cl_lines_flag: try: plt.plot([cl_intervals[0][0],cl_intervals[0][0]],[0,max_y],color=colour,linestyle='--') plt.plot([cl_intervals[0][1],cl_intervals[0][1]],[0,max_y],color=colour,linestyle='--') except: print "MAX_Y",max_y,[cl_intervals[0][0],cl_intervals[0][0]],[cl_intervals[0][1],cl_intervals[0][1]] top_cl_intervals_list[name]=(cl_intervals[0][0],cl_intervals[0][1]) if cl_lines_flag: pos_names.append(str(int(cl*100))+'%') patch_list.append(mpl.lines.Line2D(np.array([0.,1.]),np.array([0.,1.]),linestyle='--',color='black')) plt.grid() plt.xlim(min_pos,max_pos) plt.ylim(0,1) if legend: oned_legend=plt.figlegend(patch_list,pos_names,'right') for text in oned_legend.get_texts(): text.set_fontsize('small') plt.xlabel(bppu.plot_label(param)) plt.ylabel('Cumulative Probability') plt.draw() #plt.tight_layout() if injvals: print "Injection parameter is %f"%(float(injvals[0])) injpar=injvals[0] #if min(pos_samps)<injpar and max(pos_samps)>injpar: plt.plot([injpar,injpar],[0,max_y],'r-.',scalex=False,scaley=False,linewidth=4,label='Injection') if analyticCDF is not None: plt.plot(posbins,map(analyticCDF,posbins),'r') return myfig,top_cl_intervals_list#,rkde
def compare_plots_one_param_line_hist(list_of_pos_by_name,param,cl,color_by_name,cl_lines_flag=True,legend='right',analyticPDF=None): """ Plots a gaussian kernel density estimate for a set of Posteriors onto the same axis. @param list_of_pos: a list of Posterior class instances. @param plot1DParams: a dict; {paramName:Nbins} """ from scipy import seterr as sp_seterr #Create common figure myfig=plt.figure(figsize=(6,4.5),dpi=150) #myfig.add_axes([0.1,0.1,0.65,0.85]) #myfig.add_axes([0.15,0.15,0.6,0.76]) axes=plt.Axes(myfig,[0.15,0.15,0.6,0.76]) myfig.add_axes(axes) majorFormatterX=ScalarFormatter(useMathText=True) majorFormatterX.format_data=lambda data:'%.6g'%(data) majorFormatterY=ScalarFormatter(useMathText=True) majorFormatterY.format_data=lambda data:'%.6g'%(data) majorFormatterX.set_scientific(True) majorFormatterY.set_scientific(True) list_of_pos=list_of_pos_by_name.values() list_of_pos_names=list_of_pos_by_name.keys() allmins=map(lambda a: np.min(a[param].samples), list_of_pos) allmaxes=map(lambda a: np.max(a[param].samples), list_of_pos) min_pos=np.min(allmins) max_pos=np.max(allmaxes) injvals=[] patch_list=[] max_y=0 posbins=np.linspace(min_pos,max_pos,50) for name,posterior in list_of_pos_by_name.items(): colour=color_by_name[name] #myfig.gca(autoscale_on=True) if posterior[param].injval: injvals.append(posterior[param].injval) try: n,bins=np.histogram(posterior[param].samples,bins=posbins,normed=True,new=True) except: n,bins=np.histogram(posterior[param].samples,bins=posbins,normed=True) if min(bins)==max(bins): print 'Skipping '+param continue locmaxy=max(n) if locmaxy>max_y: max_y=locmaxy #(n, bins, patches)=plt.hist(posterior[param].samples,bins=bins,facecolor='white',label=name,normed=True,hold=True,color=color_by_name[name])#range=(min_pos,max_pos) (n, bins, patches)=plt.hist(posterior[param].samples,bins=bins,histtype='step',label=name,normed=True,hold=True,color=color_by_name[name]) patch_list.append(patches[0]) Nchars=max(map(lambda d:len(majorFormatterX.format_data(d)),axes.get_xticks())) if Nchars>8: Nticks=3 elif Nchars>5: Nticks=4 elif Nchars>4: Nticks=6 else: Nticks=6 locatorX=mpl.ticker.MaxNLocator(nbins=Nticks) locatorX.view_limits(bins[0],bins[-1]) axes.xaxis.set_major_locator(locatorX) plt.xlim(min_pos,max_pos) top_cl_intervals_list={} pos_names=list_of_pos_by_name.keys() for name,posterior in list_of_pos_by_name.items(): #toppoints,injectionconfidence,reses,injection_area,cl_intervals=bppu.greedy_bin_one_param(posterior,{param:greedyBinSizes[param]},[cl]) cl_intervals=posterior[param].prob_interval([cl]) colour=color_by_name[name] if cl_intervals[0] is not None and cl_lines_flag: try: plt.plot([cl_intervals[0][0],cl_intervals[0][0]],[0,max_y],color=colour,linestyle='--') plt.plot([cl_intervals[0][1],cl_intervals[0][1]],[0,max_y],color=colour,linestyle='--') except: print "MAX_Y",max_y,[cl_intervals[0][0],cl_intervals[0][0]],[cl_intervals[0][1],cl_intervals[0][1]] top_cl_intervals_list[name]=(cl_intervals[0][0],cl_intervals[0][1]) if cl_lines_flag: pos_names.append(str(int(cl*100))+'%') patch_list.append(mpl.lines.Line2D(np.array([0.,1.]),np.array([0.,1.]),linestyle='--',color='black')) plt.grid() plt.xlim(min_pos,max_pos) if legend is not None: oned_legend=plt.figlegend(patch_list,pos_names,'right') for text in oned_legend.get_texts(): text.set_fontsize('small') plt.xlabel(bppu.plot_label(param)) plt.ylabel('Probability density') plt.draw() #plt.tight_layout() if injvals: print "Injection parameter is %f"%(float(injvals[0])) injpar=injvals[0] #if min(pos_samps)<injpar and max(pos_samps)>injpar: plt.plot([injpar,injpar],[0,max_y],'r-.',scalex=False,scaley=False,linewidth=4,label='Injection') # if analyticPDF is not None: plt.plot(posbins,map(analyticPDF,posbins),'r') return myfig,top_cl_intervals_list#,rkde
def compare_plots_one_param_pdf(list_of_pos_by_name,param,analyicPDF=None): """ Plots a gaussian kernel density estimate for a set of Posteriors onto the same axis. @param list_of_pos: a list of Posterior class instances. @param plot1DParams: a dict; {paramName:Nbins} """ from scipy import seterr as sp_seterr #Create common figure myfig=plt.figure(figsize=(6,4.5),dpi=150) list_of_pos=list_of_pos_by_name.values() list_of_pos_names=list_of_pos_by_name.keys() allmins=map(lambda a: np.min(a[param].samples), list_of_pos) allmaxes=map(lambda a: np.max(a[param].samples), list_of_pos) min_pos=np.min(allmins) max_pos=np.max(allmaxes) print 'Found global min: %f, max: %f'%(min_pos,max_pos) gkdes={} injvals=[] for name,posterior in list_of_pos_by_name.items(): pos_samps=posterior[param].samples if posterior[param].injval is not None: injvals.append(posterior[param].injval) min_pos_temp=np.min(pos_samps) max_pos_temp=np.max(pos_samps) if min_pos_temp<min_pos: min_pos=min_pos_temp if max_pos_temp>max_pos: max_pos=max_pos_temp injpar=posterior[param].injval gkdes[name]=posterior[param].gaussian_kde if gkdes: ind=np.linspace(min_pos,max_pos,101) kdepdfs=[] for name,gkde in gkdes.items(): kdepdf=gkde.evaluate(ind) kdepdfs.append(kdepdf) plt.plot(ind,np.transpose(kdepdf),label=name) plt.grid() plt.legend() plt.xlabel(bppu.plot_label(param)) plt.xlim(min_pos,max_pos) plt.ylabel('Probability Density') try: plt.tight_layout() except: pass if injvals: print "Injection parameter is %f"%(float(injvals[0])) injpar=injvals[0] if min(pos_samps)<injpar and max(pos_samps)>injpar: plt.plot([injpar,injpar],[0,max(kdepdf)],'r-.',scalex=False,scaley=False) if analyticPDF is not None: plt.plot(ind,map(analyticPDF,ind),'r') # return myfig#,rkde
def compare_plots_one_param_line_hist_cum(list_of_pos_by_name,param,cl,color_by_name,cl_lines_flag=True,analyticCDF=None,legend='auto'): """ Plots a gaussian kernel density estimate for a set of Posteriors onto the same axis. @param list_of_pos: a list of Posterior class instances. @param plot1DParams: a dict; {paramName:Nbins} """ from scipy import seterr as sp_seterr #Create common figure myfig=plt.figure(figsize=(6,4.5),dpi=150) myfig.add_axes([0.15,0.15,0.6,0.76]) list_of_pos=list_of_pos_by_name.values() list_of_pos_names=list_of_pos_by_name.keys() injvals=[] allmins=map(lambda a: np.min(a[param].samples), list_of_pos) allmaxes=map(lambda a: np.max(a[param].samples), list_of_pos) min_pos=np.min(allmins) max_pos=np.max(allmaxes) patch_list=[] max_y=1. posbins=np.linspace(min_pos,max_pos,50) for name,posterior in list_of_pos_by_name.items(): colour=color_by_name[name] #myfig.gca(autoscale_on=True) if posterior[param].injval: injvals.append(posterior[param].injval) try: n,bins=np.histogram(posterior[param].samples,bins=posbins,normed=True,new=True) except: n,bins=np.histogram(posterior[param].samples,bins=posbins,normed=True) (n, bins, patches)=plt.hist(posterior[param].samples,bins=bins,histtype='step',label=name,normed=True,hold=True,color=color_by_name[name],cumulative='True')#range=(min_pos,max_pos) patch_list.append(patches[0]) top_cl_intervals_list={} pos_names=list_of_pos_by_name.keys() for name,posterior in list_of_pos_by_name.items(): #toppoints,injectionconfidence,reses,injection_area,cl_intervals=bppu.greedy_bin_one_param(posterior,{param:greedyBinSizes[param]},[cl]) cl_intervals=posterior[param].prob_interval([cl]) colour=color_by_name[name] if cl_intervals[0] is not None and cl_lines_flag: try: plt.plot([cl_intervals[0][0],cl_intervals[0][0]],[0,max_y],color=colour,linestyle='--') plt.plot([cl_intervals[0][1],cl_intervals[0][1]],[0,max_y],color=colour,linestyle='--') except: print "MAX_Y",max_y,[cl_intervals[0][0],cl_intervals[0][0]],[cl_intervals[0][1],cl_intervals[0][1]] top_cl_intervals_list[name]=(cl_intervals[0][0],cl_intervals[0][1]) if cl_lines_flag: pos_names.append(str(int(cl*100))+'%') patch_list.append(mpl.lines.Line2D(np.array([0.,1.]),np.array([0.,1.]),linestyle='--',color='black')) plt.grid() plt.xlim(min_pos,max_pos) plt.ylim(0,1) if legend: oned_legend=plt.figlegend(patch_list,pos_names,'right') for text in oned_legend.get_texts(): text.set_fontsize('small') plt.xlabel(bppu.plot_label(param)) plt.ylabel('Cumulative Probability') plt.draw() #plt.tight_layout() if injvals: print "Injection parameter is %f"%(float(injvals[0])) injpar=injvals[0] #if min(pos_samps)<injpar and max(pos_samps)>injpar: plt.plot([injpar,injpar],[0,max_y],'r-.',scalex=False,scaley=False,linewidth=4,label='Injection') if analyticCDF is not None: plt.plot(posbins,map(analyticCDF,posbins),'r') return myfig,top_cl_intervals_list#,rkde
def compare_plots_one_param_line_hist(list_of_pos_by_name,param,cl,color_by_name,cl_lines_flag=True,legend='right',analyticPDF=None): """ Plots a gaussian kernel density estimate for a set of Posteriors onto the same axis. @param list_of_pos: a list of Posterior class instances. @param plot1DParams: a dict; {paramName:Nbins} """ from scipy import seterr as sp_seterr #Create common figure myfig=plt.figure(figsize=(6,4.5),dpi=150) #myfig.add_axes([0.1,0.1,0.65,0.85]) #myfig.add_axes([0.15,0.15,0.6,0.76]) axes=plt.Axes(myfig,[0.15,0.15,0.6,0.76]) myfig.add_axes(axes) majorFormatterX=ScalarFormatter(useMathText=True) majorFormatterX.format_data=lambda data:'%.6g'%(data) majorFormatterY=ScalarFormatter(useMathText=True) majorFormatterY.format_data=lambda data:'%.6g'%(data) majorFormatterX.set_scientific(True) majorFormatterY.set_scientific(True) list_of_pos=list_of_pos_by_name.values() list_of_pos_names=list_of_pos_by_name.keys() allmins=map(lambda a: np.min(a[param].samples), list_of_pos) allmaxes=map(lambda a: np.max(a[param].samples), list_of_pos) min_pos=np.min(allmins) max_pos=np.max(allmaxes) injvals=[] patch_list=[] max_y=0 posbins=np.linspace(min_pos,max_pos,50) for name,posterior in list_of_pos_by_name.items(): colour=color_by_name[name] #myfig.gca(autoscale_on=True) if posterior[param].injval: injvals.append(posterior[param].injval) try: n,bins=np.histogram(posterior[param].samples,bins=posbins,normed=True,new=True) except: n,bins=np.histogram(posterior[param].samples,bins=posbins,normed=True) locmaxy=max(n) if locmaxy>max_y: max_y=locmaxy #(n, bins, patches)=plt.hist(posterior[param].samples,bins=bins,facecolor='white',label=name,normed=True,hold=True,color=color_by_name[name])#range=(min_pos,max_pos) (n, bins, patches)=plt.hist(posterior[param].samples,bins=bins,histtype='step',label=name,normed=True,hold=True,color=color_by_name[name]) patch_list.append(patches[0]) Nchars=max(map(lambda d:len(majorFormatterX.format_data(d)),axes.get_xticks())) if Nchars>8: Nticks=3 elif Nchars>5: Nticks=4 elif Nchars>4: Nticks=6 else: Nticks=6 locatorX=mpl.ticker.MaxNLocator(nbins=Nticks) locatorX.view_limits(bins[0],bins[-1]) axes.xaxis.set_major_locator(locatorX) plt.xlim(min_pos,max_pos) top_cl_intervals_list={} pos_names=list_of_pos_by_name.keys() for name,posterior in list_of_pos_by_name.items(): #toppoints,injectionconfidence,reses,injection_area,cl_intervals=bppu.greedy_bin_one_param(posterior,{param:greedyBinSizes[param]},[cl]) cl_intervals=posterior[param].prob_interval([cl]) colour=color_by_name[name] if cl_intervals[0] is not None and cl_lines_flag: try: plt.plot([cl_intervals[0][0],cl_intervals[0][0]],[0,max_y],color=colour,linestyle='--') plt.plot([cl_intervals[0][1],cl_intervals[0][1]],[0,max_y],color=colour,linestyle='--') except: print "MAX_Y",max_y,[cl_intervals[0][0],cl_intervals[0][0]],[cl_intervals[0][1],cl_intervals[0][1]] top_cl_intervals_list[name]=(cl_intervals[0][0],cl_intervals[0][1]) if cl_lines_flag: pos_names.append(str(int(cl*100))+'%') patch_list.append(mpl.lines.Line2D(np.array([0.,1.]),np.array([0.,1.]),linestyle='--',color='black')) plt.grid() plt.xlim(min_pos,max_pos) if legend is not None: oned_legend=plt.figlegend(patch_list,pos_names,'right') for text in oned_legend.get_texts(): text.set_fontsize('small') plt.xlabel(bppu.plot_label(param)) plt.ylabel('Probability density') plt.draw() #plt.tight_layout() if injvals: print "Injection parameter is %f"%(float(injvals[0])) injpar=injvals[0] #if min(pos_samps)<injpar and max(pos_samps)>injpar: plt.plot([injpar,injpar],[0,max_y],'r-.',scalex=False,scaley=False,linewidth=4,label='Injection') # if analyticPDF is not None: plt.plot(posbins,map(analyticPDF,posbins),'r') return myfig,top_cl_intervals_list#,rkde