def __init__(self, parent, freqs): self._parent = parent self._spectrum = None self._timestamp = 0 self._delayDraw = 1. / MAX_SPECTRUM_FPS self._axes = None self._canvas = None self._freqs = freqs pre = wx.PreDialog() self._ui = load_ui('DialogSpectrum.xrc') self._ui.LoadOnDialog(pre, parent, 'DialogSpectrum') self.PostCreate(pre) self._panelPlot = xrc.XRCCTRL(pre, 'panelPlot') self._buttonClose = xrc.XRCCTRL(pre, 'buttonClose') self.Bind(wx.EVT_BUTTON, self.__on_close, self._buttonClose) self.Bind(wx.EVT_CLOSE, self.__on_close) self.__setup_plot() self._toolbar = NavigationToolbar(self._canvas, self) sizer = self._panelPlot.GetSizer() sizer.Add(self._canvas, 1, wx.ALL | wx.GROW) sizer.Add(self._toolbar, 0, wx.LEFT | wx.EXPAND) self.Fit()
def __init__(self, parent): self._parent = parent self._toolbar = None self._timestamp = 0 self._delayDraw = 1. / MAX_TIMELINE_FPS self._axes = None self._canvas = None self._monitors = None pre = wx.PreDialog() self._ui = load_ui('DialogTimeline.xrc') self._ui.LoadOnDialog(pre, parent, 'DialogTimeline') self.PostCreate(pre) self._panelPlot = xrc.XRCCTRL(pre, 'panelPlot') self._button_Close = xrc.XRCCTRL(pre, 'buttonClose') self.Bind(wx.EVT_BUTTON, self.__on_close, self._button_Close) self.Bind(wx.EVT_CLOSE, self.__on_close) self.__setup_plot() self._toolbar = NavigationToolbar(self._canvas, self) sizer = self._panelPlot.GetSizer() sizer.Add(self._canvas, 1, wx.ALL | wx.GROW) sizer.Add(self._toolbar, 0, wx.LEFT | wx.EXPAND) self.Fit() self._timer = wx.Timer(self) self.Bind(wx.EVT_TIMER, self.__on_timer, self._timer)
class DialogTimeline(wx.Dialog, Legend): def __init__(self, parent): self._parent = parent self._toolbar = None self._timestamp = 0 self._delayDraw = 1. / MAX_TIMELINE_FPS self._axes = None self._canvas = None self._monitors = None pre = wx.PreDialog() self._ui = load_ui('DialogTimeline.xrc') self._ui.LoadOnDialog(pre, parent, 'DialogTimeline') self.PostCreate(pre) self._panelPlot = xrc.XRCCTRL(pre, 'panelPlot') self._button_Close = xrc.XRCCTRL(pre, 'buttonClose') self.Bind(wx.EVT_BUTTON, self.__on_close, self._button_Close) self.Bind(wx.EVT_CLOSE, self.__on_close) self.__setup_plot() self._toolbar = NavigationToolbar(self._canvas, self) sizer = self._panelPlot.GetSizer() sizer.Add(self._canvas, 1, wx.ALL | wx.GROW) sizer.Add(self._toolbar, 0, wx.LEFT | wx.EXPAND) self.Fit() self._timer = wx.Timer(self) self.Bind(wx.EVT_TIMER, self.__on_timer, self._timer) def __setup_plot(self): figure = Figure(facecolor='lightgrey') self._axes = figure.add_subplot(111) self._axes.set_title('Timeline') self._axes.set_xlabel('Time') self._axes.set_ylabel('Frequency (MHz)') self._axes.grid(True) locator = AutoDateLocator() formatter = AutoDateFormatter(locator) self._axes.xaxis.set_major_formatter(formatter) self._axes.xaxis.set_major_locator(locator) formatter = ScalarFormatter(useOffset=False) self._axes.yaxis.set_major_formatter(formatter) self._axes.yaxis.set_minor_locator(AutoMinorLocator(10)) self._canvas = FigureCanvas(self._panelPlot, -1, figure) self._canvas.mpl_connect('motion_notify_event', self.__on_motion) Legend.__init__(self, self._axes, self._canvas) def __on_timer(self, _event): self.set_monitors(self._monitors, True) def __on_motion(self, event): label = '' if event.xdata is not None and event.xdata >= 1: timestamp = num2epoch(event.xdata) label = datetime.datetime.fromtimestamp(timestamp).strftime('%c') self._toolbar.set_cursor_text(label) def __on_close(self, _event): evt = EventTimelineClose() wx.PostEvent(self._parent, evt) self.Destroy() def __clear_plots(self): Legend.clear(self) for child in self._axes.get_children(): gid = child.get_gid() if gid is not None and gid == 'plot': child.remove() def set_monitors(self, monitors, isLive): self._timer.Stop() self._monitors = monitors timestamp = time.time() if timestamp - self._timestamp > self._delayDraw: t1 = time.time() self._timestamp = timestamp tMin = None tMax = None height = SAMPLE_RATE / BINS height /= 1e6 self.__clear_plots() timeNow = epoch2num(time.time()) for monitor in monitors: freq = monitor.get_frequency() signals = [] periods = [] for period in monitor.get_periods(): tStart = epoch2num(period.start) if period.end is not None: tEnd = epoch2num(period.end) else: tEnd = timeNow periods.append([tStart, tEnd - tStart]) for signal in monitor.get_signals(): tStart = epoch2num(signal.start) if signal.end is not None: tEnd = epoch2num(signal.end) else: tEnd = timeNow tMin = min(tMin, tStart) tMax = max(tMax, tEnd) signals.append([tStart, tEnd - tStart]) colour = monitor.get_colour() self._axes.broken_barh(periods, [freq - height / 2, height], color=colour, alpha=0.2, gid='plot') self._axes.broken_barh(signals, [freq - height / 2, height], color=colour, gid='plot') self._axes.axhline(freq, color=colour, label='{:.6f}MHz'.format(freq), gid='plot') if isLive: tMax = timeNow self._axes.axvline(timeNow, color='black', linestyle='--', label='Latest', gid='plot') Legend.create(self) if tMax is None: self._axes.set_xlim(timeNow - 1. / 288, timeNow) self._axes.autoscale(axis='y') else: self._axes.set_xlim(tMin, tMax) self._axes.autoscale(self._toolbar.get_autoscale()) self._axes.get_figure().autofmt_xdate() self._canvas.draw() delay = time.time() - t1 self._delayDraw += delay * 2. self._delayDraw /= 2. if self._delayDraw < 1. / MAX_TIMELINE_FPS: self._delayDraw = 1. / MAX_TIMELINE_FPS if isLive: self._timer.Start(1000. / TIMELINE_FPS, True)
class DialogSpectrum(wx.Dialog, Legend): def __init__(self, parent, freqs): self._parent = parent self._spectrum = None self._timestamp = 0 self._delayDraw = 1. / MAX_SPECTRUM_FPS self._axes = None self._canvas = None self._freqs = freqs pre = wx.PreDialog() self._ui = load_ui('DialogSpectrum.xrc') self._ui.LoadOnDialog(pre, parent, 'DialogSpectrum') self.PostCreate(pre) self._panelPlot = xrc.XRCCTRL(pre, 'panelPlot') self._buttonClose = xrc.XRCCTRL(pre, 'buttonClose') self.Bind(wx.EVT_BUTTON, self.__on_close, self._buttonClose) self.Bind(wx.EVT_CLOSE, self.__on_close) self.__setup_plot() self._toolbar = NavigationToolbar(self._canvas, self) sizer = self._panelPlot.GetSizer() sizer.Add(self._canvas, 1, wx.ALL | wx.GROW) sizer.Add(self._toolbar, 0, wx.LEFT | wx.EXPAND) self.Fit() def __setup_plot(self): figure = Figure(facecolor='lightgrey') self._axes = figure.add_subplot(111) self._axes.set_title('Spectrum') self._axes.set_xlabel('Frequency (MHz)') self._axes.set_ylabel('Level (dB)') self._axes.autoscale_view(True, True, True) self._axes.grid(True) self._spectrum, = self._axes.plot([], [], 'b-', label='Spectrum') self._canvas = FigureCanvas(self._panelPlot, -1, figure) self._canvas.mpl_connect('motion_notify_event', self.__on_motion) Legend.__init__(self, self._axes, self._canvas) def __on_motion(self, event): label = '' if event.xdata is not None: freq = min(self._freqs, key=lambda x: abs(x - event.xdata)) label = '{: 8.4f}MHz'.format(freq) self._toolbar.set_cursor_text(label) def __on_close(self, _event): evt = EventSpectrumClose() wx.PostEvent(self._parent, evt) self.Destroy() def __clear_plots(self): Legend.clear(self) for child in self._axes.get_children(): gid = child.get_gid() if gid is not None and gid == 'line': child.remove() def set_spectrum(self, freqs, levels, monitors, noise): timestamp = time.time() self._freqs = freqs if timestamp - self._timestamp > self._delayDraw: t1 = time.time() self._timestamp = timestamp self.__clear_plots() self._axes.axhline(noise, color='black', ls='--', label='Noise level', gid='line') for monitor in monitors: colour = monitor.get_colour() freq = monitor.get_frequency() self._axes.axvline(freq, color=colour, dashes=[2, 1], label='{:.6f}MHz'.format(freq), gid='line') self._axes.axhline(monitor.get_threshold(), color=colour, dashes=[2, 1], gid='line') Legend.create(self) self._spectrum.set_data(freqs, levels) self._axes.relim() self._axes.autoscale_view(True, True, True) self._axes.autoscale(self._toolbar.get_autoscale()) self._canvas.draw() delay = time.time() - t1 self._delayDraw += delay * 2. self._delayDraw /= 2. if self._delayDraw < 1. / MAX_SPECTRUM_FPS: self._delayDraw = 1. / MAX_SPECTRUM_FPS def clear_spectrum(self): self.__clear_plots() self._canvas.draw()