gstlal-inspiral  1.9.3
 All Classes Namespaces Files Functions
gstlal_inspiral_compress_ranking_stat
1 #!/usr/bin/env python3
2 #
3 # Copyright (C) 2019 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 #
24 # Preamble
25 #
26 # =============================================================================
27 #
28 
29 
30 import math
31 from optparse import OptionParser
32 import sys
33 import numpy
34 
35 from ligo.lw import utils as ligolw_utils
36 from gstlal import far
37 
38 
39 __author__ = "Kipp Cannon <kipp.cannon@ligo.org>"
40 __version__ = "git id %s" % "" # FIXME
41 __date__ = "" # FIXME
42 
43 
44 #
45 # =============================================================================
46 #
47 # Command Line
48 #
49 # =============================================================================
50 #
51 
52 
53 def parse_command_line():
54  parser = OptionParser(
55  version = "Name: %%prog\n%s" % "" # FIXME
56  )
57  parser.add_option("-t", "--threshold", type = "float", default = 0.03, help = "Only keep horizon distance values that differ by this much, fractionally, from their neighbours (default = 0.03).")
58  parser.add_option("--remove-horizon-deviations", action = "store_true", help = "Remove horizon entries that display an uncharacteristic deviation in sensitivity from the non-zero mean.")
59  parser.add_option("--deviation-percent", type = "float", default = 0.50, help = "Remove horizon entries that deviate by this fraction from the non-zero mean.")
60  parser.add_option("-v", "--verbose", action = "store_true", help = "Be verbose.")
61  options, filenames = parser.parse_args()
62 
63  process_params = dict(options.__dict__)
64 
65  return options, process_params, filenames
66 
67 
68 #
69 # =============================================================================
70 #
71 # Main
72 #
73 # =============================================================================
74 #
75 
76 
77 #
78 # command line
79 #
80 
81 
82 options, process_params, filenames = parse_command_line()
83 
84 
85 #
86 # loop over ranking statistic files
87 #
88 
89 
90 for filename in filenames:
91  #
92  # load file
93  #
94 
95  xmldoc = ligolw_utils.load_filename(filename, verbose = options.verbose, contenthandler = far.RankingStat.LIGOLWContentHandler)
96 
97  #
98  # extract ranking statistic object, and erase from XML tree
99  #
100 
101  rankingstat, rankingstatpdf = far.parse_likelihood_control_doc(xmldoc)
102  if rankingstatpdf is not None and options.verbose:
103  print >>sys.stderr, "WARNING: \"%s\" contains a RankingStatPDF object, it is not a pure ranking statistic file, you might be using this program on the wrong files." % filename
104  # FIXME: don't hard-code object name
105  name = u"gstlal_inspiral_likelihood"
106  elem = rankingstat.get_xml_root(xmldoc, name)
107  elem.parentNode.removeChild(elem)
108  elem.unlink()
109  if rankingstatpdf is not None:
110  elem = rankingstatpdf.get_xml_root(xmldoc, name)
111  elem.parentNode.removeChild(elem)
112  elem.unlink()
113 
114  #
115  # compress horizon distance history. the outer loop makes a list
116  # to ensure no problems modifying the object being iterated over
117  #
118 
119  abs_ln_thresh = math.log1p(options.threshold)
120  for instrument, horizon_history in list(rankingstat.numerator.horizon_history.items()):
121  # GPS time / distance pairs
122  items = horizon_history.items()
123  if options.remove_horizon_deviations:
124  values = numpy.array(items)[:,1]
125  mean_horizon = values[values!=0].mean()
126  items = [item for item in items if item[1] < (mean_horizon * (1. + options.deviation_percent))]
127 
128  # compress array
129  j = 1
130  for i in range(1, len(items) - 1):
131  values = items[j - 1][1], items[i][1], items[i + 1][1]
132  # remove distances that are non-zero and differ
133  # fractionally from both neighbours by less than
134  # the selected threshold. always keep the first
135  # and last values
136  if values[0] > 0. and values[1] > 0. and values[2] > 0. and abs(math.log(values[1] / values[0])) < abs_ln_thresh and abs(math.log(values[1] / values[2])) < abs_ln_thresh:
137  continue
138  # remove distances that are 0 and surrounded by 0
139  # on both sides (basically the same as the last
140  # test, but we can't take log(0)).
141  if values == (0., 0., 0.):
142  continue
143  items[j] = items[i]
144  j += 1
145  del items[j:]
146  if options.verbose:
147  print >>sys.stderr, "\"%s\": %s horizon history reduced to %.3g%% of original size" % (filename, instrument, 100. * j / (i + 1.))
148 
149  # replace
150  rankingstat.numerator.horizon_history[instrument] = type(horizon_history)(items)
151 
152  #
153  # re-insert into XML tree
154  #
155 
156  far.gen_likelihood_control_doc(xmldoc, rankingstat, rankingstatpdf)
157 
158  #
159  # write to disk
160  #
161 
162  ligolw_utils.write_filename(xmldoc, filename, verbose = options.verbose)