3 # Copyright (C) 2015 Cody Messick, Kipp Cannon, Chad Hanna
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.
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.
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.
19 ### A program to recalculate likelihood values and FARs of gracedb events
23 # =============================================================================
27 # =============================================================================
31 from optparse
import OptionParser
33 from ligo.lw
import lsctables
34 from ligo.lw
import utils as ligolw_utils
35 from ligo.lw.utils
import process as ligolw_process
36 from ligo
import segments
37 from gstlal
import far
38 from gstlal
import lvalert_helper
39 from ligo.gracedb
import rest as gracedb
40 from lalburst
import calc_likelihood
41 from lalinspiral
import thinca
45 # =============================================================================
49 # =============================================================================
53 def parse_command_line():
54 parser = OptionParser()
55 parser.add_option(
"--gracedb-service-url", default=
"%s" % gracedb.DEFAULT_SERVICE_URL, help =
"GraceDb service url to upload to (default: %s)" % gracedb.DEFAULT_SERVICE_URL)
56 parser.add_option(
"--with-zerolag", action =
"store_true", help =
"Add zerolag events to background events before re-assigning likelihood ratios (default: False)")
57 parser.add_option(
"-v",
"--verbose", action =
"store_true", help =
"Be verbose (default: False)")
59 options, gid_list = parser.parse_args()
61 return options, gid_list
64 # =============================================================================
66 # Function Definitions
68 # =============================================================================
71 def get_likelihood_files(gid_list, gracedb):
72 likelihood_files_dict = {}
75 coinc_xmldoc = lvalert_helper.get_coinc_xmldoc(gracedb, gid)
76 likelihood_files_dict.setdefault(ligolw_process.get_process_params(coinc_xmldoc,
"gstlal_inspiral",
"--ranking-stat-output")[0], []).append([gid, coinc_xmldoc])
77 marg_files_dict.setdefault(ligolw_process.get_process_params(coinc_xmldoc,
"gstlal_inspiral",
"--ranking-stat-pdf")[0], []).append(gid)
79 if len(marg_files_dict.keys()) > 1:
80 raise ValueError(
"the gracedb events specified have different marginalized likelihood files, all specified events need to come from the same analysis and thus share the same marginalized likelihood file")
82 _, rankingstatpdf = far.parse_likelihood_control_doc(ligolw_utils.load_filename(marg_files_dict.keys()[0], contenthandler = far.RankingStat.LIGOLWContentHandler))
83 return likelihood_files_dict, far.FAPFAR(rankingstatpdf.new_with_extinction())
85 def reassign_likelihoods(likelihood_file, list_of_gid_xmldoc_pairs, fapfar, with_zerolag = False, verbose = False):
87 # Get necesary information from likelihood file, add zero lag rates to
88 # background rates if necessary and then re-make discrete and
92 rankingstat, _ = far.parse_likelihood_control_doc(ligolw_utils.load_filename(likelihood_file, contenthandler = far.RankingStat.LIGOLWContentHandler, verbose = verbose))
94 rankingstat.denominator.lnzerolagdensity = rankingstat.zerolag
98 # Go through each coinc xmldoc and reassign the likelihood ratio value
101 for [gid, coinc_xmldoc] in list_of_gid_xmldoc_pairs:
102 print(
"likelihood ratio of %s before: %.6f\nFAR of %s before: %g" % (gid, lsctables.CoincTable.get_table(coinc_xmldoc)[0].likelihood, gid, lsctables.CoincInspiralTable.get_table(coinc_xmldoc)[0].combined_far))
103 coinc_def_id = lsctables.CoincDefTable.get_table(coinc_xmldoc).get_coinc_def_id(thinca.InspiralCoincDef.search, thinca.InspiralCoincDef.search_coinc_type, create_new = False)
104 sngl_inspiral_table_index = dict((row.event_id, row) for row in lsctables.SnglInspiralTable.get_table(coinc_xmldoc))
105 coinc_event_map_index = dict((row.coinc_event_id, []) for row in lsctables.CoincTable.get_table(coinc_xmldoc) if row.coinc_def_id == coinc_def_id)
106 for row in lsctables.CoincMapTable.get_table(coinc_xmldoc):
107 coinc_event_map_index[row.coinc_event_id].append(sngl_inspiral_table_index[row.event_id])
109 calc_likelihood.assign_likelihood_ratios_xml(coinc_xmldoc,
110 coinc_def_id = coinc_def_id,
111 offset_vectors = lsctables.TimeSlideTable.get_table(coinc_xmldoc).as_dict(),
112 vetoseglists = segments.segmentlistdict(),
113 events_func = lambda _, coinc_event_id: coinc_event_map_index[coinc_event_id],
114 veto_func = lambda event, vetoseglists: True,
# Obviously you would never veto the single gracedb event
115 ln_likelihood_ratio_func = rankingstat.ln_lr_from_triggers,
118 print(
"likelihood ratio of %s after: %.6f\nFAR of %s after: %g" % (gid, lsctables.CoincTable.get_table(coinc_xmldoc)[0].likelihood, gid, fapfar.far_from_rank(lsctables.CoincTable.get_table(coinc_xmldoc)[0].likelihood)))
121 # =============================================================================
125 # =============================================================================
128 options, gid_list = parse_command_line()
129 gracedb = gracedb.GraceDb(options.gracedb_service_url)
130 likelihood_files_dict, fapfar = get_likelihood_files(gid_list, gracedb)
131 for likelihood_file, list_of_gid_xmldoc_pairs in likelihood_files_dict.items():
132 reassign_likelihoods(likelihood_file, list_of_gid_xmldoc_pairs, fapfar, with_zerolag = options.with_zerolag, verbose = options.verbose)