LALApps 10.1.1.1-bf6a62b
lalapps_string_calc_likelihood.py
Go to the documentation of this file.
1##python
2#
3# Copyright (C) 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
29from __future__ import print_function
30
31
32from optparse import OptionParser
33import sqlite3
34import sys
35
36
37from igwn_ligolw import dbtables
38from lal.utils import CacheEntry
39from lalburst import git_version
40from lalburst import calc_likelihood
41from lalburst import SnglBurstUtils
42from lalburst import stringutils
43
44
45__author__ = "Kipp Cannon <kipp.cannon@ligo.org>"
46__version__ = "git id %s" % git_version.id
47__date__ = git_version.date
48
49
50#
51# =============================================================================
52#
53# Command Line
54#
55# =============================================================================
56#
57
58
60 parser = OptionParser(
61 version = "Name: %%prog\n%s" % git_version.verbose_msg
62 )
63 parser.add_option("-c", "--input-cache", metavar = "filename", help = "Also process the files named in this LAL cache. See lalapps_path2cache for information on how to produce a LAL cache file.")
64 parser.add_option("-l", "--likelihood-file", metavar = "filename", action = "append", help = "Set the name of the likelihood ratio data file to use. Can be given more than once.")
65 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.")
66 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.")
67 parser.add_option("--vetoes-name", metavar = "name", help = "Set the name of the segment lists to use as vetoes (default = do not apply vetoes).")
68 parser.add_option("-v", "--verbose", action = "store_true", help = "Be verbose.")
69 options, filenames = parser.parse_args()
70
71 options.likelihood_filenames = []
72 if options.likelihood_file is not None:
73 options.likelihood_filenames += options.likelihood_file
74 if options.likelihood_cache is not None:
75 options.likelihood_filenames += [CacheEntry(line).path for line in open(options.likelihood_cache)]
76 if not options.likelihood_filenames:
77 raise ValueError("no ranking statistic likelihood data files specified")
78
79 if options.input_cache:
80 filenames += [CacheEntry(line).path for line in open(options.input_cache)]
81 if not filenames:
82 raise ValueError("no candidate databases specified")
83
84 return options, filenames
85
86
87#
88# =============================================================================
89#
90# Main
91#
92# =============================================================================
93#
94
95
96#
97# command line
98#
99
100
101options, filenames = parse_command_line()
102
103
104#
105# load likelihood data
106#
107
108
109coincparamsdistributions, likelihood_seglists = stringutils.load_likelihood_data(options.likelihood_filenames, verbose = options.verbose)
110if options.verbose:
111 print("computing event densities ...", file=sys.stderr)
112coincparamsdistributions.finish()
113
114
115#
116# iterate over files
117#
118
119
120for n, filename in enumerate(filenames):
121 #
122 # Open the database file.
123 #
124
125 if options.verbose:
126 print("%d/%d: %s" % (n + 1, len(filenames), filename), file=sys.stderr)
127
128 working_filename = dbtables.get_connection_filename(filename, tmp_path = options.tmp_space, verbose = options.verbose)
129 connection = sqlite3.connect(str(working_filename))
130 if options.tmp_space is not None:
131 dbtables.set_temp_store_directory(connection, options.tmp_space, verbose = options.verbose)
132
133 #
134 # Summarize the database.
135 #
136
137 contents = SnglBurstUtils.CoincDatabase(connection, live_time_program = "StringSearch", search = "StringCusp", veto_segments_name = options.vetoes_name)
138 if options.verbose:
139 SnglBurstUtils.summarize_coinc_database(contents)
140 if not contents.seglists and options.verbose:
141 print("\twarning: no segments found", file=sys.stderr)
142
143 #
144 # Build triangulators. The timing uncertainty of +/- 8e-5 s was
145 # measured with lalapps_string_plot_binj and is essentially
146 # identical for H1, H2, L1, and V1.
147 #
148
149 triangulators = stringutils.triangulators(dict.fromkeys(contents.instruments, 8e-5))
150
151 #
152 # Run likelihood ratio calculation.
153 #
154
155 calc_likelihood.assign_likelihood_ratios(
156 connection = contents.connection,
157 coinc_def_id = contents.bb_definer_id,
158 offset_vectors = contents.time_slide_table.as_dict(),
159 vetoseglists = contents.vetoseglists,
160 events_func = lambda cursor, coinc_event_id: calc_likelihood.sngl_burst_events_func(cursor, coinc_event_id, contents.sngl_burst_table.row_from_cols),
161 veto_func = calc_likelihood.sngl_burst_veto_func,
162 ln_likelihood_ratio_func = coincparamsdistributions.ln_lr_from_triggers,
163 verbose = options.verbose
164 )
165
166 #
167 # Clean up.
168 #
169
170 contents.xmldoc.unlink()
171 connection.close()
172 dbtables.put_connection_filename(filename, working_filename, verbose = options.verbose)