28 from optparse
import OptionParser
29 from matplotlib
import figure
30 from matplotlib.backends.backend_agg
import FigureCanvasAgg
as FigureCanvas
33 from ligo.lw
import ligolw
34 from ligo.lw
import lsctables
35 from ligo.lw
import utils
as ligolw_utils
36 from lalburst
import git_version
39 lsctables.use_in(ligolw.LIGOLWContentHandler)
42 __author__ =
"Kipp Cannon <kipp.cannon@ligo.org>"
43 __version__ =
"git id %s" % git_version.id
44 __date__ = git_version.date
57 parser = OptionParser(
58 version =
"Name: %%prog\n%s" % git_version.verbose_msg,
59 description =
"Plot projections of the time slides contained in the input files onto a two-instrument cross section of the delay space. One plot is generated for each input file."
61 parser.add_option(
"-o",
"--output", metavar =
"filename", help =
"Set output file name (required). If the string \"%n\" occurs in the filename, it will be replaced with the plot number starting from 0. If the output filename does not contain a \"%n\" in it and more than one plot is generated then the plots will over-write one another.")
62 parser.add_option(
"-x",
"--x-instrument", metavar =
"instrument", help =
"Plot this instrument's offsets along the x axis (required).")
63 parser.add_option(
"-y",
"--y-instrument", metavar =
"instrument", help =
"Plot this instrument's offsets along the y axis (required).")
64 parser.add_option(
"-n",
"--require-instruments", metavar =
"instrument[,instrument...]", help =
"Plot only time slides involving exactly these instruments.")
65 parser.add_option(
"-v",
"--verbose", action =
"store_true", help =
"Be verbose.")
66 options, filenames = parser.parse_args()
68 if not options.output:
69 raise ValueError(
"no output file specified")
71 if not options.x_instrument
or not options.y_instrument:
72 raise ValueError(
"must set instruments for x and y axes")
74 if options.require_instruments
is not None:
75 options.require_instruments = set(map(str.strip, options.require_instruments.split(
",")))
77 return options, (filenames
or [
None])
90 if row.time_slide_id
not in self.dict:
91 self.dict[row.time_slide_id] = {}
92 self.dict[row.time_slide_id][row.instrument] = row.offset
99 lsctables.TimeSlideTable.append = time_slide_append
100 lsctables.TimeSlideTable._end_of_columns = time_slide__end_of_columns
113 def __init__(self, xmldoc, x_instrument, y_instrument, require_instruments = None):
114 self.
figfig = figure.Figure()
118 self.
axesaxes.grid(
True)
120 self.
axesaxes.set_xlabel(
"%s Offset (s)" % x_instrument)
121 self.
axesaxes.set_ylabel(
"%s Offset (s)" % y_instrument)
123 tisitable = lsctables.TimeSlideTable.get_table(xmldoc)
126 max_offset = min_offset = tisitable.dict.itervalues().next().itervalues().next()
129 for offsets
in tisitable.dict.itervalues():
130 if require_instruments
is None or require_instruments == set(offsets.keys()):
131 x.append(offsets[x_instrument])
132 y.append(offsets[y_instrument])
133 min_offset =
min(x + y)
134 max_offset =
max(x + y)
137 self.
axesaxes.set_xlim([min_offset, max_offset])
138 self.
axesaxes.set_ylim([min_offset, max_offset])
140 if require_instruments
is not None:
141 self.
axesaxes.set_title(
"%d %s Time Slides" % (len(x),
"+".join(sorted(require_instruments))))
143 self.
axesaxes.set_title(
"%d Time Slides" % len(x))
158 for n, filename
in enumerate(filenames):
159 xmldoc = ligolw_utils.load_filename(filename, verbose = options.verbose, contenthandler = ligolw.LIGOLWContentHandler)
162 print(
"plotting ...", file=sys.stderr)
163 plot =
Plot(xmldoc, options.x_instrument, options.y_instrument, require_instruments = options.require_instruments)
165 output = options.output.replace(
"%n",
"%d" % n)
167 print(
"writing %s ..." % output, file=sys.stderr)
168 plot.fig.savefig(output)
static double max(double a, double b)
static double min(double a, double b)
def __init__(self, xmldoc, x_instrument, y_instrument, require_instruments=None)
def time_slide__end_of_columns(self)
def time_slide_append(self, row)