LALBurst  2.0.4.1-89842e6
lalburst_plot_tisi.py
Go to the documentation of this file.
1 #
2 # Copyright (C) 2006,2013 Kipp Cannon
3 #
4 # This program is free software; you can redistribute it and/or modify it
5 # under the terms of the GNU General Public License as published by the
6 # Free Software Foundation; either version 2 of the License, or (at your
7 # option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
12 # Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License along
15 # with this program; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 
18 
19 #
20 # =============================================================================
21 #
22 # Preamble
23 #
24 # =============================================================================
25 #
26 
27 
28 from optparse import OptionParser
29 from matplotlib import figure
30 from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
31 import sys
32 
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
37 
38 
39 lsctables.use_in(ligolw.LIGOLWContentHandler)
40 
41 
42 __author__ = "Kipp Cannon <kipp.cannon@ligo.org>"
43 __version__ = "git id %s" % git_version.id
44 __date__ = git_version.date
45 
46 
47 #
48 # =============================================================================
49 #
50 # Command Line
51 #
52 # =============================================================================
53 #
54 
55 
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."
60  )
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()
67 
68  if not options.output:
69  raise ValueError("no output file specified")
70 
71  if not options.x_instrument or not options.y_instrument:
72  raise ValueError("must set instruments for x and y axes")
73 
74  if options.require_instruments is not None:
75  options.require_instruments = set(map(str.strip, options.require_instruments.split(",")))
76 
77  return options, (filenames or [None])
78 
79 
80 #
81 # =============================================================================
82 #
83 # Time Slide Table Helper
84 #
85 # =============================================================================
86 #
87 
88 
89 def time_slide_append(self, row):
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
93 
94 
96  self.dict = {}
97 
98 
99 lsctables.TimeSlideTable.append = time_slide_append
100 lsctables.TimeSlideTable._end_of_columns = time_slide__end_of_columns
101 
102 
103 #
104 # =============================================================================
105 #
106 # Plot
107 #
108 # =============================================================================
109 #
110 
111 
112 class Plot(object):
113  def __init__(self, xmldoc, x_instrument, y_instrument, require_instruments = None):
114  self.figfig = figure.Figure()
115  self.canvascanvas = FigureCanvas(self.figfig)
116  self.axesaxes = self.figfig.gca()
117 
118  self.axesaxes.grid(True)
119 
120  self.axesaxes.set_xlabel("%s Offset (s)" % x_instrument)
121  self.axesaxes.set_ylabel("%s Offset (s)" % y_instrument)
122 
123  tisitable = lsctables.TimeSlideTable.get_table(xmldoc)
124  # grab an offset from one of the diciontaries of offsets in
125  # the time slide table.
126  max_offset = min_offset = tisitable.dict.itervalues().next().itervalues().next()
127  x = []
128  y = []
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)
135 
136  self.axesaxes.plot(x, y, "k+")
137  self.axesaxes.set_xlim([min_offset, max_offset])
138  self.axesaxes.set_ylim([min_offset, max_offset])
139 
140  if require_instruments is not None:
141  self.axesaxes.set_title("%d %s Time Slides" % (len(x), "+".join(sorted(require_instruments))))
142  else:
143  self.axesaxes.set_title("%d Time Slides" % len(x))
144 
145 
146 #
147 # =============================================================================
148 #
149 # Main
150 #
151 # =============================================================================
152 #
153 
154 
155 options, filenames = parse_command_line()
156 
157 
158 for n, filename in enumerate(filenames):
159  xmldoc = ligolw_utils.load_filename(filename, verbose = options.verbose, contenthandler = ligolw.LIGOLWContentHandler)
160 
161  if options.verbose:
162  print("plotting ...", file=sys.stderr)
163  plot = Plot(xmldoc, options.x_instrument, options.y_instrument, require_instruments = options.require_instruments)
164 
165  output = options.output.replace("%n", "%d" % n)
166  if options.verbose:
167  print("writing %s ..." % output, file=sys.stderr)
168  plot.fig.savefig(output)
static double max(double a, double b)
Definition: EPFilters.c:43
static double min(double a, double b)
Definition: EPFilters.c:42
def __init__(self, xmldoc, x_instrument, y_instrument, require_instruments=None)
def time_slide__end_of_columns(self)
def time_slide_append(self, row)