14

How can I read edf data using Python? I want to analyze data of a edf file, but I cannot read it using pyEDFlib. It threw the error OSError: The file is discontinous and cannot be read and I'm not sure why.

BossaNova
  • 1,509
  • 1
  • 13
  • 17
Mindy
  • 175
  • 1
  • 1
  • 8
  • Welcome to Stack Overflow. Please post a [minimal, complete and verifiable example](https://stackoverflow.com/help/mcve) to get help with your problem. – ggorlen Aug 16 '18 at 05:00

2 Answers2

27

I assume that your data are biological time-series like EEG, is this correct? If so, you can use the MNE library.

You have to install it first. Since it is not a standard library, take a look here. Then, you can use the read_raw_edf() method.

For example:

import mne
file = "my_path\\my_file.edf"
data = mne.io.read_raw_edf(file)
raw_data = data.get_data()
# you can get the metadata included in the file and a list of all channels:
info = data.info
channels = data.ch_names

See documentation in the links above for other properties of the data object

Jaroslav Bezděk
  • 6,967
  • 6
  • 29
  • 46
BossaNova
  • 1,509
  • 1
  • 13
  • 17
  • 1
    Yes. Your assume is right. The data I used is EEG data. Thank you very much. I have read the data successfully according to your solutions with adding the parameter 'preload=True' in code 'data = mne.io.read_raw_edf(file)' at the same time. – Mindy Aug 17 '18 at 05:39
  • @Mindy I'm glad to hear my response was helpful. – BossaNova Aug 18 '18 at 07:31
  • What if it's not EEG and I don't want to use MNE for dependency reasons? – dcxst Apr 29 '22 at 09:15
  • 1
    @dcxst I would ask a new question, link to this one and specify why this isn't sufficient for you – eric Sep 18 '22 at 18:36
0

Another way to read an edf file --> array using pyedflib (if you don't want to use mne for dependency reasons):

 import pyedflib
 def edf_to_arr(edf_path):
    f = pyedflib.EdfReader(edf_path)
    n = f.signals_in_file
    signal_labels = f.getSignalLabels()
    sigbufs = np.zeros((n, f.getNSamples()[0]))
    for i in np.arange(n):
        sigbufs[i, :] = f.readSignal(i)
    
    return sigbufs

There's more documentation here: pyedflib docs

schro
  • 21
  • 2