Loading [MathJax]/extensions/TeX/AMSsymbols.js
LALInference 4.1.9.1-b246709
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
cbcBayesGraceDBinfo.py
Go to the documentation of this file.
1##python
2# -*- coding: utf-8 -*-
3#
4# cbcBayesGraceDBinfo
5#
6# Copyright 2015
7# Salvatore Vitale <salvatore.vitale@ligo.org>
8#
9# This program is free software; you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation; either version 2 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program; if not, write to the Free Software
21# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
22# MA 02110-1301, USA.
23
24#===============================================================================
25# Preamble
26#===============================================================================
27
28#standard library imports
29import sys
30import numpy as np
31import matplotlib
32matplotlib.use("Agg")
33
34from lalinference import bayespputils as bppu
35from lalinference import git_version
36
37__author__="Salvatore Vitale <salvatore.vitale@ligo.org>"
38__version__= "git id %s"%git_version.id
39__date__= git_version.date
40
41USAGE='''%prog --gid graceDBid --samples posterior_samples.dat
42
43Upload to the pe section of the graceDB page gid information about a lalinference postprocessing run.
44For the moment this is maximum a posteriori and stdev for the parameters in the string pars.
45
46'''
47
48def cbcBayesGraceDBinfo(gid=None,samples=None,skymap=None,analysis='LALInference', bcifile=None,bsnfile=None,email=None,message=None,server="https://gracedb.ligo.org/api/"):
49
50 if gid is None or (samples is None and skymap is None):
51 print("Must provide both a graceDB id and a posterior samples file or skymap file\n")
52 sys.exit(1)
53
54 import ligo.gracedb.rest
55 import os
56 if server is not None:
57 g=ligo.gracedb.rest.GraceDb(server)
58 else:
59 g=ligo.gracedb.rest.GraceDb()
60 if samples is not None:
61 samples=os.path.realpath(samples)
62 if '.hdf' in samples or '.h5' in samples:
63 peparser = bppu.PEOutputParser('hdf5')
64 commonResultsObj=peparser.parse(samples)
65 else:
66 peparser=bppu.PEOutputParser('common')
67 commonResultsObj=peparser.parse(open(samples,'r'))
68
69 try:
70 pos = bppu.BurstPosterior(commonResultsObj)
71 pars=['frequency','quality','hrss']
72 units={'frequency':'[Hz]','quality':'','hrss':'','loghrss':''}
73 except:
74 pos = bppu.Posterior(commonResultsObj)
75 pars=['mchirp','q','distance']
76 strs=[]
77 outstr='<table><tr><th colspan=2 align=center>%s PE summary</th></tr>'%analysis
78
79 for i in pars:
80 if i in pos.names:
81 _,which=pos._posMap()
82 if i=='hrss':
83 outstr+='<tr><td align=left>%s %s</td>'%(i,units[i])
84 outstr+='<td align=left>%.3e &plusmn; %.3e</td></tr>'%(pos[i].samples[which][0],pos[i].stdev)
85 else:
86 outstr+='<tr><td align=left>%s %s</td>'%(i,units[i])
87 outstr+='<td align=left>%.3f &plusmn; %.3f</td></tr>'%(pos[i].samples[which][0],pos[i].stdev)
88 if bcifile is not None and os.path.isfile(bcifile):
89 bci=np.loadtxt(bcifile)
90 else: bci=None
91 if bci is not None:
92 outstr+='<tr><td align=left>logBCI</td>'
93 outstr+='<td align=center>%.2f</td></tr>'%(bci)
94
95 bsn=None
96 if bsnfile is not None and os.path.isfile(bsnfile):
97 bsn=np.loadtxt(bsnfile)
98 bsn=bsn[0]
99 else:
100 try:
101 import h5py
102 with h5py.File(samples,'r') as h5grp:
103 tmp=h5grp['lalinference']['lalinference_nest'].attrs
104 bsn=tmp['log_bayes_factor']
105 except Exception as e:
106 print("Could not obtain BNS\n")
107 print(e)
108
109 if bsn is not None:
110 outstr+='<tr><td align=left>logBSN</td>'
111 outstr+='<td align=center>%.2f</td></tr>'%(bsn)
112 outstr+='</table>'
113
114 if email is not None and bci is not None:
115 import os
116 import smtplib
117 address=email.split(',')
118 SERVER="localhost"
119 USER=os.environ['USER']
120 import socket
121 HOST=socket.getfqdn()#socket.gethostbyaddr(socket.gethostname())[0]
122 pref=""
123 if bci>3 and bci<6:
124 pref="A promising"
125 elif bci>6 and bci<10:
126 pref="A very interesting"
127 elif bci>10:
128 pref="A SPECTACULAR"
129 FROM="salvatore.vitale@"+HOST
130 SUBJECT="%s LIB result page is ready at "%pref+HOST+" for graceID %s!"%(gid)
131 TEXT="LIB run for graceID %s is done on "%gid+HOST+".\nThe BCI is %lf\n"%bci
132 if bci>10:
133 TEXT+="RUN!!!!!!!!!!\n"
134 message="From: %s\nTo: %s\nSubject: %s\n\n%s"%(FROM,', '.join(address),SUBJECT,TEXT)
135 try:
136 import os
137 os.system('echo "%s" | mail -s "%s" "%s"'%(TEXT,SUBJECT,', '.join(address)))
138 server=smtplib.SMTP(SERVER)
139 server.sendmail(FROM,address,message)
140 server.quit()
141 except:
142 print("Cound not send email\n")
143
144 g.writeLog(gid,outstr,filename=None,tagname='pe')
145 elif skymap is not None:
146 if bcifile is not None and os.path.isfile(bcifile):
147 bci=np.loadtxt(bcifile)
148 else: bci=None
149 if bsnfile is not None and os.path.isfile(bsnfile):
150 bsn=np.loadtxt(bsnfile)
151 else: bsn=None
152 tag=['sky_loc']
153 """
154 if bci is not None and bsn is not None:
155 if bsn>5. and bci>2.:
156 tag.append('lvem')
157 """
158 g.writeLog(gid,message,filename=skymap,tagname=tag)
159
160
161if __name__=='__main__':
162
163 from optparse import OptionParser
164 parser=OptionParser(USAGE)
165 parser.add_option("-g","--gid", dest="gid",help="GraceDB id", metavar="G123456",default=None)
166 parser.add_option("-s","--samples",dest="samples",help="posterior_samples.hdf5/dat",default=None)
167 parser.add_option("--analysis",help="Prefix to use for the graceDB entries. Should be the name of the analysis (default LALInference)",default='LALInference')
168 parser.add_option("--bci",dest="bci",help="coherence test file: bci.dat",default=None)
169 parser.add_option("--bsn",dest="bsn",help="evidence file: bsn.dat [Deprecated, now is read from samples.hdf5]",default=None)
170 parser.add_option("--skymap",dest="skymap",help="FITS file skymap",default=None)
171 parser.add_option("--message",dest="message",type='str', help="Message to go with skymap uplaod",default=None)
172 parser.add_option('--email',dest='email',help="Will email when run is done.",default=None)
173 parser.add_option('--server',dest='server',help="GraceDB server to interact with.",default="https://gracedb.ligo.org/api/")
174
175 (opts,args)=parser.parse_args()
176 if opts.gid is None:
177 print("Must provide a graceDB id with --gid/-g ")
178 sys.exit(1)
179 if opts.samples is None and opts.skymap is None:
180 print("Must provide lalinference posterior samples with --samples/-s or FITS skymap with --skymap ")
181 sys.exit(1)
182 cbcBayesGraceDBinfo(opts.gid, opts.samples,analysis=opts.analysis,bcifile=opts.bci,bsnfile=opts.bsn,email=opts.email,skymap=opts.skymap,message=opts.message,server=opts.server)
def cbcBayesGraceDBinfo(gid=None, samples=None, skymap=None, analysis='LALInference', bcifile=None, bsnfile=None, email=None, message=None, server="https://gracedb.ligo.org/api/")