gstlal-inspiral  0.4.2
 All Classes Namespaces Files Functions Variables Pages
gstlal_inspiral_reset_likelihood
1 #!/usr/bin/env python
2 #
3 # Copyright (C) 2009-2011 Kipp Cannon, Chad Hanna, Drew Keppel
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 ## @file
20 # may be deprecated soon; do not use at this time
21 
22 #
23 # =============================================================================
24 #
25 # Preamble
26 #
27 # =============================================================================
28 #
29 
30 
31 import sys
32 from optparse import OptionParser
33 
34 
35 from glue import segments
36 from glue.ligolw import ligolw
37 from glue.ligolw import lsctables
38 from glue.ligolw import array
39 from glue.ligolw import param
40 array.use_in(ligolw.LIGOLWContentHandler)
41 param.use_in(ligolw.LIGOLWContentHandler)
42 lsctables.use_in(ligolw.LIGOLWContentHandler)
43 from glue.ligolw import utils
44 from glue.ligolw.utils import process as ligolw_process
45 from glue.ligolw.utils import search_summary as ligolw_search_summary
46 
47 
48 from gstlal import far
49 from gstlal.inspiral import gen_likelihood_control_doc
50 
51 ## @file gstlal_inspiral_reset_likelihood
52 # This program resets the trials table and segments from files containing the distribution statistics for gstlal_inspiral jobs; see gstlal_inspiral_reset_likelihood for help and usage
53 
54 ## @package gstlal_inspiral_reset_likelihood
55 #
56 
57 
58 #
59 # =============================================================================
60 #
61 # Command Line
62 #
63 # =============================================================================
64 #
65 
66 
67 def parse_command_line():
68  parser = OptionParser()
69  parser.add_option("--marginalized-likelihood-file", metavar = "filename", help = "Set the name of the xml file containing the marginalized likelihood")
70  parser.add_option("--verbose", action = "store_true", help = "Be verbose.")
71  options, urls = parser.parse_args()
72  return options, urls
73 
74 
75 #
76 # =============================================================================
77 #
78 # Main
79 #
80 # =============================================================================
81 #
82 
83 
84 #
85 # parse command line
86 #
87 
88 
89 options, urls = parse_command_line()
90 
91 
92 #
93 # loop over input documents
94 #
95 
96 
97 for url in urls:
98  #
99  # load input document
100  #
101 
102  in_xmldoc = utils.load_url(url, verbose = options.verbose, contenthandler = ligolw.LIGOLWContentHandler)
103  likelihood_data = far.LocalRankingData.from_xml(in_xmldoc)
104  search_summary_row, = (row for row in lsctables.table.get_table(in_xmldoc, lsctables.SearchSummaryTable.tableName) if row.process_id == likelihood_data.distributions.process_id)
105  ifos = search_summary_row.instruments
106  # reset the clock to None
107  likelihood_data.livetime_seg = segments.segment(None,None)
108  # reset the trials table to 0
109  for k in likelihood_data.trials_table:
110  likelihood_data.trials_table[k].count = 0
111  likelihood_data.trials_table[k].count_below_thresh = 0
112 
113  xmldoc = gen_likelihood_control_doc(likelihood_data, ifos)
114 
115  utils.write_filename(xmldoc, url, gz = url.endswith(".gz"), verbose = options.verbose)
116 
117 # Reset the marginalized likelihood file if it exists too
118 if options.marginalized_likelihood_file is not None:
119 
120  marg, procid = far.RankingData.from_xml(utils.load_filename(options.marginalized_likelihood_file, contenthandler = ligolw.LIGOLWContentHandler, verbose = options.verbose))
121 
122  for k in marg.trials_table:
123  marg.trials_table[k].count = 0
124  marg.trials_table[k].count_below_thresh = 0
125 
126  marg.livetime_seg = segments.segment(None,None)
127 
128  xmldoc = ligolw.Document()
129  node = xmldoc.appendChild(ligolw.LIGO_LW())
130  node.appendChild(lsctables.New(lsctables.ProcessTable))
131  node.appendChild(lsctables.New(lsctables.ProcessParamsTable))
132  node.appendChild(lsctables.New(lsctables.SearchSummaryTable))
133  process = ligolw_process.register_to_xmldoc(xmldoc, u"gstlal_inspiral_reset_likelihood", options.__dict__)
134  search_summary = ligolw_search_summary.append_search_summary(xmldoc, process)
135  search_summary.out_segment = marg.livetime_seg
136  xmldoc.childNodes[-1].appendChild(marg.to_xml(process, search_summary))
137  ligolw_process.set_process_end_time(process)
138  outname = options.marginalized_likelihood_file
139  utils.write_filename(xmldoc, outname, gz = outname.endswith(".gz"), verbose = options.verbose)