3 # Copyright (C) 2019 Kipp Cannon
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.
22 # =============================================================================
26 # =============================================================================
31 from optparse
import OptionParser
35 from ligo.lw
import utils as ligolw_utils
36 from gstlal
import far
39 __author__ =
"Kipp Cannon <kipp.cannon@ligo.org>"
40 __version__ =
"git id %s" %
"" # FIXME
45 # =============================================================================
49 # =============================================================================
53 def parse_command_line():
54 parser = OptionParser(
55 version =
"Name: %%prog\n%s" %
"" # FIXME
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()
63 process_params = dict(options.__dict__)
65 return options, process_params, filenames
69 # =============================================================================
73 # =============================================================================
82 options, process_params, filenames = parse_command_line()
86 # loop over ranking statistic files
90 for filename in filenames:
95 xmldoc = ligolw_utils.load_filename(filename, verbose = options.verbose, contenthandler = far.RankingStat.LIGOLWContentHandler)
98 # extract ranking statistic object, and erase from XML tree
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)
109 if rankingstatpdf is not None:
110 elem = rankingstatpdf.get_xml_root(xmldoc, name)
111 elem.parentNode.removeChild(elem)
115 # compress horizon distance history. the outer loop makes a list
116 # to ensure no problems modifying the object being iterated over
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))]
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
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:
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.):
147 print >>sys.stderr,
"\"%s\": %s horizon history reduced to %.3g%% of original size" % (filename, instrument, 100. * j / (i + 1.))
150 rankingstat.numerator.horizon_history[instrument] = type(horizon_history)(items)
153 # re-insert into XML tree
156 far.gen_likelihood_control_doc(xmldoc, rankingstat, rankingstatpdf)
162 ligolw_utils.write_filename(xmldoc, filename, verbose = options.verbose)