gstlal-inspiral  1.9.3
 All Classes Namespaces Files Functions
gstlal_inspiral_calc_rank_pdfs
1 #!/usr/bin/env python3
2 #
3 # Copyright (C) 2010--2015 Kipp Cannon, Chad Hanna
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 compute the noise probability distributions of likehood ratios for inspiral triggers
20 
21 #
22 # =============================================================================
23 #
24 # Preamble
25 #
26 # =============================================================================
27 #
28 
29 
30 from optparse import OptionParser
31 import sys
32 
33 
34 from ligo.lw import ligolw
35 from ligo.lw import utils as ligolw_utils
36 from ligo.lw.utils import process as ligolw_process
37 from gstlal import far
38 from lal.utils import CacheEntry
39 
40 
41 __author__ = "Kipp Cannon <kipp.cannon@ligo.org>"
42 __version__ = "git id %s" % "" # FIXME
43 __date__ = "" # FIXME
44 
45 
46 #
47 # =============================================================================
48 #
49 # Command Line
50 #
51 # =============================================================================
52 #
53 
54 
55 def parse_command_line():
56  parser = OptionParser(
57  version = "Name: %%prog\n%s" % "" # FIXME
58  )
59  parser.add_option("--likelihood-cache", metavar = "filename", help = "Also load the likelihood ratio data files listsed in this LAL cache. See lalapps_path2cache for information on how to produce a LAL cache file.")
60  parser.add_option("-v", "--verbose", action = "store_true", help = "Be verbose.")
61  parser.add_option("--ranking-stat-samples", metavar = "N", default = 2**24, type = "int", help = "Construct ranking statistic histograms by drawing this many samples from the ranking statistic generator (default = 2^24).")
62  parser.add_option("--add-zerolag-to-background", action = "store_true", help = "Add zerolag events to background before populating coincident parameter PDF histograms")
63  parser.add_option("--output", metavar = "filename", help = "Write merged raw likelihood data and likelihood ratio histograms to this LIGO Light-Weight XML file.")
64  options, urls = parser.parse_args()
65 
66  paramdict = options.__dict__.copy()
67 
68  if options.likelihood_cache is not None:
69  urls += [CacheEntry(line).url for line in open(options.likelihood_cache)]
70  if not urls:
71  raise ValueError("must provide some likelihood files")
72 
73  if options.output is None:
74  raise ValueError("must set --output")
75 
76  return options, urls, paramdict
77 
78 
79 #
80 # =============================================================================
81 #
82 # Main
83 #
84 # =============================================================================
85 #
86 
87 
88 #
89 # command line
90 #
91 
92 
93 options, urls, paramdict = parse_command_line()
94 
95 
96 #
97 # load parameter distribution data
98 #
99 
100 
101 rankingstat = far.marginalize_pdf_urls(urls, "RankingStat", verbose = options.verbose)
102 
103 
104 #
105 # invoke .finish() to apply density estimation kernels and correct the
106 # normalization. preserve the original, raw data, so we can write it do
107 # disk later with the ranking stat PDF obtained from it. if we wish to
108 # draw samples from a population that includes the zero-lag, then we also
109 # construct a separate, sampler, PDF object.
110 #
111 
112 
113 lr_rankingstat = rankingstat.copy()
114 lr_rankingstat.finish()
115 if options.add_zerolag_to_background:
116  signal_noise_pdfs = rankingstat.copy()
117  signal_noise_pdfs.denominator.lnzerolagdensity = signal_noise_pdfs.zerolag
118  signal_noise_pdfs.finish()
119 else:
120  signal_noise_pdfs = None
121 
122 
123 #
124 # generate likelihood ratio histograms
125 #
126 
127 
128 rankingstatpdf = far.RankingStatPDF(lr_rankingstat, signal_noise_pdfs = signal_noise_pdfs, nsamples = options.ranking_stat_samples, verbose = options.verbose)
129 
130 
131 #
132 # Write the parameter and ranking statistic distribution data to a file
133 #
134 
135 
136 xmldoc = ligolw.Document()
137 xmldoc.appendChild(ligolw.LIGO_LW())
138 process = ligolw_process.register_to_xmldoc(xmldoc, u"gstlal_inspiral_calc_rank_pdfs", paramdict = paramdict, instruments = rankingstat.instruments)
139 far.gen_likelihood_control_doc(xmldoc, rankingstat, rankingstatpdf)
140 ligolw_process.set_process_end_time(process)
141 ligolw_utils.write_filename(xmldoc, options.output, verbose = options.verbose)