34from lalinference
import bayespputils
as bppu
35from lalinference
import git_version
37__author__=
"Salvatore Vitale <salvatore.vitale@ligo.org>"
38__version__=
"git id %s"%git_version.id
39__date__= git_version.date
41USAGE=
'''%prog --gid graceDBid --samples posterior_samples.dat
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.
48def cbcBayesGraceDBinfo(gid=None,samples=None,skymap=None,analysis='LALInference', bcifile=None,bsnfile=None,email=None,message=None,server="https://gracedb.ligo.org/api/
"):
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")
54 import ligo.gracedb.rest
56 if server
is not None:
57 g=ligo.gracedb.rest.GraceDb(server)
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)
66 peparser=bppu.PEOutputParser(
'common')
67 commonResultsObj=peparser.parse(open(samples,
'r'))
70 pos = bppu.BurstPosterior(commonResultsObj)
71 pars=[
'frequency',
'quality',
'hrss']
72 units={
'frequency':
'[Hz]',
'quality':
'',
'hrss':
'',
'loghrss':
''}
74 pos = bppu.Posterior(commonResultsObj)
75 pars=[
'mchirp',
'q',
'distance']
77 outstr=
'<table><tr><th colspan=2 align=center>%s PE summary</th></tr>'%analysis
83 outstr+=
'<tr><td align=left>%s %s</td>'%(i,units[i])
84 outstr+=
'<td align=left>%.3e ± %.3e</td></tr>'%(pos[i].samples[which][0],pos[i].stdev)
86 outstr+=
'<tr><td align=left>%s %s</td>'%(i,units[i])
87 outstr+=
'<td align=left>%.3f ± %.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)
92 outstr+=
'<tr><td align=left>logBCI</td>'
93 outstr+=
'<td align=center>%.2f</td></tr>'%(bci)
96 if bsnfile
is not None and os.path.isfile(bsnfile):
97 bsn=np.loadtxt(bsnfile)
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")
110 outstr+=
'<tr><td align=left>logBSN</td>'
111 outstr+=
'<td align=center>%.2f</td></tr>'%(bsn)
114 if email
is not None and bci
is not None:
117 address=email.split(
',')
119 USER=os.environ[
'USER']
121 HOST=socket.getfqdn()
125 elif bci>6
and bci<10:
126 pref=
"A very interesting"
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
133 TEXT+=
"RUN!!!!!!!!!!\n"
134 message=
"From: %s\nTo: %s\nSubject: %s\n\n%s"%(FROM,
', '.join(address),SUBJECT,TEXT)
137 os.system(
'echo "%s" | mail -s "%s" "%s"'%(TEXT,SUBJECT,
', '.join(address)))
138 server=smtplib.SMTP(SERVER)
139 server.sendmail(FROM,address,message)
142 print(
"Cound not send email\n")
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)
149 if bsnfile
is not None and os.path.isfile(bsnfile):
150 bsn=np.loadtxt(bsnfile)
154 if bci
is not None and bsn
is not None:
155 if bsn>5.
and bci>2.:
158 g.writeLog(gid,message,filename=skymap,tagname=tag)
161if __name__==
'__main__':
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/")
175 (opts,args)=parser.parse_args()
177 print(
"Must provide a graceDB id with --gid/-g ")
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 ")
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/")