2Postprocessing for the tiger pipeline
5__author__ =
"Tjonnie Li"
6__credits__ = [
"Tjonnie Li"]
7__maintainer__ =
"Tjonnie Li"
8__email__ =
"tjonnie.li@ligo.org"
9__status__ =
"Production"
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'}
23color_default = [
'blue',
'red',
'green']
24hatch_default = [
'..',
'//',
'||']
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
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
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
49py_version = version_info[:2]
50if py_version < (2, 7):
51 from optparse
import OptionParser
53 from argparse
import ArgumentParser
62inches_per_pt = 1.0/72.27
63golden_mean = (sqrt(5)-1.0)/2.0
64fig_width = fig_width_pt*inches_per_pt
65fig_height = fig_width*golden_mean
66fig_size = [fig_width,fig_height]
68fontsize =
'font.size' if mpl_version >=
'1.5.0' else 'text.fontsize'
69params = {
'backend':
'PDF',
72 'legend.fontsize': 20,
73 'xtick.labelsize': 24,
74 'ytick.labelsize': 24,
78 'lines.markersize' : 14,
79 'figure.figsize': fig_size}
81rcParams.update(params)
83scriptfilename = path.realpath(__file__)
92 -------------------------------------------------------------------------------
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()
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()
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)
127 elif (args.examplecfg !=
None):
129 elif (args.preprocessfile !=
None):
132 exit(
'Unknown options - check input')
137 Main TIGER post processing function. Calls the relevant function for source
138 collection, odds ratio calculations, and plotting
142 stdout.write(
"Configuration file: checking file\n")
143 if access(configfile, R_OK):
144 config = ConfigParser()
145 config.read(configfile)
147 exit(
"Configuration file: checking file - file does not exist. Abort script\n")
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(
',')
159 latexlabels_str = config.get(
'fetch',
'latexlabels')
160 matches=findall(
r'\$(.+?)\$',latexlabels_str)
161 latexlabels=[
"$%s$"%s
for s
in matches]
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')
183 for i
in range(len(fetch_type)):
184 if fetch_type[i] ==
'source':
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])
190 tigerruns.append(
TigerSet(locations, labels[i],latexlabels[i],testcoef,engine[i]))
192 tigerruns[-1].searchsources()
193 tigerruns[-1].pullbayes()
194 tigerruns[-1].pullsnr()
196 elif fetch_type[i] ==
'pickle':
198 tigerruns[-1].latexlabel=latexlabels[i]
200 exit(
'Fetch has to be either source or pickle. Check the configuration file - Abort')
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])
214 stdout.write(
"Applying cuts\n")
216 tg.applycut(st=fetch_snrthres, bt=fetch_bayesgrthres)
238 stdout.write(
"Producing plots\n")
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')
248 html_data_efficiencies = []
253 html_data_efficiencies.append(tmp)
254 html_data_efficiencies = array(html_data_efficiencies)
256 html_data_ks =[[
TigerKSandPvalue(tigerruns[0],j,N=ns)
for ns
in N_sources]
for j
in tigerruns[1:]
if len(tigerruns)>1]
263 stdout.write(
"... Histograms\n")
265 hist_xlim = [float(item)
for item
in config.get(
'hist',
'xlim').split(
',')]
266 if hist_xlim == [0.0,0.0]:
271 tmplist = hstack((tmplist,tg.odds(ns)))
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])])
284 ax = fig.add_subplot(111)
287 html_files_hist.append(path.join(localdest,
'hist_'+runid+
"_"+
str(ns)+
"sources."+typ))
288 fig.savefig(html_files_hist[-1],bbox_inches=
'tight')
291 html_files_snrvsodds = []
292 if plot_snrVSodds ==
True:
293 stdout.write(
"... SNR vs. Odds\n")
296 ax = fig.add_subplot(111)
299 html_files_snrvsodds.append(path.join(localdest,
"snrVSodds_"+runid+
"."+ext))
300 fig.savefig(html_files_snrvsodds[-1], bbox_inches=
'tight')
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] = []
310 ax = fig.add_subplot(111)
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')
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]):
329 ax = fig.add_subplot(111)
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')
343 stdout.write(
"Creating html pages\n")
344 htmlfile=open(path.join(localdest,
'index.html'),
'w')
351 <title> TIGER post-processing page </title>
356 <h1>TIGER post-processing page</h1>
360 timedate = datetime.fromtimestamp(timesec).strftime(
'%Y-%m-%d %H:%M:%S')
361 htmltxt+=
"Last updated on "+timedate
379 <h3>Efficiencies</h3>
388 htmltxt+=
'<td>'+
str(ns)+
'</td>'
392 for i
in range(len(tigerruns)):
395 htmltxt+=
'<td>'+tigerruns[i].label+
'</td>'
396 for j
in range(len(N_sources)):
398 effstr=
"%.2f "%html_data_efficiencies[j,i,0]+
' | '+
'%.2f'% html_data_efficiencies[j,i,1]
400 effstr=
"%.2f"%html_data_efficiencies[j,i-1,0]+
' | '+
'%.2f'%html_data_efficiencies[j,i-1,1]
401 htmltxt+=
'<td>'+effstr+
'</td>'
407 <h3>KS and p value</h3>
418 for i
in range(len(tigerruns)-1):
419 for j
in range(len(N_sources)):
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>'
432 for item
in html_files_hist:
434 itembase = path.basename(item)
435 htmltxt+=
"<a href='"+itembase+
"'>"+
"<img src='"+itembase+
"' width=512></a>"
442 for item
in html_files_snrvsodds:
444 itembase = path.basename(item)
445 htmltxt+=
"<a href='"+itembase+
"'>"+
"<img src='"+itembase+
"' width=512></a>"
450 <h2>Individual runs</h2>
452 for i
in range(len(tigerruns)):
453 htmltxt+=
"<a href='"+tigerruns[i].label+
".html"+
"'>"+tigerruns[i].label+
"</a> "
463 for i
in range(len(tigerruns)):
464 indhtmlfile=open(path.join(localdest,tigerruns[i].label+
'.html'),
'w')
471 <title> TIGER post-processing page </title>
477 indhtmltxt+="<h1>"+tigerruns[i].label+
"</h1>"
482 <h2>Cumulative frequency highest Bayes factor</h2>
484 for item
in html_files_cumfreq[i]:
486 itembase = path.basename(item)
487 indhtmltxt+=
"<a href='"+itembase+
"'>"+
"<img src='"+itembase+
"' width=512></a>"
492 <h2>Cumulative Bayes factors (per catalog)</h2>
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]:
499 itembase = path.basename(item)
500 indhtmltxt+=
"<a href='"+itembase+
"'>"+
"<img src='"+itembase+
"' width=512></a>"
502 indhtmlfile.write(indhtmltxt)
505 htmlfile.write(htmltxt)
520 print(
'Script finished')
522 ------------------------------------------------------------------------------
533 Class for a specific TIGER run
536 def __init__(self, cluster, directory, engine, subhyp):
538 Initialisation of class
541 if directory[-1] !=
'/':
559 Search for sources completed sources
in the specified cluster
and location
562 stdout.write("... searching sources: %s\t%s\r" % (self.
cluster,self.
directory))
566 if self.
cluster in clusters.keys():
567 command +=
"gsissh -C "+clusters[self.
cluster]+
" '"
569 if self.
engine ==
'lalnest':
571 elif self.
engine ==
'lalinference':
572 command+=
"/posterior_samples/"
573 command+=
"*_B.txt | xargs -n1 basename`; do "
575 if self.
engine ==
'lalnest':
576 command+=
"test -f "+self.
directory+
"/"+sh
578 elif self.
engine ==
'lalinference':
579 command+=
"test -f "+self.
directory+
"/"+sh.replace(
'phi',
'chi')
580 command+=
"/posterior_samples/"
582 command=command+
"echo $i; done"
583 if self.
cluster in clusters.keys():
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])
591 if self.
engine ==
'lalnest':
593 elif self.
engine ==
'lalinference':
597 if self.
engine ==
'lalnest':
599 elif self.
engine ==
'lalinference':
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])
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])
622 Download Bayes factors from remote location. Only works after sources are
623 found by the searchsources function.
625 stdout.write("... pulling Bayes factors: %s\t%s\r" % (self.
cluster,self.
directory))
628 stdout.write(
"... pulling Bayes factors: %s\t%s - nothing to fetch\n" % (self.
cluster,self.
directory))
634 if self.
engine ==
'lalnest':
636 elif self.
engine ==
'lalinference':
637 files +=
"/posterior_samples/"
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]))
645 savetxt(
'test.dat',self.
bayes);
650 Download SNRs from remote location. Only works after sources are found by
651 the searchsources function.
656 stdout.write(
"... pulling SNRs: %s\t%s - nothing to fetch\n" % (self.
cluster,self.
directory))
660 command+=
"gsissh -C "+clusters[self.
cluster]+
" '"
662 snrfilescomma=
",".join(self.
snrfiles)
663 command +=
" "+self.
directory+self.
subhyp[0]+
"/engine/"+
"{"+snrfilescomma+
"}"
666 p = Popen(command, stdout=PIPE, stderr=PIPE,shell=
True)
668 snrrawdata = p.stdout.readlines()
670 stdout.write(
"... Warning: Cannot find SNR file. Writing zeros\n")
674 for k
in range(len(self.
gps)):
677 self.
snr.append(float(snrrawdata[count+ndet-1].strip(
'%s:'%(self.
detectors[k])).strip()))
680 self.
snr.append(float(snrrawdata[count+ndet].strip(
'Network:').strip()))
682 self.
snr = array(self.
snr)
683 stdout.write(
"... pulling SNRs: %s\t%s - %d sources\n" % (self.
cluster,self.
directory, len(self.
snr)))
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!
692 print(
"Nothing to fetch")
697 command+=
"gsissh -C "+clusters[self.
cluster]+
" '"
700 command +=
" "+self.
directory+self.
subhyp[0]+
"/nest/"+
"{"+posteriorfilescomma+
"}"
703 p = Popen(command, stdout=PIPE, stderr=PIPE,shell=
True)
704 posteriorfilesizes = loadtxt(p.stdout, usecols=[0])[:-1]
705 print(
shape(posteriorfilesizes))
710 command+=
"gsissh -C "+clusters[self.
cluster]+
" '"
713 command +=
" "+self.
directory+self.
subhyp[0]+
"/nest/"+
"{"+posteriorfilescomma+
"}"
716 p = Popen(command, stdout=PIPE, stderr=PIPE,shell=
True)
721 def applycut(self, st=8.0, bt=32.0):
723 Apply a Bayes factor and SNR cut on the data
726 cond = ((self.
snr>st) | (self.
snr==0.0)) & (self.
bayes[:,0]>bt)
736 Pull data from remote
or local locations
745 CLASS TO CONTAIN A SET OF RUNS UNDER A COMMON CHARACTERISTIC (E.G.
746 BACKGROUND, FOREGROUND).
748 def __init__(self,locations,label,latexlabel,testcoef,engine):
750 INITIALISATION OF CLASS
757 s = combinations(testcoef, i+1)
758 self.
subhyp.extend([
"".join(j)
for j
in s])
759 self.
subhyp.insert(0,
'GR')
763 if engine
in [
'lalinference',
'lalnest']:
766 exit(
'Engine not recognised')
782 FIND COMPLETED JOBS AND GET THE FILESNAMES
789 stdout.write(
"--> searching sources: Total number of sources - %d\n" % self.
nsources)
793 PULL DATA FROM REMOTE OR LOCAL LOCATIONS
800 stdout.write(
"--> pulling Bayes factors: matrix size - %d x %d\n" % (
shape(self.
bayes)[0],
shape(self.
bayes)[1]))
804 PULL DATA FROM REMOTE OR LOCAL LOCATIONS
809 self.
snr=hstack((self.
snr,loc.snr))
810 stdout.write(
"--> pulling SNRs: total number of sources %d\n" % (len(self.
snr)))
814 PULLING THE POSTERIOR FILES
822 CALCULATE THE TOTAL ODDS FROM DATA STRUCTURE
831 APPLY DETECTION CUTS (SNR AND BAYESGR)
833 stdout.write("... %s - before cut: %d sources\n" % (self.
label,self.
nsources))
837 loc.applycut(st=st, bt=bt)
845 self.
snr=hstack((self.
snr,loc.snr))
848 stdout.write(
"... %s - after cut: %d sources\n" % (self.
label,self.
nsources))
852 APPLY DETECTION CUTS (SNR AND BAYESGR)
855 print("... Shuufling with seed",
str(seed))
861 random.shuffle(order)
865 self.
snr = self.
snr[order]
869 SAVE DATA TO PICKLE FOR QUICK FUTURE LOADING
878 SAVE DATA TO ASCII FILE FOR QUICK FUTURE LOADING.
879 NB: BROKEN - DO NOT USE!
882 savedata = empty((0,4+len(self.
subhyp)))
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')
894 PREPROCESS DATA ON THE CLUSTERS BEFORE SENDING DATA TO MASTER.
895 NB: UNDER DEVELOPMENT
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))
910 print(
'Writing preprocessfile - done')
914 command +=
"gsiscp -C"
916 command += scriptfilename
918 command += preprocesstmp
920 command += clusters[loc.cluster]+
":~/"
922 p = Popen(command, stdout=PIPE, stderr=PIPE,shell=
True)
924 if p.returncode == 0:
925 command =
"gsissh "+clusters[loc.cluster]+
" 'python "+path.basename(scriptfilename)+
" -p "+preprocesstmp+
"'"
927 p = Popen(command, stdout=PIPE, stderr=PIPE,shell=
True)
928 print(p.stdout.read())
929 print(p.stderr.read())
943 print(
'... Loading file from pickle',filename)
944 fp = open(filename,
'rb')
949 CREATE DIRECTORY IF IT DOES NOT EXIST
951 if not path.exists(f):
956 FUNCTION TO ADD IN LOGARITHMIC SPACE
961 output = x+log(1.0+exp(y-x))
963 output = y+log(1.0+exp(x-y))
969 CALCULATE THE TOTAL ODDS FROM DATA STRUCTURE
972 TotalOdds = -float_info.max
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)
982 CREATE TIGER HISTOGRAMS FROM A LIST OF TIGERRUNS
984 for i
in range(len(list_tigerruns)):
987 axis.hist(list_tigerruns[i].odds(N), \
989 label=list_tigerruns[i].latexlabel+
r"\n$\mathrm{("+
str(
size(list_tigerruns[i].odds(N)))+
r"\; sources)}$", \
990 histtype=
"stepfilled", \
994 hatch = hatch_default[i], \
995 edgecolor = color_default[i], \
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:
1002 axis.hist(list_tigerruns[i].odds(N), \
1004 label=list_tigerruns[i].latexlabel+
r"\n$\mathrm{("+
str(
size(list_tigerruns[i].odds(N)))+
r"\; catalogs)}$", \
1005 histtype=
"stepfilled", \
1009 hatch = hatch_default[i], \
1010 edgecolor = color_default[i], \
1013 axis.set_xlabel(
r"$\ln{\mathcal{O}_{GR}^{modGR}}$")
1014 axis.set_ylabel(
r"$P(\ln{\mathcal{O}_{GR}^{modGR}})$")
1017 handles, labels = axis.get_legend_handles_labels()
1018 axis.legend(handles, labels, loc=
'upper left')
1022 CALCULATE EFFICIENCY FROM A LIST OF TIGERRUNS
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))
1031 efficiencies[i,:] = 0.0
1033 efficiencies[i-1,:] = 0.0
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])
1042 eff=float(nabovebeta)/float(ntotal)
1044 efficiencies[i,j] = eff
1046 efficiencies[i-1,j] = eff
1051 CALCULATE KS STATISTICS FROM A LIST OF TIGERRUNS
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))
1056 data1 = tigerrun1.odds(N)
1057 data2 = tigerrun2.odds(N)
1058 ksstat, pvalue = ks_2samp(data1,data2)
1059 return [ksstat, pvalue]
1064 CREATE SNR VS ODDS DIAGRAMS FROM A LIST OF TIGERRUNS
1067 colors = [
'blue',
'red']
1068 axis.set_xlabel(
r"$\mathrm{SNR}$")
1069 axis.set_ylabel(
r"$\ln{O_{\mathrm{GR}}^{\mathrm{modGR}}}$")
1071 for i
in range(len(list_tigerruns)):
1072 axis.scatter(list_tigerruns[i].snr, list_tigerruns[i].odds(), \
1074 marker=markers[i], \
1075 label=list_tigerruns[i].latexlabel+
r"\n$\mathrm{("+
str(list_tigerruns[i].nsources)+
r"\; sources)}$",\
1079 axis.set_xlim(5.0,50.0)
1082 handles, labels = axis.get_legend_handles_labels()
1083 axis.legend(handles, labels, loc=
'best')
1089 CREATE CUMULATIVE FREQUENCY DIAGRAMS FROM A LIST OF TIGERRUNS
1092 snrrange = arange(xlim[0], xlim[1], 5.)
1093 nsnrsteps = len(snrrange)
1094 freqs = zeros((nsnrsteps,tigerrun.nsubhyp))
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)
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,:])))
1109 rcParams[
'legend.fontsize']=18
1110 linestyles = cycle([
'-',
'--',
'-.',
':'])
1111 markerstyles = cycle([
'+',
'*',
'.',
'<',
'd',
'^',
'x',
's'])
1112 colorstyles = cycle([
"b",
"g",
"r",
"c",
"m",
"y",
"k"])
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)
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], \
1124 marker=markerstyles.next(), \
1127 color=colorstyles.next())
1129 axis.set_xlabel(
r"$\mathrm{SNR}$")
1130 axis.set_ylabel(
r"$\mathrm{Cumulative\;frequency\;(lines)}$")
1134 handles, labels = axis.get_legend_handles_labels()
1135 axis.legend(handles, labels, loc=
'best',ncol=3)
1139 ax2.hist(tigerrun.snr, range=xlim, bins=nsnrsteps, alpha=0.25, color=
"k")
1141 ax2.set_ylabel(
r'$\mathrm{\#\;of\;sources\;per\;SNR\;bin\;(hist)}$')
1146 CREATE CUMULATIVE BAYES FACTOR/ODDS DIAGRAMS FROM A LIST OF TIGERRUNS (FOR
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)])
1159 markerstyles = cycle([
'+',
'*',
'.',
'<',
'd',
'^',
'x',
's'])
1161 axis.set_xlabel(
r"$\mathrm{SNR}$")
1162 axis.set_ylabel(
r"$\ln{{}^{(cat)}B^{i_1 i_2 \ldots i_k}_{\mathrm{GR}}}$")
1165 for i
in range(tigerrun.nsubhyp-1):
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'))+
"}$"
1172 axis.plot(snrcatsorted, cumbayes[:,i],\
1174 marker=markerstyles.next(), \
1179 axis.plot(snrcatsorted, cumodds, label=
r"$\ln{O_{GR}^{modGR}}$", linewidth=2.0, color=
"black")
1181 axis.set_xlim(5.0,50.0)
1183 handles, labels = axis.get_legend_handles_labels()
1184 axis.legend(handles, labels, loc=
'best',ncol=3)
1188 CREATE EXAMPLE CONFIGURATION FILE (TO BE USED WITH -G OPTION)
1190 basename = path.basename(filename)
1191 dirname = path.dirname(filename)
1199 outfile = open(path.join(dirname,basename),
'w')
1202###############################################################################
1204# Configuration file for tiger_postproc.py
1206###############################################################################
1208#------------------------------------------------------------------------------
1210#------------------------------------------------------------------------------
1212# Engine for TIGER runs: lalnest or lalinference
1213engine=engine1,engine2
1215# Subhypotheses: Notation depends on engine.
1216# lalnest: dphi1,dphi2,...
1218hypotheses=dphi1,dphi2,dphi3
1224localdest=/home/user/public_html/tiger/inj_tt4spin_tt4all_rec_tf2spin/
1239locs=/path/to/the/location/file.txt,/path/to/the/location/file.txt
1242labels=tt4spin,tt4all
1246latexlabels=$\\mathrm{TaylorT4+spin}$,$\\mathrm{TaylorT4+all}$
1309 outfile.write(examplecfgtxt)
1311def TigerPreProcess(preprocessfile):
1313 TIGER PREPROCESSOR FUNCTION
1314 NB: UNDER DEVELOPMENT
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()
1323 tgloc.pullposteriors()
1324 tgloc.savetopickle(
'tigerloc_preprocess.pickle')
1327 exit(
'Preprocess file cannot be found - abort')
1335if __name__ ==
"__main__":
TIGERRUN CLASS DEFINITIONS.
def pullposteriors(self)
Download posterior pdfs from remote location.
def __init__(self, cluster, directory, engine, subhyp)
Initialisation of class.
def pullbayes(self)
Download Bayes factors from remote location.
def savetopickle(self, dest)
Pull data from remote or local locations.
def searchsources(self)
Search for sources completed sources in the specified cluster and location.
def pullsnr(self)
Download SNRs from remote location.
def applycut(self, st=8.0, bt=32.0)
Apply a Bayes factor and SNR cut on the data.
CLASS TO CONTAIN A SET OF RUNS UNDER A COMMON CHARACTERISTIC (E.G.
def pullbayes(self)
PULL DATA FROM REMOTE OR LOCAL LOCATIONS.
def savetopickle(self, dest)
SAVE DATA TO PICKLE FOR QUICK FUTURE LOADING.
def savetoascii(self, filename)
SAVE DATA TO ASCII FILE FOR QUICK FUTURE LOADING.
def applycut(self, st=8.0, bt=32.0)
APPLY DETECTION CUTS (SNR AND BAYESGR)
def __init__(self, locations, label, latexlabel, testcoef, engine)
INITIALISATION OF CLASS.
def searchsources(self)
FIND COMPLETED JOBS AND GET THE FILESNAMES.
def shufflesources(self, seed)
APPLY DETECTION CUTS (SNR AND BAYESGR)
def preprocess(self)
PREPROCESS DATA ON THE CLUSTERS BEFORE SENDING DATA TO MASTER.
def pullsnr(self)
PULL DATA FROM REMOTE OR LOCAL LOCATIONS.
def odds(self, N=1)
CALCULATE THE TOTAL ODDS FROM DATA STRUCTURE.
def pullposteriors(self)
PULLING THE POSTERIOR FILES.
def TigerCreateExampleConfigFile(filename)
CREATE EXAMPLE CONFIGURATION FILE (TO BE USED WITH -G OPTION)
def TigerPreProcess(preprocessfile)
TIGER PREPROCESSOR FUNCTION NB: UNDER DEVELOPMENT.
def main()
PLOTTING OPTIONS.
def lnPLUS(x, y)
FUNCTION TO ADD IN LOGARITHMIC SPACE.
def TigerCalculateEfficiency(list_tigerruns, N=1, beta=[0.95], background=0)
CALCULATE EFFICIENCY FROM A LIST OF TIGERRUNS.
def TigerKSandPvalue(tigerrun1, tigerrun2, N=1)
CALCULATE KS STATISTICS FROM A LIST OF TIGERRUNS.
def TigerPostProcess(configfile)
Main TIGER post processing function.
def TigerCreateCumBayes(tigerrun, axis, nthcat, nsourcespercat)
CREATE CUMULATIVE BAYES FACTOR/ODDS DIAGRAMS FROM A LIST OF TIGERRUNS (FOR EACH CATALOG)
def OddsFromBayes(matrix, Nhyp)
CALCULATE THE TOTAL ODDS FROM DATA STRUCTURE.
def ensure(f)
CREATE DIRECTORY IF IT DOES NOT EXIST.
def TigerCreateCumFreq(tigerrun, axis)
CREATE CUMULATIVE FREQUENCY DIAGRAMS FROM A LIST OF TIGERRUNS.
def LoadPickledTigerSet(filename)
LOAD FROM PICKLE.
def TigerCreateHistogram(list_tigerruns, axis, N=1, xlim=None, bins=50)
CREATE TIGER HISTOGRAMS FROM A LIST OF TIGERRUNS.
def TigerCreateSNRvsOdds(list_tigerruns, axis)
CREATE SNR VS ODDS DIAGRAMS FROM A LIST OF TIGERRUNS.