sig_reader.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. """读取数据文件
  2. """
  3. from typing import List
  4. import mne
  5. import numpy as np
  6. class Reader:
  7. """读取bdf文件
  8. """
  9. def __init__(self) -> None:
  10. self._montage = mne.channels.make_standard_montage('standard_1020')
  11. def read(self, filename: str, ch_names: List[str]):
  12. raw = mne.io.read_raw_bdf(filename, preload=True)
  13. raw.set_montage(self._montage)
  14. raw.pick_channels(ch_names=ch_names)
  15. return raw
  16. def fix_annotation(self, raw:mne.io.Raw):
  17. """在线数据按秒打标签,这里将相同秒标签合并
  18. Args:
  19. raw (mne.io.Raw): eeg data
  20. """
  21. annotations = raw.annotations
  22. for item in ['miFailed', 'miSuccess']:
  23. if item in set(annotations.description):
  24. annotations.rename({item: 'mi'})
  25. all_idxes = np.arange(0, len(annotations))
  26. valid_idxes = []
  27. last_label = None
  28. for ii, annot in enumerate(annotations):
  29. if last_label != annot['description']:
  30. last_label = annot['description']
  31. valid_idxes.append(ii)
  32. valid_idxes = np.array(valid_idxes)
  33. delete_mask = ~np.isin(all_idxes, valid_idxes)
  34. delete_idxes = all_idxes[delete_mask]
  35. annotations.delete(delete_idxes)
  36. raw.set_annotations(annotations)