gstlal-inspiral  1.9.2
 All Classes Namespaces Files Functions
gstlal_inspiral_marginalize_likelihood
1 #!/usr/bin/env python3
2 #
3 # Copyright (C) 2009-2013 Kipp Cannon, Chad Hanna, Drew Keppel
4 #
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.
9 #
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.
14 #
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.
18 
19 ### A program to marginalize the likelihood pdfs in noise across mass bins for a gstlal inspiral analysis.
20 ###
21 ### Review status
22 ### -------------
23 ###
24 ### +-----------------------------+------------------------------------------+------------+
25 ### | Names | Hash | Date |
26 ### +=============================+==========================================+============+
27 ### | Florent, Jolien, Kipp, Chad | 1dbbbd963c9dc076e1f7f5f659f936e44005f33b | 2015-05-14 |
28 ### +-----------------------------+------------------------------------------+------------+
29 ###
30 
31 
32 #
33 # =============================================================================
34 #
35 # Preamble
36 #
37 # =============================================================================
38 #
39 
40 
41 from optparse import OptionParser
42 
43 
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
48 
49 
50 from gstlal import far
51 
52 
53 #
54 # =============================================================================
55 #
56 # Command Line
57 #
58 # =============================================================================
59 #
60 
61 
62 def parse_command_line():
63  parser = OptionParser(
64  )
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.")
71 
72  options, urls = parser.parse_args()
73 
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")
78 
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")
83 
84  return options, urls
85 
86 
87 #
88 # =============================================================================
89 #
90 # Main
91 #
92 # =============================================================================
93 #
94 
95 
96 #
97 # parse command line
98 #
99 
100 
101 options, urls = parse_command_line()
102 
103 
104 #
105 # initialize output document
106 #
107 
108 
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__)
112 
113 
114 #
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.
120 #
121 
122 
123 marginalized = far.marginalize_pdf_urls(
124  urls,
125  which = "RankingStat" if options.marginalize == "ranking-stat" else "RankingStatPDF",
126  ignore_missing_files = options.ignore_missing,
127  verbose = options.verbose
128 )
129 
130 
131 #
132 # Apply density estimation to zero-lag rates
133 #
134 
135 
136 if options.density_estimate_zero_lag:
137  marginalized.density_estimate_zero_lag_rates()
138 
139 
140 #
141 # write output document
142 #
143 
144 
145 if options.marginalize == "ranking-stat":
146  process.instruments = marginalized.instruments
147  far.gen_likelihood_control_doc(xmldoc, marginalized, None)
148 else:
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)