-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStatus_Window.py
More file actions
282 lines (241 loc) · 10.4 KB
/
Status_Window.py
File metadata and controls
282 lines (241 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
'''
Window to display the current status of the equipment
Load the base with: "pyuic5 -x Base_Status_Window.ui -o Base_Status_Window.py"
'''
from Interfaces.Base_Status_Window import Ui_StatusWindow
from customwidgets import VarEntry, CustomViewBox
import numpy as np
import pyqtgraph as pg
from pyqtgraph.Qt import QtCore
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QComboBox
from datetime import datetime
from os.path import join
# Unfortuantly pyqtgraph prints lots of warnings, becuase it's logarthmic plotting
# Ignore the warnings here.
import warnings
warnings.filterwarnings("ignore")
class Status_Window(Ui_StatusWindow):
'''
The window to display information about the system.
Args:
widget : the QWidget that is the base for this window
gui : The main GUI this window adds onto, usually the process main window.
equipment : The Equipment handler object
'''
def __init__(self, widget, gui, equipment):
super(Status_Window, self).__init__()
# Connect the equipment handler and all it's signals
self.equip = equipment
self.equip.guiTrackedVarSignal.connect(self.trackedVariableSlot)
self.equip.updateTrackedVarSignal.connect(self.updateTrackedVarSlot)
self.equip.timerSignal.connect(self.timerSlot)
self.widget = widget
self.gui = gui
# Switch to using white background and black foreground
# Call before setupUi
pg.setConfigOption('background', 'w')
pg.setConfigOption('foreground', 'k')
self.setupUi(self.widget)
self.t0 = datetime.now()
self.trackedVarsWidgets = dict()
self.trackedVarsData = dict()
self.trackedrow = 0
# Setup plots
self.extraWindows = []
self.plots = [self.plot0, self.plot1, self.plot2] # Add more plots later maybe
self.plottedVars = dict()
self.pgPen = pg.mkPen(41, 128, 185)
for plot in self.plots:
self.setupPlot(plot)
# Open the default values
self.defaultVar = "Pressure" # We always want to plot pressure
#
def setupUi(self, widget):
super(Status_Window, self).setupUi(widget)
widget.setWindowTitle("Tip Status Window")
widget.setGUIRef(self.gui) # IMPORTANT, to make window closing work due to convoluted nature of Qt Designer classes
# Initlize the plotting widgets
self.plot0 = pg.PlotWidget(self.defaultPlotFrame)
self.plot0.setGeometry(QtCore.QRect(0, 0, 550, 400))
self.plot0.setObjectName("plot0")
# self.plot0.setDownsampling(auto=True)
self.plot1 = pg.PlotWidget(self.plotFrame)
self.plot1.setGeometry(QtCore.QRect(0, 0, 550, 400))
self.plot1.setObjectName("plot1")
self.plot1comboBox = QComboBox(self.plotFrame)
self.plot1comboBox.setGeometry(QtCore.QRect(450, 0, 100, 20))
self.plot1comboBox.setObjectName("plot1comboBox")
self.plot1comboBox.currentTextChanged.connect(lambda s: self.startPlotting(self.plot1, s))
self.plot2 = pg.PlotWidget(self.plotFrame)
self.plot2.setGeometry(QtCore.QRect(550, 0, 550, 400))
self.plot2.setObjectName("plot2")
self.plot2comboBox = QComboBox(self.plotFrame)
self.plot2comboBox.setGeometry(QtCore.QRect(1000, 0, 100, 20))
self.plot2comboBox.setObjectName("plot2comboBox")
self.plot2comboBox.currentTextChanged.connect(lambda s: self.startPlotting(self.plot2, s))
self.widget.setWindowIcon(QIcon(join('Interfaces','images','squid_tip.png')))
self.timerLabel.setText("")
#
def timerSlot(self, s):
self.timerLabel.setText(s)
def setupPlot(self, widget):
for k in list(self.plottedVars.keys()): # The plot is already in use, overwrite it
if self.plottedVars[k][0] == widget:
return
widget.setTitle("Choose Data For Display")
widget.setLabel('left',"")
widget.setLabel('bottom',"time (s)")
widget.setXRange(0,1)
widget.setYRange(0,1)
#
def trackedVariableSlot(self, create, name, units):
'''
Add or remove a tracked variable to the GUI. If a varaible is already
Args:
create (bool) : If True will add it, if False will remove.
name (str) : The name of the tracked varaible, will display.
units (str) : The units of the tracked varaible, will display. Ignored if deleting.
'''
if create:
# Create the widget
widget = VarEntry(self.variablesFrame, name, units)
widget.move(10, 32*self.trackedrow)
self.trackedVarsWidgets[name] = widget
self.trackedrow += 1
# Create the data buffer
newdata = np.zeros((1,2))
newdata[0,0] = (datetime.now() - self.t0).total_seconds()
newdata[0,1] = float(self.equip.info[name])
self.trackedVarsData[name] = newdata
for plot in [self.plot1comboBox, self.plot2comboBox]:
if name != self.defaultVar:
plot.addItem(name)
self.plotIfAvailible(name)
else:
if name in self.trackedVarsWidgets and (name != self.defaultVar):
if name != self.defaultVar:
widget = self.trackedVarsWidgets.pop(name)
widget.deleteLater()
self.trackedVarsData.pop(name)
for cb in [self.plot1comboBox, self.plot2comboBox]:
cb.removeItem(cb.findText(name))
#
def updateTrackedVarSlot(self, name):
'''
Update the value of a tracked variable. If a variable does not exist, command is
ignored. Value of varaible is taken from the EquipmentHandler.info[name]
Args:
name (str) : The name of the tracked varaible to update.
'''
try:
if name in self.trackedVarsWidgets:
val = self.equip.info[name]
self.trackedVarsWidgets[name].setValue(val)
# Update the data
t = (datetime.now() - self.t0).total_seconds()
self.trackedVarsData[name] = np.append(self.trackedVarsData[name], np.array([t, val]).reshape(1,2), axis=0)
if name in self.plottedVars:
self.updatePlot(name)
except KeyError:
print("KeyError Could not update " + name)
#
def reset(self):
'''
Restart the status window, normally used when re-loading a recipe.
Zeros out the data buffers, removing old data.
'''
self.t0 = datetime.now()
# Clear the plots
if self.plottedVars:
for k in list(self.plottedVars.keys()):
plot = self.plottedVars.pop(k)
plot[1].clear()
for plot in self.plots:
self.setupPlot(plot)
if self.trackedVarsWidgets: # Dicitonaries evaluate to False if they are empty, True otherwise
for k in list(self.trackedVarsWidgets.keys()):
saveix = 0
if k != self.defaultVar:
widget = self.trackedVarsWidgets.pop(k)
widget.deleteLater()
self.trackedVarsData.pop(k)
for cb in [self.plot1comboBox, self.plot2comboBox]:
cb.removeItem(cb.findText(k))
else:
saveix += 1
self.trackedVarsData.pop(k)
newdata = np.zeros((1,2))
newdata[0,0] = (datetime.now() - self.t0).total_seconds()
newdata[0,1] = float(self.equip.info[k])
self.trackedVarsData[k] = newdata
self.plotIfAvailible(k)
self.trackedrow = saveix
#
def plotIfAvailible(self, variable):
'''
Plots a variable to a new plot, if one it availible.
Args:
varaible (str) : The tracked varaible to plot
start (bool) : If True will start plotting, if False will stop.
logy (bool) : If True will make the y-axis logarithmic
'''
if variable == self.defaultVar:
self.startPlotting(self.plot0, variable)
for plot in [self.plot1, self.plot2]:
inuse = False
for k in list(self.plottedVars.keys()):
if self.plottedVars[k][0] == plot:
inuse = True
if not inuse:
self.startPlotting(plot, variable)
return
#
def startPlotting(self, plotWidget, variable):
'''
Starts plotting to a plot widget and creates and entry for it in self.plottedVars
Will overwrite a plot if it is already in use.
Args:
plot : The PlotWidget to plot onto.
variable (str) : The name of the tracked variable in self.equip.info to plot
'''
if variable in self.plottedVars: # If it's already plotted do nothing.
return
if variable not in self.equip.info:
#raise ValueError("Cannot plot, variable " + str(variable) + " not tracked")
print("Cannot plot, variable " + str(variable) + " not tracked")
try:
float(self.equip.info[variable])
except:
raise ValueError("Cannot plot, variable " + str(variable) + " is not numeric.")
#
inuse = None # The plot is already in use, overwrite it
for k in list(self.plottedVars.keys()):
if self.plottedVars[k][0] == plotWidget:
inuse = k
if inuse is not None:
self.plottedVars.pop(inuse)
plotWidget.clear()
#
data = self.trackedVarsData[variable]
curve = plotWidget.plot(data[:,0], data[:,1], pen=self.pgPen)
if variable == self.defaultVar:
plotWidget.setLogMode(0, 1)
plotWidget.enableAutoRange()
plotWidget.setTitle(variable)
self.plottedVars[variable] = [plotWidget, curve]
#
def updatePlot(self, variable):
'''
Updates a plot with new data.
Args:
variable (str) : The name of the tracked variable to update. Must be a key of
self.plottedVars
'''
if variable not in self.plottedVars:
raise ValueError("Cannot update plot, variable " + str(variable) + " not being plotted")
widget, curve = self.plottedVars[variable]
data = self.trackedVarsData[variable]
curve.setData(x=data[:,0], y=data[:,1]) # Update the plot
#
#