LALApps 10.1.1.1-bf6a62b
lalapps_string_apply_vetoes.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 igwn_ligolw.utils import segments as ligolwsegments
39from lalburst import git_version
40
41
42__author__ = "Kipp Cannon <kipp.cannon@ligo.org>"
43__version__ = "git id %s" % git_version.id
44__date__ = git_version.date
45
46
47#
48# =============================================================================
49#
50# Command Line
51#
52# =============================================================================
53#
54
55
57 parser = OptionParser(
58 version = "Name: %%prog\n%s" % git_version.verbose_msg,
59 usage = "%prog [options] [filename ...]",
60 description = "%prog constructs or clears and then populates the string_vetoed_sngl table providing a list of the sngl_burst event_ids of vetoed triggers. This table is used by other post-processing tools like lalapps_string_meas_likelihood to determine which triggers have been vetoed and which haven't."
61 )
62 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.")
63 parser.add_option("-n", "--vetoes-name", metavar = "name", default = "vetoes", help = "Set the name of the segment lists to use as vetoes (default = \"vetoes\").")
64 parser.add_option("-v", "--verbose", action = "store_true", help = "Be verbose.")
65 options, filenames = parser.parse_args()
66
67 if not filenames:
68 raise ValueError("no input files!")
69
70 return options, filenames
71
72
73#
74# =============================================================================
75#
76# Main
77#
78# =============================================================================
79#
80
81
82#
83# Command line.
84#
85
86
87options, filenames = parse_command_line()
88
89
90#
91# Iterate over files
92#
93
94
95def create_string_sngl_is_vetoed_function(connection, veto_segments_name = None):
96 """
97 Creates a function named string_sngl_is_vetoed in the database at
98 connection. The function accepts three parameters --- the
99 instrument name, and the integer and integer nanoseconds components
100 of a time --- and returns true if the instrument is vetoed at that
101 time or false otherwise. veto_segments_name sets the name of the
102 segment lists used to define the vetoes.
103
104 If veto_segments_name is None then a no-op function is created that
105 always returns False.
106
107 Note: this funtion requires igwn_ligolw.dbtables and
108 igwn_ligolw.utils.segments to be imported as dbtables and
109 ligolwsegments respectively.
110 """
111 if veto_segments_name is None:
112 connection.create_function("string_sngl_is_vetoed", 3, lambda instrument, peak_time, peak_time_ns: False)
113 return
114 xmldoc = dbtables.get_xml(connection)
115 seglists = ligolwsegments.segmenttable_get_by_name(xmldoc, options.vetoes_name).coalesce()
116 xmldoc.unlink()
117 def is_vetoed(instrument, peak_time, peak_time_ns, seglists = seglists):
118 return instrument in seglists and dbtables.lsctables.LIGOTimeGPS(peak_time, peak_time_ns) in seglists[instrument]
119 connection.create_function("string_sngl_is_vetoed", 3, is_vetoed)
120
121
122for n, filename in enumerate(filenames):
123 #
124 # Open the database file.
125 #
126
127 if options.verbose:
128 print("%d/%d: %s" % (n + 1, len(filenames), filename), file=sys.stderr)
129
130 working_filename = dbtables.get_connection_filename(filename, tmp_path = options.tmp_space, verbose = options.verbose)
131 connection = sqlite3.connect(str(working_filename))
132
133 #
134 # Define the is_vetoed() SQL function.
135 #
136
137 create_string_sngl_is_vetoed_function(connection, options.vetoes_name)
138
139 #
140 # (Re)build table.
141 #
142
143 cursor = connection.cursor()
144 cursor.execute("""
145DROP TABLE IF EXISTS string_vetoed_sngl;
146 """)
147 cursor.execute("""
148CREATE TABLE string_vetoed_sngl (event_id TEXT PRIMARY KEY)
149 """)
150 cursor.execute("""
151INSERT OR REPLACE INTO
152 string_vetoed_sngl
153SELECT
154 sngl_burst.event_id AS event_id
155FROM
156 sngl_burst
157WHERE
158 string_sngl_is_vetoed(sngl_burst.ifo, sngl_burst.peak_time, sngl_burst.peak_time_ns)
159 """)
160 cursor.close()
161
162 #
163 # Clean up.
164
165 connection.close()
166 dbtables.discard_connection_filename(filename, working_filename, verbose = options.verbose)
167
168
169#
170# Done.
171#
def create_string_sngl_is_vetoed_function(connection, veto_segments_name=None)
Creates a function named string_sngl_is_vetoed in the database at connection.