25from time
import strftime
31from numpy
import floor
33import scipy.stats
as ss
35import matplotlib
as mpl
37from matplotlib
import pyplot
as plt
38from matplotlib
import colors
as mpl_colors
39from matplotlib
import cm
as mpl_cm
44from lalinference
import git_version
46__author__=
"Ben Aylott <benjamin.aylott@ligo.org>, Will M. Farr <will.farr@ligo.org>"
47__version__=
"git id %s"%git_version.id
48__date__= git_version.date
51oneDMenu=[
'mtotal',
'm1',
'm2',
'mchirp',
'mc',
'chirpmass',
'distance',
'distMPC',
'dist',
'iota',
'psi',
'eta',
'q',
'asym_massratio',
'spin1',
'spin2',
'a1',
'a2',
'phi1',
'theta1',
'phi2',
'theta2',
'costilt1',
'costilt2',
'costhetas',
'cosbeta',
'phi_orb',
'lambdat',
'dlambdat',
'lambda1',
'lambda2',
'lam_tilde',
'dlam_tilde',
'theta_jn',
'a1z',
'a2z'] + bppu.spininducedquadParams + bppu.snrParams + bppu.spinParams + bppu.cosmoParam + bppu.calParams + bppu.tigerParams + bppu.lorentzInvarianceViolationParams + bppu.qnmtestParams
53twoDGreedyMenu=[[
'mc',
'eta'],[
'mchirp',
'eta'],[
'chirpmass',
'eta'],[
'mc',
'q'],[
'mchirp',
'q'],[
'chirpmass',
'q'],[
'mc',
'asym_massratio'],[
'mchirp',
'asym_massratio'],[
'chirpmass',
'asym_massratio'],[
'm1',
'm2'],[
'mtotal',
'eta'],[
'distance',
'iota'],[
'dist',
'iota'],[
'dist',
'm1'],[
'ra',
'dec'],[
'dist',
'cos(iota)'],[
'phi_orb',
'iota'],[
'theta_jn',
'dist'],[
'spin1',
'spin2'],[
'spin1',
'mchirp'],[
'spin1',
'm1'],[
'a1',
'a2'],[
'a1',
'mchirp'],[
'a1',
'm1'],[
'tilt1',
'tilt2'],[
'tilt1',
'mchirp'],[
'tilt1',
'm1'],[
'a1z',
'a2z']]
57paramNameLatexMap = {
'm1':
'm_1',
'm2' :
'm_2',
'mtotal' :
r'M_{\rm tot}',
'mchirp' :
r'\mathcal{M}',
58 'mc':
r'\mathcal{M}',
'distance' :
'd',
'distMPC' :
'd',
'dist':
'd',
59 'iota':
r'\iota',
'psi':
r'\psi',
'eta':
r'\eta',
'asym_massratio':
'q',
'a1':
'a_1',
60 'a2':
'a_2',
'phi1':
r'\phi_1',
'phi2':
r'\phi_2',
'theta1':
r'\theta_1',
'theta2':
r'\theta_2',
61 'cos(tilt1)':
r'\cos t_1',
'cos(tilt2)':
r'\cos t_2',
'cos(thetas)':
r'\cos \theta_s',
62 'cosbeta':
r'\cos \beta',
'phi_orb':
r'\phi_{\rm orb}',
'cos(beta)':
r'\cos \beta',
63 'cos(iota)':
r'\cos \iota',
'tilt1':
r't_1',
'tilt2':
r't_2',
'ra':
r'\alpha',
'dec':
r'\delta',
64 'lambdat' :
r'\tilde{\Lambda}',
'dlambdat':
r'\delta \tilde{\Lambda}',
65 'lambda1' :
r'\lambda_1',
'lambda2':
r'\lambda_2',
66 'lam_tilde' :
r'\tilde{\Lambda}',
'dlam_tilde':
r'\delta \tilde{\Lambda}',
'dchiMinus2':
r'$d\chi_{-2}$',
'dchiMinus1':
r'$d\chi_{-1}$',
'dchi0':
r'\delta\chi_0',
'dchi1':
r'\delta\chi_1',
'dchi2':
r'\delta\chi_2',
'dchi3':
r'\delta\chi_3',
'dchi3S':
r'\delta\chi_{3S}',
'dchi3NS':
r'\delta\chi_{3NS}',
'dchi4':
r'\delta\chi_4',
'dchi4S':
r'\delta\chi_{4S}',
'dchi4NS':
r'\delta\chi_{4NS}',
'dchi5':
r'\delta\chi_5',
'dchi5S':
r'\delta\chi_{5S}',
'dchi5NS':
r'\delta\chi_{5NS}',
'dchi5l':
r'\delta\chi_{5l}',
'dchi5lS':
r'\delta\chi_{5lS}',
'dchi5lNS':
r'\delta\chi_{5lNS}',
'dchi6':
r'\delta\chi_6',
'dchi6S':
r'\delta\chi_{6S}',
'dchi6NS':
r'\delta\chi_{6NS}',
'dchi6l':
r'\delta\chi_{6l}',
'dchi7':
r'\delta\chi_7',
'dchi7S':
r'\delta\chi_{7S}',
'dchi7NS':
r'\delta\chi_{7NS}',
'dbeta2':
r'\delta\beta_2',
'dbeta3':
r'\delta\beta_3',
'dsigma2':
r'\delta\sigma_2',
'dsigma3':
r'\delta\sigma_3',
'dsigma4':
r'\delta\sigma_4',
'dbeta2':
r'\delta\beta_2',
'dbeta3':
r'\delta\beta_3' ,
'log10lambda_a':
r'$\log\lambda_{\mathbb{A}}$',
'log10lambda_eff':
r'$\log\lambda_{eff}$',
'log10livamp':
r'$\log \mathbb{A}$',
'lambda_a':
r'$\lambda_{\mathbb{A}}$',
'lambda_eff':
r'$\lambda_{eff}$',
'liv_amp':
r'$\mathbb{A}$',
'dquadmons':
r'\delta\kappa_s',
'dchikappaS':
r'\delta\chi_{kappa_{S}}',
'dchikappaA':
r'\delta\chi_{\kappa_{A}}',
'damp21':
r'$\delta A_{21}$',
'damp33':
r'$\delta A_{33}$',
67'domega220':
r'$d\omega_{220}$',
'dtau220':
r'$d\tau_{220}$',
68 'domega210':
r'$d\omega_{210}$',
'dtau210':
r'$d\tau_{210}$',
69 'domega330':
r'$d\omega_{330}$',
'dtau330':
r'$d\tau_{330}$',
70 'domega440':
r'$d\omega_{440}$',
'dtau440':
r'$d\tau_{440}$',
71 'domega550':
r'$d\omega_{550}$',
'dtau550':
r'$d\tau_{550}$',
72 'dc1':
r'$\delta c_1$',
'dc2':
r'$\delta c_2$',
'dc4':
r'$\delta c_4$',
'dcl':
r'$\delta c_l$',
'db1':
r'$\delta b_1$',
'db2':
r'$\delta b_2$',
'db3':
r'$\delta b_3$',
'db4':
r'$\delta b_4$'
76clTableParams = [
'mchirp',
'mc',
'chirpmass',
'eta',
'q',
'm1',
'm2',
'distance',
'distMPC',
'dist',
'cos(iota)',
'iota',
'theta_jn',
'psi',
'ra',
'dec',
'time',
'phase',
'a1',
'a2',
'costilt1',
'costilt2',
'dchiMinus2',
'dchiMinus1',
'dchi0',
'dchi1',
'dchi2',
'dchi3',
'dchi3S',
'dchi3NS',
'dchi4',
'dchi4S',
'dchi4NS',
'dchi5',
'dchi5S',
'dchi5NS',
'dchi5l',
'dchi5lS',
'dchi5lNS',
'dchi6',
'dchi6S',
'dchi6NS',
'dchi6l',
'dchi7',
'dchi7S',
'dchi7NS',
'dbeta2',
'dbeta3',
'dsigma2',
'dsigma3',
'dsigma4',
'dbeta2',
'dbeta3',
'log10lambda_eff',
'log10lambda_a',
'log10livamp',
'lambda_eff',
'lambda_a',
'liv_amp',
'dquadmons',
'dchikappaS',
'dchikappaA',
'domega220',
'dtau220',
'domega210',
'dtau210',
'domega330',
'dtau330',
'domega440',
'dtau440',
'domega550',
'dtau550',
'db1',
'db2',
'db3',
'db4',
'dc1',
'dc2',
'dc4',
'dcl',
'damp21',
'damp33']
79greedyBinSizes={
'mc':0.001,
'm1':0.1,
'm2':0.1,
'mass1':0.1,
'mass2':0.1,
'mtotal':0.1,
'eta':0.001,
'q':0.001,
'asym_massratio':0.001,
'iota':0.05,
'time':1e-4,
'distance':5.0,
'dist':1.0,
'mchirp':0.01,
'chirpmass':0.01,
'a1':0.02,
'a2':0.02,
'phi1':0.05,
'phi2':0.05,
'theta1':0.05,
'theta2':0.05,
'ra':0.05,
'dec':0.005,
'psi':0.1,
'cos(iota)':0.01,
'cos(tilt1)':0.01,
'cos(tilt2)':0.01,
'tilt1':0.05,
'tilt2':0.05,
'cos(thetas)':0.01,
'cos(beta)':0.01,
'phi_orb':0.2,
'inclination':0.05,
'theta_jn':0.05,
'spin1':0.02,
'spin2':0.02}
80for s
in bppu.snrParams:
81 greedyBinSizes[s]=0.02
82for s
in bppu.calParams:
83 greedyBinSizes[s]=0.02
84for s
in bppu.tigerParams + bppu.lorentzInvarianceViolationParams + bppu.qnmtestParams:
85 greedyBinSizes[s]=0.01
86for s
in bppu.spinParams:
87 greedyBinSizes[s]=0.02
88for s
in bppu.cosmoParam:
90greedyBinSizes[
'redshift']=0.005
93OneDconfidenceLevels=[0.9]
94TwoDconfidenceLevels=OneDconfidenceLevels
98twoDplots=[[
'm1',
'm2'],[
'mass1',
'mass2'],[
'RA',
'dec'],[
'ra',
'dec'],[
'cos(thetas)',
'cos(beta)'],[
'distance',
'iota'],[
'dist',
'iota'],[
'dist',
'cosiota'],[
'distance',
'cosiota'],[
'psi',
'iota'],[
'psi',
'distance'],[
'psi',
'phi0'],[
'dist',
'cos(iota)'],[
'phi_orb',
'iota'],[
'distance',
'inclination'],[
'dist',
'inclination'],[
'theta_jn',
'dist'],[
'spin1',
'spin2'],[
'spin1',
'mchirp'],[
'spin1',
'm1'],[
'a1',
'a2'],[
'a1',
'mchirp'],[
'a1',
'm1'],[
'tilt1',
'tilt2'],[
'tilt1',
'mchirp'],[
'tilt1',
'm1']]
99allowed_params=[
'mtotal',
'm1',
'm2',
'mchirp',
'mc',
'chirpmass',
'q',
'asym_massratio',
'distance',
'distMPC',
'dist',
'iota',
'psi',
'eta',
'ra',
'dec',
'a1',
'a2',
'spin1',
'spin2',
'phi1',
'theta1',
'phi2',
'theta2',
'cos(iota)',
'cos(tilt1)',
'cos(tilt2)',
'tilt1',
'tilt2',
'cos(thetas)',
'cos(beta)',
'phi_orb',
'inclination',
'logl',
'lambdat',
'dlambdat',
'lambda1',
'lambda2',
'lam_tilde',
'dlam_tilde',
'theta_jn',
'a1z',
'a2z',
'dquadmons',
'dquadmona',
'dquadmon1',
'dquadmon2']+bppu.snrParams + bppu.spinParams + bppu.cosmoParam + bppu.calParams + bppu.tigerParams
107 parsed_url=urlparse.urlparse(url)
108 url=urlparse.urljoin(parsed_url.geturl(),
'posterior_samples.dat')
111 opener = urllib2.build_opener(urllib2.HTTPCookieProcessor())
112 urllib2.install_opener(opener)
114 body={
'username':username,
'password':password}
115 txdata = urllib.urlencode(body)
116 txheaders = {
'User-agent' :
'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'}
118 req = urllib2.Request(parsed_url[0]+
'://'+parsed_url[1], txdata, txheaders)
120 resp = opener.open(req)
124 req = urllib2.Request(url, txdata, txheaders)
125 handle = opener.open(req)
127 f =
file(
'posterior_samples.dat',
'w')
132 print(
'We failed to open "%s".' % url)
133 if hasattr(e,
'code'):
134 print(
'We failed with error code - %s.' % e.code)
135 elif hasattr(e,
'reason'):
136 print(
"The error object has the following 'reason' attribute :", e.reason)
137 print(
"This usually means the server doesn't exist, is down, or we don't have an internet connection.")
140 print(
'Here are the headers of the page :')
148 for j
in L:
yield i, j
153 kerberos_args =
"--insecure -c /tmp/{0}_cookies -b /tmp/{0}_cookies --negotiate --user : --location-trusted".format(getpass.getuser()).split()
155 retcode=subprocess.call([
'curl'] + kerberos_args + [url] + args)
161 idx=common_output_table_header.index(test)
162 common_output_table_header[idx]=switch
163 print(
"Re-labelled %s -> %s"%(test,switch))
171 Plots a gaussian kernel density estimate for a set
172 of Posteriors onto the same axis.
174 @param list_of_pos_by_name: a dictionary of {name:Posterior}
class instances.
176 @param param: parameter name to compare
178 @param analyticPDF: Optional function to over-plot
183 myfig=plt.figure(figsize=(6,4.5),dpi=150)
185 list_of_pos=list(list_of_pos_by_name.values())
186 list_of_pos_names=list(list_of_pos_by_name.keys())
188 allmins=map(
lambda a: np.min(a[param].samples), list_of_pos)
189 allmaxes=map(
lambda a: np.max(a[param].samples), list_of_pos)
190 min_pos=np.min(allmins)
191 max_pos=np.max(allmaxes)
192 print(
'Found global min: %f, max: %f'%(min_pos,max_pos))
196 for name,posterior
in list_of_pos_by_name.items():
198 pos_samps=posterior[param].samples
199 if posterior[param].injval
is not None:
200 injvals.append(posterior[param].injval)
202 min_pos_temp=np.min(pos_samps)
203 max_pos_temp=np.max(pos_samps)
205 if min_pos_temp<min_pos:
207 if max_pos_temp>max_pos:
210 injpar=posterior[param].injval
212 gkdes[name]=posterior[param].gaussian_kde
215 ind=np.linspace(min_pos,max_pos,101)
218 for name,gkde
in gkdes.items():
219 kdepdf=gkde.evaluate(ind)
220 kdepdfs.append(kdepdf)
221 plt.plot(ind,np.transpose(kdepdf),label=name)
224 plt.xlabel(bppu.plot_label(param))
225 plt.xlim(min_pos,max_pos)
226 plt.ylabel(
'Probability Density')
232 print(
"Injection parameter is %f"%(float(injvals[0])))
234 if min(pos_samps)<injpar
and max(pos_samps)>injpar:
235 plt.plot([injpar,injpar],[0,
max(kdepdf)],
'r-.',scalex=
False,scaley=
False)
236 if analyticPDF
is not None:
237 plt.plot(ind,map(analyticPDF,ind),
'r')
245 Plots a gaussian kernel density estimate for a set
246 of Posteriors onto the same axis.
248 @param list_of_pos_by_name: a dict of Posterior
class instances indexed by name
250 @param param: parameter name to plot
252 @param cl: list of credible levels to plot
254 @param color_by_name: dict of colours indexed by name
256 @param cl_lines_flag: option to plot credible level lines
258 @param legend: matplotlib position
for legend
260 @param analyticPDF: Optional analytic function to over-plot
265 myfig=plt.figure(figsize=(6,4.5),dpi=150)
268 axes=plt.Axes(myfig,[0.15,0.15,0.6,0.76])
270 majorFormatterX=ScalarFormatter(useMathText=
True)
271 majorFormatterX.format_data=
lambda data:
'%.6g'%(data)
272 majorFormatterY=ScalarFormatter(useMathText=
True)
273 majorFormatterY.format_data=
lambda data:
'%.6g'%(data)
274 majorFormatterX.set_scientific(
True)
275 majorFormatterY.set_scientific(
True)
277 list_of_pos=list(list_of_pos_by_name.values())
278 list_of_pos_names=list(list_of_pos_by_name.keys())
280 allmins=map(
lambda a: np.min(a[param].samples), list_of_pos)
281 allmaxes=map(
lambda a: np.max(a[param].samples), list_of_pos)
282 min_pos=np.min(allmins)
283 max_pos=np.max(allmaxes)
290 posbins=np.linspace(min_pos,max_pos,50)
292 for name,posterior
in list_of_pos_by_name.items():
293 colour=color_by_name[name]
295 if posterior[param].injval:
296 injvals.append(posterior[param].injval)
299 n,bins=np.histogram(posterior[param].samples,bins=posbins,density=
True,new=
True)
301 n,bins=np.histogram(posterior[param].samples,bins=posbins,density=
True)
302 if min(bins)==
max(bins):
303 print(
'Skipping '+param)
306 if locmaxy>max_y: max_y=locmaxy
308 (n, bins, patches)=plt.hist(posterior[param].samples,bins=bins,histtype=
'step',label=name,density=
True,hold=
True,color=color_by_name[name])
309 patch_list.append(patches[0])
311 Nchars=
max(map(
lambda d:len(majorFormatterX.format_data(d)),axes.get_xticks()))
320 locatorX=mpl.ticker.MaxNLocator(nbins=Nticks)
321 locatorX.view_limits(bins[0],bins[-1])
322 axes.xaxis.set_major_locator(locatorX)
324 plt.xlim(min_pos,max_pos)
325 top_cl_intervals_list={}
326 pos_names=list(list_of_pos_by_name.keys())
329 for name,posterior
in list_of_pos_by_name.items():
331 cl_intervals=posterior[param].prob_interval([cl])
332 colour=color_by_name[name]
333 if cl_intervals[0]
is not None and cl_lines_flag:
335 plt.plot([cl_intervals[0][0],cl_intervals[0][0]],[0,max_y],color=colour,linestyle=
'--')
336 plt.plot([cl_intervals[0][1],cl_intervals[0][1]],[0,max_y],color=colour,linestyle=
'--')
338 print(
"MAX_Y",max_y,[cl_intervals[0][0],cl_intervals[0][0]],[cl_intervals[0][1],cl_intervals[0][1]])
339 top_cl_intervals_list[name]=(cl_intervals[0][0],cl_intervals[0][1])
342 pos_names.append(
str(
int(cl*100))+
'%')
343 patch_list.append(mpl.lines.Line2D(np.array([0.,1.]),np.array([0.,1.]),linestyle=
'--',color=
'black'))
346 plt.xlim(min_pos,max_pos)
347 if legend
is not None:
348 oned_legend=plt.figlegend(patch_list,pos_names,
'right')
349 for text
in oned_legend.get_texts():
350 text.set_fontsize(
'small')
351 plt.xlabel(bppu.plot_label(param))
352 plt.ylabel(
'Probability density')
356 print(
"Injection parameter is %f"%(float(injvals[0])))
359 plt.plot([injpar,injpar],[0,max_y],
'r-.',scalex=
False,scaley=
False,linewidth=4,label=
'Injection')
362 if analyticPDF
is not None:
363 plt.plot(posbins,map(analyticPDF,posbins),
'r')
364 return myfig,top_cl_intervals_list
368 """Returns a matrix of ks p-value tests between pairs of
369 posteriors on the 1D marginalized distributions for param.
"""
371 poss=list_of_pos_by_name.values()
375 matrix=np.zeros((N,N))
376 matrix[:,:]=float('nan')
380 for j
in range(i+1, N):
383 d,pvalue=ss.ks_2samp(pi[param].samples.flatten(), pj[param].samples.flatten())
393 Plots a gaussian kernel density estimate for a set
394 of Posteriors onto the same axis.
396 @param list_of_pos_by_name: a dict of Posterior
class instances indexed by name
398 @param param: name of parameter to plot
400 @param cl: list of confidence levels to show
402 @param color_by_name: dict of colours indexed by name
404 @param cl_lines_flag: Show confidence level lines
406 @param analyticCDF: Analytic CDF function to over-plot
408 @param legend: legend position to
pass to matplotlib
413 myfig=plt.figure(figsize=(6,4.5),dpi=150)
414 myfig.add_axes([0.15,0.15,0.6,0.76])
415 list_of_pos=list(list_of_pos_by_name.values())
416 list_of_pos_names=list(list_of_pos_by_name.keys())
419 allmins=map(
lambda a: np.min(a[param].samples), list_of_pos)
420 allmaxes=map(
lambda a: np.max(a[param].samples), list_of_pos)
421 min_pos=np.min(allmins)
422 max_pos=np.max(allmaxes)
427 posbins=np.linspace(min_pos,max_pos,50)
429 for name,posterior
in list_of_pos_by_name.items():
430 colour=color_by_name[name]
432 if posterior[param].injval:
433 injvals.append(posterior[param].injval)
436 n,bins=np.histogram(posterior[param].samples,bins=posbins,density=
True,new=
True)
438 n,bins=np.histogram(posterior[param].samples,bins=posbins,density=
True)
440 if min(bins)==
max(bins):
441 print(
'Skipping '+param)
444 (n, bins, patches)=plt.hist(posterior[param].samples,bins=bins,histtype=
'step',label=name,density=
True,hold=
True,color=color_by_name[name],cumulative=
'True')
446 patch_list.append(patches[0])
448 top_cl_intervals_list={}
449 pos_names=list(list_of_pos_by_name.keys())
452 for name,posterior
in list_of_pos_by_name.items():
454 cl_intervals=posterior[param].prob_interval([cl])
455 colour=color_by_name[name]
456 if cl_intervals[0]
is not None and cl_lines_flag:
458 plt.plot([cl_intervals[0][0],cl_intervals[0][0]],[0,max_y],color=colour,linestyle=
'--')
459 plt.plot([cl_intervals[0][1],cl_intervals[0][1]],[0,max_y],color=colour,linestyle=
'--')
461 print(
"MAX_Y",max_y,[cl_intervals[0][0],cl_intervals[0][0]],[cl_intervals[0][1],cl_intervals[0][1]])
462 top_cl_intervals_list[name]=(cl_intervals[0][0],cl_intervals[0][1])
465 pos_names.append(
str(
int(cl*100))+
'%')
466 patch_list.append(mpl.lines.Line2D(np.array([0.,1.]),np.array([0.,1.]),linestyle=
'--',color=
'black'))
469 plt.xlim(min_pos,max_pos)
472 oned_legend=plt.figlegend(patch_list,pos_names,
'right')
473 for text
in oned_legend.get_texts():
474 text.set_fontsize(
'small')
475 plt.xlabel(bppu.plot_label(param))
476 plt.ylabel(
'Cumulative Probability')
480 print(
"Injection parameter is %f"%(float(injvals[0])))
483 plt.plot([injpar,injpar],[0,max_y],
'r-.',scalex=
False,scaley=
False,linewidth=4,label=
'Injection')
484 if analyticCDF
is not None:
485 plt.plot(posbins,map(analyticCDF,posbins),
'r')
486 return myfig,top_cl_intervals_list
489def compare_bayes(outdir,names_and_pos_folders,injection_path,eventnum,username,password,reload_flag,clf,ldg_flag,contour_figsize=(4.5,4.5),contour_dpi=250,contour_figposition=[0.15,0.15,0.5,0.75],fail_on_file_err=
True,covarianceMatrices=
None,meanVectors=
None,Npixels2D=50):
493 if injection_path
is not None and os.path.exists(injection_path)
and eventnum
is not None:
494 eventnum=
int(eventnum)
495 from igwn_ligolw
import ligolw, lsctables, utils
496 injections = lsctables.SimInspiralTable.get_table(
497 utils.load_filename(injection_path))
498 if eventnum
is not None:
499 if(len(injections)<eventnum):
500 print(
"Error: You asked for event %d, but %s contains only %d injections" %(eventnum,injection_path,len(injections)))
503 injection=injections[eventnum]
506 analyticLikelihood =
None
507 if covarianceMatrices
and meanVectors:
508 analyticLikelihood = bppu.AnalyticLikelihood(covarianceMatrices, meanVectors)
509 peparser=bppu.PEOutputParser(
'common')
513 working_folder=os.getcwd()
514 for name,pos_folder
in names_and_pos_folders:
517 pos_folder_url=urlparse.urlparse(pos_folder)
518 pfu_scheme,pfu_netloc,pfu_path,pfu_params,pfu_query,pfu_fragment=pos_folder_url
520 if 'http' in pfu_scheme:
523 Retrieve a file over http(s).
525 downloads_folder=os.path.join(os.getcwd(),"downloads")
526 pos_folder_parse=urlparse.urlparse(pos_folder)
527 pfp_scheme,pfp_netloc,pfp_path,pfp_params,pfp_query,pfp_fragment=pos_folder_parse
528 head,tail=os.path.split(pfp_path)
529 if tail
is 'posplots.html' or tail:
532 pos_file_part=pfp_path
533 pos_file_url=urlparse.urlunsplit((pfp_scheme,pfp_netloc,os.path.join(pos_file_part,
'posterior_samples.dat'),
'',
''))
535 pos_file=os.path.join(os.getcwd(),downloads_folder,
"%s.dat"%name)
537 if not os.path.exists(pos_file):
541 if os.path.exists(pos_file):
543 if not os.path.exists(downloads_folder):
544 os.makedirs(downloads_folder)
547 elif pfu_scheme
is '' or pfu_scheme
is 'file':
548 pos_file=os.path.join(pos_folder,
'%s.dat'%name)
550 if not os.path.exists(pos_file):
551 print(
'%s does not exist, trying posterior_samples.dat'%(pos_file))
552 pos_file=os.path.join(pos_folder,
'posterior_samples.dat')
554 print(
"Unknown scheme for input data url: %s\nFull URL: %s"%(pfu_scheme,
str(pos_folder_url)))
557 print(
"Reading posterior samples from %s ..."%pos_file)
560 common_output_table_header,common_output_table_raw=peparser.parse(open(pos_file,
'r'))
562 print(
'Unable to read file '+pos_file)
576 if 'LI_MCMC' in name
or 'FU_MCMC' in name:
580 idx=common_output_table_header.index(
'iota')
581 print(
"Inverting iota!")
583 common_output_table_raw[:,idx]= np.pi*np.ones(len(common_output_table_raw[:,0])) - common_output_table_raw[:,idx]
598 print(
"Converting iota-> cos(iota)")
599 idx=common_output_table_header.index(
'iota')
600 common_output_table_header[idx]=
'cos(iota)'
601 common_output_table_raw[:,idx]=np.cos(common_output_table_raw[:,idx])
622 print(
"Converting thetas -> cos(thetas)")
623 idx=common_output_table_header.index(
'thetas')
624 common_output_table_header[idx]=
'cos(thetas)'
625 common_output_table_raw[:,idx]=np.cos(common_output_table_raw[:,idx])
630 print(
"Converting beta -> cos(beta)")
631 idx=common_output_table_header.index(
'beta')
632 common_output_table_header[idx]=
'cos(beta)'
633 common_output_table_raw[:,idx]=np.cos(common_output_table_raw[:,idx])
638 idx=common_output_table_header.index(
'f_ref')
639 injFrefs=np.unique(common_output_table_raw[:,idx])
640 if len(injFrefs) == 1:
641 injFref = injFrefs[0]
642 print(
"Using f_ref in results as injected value")
647 pos_temp=bppu.Posterior((common_output_table_header,common_output_table_raw),SimInspiralTableEntry=injection, injFref=injFref)
649 if 'a1' in pos_temp.names
and min(pos_temp[
'a1'].samples)[0] < 0:
650 pos_temp.append_mapping(
'spin1',
lambda a:a,
'a1')
652 pos_temp.append_mapping(
'a1',
lambda a:np.abs(a),
'spin1')
653 if 'a2' in pos_temp.names
and min(pos_temp[
'a2'].samples)[0] < 0:
654 pos_temp.append_mapping(
'spin2',
lambda a:a,
'a2')
656 pos_temp.append_mapping(
'a2',
lambda a:np.abs(a),
'spin2')
659 if 'm1' in pos_temp.names
and 'm2' in pos_temp.names:
660 print(
"Calculating total mass")
661 pos_temp.append_mapping(
'mtotal',
lambda m1,m2: m1+m2, [
'm1',
'm2'])
662 if 'mass1' in pos_temp.names
and 'mass2' in pos_temp.names:
663 print(
"Calculating total mass")
664 pos_temp.append_mapping(
'mtotal',
lambda m1,m2: m1+m2, [
'mass1',
'mass2'])
667 idx=common_output_table_header.index(
'm1')
669 idx2=common_output_table_header.index(
'm2')
671 if pos_temp[
'm1'].mean<pos_temp[
'm2'].mean:
672 print(
"SWAPPING MASS PARAMS!")
673 common_output_table_header[idx]=
'x'
674 common_output_table_header[idx2]=
'm1'
675 common_output_table_header[idx]=
'm2'
676 pos_temp=bppu.Posterior((common_output_table_header,common_output_table_raw),SimInspiralTableEntry=injection)
680 pos_list[name]=pos_temp
682 if common_params
is None:
683 common_params=pos_temp.names
685 set_of_pars = set(pos_temp.names)
686 common_params=list(set_of_pars.intersection(common_params))
688 print(
"Common parameters are %s"%
str(common_params))
690 if injection
is None and injection_path
is not None:
691 injections = SimInspiralUtils.ReadSimInspiralFromFiles([injection_path])
692 injection=bppu.get_inj_by_time(injections,pos_temp.means[
'time'])
693 if injection
is not None:
694 for pos
in pos_list.values():
695 pos.set_injection(injection)
697 set_of_pars = set(allowed_params)
698 common_params=list(set_of_pars.intersection(common_params))
700 print(
"Using parameters %s"%
str(common_params))
702 if not os.path.exists(os.path.join(os.getcwd(),
'results')):
703 os.makedirs(
'results')
705 if not os.path.exists(outdir):
708 pdfdir=os.path.join(outdir,
'pdfs')
709 if not os.path.exists(pdfdir):
714 if common_params
is not []
and common_params
is not None:
715 colorlst=bppu.__default_color_lst
717 if len(common_params)>1:
718 temp=copy.copy(common_params)
727 color_idx_max=len(names_and_pos_folders)
728 cmap_array=my_cm(np.array(range(cmap_size)))
730 hatches=[
'/',
'\\',
'|',
'-',
'+',
'x',
'o',
'O',
'.',
'*']
734 for name,infolder
in names_and_pos_folders:
736 color_by_name[name]=cmap_array[
int(floor(color_idx*cmap_size/color_idx_max)),:]
737 color_idx=(color_idx+1) % color_idx_max
738 hatches_by_name[name]=hatches[color_idx]
745 pplst_cond=(pplst
in twoDplots)
746 rpplst_cond=(rpplst
in twoDplots)
747 if pplst_cond
or rpplst_cond:
750 print(
'2d plots: building ',i,j)
751 greedy2Params={i:greedyBinSizes[i],j:greedyBinSizes[j]}
758 slinestyles=[
'solid',
'dashed',
'dashdot',
'dotted']
760 fig=bppu.plot_two_param_kde_greedy_levels(pos_list,greedy2Params,TwoDconfidenceLevels,color_by_name,figsize=contour_figsize,dpi=contour_dpi,figposition=contour_figposition,legend=ldg,line_styles=slinestyles,hatches_by_name=hatches_by_name,Npixels=Npixels2D)
761 if fig
is None:
continue
763 greedy2savepaths.append(
'%s-%s.png'%(pplst[0],pplst[1]))
764 fig.savefig(os.path.join(outdir,
'%s-%s.png'%(pplst[0],pplst[1])),bbox_inches=
'tight')
765 fig.savefig(os.path.join(pdfdir,
'%s-%s.pdf'%(pplst[0],pplst[1])),bbox_inches=
'tight')
771 confidence_levels=[{},{},{},{}]
772 confidence_uncertainty={}
773 for param
in common_params:
774 print(
"Plotting comparison for '%s'"%param)
776 cl_table_header=
'<table><th>Run</th>'
779 cl_table_min_max_str=
'<tr><td> Min | Max </td>'
781 for confidence_level
in OneDconfidenceLevels:
782 if analyticLikelihood:
783 pdf=analyticLikelihood.pdf(param)
784 cdf=analyticLikelihood.cdf(param)
788 cl_table_header+=
'<th colspan="2">%i%% (Lower|Upper)</th>'%(
int(100*confidence_level))
794 confidence_levels[level_index][param]=[]
796 for name,pos
in pos_list.items():
797 median=pos[param].median
798 low,high=cl_intervals[name]
800 confidence_levels[level_index][param].append((name,low,median,high))
802 level_index=level_index+1
805 for name,pos
in pos_list.items():
806 cl_bounds.append(cl_intervals[name])
807 poses.append(pos[param])
808 confidence_uncertainty[param]=bppu.confidence_interval_uncertainty(confidence_level, cl_bounds, poses)
811 if hist_fig
is not None:
812 save_path=os.path.join(outdir,
'%s_%i.png'%(param,
int(100*confidence_level)))
813 save_path_pdf=os.path.join(pdfdir,
'%s_%i.pdf'%(param,
int(100*confidence_level)))
815 plt.tight_layout(hist_fig)
816 plt.tight_layout(hist_fig2)
819 hist_fig.savefig(save_path,bbox_inches=
'tight')
820 hist_fig.savefig(save_path_pdf,bbox_inches=
'tight')
821 save_paths.append(save_path)
822 save_path=os.path.join(outdir,
'%s_%i_cum.png'%(param,
int(100*confidence_level)))
823 save_path_pdf=os.path.join(pdfdir,
'%s_%i_cum.pdf'%(param,
int(100*confidence_level)))
824 hist_fig2.savefig(save_path,bbox_inches=
'tight')
825 hist_fig2.savefig(save_path_pdf,bbox_inches=
'tight')
826 save_paths.append(save_path)
827 min_low,max_high=cl_intervals.values()[0]
828 for name,interval
in cl_intervals.items():
835 cl_table[name]+=
'<td>%s</td><td>%s</td>'%(low,high)
837 cl_table[name]=
'<td>%s</td><td>%s</td>'%(low,high)
838 cl_table_min_max_str+=
'<td>%s</td><td>%s</td>'%(min_low,max_high)
839 cl_table_str=cl_table_header
840 for name,row_contents
in cl_table.items():
841 cl_table_str+=
'<tr><td>%s<font color="%s"></font></td>'%(name,
str(mpl_colors.rgb2hex(color_by_name[name][0:3])))
843 cl_table_str+=row_contents+
'</tr>'
844 cl_table_str+=cl_table_min_max_str+
'</tr>'
845 cl_table_str+=
'</table>'
847 cl_uncer_str=
'<table> <th>Confidence Relative Uncertainty</th> <th>Confidence Fractional Uncertainty</th> <th>Confidence Percentile Uncertainty</th>\n'
848 cl_uncer_str+=
'<tr> <td> %g </td> <td> %g </td> <td> %g </td> </tr> </table>'%(confidence_uncertainty[param][0], confidence_uncertainty[param][1], confidence_uncertainty[param][2])
852 N=ks_matrix.shape[0]+1
855 ks_table_str=
'<table><th colspan="%d"> K-S test p-value matrix </th>'%N
858 ks_table_str+=
'<tr> <td> -- </td> '
859 for name,pos
in pos_list.items():
860 ks_table_str+=
'<td> %s </td>'%name
861 ks_table_str+=
'</tr>'
864 for i
in range(len(pos_list)):
865 ks_table_str+=
'<tr> <td> %s </td>'%(pos_list.keys()[i])
866 for j
in range(len(pos_list)):
868 ks_table_str+=
'<td> -- </td>'
869 elif ks_matrix[i,j] < 0.05:
871 ks_table_str+=
'<td> <b> %g </b> </td>'%ks_matrix[i,j]
873 ks_table_str+=
'<td> %g </td>'%ks_matrix[i,j]
875 ks_table_str+=
'</tr>'
877 ks_table_str+=
'</table>'
879 oned_data[param]=(save_paths,cl_table_str,ks_table_str,cl_uncer_str)
882 max_logls = [[name,
max(pos._logL)]
for name,pos
in pos_list.items()]
883 dics = [pos.DIC
for name, pos
in pos_list.items()]
885 return greedy2savepaths,oned_data,confidence_uncertainty,confidence_levels,max_logls,dics
888 """Outputs a LaTeX table of parameter and run medians and confidence levels."""
889 outfile=open(os.path.join(outpath,
'confidence_table.tex'),
'w')
890 for level_index
in range(len(OneDconfidenceLevels)):
891 params=list(clevels[level_index].keys())
894 for param
in clTableParams:
896 for name,low,med,high
in clevels[level_index][param]:
897 if name
in clevels_by_name:
898 clevels_by_name[name].append((param,low,med,high))
900 clevels_by_name[name] = [(param,low,med,high)]
903 outfile.write(
'confidence level %1.3g\n'%OneDconfidenceLevels[level_index])
904 outfile.write(
r'\begin{tabular}{|l||')
905 for param
in clTableParams:
910 outfile.write(
r'\hline ')
911 for param
in clTableParams:
913 tparam=paramNameLatexMap.get(param,param)
914 outfile.write(
r'& $%s$ '%tparam)
915 outfile.write(
'\\\\ \n \\hline \\hline ')
917 for name,levels
in clevels_by_name.items():
919 for param,low,med,high
in levels:
920 outfile.write(
r' & $%0.5g^{%0.5g}_{%0.5g}$ '%(med,high,low))
921 outfile.write(
'\\\\ \n')
923 outfile.write(
'\\hline \n \\end{tabular}')
925 outfile.write(
'\n\n')
930 """Outputs a LaTeX table of parameter and run medians and confidence levels."""
931 outfile=open(os.path.join(outpath,
'confidence_table.dat'),
'w')
932 for level_index
in range(len(OneDconfidenceLevels)):
933 params=list(clevels[level_index].keys())
936 for param
in clTableParams:
938 for name,low,med,high
in clevels[level_index][param]:
939 if name
in clevels_by_name:
940 clevels_by_name[name].append((param,low,med,high))
942 clevels_by_name[name] = [(param,low,med,high)]
945 outfile.write(
'%1.3g\t'%OneDconfidenceLevels[level_index])
946 for param
in clTableParams:
948 tparam=paramNameLatexMap.get(param,param)
949 outfile.write(
'%s\t'%param)
952 for name,levels
in clevels_by_name.items():
954 for param,low,med,high
in levels:
955 outfile.write(
'\t%6.6g - %6.6g'%(low,high))
963 outfile=open(os.path.join(outpath,
'confidence_uncertainty.dat'),
'w')
965 params=list(cluncertainty.keys())
966 uncer=list(cluncertainty.values())
968 outfile.write(
'# Uncertainty in confidence levels.\n')
969 outfile.write(
'# First row is relative uncertainty (wrt to parameter mean).\n')
970 outfile.write(
'# Second row is fractional uncertainty (wrt to combined conf interval).\n')
971 outfile.write(
'# Third row is percentile uncertainty (wrt combined samples).\n')
974 outfile.write(
str(param) +
' ')
977 rel = np.array([d[0]
for d
in uncer])
978 fracs = np.array([d[1]
for d
in uncer])
979 quants = np.array([d[2]
for d
in uncer])
981 np.savetxt(outfile, np.reshape(rel, (1, -1)))
982 np.savetxt(outfile, np.reshape(fracs, (1, -1)))
983 np.savetxt(outfile, np.reshape(quants, (1,-1)))
987if __name__ ==
'__main__':
988 from optparse
import OptionParser
989 parser=OptionParser()
990 parser.add_option(
"-o",
"--outpath", dest=
"outpath",help=
"Make page and plots in DIR.", metavar=
"DIR")
991 parser.add_option(
"-p",
"--pos",dest=
"pos_list",action=
"append",help=
"Path to folders containing output of cbcBayesPostProc.")
992 parser.add_option(
"-n",
"--name",dest=
"names",action=
"append",help=
"Name of posterior result e.g. followupMCMC 2.5PN (optional)")
993 parser.add_option(
"-i",
"--inj",dest=
"inj",help=
"Path of injection XML containing SimInspiralTable (optional).")
994 parser.add_option(
"-e",
"--eventnum",dest=
"eventnum",help=
"Sim ID of injection described in injection XML (optional).")
995 parser.add_option(
"-u",dest=
"username",help=
"User name for https authenticated content (optional).")
996 parser.add_option(
"-x",dest=
"password",help=
"Password for https authenticated content (optional).")
997 parser.add_option(
"--reload",dest=
"reload_flag",action=
"store_true",help=
"Re-download all pos files (optional).")
998 parser.add_option(
"--hide-cl-lines",dest=
"clf",action=
"store_false",default=
True,help=
"Hide confidence level lines on 1D plots for clarity (optional).")
999 parser.add_option(
"--contour-dpi",dest=
"cdpi",default=250,help=
"DPI for contour plot (optional).")
1000 parser.add_option(
"--contour-width",dest=
"cw",default=7,help=
"Width (in inches) of contour plots (optional).")
1001 parser.add_option(
"--contour-height",dest=
"ch",default=6,help=
"Height (in inches) of contour plots (optional).")
1002 parser.add_option(
"--contour-plot-width",dest=
"cpw",default=0.5,help=
"Relative width of plot element 0.15<width<1 (optional).")
1003 parser.add_option(
"--contour-plot-height",dest=
"cph",default=0.76,help=
"Relative height of plot element 0.15<width<1 (optional).")
1004 parser.add_option(
"--no-legend",dest=
"ldg_flag",action=
"store_false",default=
True,help=
"Hide legend (optional).")
1005 parser.add_option(
"--ignore-missing-files",dest=
"readFileErr",default=
False,action=
"store_true",help=
"Do not fail when files are missing (optional).")
1006 parser.add_option(
"-c",
"--covarianceMatrix",dest=
"covarianceMatrices",action=
"append",default=
None,help=
"CSV file containing covariance (must give accompanying mean vector CSV. Can add more than one matrix.")
1007 parser.add_option(
"-m",
"--meanVectors",dest=
"meanVectors",action=
"append",default=
None,help=
"Comma separated list of locations of the multivariate gaussian described by the correlation matrix. First line must be list of params in the order used for the covariance matrix. Provide one list per covariance matrix.")
1008 parser.add_option(
"--no2D",dest=
"no2d",action=
"store_true",default=
False,help=
"Disable 2D plots")
1009 parser.add_option(
"--npixels-2d",dest=
"npixels_2d",action=
"store",type=
"int",default=50,help=
"Number of pixels on a side of the 2D plots (default 50)",metavar=
"N")
1011 (opts,args)=parser.parse_args()
1013 if opts.outpath
is None:
1014 print(
"No output directory specified. Output will be saved to PWD : %s"%os.getcwd())
1017 outpath=opts.outpath
1019 if opts.pos_list
is None:
1020 print(
"No input paths given!")
1023 if opts.names
is None:
1024 print(
"No names given, making some up!")
1026 for i
in range(len(opts.pos_list)):
1027 names.append(
str(i))
1031 if len(opts.pos_list)!=len(names):
1032 print(
"Either add names for all posteriors or dont put any at all!")
1035 names,pos_list = zip(*sorted(zip(names,opts.pos_list)))
1041 greedy2savepaths,oned_data,confidence_uncertainty,confidence_levels,max_logls,dics=
compare_bayes(outpath,zip(names,pos_list),opts.inj,opts.eventnum,opts.username,opts.password,opts.reload_flag,opts.clf,opts.ldg_flag,contour_figsize=(float(opts.cw),float(opts.ch)),contour_dpi=
int(opts.cdpi),contour_figposition=[0.15,0.15,float(opts.cpw),float(opts.cph)],fail_on_file_err=
not opts.readFileErr,covarianceMatrices=opts.covarianceMatrices,meanVectors=opts.meanVectors,Npixels2D=
int(opts.npixels_2d))
1052 compare_page=bppu.htmlPage(
'Compare PDFs (single event)',css=bppu.__default_css_string)
1054 param_section=compare_page.add_section(
'Meta')
1056 param_section_write=
'<div><p>This comparison was created from the following analyses</p>'
1057 param_section_write+=
'<table border="1">'
1058 param_section_write+=
'<th>Analysis</th> <th> max(log(L)) </th> <th> DIC </th>'
1059 for (name,logl_max), dic
in zip(max_logls, dics):
1060 param_section_write+=
'<tr><td><a href="%s">%s</a></td> <td>%g</td> <td>%.1f</td></tr>'%(dict(zip(names,pos_list))[name],name,logl_max,dic)
1061 param_section_write+=
'</table></div>'
1063 param_section.write(param_section_write)
1064 param_section.write(
'<div><p><a href="confidence_table.tex">LaTeX table</a> of medians and confidence levels.</p></div>')
1067 param_section=compare_page.add_section(
'1D marginal posteriors')
1069 for param_name,data
in oned_data.items():
1070 param_section.h3(param_name)
1071 save_paths,cl_table_str,ks_table_str,cl_uncer_str=data
1073 for save_path
in save_paths:
1074 head,plotfile=os.path.split(save_path)
1075 param_section.write(
'<img src="%s"/>'%
str(plotfile))
1077 param_section.write(cl_table_str)
1078 param_section.write(cl_uncer_str)
1079 param_section.write(ks_table_str)
1081 if greedy2savepaths:
1083 param_section=compare_page.add_section(
'2D greedy bin histograms')
1084 for plot_path
in greedy2savepaths:
1085 temp,param_name=os.path.split(plot_path)
1086 param_name=param_name.split(
'.')[0]
1087 head,plotfile=os.path.split(plot_path)
1088 param_section.write(
'<img src="%s"/>'%
str(plotfile))
1092 compare_page_footer=compare_page.add_section(
'')
1093 compare_page_footer.p(
'Produced using cbcBayesCompPos.py at '+strftime(
"%Y-%m-%d %H:%M:%S")+
' .')
1095 cc_args=copy.deepcopy(sys.argv)
1099 user_idx=cc_args.index(
'-u')
1100 cc_args[user_idx+1]=
'<LIGO username>'
1105 pass_idx=cc_args.index(
'-x')
1106 cc_args[pass_idx+1]=
'<LIGO password>'
1111 for cc_arg
in cc_args:
1112 cc_args_str+=cc_arg+
' '
1114 compare_page_footer.p(
'Command line: %s'%cc_args_str)
1115 compare_page_footer.p(git_version.verbose_msg)
1118 resultspage=open(os.path.join(outpath,
'index.html'),
'w')
1119 resultspage.write(
str(compare_page))
def open_url_curl(url, args=[])
def compute_ks_pvalue_matrix(list_of_pos_by_name, param)
Returns a matrix of ks p-value tests between pairs of posteriors on the 1D marginalized distributions...
def compare_bayes(outdir, names_and_pos_folders, injection_path, eventnum, username, password, reload_flag, clf, ldg_flag, contour_figsize=(4.5, 4.5), contour_dpi=250, contour_figposition=[0.15, 0.15, 0.5, 0.75], fail_on_file_err=True, covarianceMatrices=None, meanVectors=None, Npixels2D=50)
def compare_plots_one_param_pdf(list_of_pos_by_name, param, analyticPDF=None)
Plots a gaussian kernel density estimate for a set of Posteriors onto the same axis.
def output_confidence_uncertainty(cluncertainty, outpath)
def output_confidence_levels_dat(clevels, outpath)
Outputs a LaTeX table of parameter and run medians and confidence levels.
def output_confidence_levels_tex(clevels, outpath)
Outputs a LaTeX table of parameter and run medians and confidence levels.
def open_url(url, username, password)
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.
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.
def test_and_switch_param(common_output_table_header, test, switch)