28 matplotlib.rcParams.update({
30 "axes.titlesize": 10.0,
31 "axes.labelsize": 10.0,
32 "xtick.labelsize": 8.0,
33 "ytick.labelsize": 8.0,
34 "legend.fontsize": 8.0,
40 from matplotlib
import figure
41 from matplotlib.backends.backend_agg
import FigureCanvasAgg
as FigureCanvas
53 from gstlal
import pipeio
54 from gstlal.elements
import matplotlibcaps
57 __author__ =
"Kipp Cannon <kipp.cannon@ligo.org>"
75 "Generates a video showing a histogram of the input time series",
80 gst.PadTemplate(
"sink",
84 "audio/x-raw-float, " +
85 "rate = (int) [1, MAX], " +
86 "channels = (int) [1, MAX], " +
87 "endianness = (int) BYTE_ORDER, " +
88 "width = (int) {32, 64};" +
90 "rate = (int) [1, MAX], " +
91 "channels = (int) [1, MAX], " +
92 "endianness = (int) BYTE_ORDER, " +
95 "signed = (bool) {true, false}; " +
97 "rate = (int) [1, MAX], " +
98 "channels = (int) [1, MAX], " +
99 "endianness = (int) BYTE_ORDER, " +
100 "width = (int) 64," +
101 "depth = (int) 64," +
102 "signed = (bool) {true, false}"
105 gst.PadTemplate(
"src",
108 gst.caps_from_string(
109 matplotlibcaps +
", " +
110 "width = (int) [1, MAX], " +
111 "height = (int) [1, MAX], " +
112 "framerate = (fraction) [0, MAX]"
119 gst.BaseTransform.__init__(self)
130 def do_set_caps(self, incaps, outcaps):
131 channels = incaps[0][
"channels"]
133 self.
buf = numpy.zeros((0, channels), dtype = pipeio.numpy_dtype_from_caps(incaps))
135 self.
in_rate = incaps[0][
"rate"]
136 self.
out_rate = outcaps[0][
"framerate"]
149 def do_get_unit_size(self, caps):
150 return pipeio.get_unit_size(caps)
153 def do_event(self, event):
154 if event.type == gst.EVENT_TAG:
155 tags = pipeio.parse_framesrc_tags(event.parse_tag())
162 def make_frame(self, samples, outbuf):
170 outbuf.timestamp = self.
t0 + int(round(float(int(outbuf.offset - self.
offset0) / self.
out_rate) * gst.SECOND))
171 outbuf.duration = self.
t0 + int(round(float(int(outbuf.offset_end - self.
offset0) / self.
out_rate) * gst.SECOND)) - outbuf.timestamp
177 fig = figure.Figure()
179 fig.set_size_inches(self.
out_width / float(fig.get_dpi()), self.
out_height / float(fig.get_dpi()))
180 axes = fig.gca(yscale =
"log", rasterized =
True)
181 for channel
in numpy.transpose(samples)[:]:
182 axes.hist(channel, bins = 101, histtype =
"step")
184 axes.set_xlabel(
r"Amplitude (%s)" % ((self.
sample_units is not None)
and (str(self.
sample_units)
or "dimensionless")
or "unkown units"))
185 axes.set_ylabel(
r"Count")
186 axes.set_title(
r"%s, %s (%.9g s -- %.9g s)" % (self.
instrument or "Unknown Instrument", self.
channel_name or "Unknown Channel", float(outbuf.timestamp) / gst.SECOND, float(outbuf.timestamp + outbuf.duration) / gst.SECOND))
193 rgba_buffer = fig.canvas.buffer_rgba(0, 0)
194 rgba_buffer_size = len(rgba_buffer)
200 outbuf[0:rgba_buffer_size] = rgba_buffer
201 outbuf.datasize = rgba_buffer_size
210 def do_transform(self, inbuf, outbuf):
216 self.
t0 = inbuf.timestamp
224 self.
buf = numpy.concatenate((self.
buf, pipeio.array_from_audio_buffer(inbuf)))
236 if len(self.
buf) < samples_per_frame:
243 return gst.FLOW_CUSTOM_SUCCESS
245 while len(self.
buf) >= 2 * samples_per_frame:
247 self.get_pad(
"src").push(self.
make_frame(self.
buf[:samples_per_frame], newoutbuf))
248 self.
buf = self.
buf[samples_per_frame:]
250 self.
buf = self.
buf[samples_per_frame:]
259 def do_transform_caps(self, direction, caps):
260 if direction == gst.PAD_SRC:
265 return self.get_pad(
"sink").get_fixed_caps_func()
267 elif direction == gst.PAD_SINK:
272 return self.get_pad(
"src").get_fixed_caps_func()
274 raise ValueError(direction)
277 def do_transform_size(self, direction, caps, size, othercaps):
280 if direction == gst.PAD_SRC:
286 bytes_per_frame = caps[0][
"width"] * caps[0][
"height"] * caps[0][
"bpp"] / 8
287 samples = int(size / bytes_per_frame) * samples_per_frame - len(self.
buf)
295 return samples * (othercaps[0][
"width"] // 8) * othercaps[0][
"channels"]
297 elif direction == gst.PAD_SINK:
303 frames = (int(size * 8 / caps[0][
"width"]) // caps[0][
"channels"] + len(self.
buf)) / samples_per_frame
315 return othercaps[0][
"width"] * othercaps[0][
"height"] * othercaps[0][
"bpp"] / 8
317 raise ValueError(direction)
325 gobject.type_register(lal_histogramplot)
327 __gstelementfactory__ = (
328 lal_histogramplot.__name__,