3 # Copyright (C) 2010--2014 Kipp Cannon, Chad Hanna
5 # This program is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by the
7 # Free Software Foundation; either version 2 of the License, or (at your
8 # option) any later version.
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
13 # Public License for more details.
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 ### A program to reset likelihood data after a burn in
26 # =============================================================================
30 # =============================================================================
37 from gstlal
import far
38 from ligo.lw
import utils as ligolw_utils
39 from ligo.lw.utils
import process as ligolw_process
40 from ligo.lw
import ligolw
41 from ligo
import segments
42 from lal
import LIGOTimeGPS
43 from optparse
import OptionParser
46 def parse_command_line():
47 parser = OptionParser(
48 version =
"Name: %%prog\n%s" %
"" # FIXME
50 parser.add_option(
"-v",
"--verbose", action =
"store_true", help =
"Be verbose.")
51 parser.add_option(
"--background-ranking-file", metavar =
"filename", help =
"Write likelihood files to disk and include the names in this cachefile file.")
53 parser.add_option(
"--zerolag-ranking-file", metavar =
"filename", help =
"Write zerolag likelihood files to disk and include the names in this cachefile file.")
54 parser.add_option(
"--add-zerolag-to-background", action =
"store_true", help =
"Add zerolag events to background before populating coincident parameter PDF histograms")
55 parser.add_option(
"--skip-seg-and-rd-reset", action =
"store_true", help =
"Skip adjusting the segments and ranking data. This would make the entire use of this program a no op unless --add-zerolag-to-background is used. So you have to do that.")
56 parser.add_option(
"--segment-and-horizon", action =
"append", help =
"Append to a list of segments and horizon distances for a given instrument. Argument specified as IFO:start:end:horizon, e.g., H1:1000000000:1000000100:120 ")
57 options, filenames = parser.parse_args()
59 process_params = dict(options.__dict__)
61 def parse_segment_and_horizon(options = options):
62 seglistdict = segments.segmentlistdict()
63 for x in options.segment_and_horizon:
64 ifo, start, stop, horizon = x.split(
":")
65 seglistdict.setdefault(ifo, segments.segmentlist()).append(segments.segment([LIGOTimeGPS(start), LIGOTimeGPS(stop)]))
68 if options.segment_and_horizon is None:
69 raise ValueError(
"--segment-and-horizon required")
70 seglistdict = parse_segment_and_horizon(options)
72 if options.skip_seg_and_rd_reset and not options.add_zerolag_to_background:
73 raise ValueError(
"if using --skip-seg-and-rd-reset you must use --add-zerolag-to-background or else this program will do nothing")
75 return options, process_params, seglistdict
78 options, process_params, segs = parse_command_line()
81 print >> sys.stderr,
"Livetime: ", abs(segs.extent_all())
82 print >> sys.stderr,
"Extent: ", segs.extent_all()
84 like_cpd, like_rd = far.parse_likelihood_control_doc(ligolw_utils.load_filename(options.background_ranking_file, verbose = options.verbose, contenthandler = far.RankingStat.LIGOLWContentHandler))
86 _, zlike_rd = far.parse_likelihood_control_doc(ligolw_utils.load_filename(options.zerolag_ranking_file, verbose = options.verbose, contenthandler = far.RankingStat.LIGOLWContentHandler))
88 # optionally add the zero lag SNR/chi counts to the background
89 # FIXME: this is busted. needs to be updated following the re-write
90 if options.add_zerolag_to_background:
91 raise ValueError(
"not implemented")
93 if not options.skip_seg_and_rd_reset:
94 # replace thae live time with the user specified live time
98 # Make the observed counts match the background sample but with the correct normalization
99 num_zero_lag = zlike_rd.zero_lag_lr_lnpdf.array.sum() * float(abs(new_like_segs)) / float(abs(like_rd.segments))
100 if num_zero_lag <= 100 or numpy.isnan(num_zero_lag):
101 warnings.warn(
"found %f zero lag counts, replacing with 100" % num_zero_lag)
103 zlike_rd.zero_lag_lr_lnpdf.array[:] = like_rd.noise_lr_lnpdf.array[:]
104 zlike_rd.zero_lag_lr_lnpdf.array *= num_zero_lag / zlike_rd.zero_lag_lr_lnpdf.array.sum()
105 # make the counts integers
107 print >> sys.stderr,
"replacing with %f zero lag counts" % zlike_rd.zero_lag_lr_lnpdf.array.sum()
110 # write out the background file
111 xmldoc = ligolw.Document()
112 xmldoc.appendChild(ligolw.LIGO_LW())
113 process = ligolw_process.register_to_xmldoc(xmldoc, sys.argv[0], instruments = like_cpd.instruments, paramdict = process_params)
114 far.gen_likelihood_control_doc(xmldoc, like_cpd, like_rd)
115 ligolw_utils.write_filename(xmldoc, options.background_ranking_file, verbose = options.verbose)
117 # write out the zerolag file
118 xmldoc = ligolw.Document()
119 xmldoc.appendChild(ligolw.LIGO_LW())
120 process = ligolw_process.register_to_xmldoc(xmldoc, sys.argv[0], instruments = like_cpd.instruments, paramdict = process_params)
121 far.gen_likelihood_control_doc(xmldoc, None, zlike_rd)
122 ligolw_utils.write_filename(xmldoc, options.zerolag_ranking_file, verbose = options.verbose)