qlsdk2 0.3.0a2__py3-none-any.whl → 0.4.0__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.
qlsdk/ar4m/persist.py DELETED
@@ -1,178 +0,0 @@
1
- from datetime import datetime
2
- from multiprocessing import Lock, Queue
3
- from pyedflib import FILETYPE_BDFPLUS, FILETYPE_EDFPLUS, EdfWriter
4
- from threading import Thread
5
- from loguru import logger
6
- import numpy as np
7
- import os
8
-
9
- class EdfHandler(object):
10
- def __init__(self, sample_frequency, physical_max, physical_min, digital_max, digital_min, resolution=16, storage_path = None):
11
- self.physical_max = physical_max
12
- self.physical_min = physical_min
13
- self.digital_max = digital_max
14
- self.digital_min = digital_min
15
- self.channels = None
16
- self._cache = Queue()
17
- self.resolution = resolution
18
- self.sample_frequency = sample_frequency
19
- # bytes per second
20
- self.bytes_per_second = 0
21
- self._edf_writer = None
22
- self._cache2 = tuple()
23
- self._recording = False
24
- self._edf_writer = None
25
- self.annotations = None
26
- # 每个数据块大小
27
- self._chunk = np.array([])
28
- self._Lock = Lock()
29
- self._duration = 0
30
- self._points = 0
31
- self._first_pkg_id = None
32
- self._last_pkg_id = None
33
- self._first_timestamp = None
34
- self._end_time = None
35
- self._patient_code = "patient_code"
36
- self._patient_name = "patient_name"
37
- self._device_type = None
38
- self._total_packets = 0
39
- self._lost_packets = 0
40
- self._storage_path = storage_path
41
-
42
- @property
43
- def file_name(self):
44
- if self._storage_path:
45
- try:
46
- os.makedirs(self._storage_path, exist_ok=True) # 自动创建目录,存在则忽略
47
- return f"{self._storage_path}/{self._device_type}_{self._first_timestamp}.edf"
48
- except Exception as e:
49
- logger.error(f"创建目录[{self._storage_path}]失败: {e}")
50
-
51
- return f"{self._device_type}_{self._first_timestamp}.edf"
52
-
53
- @property
54
- def file_type(self):
55
- return FILETYPE_BDFPLUS if self.resolution == 24 else FILETYPE_EDFPLUS
56
-
57
- def set_device_type(self, device_type):
58
- self._device_type = device_type
59
-
60
- def set_storage_path(self, storage_path):
61
- self._storage_path = storage_path
62
-
63
- def set_patient_code(self, patient_code):
64
- self._patient_code = patient_code
65
-
66
- def set_patient_name(self, patient_name):
67
- self._patient_name = patient_name
68
-
69
- def append(self, data):
70
- if data:
71
- # 通道数
72
- if self._first_pkg_id is None:
73
- self.channels = data.eeg_ch_count
74
- self._first_pkg_id = data.pkg_id
75
- self._first_timestamp = data.time_stamp
76
-
77
- if self._last_pkg_id and self._last_pkg_id != data.pkg_id - 1:
78
- self._lost_packets += data.pkg_id - self._last_pkg_id - 1
79
- logger.warning(f"数据包丢失: {self._last_pkg_id} -> {data.pkg_id}, 丢包数: {data.pkg_id - self._last_pkg_id - 1}")
80
-
81
- self._last_pkg_id = data.pkg_id
82
- self._total_packets += 1
83
-
84
- # 数据
85
- self._cache.put(data)
86
- if not self._recording:
87
- self.start()
88
-
89
- def trigger(self, data):
90
- pass
91
-
92
- def start(self):
93
- self._recording = True
94
- record_thread = Thread(target=self._consumer)
95
- record_thread.start()
96
-
97
- def _consumer(self):
98
- logger.debug(f"开始消费数据 _consumer: {self._cache.qsize()}")
99
- while True:
100
- if self._recording or (not self._cache.empty()):
101
- data = self._cache.get()
102
- if data is None:
103
- break
104
- # 处理数据
105
- self._points += len(data.eeg[0])
106
- self._write_file(data.eeg)
107
- else:
108
- break
109
-
110
- self.close()
111
-
112
- def _write_file(self, data):
113
- try:
114
- if self._edf_writer is None:
115
- self.initialize_edf()
116
-
117
- if (self._chunk.size == 0):
118
- self._chunk = np.asarray(data)
119
- else:
120
- self._chunk = np.hstack((self._chunk, data))
121
-
122
- if self._chunk.size >= self.sample_frequency * self.channels:
123
- self._write_chunk(self._chunk[:self.sample_frequency])
124
- self._chunk = self._chunk[self.sample_frequency:]
125
-
126
- except Exception as e:
127
- logger.error(f"写入数据异常: {str(e)}")
128
-
129
- def close(self):
130
- self._recording = False
131
- if self._edf_writer:
132
- self._end_time = datetime.now().timestamp()
133
- self._edf_writer.writeAnnotation(0, 1, "start recording ")
134
- self._edf_writer.writeAnnotation(self._duration, 1, "recording end")
135
- self._edf_writer.close()
136
-
137
- logger.info(f"文件: {self.file_name}完成记录, 总点数: {self._points}, 总时长: {self._duration}秒 丢包数: {self._lost_packets}/{self._total_packets + self._lost_packets}")
138
-
139
-
140
-
141
- def initialize_edf(self):
142
- # 创建EDF+写入器
143
- self._edf_writer = EdfWriter(
144
- self.file_name,
145
- self.channels,
146
- file_type=self.file_type
147
- )
148
-
149
- # 设置头信息
150
- self._edf_writer.setPatientCode(self._patient_code)
151
- self._edf_writer.setPatientName(self._patient_name)
152
- self._edf_writer.setEquipment(self._device_type)
153
- self._edf_writer.setStartdatetime(datetime.now())
154
-
155
- # 配置通道参数
156
- signal_headers = []
157
- for ch in range(self.channels):
158
- signal_headers.append({
159
- "label": f'channels {ch + 1}',
160
- "dimension": 'uV',
161
- "sample_frequency": self.sample_frequency,
162
- "physical_min": self.physical_min,
163
- "physical_max": self.physical_max,
164
- "digital_min": self.digital_min,
165
- "digital_max": self.digital_max
166
- })
167
-
168
- self._edf_writer.setSignalHeaders(signal_headers)
169
-
170
- def _write_chunk(self, chunk):
171
- logger.debug(f"写入数据: {chunk}")
172
- # 转换数据类型为float64(pyedflib要求)
173
- data_float64 = chunk.astype(np.float64)
174
- # 写入时转置为(样本数, 通道数)格式
175
- self._edf_writer.writeSamples(data_float64)
176
- self._duration += 1
177
-
178
-
@@ -1,18 +0,0 @@
1
- qlsdk/__init__.py,sha256=w0UCnZJekAq4orDahQXRL8s5JJGrWMz0De5LVIf4X_c,426
2
- qlsdk/ar4/__init__.py,sha256=iR0bFanDwqSxSEM1nCcge1l-GSIWjwnMeRQa47wGrds,4752
3
- qlsdk/ar4m/__init__.py,sha256=Z13X53WKmKp6MW_fA1FUPdQgJxhxxuyzemJWPS_CEHA,639
4
- qlsdk/ar4m/ar4sdk.py,sha256=OtmlvbO7XAgk5FNPFYIv_rXGj14maaWtgi2lQ7zQkvY,41418
5
- qlsdk/ar4m/persist.py,sha256=YrISKVfY-r1m2GZOw-JLuqTbEajA5wYa31MejGlz14I,6696
6
- qlsdk/ar4m/libs/libAr4SDK.dll,sha256=kZp9_DRwPdAJ5OgTFQSqS8tEETxUs7YmmETuBP2g60U,15402132
7
- qlsdk/ar4m/libs/libwinpthread-1.dll,sha256=W77ySaDQDi0yxpnQu-ifcU6-uHKzmQpcvsyx2J9j5eg,52224
8
- qlsdk/persist/__init__.py,sha256=UYaNL_RSczIZL_YrBWAifoAnNxpgZHEeSp-goHqyqv4,27
9
- qlsdk/persist/edf.py,sha256=usgFjZJVcfAUwSy6cJtRdgjdqMgOS4viLCDGcwAGMZE,7106
10
- qlsdk/sdk/__init__.py,sha256=v9LKP-5qXCqnAsCkiRE9LDb5Tagvl_Qd_fqrw7y9yd4,68
11
- qlsdk/sdk/ar4sdk.py,sha256=ZILuUVgUZN7-LNQm4xdItxmZWGwvFt4oWUKGez1BCLI,27101
12
- qlsdk/sdk/hub.py,sha256=uEOGZBZtMDCWlV8G2TZe6FAo6eTPcwHAW8zdqr1eq_0,1571
13
- qlsdk/x8/__init__.py,sha256=XlQn3Af_HA7bGrxhv3RZKWMob_SnmissXiso7OH1UgI,4750
14
- qlsdk/x8m/__init__.py,sha256=cLeUqEEj65qXw4Qa4REyxoLh6T24anSqPaKe9_lR340,634
15
- qlsdk2-0.3.0a2.dist-info/METADATA,sha256=o6ms9sTAOlaWyWyGuLc0IWYvFYu1oXXVDhqNMixvpqI,7066
16
- qlsdk2-0.3.0a2.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
17
- qlsdk2-0.3.0a2.dist-info/top_level.txt,sha256=2CHzn0SY-NIBVyBl07Suh-Eo8oBAQfyNPtqQ_aDatBg,6
18
- qlsdk2-0.3.0a2.dist-info/RECORD,,
File without changes
File without changes