edf tools for evismo

browse-edf.py 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. from pathlib import Path
  2. import matplotlib.pyplot as plt
  3. import numpy as np
  4. import scipy as sp
  5. from pathlib import Path
  6. import os
  7. #import splitedf2
  8. import pyedflib
  9. import edfrd
  10. from datetime import datetime, timedelta
  11. from collections import namedtuple
  12. import pyedflib
  13. import mne
  14. def set_header_edfrd(header, startdate):
  15. Header = edfrd.Header
  16. startdate_of_recording = (startdate.day, startdate.month, startdate.year)
  17. starttime_of_recording = (startdate.hour, startdate.minute, startdate.second)
  18. h = [
  19. (header.version),
  20. (header.local_patient_identification),
  21. (header.local_recording_identification),
  22. (startdate_of_recording),
  23. (starttime_of_recording),
  24. (header.number_of_bytes_in_header_record),
  25. (header.reserved),
  26. (header.number_of_data_records),
  27. (header.duration_of_a_data_record),
  28. (header.number_of_signals)
  29. ]
  30. h.append(header.signals)
  31. return Header(*h)
  32. file_type = pyedflib.FILETYPE_EDF
  33. use_precomputed = True
  34. merge_dir = Path('edf', 'tmp')
  35. fp = Path('edf-data', '3785_2019_10_07_normal.edf')
  36. file_in_2 = Path('edf', '24h_1ch_sample3.EDF')
  37. file_in_3 = Path('edf', 'Pekka_1ch_FastFix_125hz_fw340rc3_23-07-45.EDF')
  38. file_in_4 = Path('edf', 'Pekka_1ch_FastFix_125hz_fw340rc3_23-07-45_part_fixed_0.edf')
  39. fp_out = Path('edf-data', '3785_2019_10_07_normal_new_header.edf')
  40. parts = merge_dir.glob('./*.EDF')
  41. ## edfrd
  42. header = edfrd.read_header(os.fspath(fp), parse_date_time=True)
  43. print(header)
  44. record_start_date = datetime(
  45. day=header.startdate_of_recording[0],
  46. month=header.startdate_of_recording[1],
  47. year=header.startdate_of_recording[2],
  48. hour=header.starttime_of_recording[0],
  49. minute=header.starttime_of_recording[1],
  50. second=header.starttime_of_recording[2],
  51. )
  52. print(record_start_date)
  53. header = edfrd.read_header(os.fspath(fp))
  54. print(header)
  55. # header[3]._replace(startdate_of_recording='(4,10,10)')
  56. #header[3]='(4,10,10)'
  57. #print(header)
  58. rsd = datetime(2019, 10, 20, 13, 24, 45)
  59. h = set_header_edfrd(header, rsd)
  60. # data_record_part = edfrd.read_data_records(fp, header)
  61. # with fp_out.open('wb') as fout:
  62. # edfrd.write_header(fout, h._make(header))
  63. # edfrd.write_data_records(fout, data_record_part)
  64. # print('written part file: ', os.fspath(fp_out))
  65. # fout.close()
  66. print(h)
  67. exit()
  68. ## read edf file
  69. f_in = pyedflib.EdfReader(os.fspath(fp))
  70. file_duration= f_in.getFileDuration()
  71. start_date = f_in.getStartdatetime()
  72. header = f_in.getHeader()
  73. sample_frequencies = f_in.getSampleFrequencies()
  74. signal_headers = f_in.getSignalHeaders()
  75. channel_info = f_in.getSignalHeaders()
  76. channels = f_in.signals_in_file
  77. ## write same file again
  78. f_out = fp.with_name(fp.stem + "copy").with_suffix(fp.suffix)
  79. fo = pyedflib.EdfWriter(os.fspath(f_out), channels, file_type=pyedflib.FILETYPE_EDF)
  80. fo.setHeader(header)
  81. fo.n_channels = channels
  82. fo.channels = channel_info
  83. fo.setStartdatetime(start_date)
  84. fo.duration = 1
  85. fo.file_type = file_type
  86. data_list = list()
  87. for channel in range(0, channels):
  88. sample_frequency = f_in.getSampleFrequency(channel)
  89. signal_header = f_in.getSignalHeader(channel)
  90. fo.setSignalHeader(channel, signal_header)
  91. print(signal_header)
  92. #fo.writePhysicalSamples(f_in.readSignal(channel))
  93. # fo.writeDigitalSamples(f_in.readSignal(channel))
  94. #data_list.append(f_in.readSignal(channel, start=slice_start, n=duration * sample_frequency))
  95. data_list.append(f_in.readSignal(channel))
  96. fo.setSignalHeaders(signal_headers)
  97. fo.writeSamples(data_list)
  98. fo.close()
  99. ## compare values
  100. f_in_2 = pyedflib.EdfReader(os.fspath(f_out))
  101. out_file_duration= f_in_2.getFileDuration()
  102. out_start_date = f_in_2.getStartdatetime()
  103. print(file_duration, out_file_duration)
  104. print(start_date, out_start_date)
  105. print(header, f_in_2.getHeader())