wfdb

These core components are accessible by importing the wfdb package directly, as well as from their respective subpackages.

WFDB Records

wfdb.rdrecord(record_name, sampfrom=0, sampto=None, channels=None, physical=True, pn_dir=None, m2s=True, smooth_frames=True, ignore_skew=False, return_res=64, force_channels=True, channel_names=None, warn_empty=False)

Read a WFDB record and return the signal and record descriptors as attributes in a Record or MultiRecord object.

record_name : str
The name of the WFDB record to be read, without any file extensions. If the argument contains any path delimiter characters, the argument will be interpreted as PATH/BASE_RECORD. Both relative and absolute paths are accepted. If the pn_dir parameter is set, this parameter should contain just the base record name, and the files fill be searched for remotely. Otherwise, the data files will be searched for in the local path. Can also handle .edf and .wav files as long as the extension is provided in the record_name.
sampfrom : int, optional
The starting sample number to read for all channels.
sampto : int, ‘end’, optional
The sample number at which to stop reading for all channels. Reads the entire duration by default.
channels : list, optional
List of integer indices specifying the channels to be read. Reads all channels by default.
physical : bool, optional
Specifies whether to return signals in physical units in the p_signal field (True), or digital units in the d_signal field (False).
pn_dir : str, optional
Option used to stream data from Physionet. The Physionet database directory from which to find the required record files. eg. For record ‘100’ in ‘http://physionet.org/content/mitdb’ pn_dir=’mitdb’.
m2s : bool, optional
Used when reading multi-segment records. Specifies whether to directly return a WFDB MultiRecord object (False), or to convert it into and return a WFDB Record object (True).
smooth_frames : bool, optional
Used when reading records with signals having multiple samples per frame. Specifies whether to smooth the samples in signals with more than one sample per frame and return an (MxN) uniform numpy array as the d_signal or p_signal field (True), or to return a list of 1d numpy arrays containing every expanded sample as the e_d_signal or e_p_signal field (False).
ignore_skew : bool, optional
Used when reading records with at least one skewed signal. Specifies whether to apply the skew to align the signals in the output variable (False), or to ignore the skew field and load in all values contained in the dat files unaligned (True).
return_res : int, optional
The numpy array dtype of the returned signals. Options are: 64, 32, 16, and 8, where the value represents the numpy int or float dtype. Note that the value cannot be 8 when physical is True since there is no float8 format.
force_channels : bool, optional
Used when reading multi-segment variable layout records. Whether to update the layout specification record, and the converted Record object if m2s is True, to match the input channels argument, or to omit channels in which no read segment contains the signals.
channel_names : list, optional
List of channel names to return. If this parameter is specified, it takes precedence over channels.
warn_empty : bool, optional
Whether to display a warning if the specified channel indices or names are not contained in the record, and no signal is returned.
record : Record or MultiRecord
The WFDB Record or MultiRecord object representing the contents of the record read.

If a signal range or channel selection is specified when calling this function, the resulting attributes of the returned object will be set to reflect the section of the record that is actually read, rather than necessarily the entire record. For example, if channels=[0, 1, 2] is specified when reading a 12 channel record, the ‘n_sig’ attribute will be 3, not 12.

The rdsamp function exists as a simple alternative to rdrecord for the common purpose of extracting the physical signals and a few important descriptor fields.

>>> record = wfdb.rdrecord('sample-data/test01_00s', sampfrom=800,
                           channels=[1, 3])
wfdb.rdheader(record_name, pn_dir=None, rd_segments=False)

Read a WFDB header file and return a Record or MultiRecord object with the record descriptors as attributes.

record_name : str
The name of the WFDB record to be read, without any file extensions. If the argument contains any path delimiter characters, the argument will be interpreted as PATH/BASE_RECORD. Both relative and absolute paths are accepted. If the pn_dir parameter is set, this parameter should contain just the base record name, and the files fill be searched for remotely. Otherwise, the data files will be searched for in the local path.
pn_dir : str, optional
Option used to stream data from Physionet. The Physionet database directory from which to find the required record files. eg. For record ‘100’ in ‘http://physionet.org/content/mitdb’ pn_dir=’mitdb’.
rd_segments : bool, optional
Used when reading multi-segment headers. If True, segment headers will also be read (into the record object’s segments field).
record : Record or MultiRecord
The WFDB Record or MultiRecord object representing the contents of the header read.
>>> ecg_record = wfdb.rdheader('sample-data/test01_00s', sampfrom=800,
                               channels = [1,3])
wfdb.rdsamp(record_name, sampfrom=0, sampto=None, channels=None, pn_dir=None, channel_names=None, warn_empty=False, return_res=64)

Read a WFDB record, and return the physical signals and a few important descriptor fields.

record_name : str
The name of the WFDB record to be read (without any file extensions). If the argument contains any path delimiter characters, the argument will be interpreted as PATH/baserecord and the data files will be searched for in the local path.
sampfrom : int, optional
The starting sample number to read for all channels.
sampto : int, ‘end’, optional
The sample number at which to stop reading for all channels. Reads the entire duration by default.
channels : list, optional
List of integer indices specifying the channels to be read. Reads all channels by default.
pn_dir : str, optional
Option used to stream data from Physionet. The Physionet database directory from which to find the required record files. eg. For record ‘100’ in ‘http://physionet.org/content/mitdb’ pn_dir=’mitdb’.
channel_names : list, optional
List of channel names to return. If this parameter is specified, it takes precedence over channels.
warn_empty : bool, optional
Whether to display a warning if the specified channel indices or names are not contained in the record, and no signal is returned.
return_res : int, optional
The numpy array dtype of the returned signals. Options are: 64, 32, 16, and 8, where the value represents the numpy int or float dtype. Note that the value cannot be 8 when physical is True since there is no float8 format.
signals : ndarray
A 2d numpy array storing the physical signals from the record.
fields : dict

A dictionary containing several key attributes of the read record:

  • fs: The sampling frequency of the record.
  • units: The units for each channel.
  • sig_name: The signal name for each channel.
  • comments: Any comments written in the header.

If a signal range or channel selection is specified when calling this function, the resulting attributes of the returned object will be set to reflect the section of the record that is actually read, rather than necessarily the entire record. For example, if channels=[0, 1, 2] is specified when reading a 12 channel record, the ‘n_sig’ attribute will be 3, not 12.

The rdrecord function is the base function upon which this one is built. It returns all attributes present, along with the signals, as attributes in a Record object. The function, along with the returned data type, has more options than rdsamp for users who wish to more directly manipulate WFDB content.

>>> signals, fields = wfdb.rdsamp('sample-data/test01_00s',
                                  sampfrom=800,
                                  channel =[1,3])
wfdb.wrsamp(record_name, fs, units, sig_name, p_signal=None, d_signal=None, fmt=None, adc_gain=None, baseline=None, comments=None, base_time=None, base_date=None, write_dir='')

Write a single segment WFDB record, creating a WFDB header file and any associated dat files.

record_name : str
The string name of the WFDB record to be written (without any file extensions). Must not contain any “.” since this would indicate an EDF file which is not compatible at this point.
fs : int, float
The sampling frequency of the record.
units : list
A list of strings giving the units of each signal channel.
sig_name : list, str
A list of strings giving the signal name of each signal channel.
p_signal : ndarray, optional
An (MxN) 2d numpy array, where M is the signal length. Gives the physical signal values intended to be written. Either p_signal or d_signal must be set, but not both. If p_signal is set, this method will use it to perform analogue-digital conversion, writing the resultant digital values to the dat file(s). If fmt is set, gain and baseline must be set or unset together. If fmt is unset, gain and baseline must both be unset.
d_signal : ndarray, optional
An (MxN) 2d numpy array, where M is the signal length. Gives the digital signal values intended to be directly written to the dat file(s). The dtype must be an integer type. Either p_signal or d_signal must be set, but not both. In addition, if d_signal is set, fmt, gain and baseline must also all be set.
fmt : list, optional
A list of strings giving the WFDB format of each file used to store each channel. Accepted formats are: ‘80’,’212’,’16’,’24’, and ‘32’. There are other WFDB formats as specified by: https://www.physionet.org/physiotools/wag/signal-5.htm but this library will not write (though it will read) those file types.
adc_gain : list, optional
A list of numbers specifying the ADC gain.
baseline : list, optional
A list of integers specifying the digital baseline.
comments : list, optional
A list of string comments to be written to the header file.
base_time : str, optional
A string of the record’s start time in 24h ‘HH:MM:SS(.ms)’ format.
base_date : str, optional
A string of the record’s start date in ‘DD/MM/YYYY’ format.
write_dir : str, optional
The directory in which to write the files.

N/A

This is a gateway function, written as a simple method to write WFDB record files using the most common parameters. Therefore not all WFDB fields can be set via this function.

For more control over attributes, create a Record object, manually set its attributes, and call its wrsamp instance method. If you choose this more advanced method, see also the set_defaults, set_d_features, and set_p_features instance methods to help populate attributes.

>>> # Read part of a record from Physionet
>>> signals, fields = wfdb.rdsamp('a103l', sampfrom=50000, channels=[0,1],
                                  pn_dir='challenge-2015/training')
>>> # Write a local WFDB record (manually inserting fields)
>>> wfdb.wrsamp('ecgrecord', fs = 250, units=['mV', 'mV'],
                sig_name=['I', 'II'], p_signal=signals, fmt=['16', '16'])
wfdb.edf2mit(record_name, pn_dir=None, delete_file=True, record_only=True, header_only=False, verbose=False, rdedfann_flag=False)

Convert EDF formatted files to MIT format.

Many EDF files contain signals at widely varying sampling frequencies. edf2mit handles these properly, but the default behavior of most WFDB applications is to read such data in low-resolution mode (in which all signals are resampled at the lowest sampling frequency used for any signal in the record). This is almost certainly not what you want if, for example, the record contains EEG signals sampled at 200 Hz and body temperature sampled at 1 Hz; by default, applications such as rdsamp will resample the EEGs (and any other signals in the record) at 1 Hz. To avoid this behavior, you can set smooth_frames to False (high resolution) provided by rdrecord and a few other WFDB applications.

Note that applications built using version 3.1.0 and later versions of the WFDB-Python library can read EDF files directly, so that the conversion performed by edf2mit is no longer necessary. However, one can still use this function to produce WFDB-compatible files from EDF files if desired.

record_name : str
The name of the input EDF record to be read.
pn_dir : str, optional
Option used to stream data from Physionet. The Physionet database directory from which to find the required record files. eg. For record ‘100’ in ‘http://physionet.org/content/mitdb’ pn_dir=’mitdb’.
delete_file : bool, optional
Whether to delete the saved EDF file (False) or not (True) after being imported.
record_only : bool, optional
Whether to only return the record information (True) or not (False). If false, this function will generate both a .dat and .hea file.
header_only : bool, optional
Whether to only return the header information (True) or not (False). If true, this function will only return [‘fs’, ‘sig_len’, ‘n_sig’, ‘base_date’, ‘base_time’, ‘units’, ‘sig_name’, ‘comments’].
verbose : bool, optional
Whether to print all the information read about the file (True) or not (False).
rdedfann_flag : bool, optional
Whether the function is being called by rdedfann or the user. If it is being called by the user and the file has annotations, then warn them that the EDF file has annotations and that they should use rdedfann instead.
record : dict, optional
All of the record information needed to generate MIT formatted files. Only returns if ‘record_only’ is set to True, else generates the corresponding .dat and .hea files. This record file will not match the rdrecord output since it will only give us the digital signal for now.

The entire file is composed of (seen here: https://www.edfplus.info/specs/edf.html):

HEADER RECORD (we suggest to also adopt the 12 simple additional EDF+ specs) 8 ascii : version of this data format (0) 80 ascii : local patient identification (mind item 3 of the additional EDF+ specs) 80 ascii : local recording identification (mind item 4 of the additional EDF+ specs) 8 ascii : startdate of recording (dd.mm.yy) (mind item 2 of the additional EDF+ specs) 8 ascii : starttime of recording (hh.mm.ss) 8 ascii : number of bytes in header record 44 ascii : reserved 8 ascii : number of data records (-1 if unknown, obey item 10 of the additional EDF+ specs) 8 ascii : duration of a data record, in seconds 4 ascii : number of signals (ns) in data record ns * 16 ascii : ns * label (e.g. EEG Fpz-Cz or Body temp) (mind item 9 of the additional EDF+ specs) ns * 80 ascii : ns * transducer type (e.g. AgAgCl electrode) ns * 8 ascii : ns * physical dimension (e.g. uV or degreeC) ns * 8 ascii : ns * physical minimum (e.g. -500 or 34) ns * 8 ascii : ns * physical maximum (e.g. 500 or 40) ns * 8 ascii : ns * digital minimum (e.g. -2048) ns * 8 ascii : ns * digital maximum (e.g. 2047) ns * 80 ascii : ns * prefiltering (e.g. HP:0.1Hz LP:75Hz) ns * 8 ascii : ns * nr of samples in each data record ns * 32 ascii : ns * reserved

DATA RECORD nr of samples[1] * integer : first signal in the data record nr of samples[2] * integer : second signal .. .. nr of samples[ns] * integer : last signal

Bytes 0 - 127: descriptive text Bytes 128 - 131: master tag (data type = matrix) Bytes 132 - 135: master tag (data size) Bytes 136 - 151: array flags (4 byte tag with data type, 4 byte

tag with subelement size, 8 bytes of content)
Bytes 152 - 167: array dimension (4 byte tag with data type, 4
byte tag with subelement size, 8 bytes of content)
Bytes 168 - 183: array name (4 byte tag with data type, 4 byte
tag with subelement size, 8 bytes of content)
Bytes 184 - …: array content (4 byte tag with data type, 4 byte
tag with subelement size, … bytes of content)
>>> edf_record = wfdb.edf2mit('x001_FAROS.edf',
                              pn_dir='simultaneous-measurements/raw_data')
wfdb.mit2edf(record_name, pn_dir=None, sampfrom=0, sampto=None, channels=None, output_filename='', edf_plus=False)

These programs convert EDF (European Data Format) files into WFDB-compatible files (as used in PhysioNet) and vice versa. European Data Format (EDF) was originally designed for storage of polysomnograms.

Note that WFDB format does not include a standard way to specify the transducer type or the prefiltering specification; these parameters are not preserved by these conversion programs. Also note that use of the standard signal and unit names specified for EDF is permitted but not enforced by mit2edf.

record_name : str
The name of the input WFDB record to be read. Can also work with both EDF and WAV files.
pn_dir : str, optional
Option used to stream data from Physionet. The Physionet database directory from which to find the required record files. eg. For record ‘100’ in ‘http://physionet.org/content/mitdb’ pn_dir=’mitdb’.
sampfrom : int, optional
The starting sample number to read for all channels.
sampto : int, ‘end’, optional
The sample number at which to stop reading for all channels. Reads the entire duration by default.
channels : list, optional
List of integer indices specifying the channels to be read. Reads all channels by default.
output_filename : str, optional
The desired name of the output file. If this value set to the default value of ‘’, then the output filename will be ‘REC.edf’.
edf_plus : bool, optional
Whether to write the output file in EDF (False) or EDF+ (True) format.

N/A

The entire file is composed of (seen here: https://www.edfplus.info/specs/edf.html):

HEADER RECORD (we suggest to also adopt the 12 simple additional EDF+ specs) 8 ascii : version of this data format (0) 80 ascii : local patient identification (mind item 3 of the additional EDF+ specs) 80 ascii : local recording identification (mind item 4 of the additional EDF+ specs) 8 ascii : startdate of recording (dd.mm.yy) (mind item 2 of the additional EDF+ specs) 8 ascii : starttime of recording (hh.mm.ss) 8 ascii : number of bytes in header record 44 ascii : reserved 8 ascii : number of data records (-1 if unknown, obey item 10 of the additional EDF+ specs) 8 ascii : duration of a data record, in seconds 4 ascii : number of signals (ns) in data record ns * 16 ascii : ns * label (e.g. EEG Fpz-Cz or Body temp) (mind item 9 of the additional EDF+ specs) ns * 80 ascii : ns * transducer type (e.g. AgAgCl electrode) ns * 8 ascii : ns * physical dimension (e.g. uV or degreeC) ns * 8 ascii : ns * physical minimum (e.g. -500 or 34) ns * 8 ascii : ns * physical maximum (e.g. 500 or 40) ns * 8 ascii : ns * digital minimum (e.g. -2048) ns * 8 ascii : ns * digital maximum (e.g. 2047) ns * 80 ascii : ns * prefiltering (e.g. HP:0.1Hz LP:75Hz) ns * 8 ascii : ns * nr of samples in each data record ns * 32 ascii : ns * reserved

DATA RECORD nr of samples[1] * integer : first signal in the data record nr of samples[2] * integer : second signal .. .. nr of samples[ns] * integer : last signal

Bytes 0 - 127: descriptive text Bytes 128 - 131: master tag (data type = matrix) Bytes 132 - 135: master tag (data size) Bytes 136 - 151: array flags (4 byte tag with data type, 4 byte

tag with subelement size, 8 bytes of content)
Bytes 152 - 167: array dimension (4 byte tag with data type, 4
byte tag with subelement size, 8 bytes of content)
Bytes 168 - 183: array name (4 byte tag with data type, 4 byte
tag with subelement size, 8 bytes of content)
Bytes 184 - …: array content (4 byte tag with data type, 4 byte
tag with subelement size, … bytes of content)
>>> wfdb.mit2edf('100', pn_dir='pwave')

The output file name is ‘100.edf’

wfdb.wav2mit(record_name, pn_dir=None, delete_file=True, record_only=False)

Convert .wav (format 16, multiplexed signals, with embedded header information) formatted files to MIT format. See here for more details about the formatting of a .wav file: http://soundfile.sapp.org/doc/WaveFormat/.

This process may not work with some .wav files that are encoded using variants of the original .wav format that are not WFDB-compatible. In principle, this program should be able to recognize such files by their format codes, and it will produce an error message in such cases. If the format code is incorrect, however, wav2mit may not recognize that an error has occurred.

record_name : str
The name of the input .wav record to be read.
pn_dir : str, optional
Option used to stream data from Physionet. The Physionet database directory from which to find the required record files. eg. For record ‘100’ in ‘http://physionet.org/content/mitdb’ pn_dir=’mitdb’.
delete_file : bool, optional
Whether to delete the saved .wav file (False) or not (True) after being imported.
record_only : bool, optional
Whether to only return the record information (True) or not (False). If false, this function will generate both a .dat and .hea file.
record : dict, optional
All of the record information needed to generate MIT formatted files. Only returns if ‘record_only’ is set to True, else generates the corresponding .dat and .hea files. This record file will not match the rdrecord output since it will only give us the digital signal for now.

Files that can be processed successfully using wav2mit always have exactly three chunks (a header chunk, a format chunk, and a data chunk). In .wav files, binary data are always written in little-endian format (least significant byte first). The format of wav2mit’s input files is as follows:

[Header chunk] Bytes 0 - 3: “RIFF” [4 ASCII characters] Bytes 4 - 7: L-8 (number of bytes to follow in the file, excluding bytes 0-7) Bytes 8 - 11: “WAVE” [4 ASCII characters]

[Format chunk] Bytes 12 - 15: “fmt ” [4 ASCII characters, note trailing space] Bytes 16 - 19: 16 (format chunk length in bytes, excluding bytes 12-19) Bytes 20 - 35: format specification, consisting of: Bytes 20 - 21: 1 (format tag, indicating no compression is used) Bytes 22 - 23: number of signals (1 - 65535) Bytes 24 - 27: sampling frequency in Hz (per signal)

Note that the sampling frequency in a .wav file must be an integer multiple of 1 Hz, a restriction that is not imposed by MIT (WFDB) format.

Bytes 28 - 31: bytes per second (sampling frequency * frame size in bytes) Bytes 32 - 33: frame size in bytes Bytes 34 - 35: bits per sample (ADC resolution in bits)

Note that the actual ADC resolution (e.g., 12) is written in this field, although each output sample is right-padded to fill a full (16-bit) word. (.wav format allows for 8, 16, 24, and 32 bits per sample)

[Data chunk] Bytes 36 - 39: “data” [4 ASCII characters] Bytes 40 - 43: L-44 (number of bytes to follow in the data chunk) Bytes 44 - L-1: sample data, consisting of: Bytes 44 - 45: sample 0, channel 0 Bytes 46 - 47: sample 0, channel 1 … etc. (same order as in a multiplexed WFDB signal file)

>>> wav_record = wfdb.wav2mit('sample-data/SC4001E0-PSG.wav', record_only=True)
wfdb.mit2wav(record_name, pn_dir=None, sampfrom=0, sampto=None, channels=None, output_filename='', write_header=False)

This program converts a WFDB record into .wav format (format 16, multiplexed signals, with embedded header information). Use ‘wav2mit’ to perform the reverse conversion.

record_name : str
The name of the input WFDB record to be read. Can also work with both EDF and WAV files.
pn_dir : str, optional
Option used to stream data from Physionet. The Physionet database directory from which to find the required record files. eg. For record ‘100’ in ‘http://physionet.org/content/mitdb’ pn_dir=’mitdb’.
sampfrom : int, optional
The starting sample number to read for all channels.
sampto : int, ‘end’, optional
The sample number at which to stop reading for all channels. Reads the entire duration by default.
channels : list, optional
List of integer indices specifying the channels to be read. Reads all channels by default.
output_filename : str, optional
The desired name of the output file. If this value set to the default value of ‘’, then the output filename will be ‘REC.wav’.
write_header : bool, optional
Whether to write (True) or not to write (False) a header file to accompany the generated WAV file. The default value is ‘False’.

N/A

Files that can be processed successfully using wav2mit always have exactly three chunks (a header chunk, a format chunk, and a data chunk). In .wav files, binary data are always written in little-endian format (least significant byte first). The format of wav2mit’s input files is as follows:

[Header chunk] Bytes 0 - 3: “RIFF” [4 ASCII characters] Bytes 4 - 7: L-8 (number of bytes to follow in the file, excluding bytes 0-7) Bytes 8 - 11: “WAVE” [4 ASCII characters]

[Format chunk] Bytes 12 - 15: “fmt ” [4 ASCII characters, note trailing space] Bytes 16 - 19: 16 (format chunk length in bytes, excluding bytes 12-19) Bytes 20 - 35: format specification, consisting of: Bytes 20 - 21: 1 (format tag, indicating no compression is used) Bytes 22 - 23: number of signals (1 - 65535) Bytes 24 - 27: sampling frequency in Hz (per signal)

Note that the sampling frequency in a .wav file must be an integer multiple of 1 Hz, a restriction that is not imposed by MIT (WFDB) format.

Bytes 28 - 31: bytes per second (sampling frequency * frame size in bytes) Bytes 32 - 33: frame size in bytes Bytes 34 - 35: bits per sample (ADC resolution in bits)

Note that the actual ADC resolution (e.g., 12) is written in this field, although each output sample is right-padded to fill a full (16-bit) word. (.wav format allows for 8, 16, 24, and 32 bits per sample)

[Data chunk] Bytes 36 - 39: “data” [4 ASCII characters] Bytes 40 - 43: L-44 (number of bytes to follow in the data chunk) Bytes 44 - L-1: sample data, consisting of: Bytes 44 - 45: sample 0, channel 0 Bytes 46 - 47: sample 0, channel 1 … etc. (same order as in a multiplexed WFDB signal file)

>>> wfdb.mit2wav('100', pn_dir='pwave')

The output file name is ‘100.wav’

wfdb.csv2mit(file_name, fs, units, fmt=None, adc_gain=None, baseline=None, samps_per_frame=None, counter_freq=None, base_counter=None, base_time=None, base_date=None, comments=None, sig_name=None, dat_file_name=None, skew=None, byte_offset=None, adc_res=None, adc_zero=None, init_value=None, checksum=None, block_size=None, record_only=False, header=True, delimiter=', ', verbose=False)

Read a WFDB header file and return either a Record object with the record descriptors as attributes or write a record and header file.

file_name : str
The name of the WFDB record to be read, without any file extensions. If the argument contains any path delimiter characters, the argument will be interpreted as PATH/BASE_RECORD. Both relative and absolute paths are accepted. If the pn_dir parameter is set, this parameter should contain just the base record name, and the files fill be searched for remotely. Otherwise, the data files will be searched for in the local path.
fs : float
This number can be expressed in any format legal for a Python input of floating point numbers (thus ‘360’, ‘360.’, ‘360.0’, and ‘3.6e2’ are all legal and equivalent). The sampling frequency must be greater than 0; if it is missing, a value of 250 is assumed.
units : list, str
This will be applied as the passed list unless a single str is passed instead - in which case the str will be assigned for all channels. This field can be present only if the ADC gain is also present. It follows the baseline field if that field is present, or the gain field if the baseline field is absent. The units field is a list of character strings that specifies the type of physical unit. If the units field is absent, the physical unit may be assumed to be 1 mV.
fmt : list, str, optional
This will be applied as the passed list unless a single str is passed instead - in which case the str will be assigned for all channels. A list of strings giving the WFDB format of each file used to store each channel. Accepted formats are: ‘80’,’212’,’16’,’24’, and ‘32’. There are other WFDB formats as specified by: https://www.physionet.org/physiotools/wag/signal-5.htm but this library will not write (though it will read) those file types. Each field is an integer that specifies the storage format of the signal. All signals in a given group are stored in the same format. The most common format is format 16 (sixteen-bit amplitudes). The parameters samps_per_frame, skew, and byte_offset are optional fields, and if present, are bound to the format field. In other words, they may be considered as format modifiers, since they further describe the encoding of samples within the signal file.
adc_gain : list, int, optional
This will be applied as the passed list unless a single int is passed instead - in which case the int will be assigned for all channels. This field is a list of numbers that specifies the difference in sample values that would be observed if a step of one physical unit occurred in the original analog signal. For ECGs, the gain is usually roughly equal to the R-wave amplitude in a lead that is roughly parallel to the mean cardiac electrical axis. If the gain is zero or missing, this indicates that the signal amplitude is uncalibrated; in such cases, a value of 200 ADC units per physical unit may be assumed.
baseline : list, int, optional
This will be applied as the passed list unless a single int is passed instead - in which case the int will be assigned for all channels. This field can be present only if the ADC gain is also present. It is not separated by whitespace from the ADC gain field; rather, it is surrounded by parentheses, which delimit it. The baseline is an integer that specifies the sample value corresponding to 0 physical units. If absent, the baseline is taken to be equal to the ADC zero. Note that the baseline need not be a value within the ADC range; for example, if the ADC input range corresponds to 200-300 degrees Kelvin, the baseline is the (extended precision) value that would map to 0 degrees Kelvin.
samps_per_frame : list, int, optional
This will be applied as the passed list unless a single int is passed instead - in which case the int will be assigned for all channels. Normally, all signals in a given record are sampled at the (base) sampling frequency as specified by fs; in this case, the number of samples per frame is 1 for all signals, and this field is conventionally omitted. If the signal was sampled at some integer multiple, n, of the base sampling frequency, however, each frame contains n samples of the signal, and the value specified in this field is also n. (Note that non-integer multiples of the base sampling frequency are not supported).
counter_freq : float, optional
This field (a floating-point number, in the same format as fs) can be present only if fs is also present. Typically, the counter frequency may be derived from an analog tape counter, or from page numbers in a chart recording. If the counter frequency is absent or not positive, it is assumed to be equal to fs.
base_counter : float, optional
This field can be present only if the counter frequency is also present. The base counter value is a floating-point number that specifies the counter value corresponding to sample 0. If absent, the base counter value is taken to be 0.
base_time : str, optional
This field can be present only if the number of samples is also present. It gives the time of day that corresponds to the beginning of the record, in ‘HH:MM:SS’ format (using a 24-hour clock; thus ‘13:05:00’, or ‘13:5:0’, represent 1:05 pm). If this field is absent, the time-conversion functions assume a value of ‘0:0:0’, corresponding to midnight.
base_date : str, optional
This field can be present only if the base time is also present. It contains the date that corresponds to the beginning of the record, in ‘DD/MM/YYYY’ format (e.g., ‘25/4/1989’ is ‘25 April 1989’).
comments : list, optional
A list of string comments to be written to the header file. Each string entry represents a new line to be appended to the bottom of the header file (‘.hea’).
sig_name : list, optional
A list of strings giving the signal name of each signal channel. This will be used for plotting the signal both in this package and LightWave. Note, this value will be used in preference to the CSV header, if applicable, to define custom signal names.
dat_file_name : str, optional
The name of the file in which samples of the signal are kept. Although the record name is usually part of the signal file name, this convention is not a requirement. Note that several signals can share the same file (i.e., they can belong to the same signal group); all entries for signals that share a given file must be consecutive, however. Note, the default behavior is to save the files in the current working directory, not the directory of the file being read.
skew : list, int, optional
This will be applied as the passed list unless a single int is passed instead - in which case the int will be assigned for all channels. Ideally, within a given record, samples of different signals with the same sample number are simultaneous (within one sampling interval). If this is not the case (as, for example, when a multitrack analog tape recording is digitized and the azimuth of the playback head does not match that of the recording head), the skew between signals can sometimes be determined (for example, by locating recorded waveform features with known time relationships, such as calibration signals). If this has been done, the skew field may be inserted into the header file to indicate the (positive) number of samples of the signal that are considered to precede sample 0. These samples, if any, are included in the checksum. (Note the checksum need not be changed if the skew field is inserted or modified).
byte_offset : list, int, optional
This will be applied as the passed list unless a single int is passed instead - in which case the int will be assigned for all channels. Normally, signal files include only sample data. If a signal file includes a preamble, however, this field specifies the offset in bytes from the beginning of the signal file to sample 0 (i.e., the length of the preamble). Data within the preamble is not included in the signal checksum. Note that the byte offset must be the same for all signals within a given group (use the skew field to correct for intersignal skew). This feature is provided only to simplify the task of reading signal files not generated using the WFDB library; the WFDB library does not support any means of writing such files, and byte offsets must be inserted into header files manually.
adc_res: list, int, optional
This will be applied as the passed list unless a single int is passed instead - in which case the int will be assigned for all channels. This field can be present only if the ADC gain is also present. It specifies the resolution of the analog-to-digital converter used to digitize the signal. Typical ADCs have resolutions between 8 and 16 bits. If this field is missing or zero, the default value is 12 bits for amplitude-format signals, or 10 bits for difference-format signals (unless a lower value is specified by the format field).
adc_zero: list, int, optional
This will be applied as the passed list unless a single int is passed instead - in which case the int will be assigned for all channels. This field can be present only if the ADC resolution is also present. It is an integer that represents the amplitude (sample value) that would be observed if the analog signal present at the ADC inputs had a level that fell exactly in the middle of the input range of the ADC. For a bipolar ADC, this value is usually zero, but a unipolar (offset binary) ADC usually produces a non-zero value in the middle of its range. Together with the ADC resolution, the contents of this field can be used to determine the range of possible sample values. If this field is missing, a value of 0 is assumed.
init_value : list, int, optional
This will be applied as the passed list unless a single int is passed instead - in which case the int will be assigned for all channels. This field can be present only if the ADC zero is also present. It specifies the value of sample 0 in the signal, but is used only if the signal is stored in difference format. If this field is missing, a value equal to the ADC zero is assumed.
checksum : list, optional
This field can be present only if the initial value is also present. It is a 16-bit signed checksum of all samples in the signal. (Thus the checksum is independent of the storage format.) If the entire record is read without skipping samples, and the header’s record line specifies the correct number of samples per signal, this field is compared against a computed checksum to verify that the signal file has not been corrupted. A value of zero may be used as a field placeholder if the number of samples is unspecified.
block_size : list, int, optional
This will be applied as the passed list unless a single int is passed instead - in which case the int will be assigned for all channels. This field can be present only if the checksum is present. This field is an integer and is usually 0. If the signal is stored in a file that must be read in blocks of a specific size, however, this field specifies the block size in bytes. (On UNIX systems, this is the case only for character special files, corresponding to certain tape and raw disk files. If necessary, the block size may be given as a negative number to indicate that the associated file lacks I/O driver support for some operations.) All signals belonging to the same signal group have the same block size.
record_only : bool, optional
Whether to only return the record information (True) or not (False). If false, this function will generate both a .dat and .hea file.
header : bool, optional
Whether to assume the CSV has a first line header (True) or not (False) which defines the signal names. If false, this function will generate either the signal names provided by sig_name or set [ch_1, ch_2, …] as the default.
delimiter : str, optional
What to use as the delimiter for the file to separate data. The default if a comma (‘,’). Other common delimiters are tabs (’ ‘), spaces (’ ‘), pipes (‘|’), and colons (‘:’).
verbose : bool, optional
Whether to print all the information read about the file (True) or not (False).
record : Record or MultiRecord, optional
The WFDB Record or MultiRecord object representing the contents of the CSV file read.

CSVs should be in the following format:

sig_1_name,sig_2_name,… sig_1_val_1,sig_2_val_1,… sig_1_val_2,sig_2_val_2,… …,…,…

Or this format if header=False is defined:

sig_1_val_1,sig_2_val_1,… sig_1_val_2,sig_2_val_2,… …,…,…

The signal will be saved defaultly as a p_signal so both floats and ints are acceptable.

Create the header (‘.hea’) and record (‘.dat’) files, specifies both units to be ‘mV’ >>> wfdb.csv2mit(‘sample-data/100.csv’, fs=360, units=’mV’)

Create the header (‘.hea’) and record (‘.dat’) files, change units for each signal >>> wfdb.csv2mit(‘sample-data/100.csv’, fs=360, units=[‘mV’,’kV’])

Return just the record, note the use of lists to specify which values should be applied to each signal >>> csv_record = wfdb.csv2mit(‘sample-data/100.csv’, fs=360, units=[‘mV’,’mV’],

fmt=[‘80’,212’], adc_gain=[100,200], baseline=[1024,512], record_only=True)

Return just the record, note the use of single strings and ints to specify when fields can be applied to all signals >>> csv_record = wfdb.csv2mit(‘sample-data/100.csv’, fs=360, units=’mV’,

fmt=[‘80’,’212’], adc_gain=200, baseline=1024, record_only=True)
class wfdb.Record(p_signal=None, d_signal=None, e_p_signal=None, e_d_signal=None, record_name=None, n_sig=None, fs=None, counter_freq=None, base_counter=None, sig_len=None, base_time=None, base_date=None, file_name=None, fmt=None, samps_per_frame=None, skew=None, byte_offset=None, adc_gain=None, baseline=None, units=None, adc_res=None, adc_zero=None, init_value=None, checksum=None, block_size=None, sig_name=None, comments=None)

The class representing single segment WFDB records.

Record objects can be created using the initializer, by reading a WFDB header with rdheader, or a WFDB record (header and associated dat files) with rdrecord.

The attributes of the Record object give information about the record as specified by: https://www.physionet.org/physiotools/wag/header-5.htm

In addition, the d_signal and p_signal attributes store the digital and physical signals of WFDB records with at least one channel.

p_signal : ndarray, optional
An (MxN) 2d numpy array, where M is the signal length. Gives the physical signal values intended to be written. Either p_signal or d_signal must be set, but not both. If p_signal is set, this method will use it to perform analogue-digital conversion, writing the resultant digital values to the dat file(s). If fmt is set, gain and baseline must be set or unset together. If fmt is unset, gain and baseline must both be unset.
d_signal : ndarray, optional
An (MxN) 2d numpy array, where M is the signal length. Gives the digital signal values intended to be directly written to the dat file(s). The dtype must be an integer type. Either p_signal or d_signal must be set, but not both. In addition, if d_signal is set, fmt, gain and baseline must also all be set.
e_p_signal : ndarray, optional
The expanded physical conversion of the signal. Either a 2d numpy array or a list of 1d numpy arrays.
e_d_signal : ndarray, optional
The expanded digital conversion of the signal. Either a 2d numpy array or a list of 1d numpy arrays.
record_name : str, optional
The name of the WFDB record to be read, without any file extensions. If the argument contains any path delimiter characters, the argument will be interpreted as PATH/BASE_RECORD. Both relative and absolute paths are accepted. If the pn_dir parameter is set, this parameter should contain just the base record name, and the files fill be searched for remotely. Otherwise, the data files will be searched for in the local path.
n_sig : int, optional
Total number of signals.
fs : float, optional
The sampling frequency of the record.
counter_freq : float, optional
The frequency used to start counting.
base_counter : float, optional
The counter used at the start of the file.
sig_len : int, optional
The total length of the signal.
base_time : str, optional
A string of the record’s start time in 24h ‘HH:MM:SS(.ms)’ format.
base_date : str, optional
A string of the record’s start date in ‘DD/MM/YYYY’ format.
file_name : str, optional
The name of the file used for analysis.
fmt : list, optional
A list of strings giving the WFDB format of each file used to store each channel. Accepted formats are: ‘80’,’212’,’16’,’24’, and ‘32’. There are other WFDB formats as specified by: https://www.physionet.org/physiotools/wag/signal-5.htm but this library will not write (though it will read) those file types.
samps_per_frame : int, optional
The total number of samples per frame.
skew : float, optional
The offset used to allign signals.
byte_offset : int, optional
The byte offset used to allign signals.
adc_gain : list, optional
A list of numbers specifying the ADC gain.
baseline : list, optional
A list of integers specifying the digital baseline.
units : list, optional
A list of strings giving the units of each signal channel.
adc_res: int, optional
The value produced by the ADC given a given Volt input.
adc_zero: int, optional
The value produced by the ADC given a 0 Volt input.
init_value : list, optional
The initial value of the signal.
checksum : list, int, optional
The checksum of the signal.
block_size : str, optional
The dimensions of the field data.
sig_name : list, optional
A list of strings giving the signal name of each signal channel.
comments : list, optional
A list of string comments to be written to the header file.
>>> record = wfdb.Record(record_name='r1', fs=250, n_sig=2, sig_len=1000,
                     file_name=['r1.dat','r1.dat'])
adc(expanded=False, inplace=False)

Performs analogue to digital conversion of the physical signal stored in p_signal if expanded is False, or e_p_signal if expanded is True.

The p_signal/e_p_signal, fmt, gain, and baseline fields must all be valid.

If inplace is True, the adc will be performed inplace on the variable, the d_signal/e_d_signal attribute will be set, and the p_signal/e_p_signal field will be set to None.

expanded : bool, optional
Whether to transform the e_p_signal attribute (True) or the p_signal attribute (False).
inplace : bool, optional
Whether to automatically set the object’s corresponding digital signal attribute and set the physical signal attribute to None (True), or to return the converted signal as a separate variable without changing the original physical signal attribute (False).
d_signal : ndarray, optional
The digital conversion of the signal. Either a 2d numpy array or a list of 1d numpy arrays.
>>> import wfdb
>>> record = wfdb.rdsamp('sample-data/100')
>>> d_signal = record.adc()
>>> record.adc(inplace=True)
>>> record.dac(inplace=True)
dac(expanded=False, return_res=64, inplace=False)

Performs the digital to analogue conversion of the signal stored in d_signal if expanded is False, or e_d_signal if expanded is True.

The d_signal/e_d_signal, fmt, gain, and baseline fields must all be valid.

If inplace is True, the dac will be performed inplace on the variable, the p_signal/e_p_signal attribute will be set, and the d_signal/e_d_signal field will be set to None.

expanded : bool, optional
Whether to transform the e_d_signal attribute (True) or the d_signal attribute (False).
return_res : int, optional
The numpy array dtype of the returned signals. Options are: 64, 32, 16, and 8, where the value represents the numpy int or float dtype. Note that the value cannot be 8 when physical is True since there is no float8 format.
inplace : bool, optional
Whether to automatically set the object’s corresponding physical signal attribute and set the digital signal attribute to None (True), or to return the converted signal as a separate variable without changing the original digital signal attribute (False).
p_signal : ndarray, optional
The physical conversion of the signal. Either a 2d numpy array or a list of 1d numpy arrays.
>>> import wfdb
>>> record = wfdb.rdsamp('sample-data/100', physical=False)
>>> p_signal = record.dac()
>>> record.dac(inplace=True)
>>> record.adc(inplace=True)
wrsamp(expanded=False, write_dir='')

Write a WFDB header file and any associated dat files from this object.

expanded : bool, optional
Whether to write the expanded signal (e_d_signal) instead of the uniform signal (d_signal).
write_dir : str, optional
The directory in which to write the files.

N/A

class wfdb.MultiRecord(segments=None, layout=None, record_name=None, n_sig=None, fs=None, counter_freq=None, base_counter=None, sig_len=None, base_time=None, base_date=None, seg_name=None, seg_len=None, comments=None, sig_name=None, sig_segments=None)

The class representing multi-segment WFDB records.

MultiRecord objects can be created using the initializer, or by reading a multi-segment WFDB record using ‘rdrecord’ with the m2s (multi to single) input parameter set to False.

The attributes of the MultiRecord object give information about the entire record as specified by: https://www.physionet.org/physiotools/wag/header-5.htm

In addition, the segments parameter is a list of Record objects representing each individual segment, or None representing empty segments, of the entire multi-segment record.

Notably, this class has no attribute representing the signals as a whole. The ‘multi_to_single’ instance method can be called on MultiRecord objects to return a single segment representation of the record as a Record object. The resulting Record object will have its ‘p_signal’ field set.

segments : list, optional
The segments to be read.
layout : str, optional
Whether the record will be ‘fixed’ or ‘variable’.
record_name : str, optional
The name of the WFDB record to be read, without any file extensions. If the argument contains any path delimiter characters, the argument will be interpreted as PATH/BASE_RECORD. Both relative and absolute paths are accepted. If the pn_dir parameter is set, this parameter should contain just the base record name, and the files fill be searched for remotely. Otherwise, the data files will be searched for in the local path.
n_sig : int, optional
Total number of signals.
fs : int, float, optional
The sampling frequency of the record.
counter_freq : float, optional
The frequency used to start counting.
base_counter : float, optional
The counter used at the start of the file.
sig_len : int, optional
The total length of the signal.
base_time : str, optional
A string of the record’s start time in 24h ‘HH:MM:SS(.ms)’ format.
base_date : str, optional
A string of the record’s start date in ‘DD/MM/YYYY’ format.
seg_name : str, optional
The name of the segment.
seg_len : int, optional
The length of the segment.
comments : list, optional
A list of string comments to be written to the header file.
sig_name : str, optional
A list of strings giving the signal name of each signal channel.
sig_segments : list, optional
The signal segments to be read.
>>> record_m = wfdb.MultiRecord(record_name='rm', fs=50, n_sig=8,
                                sig_len=9999, seg_name=['rm_1', '~', rm_2'],
                                seg_len=[800, 200, 900])
>>> # Get a MultiRecord object
>>> record_s = wfdb.rdsamp('s00001-2896-10-10-00-31', m2s=False)
>>> # Turn it into a single record
>>> record_s = record_s.multi_to_single()

record_s initially stores a MultiRecord object, and is then converted into a Record object.

multi_to_single(physical, return_res=64)

Create a Record object from the MultiRecord object. All signal segments will be combined into the new object’s p_signal or d_signal field. For digital format, the signals must have the same storage format, baseline, and adc_gain in all segments.

physical : bool
Whether to convert the physical or digital signal.
return_res : int, optional
The return resolution of the p_signal field. Options are: 64, 32, and 16.
record : WFDB Record
The single segment record created.

WFDB Anotations

wfdb.rdann(record_name, extension, sampfrom=0, sampto=None, shift_samps=False, pn_dir=None, return_label_elements=['symbol'], summarize_labels=False)

Read a WFDB annotation file record_name.extension and return an Annotation object.

record_name : str
The record name of the WFDB annotation file. ie. for file ‘100.atr’, record_name=’100’.
extension : str
The annotatator extension of the annotation file. ie. for file ‘100.atr’, extension=’atr’.
sampfrom : int, optional
The minimum sample number for annotations to be returned.
sampto : int, optional
The maximum sample number for annotations to be returned.
shift_samps : bool, optional
Specifies whether to return the sample indices relative to sampfrom (True), or sample 0 (False).
pn_dir : str, optional
Option used to stream data from Physionet. The PhysioNet database directory from which to find the required annotation file. eg. For record ‘100’ in ‘http://physionet.org/content/mitdb’: pn_dir=’mitdb’.
return_label_elements : list, optional
The label elements that are to be returned from reading the annotation file. A list with at least one of the following options: ‘symbol’, ‘label_store’, ‘description’.
summarize_labels : bool, optional
If True, assign a summary table of the set of annotation labels contained in the file to the ‘contained_labels’ attribute of the returned object. This table will contain the columns: [‘label_store’, ‘symbol’, ‘description’, ‘n_occurrences’].
annotation : Annotation
The Annotation object. Call help(wfdb.Annotation) for the attribute descriptions.

For every annotation sample, the annotation file explictly stores the ‘sample’ and ‘symbol’ fields, but not necessarily the others. When reading annotation files using this function, fields which are not stored in the file will either take their default values of 0 or None, or will be carried over from their previous values if any.

>>> ann = wfdb.rdann('sample-data/100', 'atr', sampto=300000)
wfdb.wrann(record_name, extension, sample, symbol=None, subtype=None, chan=None, num=None, aux_note=None, label_store=None, fs=None, custom_labels=None, write_dir='')

Write a WFDB annotation file.

Specify at least the following:

  • The record name of the WFDB record (record_name).
  • The annotation file extension (extension).
  • The annotation locations in samples relative to the beginning of the record (sample).
  • Either the numerical values used to store the labels. (label_store), or more commonly, the display symbols of each label (symbol).
record_name : str
The string name of the WFDB record to be written (without any file extensions).
extension : str
The string annotation file extension.
sample : ndarray
A numpy array containing the annotation locations in samples relative to the beginning of the record.
symbol : list, numpy array, optional
The symbols used to display the annotation labels. List or numpy array. If this field is present, label_store must not be present.
subtype : ndarray, optional
A numpy array containing the marked class/category of each annotation.
chan : ndarray, optional
A numpy array containing the signal channel associated with each annotation.
num : ndarray, optional
A numpy array containing the labelled annotation number for each annotation.
aux_note : list, optional
A list containing the auxiliary information string (or None for annotations without notes) for each annotation.
label_store : ndarray, optional
A numpy array containing the integer values used to store the annotation labels. If this field is present, symbol must not be present.
fs : int, float, optional
The numerical sampling frequency of the record to be written to the file.
custom_labels : pandas dataframe, optional

The map of custom defined annotation labels used for this annotation, in addition to the standard WFDB annotation labels. Custom labels are defined by two or three fields:

  • The integer values used to store custom annotation labels in the file (optional).
  • Their short display symbols
  • Their long descriptions.

This input argument may come in four formats:

  1. A pandas.DataFrame object with columns: [‘label_store’, ‘symbol’, ‘description’]
  2. A pandas.DataFrame object with columns: [‘symbol’, ‘description’] If this option is chosen, label_store values are automatically chosen.
  3. A list or tuple of tuple triplets, with triplet elements representing: (label_store, symbol, description).
  4. A list or tuple of tuple pairs, with pair elements representing: (symbol, description). If this option is chosen, label_store values are automatically chosen.

If the label_store field is given for this function, and custom_labels is defined, custom_labels must contain label_store in its mapping. ie. it must come in format 1 or 3 above.

write_dir : str, optional
The directory in which to write the annotation file

N/A

This is a gateway function, written as a simple way to write WFDB annotation files without needing to explicity create an Annotation object. You may also create an Annotation object, manually set its attributes, and call its wrann instance method.

Each annotation stored in a WFDB annotation file contains a sample field and a label field. All other fields may or may not be present.

>>> # Read an annotation as an Annotation object
>>> annotation = wfdb.rdann('b001', 'atr', pn_dir='cebsdb')
>>> # Write a copy of the annotation file
>>> wfdb.wrann('b001', 'cpy', annotation.sample, annotation.symbol)
wfdb.show_ann_labels()

Display the standard WFDB annotation label mapping.

N/A

N/A

>>> show_ann_labels()
wfdb.show_ann_classes()

Display the standard WFDB annotation classes.

N/A

N/A

>>> show_ann_classes()
class wfdb.Annotation(record_name, extension, sample, symbol=None, subtype=None, chan=None, num=None, aux_note=None, fs=None, label_store=None, description=None, custom_labels=None, contained_labels=None)

The class representing WFDB annotations.

Annotation objects can be created using the initializer, or by reading a WFDB annotation file with rdann.

The attributes of the Annotation object give information about the annotation as specified by: https://www.physionet.org/physiotools/wag/annot-5.htm

Call show_ann_labels() to see the list of standard annotation codes. Any text used to label annotations that are not one of these codes should go in the ‘aux_note’ field rather than the ‘sym’ field.

The current annotation values organized as such:

AnnotationLabel(label_store (or subtype), symbol (or aux_note), short_description, description)

where the associated values are:

ann_labels = [
AnnotationLabel(0, ” “, ‘NOTANN’, ‘Not an actual annotation’), AnnotationLabel(1, “N”, ‘NORMAL’, ‘Normal beat’), AnnotationLabel(2, “L”, ‘LBBB’, ‘Left bundle branch block beat’), AnnotationLabel(3, “R”, ‘RBBB’, ‘Right bundle branch block beat’), AnnotationLabel(4, “a”, ‘ABERR’, ‘Aberrated atrial premature beat’), AnnotationLabel(5, “V”, ‘PVC’, ‘Premature ventricular contraction’), AnnotationLabel(6, “F”, ‘FUSION’, ‘Fusion of ventricular and normal beat’), AnnotationLabel(7, “J”, ‘NPC’, ‘Nodal (junctional) premature beat’), AnnotationLabel(8, “A”, ‘APC’, ‘Atrial premature contraction’), AnnotationLabel(9, “S”, ‘SVPB’, ‘Premature or ectopic supraventricular beat’), AnnotationLabel(10, “E”, ‘VESC’, ‘Ventricular escape beat’), AnnotationLabel(11, “j”, ‘NESC’, ‘Nodal (junctional) escape beat’), AnnotationLabel(12, “/”, ‘PACE’, ‘Paced beat’), AnnotationLabel(13, “Q”, ‘UNKNOWN’, ‘Unclassifiable beat’), AnnotationLabel(14, “~”, ‘NOISE’, ‘Signal quality change’), AnnotationLabel(16, “|”, ‘ARFCT’, ‘Isolated QRS-like artifact’), AnnotationLabel(18, “s”, ‘STCH’, ‘ST change’), AnnotationLabel(19, “T”, ‘TCH’, ‘T-wave change’), AnnotationLabel(20, “*”, ‘SYSTOLE’, ‘Systole’), AnnotationLabel(21, “D”, ‘DIASTOLE’, ‘Diastole’), AnnotationLabel(22, ‘”’, ‘NOTE’, ‘Comment annotation’), AnnotationLabel(23, “=”, ‘MEASURE’, ‘Measurement annotation’), AnnotationLabel(24, “p”, ‘PWAVE’, ‘P-wave peak’), AnnotationLabel(25, “B”, ‘BBB’, ‘Left or right bundle branch block’), AnnotationLabel(26, “^”, ‘PACESP’, ‘Non-conducted pacer spike’), AnnotationLabel(27, “t”, ‘TWAVE’, ‘T-wave peak’), AnnotationLabel(28, “+”, ‘RHYTHM’, ‘Rhythm change’), AnnotationLabel(29, “u”, ‘UWAVE’, ‘U-wave peak’), AnnotationLabel(30, “?”, ‘LEARN’, ‘Learning’), AnnotationLabel(31, “!”, ‘FLWAV’, ‘Ventricular flutter wave’), AnnotationLabel(32, “[”, ‘VFON’, ‘Start of ventricular flutter/fibrillation’), AnnotationLabel(33, “]”, ‘VFOFF’, ‘End of ventricular flutter/fibrillation’), AnnotationLabel(34, “e”, ‘AESC’, ‘Atrial escape beat’), AnnotationLabel(35, “n”, ‘SVESC’, ‘Supraventricular escape beat’), AnnotationLabel(36, “@”, ‘LINK’, ‘Link to external data (aux_note contains URL)’), AnnotationLabel(37, “x”, ‘NAPC’, ‘Non-conducted P-wave (blocked APB)’), AnnotationLabel(38, “f”, ‘PFUS’, ‘Fusion of paced and normal beat’), AnnotationLabel(39, “(”, ‘WFON’, ‘Waveform onset’), AnnotationLabel(40, “)”, ‘WFOFF’, ‘Waveform end’), AnnotationLabel(41, “r”, ‘RONT’, ‘R-on-T premature ventricular contraction’)

]

The current annotation classes are organized as such:

AnnotationClass(extension, description, human_reviewed)

where the associated values are:

ann_classes = [
AnnotationClass(‘atr’, ‘Reference ECG annotations’, True), AnnotationClass(‘blh’, ‘Human reviewed beat labels’, True), AnnotationClass(‘blm’, ‘Machine beat labels’, False), AnnotationClass(‘alh’, ‘Human reviewed alarms’, True), AnnotationClass(‘alm’, ‘Machine alarms’, False), AnnotationClass(‘qrsc’, ‘Human reviewed QRS detections’, True), AnnotationClass(‘qrs’, ‘Machine QRS detections’, False), AnnotationClass(‘bph’, ‘Human reviewed BP beat detections’, True), AnnotationClass(‘bpm’, ‘Machine BP beat detections’, False)

]

record_name : str
The base file name (without extension) of the record that the annotation is associated with.
extension : str
The file extension of the file the annotation is stored in.
sample : ndarray
A numpy array containing the annotation locations in samples relative to the beginning of the record.
symbol : list, numpy array, optional
The symbols used to display the annotation labels. List or numpy array. If this field is present, label_store must not be present.
subtype : ndarray, optional
A numpy array containing the marked class/category of each annotation.
chan : ndarray, optional
A numpy array containing the signal channel associated with each annotation.
num : ndarray, optional
A numpy array containing the labelled annotation number for each annotation.
aux_note : list, optional
A list containing the auxiliary information string (or None for annotations without notes) for each annotation.
fs : int, float, optional
The sampling frequency of the record.
label_store : ndarray, optional
The integer value used to store/encode each annotation label
description : list, optional
A list containing the descriptive string of each annotation label.
custom_labels : pandas dataframe, optional
The custom annotation labels defined in the annotation file. Maps the relationship between the three label fields. The data type is a pandas DataFrame with three columns: [‘label_store’, ‘symbol’, ‘description’].
contained_labels : pandas dataframe, optional
The unique labels contained in this annotation. Same structure as custom_labels.
>>> ann1 = wfdb.Annotation(record_name='rec1', extension='atr',
                           sample=[10,20,400], symbol=['N','N','['],
                           aux_note=[None, None, 'Serious Vfib'])
wrann(write_fs=False, write_dir='')

Write a WFDB annotation file from this object.

write_fs : bool, optional
Whether to write the fs attribute to the file.
write_dir : str, optional
The output directory in which the header is written.

N/A

Downloading

wfdb.get_dbs()

Get a list of all the PhysioNet databases available.

N/A

dbs : list
All of the databases currently available for analysis.
>>> dbs = wfdb.get_dbs()
>>> dbs
[
 ['aami-ec13', 'ANSI/AAMI EC13 Test Waveforms'],
 ['adfecgdb', 'Abdominal and Direct Fetal ECG Database'],
 ...
 ['wrist', 'Wrist PPG During Exercise']
]
wfdb.get_record_list(db_dir, records='all')

Get a list of records belonging to a database.

db_dir : str
The database directory, usually the same as the database slug. The location to look for a RECORDS file.
records : list, optional
An option used when this function acts as a helper function. Leave as default ‘all’ to get all records.
record_list : list
All of the possible record names for the input database.
>>> wfdb.get_record_list('mitdb')
wfdb.dl_database(db_dir, dl_dir, records='all', annotators='all', keep_subdirs=True, overwrite=False)

Download WFDB record (and optionally annotation) files from a PhysioNet database. The database must contain a ‘RECORDS’ file in its base directory which lists its WFDB records.

db_dir : str
The PhysioNet database directory to download. eg. For database: ‘http://physionet.org/content/mitdb/’, db_dir=’mitdb’.
dl_dir : str
The full local directory path in which to download the files.
records : list, ‘all’, optional
A list of strings specifying the WFDB records to download. Leave as ‘all’ to download all records listed in the database’s RECORDS file. eg. records=[‘test01_00s’, test02_45s] for database: https://physionet.org/content/macecgdb/
annotators : list, ‘all’, None, optional
A list of strings specifying the WFDB annotation file types to download along with the record files. Is either None to skip downloading any annotations, ‘all’ to download all annotation types as specified by the ANNOTATORS file, or a list of strings which each specify an annotation extension. eg. annotators = [‘anI’] for database: https://physionet.org/content/prcp/
keep_subdirs : bool, optional
Whether to keep the relative subdirectories of downloaded files as they are organized in PhysioNet (True), or to download all files into the same base directory (False).
overwrite : bool, optional
If True, all files will be redownloaded regardless. If False, existing files with the same name and relative subdirectory will be checked. If the local file is the same size as the online file, the download is skipped. If the local file is larger, it will be deleted and the file will be redownloaded. If the local file is smaller, the file will be assumed to be partially downloaded and the remaining bytes will be downloaded and appended.

N/A

>>> wfdb.dl_database('ahadb', os.getcwd())
wfdb.dl_files(db, dl_dir, files, keep_subdirs=True, overwrite=False)

Download specified files from a PhysioNet database.

db : str
The PhysioNet database directory to download. eg. For database: ‘http://physionet.org/content/mitdb’, db=’mitdb’.
dl_dir : str
The full local directory path in which to download the files.
files : list
A list of strings specifying the file names to download relative to the database base directory.
keep_subdirs : bool, optional
Whether to keep the relative subdirectories of downloaded files as they are organized in PhysioNet (True), or to download all files into the same base directory (False).
overwrite : bool, optional
If True, all files will be redownloaded regardless. If False, existing files with the same name and relative subdirectory will be checked. If the local file is the same size as the online file, the download is skipped. If the local file is larger, it will be deleted and the file will be redownloaded. If the local file is smaller, the file will be assumed to be partially downloaded and the remaining bytes will be downloaded and appended.

N/A

>>> wfdb.dl_files('ahadb', os.getcwd(),
                  ['STAFF-Studies-bibliography-2016.pdf', 'data/001a.hea',
                  'data/001a.dat'])
wfdb.set_db_index_url(db_index_url='https://physionet.org/files/')

Set the database index url to a custom value, to stream remote files from another location.

db_index_url : str, optional
The desired new database index url. Leave as default to reset to the PhysioNet index url.

N/A

Plotting

wfdb.plot_items(signal=None, ann_samp=None, ann_sym=None, fs=None, time_units='samples', sig_name=None, sig_units=None, xlabel=None, ylabel=None, title=None, sig_style=[''], ann_style=['r*'], ecg_grids=[], figsize=None, sharex=False, sharey=False, return_fig=False, return_fig_axes=False)

Subplot individual channels of signals and/or annotations.

signal : 1d or 2d numpy array, optional
The uniformly sampled signal to be plotted. If signal.ndim is 1, it is assumed to be a one channel signal. If it is 2, axes 0 and 1, must represent time and channel number respectively.
ann_samp: list, optional

A list of annotation locations to plot, with each list item corresponding to a different channel. List items may be:

  • 1d numpy array, with values representing sample indices. Empty arrays are skipped.
  • list, with values representing sample indices. Empty lists are skipped.
  • None. For channels in which nothing is to be plotted.

If signal is defined, the annotation locations will be overlaid on the signals, with the list index corresponding to the signal channel. The length of annotation does not have to match the number of channels of signal.

ann_sym: list, optional
A list of annotation symbols to plot, with each list item corresponding to a different channel. List items should be lists of strings. The symbols are plotted over the corresponding ann_samp index locations.
fs : int, float, optional
The sampling frequency of the signals and/or annotations. Used to calculate time intervals if time_units is not ‘samples’. Also required for plotting ECG grids.
time_units : str, optional
The x axis unit. Allowed options are: ‘samples’, ‘seconds’, ‘minutes’, and ‘hours’.
sig_name : list, optional
A list of strings specifying the signal names. Used with sig_units to form y labels, if ylabel is not set.
sig_units : list, optional
A list of strings specifying the units of each signal channel. Used with sig_name to form y labels, if ylabel is not set. This parameter is required for plotting ECG grids.
xlabel : list, optional
A list of strings specifying the final x labels to be used. If this option is present, no ‘time/’time_units is used.
ylabel : list, optional
A list of strings specifying the final y labels. If this option is present, sig_name and sig_units will not be used for labels.
title : str, optional
The title of the graph.
sig_style : list, optional
A list of strings, specifying the style of the matplotlib plot for each signal channel. The list length should match the number of signal channels. If the list has a length of 1, the style will be used for all channels.
ann_style : list, optional
A list of strings, specifying the style of the matplotlib plot for each annotation channel. If the list has a length of 1, the style will be used for all channels.
ecg_grids : list, optional
A list of integers specifying channels in which to plot ECG grids. May also be set to ‘all’ for all channels. Major grids at 0.5mV, and minor grids at 0.125mV. All channels to be plotted with grids must have sig_units equal to ‘uV’, ‘mV’, or ‘V’.
sharex, sharey : bool, optional
Controls sharing of properties among x (sharex) or y (sharey) axes. If True: x- or y-axis will be shared among all subplots. If False, each subplot x- or y-axis will be independent.
figsize : tuple, optional
Tuple pair specifying the width, and height of the figure. It is the ‘figsize’ argument passed into matplotlib.pyplot’s figure function.
return_fig : bool, optional
Whether the figure is to be returned as an output argument.
fig : matplotlib figure, optional
The matplotlib figure generated. Only returned if the ‘return_fig’ or ‘return_fig_axes’ parameter is set to True.
axes : matplotlib axes, optional
The matplotlib axes generated. Only returned if the ‘return_fig_axes’ parameter is set to True.
>>> record = wfdb.rdrecord('sample-data/100', sampto=3000)
>>> ann = wfdb.rdann('sample-data/100', 'atr', sampto=3000)
>>> wfdb.plot_items(signal=record.p_signal,
                    ann_samp=[ann.sample, ann.sample],
                    title='MIT-BIH Record 100', time_units='seconds',
                    figsize=(10,4), ecg_grids='all')
wfdb.plot_wfdb(record=None, annotation=None, plot_sym=False, time_units='samples', title=None, sig_style=[''], ann_style=['r*'], ecg_grids=[], figsize=None, return_fig=False)

Subplot individual channels of a WFDB record and/or annotation.

This function implements the base functionality of the plot_items function, while allowing direct input of WFDB objects.

If the record object is input, the function will extract from it:
  • signal values, from the p_signal (priority) or d_signal attribute
  • sampling frequency, from the fs attribute
  • signal names, from the sig_name attribute
  • signal units, from the units attribute
If the annotation object is input, the function will extract from it:
  • sample locations, from the sample attribute
  • symbols, from the symbol attribute
  • the annotation channels, from the chan attribute
  • the sampling frequency, from the fs attribute if present, and if fs was not already extracted from the record argument.
record : WFDB Record, optional
The Record object to be plotted.
annotation : WFDB Annotation, optional
The Annotation object to be plotted.
plot_sym : bool, optional
Whether to plot the annotation symbols on the graph.
time_units : str, optional
The x axis unit. Allowed options are: ‘samples’, ‘seconds’, ‘minutes’, and ‘hours’.
title : str, optional
The title of the graph.
sig_style : list, optional
A list of strings, specifying the style of the matplotlib plot for each signal channel. The list length should match the number of signal channels. If the list has a length of 1, the style will be used for all channels.
ann_style : list, optional
A list of strings, specifying the style of the matplotlib plot for each annotation channel. The list length should match the number of annotation channels. If the list has a length of 1, the style will be used for all channels.
ecg_grids : list, optional
A list of integers specifying channels in which to plot ECG grids. May also be set to ‘all’ for all channels. Major grids at 0.5mV, and minor grids at 0.125mV. All channels to be plotted with grids must have sig_units equal to ‘uV’, ‘mV’, or ‘V’.
figsize : tuple, optional
Tuple pair specifying the width, and height of the figure. It is the ‘figsize’ argument passed into matplotlib.pyplot’s figure function.
return_fig : bool, optional
Whether the figure is to be returned as an output argument.
figure : matplotlib figure, optional
The matplotlib figure generated. Only returned if the ‘return_fig’ option is set to True.
>>> record = wfdb.rdrecord('sample-data/100', sampto=3000)
>>> annotation = wfdb.rdann('sample-data/100', 'atr', sampto=3000)
>>> wfdb.plot_wfdb(record=record, annotation=annotation, plot_sym=True
                   time_units='seconds', title='MIT-BIH Record 100',
                   figsize=(10,4), ecg_grids='all')
wfdb.plot_all_records(directory='')

Plot all WFDB records in a directory (by finding header files), one at a time, until the ‘enter’ key is pressed.

directory : str, optional
The directory in which to search for WFDB records. Defaults to current working directory.

N/A