-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathraw_data_plot.py
222 lines (179 loc) · 7.75 KB
/
raw_data_plot.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
#!/usr/bin/python3
import time
import pyqtgraph as pg
from PyQt5 import QtCore, QtGui, QtWidgets
from ws_data import WS_Data
from scale_line import Plot_Scale_Line
from dialogs import ChannelSelector, EventTable, ScaleHandler
class Raw_Data_Plot(QtGui.QWidget):
resized = QtCore.pyqtSignal()
def __init__(self, url=None):
"""
Initialize the UI and create ``WS_Data`` to connect to web socket
url: The url for web socket to connect with
"""
super().__init__()
if url is None:
ws_url = "ws://localhost:7777"
else:
ws_url = url
self.setWindowTitle("Decimated Data Plot")
self.timer_interval = 0.1
self.time_scale = 10
self.ws_data = WS_Data(url=ws_url, update_time_interval=self.timer_interval,
time_scale=self.time_scale)
self.curve_size = 27 # 1080 / 2 / 20
self.selected_channels = list(range(1, 64))
self.raw_data_mode = "Scan"
self.last_cursor = 0
self.channel_selector_win = None
self.event_table_win = None
self.scale_adjust_win = None
self.init_ui()
self.setup_signal_handler()
self.show()
def init_ui(self):
gridlayout = QtGui.QGridLayout(self)
self.mode_btn = QtGui.QPushButton("Change to Scroll Mode")
self.ch_select_btn = QtGui.QPushButton("Select Channels")
self.scale_adjust_btn = QtGui.QPushButton("Adjust Scales")
self.event_table_btn = QtGui.QPushButton("Show Events")
self.plot = pg.PlotWidget()
self.plot.setMenuEnabled(enableMenu=False)
self.plot.setMouseEnabled(x= False, y= False)
self.plot.setLimits(xMin=0, xMax=10)
self.plot.enableAutoRange(x=True, y=False)
self.scroll = QtGui.QScrollBar()
raw_plot_widget = QtGui.QWidget()
plot_layout = QtGui.QHBoxLayout(raw_plot_widget)
plot_layout.setSpacing(0)
plot_layout.addWidget(self.plot)
plot_layout.addWidget(self.scroll)
gridlayout.addWidget(self.mode_btn, 0, 0, 1, 1)
gridlayout.addWidget(self.ch_select_btn, 0, 1, 1, 1)
gridlayout.addWidget(self.scale_adjust_btn, 0, 2, 1, 1)
gridlayout.addWidget(self.event_table_btn, 0, 3, 1, 1)
gridlayout.addWidget(raw_plot_widget, 1, 0, 4, 4)
self.cursor = pg.InfiniteLine(pos=0)
self.plot.addItem(self.cursor)
self.scale_lines = Plot_Scale_Line(self.plot)
self.curves = list()
for i in range(64):
r = (i + 60 ) * 5 % 256
g = (i + 2 ) * 7 % 256
b = (i + 15 ) * 11 % 256
curve = self.plot.plot(pen=(r,g,b))
self.curves.append(curve)
self.update_curves_size()
self.event_lines = list()
#TODO increase number of event line
for i in range(20):
event_line = pg.InfiniteLine(pos=0)
self.event_lines.append(event_line)
self.plot.addItem(event_line)
event_line.hide()
def setup_signal_handler(self):
self.resized.connect(self.update_curves_size)
self.mode_btn.clicked.connect(self.raw_data_mode_handler)
self.ch_select_btn.clicked.connect(self.show_channel_select_window)
self.scale_adjust_btn.clicked.connect(self.show_scale_adjust_window)
self.event_table_btn.clicked.connect(self.show_event_table_window)
self.scroll.valueChanged.connect(self.update_curves_size)
self.timer = QtCore.QTimer()
self.timer.setInterval(self.timer_interval*1000)
self.timer.timeout.connect(self.update_raw_plot)
self.timer.start()
def update_curves_size(self):
geo = self.plot.frameGeometry()
scroll_val = self.scroll.value()
num_show_curves = geo.height() // self.curve_size
num_select_channels = len(self.selected_channels)
if num_select_channels < num_show_curves:
num_show_curves = len(self.selected_channels)
maxY = (num_select_channels - scroll_val) * 10 + 5
minY = (num_select_channels - scroll_val - num_show_curves) * 10 + 5
if minY < 0:
minY = 0
self.plot.setYRange(minY, maxY, padding=0)
self.scroll.setMaximum(num_select_channels - num_show_curves)
def update_raw_plot(self):
if not self.ws_data.ws.sock or not self.ws_data.ws.sock.connected:
return
if not self.ws_data.decimated_data_time:
return
cursor_time = self.ws_data.cursor
if cursor_time is None:
self.ws_data.cursor = 0
return
# init plot origin
if self.ws_data.plot_origin is None:
self.ws_data.plot_origin = cursor_time
if cursor_time <= self.last_cursor:
return
tol_start = time.time()
plot_data = self.ws_data.get_plot_decimated_data(mode=self.raw_data_mode,
channels=self.selected_channels)
for i in range(len(self.event_lines)):
self.event_lines[i].hide()
axis = self.plot.getAxis("bottom")
axis.setTicks([plot_data.axises_tick["bottom"]])
axis.setLabel("Time (sec)")
axis = self.plot.getAxis("left")
axis.setTicks([plot_data.axises_tick["left"]])
axis.setLabel("Voltage (uV)")
self.scale_lines.update(self.selected_channels,
plot_data.axises_tick["bottom"])
for i in range(64):
if i < len(self.selected_channels):
self.curves[i].show()
else:
self.curves[i].hide()
continue
self.curves[i].setData(plot_data.time_data, plot_data.channel_data[i])
while len(plot_data.trans_events) > 20:
plot_data.trans_events.pop(0)
for idx in range(len(plot_data.trans_events)):
self.event_lines[idx].setValue(plot_data.trans_events[idx]['time'])
self.event_lines[idx].show()
self.plot.setTitle("FPS: {:.1f}".format(1/(time.time()-tol_start)))
self.cursor.setValue(plot_data.cursor)
self.ws_data.clean_oudated_data(cursor_time)
if self.event_table_win:
self.event_table_win.update_event_table()
self.last_cursor = cursor_time
def raw_data_mode_handler(self):
if self.raw_data_mode == "Scan":
self.raw_data_mode = "Scroll"
self.mode_btn.setText("Change to Scan Mode")
self.cursor.hide()
elif self.raw_data_mode == "Scroll":
self.raw_data_mode = "Scan"
self.mode_btn.setText("Change to Scroll Mode")
self.cursor.show()
def show_scale_adjust_window(self):
if self.scale_adjust_win is None:
self.scale_adjust_win = ScaleHandler(self)
def show_channel_select_window(self):
if self.channel_selector_win is None:
self.channel_selector_win = ChannelSelector(self)
def show_event_table_window(self):
if self.event_table_win is None:
self.event_table_win = EventTable(self)
def resizeEvent(self, event):
self.resized.emit()
return super().resizeEvent(event)
def wheelEvent(self, event):
scroll_change = -(event.angleDelta().y() // 120)
scroll_val = self.scroll.value()
self.scroll.setValue(scroll_val + scroll_change)
def update_time_scale(self, time_scale):
self.time_scale = time_scale
self.plot.setLimits(xMin=0, xMax=time_scale)
self.ws_data.update_time_scale(time_scale)
def update_selected_channels(self, channels):
self.selected_channels = channels
self.update_curves_size()
if __name__ == "__main__":
app = QtGui.QApplication([])
plot = Raw_Data_Plot()
app.exec_()