Loading [MathJax]/extensions/TeX/AMSsymbols.js
LALBurst 2.0.7.1-5e288d3
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Macros Modules Pages
lalburst_plot_tisi.py
Go to the documentation of this file.
1##python
2#
3# Copyright (C) 2006,2013 Kipp Cannon
4#
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.
9#
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.
14#
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.
18
19
20#
21# =============================================================================
22#
23# Preamble
24#
25# =============================================================================
26#
27
28
29from optparse import OptionParser
30from matplotlib import figure
31from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
32import sys
33
34from igwn_ligolw import lsctables
35from igwn_ligolw import utils as ligolw_utils
36from lalburst import git_version
37
38
39__author__ = "Kipp Cannon <kipp.cannon@ligo.org>"
40__version__ = "git id %s" % git_version.id
41__date__ = git_version.date
42
43
44#
45# =============================================================================
46#
47# Command Line
48#
49# =============================================================================
50#
51
52
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."
57 )
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()
64
65 if not options.output:
66 raise ValueError("no output file specified")
67
68 if not options.x_instrument or not options.y_instrument:
69 raise ValueError("must set instruments for x and y axes")
70
71 if options.require_instruments is not None:
72 options.require_instruments = set(map(str.strip, options.require_instruments.split(",")))
73
74 return options, (filenames or [None])
75
76
77#
78# =============================================================================
79#
80# Time Slide Table Helper
81#
82# =============================================================================
83#
84
85
86def time_slide_append(self, row):
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
90
91
93 self.dict = {}
94
95
96lsctables.TimeSlideTable.append = time_slide_append
97lsctables.TimeSlideTable._end_of_columns = time_slide__end_of_columns
98
99
100#
101# =============================================================================
102#
103# Plot
104#
105# =============================================================================
106#
107
108
109class Plot(object):
110 def __init__(self, xmldoc, x_instrument, y_instrument, require_instruments = None):
111 self.fig = figure.Figure()
112 self.canvas = FigureCanvas(self.fig)
113 self.axes = self.fig.gca()
114
115 self.axes.grid(True)
116
117 self.axes.set_xlabel("%s Offset (s)" % x_instrument)
118 self.axes.set_ylabel("%s Offset (s)" % y_instrument)
119
120 tisitable = lsctables.TimeSlideTable.get_table(xmldoc)
121 # grab an offset from one of the diciontaries of offsets in
122 # the time slide table.
123 max_offset = min_offset = tisitable.dict.itervalues().next().itervalues().next()
124 x = []
125 y = []
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)
132
133 self.axes.plot(x, y, "k+")
134 self.axes.set_xlim([min_offset, max_offset])
135 self.axes.set_ylim([min_offset, max_offset])
136
137 if require_instruments is not None:
138 self.axes.set_title("%d %s Time Slides" % (len(x), "+".join(sorted(require_instruments))))
139 else:
140 self.axes.set_title("%d Time Slides" % len(x))
141
142
143#
144# =============================================================================
145#
146# Main
147#
148# =============================================================================
149#
150
151
152options, filenames = parse_command_line()
153
154
155for n, filename in enumerate(filenames):
156 xmldoc = ligolw_utils.load_filename(filename, verbose = options.verbose)
157
158 if options.verbose:
159 print("plotting ...", file=sys.stderr)
160 plot = Plot(xmldoc, options.x_instrument, options.y_instrument, require_instruments = options.require_instruments)
161
162 output = options.output.replace("%n", "%d" % n)
163 if options.verbose:
164 print("writing %s ..." % output, file=sys.stderr)
165 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)