29from optparse
import OptionParser
30from matplotlib
import figure
31from matplotlib.backends.backend_agg
import FigureCanvasAgg
as FigureCanvas
34from igwn_ligolw
import lsctables
35from igwn_ligolw
import utils
as ligolw_utils
36from lalburst
import git_version
39__author__ =
"Kipp Cannon <kipp.cannon@ligo.org>"
40__version__ =
"git id %s" % git_version.id
41__date__ = git_version.date
54 parser = OptionParser(
55 version =
"Name: %%prog\n%s" % git_version.verbose_msg,
56 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."
58 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.")
59 parser.add_option(
"-x",
"--x-instrument", metavar =
"instrument", help =
"Plot this instrument's offsets along the x axis (required).")
60 parser.add_option(
"-y",
"--y-instrument", metavar =
"instrument", help =
"Plot this instrument's offsets along the y axis (required).")
61 parser.add_option(
"-n",
"--require-instruments", metavar =
"instrument[,instrument...]", help =
"Plot only time slides involving exactly these instruments.")
62 parser.add_option(
"-v",
"--verbose", action =
"store_true", help =
"Be verbose.")
63 options, filenames = parser.parse_args()
65 if not options.output:
66 raise ValueError(
"no output file specified")
68 if not options.x_instrument
or not options.y_instrument:
69 raise ValueError(
"must set instruments for x and y axes")
71 if options.require_instruments
is not None:
72 options.require_instruments = set(map(str.strip, options.require_instruments.split(
",")))
74 return options, (filenames
or [
None])
87 if row.time_slide_id
not in self.dict:
88 self.dict[row.time_slide_id] = {}
89 self.dict[row.time_slide_id][row.instrument] = row.offset
96lsctables.TimeSlideTable.append = time_slide_append
97lsctables.TimeSlideTable._end_of_columns = time_slide__end_of_columns
110 def __init__(self, xmldoc, x_instrument, y_instrument, require_instruments = None):
111 self.
fig = figure.Figure()
117 self.
axes.set_xlabel(
"%s Offset (s)" % x_instrument)
118 self.
axes.set_ylabel(
"%s Offset (s)" % y_instrument)
120 tisitable = lsctables.TimeSlideTable.get_table(xmldoc)
123 max_offset = min_offset = tisitable.dict.itervalues().next().itervalues().next()
126 for offsets
in tisitable.dict.itervalues():
127 if require_instruments
is None or require_instruments == set(offsets.keys()):
128 x.append(offsets[x_instrument])
129 y.append(offsets[y_instrument])
130 min_offset =
min(x + y)
131 max_offset =
max(x + y)
134 self.
axes.set_xlim([min_offset, max_offset])
135 self.
axes.set_ylim([min_offset, max_offset])
137 if require_instruments
is not None:
138 self.
axes.set_title(
"%d %s Time Slides" % (len(x),
"+".join(sorted(require_instruments))))
140 self.
axes.set_title(
"%d Time Slides" % len(x))
155for n, filename
in enumerate(filenames):
156 xmldoc = ligolw_utils.load_filename(filename, verbose = options.verbose)
159 print(
"plotting ...", file=sys.stderr)
160 plot =
Plot(xmldoc, options.x_instrument, options.y_instrument, require_instruments = options.require_instruments)
162 output = options.output.replace(
"%n",
"%d" % n)
164 print(
"writing %s ..." % output, file=sys.stderr)
165 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)