Loading [MathJax]/extensions/TeX/AMSsymbols.js
LALInference 4.1.9.1-8a6b96f
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
postproc.py
Go to the documentation of this file.
1"""
2Postprocessing for the tiger pipeline
3"""
4
5__author__ = "Tjonnie Li"
6__credits__ = ["Tjonnie Li"]
7__maintainer__ = "Tjonnie Li"
8__email__ = "tjonnie.li@ligo.org"
9__status__ = "Production"
10
11###############################################################################
12#
13# USER DEFINED DATA
14#
15###############################################################################
16
17clusters = {'atlas':'titan2.atlas.aei.uni-hannover.de', \
18 'nemo':'pcdev2.phys.uwm.edu', \
19 'cit':'ldas-pcdev2.ligo.caltech.edu', \
20 'llo':'ldas-pcdev2.ligo-la.caltech.edu', \
21 'lho':'ldas-pcdev1.ligo-wa.caltech.edu'}
22
23color_default = ['blue','red','green']
24hatch_default = ['..','//','||']
25
26###############################################################################
27#
28# LOAD LIBRARIES
29#
30###############################################################################
31
32from configparser import ConfigParser
33from pickle import dump, load
34from datetime import datetime
35from itertools import combinations
36from itertools import cycle
37from matplotlib import use, rcParams, __version__ as mpl_version
38use('Agg')
39from matplotlib.pyplot import clf, figure
40from os import access, path, R_OK, makedirs
41from scipy.stats import ks_2samp
42from scipy.stats.mstats import mquantiles
43from subprocess import Popen, PIPE
44from sys import exit, stdout, version_info, float_info
45from time import time
46from numpy import sqrt, array, empty, hstack, min, max, reshape, shape, loadtxt, vstack, append, arange, random, column_stack, concatenate, savetxt, log, exp, size, zeros, argmax, argsort, sort, sum, subtract, array_split
47from re import findall
48
49py_version = version_info[:2]
50if py_version < (2, 7):
51 from optparse import OptionParser
52else:
53 from argparse import ArgumentParser
54
55###############################################################################
56#
57# PLOTTING OPTIONS
58#
59###############################################################################
60
61fig_width_pt = 3*246.0 # Get this from LaTeX using \showthe\columnwidth
62inches_per_pt = 1.0/72.27 # Convert pt to inch
63golden_mean = (sqrt(5)-1.0)/2.0 # Aesthetic ratio
64fig_width = fig_width_pt*inches_per_pt # width in inches
65fig_height = fig_width*golden_mean # height in inches
66fig_size = [fig_width,fig_height]
67
68fontsize = 'font.size' if mpl_version >= '1.5.0' else 'text.fontsize'
69params = {'backend': 'PDF',
70 'axes.labelsize': 24,
71 fontsize: 24,
72 'legend.fontsize': 20,
73 'xtick.labelsize': 24,
74 'ytick.labelsize': 24,
75 'axes.grid' : True,
76 'text.usetex': True,
77 'savefig.dpi' : 200,
78 'lines.markersize' : 14,
79 'figure.figsize': fig_size}
80
81rcParams.update(params)
82
83scriptfilename = path.realpath(__file__)
84###############################################################################
85#
86# PLOTTING OPTIONS
87#
88###############################################################################
89
90def main():
91 """
92 -------------------------------------------------------------------------------
93 """
94
95
96 ###############################################################################
97 #
98 # ARGUMENT PARSING
99 #
100 ###############################################################################
101
102 # SETUP ARGUMENT PARSER
103 if py_version < (2, 7):
104 parser = OptionParser()
105 parser.add_option('-c', '--config', metavar='FILE', dest='configfile', \
106 type=str, help='configuration file')
107 parser.add_option('-g', '--generate', metavar='FILE', dest='examplecfg', \
108 type=str, help='generate example configuration file')
109 parser.add_option('-p', '--preprocess', metavar='FILE', dest='preprocessfile', \
110 type=str, help='preprocess data before sending to main script (NB: NOT WORKING!)')
111 (args, options) = parser.parse_args()
112 else:
113 parser = ArgumentParser(description='Post-processing for TIGER')
114 parser.add_argument('-c', '--config', metavar='FILE', dest='configfile', \
115 type=str, help='configuration file')
116 parser.add_argument('-g', '--generate', metavar='FILE', dest='examplecfg', \
117 type=str, help='generate example configuration file')
118 parser.add_argument('-p', '--preprocess', metavar='FILE', dest='preprocessfile', \
119 type=str, help='preprocess data before sending to main script (NB: NOT WORKING!)')
120 args = parser.parse_args()
121
122 if (args.configfile == None) and (args.examplecfg == None) and (args.preprocessfile == None):
123 exit("Specify either -c/--config, -g/--generate or -p/--preprocess")
124 elif (args.configfile != None):
125 stdout.write("****************************************\nTIGER post-process\n%s\n****************************************\n" % args.configfile)
126 TigerPostProcess(args.configfile)
127 elif (args.examplecfg != None):
128 TigerCreateExampleConfigFile(args.examplecfg)
129 elif (args.preprocessfile != None):
130 TigerPreProcess(args.preprocessfile)
131 else:
132 exit('Unknown options - check input')
133 exit(0)
134
135def TigerPostProcess(configfile):
136 """
137 Main TIGER post processing function. Calls the relevant function for source
138 collection, odds ratio calculations, and plotting
139 """
140
141 # CONFIGURATION FILE: CHECKING FILE
142 stdout.write("Configuration file: checking file\n")
143 if access(configfile, R_OK):
144 config = ConfigParser()
145 config.read(configfile)
146 else:
147 exit("Configuration file: checking file - file does not exist. Abort script\n")
148
149 # CONFIGURATION FILE: READING
150 stdout.write("Configuration file: reading\n")
151 testcoef = config.get('run','hypotheses').split(',')
152 localdest = config.get('run','localdest')
153 N_sources=[int(i) for i in config.get('hist','nsources').split(',')]
154 types = config.get('plot','types').split(',')
155 engine=config.get('run','engine').split(',')
156 runid = config.get('run','runid')
157 labels=config.get('fetch','labels').split(',')
158
159 latexlabels_str = config.get('fetch','latexlabels')
160 matches=findall(r'\$(.+?)\$',latexlabels_str)
161 latexlabels=["$%s$"%s for s in matches]
162
163 # CREATE DESTINATION DIRECTOR
164 ensure(localdest)
165
166 ###############################################################################
167 #
168 # FETCH DATA
169 #
170 ###############################################################################
171
172 # FETCHING DATA: GETTING OPTIONS
173 fetch_type = config.get('fetch','type').split(',')
174 fetch_loc = config.get('fetch','locs').split(',')
175 fetch_snrthres = config.getfloat('cut','snrthres')
176 fetch_bayesgrthres = config.getfloat('cut','bayesgrthres')
177 fetch_seed = config.getint('fetch','seed')
178 if len(fetch_type) != len(fetch_loc):
179 exit('Locations and types are not of the same length - exit\n')
180
181 # FETCHING DATA
182 tigerruns = []
183 for i in range(len(fetch_type)):
184 if fetch_type[i] == 'source':
185 # READING LOCATION FILES
186 locfp = open(fetch_loc[i],'r')
187 locations = [k.split() for k in locfp.readlines()]
188 stdout.write("Fetching data: %s\n" % fetch_loc[i])
189 # CREATING CLASS INSTANCES
190 tigerruns.append(TigerSet(locations, labels[i],latexlabels[i],testcoef,engine[i]))
191 # FETCH DATA
192 tigerruns[-1].searchsources() # SEARCH FOR SOURCES
193 tigerruns[-1].pullbayes() # PULL BAYESFACTORS
194 tigerruns[-1].pullsnr() # PULL SNR
195 #tigerruns[-1].preprocess() # preprocess data on clusters (NOT READY YET)
196 elif fetch_type[i] == 'pickle':
197 tigerruns.append(LoadPickledTigerSet(fetch_loc[i]))
198 tigerruns[-1].latexlabel=latexlabels[i]
199 else:
200 exit('Fetch has to be either source or pickle. Check the configuration file - Abort')
201
202 # SAVING DATA BEFORE CUT
203 html_files_sources = []
204 stdout.write('Saving data\n')
205 for i in range(len(tigerruns)):
206 html_files_sources.append(path.join(localdest,tigerruns[i].label+'.pickle'))
207 tigerruns[i].savetopickle(html_files_sources[-1])
208 stdout.write('... Saving data: %s\n' % html_files_sources[-1])
209 html_files_sources.append(path.join(localdest,tigerruns[i].label+'.dat'))
210 tigerruns[i].savetoascii(html_files_sources[-1])
211 stdout.write('... Saving data: %s\n' % html_files_sources[-1])
212
213 # APPLY CUTS
214 stdout.write("Applying cuts\n")
215 for tg in tigerruns:
216 tg.applycut(st=fetch_snrthres, bt=fetch_bayesgrthres)
217
218 # SHUFFLE REALISATIONS
219 #if fetch_seed != 0:
220 #print 'Shuffling realisation'
221 #for tg in tigerruns:
222 #tg.shufflesources(seed=fetch_seed)
223
224 # SAVING DATA AFTER CUT
225 #print 'Saving data - after cut'
226 #for i in range(len(tigerruns)):
227 #html_files_sources.append(path.join(localdest,tigerruns[i].label+'_cut'+'.pickle'))
228 #tigerruns[i].savetopickle(html_files_sources[-1])
229 #html_files_sources.append(path.join(localdest,tigerruns[i].label+'_cut'+'.dat'))
230 #tigerruns[i].savetoascii(html_files_sources[-1])
231
232 ###############################################################################
233 #
234 # PRODUCE PLOTS
235 #
236 ###############################################################################
237
238 stdout.write("Producing plots\n")
239
240 # FETCH OPTIONS
241 plot_hist = config.getboolean('plot','hist')
242 plot_snrVSodds = config.getboolean('plot','snrvsodds')
243 plot_cumbayes = config.getboolean('plot','cumbayes')
244 plot_cumfreq = config.getboolean('plot','cumfreq')
245 hist_odds_bins=config.getint('hist','bins')
246
247 # STATISTICS
248 html_data_efficiencies = []
249 betas = [0.95,0.99]
250 if len(tigerruns)>1:
251 for ns in N_sources:
252 tmp = TigerCalculateEfficiency(tigerruns,N=ns,beta=betas)
253 html_data_efficiencies.append(tmp)
254 html_data_efficiencies = array(html_data_efficiencies)
255
256 html_data_ks =[[TigerKSandPvalue(tigerruns[0],j,N=ns) for ns in N_sources] for j in tigerruns[1:] if len(tigerruns)>1]
257 #for ns in N_sources:
258 #html_data_ks.append(TigerKSandPvalue(tigerruns[0],tigerruns[1],N=ns))
259
260 # HISTOGRAMS
261 html_files_hist = []
262 if plot_hist==True:
263 stdout.write("... Histograms\n")
264 # GET XLIM FOR HISTOGRAMS (IF SPECIFICIED)
265 hist_xlim = [float(item) for item in config.get('hist','xlim').split(',')]
266 if hist_xlim == [0.0,0.0]:
267 # SETTING HISTOGRAM LIMITS (ADD 10% TO EACH EDGE)
268 tmplist = empty(0)
269 for tg in tigerruns:
270 for ns in N_sources:
271 tmplist = hstack((tmplist,tg.odds(ns)))
272
273 # CALCULATE XLIM FROM [MIN,MAX] + 10 PERCENT
274 xlim = array([min(tmplist),max(tmplist)])
275 xlim = array([xlim[0]-0.1*(xlim[1]-xlim[0]),xlim[1]+0.1*(xlim[1]-xlim[0])])
276 del tmplist
277 else:
278 xlim = hist_xlim
279
280 # CREATE HISTOGRAMS
281 for ns in N_sources:
282 clf()
283 fig = figure(ns)
284 ax = fig.add_subplot(111)
285 TigerCreateHistogram(tigerruns,ax,N=ns, xlim=xlim, bins=hist_odds_bins)
286 for typ in types:
287 html_files_hist.append(path.join(localdest,'hist_'+runid+"_"+str(ns)+"sources."+typ))
288 fig.savefig(html_files_hist[-1],bbox_inches='tight')
289
290 # SNR VS ODDS
291 html_files_snrvsodds = []
292 if plot_snrVSodds == True:
293 stdout.write("... SNR vs. Odds\n")
294 clf()
295 fig = figure()
296 ax = fig.add_subplot(111)
297 TigerCreateSNRvsOdds(tigerruns, ax)
298 for ext in types:
299 html_files_snrvsodds.append(path.join(localdest,"snrVSodds_"+runid+"."+ext))
300 fig.savefig(html_files_snrvsodds[-1], bbox_inches='tight')
301
302 # CUMULATIVE FREQUENCY PLOTS
303 html_files_cumfreq = [[]]*len(tigerruns)
304 if plot_cumfreq == True:
305 stdout.write("... Cumulative frequency\n")
306 for i in range(len(tigerruns)):
307 html_files_cumfreq[i] = []
308 clf()
309 fig = figure()
310 ax = fig.add_subplot(111)
311 TigerCreateCumFreq(tigerruns[i], ax)
312 for ext in types:
313 html_files_cumfreq[i].append(path.join(localdest,"cumfreq_"+tigerruns[i].label+"."+ext))
314 fig.savefig(html_files_cumfreq[i][-1],bbox_inches='tight')
315
316 # CUMULATIVE BAYES PLOTS
317 html_files_cumbayes = [[]]*len(tigerruns)
318 N_sources = array(N_sources)
319 stdout.write("... Cumulative Bayes\n")
320 if plot_cumbayes == True:
321 N_sources_cats = array(N_sources[N_sources!=1])
322 for i in range(len(tigerruns)):
323 html_files_cumbayes[i] = [[]]*len(N_sources_cats)
324 for j in range(len(N_sources_cats)):
325 html_files_cumbayes[i][j] = []
326 for k in range(tigerruns[i].nsources/N_sources_cats[j]):
327 clf()
328 fig = figure()
329 ax = fig.add_subplot(111)
330 TigerCreateCumBayes(tigerruns[i],ax,k,N_sources_cats[j])
331 for ext in types:
332 html_files_cumbayes[i][j].append(path.join(localdest,"cumbayes_"+tigerruns[i].label+"_catalog"+str(k)+"_sourcespercat_"+str(N_sources_cats[j])+"."+ext))
333 fig.savefig(html_files_cumbayes[i][j][-1], bbox_inches='tight')
334
335 ###############################################################################
336 #
337 # MAKE HTML OUTPUT PAGE
338 #
339 ###############################################################################
340
341 # TODO - CREATE SIMPLE OUTPUT PAGE
342
343 stdout.write("Creating html pages\n")
344 htmlfile=open(path.join(localdest,'index.html'), 'w')
345 htmltxt=\
346 """
347 <!DOCTYPE html>
348 <html>
349
350 <head>
351 <title> TIGER post-processing page </title>
352 </head>
353
354 <body>
355
356 <h1>TIGER post-processing page</h1>
357 """
358
359 timesec = time()
360 timedate = datetime.fromtimestamp(timesec).strftime('%Y-%m-%d %H:%M:%S')
361 htmltxt+="Last updated on "+timedate
362
363 # ADD SOURCE LOCATIONS
364 #htmltxt+=\
365 #"""
366 #<h2>Sources</h2>
367 #"""
368 #for hfs in html_files_sources:
369 #hfsbase = path.basename(hfs)
370 #htmltxt+="<a href='"+hfsbase+"'>"+hfsbase+"</a>&nbsp;&nbsp;&nbsp;&nbsp;"
371
372 if len(tigerruns)>1:
373 htmltxt+=\
374 """
375 <h2>Statistics</h2>
376 """
377 htmltxt+=\
378 """
379 <h3>Efficiencies</h3>
380 """
381 htmltxt+=\
382 """
383 <table border="1">
384 <tr>
385 <td>catsize</td>
386 """
387 for ns in N_sources:
388 htmltxt+='<td>'+str(ns)+'</td>'
389 htmltxt+='</tr>'
390
391 background = 0
392 for i in range(len(tigerruns)):
393 if i != background: # hardcoded for the time being
394 htmltxt+='<tr>'
395 htmltxt+='<td>'+tigerruns[i].label+'</td>'
396 for j in range(len(N_sources)):
397 if i < background:
398 effstr="%.2f "%html_data_efficiencies[j,i,0]+' | '+ '%.2f'% html_data_efficiencies[j,i,1]
399 else:
400 effstr="%.2f"%html_data_efficiencies[j,i-1,0]+' | '+'%.2f'%html_data_efficiencies[j,i-1,1]
401 htmltxt+='<td>'+effstr+'</td>'
402 htmltxt+='</tr>'
403 htmltxt+='</table>'
404
405 htmltxt+=\
406 """
407 <h3>KS and p value</h3>
408 """
409 htmltxt+=\
410 """
411 <table border="1">
412 <tr>
413 <td></td>
414 <td>ks stat</td>
415 <td>p value</td>
416 </tr>
417 """
418 for i in range(len(tigerruns)-1):
419 for j in range(len(N_sources)):
420 htmltxt+='<tr>'
421 htmltxt+='<td>'+'N='+str(N_sources[j])+'</td>'
422 htmltxt+='<td>'+'%.2f'%html_data_ks[i][j][0]+'</td>'
423 htmltxt+='<td>'+'%.2f'%html_data_ks[i][j][1]+'</td>'
424 htmltxt+='</tr>'
425 htmltxt+='</table>'
426
427 # HISTOGRAMS
428 htmltxt+=\
429 """
430 <h2>Histograms</h2>
431 """
432 for item in html_files_hist:
433 if ".png" in item:
434 itembase = path.basename(item)
435 htmltxt+="<a href='"+itembase+"'>"+"<img src='"+itembase+"' width=512></a>"
436
437 # SNR VS ODDS
438 htmltxt+=\
439 """
440 <h2>SNR vs odds</h2>
441 """
442 for item in html_files_snrvsodds:
443 if ".png" in item:
444 itembase = path.basename(item)
445 htmltxt+="<a href='"+itembase+"'>"+"<img src='"+itembase+"' width=512></a>"
446
447 # PREPARE INDIVIDUAL PAGES
448 htmltxt+=\
449 """
450 <h2>Individual runs</h2>
451 """
452 for i in range(len(tigerruns)):
453 htmltxt+="<a href='"+tigerruns[i].label+".html"+"'>"+tigerruns[i].label+"</a>&nbsp;&nbsp;&nbsp;&nbsp;"
454
455 # CLOSE
456 htmltxt+=\
457 """
458 </body>
459 </html>
460 """
461
462 # CREATE PAGES FOR INIDVIDUAL RUNS
463 for i in range(len(tigerruns)):
464 indhtmlfile=open(path.join(localdest,tigerruns[i].label+'.html'), 'w')
465 indhtmltxt=\
466 """
467 <!DOCTYPE html>
468 <html>
469
470 <head>
471 <title> TIGER post-processing page </title>
472 </head>
473
474 <body>
475 """
476
477 indhtmltxt+="<h1>"+tigerruns[i].label+"</h1>"
478
479 # CUMFREQ
480 indhtmltxt+=\
481 """
482 <h2>Cumulative frequency highest Bayes factor</h2>
483 """
484 for item in html_files_cumfreq[i]:
485 if ".png" in item:
486 itembase = path.basename(item)
487 indhtmltxt+="<a href='"+itembase+"'>"+"<img src='"+itembase+"' width=512></a>"
488
489 # CUMBAYES
490 indhtmltxt+=\
491 """
492 <h2>Cumulative Bayes factors (per catalog)</h2>
493 """
494 N_sources_cats = array(N_sources[N_sources!=1])
495 for j in range(len(html_files_cumbayes[i])):
496 indhtmltxt+="<h2>"+str(N_sources_cats[j])+" sources per catalog"+"</h2>"
497 for item in html_files_cumbayes[i][j]:
498 if ".png" in item:
499 itembase = path.basename(item)
500 indhtmltxt+="<a href='"+itembase+"'>"+"<img src='"+itembase+"' width=512></a>"
501
502 indhtmlfile.write(indhtmltxt)
503
504
505 htmlfile.write(htmltxt)
506
507 #############################################################################
508 #
509 # COPY TO REMOTE CLUSTER
510 #
511 #############################################################################
512
513
514 #############################################################################
515 #
516 # EXIT MAIN
517 #
518 #############################################################################
519
520 print('Script finished')
521 """
522 ------------------------------------------------------------------------------
523 """
524
525###############################################################################
526#
527# TIGERRUN CLASS DEFINITIONS
528#
529###############################################################################
530
531class TigerRun:
532 """
533 Class for a specific TIGER run
534 """
535
536 def __init__(self, cluster, directory, engine, subhyp):
537 """
538 Initialisation of class
539 """
540 self.cluster = cluster
541 if directory[-1] != '/':
542 self.directory = directory+'/'
543 else:
544 self.directory = directory
545 self.engine = engine
546 self.subhyp = subhyp
547 self.bayesfiles = []
548 self.snrfiles = []
549 self.detectors = []
551 self.gps = []
552 self.nsources = 0
553 self.bayes = []
554 self.snr = []
556
557 def searchsources(self):
558 """
559 Search for sources completed sources in the specified cluster and location
560 """
561
562 stdout.write("... searching sources: %s\t%s\r" % (self.cluster,self.directory))
563 stdout.flush()
564 # GET BAYESFILES
565 command = ""
566 if self.cluster in clusters.keys():
567 command += "gsissh -C "+clusters[self.cluster]+" '"
568 command+="for i in `ls -a "+self.directory+"/"+self.subhyp[0]
569 if self.engine == 'lalnest':
570 command+="/nest/"
571 elif self.engine == 'lalinference':
572 command+="/posterior_samples/"
573 command+="*_B.txt | xargs -n1 basename`; do "
574 for sh in self.subhyp:
575 if self.engine == 'lalnest':
576 command+="test -f "+self.directory+"/"+sh
577 command+="/nest/"
578 elif self.engine == 'lalinference':
579 command+="test -f "+self.directory+"/"+sh.replace('phi','chi')
580 command+="/posterior_samples/"
581 command+="$i && "
582 command=command+"echo $i; done"
583 if self.cluster in clusters.keys():
584 command += "'"
585 p = Popen(command, stdout=PIPE, stderr=PIPE,shell=True)
586 lines = p.stdout.readlines()
587 self.bayesfiles = array([k.strip('\n') for k in lines])
588 del lines
589
590 # GET DETECTOR INFORMATION
591 if self.engine == 'lalnest':
592 self.detectors = array([k.split('_')[2].split('.')[0] for k in self.bayesfiles])
593 elif self.engine == 'lalinference':
594 self.detectors = array([k.split('_')[1] for k in self.bayesfiles])
595
596 # GET POSTERIOR FILES
597 if self.engine == 'lalnest':
598 self.posteriorfiles = array([k.replace('_B.txt','') for k in self.bayesfiles])
599 elif self.engine == 'lalinference':
600 self.posteriorfiles = array([k.replace('_B.txt','') for k in self.bayesfiles])
601
602 # EXTRACT GPS TIMES
603 if self.engine == 'lalnest':
604 self.gps=array([k.split('_')[1].split('.')[0] for k in self.bayesfiles])
605 elif self.engine == 'lalinference':
606 self.gps=array([k.split('_')[2].split('-')[0] for k in self.bayesfiles])
607 #self.gps=hstack((self.gps,gpstmp))
608
609 # GET SNR FILES
610 #snrfilename = "snr_%s_%.1f.dat"%(self.bayesfiles[k].split('_')[1],int(self.gps[k]))
611 #self.snrfiles = array(["snr_%s_%.1f.dat"%(self.bayesfiles[k].split('_')[1],int(self.gps[k])) for k in range(len(self.bayesfiles))])
612 #self.snrfiles = array(["snr_%s_%.1f.dat"%(x,int(y)) for x,y in zip(self.detectors, self.gps)])
613 self.snrfiles = array([k.replace('_','-').replace('-B.txt','_snr.txt').replace('posterior','lalinferencenest-%s'%(k.split('-')[1].split('.')[0])).replace('%s'%(k.split('-')[0].split('_')[-1]),'%.1f'%(float(k.split('-')[0].split('_')[-1]))) for k in self.bayesfiles])
614 #self.snrfiles = array(["snr_H1L1V1_"+item+".0.dat" for item in self.gps])
615
616 self.nsources = len(self.bayesfiles)
617 stdout.write("... searching sources: %s\t%s - %d sources\n" % (self.cluster,self.directory, self.nsources))
618 #print "... ", self.cluster,self.directory, " - ", self.nsources, " sources found"
619
620 def pullbayes(self):
621 """
622 Download Bayes factors from remote location. Only works after sources are
623 found by the searchsources function.
624 """
625 stdout.write("... pulling Bayes factors: %s\t%s\r" % (self.cluster,self.directory))
626 stdout.flush()
627 if self.nsources == 0:
628 stdout.write("... pulling Bayes factors: %s\t%s - nothing to fetch\n" % (self.cluster,self.directory))
629 else:
630 # new command
631 files=""
632 for item in self.bayesfiles:
633 files+=" "+self.directory+"{"+",".join(self.subhyp)+"}"
634 if self.engine == 'lalnest':
635 files += "/nest/"
636 elif self.engine == 'lalinference':
637 files += "/posterior_samples/"
638 files += item
639 nsplit = 3 if self.nsources > 1000 else 1
640 for fsplit in array_split(files.split(),nsplit):
641 command="%s %s '%s %s'"%("gsissh -C",clusters[self.cluster],"cat"," ".join(fsplit)) if self.cluster in clusters.keys() else "%s %s"%("cat"," ".join(fsplit))
642 p = Popen(command, stdout=PIPE, stderr=PIPE,shell=True)
643 self.bayes.extend(loadtxt(p.stdout, usecols=[0]))
644 self.bayes = reshape(self.bayes, (self.nsources,len(self.subhyp)))
645 savetxt('test.dat',self.bayes);
646 stdout.write("... pulling Bayes factors: %s\t%s - %d x %d dimension\n" % (self.cluster,self.directory, shape(self.bayes)[0],shape(self.bayes)[1]))
647
648 def pullsnr(self):
649 """
650 Download SNRs from remote location. Only works after sources are found by
651 the searchsources function.
652 """
653 stdout.write("... pulling SNRs: %s\t%s\n" % (self.cluster,self.directory))
654 # GET SNRS
655 if self.nsources == 0:
656 stdout.write("... pulling SNRs: %s\t%s - nothing to fetch\n" % (self.cluster,self.directory))
657 else:
658 command = ""
659 if self.cluster != 'local':
660 command+="gsissh -C "+clusters[self.cluster]+" '"
661 command+="cat"
662 snrfilescomma=",".join(self.snrfiles)
663 command += " "+self.directory+self.subhyp[0]+"/engine/"+"{"+snrfilescomma+"}"
664 if self.cluster != 'local':
665 command+="'"
666 p = Popen(command, stdout=PIPE, stderr=PIPE,shell=True)
667 #self.snr = array([float(k.strip('Network:').strip()) for k in p.stdout.readlines() if k.find('Network')!=-1])
668 snrrawdata = p.stdout.readlines()
669 if snrrawdata == []:
670 stdout.write("... Warning: Cannot find SNR file. Writing zeros\n")
671 self.snr = zeros(self.nsources)
672 return
673 count = 0
674 for k in range(len(self.gps)):
675 ndet = len(self.detectors[k])/2
676 if ndet == 1:
677 self.snr.append(float(snrrawdata[count+ndet-1].strip('%s:'%(self.detectors[k])).strip()))
678 count+=1
679 else:
680 self.snr.append(float(snrrawdata[count+ndet].strip('Network:').strip()))
681 count+=ndet+1
682 self.snr = array(self.snr)
683 stdout.write("... pulling SNRs: %s\t%s - %d sources\n" % (self.cluster,self.directory, len(self.snr)))
684
685 def pullposteriors(self):
686 """
687 Download posterior pdfs from remote location. Only works after sources are
688 found by the searchsources function.
689 NB: NOT READY FOR USE YET!
690 """
691 if self.nsources == 0:
692 print("Nothing to fetch")
693 else:
694 # FIND THE NUMBER OF LINE FIRST SO THAT ONE CAN DIFFERENTIATE FILES
695 command = ""
696 if self.cluster != 'local':
697 command+="gsissh -C "+clusters[self.cluster]+" '"
698 command+="wc -l"
699 posteriorfilescomma=",".join(self.posteriorfiles)
700 command += " "+self.directory+self.subhyp[0]+"/nest/"+"{"+posteriorfilescomma+"}"
701 if self.cluster != 'local':
702 command+="'"
703 p = Popen(command, stdout=PIPE, stderr=PIPE,shell=True)
704 posteriorfilesizes = loadtxt(p.stdout, usecols=[0])[:-1]
705 print(shape(posteriorfilesizes))
706
707 # PULLING THE POSTERIOR SAMPLES
708 command = ""
709 if self.cluster != 'local':
710 command+="gsissh -C "+clusters[self.cluster]+" '"
711 command+="cat"
712 posteriorfilescomma=",".join(self.posteriorfiles)
713 command += " "+self.directory+self.subhyp[0]+"/nest/"+"{"+posteriorfilescomma+"}"
714 if self.cluster != 'local':
715 command+="'"
716 p = Popen(command, stdout=PIPE, stderr=PIPE,shell=True)
717 self.posteriorsamples = loadtxt(p.stdout)
718 print(shape(self.posteriorsamples))
719
720
721 def applycut(self, st=8.0, bt=32.0):
722 """
723 Apply a Bayes factor and SNR cut on the data
724 """
725 # SET CONDITION
726 cond = ((self.snr>st) | (self.snr==0.0)) & (self.bayes[:,0]>bt)
727
728 # UPDATE DATA
729 self.bayes = self.bayes[cond,:]
730 self.snr = self.snr[cond]
731 self.gps = self.gps[cond]
732 self.nsources=shape(self.bayes)[0]
733
734 def savetopickle(self,dest):
735 """
736 Pull data from remote or local locations
737 """
738
739 # SAVING TO PICKLE
740 f = open(dest,'wb')
741 dump(self,f,2)
742
743class TigerSet:
744 """
745 CLASS TO CONTAIN A SET OF RUNS UNDER A COMMON CHARACTERISTIC (E.G.
746 BACKGROUND, FOREGROUND).
747 """
748 def __init__(self,locations,label,latexlabel,testcoef,engine):
749 """
750 INITIALISATION OF CLASS
751 """
752
753 # CALCULATING SUBHYPOTHESES
754 self.testcoef = testcoef # testing coefficients
755 self.subhyp=[]
756 for i in range(len(self.testcoef)):
757 s = combinations(testcoef, i+1)
758 self.subhyp.extend(["".join(j) for j in s])
759 self.subhyp.insert(0,'GR')
760 self.nsubhyp = len(self.subhyp)
761
762 # SET ENGINE
763 if engine in ['lalinference','lalnest']: # engine: either lalnest or lalinference
764 self.engine = engine
765 else:
766 exit('Engine not recognised')
767
768 # CREATE LOCATIONS
769 self.locations = [TigerRun(loc[0].lower(),loc[1],self.engine,self.subhyp) for loc in locations if loc[0].lower() in clusters.keys()+['local']]
770
771 # SET LABELS
772 self.label = label
773 self.latexlabel = latexlabel
774
775 # DATA STRUCTURES TO BE FILLED USING FUNCTIONS
776 self.nsources=0
777 self.bayes = empty((0,len(self.subhyp)))
778 self.snr = empty(0)
779
780 def searchsources(self):
781 """
782 FIND COMPLETED JOBS AND GET THE FILESNAMES
783 """
784
785 for loc in self.locations:
786 loc.searchsources()
787 self.nsources += loc.nsources
788
789 stdout.write("--> searching sources: Total number of sources - %d\n" % self.nsources)
790
791 def pullbayes(self):
792 """
793 PULL DATA FROM REMOTE OR LOCAL LOCATIONS
794 """
795
796 for loc in self.locations:
797 if loc.nsources > 0:
798 loc.pullbayes()
799 self.bayes=vstack((self.bayes,loc.bayes))
800 stdout.write("--> pulling Bayes factors: matrix size - %d x %d\n" % (shape(self.bayes)[0], shape(self.bayes)[1]))
801
802 def pullsnr(self):
803 """
804 PULL DATA FROM REMOTE OR LOCAL LOCATIONS
805 """
806 for loc in self.locations:
807 if loc.nsources > 0:
808 loc.pullsnr()
809 self.snr=hstack((self.snr,loc.snr))
810 stdout.write("--> pulling SNRs: total number of sources %d\n" % (len(self.snr)))
811
812 def pullposteriors(self):
813 """
814 PULLING THE POSTERIOR FILES
815 """
816 for loc in self.locations:
817 if loc.nsources > 0:
818 loc.pullposteriors()
819
820 def odds(self,N=1):
821 """
822 CALCULATE THE TOTAL ODDS FROM DATA STRUCTURE
823 """
824 odds = empty(0)
825 for i in range(self.nsources/N):
826 odds = append(odds,OddsFromBayes(self.bayes[i*N:(i+1)*N,:], len(self.testcoef)))
827 return odds
828
829 def applycut(self, st=8.0,bt=32.0):
830 """
831 APPLY DETECTION CUTS (SNR AND BAYESGR)
832 """
833 stdout.write("... %s - before cut: %d sources\n" % (self.label,self.nsources))
834
835 for loc in self.locations:
836 if loc.nsources > 0:
837 loc.applycut(st=st, bt=bt)
838
839 # RELOAD BAYES, SNR AND GPS
840 self.bayes = empty((0,len(self.subhyp)))
841 self.snr = empty(0)
842 for loc in self.locations:
843 if loc.nsources > 0:
844 self.bayes=vstack((self.bayes,loc.bayes))
845 self.snr=hstack((self.snr,loc.snr))
846 self.nsources=shape(self.bayes)[0]
847
848 stdout.write("... %s - after cut: %d sources\n" % (self.label,self.nsources))
849
850 def shufflesources(self,seed):
851 """
852 APPLY DETECTION CUTS (SNR AND BAYESGR)
853 """
854
855 print("... Shuufling with seed", str(seed))
856
857 # CREATE NEW ORDER OF SOURCES
858 if (self.nsources != 0) and (self.nsources == shape(self.bayes)[0]):
859 order = arange(self.nsources)
860 random.seed(seed)
861 random.shuffle(order)
862
863 # UPDATE DATA
864 self.bayes = self.bayes[order,:]
865 self.snr = self.snr[order]
866
867 def savetopickle(self,dest):
868 """
869 SAVE DATA TO PICKLE FOR QUICK FUTURE LOADING
870 """
871
872 # SAVING TO PICKLE
873 f = open(dest,'wb')
874 dump(self,f,2)
875
876 def savetoascii(self,filename):
877 """
878 SAVE DATA TO ASCII FILE FOR QUICK FUTURE LOADING.
879 NB: BROKEN - DO NOT USE!
880 """
881 # PRINT DATA FOR FUTURE USE
882 savedata = empty((0,4+len(self.subhyp))) # cluster, directory, gps, bayes, ...., netsnr
883 for loc in self.locations:
884 if loc.nsources > 0:
885 locsdata = column_stack(([loc.cluster]*loc.nsources, [loc.directory]*loc.nsources))
886 savedatatmp = column_stack((locsdata,loc.gps,loc.bayes,loc.snr))
887 savedata = vstack((savedata,savedatatmp))
888 header = array(concatenate((["#cluster",'folder','gps'],self.subhyp,['netsnr'])))
889 savedata = vstack((header,savedata))
890 savetxt(filename,savedata,delimiter='\t',fmt='%s')
891
892 def preprocess(self):
893 """
894 PREPROCESS DATA ON THE CLUSTERS BEFORE SENDING DATA TO MASTER.
895 NB: UNDER DEVELOPMENT
896 """
897 for loc in self.locations:
898 # CREATE PREPROCESSFILE
899 print('Writing preprocessfile')
900 preprocesstmp = 'tig_preprocess_tmp.txt'
901 preprocessfp = open(preprocesstmp,'w')
902 preprocessfp.write('local')
903 preprocessfp.write('\t')
904 preprocessfp.write(loc.directory)
905 preprocessfp.write('\t')
906 preprocessfp.write(self.engine)
907 preprocessfp.write('\t')
908 preprocessfp.write(str(loc.subhyp))
909 preprocessfp.close()
910 print('Writing preprocessfile - done')
911
912 # UPLOAD FILE AND RUN: TESTING PHASE
913 command = ""
914 command += "gsiscp -C"
915 command += " "
916 command += scriptfilename
917 command += " "
918 command += preprocesstmp
919 command += " "
920 command += clusters[loc.cluster]+":~/"
921 print(command)
922 p = Popen(command, stdout=PIPE, stderr=PIPE,shell=True)
923 p.communicate()
924 if p.returncode == 0:
925 command = "gsissh "+clusters[loc.cluster]+" 'python "+path.basename(scriptfilename)+" -p "+preprocesstmp+"'"
926 print(command)
927 p = Popen(command, stdout=PIPE, stderr=PIPE,shell=True)
928 print(p.stdout.read())
929 print(p.stderr.read())
930 exit(0)
931
932
933###############################################################################
934#
935# FUNCTIONS
936#
937###############################################################################
938
939def LoadPickledTigerSet(filename):
940 """
941 LOAD FROM PICKLE
942 """
943 print('... Loading file from pickle',filename)
944 fp = open(filename,'rb')
945 return load(fp)
946
947def ensure(f):
948 """
949 CREATE DIRECTORY IF IT DOES NOT EXIST
950 """
951 if not path.exists(f):
952 makedirs(f)
953
954def lnPLUS(x,y):
955 """
956 FUNCTION TO ADD IN LOGARITHMIC SPACE
957 """
958
959 output = 0.0
960 if x>y:
961 output = x+log(1.0+exp(y-x))
962 else:
963 output = y+log(1.0+exp(x-y))
964
965 return output
966
967def OddsFromBayes(matrix, Nhyp):
968 """
969 CALCULATE THE TOTAL ODDS FROM DATA STRUCTURE
970 """
971
972 TotalOdds = -float_info.max
973 # ONE SMALLER THAN THE WIDTH OF THE MATRIX FOR GR
974 for j in range(shape(matrix)[1]-1):
975 TotalOdds = lnPLUS(TotalOdds, sum(matrix[:, j+1]-matrix[:, 0]))
976 TotalOdds -= log(pow(2,Nhyp)-1)
977
978 return TotalOdds
979
980def TigerCreateHistogram(list_tigerruns, axis, N=1, xlim=None,bins=50):
981 """
982 CREATE TIGER HISTOGRAMS FROM A LIST OF TIGERRUNS
983 """
984 for i in range(len(list_tigerruns)):
985 if N == 1:
986 # PLOT HISTOGRAMS
987 axis.hist(list_tigerruns[i].odds(N), \
988 facecolor="none", \
989 label=list_tigerruns[i].latexlabel+r"\n$\mathrm{("+str(size(list_tigerruns[i].odds(N)))+r"\; sources)}$", \
990 histtype="stepfilled", \
991 linewidth=1, \
992 range=xlim, \
993 bins = bins, \
994 hatch = hatch_default[i], \
995 edgecolor = color_default[i], \
996 density=True)
997 # ADD X-AXIS LABEL
998 axis.set_xlabel(r"$\ln{O_{GR}^{modGR}}$")
999 axis.set_ylabel(r"$P(\ln{O_{GR}^{modGR}})$")
1000 elif N < list_tigerruns[i].nsources:
1001 # PLOT HISTOGRAMS
1002 axis.hist(list_tigerruns[i].odds(N), \
1003 facecolor="none", \
1004 label=list_tigerruns[i].latexlabel+r"\n$\mathrm{("+str(size(list_tigerruns[i].odds(N)))+r"\; catalogs)}$", \
1005 histtype="stepfilled", \
1006 linewidth=1, \
1007 range=xlim, \
1008 bins = bins, \
1009 hatch = hatch_default[i], \
1010 edgecolor = color_default[i], \
1011 density=True)
1012 # ADD X-AXIS LABEL
1013 axis.set_xlabel(r"$\ln{\mathcal{O}_{GR}^{modGR}}$")
1014 axis.set_ylabel(r"$P(\ln{\mathcal{O}_{GR}^{modGR}})$")
1015
1016 # ADD LEGEND
1017 handles, labels = axis.get_legend_handles_labels()
1018 axis.legend(handles, labels, loc='upper left')
1019
1020def TigerCalculateEfficiency(list_tigerruns, N=1, beta=[0.95], background=0):
1021 """
1022 CALCULATE EFFICIENCY FROM A LIST OF TIGERRUNS
1023 """
1024 efficiencies = []
1025 OddsBeta=[mquantiles(list_tigerruns[background].odds(N),prob=[b]) for b in beta]
1026 efficiencies = empty((len(list_tigerruns)-1,len(beta)))
1027 for i in range(len(list_tigerruns)):
1028 if N>list_tigerruns[i].nsources:
1029 stdout.write("... Warning: Not sufficient events (%s) to calculate the efficiency for %s sources. Writing zeros\n"%(list_tigerruns[i].nsources,N))
1030 if i < background:
1031 efficiencies[i,:] = 0.0
1032 else:
1033 efficiencies[i-1,:] = 0.0
1034 continue
1035 if i != background:
1036 tmp = list_tigerruns[i].odds(N)
1037 for j in range(len(OddsBeta)):
1038 msk = tmp>OddsBeta[j]
1039 nmsk = tmp<OddsBeta[j]
1040 nabovebeta=len(tmp[msk])
1041 ntotal=len(tmp)
1042 eff=float(nabovebeta)/float(ntotal)
1043 if i < background:
1044 efficiencies[i,j] = eff
1045 else:
1046 efficiencies[i-1,j] = eff
1047 return efficiencies
1048
1049def TigerKSandPvalue(tigerrun1,tigerrun2, N=1):
1050 """
1051 CALCULATE KS STATISTICS FROM A LIST OF TIGERRUNS
1052 """
1053 if tigerrun1.nsources < N or tigerrun2.nsources < N:
1054 stdout.write("... Warning: Not sufficient events (%s,%s) to calculate KS-statistic for %s sources. Writing zeros\n"%(tigerrun1.nsources, tigerrun2.nsources,N))
1055 return [0.0,0.0]
1056 data1 = tigerrun1.odds(N)
1057 data2 = tigerrun2.odds(N)
1058 ksstat, pvalue = ks_2samp(data1,data2)
1059 return [ksstat, pvalue]
1060
1061
1062def TigerCreateSNRvsOdds(list_tigerruns, axis):
1063 """
1064 CREATE SNR VS ODDS DIAGRAMS FROM A LIST OF TIGERRUNS
1065 """
1066 markers = ['x','o']
1067 colors = ['blue','red']
1068 axis.set_xlabel(r"$\mathrm{SNR}$")
1069 axis.set_ylabel(r"$\ln{O_{\mathrm{GR}}^{\mathrm{modGR}}}$")
1070
1071 for i in range(len(list_tigerruns)):
1072 axis.scatter(list_tigerruns[i].snr, list_tigerruns[i].odds(), \
1073 color=colors[i], \
1074 marker=markers[i], \
1075 label=list_tigerruns[i].latexlabel+r"\n$\mathrm{("+str(list_tigerruns[i].nsources)+r"\; sources)}$",\
1076 s=64)
1077
1078 # SET AXIS LIMITS
1079 axis.set_xlim(5.0,50.0)
1080
1081 # ADD LEGEND
1082 handles, labels = axis.get_legend_handles_labels()
1083 axis.legend(handles, labels, loc='best')
1084
1085 return None
1086
1087def TigerCreateCumFreq(tigerrun, axis):
1088 """
1089 CREATE CUMULATIVE FREQUENCY DIAGRAMS FROM A LIST OF TIGERRUNS
1090 """
1091 xlim = [5.0,100.0]
1092 snrrange = arange(xlim[0], xlim[1], 5.)
1093 nsnrsteps = len(snrrange)
1094 freqs = zeros((nsnrsteps,tigerrun.nsubhyp))
1095
1096 for i in range(nsnrsteps):
1097 if snrrange[i] > min(tigerrun.snr):
1098 snrmask = tigerrun.snr<snrrange[i]
1099 bayesmasked = tigerrun.bayes[snrmask,:]
1100 maxarray = list(argmax(bayesmasked, axis=1))
1101 for j in range(tigerrun.nsubhyp):
1102 freqs[i,j] = maxarray.count(j)
1103
1104 # CHECK IF TOTAL ADDS UP TO TOTAL NUMBER OF SOURCES
1105 if sum(freqs[-1,:]) != tigerrun.nsources:
1106 print(r"Warning, some sources maybe out of the SNR range and are not plotted: SNR \in [%.1f:%.1f], %i sources missing" %(xlim[0], xlim[1], tigerrun.nsources-sum(freqs[-1,:])))
1107
1108 # PLOT CUMULATIVE HIGHEST BAYES SIGNAL VERSUS NOISE
1109 rcParams['legend.fontsize']=18 # MANUAL RESET FONTSIZE
1110 linestyles = cycle(['-', '--', '-.', ':'])
1111 markerstyles = cycle(['+','*','.','<','d', '^', 'x', 's'])
1112 colorstyles = cycle(["b", "g","r","c","m","y","k"])
1113
1114 for i in range(tigerrun.nsubhyp):
1115 if tigerrun.subhyp[i].split("_")[-1] =="GR":
1116 axis.plot(snrrange, freqs[:,i], label=r'$\mathcal{H}_{\mathrm{GR}}$', color="black", linewidth=3.0)
1117 else:
1118 if tigerrun.engine == 'lalnest':
1119 curlabel = "$H_{"+''.join(tigerrun.subhyp[i].split("_")[-1].split('dphi'))+"}$"
1120 elif tigerrun.engine == 'lalinference':
1121 curlabel="$H_{"+''.join(tigerrun.subhyp[i].split("_")[-1].split('dchi'))+"}$"
1122 axis.plot(snrrange, freqs[:,i], \
1123 label=curlabel,\
1124 marker=markerstyles.next(), \
1125 linestyle='--', \
1126 linewidth=1.0, \
1127 color=colorstyles.next())
1128
1129 axis.set_xlabel(r"$\mathrm{SNR}$")
1130 axis.set_ylabel(r"$\mathrm{Cumulative\;frequency\;(lines)}$")
1131 axis.set_xlim(xlim)
1132
1133 # ADD LEGEND
1134 handles, labels = axis.get_legend_handles_labels()
1135 axis.legend(handles, labels, loc='best',ncol=3)
1136
1137 # DRAW HISTOGRAM
1138 ax2 = axis.twinx()
1139 ax2.hist(tigerrun.snr, range=xlim, bins=nsnrsteps, alpha=0.25, color="k")
1140 ax2.grid(False)
1141 ax2.set_ylabel(r'$\mathrm{\#\;of\;sources\;per\;SNR\;bin\;(hist)}$')
1142 ax2.set_xlim(xlim)
1143
1144def TigerCreateCumBayes(tigerrun,axis,nthcat,nsourcespercat):
1145 """
1146 CREATE CUMULATIVE BAYES FACTOR/ODDS DIAGRAMS FROM A LIST OF TIGERRUNS (FOR
1147 EACH CATALOG)
1148 """
1149
1150 # COUNT CUMULATIVE FREQUENCY OF HIGHEST BAYES (SIGNAL VS. NOISE)
1151 snrcat = tigerrun.snr[nthcat*nsourcespercat:(nthcat+1)*nsourcespercat]
1152 bayescat = tigerrun.bayes[nthcat*nsourcespercat:(nthcat+1)*nsourcespercat,:]
1153 sortorder = argsort(snrcat)
1154 snrcatsorted = sort(snrcat)
1155 bayescatsorted = bayescat[sortorder,:]
1156 cumodds = array([OddsFromBayes(bayescatsorted[:i+1,:],len(tigerrun.testcoef)) for i in range(nsourcespercat)])
1157 cumbayes = array([sum(subtract(bayescatsorted[:i+1,1:].T,bayescatsorted[:i+1,0]).T,axis=0) for i in range(nsourcespercat)])
1158
1159 markerstyles = cycle(['+','*','.','<','d', '^', 'x', 's'])
1160
1161 axis.set_xlabel(r"$\mathrm{SNR}$")
1162 axis.set_ylabel(r"$\ln{{}^{(cat)}B^{i_1 i_2 \ldots i_k}_{\mathrm{GR}}}$")
1163
1164 # PLOT BAYESFACTORS
1165 for i in range(tigerrun.nsubhyp-1):
1166
1167 if tigerrun.engine == 'lalnest':
1168 curlabel = "$H_{"+''.join(tigerrun.subhyp[i+1].split("_")[-1].split('dphi'))+"}$"
1169 elif tigerrun.engine == 'lalinference':
1170 curlabel="$H_{"+''.join(tigerrun.subhyp[i+1].split("_")[-1].split('dchi'))+"}$"
1171
1172 axis.plot(snrcatsorted, cumbayes[:,i],\
1173 label=curlabel, \
1174 marker=markerstyles.next(), \
1175 linestyle='--', \
1176 linewidth=1.0)
1177
1178 # PLOT ODDS RATIO
1179 axis.plot(snrcatsorted, cumodds, label=r"$\ln{O_{GR}^{modGR}}$", linewidth=2.0, color="black")
1180
1181 axis.set_xlim(5.0,50.0)
1182 # ADD LEGEND
1183 handles, labels = axis.get_legend_handles_labels()
1184 axis.legend(handles, labels, loc='best',ncol=3)
1185
1186def TigerCreateExampleConfigFile(filename):
1187 """
1188 CREATE EXAMPLE CONFIGURATION FILE (TO BE USED WITH -G OPTION)
1189 """
1190 basename = path.basename(filename)
1191 dirname = path.dirname(filename)
1192 if dirname == '':
1193 dirname = './'
1194
1195 # CREATE DIRECTORY IF IT DOES NOT EXIST
1196 ensure(dirname)
1197
1198 # OPEN FILE
1199 outfile = open(path.join(dirname,basename),'w')
1200
1201 examplecfgtxt="""\
1202###############################################################################
1203#
1204# Configuration file for tiger_postproc.py
1205#
1206###############################################################################
1207
1208#------------------------------------------------------------------------------
1209[run]
1210#------------------------------------------------------------------------------
1211
1212# Engine for TIGER runs: lalnest or lalinference
1213engine=engine1,engine2
1214
1215# Subhypotheses: Notation depends on engine.
1216# lalnest: dphi1,dphi2,...
1217# lalinference: dchi1,dchi2,...
1218hypotheses=dphi1,dphi2,dphi3
1219
1220# Run identifier (used in filenames for plots)
1221runid=name_for_run
1222
1223# Local destination folder for postproc files: created if non-existing
1224localdest=/home/user/public_html/tiger/inj_tt4spin_tt4all_rec_tf2spin/
1225
1226#------------------------------------------------------------------------------
1227[fetch]
1228#------------------------------------------------------------------------------
1229
1230# Fetch data from (one per type of runs)
1231# - source : directly from output of engine
1232# - pickle : saved state
1233type=source,source
1234
1235# Location files.(one per type of runs)
1236# source: ascii file two columns: cluster directory
1237# cluster: (atlas, nemo, cit, lho, llo, or local)
1238# pickle: location to python pickle file
1239locs=/path/to/the/location/file.txt,/path/to/the/location/file.txt
1240
1241# Labels (one for each type of runs)
1242labels=tt4spin,tt4all
1243
1244# Latex labels for different runs for plot legends (one for each run type).
1245# Currently not working with commas in the latex itself!
1246latexlabels=$\\mathrm{TaylorT4+spin}$,$\\mathrm{TaylorT4+all}$
1247
1248# Seed for shuffling the sources. seed=0 means no shuffling
1249# NB: NOT WORKING!
1250seed=0
1251
1252#------------------------------------------------------------------------------
1253[cut]
1254#------------------------------------------------------------------------------
1255
1256# SNR threshold
1257snrthres=8.0
1258
1259# B^GR_noise cutoff
1260bayesgrthres=32.0
1261
1262#------------------------------------------------------------------------------
1263[plot]
1264#------------------------------------------------------------------------------
1265
1266# Plot histograms (see [hist] for plotting options)
1267hist=true
1268
1269# Plot SNR vs odds scatter plot (see [snrvsodds] for plotting options)
1270snrvsodds=true
1271
1272# Plot cumulative frequency of a subhypothesis having the highest Bayes factor
1273# (see [cumfreq] for plotting options)
1274cumfreq=true
1275
1276# Plot cumulative Bayes factor, per subhypothesis, and the odds ratio, as a
1277# function of SNR, per catalog. (see [cumbayes] for plotting options).
1278cumbayes=false
1279
1280# File output extensions: png required for html page
1281# Other supported formats: pdf, ps, eps, svg
1282types=png,pdf
1283
1284#------------------------------------------------------------------------------
1285[hist]
1286#------------------------------------------------------------------------------
1287
1288# Number of bins in the histograms
1289bins=50
1290
1291# Number of sources per catalog for the histograms
1292nsources=1,15
1293
1294# Limits on the x-axis. Use xlim=0,0 for automatic determination (range will be
1295# the same for ALL sizes of the catalog).
1296xlim=0,0
1297
1298#------------------------------------------------------------------------------
1299[snrvsodds]
1300#------------------------------------------------------------------------------
1301
1302
1303#------------------------------------------------------------------------------
1304[cumbayes]
1305#------------------------------------------------------------------------------
1306
1307"""
1308
1309 outfile.write(examplecfgtxt)
1310
1311def TigerPreProcess(preprocessfile):
1312 """
1313 TIGER PREPROCESSOR FUNCTION
1314 NB: UNDER DEVELOPMENT
1315 """
1316 if access(preprocessfile, R_OK):
1317 preprocessfp = open(preprocessfile,'r')
1318 args = preprocessfp.read().split('\t')
1319 subhyp= args[3][1:-1].replace("'","").replace(" ","").split(',')
1320 tgloc = TigerRun(args[0].lower(),args[1],args[2],subhyp)
1321 tgloc.searchsources()
1322 tgloc.pullbayes()
1323 tgloc.pullposteriors()
1324 tgloc.savetopickle('tigerloc_preprocess.pickle')
1325 exit(0)
1326 else:
1327 exit('Preprocess file cannot be found - abort')
1328
1329###############################################################################
1330#
1331# START THE MAIN FUNCTION
1332#
1333###############################################################################
1334
1335if __name__ == "__main__":
1336 # START THE MAIN FUNCTION IF RUN AS A SCRIPT. OTHERWISE, JUST LOADS THE CLASS
1337 # AND FUNCTION DEFINITIONS
1338 exit(main())
#define max(a, b)
TIGERRUN CLASS DEFINITIONS.
Definition: postproc.py:534
def pullposteriors(self)
Download posterior pdfs from remote location.
Definition: postproc.py:690
def __init__(self, cluster, directory, engine, subhyp)
Initialisation of class.
Definition: postproc.py:539
def pullbayes(self)
Download Bayes factors from remote location.
Definition: postproc.py:624
def savetopickle(self, dest)
Pull data from remote or local locations.
Definition: postproc.py:737
def searchsources(self)
Search for sources completed sources in the specified cluster and location.
Definition: postproc.py:560
def pullsnr(self)
Download SNRs from remote location.
Definition: postproc.py:652
def applycut(self, st=8.0, bt=32.0)
Apply a Bayes factor and SNR cut on the data.
Definition: postproc.py:724
CLASS TO CONTAIN A SET OF RUNS UNDER A COMMON CHARACTERISTIC (E.G.
Definition: postproc.py:747
def pullbayes(self)
PULL DATA FROM REMOTE OR LOCAL LOCATIONS.
Definition: postproc.py:794
def savetopickle(self, dest)
SAVE DATA TO PICKLE FOR QUICK FUTURE LOADING.
Definition: postproc.py:870
def savetoascii(self, filename)
SAVE DATA TO ASCII FILE FOR QUICK FUTURE LOADING.
Definition: postproc.py:880
def applycut(self, st=8.0, bt=32.0)
APPLY DETECTION CUTS (SNR AND BAYESGR)
Definition: postproc.py:832
def __init__(self, locations, label, latexlabel, testcoef, engine)
INITIALISATION OF CLASS.
Definition: postproc.py:751
def searchsources(self)
FIND COMPLETED JOBS AND GET THE FILESNAMES.
Definition: postproc.py:783
def shufflesources(self, seed)
APPLY DETECTION CUTS (SNR AND BAYESGR)
Definition: postproc.py:853
def preprocess(self)
PREPROCESS DATA ON THE CLUSTERS BEFORE SENDING DATA TO MASTER.
Definition: postproc.py:896
def pullsnr(self)
PULL DATA FROM REMOTE OR LOCAL LOCATIONS.
Definition: postproc.py:805
def odds(self, N=1)
CALCULATE THE TOTAL ODDS FROM DATA STRUCTURE.
Definition: postproc.py:823
def pullposteriors(self)
PULLING THE POSTERIOR FILES.
Definition: postproc.py:815
def TigerCreateExampleConfigFile(filename)
CREATE EXAMPLE CONFIGURATION FILE (TO BE USED WITH -G OPTION)
Definition: postproc.py:1189
def TigerPreProcess(preprocessfile)
TIGER PREPROCESSOR FUNCTION NB: UNDER DEVELOPMENT.
Definition: postproc.py:1315
def main()
PLOTTING OPTIONS.
Definition: postproc.py:93
def lnPLUS(x, y)
FUNCTION TO ADD IN LOGARITHMIC SPACE.
Definition: postproc.py:957
def TigerCalculateEfficiency(list_tigerruns, N=1, beta=[0.95], background=0)
CALCULATE EFFICIENCY FROM A LIST OF TIGERRUNS.
Definition: postproc.py:1023
def TigerKSandPvalue(tigerrun1, tigerrun2, N=1)
CALCULATE KS STATISTICS FROM A LIST OF TIGERRUNS.
Definition: postproc.py:1052
def TigerPostProcess(configfile)
Main TIGER post processing function.
Definition: postproc.py:139
def TigerCreateCumBayes(tigerrun, axis, nthcat, nsourcespercat)
CREATE CUMULATIVE BAYES FACTOR/ODDS DIAGRAMS FROM A LIST OF TIGERRUNS (FOR EACH CATALOG)
Definition: postproc.py:1148
def OddsFromBayes(matrix, Nhyp)
CALCULATE THE TOTAL ODDS FROM DATA STRUCTURE.
Definition: postproc.py:970
def ensure(f)
CREATE DIRECTORY IF IT DOES NOT EXIST.
Definition: postproc.py:950
def TigerCreateCumFreq(tigerrun, axis)
CREATE CUMULATIVE FREQUENCY DIAGRAMS FROM A LIST OF TIGERRUNS.
Definition: postproc.py:1090
def LoadPickledTigerSet(filename)
LOAD FROM PICKLE.
Definition: postproc.py:942
def TigerCreateHistogram(list_tigerruns, axis, N=1, xlim=None, bins=50)
CREATE TIGER HISTOGRAMS FROM A LIST OF TIGERRUNS.
Definition: postproc.py:983
def TigerCreateSNRvsOdds(list_tigerruns, axis)
CREATE SNR VS ODDS DIAGRAMS FROM A LIST OF TIGERRUNS.
Definition: postproc.py:1065