edfrd is a Python 3 software library to read and write EDF files.
Project description
edfrd
edfrd is a Python 3 software library to read and write EDF files.
It is designed as a low-level library, that does not interpret the EDF data whenever possible. Therefore, edfrd can read files even if non-standard values are contained in the header.
Data records are loaded as int16
arrays using numpy.
Installation
pip3 install --user edfrd
Reading EDF Header and Data Records
from edfrd import read_header, read_data_records
file_path = 'PATH/TO/FILE.edf'
header = read_header(file_path)
print(header)
for data_record in read_data_records(file_path, header): # generator
# iterate through data_records
break
for signal in data_record:
# iterate through signal arrays of a single data_record
print(signal.size)
for signal_header, signal in zip(header.signals, data_record):
# iterate through signal headers and signal arrays
print(signal_header.label, signal.size)
If the header of your EDF file does not correctly specifiy the number of data records, you can use the following option to calculate it from the file size.
header = read_header(file_path, calculate_number_of_data_records=True)
You can try parsing the startdate_of_recording
and starttime_of_recording
as integer tuples. If parsing fails the
original string will be returned.
header = read_header(file_path, parse_date_time=True)
day, month, year = header.startdate_of_recording
hours, minutes, seconds = header.starttime_of_recording
The number of data records being read can be limited by specifying an optional start
or end
index.
for data_record in read_data_records(file_path, header, start=0, end=header.number_of_data_records):
break
To work with larger chunks of a signal than provided by a data record, consider creating a new numpy array as a
buffer
.
import numpy as np
from edfrd import read_header, read_data_records
file_path = 'PATH/TO/FILE.edf'
header = read_header(file_path)
start, end = 2, 4
signal_index = 0
signal_header = header.signals[signal_index]
buffer_length = (end - start) * signal_header.nr_of_samples_in_each_data_record
buffer = np.empty(buffer_length, dtype=np.int16)
pointer = 0
for data_record in read_data_records(start, end):
buffer[pointer:pointer+signal_header.nr_of_samples_in_each_data_record] = data_record[signal_index]
pointer += signal_header.nr_of_samples_in_each_data_record
print(buffer)
You can also pass a file descriptor (fr
) instead of a string (file_path
). Note that read_data_records
will
continue from the current byte position, where read_header
stopped, without performing an additional seek operation.
with open(file_path, 'rb') as fr:
header = read_header(fr)
for data_record in read_data_records(fr, header):
break
Writing EDF Header and Data Records
from edfrd import read_header, read_data_records, write_header, write_data_records
file_path = 'PATH/TO/FILE.edf'
new_file_path = 'PATH/TO/NEW_FILE.edf'
header = read_header(file_path)
data_records = read_data_records(file_path, header)
write_header(file_path, header)
write_data_records(file_path, data_records)
Again, using file descriptors (fr
and fw
) is possible.
with open(file_path, 'rb') as fr:
header = read_header(fr)
data_records = read_data_records(fr, header)
with open(new_file_path, 'wb') as fw:
write_header(fw, header)
write_data_records(fw, data_records)
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file edfrd-0.7.tar.gz
.
File metadata
- Download URL: edfrd-0.7.tar.gz
- Upload date:
- Size: 6.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/0.12.16 CPython/3.7.3 Linux/5.1.15-300.fc30.x86_64
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7bb83cfe355f3a74d72484875df3040d7856f0be90f06f46901ce737389e0a3d |
|
MD5 | d7d5eb6253a3ce24a4399de46b69cc85 |
|
BLAKE2b-256 | cc6b397e113ae4d12f134a53cd33316f43425d8774ea0cd73ff674926027df24 |
File details
Details for the file edfrd-0.7-py3-none-any.whl
.
File metadata
- Download URL: edfrd-0.7-py3-none-any.whl
- Upload date:
- Size: 7.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/0.12.16 CPython/3.7.3 Linux/5.1.15-300.fc30.x86_64
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e11c364a75ded4b94701fef87184415bb8f04e986fe15126c6c9a95036a2f250 |
|
MD5 | 962b45e6093552a9a305838a2a10a550 |
|
BLAKE2b-256 | 64b3fb09e21c5fb091fb2b27d269654b6c0aa79f960ff916d11a90e7d0625686 |