2 This program simply computes the bayes factor between the coherent and incoherent models
3 given as input the B files for the run.
6 from __future__
import print_function
8 from optparse
import OptionParser
13 from pylab
import loadtxt
14 from math
import log,exp
16 usage=
'''%prog [options] coherent_B.txt incoherent1_B.txt incoherent2_B.txt ... incoherentN_B.txt
17 %prog takes a list of arguments which specify FIRST the coherent B.txt file, followed by all the
18 B.txt files from the individual runs on each IFO. The coherent file must be specified first.
20 The code will then perform either the test of coherent vs incoherent signal models, i.e.
21 B_{CI} = Z_co / Z_1 * Z_2 * Z_3
23 or a test of coherent vs incoherent
24 B_{CIN} = Z_co / (Z_1 * Z_2 * Z_3 + N_{123})
26 or the test of coherent vs ( incoherent signal or noise ) models, i.e.
27 B_{CIN} = Z_co / Z_{inc OR noise}
28 with Z_{inc OR noise} = sum over all permutations of the signal and noise hypotheses in each detector
31 see also https://dcc.ligo.org/LIGO-T1600068
33 Output will be written to stdout, and to a text file specified by the --outfile option.
37 parser=OptionParser(usage)
38 parser.add_option(
"-c",
"--coherent-incoherent",action=
"store_true",dest=
"ci",help=
"Perform coherence test of Coherent signal vs incoherent signal",default=
False )
39 parser.add_option(
"-n",
"--coherent-incoherent-noise", dest=
"cin", action=
"store_true", help=
"Perform coherence test of coherent signal vs incoherent signal or noise", default=
False )
40 parser.add_option(
"-b",
"--new-coherent-incoherent-noise", dest=
"cin_or_n", action=
"store_true", help=
"Perform coherence test of coherent signal vs incoherent signal or noise using the most inclusive hypothesis", default=
False )
41 parser.add_option(
"-o",
"--outfile",default=
None, help=
"Optional output file name", metavar=
"OUTFILE.txt",type=
"string",action=
"store")
43 (opts,args)=parser.parse_args()
47 with h5py.File(filename,
'r')
as hdf:
50 print(
'Error: multiple runs %s found in input file %s'%g.keys(),filename)
53 g=g[list(g.keys())[0]]
58 print(
'No input files specified')
61 if( sum([opts.ci,opts.cin,opts.cin_or_n])!=1):
62 print(
'Please specify one of -c, -n or -b. See help for details.')
69 print(
'Cannot perform coherence test on less than 2 IFOs')
73 print(
'Error: You must specify incoherent input files')
78 return (b+log(1+exp(a-b)))
81 print(
'Looking for '+Bfile)
82 if os.access(Bfile,os.R_OK):
83 outstat = loadtxt(Bfile)
84 if key==
'log_evidence':
86 if key==
'log_noise_evidence':
88 if key==
'log_bayes_factor':
90 print(
'Unknown key %s'%(key))
96 if '.h5' in filename
or '.hdf' in filename:
103 Z_single_noise = [
get_metadata(inco,
'log_noise_evidence')
for inco
in incofiles]
104 Z_single_signal = [
get_metadata(inco,
'log_evidence')
for inco
in incofiles]
109 from itertools
import combinations
111 from scipy.special
import logsumexp
113 from scipy.misc
import logsumexp
116 return False if t[0]
in seen
or t[1]
in seen
else seen.update(t)
or True
118 Z_incoherentORnoise = logsumexp([sum(p)
for p
in list(filter(
lambda t, seen=set():
False if t[0]
in seen
or t[1]
in seen
else seen.update(t)
or True, combinations(Z_single_signal+Z_single_noise, len(Z_single_signal))))])
120 for inco
in incofiles:
124 if(opts.outfile
is not None):
125 out=open(opts.outfile,
'w')
130 B=Zco-
logadd(Zinco,Znoise)
132 B=Zco-Z_incoherentORnoise
136 if(opts.outfile
is not None ):
137 print(
'%.3f'%(B), file=out)
def get_metadata_hdf5(filename, key)
def get_metadata_old(Bfile, key)
def not_seen(t, seen=set())
def get_metadata(filename, key)