-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlogger.py
263 lines (203 loc) · 8.08 KB
/
logger.py
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
"""
Sergey Tomin, DESY, 2021
"""
import sys
import numpy as np
import json
from PyQt5 import QtGui, QtCore
from PyQt5.QtWidgets import QWidget, QMessageBox, QApplication
import pyqtgraph as pg
from gui.UILogger import Ui_Form
import time
import os
from threading import Thread, Event
import logging
# filename="logs/afb.log",
#logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
class Statistics(Thread):
def __init__(self):
super(Statistics, self).__init__()
self._stop_event = Event()
self.do = None
self.delay = 0.1
def run(self):
while 1:
self.do()
logger.info("do")
time.sleep(self.delay)
def stop(self):
self._stop_event.set()
class UILogger(QWidget):
def __init__(self, parent=None):
#QWidget.__init__(self, parent)
super().__init__()
self.parent = parent
self.ui = Ui_Form()
self.ui.setupUi(self)
gui_index = self.parent.ui.get_style_name_index()
style_name = self.parent.gui_styles[gui_index]
self.loadStyleSheet(filename=self.parent.gui_dir + style_name)
self.mi = self.parent.mi
self.add_plot()
self.timer_live = pg.QtCore.QTimer()
self.timer_live.timeout.connect(self.plot_data)
self.ui.combo_log_ch_a.addItems(["max(spectrum)"])
self.ui.combo_log_ch_b.addItems(["max(av_spectrum)"])
self.ui.pb_start_log.clicked.connect(self.start_stop_logger)
#self.ui = self.parent.ui
def save_state(self, filename):
table = {}
#table["sr_period"] = self.sb_sr_period.value()
#with open(filename, 'w') as f:
# json.dump(table, f)
#logger.info("Save State")
pass
def load_state(self, filename):
#with open(filename, 'r') as f:
# table = json.load(f)
#if "sr_delay" in table: self.sb_sr_delay.setValue(table["sr_delay"])
#logger.info("Load State")
pass
def closeEvent(self, QCloseEvent):
self.stop_logger()
def start_stop_logger(self):
if self.ui.pb_start_log.text() == "Stop":
self.stop_logger()
else:
if self.parent.ui.pb_start.text() == "Start":
self.error_box("Start spectrometer first")
return
if not self.parent.spectrometer.is_online():
self.error_box("Spectrometer is not ONLINE")
return
#self.counter_spect = 0
#self.data_2d = np.zeros((self.spectrometer.num_px, self.sb_2d_hist_size))
self.line1 = []
self.line2 = []
self.line3 = []
self.line4 = []
self.timer_live.start(100)
self.ui.pb_start_log.setText("Stop")
self.ui.pb_start_log.setStyleSheet("color: rgb(63, 191, 95); font-size: 18pt")
def plot_data(self):
spec = self.parent.spectrum_event_disp
av_spec = self.parent.ave_spectrum
if len(self.line1) >= self.ui.sb_nponts.value():
self.line1 = self.line1[1:]
self.line2 = self.line2[1:]
self.line3 = self.line3[1:]
self.line4 = self.line4[1:]
try:
maxspec = np.amax(spec)
except ValueError:
maxspec = 0
try:
maxspec_av = np.amax(av_spec)
except ValueError:
maxspec_av = 0
self.line1 = np.append(self.line1, maxspec)
self.line2 = np.append(self.line2, maxspec_av)
x = np.arange(len(self.line1))
if self.ui.combo_log_ch_a.currentIndex() == 0:
self.single.setData(x=x, y=self.line1)
if self.ui.combo_log_ch_b.currentIndex() == 0:
self.average.setData(x=x, y=self.line2)
def add_plot(self):
gui_index = self.parent.ui.get_style_name_index()
if "standard" in self.parent.gui_styles[gui_index]:
pg.setConfigOption('background', 'w')
pg.setConfigOption('foreground', 'k')
single_pen = pg.mkPen("k")
else:
single_pen = pg.mkPen("w")
win = pg.GraphicsLayoutWidget()
# justify='right',,
self.label = pg.LabelItem(justify='left', row=0, col=0)
win.addItem(self.label)
# self.plot1 = win.addPlot(row=0, col=0)
self.plot1 = win.addPlot(row=1, col=0)
self.label2 = pg.LabelItem(justify='right')
win.addItem(self.label2, row=0, col=0)
self.plot1.setLabel('left', "A", units='au')
self.plot1.setLabel('bottom', "", units='n shots')
self.plot1.showGrid(1, 1, 1)
self.plot1.getAxis('left').enableAutoSIPrefix(enable=False) # stop the auto unit scaling on y axes
layout = QtGui.QGridLayout()
self.ui.widget_log.setLayout(layout)
layout.addWidget(win, 0, 0)
self.plot1.setAutoVisible(y=True)
self.plot1.addLegend()
self.single = pg.PlotCurveItem(pen=single_pen, name='single')
self.plot1.addItem(self.single)
pen = pg.mkPen((51, 255, 51), width=2)
pen = pg.mkPen((255, 0, 0), width=3)
# self.average = pg.PlotCurveItem(x=[], y=[], pen=pen, name='average')
self.average = pg.PlotCurveItem(pen=pen, name='average')
self.plot1.addItem(self.average)
pen = pg.mkPen((0, 255, 255), width=2)
self.fit_func = pg.PlotCurveItem(pen=pen, name='Gauss Fit')
# self.plot1.addItem(self.fit_func)
# self.plot1.enableAutoRange(False)
# self.textItem = pg.TextItem(text="", border='w', fill=(0, 0, 0))
# self.textItem.setPos(10, 10)
pen = pg.mkPen((0, 100, 0), width=1)
# self.average = pg.PlotCurveItem(x=[], y=[], pen=pen, name='average')
self.back_plot = pg.PlotCurveItem(pen=pen, name='background')
# self.plot1.addItem(self.back_plot) ##################################### SS removed, as typically we don;t need it once start pySpectrometer
# cross hair
self.vLine = pg.InfiniteLine(angle=90, movable=False)
self.hLine = pg.InfiniteLine(angle=0, movable=False)
self.plot1.addItem(self.vLine, ignoreBounds=True)
self.plot1.addItem(self.hLine, ignoreBounds=True)
# self.plot1.sigRangeChanged.connect(self.zoom_signal)
def stop_logger(self):
self.timer_live.stop()
logger.info("Stop Logger")
self.timer_live.stop()
self.ui.pb_start_log.setStyleSheet("color: rgb(255, 0, 0); font-size: 18pt")
self.ui.pb_start_log.setText("Start")
def zoom_signal(self):
pass
#s_up = self.plot_y.viewRange()[0][0]
#s_down = self.plot_y.viewRange()[0][1]
def loadStyleSheet(self, filename):
"""
Sets the dark GUI theme from a css file.
:return:
"""
try:
self.cssfile = "gui/" + filename
with open(self.cssfile, "r") as f:
self.setStyleSheet(f.read())
except IOError:
logger.error('No style sheet found!')
def error_box(self, message):
QtGui.QMessageBox.about(self, "Error box", message)
def question_box(self, message):
#QtGui.QMessageBox.question(self, "Question box", message)
reply = QtGui.QMessageBox.question(self, "Question Box",
message,
QtGui.QMessageBox.Yes | QtGui.QMessageBox.No)
if reply==QtGui.QMessageBox.Yes:
return True
return False
def main():
#make pyqt threadsafe
QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_X11InitThreads)
#create the application
app = QApplication(sys.argv)
window = UILogger()
#show app
#window.setWindowIcon(QtGui.QIcon('gui/angry_manul.png'))
# setting the path variable for icon
path = os.path.join(os.path.dirname(sys.modules[__name__].__file__), 'gui/manul.png')
app.setWindowIcon(QtGui.QIcon(path))
window.show()
window.raise_()
#Build documentaiton if source files have changed
#os.system("cd ./docs && xterm -T 'Ocelot Doc Builder' -e 'bash checkDocBuild.sh' &")
#exit script
sys.exit(app.exec_())
if __name__ == "__main__":
main()