Reading EDF File In Python

Sunny Kumar
3 min readMay 25, 2023

--

Have you ever faced an “.edf” file handling issue? if your answer is yes, this article is your one shot at it. In further sections we will be discussing the reading and handling of “.edf “ file using python. Let us first understand what is an .edf file?

According to wikepedia “European Data Format (EDF) is a standard file format designed for exchange and storage of medical time series. Being an open and non-proprietary format, EDF(+) is commonly used to archive, exchange and analyse data from commercial devices in a format that is independent of the acquisition system”. If you are a software developer EDF file will look like latin to you. We often need expert to analyse the output of EDF file.

Where to get Sample .edf data?

Here is the link for sample dataset which we will use for the blog. It contains electroencephalograms (EEG) of four dying patients before and after the clinical withdrawal of their ventilatory support.

Step 1

let us first install the required libraries. There is python package to analyse .edf file which can be accessed here. In below code chunk , using pip I have installed the library.

pip install pyEDFlib

step 2

Once installed, it is time to import all the necessary libraries required further. “highlevel” from pyedflib will give us easy access to signals and its labels and matplotlib will be used for visualisation of signals.

from pyedflib import highlevel
import pyedflib as plib
import numpy as np
import matplotlib.pyplot as plt

step 3

It is just 2 lines of code to read the file. Using “highlevel” we can return signals, signal_headers, header , names are self explanatory here.

path = "filepath/Pt1_S1.edf"
signals, signal_headers, header = highlevel.read_edf(path)

step 4

let us see what signal we have in our file, we can directly print the same. Any metadata about patient can be seen using header.

step 5

Too much too handle for signals, let us try to plot it using matplotlib.

n = len(signals)

fig = plt.figure(figsize=(150,50))
ax = plt.axes()
for i in np.arange(n):
ax.plot(signals[i] , color='purple' )
plt.show()

let us visualise another patients data.

Conclusion

In this blog we looked at .edf file reading and handling. There is a lot more to it. We often need expert to analyse these type of data. I hope you enjoyed it. Happy reading :).

--

--

Sunny Kumar

Loves technology . Combining AI and AR to create meaningful solutions.