-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGUI_utils.py
350 lines (302 loc) · 12.4 KB
/
GUI_utils.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
import copy
from pathlib import Path
import json
import datetime
from PyQt6 import QtWidgets, QtCore, QtGui
COLOR_PALETTE = ['#023eff', '#ff7c00', '#1ac938', '#e8000b', '#8b2be2', '#9f4800', '#f14cc1', '#a3a3a3', '#ffc400',
'#00d7ff', '#023eff', '#ff7c00', '#1ac938', '#e8000b', '#8b2be2', '#9f4800']
# 'bright' from seaborn
from enum import IntEnum, Enum, unique
MAX_GRAPHS = 4
@unique
class TimeBases(Enum):
def __new__(cls, string):
value = len(cls.__members__)
obj = object.__new__(cls)
obj.context = value
obj._value_ = string
obj.duration = int(string.split(" ")[0])
return obj
s20 = '20 s'
s10 = '10 s'
s5 = '5 s'
s2 = '2 s'
s1 = '1 s'
@unique
class YRanges(Enum):
def __new__(cls, string):
value = len(cls.__members__)
obj = object.__new__(cls)
obj.context = value
obj._value_ = string
return obj
birange_10 = '+-10 V'
birange_5 = '+-5 V'
birange_3 = '+-3.3 V'
range_10 = '0-10 V'
range_5 = '0-5 V'
range_3 = '0-3.3 V'
class PlotWindowEnum(IntEnum):
NotVisible = -1
A = 0
B = 1
C = 2
D = 3
E = 4
F = 5
G = 6
H = 7
J = 8
I = 9
K = 10
L = 11
class GraphSettings(QtWidgets.QWidget):
def __init__(self, nr_of_graphs=4, idx=0, parent=None):
super().__init__(parent)
self._nr_of_graphs = nr_of_graphs
self.idx = idx # indicating which viewer this belongs to
self.layout = QtWidgets.QVBoxLayout(self)
self.created_elements = []
self.create_settings_widget()
@property
def nr_of_graphs(self):
return self._nr_of_graphs
@nr_of_graphs.setter
def nr_of_graphs(self, value):
if 0 <= value <= 4:
self._nr_of_graphs = value
self.adjust_current_widget()
def get_current_settings(self) -> dict:
settings = {}
for idx in range(self._nr_of_graphs):
settings[self.graph_name_list[idx].text()] = {}
settings[self.graph_name_list[idx].text()]["Yrange"] = self.yrange_combo_list[idx].currentText()
settings[self.graph_name_list[idx].text()]["time_base"] = self.timebase_combo_list[idx].currentText()
return settings
def set_current_settings(self, settings):
for graph in settings.graphsettings.keys():
for idx in range(4):
if graph == self.graph_name_list[idx].text():
self.yrange_combo_list[idx].setCurrentText(settings.graphsettings[graph]["Yrange"])
self.timebase_combo_list[idx].setCurrentText(settings.graphsettings[graph]["time_base"])
def adjust_current_widget(self):
for idx in range(4):
if idx + 1 > self._nr_of_graphs:
self.timebase_combo_list[idx].setEnabled(False)
self.yrange_combo_list[idx].setEnabled(False)
self.graph_name_list[idx].setEnabled(False)
self.tb_list[idx].setEnabled(False)
self.yr_list[idx].setEnabled(False)
else:
self.timebase_combo_list[idx].setEnabled(True)
self.yrange_combo_list[idx].setEnabled(True)
self.graph_name_list[idx].setEnabled(True)
self.tb_list[idx].setEnabled(True)
self.yr_list[idx].setEnabled(True)
self.graph_name_list[idx].setText(PlotWindowEnum(MAX_GRAPHS * self.idx + idx).name)
def create_settings_widget(self):
# for _ in range(len(self.created_elements)):
# el = self.created_elements.pop()
# self.layout.removeWidget(el)
# #del el
# print(f'was called with {self._nr_of_graphs}')
self.timebase_combo_list = []
self.yrange_combo_list = []
self.graph_name_list = []
self.tb_list = []
self.yr_list = []
font = QtGui.QFont()
font.setPointSize(9)
distance_between_elements = 80
for idx in range(4):
graph_name = PlotWindowEnum(MAX_GRAPHS * self.idx + idx).name
TimeBaseCombo = QtWidgets.QComboBox(parent=self)
TimeBaseCombo.setGeometry(QtCore.QRect(100, 20 + distance_between_elements * idx, 86, 25))
TimeBaseCombo.setFont(font)
TimeBaseCombo.setObjectName(f"TimeBaseCombo_{idx}")
TimeBaseCombo.addItems([e.value for e in TimeBases])
self.timebase_combo_list.append(TimeBaseCombo)
YrangeCombo = QtWidgets.QComboBox(parent=self)
YrangeCombo.setGeometry(QtCore.QRect(100, 50 + distance_between_elements * idx, 86, 25))
YrangeCombo.setFont(font)
YrangeCombo.setObjectName(f"YrangeCombo_{idx}")
YrangeCombo.addItems([e.value for e in YRanges])
self.yrange_combo_list.append(YrangeCombo)
GName = QtWidgets.QLabel(parent=self)
GName.setGeometry(QtCore.QRect(0, 0 + distance_between_elements * idx, 91, 17))
GName.setObjectName(f"GName_{idx}")
GName.setText(graph_name)
self.graph_name_list.append(GName)
label_tb = QtWidgets.QLabel(parent=self)
label_tb.setGeometry(QtCore.QRect(10, 30 + distance_between_elements * idx, 91, 17))
label_tb.setFont(font)
label_tb.setObjectName(f"label_tb_{idx}")
label_tb.setText("time base")
self.tb_list.append(label_tb)
# self.created_elements.append(label_tb)
# self.layout.addWidget(label_tb)
label_yr = QtWidgets.QLabel(parent=self)
label_yr.setGeometry(QtCore.QRect(10, 50 + distance_between_elements * idx, 91, 17))
label_yr.setFont(font)
label_yr.setObjectName(f"label_yr_{idx}")
label_yr.setText("Y Range")
self.yr_list.append(label_yr)
# self.layout.addWidget(label_yr)
# self.created_elements.append(label_yr)
class MyBinaryFile_Reader:
def __init__(self, file_name):
self.rec_duration = None
self.file_name = file_name
self.sampling_rate = None
self.device = None
self.voltage_range = None
self.channel_names = None
self.num_channels = None
self.data = None
self.header = None
self.read_file()
self.make_fields_toproperties()
def process_header(self):
self.num_channels = self.header['num_channels']
self.channel_names = [channel['name'] for channel in self.header['channel_list']]
self.voltage_range = self.header['voltage_range']
self.device = self.header['device']
self.sampling_rate = self.header['sampling_rate']
def read_file(self):
import numpy as np
with open(self.file_name, 'rb') as fi:
header_length = int.from_bytes(fi.read(16), 'little')
self.header = json.loads(fi.read(header_length).decode('utf-8'))
# print(struct.unpack('f',fi.read(4)))
self.process_header()
data = np.fromfile(fi, float)
len_remainder = len(data) % self.num_channels
if len_remainder != 0:
print('Data length is not multiple of channel count !! Cropping..')
data = data[:-len_remainder]
self.data = np.reshape(data, (-1, self.num_channels))
self.rec_duration = self.data.shape[0] / self.sampling_rate
def make_fields_toproperties(self):
'''this adds the channel names as fields to the class'''
for idx, channel_name in enumerate(self.channel_names):
self.__dict__[channel_name] = self.data[:, idx]
class MCC_settings:
def __init__(self):
self.scan_counters = False
self.num_channels = None
self.channel_list = []
self.device = None
self.voltage_range = None
self.pulse_rate = 30
self.sampling_rate = 1000
self.graphsettings = {}
default_params_file = 'MCC_settings_default.json'
if Path(default_params_file).exists():
self.from_file(default_params_file)
else:
self.default_setting()
def to_header(self) -> bytes:
self.get_active_channels()
dictionary = copy.deepcopy(vars(self))
dictionary['datetime'] = datetime.datetime.now().strftime('%Y%m%d_%H%M%S')
ids_topop = []
for c_id, channel in enumerate(dictionary['channel_list']):
channel.pop('color', None)
channel.pop('win', None)
if not channel["active"]:
ids_topop.append(c_id)
dictionary.pop("graphsettings")
for c_id in sorted(ids_topop, reverse=True):
dictionary['channel_list'].pop(c_id)
return json.dumps(dictionary).encode()
def get_active_channels(self):
active_channels = [ch_id for ch_id, channel in enumerate(self.channel_list) if channel['active']]
low_channel = min(active_channels)
high_channel = max(active_channels)
self.num_channels = high_channel - low_channel + 1
return low_channel, high_channel
def default_setting(self, num_channels=16):
if num_channels == 16:
self.device = "USB-1608G"
self.voltage_range = 'BIP5VOLTS'
elif num_channels == 8:
self.voltage_range = 'BIP10VOLTS'
raise NotImplementedError
else:
raise NotImplementedError
for ch_id in range(num_channels):
channel_dict = {}
channel_dict['id'] = ch_id
channel_dict['name'] = f"Channel_{ch_id}"
channel_dict['active'] = True
channel_dict['win'] = -1
channel_dict['color'] = COLOR_PALETTE[ch_id]
self.channel_list.append(channel_dict)
def from_file(self, path2file: (str, Path)):
with open(path2file, 'r') as fi:
loaded_dict = json.load(fi)
for key in loaded_dict.keys():
if key in self.__dict__.keys():
self.__dict__[key] = loaded_dict[key]
def to_file(self, path2file: (str, Path)):
dict_to_save = vars(self)
with open(path2file, 'w') as fi:
json.dump(dict_to_save, fi, indent=4)
def add_graphsettings(self, graphsetting: dict):
# # throw away old settings
# if "A" in graphsetting.keys():
# for graph_name in [el.name for el in list(PlotWindowEnum)[1:5]]:
# self.graphsettings.pop(graph_name, None)
# elif "E" in graphsetting.keys():
# for graph_name in [el.name for el in list(PlotWindowEnum)[5:9]]:
# self.graphsettings.pop(graph_name, None)
# elif "J" in graphsetting.keys():
# for graph_name in [el.name for el in list(PlotWindowEnum)[9:13]]:
# self.graphsettings.pop(graph_name, None)
self.graphsettings.update({**graphsetting})
class RemoteConnDialog(QtWidgets.QDialog):
"""
Dialog to wait for remote connection, with abort button
"""
def __init__(self, socket_comm, parent=None):
super().__init__(parent)
self.parent = parent
self.socket_comm = socket_comm
self.setWindowTitle('Remote Connection')
self.abort_button = QtWidgets.QPushButton("Abort")
self.abort_button.clicked.connect(self.stopwaiting)
self.abort_button.setIcon(QtGui.QIcon("GUI/icons/HandRaised.svg"))
self.label = QtWidgets.QLabel("waiting for remote connection...")
layout = QtWidgets.QVBoxLayout()
layout.addWidget(self.label)
layout.addWidget(self.abort_button)
self.setLayout(layout)
self.connectio_time = QtCore.QTimer()
self.connectio_time.timeout.connect(self.check_connection)
self.connectio_time.start(500)
self.aborted = False
def check_connection(self):
"""
Check if connection is established, if so close dialog, called regularly by timer
"""
if self.socket_comm.connected:
self.close()
def stopwaiting(self):
"""
Stop waiting for connection, called by abort button
"""
self.socket_comm.stop_waiting_for_connection()
self.aborted = True
self.close()
def closeEvent(self, event):
# If the user closes the dialog, kill the process
self.stopwaiting()
self.aborted = True
event.accept()
if __name__ == '__main__':
settings = MCC_settings()
print(vars(settings))
settings.to_header()
print(vars(settings))
# settings.default_setting()
# settings.to_file('MCC_settings_default.json')