3 # Copyright (C) 2015 Cody Messick
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 from optparse
import OptionParser
23 from io
import StringIO
25 from ligo.lw
import lsctables
26 from ligo.lw
import utils as ligolw_utils
27 from ligo.lw.utils
import segments as ligolw_segments
28 from gstlal
import far
29 from gstlal.plots
import sensitivity as plotsensitivity
30 from lal
import GPSTimeNow
33 # =============================================================================
37 # =============================================================================
40 def parse_command_line():
41 parser = OptionParser(usage=
"%prog [options] database.sqlite")
42 parser.add_option(
"--png-output", metavar =
"filename", help =
"Path (including filename) of output png")
43 parser.add_option(
"--base64", action =
"store_true", help =
"Save image as sensitivity_base64.txt in directory program is run from, for use by gstlalcbcsummary in online analysis")
44 parser.add_option(
"--cumulative-segment-file", metavar =
"filename", help =
"Path of cumulative segments file")
45 parser.add_option(
"--marginalized-likelihood-file", metavar =
"filename", help =
"Path of marginalized likelihood file")
46 parser.add_option(
"--verbose", action =
"store_true", help =
"Be verbose")
47 parser.add_option(
"--min-inj-distance", type =
"float", help =
"Minimum injection distance in Mpc")
48 parser.add_option(
"--max-inj-distance", type =
"float", help =
"Maximum injection distance in Mpc")
49 parser.add_option(
"--simdb-query-end-time", type =
"float", help =
"The end of the last interval of time which a simdb query was downloaded for")
50 parser.add_option(
"--plot-length", metavar =
"seconds", type =
"float", default = 86400., help =
"Length in seconds of time to plot (warning: not tested) [default 86400]")
51 parser.add_option(
"--width", metavar =
"inches", default = 7.5, help =
"width of output image in inches. [Default: 7.5]")
52 parser.add_option(
"--height", metavar =
"inches", default = 4.5, help =
"height of output image in inches. [Default: 4.5]")
54 options, database = parser.parse_args()
56 if len(database) != 1:
57 raise ValueError(
"must provide one and only one database, %d appear to have been provided" % len(database))
59 if options.max_inj_distance <= options.min_inj_distance:
60 raise ValueError(
"--max-inj-distance must be larger than --min-inj-distance")
62 found_inj, missed_inj, seglistdicts, seg_bins = plotsensitivity.parse_sensitivity_docs(database[0], options.cumulative_segment_file, options.simdb_query_end_time)
64 return options, found_inj, missed_inj, seglistdicts, seg_bins
67 # =============================================================================
71 # =============================================================================
78 options, found_inj, missed_inj, seglistdicts, seg_bins = parse_command_line()
80 horizon_history = far.parse_likelihood_control_doc(ligolw_utils.load_filename(options.marginalized_likelihood_file, contenthandler=far.RankingStat.LIGOLWContentHandler, verbose = options.verbose))[0].numerator.horizon_history
86 thi = float(GPSTimeNow())
87 tlo = thi - options.plot_length
88 # FIXME The figure must be made 1.5 inches shorter in width than the ending
89 # width in order for the hacky save method needed for the legends to work
90 fig = matplotlib.pyplot.figure(figsize = (options.width-1.5, options.height))
91 fig, lgd1, lgd2 = plotsensitivity.plot_missedfound_range_segments(found_inj, missed_inj, seglistdicts, seg_bins, (tlo, thi), (options.min_inj_distance, options.max_inj_distance), horizon_history, fig = fig)
93 # FIXME This is a hacky way to force matplotlib to save the image with legends
94 # outside of the plots. tight_layout() may negate the need for this
95 if options.png_output:
96 fig.savefig(options.png_output, format=
"png", additional_artists=[lgd1, lgd2], bbox_inches=matplotlib.transforms.Bbox([[0.,0.],[options.width,options.height]]))
99 f = StringIO.StringIO()
100 fig.savefig(f, format=
"png", additional_artists=[lgd1, lgd2], bbox_inches=matplotlib.transforms.Bbox([[0.,0.],[options.width,options.height]]))
101 with open(
'./sensitivity_base64.txt',
'w') as sensitivity_file:
102 sensitivity_file.write(base64.b64encode(f.getvalue()))