Browse Source

started function split

Michael Kistler 1 month ago
parent
commit
80f40775e4
1 changed files with 84 additions and 15 deletions
  1. 84 15
      split-edf2.py

+ 84 - 15
split-edf2.py

@@ -161,6 +161,87 @@ def _parts(duration, start, end, period):
161 161
     interval.append(list([(parts-1)*period, end]))
162 162
     return interval
163 163
 
164
+
165
+
166
+
167
+def read_samples_slice(fp, channels, slice_start=None, slice_end=None):
168
+    duration = slice_end - slice_start
169
+
170
+    f = pyedflib.EdfReader(os.fspath(fp))
171
+    data_list = list()
172
+    for channel in range(0, channels):
173
+        sample_frequency = f.getSampleFrequency(channel)
174
+        #signal_header = f.getSignalHeader(channel)
175
+        data_list.append(f.readSignal(channel, start=slice_start * sample_frequency, n=duration * sample_frequency))
176
+    f.close()
177
+    return data_list
178
+
179
+def set_slice_header(f, f_in, recording_start_date, fp=None, file_type=pyedflib.FILETYPE_EDFPLUS):
180
+    if fp:
181
+        f.path = fp.name  # f.file_name
182
+    else:
183
+        f.path = f.file_name
184
+    f.file_type = file_type
185
+    f.patient_name = f_in.getPatientName()
186
+    f.patient_code = f_in.getPatientCode()
187
+    f.technician = f_in.getTechnician()
188
+    f.equipment = f_in.getEquipment()
189
+    f.recording_additional = f_in.getRecordingAdditional()
190
+    f.patient_additional = f_in.getPatientAdditional()
191
+    f.admincode = f_in.getAdmincode()
192
+    f.gender = f_in.getGender()
193
+    f.recording_start_time = recording_start_date
194
+    f.birthdate = f_in.getBirthdate()
195
+    f.duration = 1
196
+    f.number_of_annotations = 1 if file_type in [pyedflib.FILETYPE_EDFPLUS, pyedflib.FILETYPE_BDFPLUS] else 0
197
+    f.n_channels = f_in.signals_in_file
198
+    f.channels = f_in.getSignalHeaders()
199
+    return f
200
+
201
+def write_slice(f_in,fp, recording_start_date, slice_start=None, slice_end=None, file_type=pyedflib.FILETYPE_EDFPLUS):
202
+
203
+    duration = slice_end - slice_start
204
+
205
+    anno = _annotations(annotations, slice=list([slice_start, slice_end]))
206
+    data_list = list()
207
+
208
+    f = pyedflib.EdfWriter(os.fspath(fp), channels, file_type=file_type)
209
+    f.path = fp.name  # f.file_name
210
+    f.file_type = file_type
211
+    f.patient_name = f_in.getPatientName()
212
+    f.patient_code = f_in.getPatientCode()
213
+    f.technician = f_in.getTechnician()
214
+    f.equipment = f_in.getEquipment()
215
+    f.recording_additional = f_in.getRecordingAdditional()
216
+    f.patient_additional = f_in.getPatientAdditional()
217
+    f.admincode = f_in.getAdmincode()
218
+    f.gender = f_in.getGender()
219
+    f.recording_start_time = recording_start_date
220
+    f.birthdate = f_in.getBirthdate()
221
+    f.duration = 1
222
+    f.number_of_annotations = 1 if file_type in [pyedflib.FILETYPE_EDFPLUS, pyedflib.FILETYPE_BDFPLUS] else 0
223
+    f.n_channels = f_in.signals_in_file
224
+    f.channels = channel_info
225
+
226
+    for channel in range(0, channels):
227
+        sample_frequency = f_in.getSampleFrequency(channel)
228
+        signal_header = f_in.getSignalHeader(channel)
229
+        # fo.setSignalHeader(channel, signal_header)
230
+        data_list.append(f_in.readSignal(channel, start=slice_start * sample_frequency, n=duration * sample_frequency))
231
+
232
+    # adapt header files with correct numbers (date)
233
+
234
+    # fo.sample_buffer = []
235
+    # fo.setHeader(f_in.getHeader())
236
+    f.setSignalHeaders(channel_info)
237
+
238
+    for a in anno:
239
+        f.writeAnnotation(a[0], a[1], a[2])
240
+
241
+    f.writeSamples(data_list)
242
+    del f
243
+    return data_list
244
+
164 245
 def split_edf(fp,interval_td, start_time_td, end_time_td=None):
165 246
     '''
166 247
     split an EDF file into files of n days
@@ -183,7 +264,7 @@ def split_edf(fp,interval_td, start_time_td, end_time_td=None):
183 264
     interval_in_s = int(interval_td.total_seconds())
184 265
 
185 266
     if end_time_s:
186
-        file_duration_stripped_s = file_duration_s-start_time_s-end_time_s
267
+        file_duration_stripped_s = end_time_s-start_time_s
187 268
     else:
188 269
         file_duration_stripped_s = file_duration_s-start_time_s
189 270
 
@@ -197,19 +278,6 @@ def split_edf(fp,interval_td, start_time_td, end_time_td=None):
197 278
     channel_info = f_in.getSignalHeaders()
198 279
     channels = f_in.signals_in_file
199 280
     annotations = f_in.readAnnotations()
200
-    len(annotations)
201
-    ## annotations
202
-    ## timestamp, value, default -1, name/description
203
-
204
-    # test_an = _annotations(annotations,slice=list([0, 50000]))
205
-    # for ta in test_an:
206
-    #     print(ta)
207
-    #     print(ta[0],ta[1],ta[2])
208
-
209
-
210
-    # print(file_duration_stripped_s, "stripped seconds in file")
211
-    # print(file_duration_s, " seconds in file")
212
-    # print(file_duration_s, interval_in_seconds_td.total_seconds())
213 281
 
214 282
     if file_duration_stripped_s > interval_in_s:
215 283
         #parts = list(_chunks(range(0, file_duration_stripped_s), int(interval_in_seconds_td.total_seconds())))
@@ -252,13 +320,14 @@ def split_edf(fp,interval_td, start_time_td, end_time_td=None):
252 320
                 #fo.setSignalHeader(channel, signal_header)
253 321
                 data_list.append(f_in.readSignal(channel, start=slice_start * sample_frequency, n=duration * sample_frequency))
254 322
 
255
-
323
+            #read_samples_slice(fp,)
256 324
             # adapt header files with correct numbers (date)
257 325
 
258 326
             #fo.sample_buffer = []
259 327
             #fo.setHeader(f_in.getHeader())
260 328
             fo.setSignalHeaders(channel_info)
261 329
 
330
+            anno = _annotations(annotations, slice=list([slice_start, slice_end]))
262 331
             for a in anno:
263 332
                 fo.writeAnnotation(a[0], a[1], a[2])
264 333