Guest User

Untitled

a guest
Jan 29th, 2017
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.45 KB | None | 0 0
  1. import sys, time
  2. import random
  3. import numpy as np
  4. import pyqtgraph as pg
  5. from PyQt4 import QtCore, QtGui
  6.  
  7. class ResultsViewer(QtGui.QWidget):
  8.     plots = {} #the currently displayed plot widgets
  9.     curves = {} #the currently displayed data that store the points
  10.  
  11.     def __init__(self):
  12.         super(ResultsViewer, self).__init__()
  13.         self.win = QtGui.QMainWindow()
  14.         self.win.setCentralWidget(self)
  15.         self.win.resize(800, 250)
  16.         self.win.setWindowTitle("Cavity Results Viewer")
  17.         self.grid = QtGui.QGridLayout(self)
  18.         self.win.setWindowFlags(self.win.windowFlags() | QtCore.Qt.WindowStaysOnTopHint)
  19.  
  20.         self.win.show()
  21.  
  22.     def reset_indicators(self):
  23.         #return #Uncomment this to skip modifying the CSS (resizing problem seems to go away!!!)
  24.         for plot in self.plots.keys():
  25.             self.plots[plot].setStyleSheet("""
  26.                                border-top: 5px solid yellow;
  27.                                border-radius: 12px;
  28.                                """)
  29.  
  30.     def set_indicator_border_color(self, cavnum, results):
  31.         #return #Uncomment this to skip modifying the CSS (resizing problem seems to go away!!!)
  32.         color = "lime" if results[cavnum]['decision'] else "red"
  33.         self.plots[cavnum].setStyleSheet("""
  34.                        border-top: 5px solid %s;
  35.                        border-radius: 12px;
  36.                        """ % color)
  37.  
  38.     def create_indicators(self, params):
  39.         for c, cavnum in enumerate(params.keys()):
  40.             r = (4 % (c + 1)) / 4 #every fourth column jump to next row
  41.  
  42.             box = QtGui.QHBoxLayout()
  43.  
  44.             plt = pg.PlotWidget()
  45.            
  46.             plt.setStyleSheet("""
  47.                        border-top: 5px solid yellow;
  48.                        border-radius: 12px;
  49.                    """)
  50.  
  51.             curve_blue = plt.plotItem.plot(x=[0.1],y=[0.1]) #points for showing history data
  52.             curve_green = plt.plotItem.plot(x=[0.1],y=[0.1])
  53.  
  54.  
  55.             plt.plotItem.setLabel('left', "Amplitude", units='A')
  56.             plt.plotItem.setLabel('bottom', "Frequency", units='Hz')
  57.             self.plots[cavnum] = plt
  58.             self.curves[cavnum] = {"blue": curve_blue,
  59.                                    "green": curve_green}
  60.             box.addWidget(plt)
  61.             self.grid.addLayout(box, r, c % 4)
  62.  
  63.     def update(self, cavnum, results):
  64.         #update the plots and render all points
  65.         result = results[cavnum]
  66.         if result.has_key('peaks'):
  67.             peaks = result['peaks']
  68.  
  69.             if peaks.has_key('amps'):
  70.                 amps = peaks['amps']
  71.                 freqs = peaks['freqs']
  72.  
  73.                 x_blue, y_blue = self.curves[cavnum]['blue'].getData()
  74.  
  75.                 x_blue = np.append(x_blue, freqs)
  76.                 y_blue = np.append(y_blue, amps )
  77.  
  78.                 self.curves[cavnum]['blue'].setData(x_blue, y_blue)
  79.  
  80.         self.set_indicator_border_color(cavnum, results)
  81.  
  82. class MyThread(QtCore.QThread):
  83.     update = QtCore.pyqtSignal(int, object)
  84.  
  85.     def __init__(self, results, parent=None):
  86.         super(MyThread, self).__init__(parent)
  87.         self.results = results #number of plots
  88.  
  89.     def run(self):
  90.         while True:
  91.             for cavnum in range(8):
  92.                 time.sleep(1)
  93.                 peaks = {'amps': np.random.rand(3,1), 'freqs': np.random.rand(3,1)}
  94.                 self.results[cavnum]['decision'] = bool(random.getrandbits(1))
  95.                 self.results[cavnum]['peaks'] = peaks
  96.                 self.update.emit(cavnum, self.results)
  97.  
  98.  
  99. # create the dialog for zoom to point
  100. class MainApp(QtGui.QMainWindow):
  101.     def __init__(self, parent=None):
  102.         super(MainApp, self).__init__(parent)
  103.         # Set up the user interface from Designer.
  104.  
  105.         n = 8
  106.         results = {}
  107.         for x in range(n):
  108.             peaks = {'amps': np.random.rand(3,1), 'freqs': np.random.rand(3,1)}
  109.             results[x] = {'decision' : bool(random.getrandbits(1)) ,'peaks':peaks}
  110.  
  111.         self.thread = MyThread(results)
  112.         self.thread.update.connect(self.update)
  113.  
  114.         self.viewer = ResultsViewer()
  115.         self.viewer.create_indicators(results)
  116.  
  117.         self.thread.start()
  118.  
  119.     def update(self, cavnum, data):
  120.         self.viewer.update(cavnum, data)
  121.  
  122. if __name__ == "__main__":
  123.     app = QtGui.QApplication([])
  124.     widget = MainApp()
  125.     widget.move(300, 300)
  126.     widget.show()
  127.     sys.exit(app.exec_())
Advertisement
Add Comment
Please, Sign In to add comment