Loading [MathJax]/extensions/TeX/AMSsymbols.js
LALBurst 2.0.7.1-b246709
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Macros Modules Pages
calc_likelihood.py
Go to the documentation of this file.
1# Copyright (C) 2007--2021 Kipp Cannon
2#
3# This program is free software; you can redistribute it and/or modify it
4# under the terms of the GNU General Public License as published by the
5# Free Software Foundation; either version 2 of the License, or (at your
6# option) any later version.
7#
8# This program is distributed in the hope that it will be useful, but
9# WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
11# Public License for more details.
12#
13# You should have received a copy of the GNU General Public License along
14# with this program; if not, write to the Free Software Foundation, Inc.,
15# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16
17
18#
19# =============================================================================
20#
21# Preamble
22#
23# =============================================================================
24#
25
26
27import sys
28from tqdm import tqdm
29import traceback
30
31
32from igwn_ligolw import lsctables
33
34
35__author__ = "Kipp Cannon <kipp.cannon@ligo.org>"
36from .git_version import date as __date__
37from .git_version import version as __version__
38
39
40#
41# =============================================================================
42#
43# Library Interface
44#
45# =============================================================================
46#
47
48
49#
50# Core routine
51#
52
53
54def assign_likelihood_ratios(connection, coinc_def_id, offset_vectors, vetoseglists, events_func, veto_func, ln_likelihood_ratio_func, verbose = False):
55 """
56 Assigns likelihood ratio values to coincidences.
57 """
58 #
59 # Create a cursor object for events_func() to reuse
60 #
61
62 cursor = connection.cursor()
63
64 #
65 # Construct the in-SQL likelihood ratio function. Rely on Python's
66 # closure mechanism to retain all local variables at the time of
67 # this function's creation for use inside the function.
68 #
69
70 def ln_likelihood_ratio(coinc_event_id, time_slide_id):
71 try:
72 return ln_likelihood_ratio_func([event for event in events_func(cursor, coinc_event_id) if veto_func(event, vetoseglists)], offset_vectors[time_slide_id])
73 except:
74 traceback.print_exc()
75 raise
76
77 connection.create_function("ln_likelihood_ratio", 2, ln_likelihood_ratio)
78
79 #
80 # Iterate over all coincs, assigning likelihood ratios.
81 #
82
83 if verbose:
84 print("computing likelihood ratios ...", file=sys.stderr)
85
86 connection.cursor().execute("""
87UPDATE
88 coinc_event
89SET
90 likelihood = ln_likelihood_ratio(coinc_event_id, time_slide_id)
91WHERE
92 coinc_def_id == ?
93 """, (coinc_def_id,))
94
95 #
96 # Done
97 #
98
99 connection.commit()
100 cursor.close()
101
102
103def assign_likelihood_ratios_xml(xmldoc, coinc_def_id, offset_vectors, vetoseglists, events_func, veto_func, ln_likelihood_ratio_func, verbose = False):
104 """
105 Assigns likelihood ratio values to coincidences (XML version).
106 """
107 #
108 # Iterate over all coincs, assigning likelihood ratios.
109 #
110
111 for coinc_event in tqdm(lsctables.CoincTable.get_table(xmldoc), desc = "computing ln L", disable = not verbose):
112 if coinc_event.coinc_def_id != coinc_def_id:
113 continue
114 coinc_event.likelihood = ln_likelihood_ratio_func([event for event in events_func(None, coinc_event.coinc_event_id) if veto_func(event, vetoseglists)], offset_vectors[coinc_event.time_slide_id])
115
116 #
117 # Done
118 #
119
120 return
121
122
123#
124# Burst-specific interface
125#
126
127
128def sngl_burst_events_func(cursor, coinc_event_id, row_from_cols):
129 return map(row_from_cols, cursor.execute("""
130SELECT
131 sngl_burst.*
132FROM
133 sngl_burst
134 JOIN coinc_event_map ON (
135 coinc_event_map.table_name == 'sngl_burst'
136 AND coinc_event_map.event_id == sngl_burst.event_id
137 )
138WHERE
139 coinc_event_map.coinc_event_id == ?
140 """, (coinc_event_id,)))
141
142
143def sngl_burst_veto_func(event, vetoseglists):
144 # return True if event should be *retained*
145 return event.ifo not in vetoseglists or event.peak not in vetoseglists[event.ifo]
def sngl_burst_events_func(cursor, coinc_event_id, row_from_cols)
def assign_likelihood_ratios_xml(xmldoc, coinc_def_id, offset_vectors, vetoseglists, events_func, veto_func, ln_likelihood_ratio_func, verbose=False)
Assigns likelihood ratio values to coincidences (XML version).
def assign_likelihood_ratios(connection, coinc_def_id, offset_vectors, vetoseglists, events_func, veto_func, ln_likelihood_ratio_func, verbose=False)
Assigns likelihood ratio values to coincidences.