qlsdk2 0.4.0a3__py3-none-any.whl → 0.4.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,388 @@
1
+
2
+ from multiprocessing import Queue
3
+ from threading import Thread
4
+ from time import sleep, time_ns
5
+ from typing import Any, Dict, Literal
6
+
7
+ from loguru import logger
8
+ from qlsdk.persist import RscEDFHandler
9
+ from qlsdk.rsc.interface import IDevice, IParser
10
+ from qlsdk.rsc.parser import TcpMessageParser
11
+ from qlsdk.rsc.command import StartImpedanceCommand, StopImpedanceCommand, StartStimulationCommand, StopStimulationCommand, SetAcquisitionParamCommand, StartAcquisitionCommand, StopAcquisitionCommand
12
+ # from qlsdk.rsc.command import *
13
+
14
+ class QLBaseDevice(IDevice):
15
+ def __init__(self, socket):
16
+ self.socket = socket
17
+
18
+ # 设备信息
19
+ self.device_id = None
20
+ self.device_name = None
21
+ self._device_type = None
22
+ self._device_no = None
23
+ self.software_version = None
24
+ self.hardware_version = None
25
+ self.connect_time = None
26
+ self.current_time = None
27
+ # mV
28
+ self.voltage = None
29
+ # %
30
+ self.battery_remain = None
31
+ # %
32
+ self.battery_total = None
33
+ # persist
34
+ self._recording = False
35
+ self._storage_path = None
36
+ self._file_prefix = None
37
+
38
+ # 可设置参数
39
+ # 采集:采样量程、采样率、采样通道
40
+ # 刺激:刺激电流、刺激频率、刺激时间、刺激通道
41
+ # 采样量程(mV):188、375、563、750、1125、2250、4500
42
+ self._sample_range:Literal[188, 375, 563, 750, 1125, 2250, 4500] = 188
43
+ # 采样率(Hz):250、500、1000、2000、4000、8000、16000、32000
44
+ self._sample_rate:Literal[250, 500, 1000, 2000, 4000, 8000, 16000, 32000] = 500
45
+ self._physical_max = self._sample_range * 1000 # 物理最大值(uV)
46
+ self._physical_min = -self._sample_range * 1000 # 物理最小值(uV)
47
+ self._digital_max = 8388607
48
+ self._digital_min = -8388608
49
+ self._physical_range = self._physical_max - self._physical_min
50
+ self._digital_range = 16777215
51
+ self._acq_channels = None
52
+ self._acq_param = {
53
+ "sample_range": 188,
54
+ "sample_rate": 500,
55
+ "channels": [],
56
+ }
57
+
58
+ self._stim_param = {
59
+ "stim_type": 0, # 刺激类型:0-所有通道参数相同, 1: 通道参数不同
60
+ "channels": [],
61
+ "param": [{
62
+ "channel_id": 0, #通道号 从0开始 -- 必填
63
+ "waveform": 3, #波形类型:0-直流,1-交流 2-方波 3-脉冲 -- 必填
64
+ "current": 1, #电流强度(mA) -- 必填
65
+ "duration": 30, #平稳阶段持续时间(s) -- 必填
66
+ "ramp_up": 5, #上升时间(s) 默认0
67
+ "ramp_down": 5, #下降时间(s) 默认0
68
+ "frequency": 500, #频率(Hz) -- 非直流必填
69
+ "phase_position": 0, #相位 -- 默认0
70
+ "duration_delay": "0", #延迟启动时间(s) -- 默认0
71
+ "pulse_width": 0, #脉冲宽度(us) -- 仅脉冲类型电流有效, 默认100us
72
+ },
73
+ {
74
+ "channel_id": 1, #通道号 从0开始 -- 必填
75
+ "waveform": 3, #波形类型:0-直流,1-交流 2-方波 3-脉冲 -- 必填
76
+ "current": 1, #电流强度(mA) -- 必填
77
+ "duration": 30, #平稳阶段持续时间(s) -- 必填
78
+ "ramp_up": 5, #上升时间(s) 默认0
79
+ "ramp_down": 5, #下降时间(s) 默认0
80
+ "frequency": 500, #频率(Hz) -- 非直流必填
81
+ "phase_position": 0, #相位 -- 默认0
82
+ "duration_delay": "0", #延迟启动时间(s) -- 默认0
83
+ "pulse_width": 0, #脉冲宽度(us) -- 仅脉冲类型电流有效, 默认100us
84
+ }
85
+ ]
86
+ }
87
+
88
+ self.stim_paradigm = None
89
+
90
+ signal_info = {
91
+ "param" : None,
92
+ "start_time" : None,
93
+ "finished_time" : None,
94
+ "packet_total" : None,
95
+ "last_packet_time" : None,
96
+ "state" : 0
97
+ }
98
+ stim_info = {
99
+
100
+ }
101
+ Impedance_info = {
102
+
103
+ }
104
+ # 信号采集状态
105
+ # 信号数据包总数(一个信号采集周期内)
106
+ # 信号采集参数
107
+ # 电刺激状态
108
+ # 电刺激开始时间(最近一次)
109
+ # 电刺激结束时间(最近一次)
110
+ # 电刺激参数
111
+ # 启动数据解析线程
112
+ # 数据存储状态
113
+ # 存储目录
114
+
115
+ #
116
+ self.__signal_consumer: Dict[str, Queue[Any]]={}
117
+ self.__impedance_consumer: Dict[str, Queue[Any]]={}
118
+
119
+ # EDF文件处理器
120
+ self._edf_handler = None
121
+ self.storage_enable = True
122
+
123
+ # 启动数据解析线程
124
+ self._parser: IParser = TcpMessageParser(self)
125
+ self._parser.start()
126
+
127
+ # 启动数据接收线程
128
+ self._accept = Thread(target=self.accept)
129
+ self._accept.daemon = True
130
+ self._accept.start()
131
+
132
+ @property
133
+ def device_type(self) -> int:
134
+ return self._device_type
135
+
136
+ def set_device_type(self, type: int):
137
+ self._device_type = type
138
+
139
+ @property
140
+ def device_no(self) -> int:
141
+ return self._device_no
142
+
143
+ def set_device_no(self, value: int):
144
+ self._device_no = value
145
+
146
+ @property
147
+ def physical_max(self) -> int:
148
+ return self._physical_max
149
+
150
+ @property
151
+ def physical_min(self) -> int:
152
+ return self._physical_min
153
+
154
+ @property
155
+ def physical_range(self) -> int:
156
+ return self._physical_range
157
+
158
+ @property
159
+ def digital_max(self) -> int:
160
+ return self._digital_max
161
+
162
+ @property
163
+ def digital_min(self) -> int:
164
+ return self._digital_min
165
+
166
+ @property
167
+ def digital_range(self) -> int:
168
+ return self._digital_range
169
+
170
+ def init_edf_handler(self):
171
+ self._edf_handler = RscEDFHandler(self.sample_rate, self.sample_range * 1000 , - self.sample_range * 1000, self.resolution)
172
+ self._edf_handler.set_device_type(self.device_type)
173
+ self._edf_handler.set_device_no(self.device_name)
174
+ self._edf_handler.set_storage_path(self._storage_path)
175
+ self._edf_handler.set_file_prefix(self._file_prefix)
176
+
177
+ # eeg数字值转物理值
178
+ def eeg2phy(self, digital:int):
179
+ # 向量化计算(自动支持广播)
180
+ return ((digital - self.digital_min) / self.digital_range) * self.physical_range + self.physical_min
181
+
182
+ @property
183
+ def edf_handler(self):
184
+ if not self.storage_enable:
185
+ return None
186
+
187
+ if self._edf_handler is None:
188
+ self.init_edf_handler()
189
+
190
+ return self._edf_handler
191
+
192
+ @property
193
+ def acq_channels(self):
194
+ if self._acq_channels is None:
195
+ self._acq_channels = [i for i in range(1, 63)]
196
+ return self._acq_channels
197
+ @property
198
+ def sample_range(self):
199
+ return self._sample_range if self._sample_range else 188
200
+ @property
201
+ def sample_rate(self):
202
+ return self._sample_rate if self._sample_rate else 500
203
+ @property
204
+ def resolution(self):
205
+ return 24
206
+ @property
207
+ def sample_num(self) -> int:
208
+ return 10
209
+ @property
210
+ def signal_consumers(self):
211
+ return self.__signal_consumer
212
+
213
+ @property
214
+ def impedance_consumers(self):
215
+ return self.__impedance_consumer
216
+
217
+ # 设置记录文件路径
218
+ def set_storage_path(self, path):
219
+ self._storage_path = path
220
+
221
+ # 设置记录文件名称前缀
222
+ def set_file_prefix(self, prefix):
223
+ self._file_prefix = prefix
224
+
225
+ # 接收socket消息
226
+ def accept(self):
227
+ while True:
228
+ # 缓冲去4M
229
+ data = self.socket.recv(4096*1024)
230
+ if not data:
231
+ logger.warning(f"设备{self.device_name}连接结束")
232
+ break
233
+
234
+ self._parser.append(data)
235
+
236
+
237
+ # socket发送数据
238
+ def send(self, data):
239
+ self.socket.sendall(data)
240
+
241
+ # 设置刺激参数
242
+ def set_stim_param(self, param):
243
+ self.stim_paradigm = param
244
+
245
+ # 设置采集参数
246
+ def set_acq_param(self, channels, sample_rate = 500, sample_range = 188):
247
+ self._acq_param["channels"] = channels
248
+ self._acq_param["sample_rate"] = sample_rate
249
+ self._acq_param["sample_range"] = sample_range
250
+ self._acq_channels = channels
251
+ self._sample_rate = sample_rate
252
+ self._sample_range = sample_range
253
+
254
+ # 通用配置-TODO
255
+ def set_config(self, key:str, val: str):
256
+ pass
257
+
258
+ def start_impedance(self):
259
+ logger.info("启动阻抗测量")
260
+ msg = StartImpedanceCommand.build(self).pack()
261
+ logger.debug(f"start_impedance message is {msg.hex()}")
262
+ self.socket.sendall(msg)
263
+
264
+ def stop_impedance(self):
265
+ logger.info("停止阻抗测量")
266
+ msg = StopImpedanceCommand.build(self).pack()
267
+ logger.debug(f"stop_impedance message is {msg.hex()}")
268
+ self.socket.sendall(msg)
269
+
270
+ def start_stimulation(self):
271
+ if self.stim_paradigm is None:
272
+ logger.warning("刺激参数未设置,请先设置刺激参数")
273
+ return
274
+ logger.info("启动电刺激")
275
+ msg = StartStimulationCommand.build(self).pack()
276
+ logger.debug(f"start_stimulation message is {msg.hex()}")
277
+ self.socket.sendall(msg)
278
+ t = Thread(target=self._stop_stimulation_trigger, args=(self.stim_paradigm.duration,))
279
+ t.start()
280
+
281
+ def _stop_stimulation_trigger(self, duration):
282
+ delay = duration
283
+ while delay > 0:
284
+ sleep(1)
285
+ delay -= 1
286
+ logger.info(f"_stop_stimulation_trigger duration: {duration}")
287
+ if self._edf_handler:
288
+ self._edf_handler.trigger("stimulation should be stopped")
289
+ else:
290
+ logger.warning("stop stim trigger fail. no edf writer alive")
291
+
292
+ def stop_stimulation(self):
293
+ logger.info("停止电刺激")
294
+ msg = StopStimulationCommand.pack()
295
+ logger.debug(f"stop_stimulation message is {msg.hex()}")
296
+ self.socket.sendall(msg)
297
+
298
+ # 启动采集
299
+ def start_acquisition(self, recording = True):
300
+ logger.info("启动信号采集")
301
+ self._recording = recording
302
+ # 设置数据采集参数
303
+ param_bytes = SetAcquisitionParamCommand.build(self).pack()
304
+ # 启动数据采集
305
+ start_bytes = StartAcquisitionCommand.build(self).pack()
306
+ msg = param_bytes + start_bytes
307
+ logger.debug(f"start_acquisition message is {msg.hex()}")
308
+ self.socket.sendall(msg)
309
+
310
+ # 停止采集
311
+ def stop_acquisition(self):
312
+ logger.info("停止信号采集")
313
+ msg = StopAcquisitionCommand.build(self).pack()
314
+ logger.debug(f"stop_acquisition message is {msg.hex()}")
315
+ self.socket.sendall(msg)
316
+ if self._edf_handler:
317
+ # 发送结束标识
318
+ self.edf_handler.write(None)
319
+
320
+ # 订阅实时数据
321
+ def subscribe(self, topic:str=None, q : Queue=None, type : Literal["signal","impedance"]="signal"):
322
+
323
+ # 数据队列
324
+ if q is None:
325
+ q = Queue(maxsize=1000)
326
+
327
+ # 队列名称
328
+ if topic is None:
329
+ topic = f"{type}_{time_ns()}"
330
+
331
+ # 订阅生理电信号数据
332
+ if type == "signal":
333
+ # topic唯一,用来区分不同的订阅队列(下同)
334
+ if topic in list(self.__signal_consumer.keys()):
335
+ logger.warning(f"exists {type} subscribe of {topic}")
336
+ else:
337
+ self.__signal_consumer[topic] = q
338
+
339
+ # 订阅阻抗数据
340
+ if type == "impedance":
341
+ if topic in list(self.__signal_consumer.keys()):
342
+ logger.warning(f"exists {type} subscribe of {topic}")
343
+ else:
344
+ self.__impedance_consumer[topic] = q
345
+
346
+ return topic, q
347
+
348
+ def trigger(self, desc):
349
+ if self._edf_handler:
350
+ self.edf_handler.trigger(desc)
351
+ else:
352
+ logger.warning("no edf handler, no place to recording trigger")
353
+
354
+ def __str__(self):
355
+ return f'''
356
+ Device:
357
+ Name: {self.device_name},
358
+ Type: {hex(self.device_type) if self.device_type else None},
359
+ ID: {self.device_id if self.device_id else None},
360
+ Software: {self.software_version},
361
+ Hardware: {self.hardware_version},
362
+ Connect Time: {self.connect_time},
363
+ Current Time: {self.current_time},
364
+ Voltage: {str(self.voltage) + "mV" if self.voltage else None},
365
+ Battery Remain: {str(self.battery_remain)+ "%" if self.battery_remain else None},
366
+ Battery Total: {str(self.battery_total) + "%" if self.battery_total else None}
367
+ '''
368
+
369
+ def __repr__(self):
370
+ return f'''
371
+ Device:
372
+ Name: {self.device_name},
373
+ Type: {hex(self.device_type)},
374
+ ID: {self.device_id},
375
+ Software: {self.software_version},
376
+ Hardware: {self.hardware_version},
377
+ Connect Time: {self.connect_time},
378
+ Current Time: {self.current_time},
379
+ Voltage: {self.voltage}mV,
380
+ Battery Remain: {self.battery_remain}%,
381
+ Battery Total: {self.battery_total}%
382
+ '''
383
+
384
+ def __eq__(self, other):
385
+ return self.device_name == other.device_name and self.device_type == other.device_type and self.device_id == other.device_id
386
+
387
+ def __hash__(self):
388
+ return hash((self.device_name, self.device_type, self.device_id))