gstlal-inspiral  1.9.2
 All Classes Namespaces Files Functions
gstlal_inspiral_recompute_online_far_from_gracedb
1 #!/usr/bin/env python3
2 #
3 # Copyright (C) 2015 Cody Messick, Kipp Cannon, Chad Hanna
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 ### A program to recalculate likelihood values and FARs of gracedb events
20 
21 
22 #
23 # =============================================================================
24 #
25 # Preamble
26 #
27 # =============================================================================
28 #
29 
30 
31 from optparse import OptionParser
32 
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
42 
43 
44 #
45 # =============================================================================
46 #
47 # Command Line
48 #
49 # =============================================================================
50 #
51 
52 
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)")
58 
59  options, gid_list = parser.parse_args()
60 
61  return options, gid_list
62 
63 #
64 # =============================================================================
65 #
66 # Function Definitions
67 #
68 # =============================================================================
69 #
70 
71 def get_likelihood_files(gid_list, gracedb):
72  likelihood_files_dict = {}
73  marg_files_dict = {}
74  for gid in gid_list:
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)
78 
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")
81 
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())
84 
85 def reassign_likelihoods(likelihood_file, list_of_gid_xmldoc_pairs, fapfar, with_zerolag = False, verbose = False):
86  #
87  # Get necesary information from likelihood file, add zero lag rates to
88  # background rates if necessary and then re-make discrete and
89  # interpolated PDFs
90  #
91 
92  rankingstat, _ = far.parse_likelihood_control_doc(ligolw_utils.load_filename(likelihood_file, contenthandler = far.RankingStat.LIGOLWContentHandler, verbose = verbose))
93  if with_zerolag:
94  rankingstat.denominator.lnzerolagdensity = rankingstat.zerolag
95  rankingstat.finish()
96 
97  #
98  # Go through each coinc xmldoc and reassign the likelihood ratio value
99  #
100 
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])
108 
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,
116  verbose = verbose)
117 
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)))
119 
120 #
121 # =============================================================================
122 #
123 # Main
124 #
125 # =============================================================================
126 #
127 
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)