EDF/BDF highlevel functions

pyedflib.highlevel.change_polarity(edf_file, channels, new_file=None, verify=True, verbose=False)[source]

Change polarity of certain channels

Parameters:
edf_file : str

from which file to change polarity.

channels : list of int

the indices of the channels.

new_file : str, optional

where to save the edf with inverted channels. The default is None.

verify : bool, optional

whether to verify the two edfs for similarity. The default is True.

verbose : str, optional

print progress or not. The default is True.

Returns:
bool

True if success.

pyedflib.highlevel.rename_channels(edf_file, mapping, new_file=None, verbose=False)[source]

A convenience function to rename channels in an EDF file.

Parameters:
edf_file : str

an string pointing to an edf file.

mapping : dict

a dictionary with channel mappings as key:value. eg: {‘M1-O2’:’A1-O2’}

new_file : str, optional

the new filename. If None will be edf_file + ‘_renamed’ The default is None.

verbose : bool, optional

print progress or not. The default is False.

Returns:
bool

True if successful, False if failed.

pyedflib.highlevel.anonymize_edf(edf_file, new_file=None, to_remove=['patientname', 'birthdate'], new_values=['xxx', ''], verify=False, verbose=False)[source]

Anonymize an EDF file by replacing values of header fields.

This function can be used to overwrite all header information that is patient specific, for example birthdate and patientname. All header fields can be overwritten this way (i.e., all header.keys() given _, _, header = read_edf(edf_file, digital=True)).

Parameters:
edf_file : str

Filename of an EDF/BDF.

new_file : str | None

The filename of the anonymized file. If None, the input filename appended with ‘_anonymized’ is used. Defaults to None.

to_remove : list of str

List of attributes to overwrite in the edf_file. Defaults to [‘patientname’, ‘birthdate’].

new_values : list of str

List of values used for overwriting the attributes specified in to_remove. Each item in to_remove must have a corresponding item in new_values. Defaults to [‘xxx’, ‘’].

verify : bool

Compare edf_file and new_file for equality (i.e., double check that values are same). Defaults to False

verbose : bool, optional

print progress or not. The default is False.

Returns:
bool

True if successful, or if verify is False. Raises an error otherwise.

pyedflib.highlevel.drop_channels(edf_source, edf_target=None, to_keep=None, to_drop=None, verbose=False)[source]

Remove channels from an edf file. Save the file. For safety reasons, no source files can be overwritten.

Parameters:
edf_source : str

The source edf file from which to drop channels.

edf_target : str, optional

Where to save the file.If None, will be edf_source+’dropped.edf’. The default is None.

to_keep : list, optional

A list of channel names or indices that will be kept. Strings will always be interpreted as channel names. ‘to_keep’ will overwrite any droppings proposed by to_drop. The default is None.

to_drop : list, optional

A list of channel names/indices that should be dropped. Strings will be interpreted as channel names. The default is None.

verbose : bool, optional

print progress or not. The default is False.

Returns:
edf_target : str

the target filename with the dropped channels.

pyedflib.highlevel.compare_edf(edf_file1, edf_file2, verbose=False)[source]

Loads two edf files and checks whether the values contained in them are the same. Does not check the header or annotations data.

Mainly to verify that other options (eg anonymization) produce the same EDF file.

Parameters:
edf_file1 : str

edf file 1 to compare.

edf_file2 : str

edf file 2 to compare.

verbose : bool, optional

print progress or not. The default is False.

Returns:
bool

True if signals are equal, else raises error.

pyedflib.highlevel.read_edf_header(edf_file, read_annotations=True)[source]

Reads the header and signal headers of an EDF file and it’s annotations

Parameters:
edf_file : str

EDF/BDF file to read.

Returns:
summary : dict

header of the edf file as dictionary.

pyedflib.highlevel.write_edf_quick(edf_file, signals, sfreq, digital=False)[source]

wrapper for write_pyedf without creating headers. Use this if you don’t care about headers or channel names and just want to dump some signals with the same sampling freq. to an edf

Parameters:
edf_file : str

where to store the data/edf.

signals : np.ndarray

The signals you want to store as numpy array.

sfreq : int

the sampling frequency of the signals.

digital : bool, optional

if the data is present digitally (int) or as mV/uV.The default is False.

Returns:
bool

True if successful, else False or raise Error.

pyedflib.highlevel.write_edf(edf_file, signals, signal_headers, header=None, digital=False, file_type=-1, block_size=1)[source]

Write signals to an edf_file. Header can be generated on the fly with generic values. EDF+/BDF+ is selected based on the filename extension, but can be overwritten by setting file_type to pyedflib.FILETYPE_XXX

Parameters:
edf_file : np.ndarray or list

where to save the EDF file

signals : list

The signals as a list of arrays or a ndarray.

signal_headers : list of dict

a list with one signal header(dict) for each signal. See pyedflib.EdfWriter.setSignalHeader..

header : dict

a main header (dict) for the EDF file, see pyedflib.EdfWriter.setHeader for details. If no header present, will create an empty header

digital : bool, optional

whether the signals are in digital format (ADC). The default is False.

file_type: int, optional

choose file_type for saving. EDF = 0, EDF+ = 1, BDF = 2, BDF+ = 3, automatic from extension = -1

block_size : int

set the block size for writing. Should be divisor of signal length in seconds. Higher values mean faster writing speed, but if it is not a divisor of the signal duration, it will append zeros. Can be any value between 1=><=60, -1 will auto-infer the fastest value.

Returns:
bool

True if successful, False if failed.

pyedflib.highlevel.read_edf(edf_file, ch_nrs=None, ch_names=None, digital=False, verbose=False)[source]

Convenience function for reading EDF+/BDF data with pyedflib.

Will load the edf and return the signals, the headers of the signals and the header of the EDF. If all signals have the same sample frequency will return a numpy array, else a list with the individual signals

Parameters:
edf_file : str

link to an edf file.

ch_nrs : list of int, optional

The indices of the channels to read. The default is None.

ch_names : list of str, optional

The names of channels to read. The default is None.

digital : bool, optional

will return the signals as digital values (ADC). The default is False.

verbose : bool, optional

Print progress bar while loading or not. The default is False.

Returns:
signals : np.ndarray or list

the signals of the chosen channels contained in the EDF.

signal_headers : list

one signal header for each channel in the EDF.

header : dict

the main header of the EDF file containing meta information.

pyedflib.highlevel.make_signal_headers(list_of_labels, dimension='uV', sample_rate=256, sample_frequency=None, physical_min=-200.0, physical_max=200.0, digital_min=-32768, digital_max=32767, transducer='', prefiler='')[source]

A function that creates signal headers for a given list of channel labels. This can only be used if each channel has the same sampling frequency

Parameters:
list_of_labels : list of str

A list with labels for each channel.

dimension : str, optional

dimension, eg mV. The default is ‘uV’.

sample_rate : int, optional

sampling frequency. The default is 256. Deprecated: use ‘sample_frequency’ instead.

sample_frequency : int, optional

sampling frequency. The default is 256.

physical_min : float, optional

minimum value in dimension. The default is -200.

physical_max : float, optional

maximum value in dimension. The default is 200.

digital_min : int, optional

digital minimum of the ADC. The default is -32768.

digital_max : int, optional

digital maximum of the ADC. The default is 32767.

transducer : str, optional

electrode type that was used. The default is ‘’.

prefiler : str, optional

filtering and sampling method. The default is ‘’.

Returns:
signal_headers : list of dict

returns n signal headers as a list to save several signal headers.

pyedflib.highlevel.make_signal_header(label, dimension='uV', sample_rate=256, sample_frequency=None, physical_min=-200, physical_max=200, digital_min=-32768, digital_max=32767, transducer='', prefiler='')[source]

A convenience function that creates a signal header for a given signal. This can be used to create a list of signal headers that is used by pyedflib to create an edf. With this, different sampling frequencies can be indicated.

Parameters:
label : str

the name of the channel.

dimension : str, optional

dimension, eg mV. The default is ‘uV’.

sample_rate : int, optional

sampling frequency. The default is 256. Deprecated: use ‘sample_frequency’ instead.

sample_frequency : int, optional

sampling frequency. The default is 256.

physical_min : float, optional

minimum value in dimension. The default is -200.

physical_max : float, optional

maximum value in dimension. The default is 200.

digital_min : int, optional

digital minimum of the ADC. The default is -32768.

digital_max : int, optional

digital maximum of the ADC. The default is 32767.

transducer : str, optional

electrode type that was used. The default is ‘’.

prefiler : str, optional

filtering and sampling method. The default is ‘’.

Returns:
signal_header : dict

a signal header that can be used to save a channel to an EDF.

pyedflib.highlevel.make_header(technician='', recording_additional='', patientname='', patient_additional='', patientcode='', equipment='', admincode='', gender='', startdate=None, birthdate='')[source]

A convenience function to create an EDF header (a dictionary) that can be used by pyedflib to update the main header of the EDF

Parameters:
technician : str, optional

name of the technician. The default is ‘’.

recording_additional : str, optional

comments etc. The default is ‘’.

patientname : str, optional

the name of the patient. The default is ‘’.

patient_additional : TYPE, optional

more info about the patient. The default is ‘’.

patientcode : str, optional

alphanumeric code. The default is ‘’.

equipment : str, optional

which system was used. The default is ‘’.

admincode : str, optional

code of the admin. The default is ‘’.

gender : str, optional

gender of patient. The default is ‘’.

startdate : datetime.datetime, optional

startdate of recording. The default is None.

birthdate : str/datetime.datetime, optional

date of birth of the patient. The default is ‘’.

Returns:
header : dict

a dictionary with the values given filled in.

pyedflib.highlevel.phys2dig(signal, dmin, dmax, pmin, pmax)[source]

converts physical values to digital values

Parameters:
signal : np.ndarray or int

A numpy array with int values (digital values) or an int.

dmin : int

digital minimum value of the edf file (eg -2048).

dmax : int

digital maximum value of the edf file (eg 2048).

pmin : float

physical maximum value of the edf file (eg -200.0).

pmax : float

physical maximum value of the edf file (eg 200.0).

Returns:
digital : np.ndarray or int

converted digital values

pyedflib.highlevel.dig2phys(signal, dmin, dmax, pmin, pmax)[source]

converts digital edf values to physical values

Parameters:
signal : np.ndarray or int

A numpy array with int values (digital values) or an int.

dmin : int

digital minimum value of the edf file (eg -2048).

dmax : int

digital maximum value of the edf file (eg 2048).

pmin : float

physical maximum value of the edf file (eg -200.0).

pmax : float

physical maximum value of the edf file (eg 200.0).

Returns:
physical : np.ndarray or float

converted physical values