Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys, time
- import random
- import numpy as np
- import pyqtgraph as pg
- from PyQt4 import QtCore, QtGui
- class ResultsViewer(QtGui.QWidget):
- plots = {} #the currently displayed plot widgets
- curves = {} #the currently displayed data that store the points
- def __init__(self):
- super(ResultsViewer, self).__init__()
- self.win = QtGui.QMainWindow()
- self.win.setCentralWidget(self)
- self.win.resize(800, 250)
- self.win.setWindowTitle("Cavity Results Viewer")
- self.grid = QtGui.QGridLayout(self)
- self.win.setWindowFlags(self.win.windowFlags() | QtCore.Qt.WindowStaysOnTopHint)
- self.win.show()
- def reset_indicators(self):
- #return #Uncomment this to skip modifying the CSS (resizing problem seems to go away!!!)
- for plot in self.plots.keys():
- self.plots[plot].setStyleSheet("""
- border-top: 5px solid yellow;
- border-radius: 12px;
- """)
- def set_indicator_border_color(self, cavnum, results):
- #return #Uncomment this to skip modifying the CSS (resizing problem seems to go away!!!)
- color = "lime" if results[cavnum]['decision'] else "red"
- self.plots[cavnum].setStyleSheet("""
- border-top: 5px solid %s;
- border-radius: 12px;
- """ % color)
- def create_indicators(self, params):
- for c, cavnum in enumerate(params.keys()):
- r = (4 % (c + 1)) / 4 #every fourth column jump to next row
- box = QtGui.QHBoxLayout()
- plt = pg.PlotWidget()
- plt.setStyleSheet("""
- border-top: 5px solid yellow;
- border-radius: 12px;
- """)
- curve_blue = plt.plotItem.plot(x=[0.1],y=[0.1]) #points for showing history data
- curve_green = plt.plotItem.plot(x=[0.1],y=[0.1])
- plt.plotItem.setLabel('left', "Amplitude", units='A')
- plt.plotItem.setLabel('bottom', "Frequency", units='Hz')
- self.plots[cavnum] = plt
- self.curves[cavnum] = {"blue": curve_blue,
- "green": curve_green}
- box.addWidget(plt)
- self.grid.addLayout(box, r, c % 4)
- def update(self, cavnum, results):
- #update the plots and render all points
- result = results[cavnum]
- if result.has_key('peaks'):
- peaks = result['peaks']
- if peaks.has_key('amps'):
- amps = peaks['amps']
- freqs = peaks['freqs']
- x_blue, y_blue = self.curves[cavnum]['blue'].getData()
- x_blue = np.append(x_blue, freqs)
- y_blue = np.append(y_blue, amps )
- self.curves[cavnum]['blue'].setData(x_blue, y_blue)
- self.set_indicator_border_color(cavnum, results)
- class MyThread(QtCore.QThread):
- update = QtCore.pyqtSignal(int, object)
- def __init__(self, results, parent=None):
- super(MyThread, self).__init__(parent)
- self.results = results #number of plots
- def run(self):
- while True:
- for cavnum in range(8):
- time.sleep(1)
- peaks = {'amps': np.random.rand(3,1), 'freqs': np.random.rand(3,1)}
- self.results[cavnum]['decision'] = bool(random.getrandbits(1))
- self.results[cavnum]['peaks'] = peaks
- self.update.emit(cavnum, self.results)
- # create the dialog for zoom to point
- class MainApp(QtGui.QMainWindow):
- def __init__(self, parent=None):
- super(MainApp, self).__init__(parent)
- # Set up the user interface from Designer.
- n = 8
- results = {}
- for x in range(n):
- peaks = {'amps': np.random.rand(3,1), 'freqs': np.random.rand(3,1)}
- results[x] = {'decision' : bool(random.getrandbits(1)) ,'peaks':peaks}
- self.thread = MyThread(results)
- self.thread.update.connect(self.update)
- self.viewer = ResultsViewer()
- self.viewer.create_indicators(results)
- self.thread.start()
- def update(self, cavnum, data):
- self.viewer.update(cavnum, data)
- if __name__ == "__main__":
- app = QtGui.QApplication([])
- widget = MainApp()
- widget.move(300, 300)
- widget.show()
- sys.exit(app.exec_())
Advertisement
Add Comment
Please, Sign In to add comment