Read, write, and analyze physiologic waveforms and annotations in the WFDB format. WFDB-Python works with local files, records published on PhysioNet, and supported cloud storage.
WFDB-Python requires Python 3.11 or newer:
pip install wfdbWith uv, use uv add wfdb. On some systems,
soundfile also needs a separate installation of libsndfile; see its
installation notes.
Read ten seconds of ECG from the MIT-BIH Arrhythmia Database directly from PhysioNet:
import wfdb
record = wfdb.read_record("physionet://mitdb/1.0.0/100", layout="uniform", stop=3600)
print(record.signals.values.shape) # (3600, 2): frames x channels
print(record.metadata.fs) # 360 frames per second
print([channel.name for channel in record.metadata.signals]) # ['MLII', 'V5']read_record returns RecordData: editable signal arrays, fixed metadata,
and information about the source. layout="uniform" gives one value per
channel per frame; layout="expanded" retains every sample when a channel
has several samples per frame. Physical values are the default. For digital
integers and a mask of missing samples, pass kind="digital".
For local files, pass a record stem or a .hea path instead of a URI:
record = wfdb.read_record("path/to/record", layout="uniform")| Task | API |
|---|---|
| Read a selected, combined record | read_record |
| Inspect a header without loading samples | read_header |
| Keep a multi-segment record's boundaries | read_segments |
| Write a multi-segment record with its boundaries | write_segments |
| Read an annotation file | read_annotations |
| Plot signals and annotations | record.plot(annotation=...) |
| Write a loaded record or an array | write_record or write_signals |
| Check files before sharing | wfdb check |
Read beat annotations from the same record:
annotations = wfdb.read_annotations("physionet://mitdb/1.0.0/100.atr", stop=3600)
print(annotations.sample_indices[:5])
print(annotations.symbols[:5])
record.plot(
annotation=annotations,
plot_sym=True,
title="MIT-BIH record 100",
)For a local annotation, pass its filename, such as
"path/to/record.atr". plot_wfdb also accepts RecordData directly, while
continuing to support legacy Record objects.
For a multi-segment record, plot the original pieces and gaps together:
segments = wfdb.read_segments("path/to/record", layout="uniform")
segments.plot() # one figure with segment boundaries and shaded gapsUse write_record to save a loaded or edited RecordData; it already has
channel names, sampling frequency, and other metadata:
selected = record.select(start=0, stop=1000)
wfdb.write_record("selected", selected)To keep the boundaries of a multi-segment record, read and write its segments instead. This also preserves gaps between segments:
segments = wfdb.read_segments(
"path/to/multi-segment-record", layout="uniform", start=0, stop=1000
)
segments.write("selected-segments")
# Equivalent: wfdb.write_segments("selected-segments", segments)If the source uses a format that cannot be written, pass a target format such
as formats="16". You can also pass one format per selected channel in master
channel order. A fixed-layout selection needs at least one data segment to
define its channels; a window containing only gaps cannot be written.
Use write_signals to create a single-segment record from arrays. In that
case, supply the sampling frequency, value kind, storage format, channel
names, and units:
import numpy as np
signal = np.array([0.0, 0.2, 0.0, -0.2])[:, None]
wfdb.write_signals(
"example",
signal,
fs=250,
kind="physical",
formats="16",
channel_names=["ECG"],
units=["mV"],
)This writes example.hea and example.dat. The input has one column per
channel; formats specifies the WFDB storage format. See the
writing guide for expanded
signals, digital calibration, missing samples, archives, and
write_annotations.
Before sharing local files, check a record or a whole directory:
wfdb check path/to/record.hea
wfdb check path/to/datasetThe checker reports header syntax problems, missing referenced signal or
segment files, and problems in the first signal frame. It checks .hea files
and .wfdb archives while ignoring unrelated PDFs and CSVs. This is a
preflight check, not a full scan of every sample or annotation. See the
checking guide
for check_record() and check_dataset().
- Remote records: Use a
physionet://URI, an HTTP URL, or a supportedfsspeccloud URI such ass3://orgs://. See source locations. - Archives: Read and write
.wfdbarchives. See the archive guide. - Signal processing: Resample signals and detect beats with the processing tools.
- Conversions: See the conversion guide.
In 6.0, read_record returns RecordData instead of a mutable Record or
MultiRecord. read_signals has been removed; use read_record instead.
Use read_segments when you need uncombined segments, or rdrecord when you
need a mutable legacy object. The legacy API remains available. The
6.0 migration guide
maps the old fields and calls to their replacements.
The documentation covers the full API. The demo notebook walks through bundled sample data offline. Report bugs and request features in the issue tracker.
See DEVELOPING.md to contribute. If you use WFDB-Python in research, please cite its PhysioNet publication.
WFDB-Python follows the open WFDB specifications and is inspired by the original WFDB Software Package. The WFDB project website introduces the format and its other implementations.
