class VNAVibrationWidget(VNAWidget): def __init__(self): super(VNAVibrationWidget, self).__init__() self.setWindowTitle("VNA Vibration Analyzer") self.fft_plot = PlotWidget() self.mean_fft_plot = PlotWidget() self.mean_fft_plot.addLegend() self.plot_layout.addWidget(self.fft_plot) self.plot_layout.addWidget(self.mean_fft_plot) continuous_button = QPushButton("Continuous Acquire") break_button = QPushButton("Break") reset_button = QPushButton("Reset Average") self.button_layout.addWidget(continuous_button) self.button_layout.addWidget(break_button) self.button_layout.addWidget(reset_button) continuous_button.clicked.connect(self.continuous_acquire) break_button.clicked.connect(self.set_break_acquire) reset_button.clicked.connect(self.reset_averaging) self.reset_averaging() def reset_averaging(self): self.acquisition_number = 0 self.fft_points = {} self.mean_fft_points = {} self.mean_fft_plot.clear() def grab_trace(self): super(VNAVibrationWidget, self).grab_trace() self.acquisition_number += 1 # Frequency Axis sweep_time = self.get_vna().get_sweep_time() n_points = self.get_vna().get_points() self.fft_plot.clear() self.mean_fft_plot.clear() self.mean_fft_plot.plotItem.legend.setParent(None) self.mean_fft_plot.addLegend() for name, dataset, pen in [('mag', self.mags, 'r'), ('phase', self.phases, 'g')]: self.fft_points[name] = numpy.abs(rfft(dataset - dataset.mean())) if name not in self.mean_fft_points: self.mean_fft_points[name] = self.fft_points[name] else: self.mean_fft_points[name] += self.fft_points[name] self.mean_fft_points[name] /= float(self.acquisition_number-1) / self.acquisition_number self.fft_freqs = fftfreq(n_points, sweep_time / n_points)[:len(self.fft_points[name])] self.fft_plot.plot(self.fft_freqs, self.fft_points[name], pen=pen) self.mean_fft_plot.plot(self.fft_freqs, self.mean_fft_points[name], pen=pen, name=name) self.mean_fft_plot.autoRange() def save_trace(self): super(VNAVibrationWidget, self).save_trace() group = self.get_h5group() group['fft_freqs'] = self.fft_freqs for name in ('mag', 'phase'): group[name+'_fft'] = self.fft_points[name] group[name+'_mean_fft'] = self.mean_fft_points[name] dataserver_helpers.set_scale(group, 'fft_freqs', name+'_fft') dataserver_helpers.set_scale(group, 'fft_freqs', name+'_mean_fft') group.attrs['n_averages'] = self.acquisition_number group.file.close() def set_break_acquire(self): self.break_acquire = True def continuous_acquire(self): self.break_acquire = False while not self.break_acquire: self.grab_trace() self.message('Acquisition Number %d' % self.acquisition_number) QApplication.instance().processEvents()
class PyQtGraphDataPlot(QWidget): limits_changed = Signal() def __init__(self, parent=None): super(PyQtGraphDataPlot, self).__init__(parent) self._plot_widget = PlotWidget() self._plot_widget.getPlotItem().addLegend() self._plot_widget.setBackground((255, 255, 255)) self._plot_widget.setXRange(0, 10, padding=0) vbox = QVBoxLayout() vbox.addWidget(self._plot_widget) self.setLayout(vbox) self._plot_widget.getPlotItem().sigRangeChanged.connect( self.limits_changed) self.bins = 10 self.window = 100 self._curves = {} self._current_vline = None def add_curve(self, curve_id, curve_name, curve_color=QColor(Qt.blue), markers_on=False): pen = mkPen(curve_color, width=1) # this adds the item to the plot and legend plot = self._plot_widget.plot(stepMode=True, fillLevel=0, brush=(0, 0, 255, 150)) self._curves[curve_id] = plot def remove_curve(self, curve_id): curve_id = str(curve_id) if curve_id in self._curves: self._plot_widget.removeItem(self._curves[curve_id]) del self._curves[curve_id] self._update_legend() def _update_legend(self): # clear and rebuild legend (there is no remove item method for the legend...) self._plot_widget.clear() self._plot_widget.getPlotItem().legend.items = [] for curve in self._curves.values(): self._plot_widget.addItem(curve) if self._current_vline: self._plot_widget.addItem(self._current_vline) def redraw(self): pass def set_values(self, curve_id, data_x, data_y): curve = self._curves[curve_id] if len(data_y) > 0: y, x = numpy.histogram(data_y[-self.window:], self.bins) curve.setData(x, y) else: curve.clear() self._plot_widget.autoRange() def vline(self, x, color): if self._current_vline: self._plot_widget.removeItem(self._current_vline) self._current_vline = self._plot_widget.addLine(x=x, pen=color) def set_xlim(self, limits): # TODO: this doesn't seem to handle fast updates well self._plot_widget.setXRange(limits[0], limits[1], padding=0) def set_ylim(self, limits): self._plot_widget.setYRange(limits[0], limits[1], padding=0) def get_xlim(self): x_range, _ = self._plot_widget.viewRange() return x_range def get_ylim(self): _, y_range = self._plot_widget.viewRange() return y_range
class StatsWindow(QWidget): def __init__(self): super().__init__() self.setGeometry(150 + 1024, 150, 800, 600) main_box = QVBoxLayout() self.graph_rfi = PlotWidget() self.graph_rfi.setBackground("w") self.graph_rfi.setTitle("RFI", color="k") self.graph_rfi.plot = self.graph_rfi.plot([0,0], [0], pen=mkPen('k', width=1), stepMode=True) self.graph_cand = PlotWidget() self.graph_cand.setBackground("w") self.graph_cand.setTitle("Candidates", color="k") self.graph_cand.plot = self.graph_cand.plot([0,0], [0], pen=mkPen('k', width=1), stepMode=True) self.dist_plot = PlotWidget() self.dist_plot.setMouseEnabled(y=False) self.dist_plot.setBackground("w") self.dist_plot.setTitle("Full distribution", color="k") self.dist_plot.plot = self.dist_plot.plot([0,0], [0], pen=mkPen('k', width=1), stepMode=True) plots_box = QHBoxLayout() plots_box.addWidget(self.graph_rfi) plots_box.addWidget(self.graph_cand) main_box.addLayout(plots_box) main_box.addWidget(self.dist_plot) limits_box = QHBoxLayout() limits_box.setAlignment(Qt.AlignLeft) self.limits_choice = QComboBox() self.limits_choice.addItems(["DM", "MJD"]) self.limits_choice.setFixedWidth(100) limits_box.addWidget(self.limits_choice) self.start_limit = QLineEdit() self.start_limit.setPlaceholderText("from") self.start_limit.setFixedWidth(150) limits_box.addWidget(self.start_limit) self.end_limit = QLineEdit() self.end_limit.setPlaceholderText("to") self.end_limit.setFixedWidth(150) limits_box.addWidget(self.end_limit) self.apply_limits_button = QPushButton() self.apply_limits_button.setFixedWidth(150) self.apply_limits_button.setText("Remove limits") limits_box.addWidget(self.apply_limits_button) self.remove_label = QLabel() limits_box.addWidget(self.remove_label) main_box.addLayout(limits_box) self.setLayout(main_box) def _update(self, rfi_data, cand_data): y_rfi, x_rfi = histogram([cand[1] for cand in rfi_data], bins=min(len(rfi_data) + 1, 100)) self.graph_rfi.plot.setData(x_rfi, y_rfi) y_cand, x_cand = histogram([cand[1] for cand in cand_data], bins=min(len(cand_data) + 1, 100)) self.graph_cand.plot.setData(x_cand, y_cand) def update_dist_plot(self, data, extra_dec=False): y_dist, x_dist = histogram(data, bins=100) ax = self.dist_plot.getAxis("bottom") min_val = min(data) max_val = max(data) tick_vals = linspace(min_val, max_val, num=6) decimals = 2 + extra_dec * 4 ticks = [(val, "{:.{dec}f}".format(val, dec=decimals)) for val in tick_vals] ax.setTicks( [ticks, []]) ax.setStyle(tickLength=-5) self.dist_plot.plot.setData(x_dist, y_dist) self.dist_plot.autoRange()