3 # Copyright (C) 2009-2013 Kipp Cannon, Chad Hanna, Drew Keppel
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.
19 ### A program to marginalize the likelihood pdfs in noise across mass bins for a gstlal inspiral analysis.
24 ### +-----------------------------+------------------------------------------+------------+
25 ### | Names | Hash | Date |
26 ### +=============================+==========================================+============+
27 ### | Florent, Jolien, Kipp, Chad | 1dbbbd963c9dc076e1f7f5f659f936e44005f33b | 2015-05-14 |
28 ### +-----------------------------+------------------------------------------+------------+
33 # =============================================================================
37 # =============================================================================
41 from optparse
import OptionParser
44 from ligo.lw
import ligolw
45 from ligo.lw
import utils as ligolw_utils
46 from ligo.lw.utils
import process as ligolw_process
47 from lal.utils
import CacheEntry
50 from gstlal
import far
54 # =============================================================================
58 # =============================================================================
62 def parse_command_line():
63 parser = OptionParser(
65 parser.add_option(
"--ignore-missing", action =
"store_true", help =
"Ignore and skip missing input documents.")
66 parser.add_option(
"--marginalize", metavar =
"{ranking-stat|ranking-stat-pdf}", default =
"ranking-stat", help =
"Set which set of PDFs to marginalize, the ranking statistics themselves, or the distributions of ranking statistic values (default: ranking-stat).")
67 parser.add_option(
"--density-estimate-zero-lag", action =
"store_true", help =
"Apply density estimation algorithm to zero-lag PDFs (default: do not). Requires --marginalize=ranking-stat-pdf. In the online analysis, the ingested zero-lag histograms contain raw bin counts, but the consumer of this program's output requires the PDFs to be properly density-estimated, so we provide the option of performing that operation here as a courtesy. This will probably work differently one day.")
68 parser.add_option(
"-o",
"--output", metavar =
"filename", help =
"Set the output file name (default = write to stdout).")
69 parser.add_option(
"--likelihood-cache", metavar =
"filename", help =
"Set the cache file name from which to read likelihood files.")
70 parser.add_option(
"--verbose", action =
"store_true", help =
"Be verbose.")
72 options, urls = parser.parse_args()
74 if options.marginalize not in (
"ranking-stat",
"ranking-stat-pdf"):
75 raise ValueError(
"--marginalize must be one of 'ranking-stat' or 'ranking-stat-pdf'")
76 if options.density_estimate_zero_lag and options.marginalize !=
"ranking-stat-pdf":
77 raise ValueError(
"--density-estimate-zero-lag cannot be set unless --marginalize=ranking-stat-pdf")
79 if options.likelihood_cache:
80 urls += [CacheEntry(line).url for line in open(options.likelihood_cache)]
81 if not urls and not options.ignore_missing:
82 raise ValueError(
"no input documents")
88 # =============================================================================
92 # =============================================================================
101 options, urls = parse_command_line()
105 # initialize output document
109 xmldoc = ligolw.Document()
110 xmldoc.appendChild(ligolw.LIGO_LW())
111 process = ligolw_process.register_to_xmldoc(xmldoc, u
"gstlal_inspiral_marginalize_likelihood", options.__dict__)
115 # loop over input documents. NOTE: this process leaves the internal PDF
116 # metadata improperly normalized (the normalizations are not recomputed by
117 # the .__iadd__() methods for performance reasons). The XML writing code,
118 # however, will ensure everything has been normalized properly before
119 # writing it to disk.
123 marginalized = far.marginalize_pdf_urls(
125 which =
"RankingStat" if options.marginalize ==
"ranking-stat" else
"RankingStatPDF",
126 ignore_missing_files = options.ignore_missing,
127 verbose = options.verbose
132 # Apply density estimation to zero-lag rates
136 if options.density_estimate_zero_lag:
137 marginalized.density_estimate_zero_lag_rates()
141 # write output document
145 if options.marginalize ==
"ranking-stat":
146 process.instruments = marginalized.instruments
147 far.gen_likelihood_control_doc(xmldoc, marginalized, None)
149 far.gen_likelihood_control_doc(xmldoc, None, marginalized)
150 ligolw_process.set_process_end_time(process)
151 ligolw_utils.write_filename(xmldoc, options.output, verbose = options.verbose)