LALBurst 2.0.7.1-eeff03c
lalburst_power_meas_likelihood.py
Go to the documentation of this file.
1##python
2#
3# Copyright (C) 2007-2010 Kipp Cannon
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
20#
21# =============================================================================
22#
23# Preamble
24#
25# =============================================================================
26#
27
28
29import math
30from optparse import OptionParser
31import sqlite3
32import string
33import sys
34
35from lal.utils import CacheEntry
36
37from igwn_ligolw import dbtables
38from igwn_ligolw import utils as ligolw_utils
39from lalburst import burca_tailor
40from lalburst import SnglBurstUtils
41from lalburst.SimBurstUtils import MW_CENTER_J2000_RA_RAD, MW_CENTER_J2000_DEC_RAD
42from lalburst import git_version
43import igwn_segments as segments
44
45
46# characters allowed to appear in the description string
47T010150_letters = set(string.ascii_lowercase + string.ascii_uppercase + string.digits + "_+#")
48
49
50__author__ = "Kipp Cannon <kipp.cannon@ligo.org>"
51__version__ = "git id %s" % git_version.id
52__date__ = git_version.date
53
54
55#
56# =============================================================================
57#
58# Command Line
59#
60# =============================================================================
61#
62
63
65 parser = OptionParser(
66 version = "Name: %%prog\n%s" % git_version.verbose_msg,
67 usage = "%prog [options] [filename ...]",
68 description = "%prog analyzes a collection of SQLite3 database files containing lalburst_coinc outputs, and measures probability distributions for a variety of parameters computed from the coincidences therein. The distributions are written to a likelihood data file in XML format, which can be used by lalburst_coinc for the excesspower2 algorithm in which a second pass assigns likelihoods to each coincidence. The command line arguments are used to provide shell patterns for the files from which to obtain injection and backgroun coincidences. If file names are given on the command line following the arguments, then likelihood data is loaded from those files and added to the output."
69 )
70 parser.add_option("--add-from", metavar = "filename", default = [], action = "append", help = "Also add likelihood data from this XML file.")
71 parser.add_option("--add-from-cache", metavar = "filename", help = "Also add likelihood data from all XML files listed in this LAL cache.")
72 parser.add_option("-o", "--output", metavar = "filename", default = None, help = "Set the name of the likelihood control file to write (default = stdout).")
73 parser.add_option("-t", "--tmp-space", metavar = "path", help = "Path to a directory suitable for use as a work area while manipulating the database file. The database file will be worked on in this directory, and then moved to the final location when complete. This option is intended to improve performance when running in a networked environment, where there might be a local disk with higher bandwidth than is available to the filesystem on which the final output will reside.")
74 parser.add_option("--T010150", metavar = "description", default = None, help = "Write the output to a file whose name is compatible with the file name format described in LIGO-T010150-00-E, \"Naming Convention for Frame Files which are to be Processed by LDAS\". The description string will be used to form the second field in the file name.")
75 parser.add_option("-p", "--live-time-program", metavar = "program", default = "lalapps_power", help = "Program from which to draw the livetime segments. (Necessary in case of giving --T010150.")
76 parser.add_option("-v", "--verbose", action = "store_true", help = "Be verbose.")
77 options, filenames = parser.parse_args()
78
79 if options.T010150 is not None:
80 if options.output is not None:
81 raise ValueError("cannot set both --T010150 and --output")
82 if options.T010150 == "":
83 options.T010150 = "EXCESSPOWER_LIKELIHOOD"
84 elif set(options.T010150) - T010150_letters:
85 raise ValueError("invalid characters in description \"%s\"" % options.T010150)
86
87 if options.add_from_cache:
88 options.add_from += [CacheEntry(line).path for line in file(options.add_from_cache)]
89
90 return options, filenames
91
92
93#
94# =============================================================================
95#
96# Main
97#
98# =============================================================================
99#
100
101
102#
103# Command line.
104#
105
106
107options, filenames = parse_command_line()
108
109
110#
111# Coinc params
112#
113
114
115distributions = burca_tailor.EPGalacticCoreCoincParamsDistributions()
116segs = segments.segmentlistdict()
117
118
119#
120# Load pre-computed likelihood data.
121#
122
123
124if options.add_from:
125 c, s = distributions.from_filenames(options.add_from, "lalburst_power_meas_likelihood", verbose = options.verbose)
126 distributions += c
127 segs |= s
128 del c
129 del s
130
131
132#
133# Iterate over files
134#
135
136
137for n, filename in enumerate(filenames):
138 #
139 # Open the database file.
140 #
141
142 if options.verbose:
143 print("%d/%d: %s" % (n + 1, len(filenames), filename), file=sys.stderr)
144
145 working_filename = dbtables.get_connection_filename(filename, tmp_path = options.tmp_space, verbose = options.verbose)
146 connection = sqlite3.connect(str(working_filename))
147 connection.execute("PRAGMA synchronous = OFF;")
148 connection.execute("PRAGMA temp_store_directory = '%s';" % dbtables.tempfile.gettempdir())
149
150 #
151 # Summarize the database.
152 #
153
154 database = SnglBurstUtils.CoincDatabase(connection, options.live_time_program)
155 if options.verbose:
156 SnglBurstUtils.summarize_coinc_database(database)
157 segs |= database.seglists
158
159 #
160 # Record statistics. Assume all files with sim_burst tables are
161 # the outputs of injection runs, and others aren't.
162 #
163
164 if database.sim_burst_table is None:
165 # iterate over burst<-->burst coincs
166 for is_background, events, offsetvector in database.get_noninjections():
167 params = distributions.coinc_params(events, offsetvector, MW_CENTER_J2000_RA_RAD, MW_CENTER_J2000_DEC_RAD)
168 if params is not None:
169 if is_background:
170 distributions.denominator.increment(params)
171 else:
172 distributions.candidates.increment(params)
173 else:
174 # iterate over burst<-->burst coincs matching injections
175 # "exactly"
176 for sim, events, offsetvector in database.get_injections():
177 params = distributions.coinc_params(events, offsetvector, MW_CENTER_J2000_RA_RAD, MW_CENTER_J2000_DEC_RAD)
178 if params is not None:
179 distributions.numerator.increment(params)
180
181 #
182 # Clean up.
183 #
184
185 del database, connection
186 dbtables.discard_connection_filename(filename, working_filename, verbose = options.verbose)
187
188
189#
190# Output.
191#
192
193
194def T010150_basename(description, seglists):
195 seg = seglists.extent_all()
196 return "%s-%s-%s-%s" % ("+".join(sorted(seglists.keys())), description, str(int(seg[0])), str(int(math.ceil(abs(seg)))))
197
198
199if options.T010150:
200 filename = T010150_basename(options.T010150, segs) + ".xml.gz"
201else:
202 filename = options.output
203
204
205xmldoc = burca_tailor.gen_likelihood_control(distributions, segs)
206ligolw_utils.write_filename(xmldoc, filename, verbose = options.verbose)