3This program simply computes the bayes factor between the coherent and incoherent models
4given as input the B files for the run.
7from __future__
import print_function
9from optparse
import OptionParser
14from pylab
import loadtxt
15from math
import log,exp
17usage=
'''%prog [options] coherent_B.txt incoherent1_B.txt incoherent2_B.txt ... incoherentN_B.txt
18%prog takes a list of arguments which specify FIRST the coherent B.txt file, followed by all the
19B.txt files from the individual runs on each IFO. The coherent file must be specified first.
21The code will then perform either the test of coherent vs incoherent signal models, i.e.
22B_{CI} = Z_co / Z_1 * Z_2 * Z_3
24or a test of coherent vs incoherent
25B_{CIN} = Z_co / (Z_1 * Z_2 * Z_3 + N_{123})
27or the test of coherent vs ( incoherent signal or noise ) models, i.e.
28B_{CIN} = Z_co / Z_{inc OR noise}
29with Z_{inc OR noise} = sum over all permutations of the signal and noise hypotheses in each detector
32see also https://dcc.ligo.org/LIGO-T1600068
34Output will be written to stdout, and to a text file specified by the --outfile option.
38parser=OptionParser(usage)
39parser.add_option(
"-c",
"--coherent-incoherent",action=
"store_true",dest=
"ci",help=
"Perform coherence test of Coherent signal vs incoherent signal",default=
False )
40parser.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 )
41parser.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 )
42parser.add_option(
"-o",
"--outfile",default=
None, help=
"Optional output file name", metavar=
"OUTFILE.txt",type=
"string",action=
"store")
44(opts,args)=parser.parse_args()
48 with h5py.File(filename,
'r')
as hdf:
51 print(
'Error: multiple runs %s found in input file %s'%g.keys(),filename)
54 g=g[list(g.keys())[0]]
59 print(
'No input files specified')
62if( sum([opts.ci,opts.cin,opts.cin_or_n])!=1):
63 print(
'Please specify one of -c, -n or -b. See help for details.')
70 print(
'Cannot perform coherence test on less than 2 IFOs')
74 print(
'Error: You must specify incoherent input files')
79 return (b+log(1+exp(a-b)))
82 print(
'Looking for '+Bfile)
83 if os.access(Bfile,os.R_OK):
84 outstat = loadtxt(Bfile)
85 if key==
'log_evidence':
87 if key==
'log_noise_evidence':
89 if key==
'log_bayes_factor':
91 print(
'Unknown key %s'%(key))
97 if '.h5' in filename
or '.hdf' in filename:
104Z_single_noise = [
get_metadata(inco,
'log_noise_evidence')
for inco
in incofiles]
105Z_single_signal = [
get_metadata(inco,
'log_evidence')
for inco
in incofiles]
110from itertools
import combinations
112 from scipy.special
import logsumexp
114 from scipy.misc
import logsumexp
117 return False if t[0]
in seen
or t[1]
in seen
else seen.update(t)
or True
119Z_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))))])
121for inco
in incofiles:
125if(opts.outfile
is not None):
126 out=open(opts.outfile,
'w')
131 B=Zco-
logadd(Zinco,Znoise)
133 B=Zco-Z_incoherentORnoise
137if(opts.outfile
is not None ):
138 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)