A fast, lightweight implementation of the WFDB library and command-line tools, with identical behavior across seven programming languages
WFDB-x is a reimplementation of the WFDB Software Package (the library and its command-line tools) in seven languages: Python, C++, MATLAB/Octave, Rust, Julia, R and Kotlin, tested to give byte-for-byte identical results to the original C package and to each other. This is the Rust implementation. Website (wfdb-x.org) and the other languages · WFDB-x at a glance (overview figure) · Benchmarks · User’s manual (PDF) (chapter “Rust” for this language) · How to cite WFDB-x
The wfdbx crate reads and writes physiological signal records in the WFDB
format used by PhysioNet. It is a complete Rust
implementation of the WFDB Software Package: the library and all of its
command-line tools. It reads and writes the same files as the original C
library, and its tools print the same output.
The crate needs Rust 1.70 or later. It has no dependencies beyond the Rust standard library, and runs on Linux, macOS and Windows.
All three are written in Rust; none of them calls the C library.
read_record,
read_header, read_annotations, write_record,
write_record_physical, write_annotations and check_record read and
write whole records and annotation files in one call, as Vecs (samples
in Vec<i32>, frame by frame, or Vec<f64> in physical units), with
their options in a ReadOptions or WriteOptions struct, and return a
Result whose error type is wfdbx::record::Error. They read signal
files in large blocks, so they are the fastest way to read a record.
read_metadata, get_metadata, set_metadata, remove_metadata and
update_metadata read and change the Key: value entries of a header
(see Header metadata).
They are an addition of WFDB-x: the C package has no equivalent. Use
them for new code and analysis.isigopen,
getvec, getann, putann, …), with the C behavior: sample by
sample and annotation by annotation, errors reported by return values.
Use them to port a program written against the C library, or for the
exact C semantics.rdsamp, rdann, wrsamp, xform, and
29 others), with the options, output and exit status of the C tools,
each a separate program run by name after cargo install --path rust
(rdsamp -r 100s). Use them in shell scripts and pipelines, or to
reproduce work done with the original tools.The high-level functions return exactly the samples and annotations that the library functions (and the C library) give.
To use the library in a Cargo project, add it to the project’s Cargo.toml:
[dependencies]
wfdbx = { git = "https://github.com/alphanumericslab/wfdb-x" }
# or, from a copy of the repository:
wfdbx = { path = "../wfdb-x/rust" }
To install the command-line tools in ~/.cargo/bin, run this from the root
of a copy of the repository:
cargo install --path rust
cargo build --release in the rust directory builds the library and the
tools in rust/target/release/ without installing them.
The repository includes a sample record, 100s, in tests/data: the first
minute of record 100 of the
MIT-BIH Arrhythmia Database, with two
ECG signals at 360 Hz and its beat annotations. This program reads it and
writes a copy of its first 10 seconds as a new record, copy. Run it from
the root of the repository, or change the directory in its first line.
use wfdbx::{read_annotations, read_header, read_record, ReadOptions, WriteOptions};
fn main() -> Result<(), wfdbx::record::Error> {
// Read the sample record from the repository's tests/data directory.
wfdbx::wfdbx_set_input_dir("tests/data");
let rec = read_record("100s", &ReadOptions::default())?; // signals in ADC units
println!("{} {} {}", rec.fs, rec.nsig, rec.nsamples()); // 360 2 21600
let mlii = rec.physical_signal(0)?; // signal 0 in physical units
println!("{:?}", &mlii[..3]); // [-0.145, -0.145, -0.145]
let hdr = read_header("100s", false)?; // the header only, no samples
println!("{} {}", hdr.nsig, hdr.nsamples); // 2 21600
let ann = read_annotations("100s", "atr", None, None, None, false)?; // beat annotations
println!("{} {:?}", ann.len(), &ann.samples[..3]); // 75 [18, 77, 370]
// Write the first 10 seconds as a new record, "copy", in the current directory.
let opt = ReadOptions { to: Some("0:10".into()), ..Default::default() };
let x = read_record("100s", &opt)?;
let w = WriteOptions {
gains: x.siginfo.iter().map(|s| s.gain).collect(),
baselines: x.siginfo.iter().map(|s| s.baseline).collect(),
adcres: vec![11],
units: vec![Some("mV".into())],
names: x.names(),
..Default::default()
};
wfdbx::write_record("copy", &x.samples, x.nsig, x.fs, &w)?;
Ok(())
}
The examples in this section read the sample record 100s of the quick
start (the first minute of record 100 of the MIT-BIH Arrhythmia Database,
included with WFDB-x in the tests/data directory of a copy of the
repository). Run them from the root of the repository: each one sets the
input directory to tests/data in its first line.
Each high-level function returns a Result whose error type is
wfdbx::record::Error. Text read from files, such as signal descriptions,
units and annotation mnemonics, is given as bytes (Vec<u8>). Use
String::from_utf8_lossy to print it.
read_recordread_record(record, &opt) reads the signals of a record. The options are
the fields of a ReadOptions:
use wfdbx::{read_record, ReadOptions};
wfdbx::wfdbx_set_input_dir("tests/data"); // the sample record 100s
let opt = ReadOptions {
channels: vec!["MLII".into()],
from: Some("0:10".into()),
to: Some("0:20".into()),
..Default::default()
};
let rec = read_record("100s", &opt)?;
let mlii = rec.physical_signal(0)?; // Vec<f64>, in physical units
| Argument or option | Meaning | Default |
|---|---|---|
record |
record name or path, without .hea (see Record names) |
|
channels |
signals to read, by number ("0") or name ("MLII", as in names()) |
all signals |
from, to |
first sample, and the sample after the last one: a sample number (from is not negative) or a time string (see below) |
the whole record |
highres |
read a multi-frequency record at its highest sampling frequency | false |
pad |
replace invalid samples by the previous valid one | false |
checksum_warning |
warn when the data do not match the header’s checksums | true |
input_dir, network |
input directory (for relative record names only) and network access for this call only | the current settings |
legacy |
behave as the original package (see Legacy mode) | false |
from and to accept a sample number (Some(3600.into())) or a WFDB time
string: "0:10" (10 seconds), "1:02:03.5", "s3600" (sample 3600) or
"e" (the end of the record).
The result is a Record:
| Field or method | Contents |
|---|---|
samples |
all samples in ADC units (i32), frame by frame: samples[i * nsig + s] is sample i of signal s |
nsig, nsamples() |
the number of signals, and the number of samples per signal |
at(i, s) |
sample i of signal s, in ADC units |
physical(i, s), physical_signal(s) |
one sample, or all samples of signal s, in physical units (f64) |
fs |
sampling frequency in Hz (0 if the header gives none) |
siginfo |
the specifications of each signal (Siginfo): desc, units, gain (ADC units per physical unit; 0 means uncalibrated), baseline, adcres, fmt, … |
names() |
the description of each signal, without the white space that a header may put at either end of it |
signals |
the number, in the record, of each signal read |
sampfrom |
the number of the first sample returned |
base_time |
start time and date as text, to the second, such as "[14:00:03 21/06/2024]" |
base_time_ms |
start time in milliseconds after midnight, such as 50403500 for 14:00:03.500 |
base_date |
start date as [year, month, day] |
comments |
the info strings of the header: its lines that begin with # after the record line (an empty or blank first line is not the record line) |
checksum_ok |
false if the data do not match the header’s checksums |
base_time, base_time_ms and base_date are None when the header does
not give them. Invalid samples are wfdbx::WFDB_INVALID_SAMPLE, or NaN in
physical units.
read_headerread_header(record, legacy) reads the header of a record without opening
its signal files. It returns a Header with these fields: name, fs,
counter_freq, base_counter, nsamples (0 if the header does not give
the length), nsig, siginfo, base_time, base_time_ms, base_date,
comments and metadata. They have the same meaning as in Record;
metadata holds the Key: value entries of the header, as
read_metadata returns them (see Header metadata);
it is empty for an EDF record (which has no .hea file) and for a header
read from the standard input (-). read_header_with
takes a ReadOptions instead of legacy, for its input_dir, network
and legacy options.
wfdbx::wfdbx_set_input_dir("tests/data"); // the sample record 100s
let hdr = wfdbx::read_header("100s", false)?;
for s in &hdr.siginfo {
let desc = String::from_utf8_lossy(s.desc.as_deref().unwrap_or(b""));
println!("{} {} {} {:?}", desc, s.fmt, s.gain, s.units); // MLII 212 200 None
}
read_annotationsuse wfdbx::{read_annotations, PVC};
wfdbx::wfdbx_set_input_dir("tests/data"); // the sample record 100s
let ann = read_annotations("100s", "atr", Some("0:10".into()), Some("0:30".into()), None, false)?;
let npvc = ann.codes.iter().filter(|&&c| c == PVC).count();
The arguments are record, annotator, from, to, fs and legacy.
annotator is the file suffix ("atr" reads 100s.atr). from and to
limit the annotations to a time interval, as in read_record. fs gives
the sampling frequency when the record has no header, or its header gives
none. read_annotations_with(record, annotator, fs, &opt) takes from,
to, input_dir, network and legacy from a ReadOptions. The result
is an Annotations value, in file order:
| Field | Contents |
|---|---|
samples |
annotation times, in samples (i64) |
codes, symbols |
annotation codes (wfdbx::NORMAL, wfdbx::PVC, …) and their mnemonics (b"N", b"V", …) |
subtypes, chans, nums |
subtype, channel and annotator number of each annotation (i32) |
aux |
auxiliary text of each annotation, or None |
fs |
the frequency in which samples are counted (0 if unknown) |
len() |
the number of annotations |
write_recordwrite_record(record, &samples, nsig, fs, &opt) writes a record from
samples in ADC units (i32). write_record_physical takes the same
arguments with samples in physical units (f64): NaN becomes an invalid
sample, and an infinite value, or one beyond the range of i32 in ADC
units, is an error. This example writes two
sine waves as a new record, sine, in the current directory:
use wfdbx::{write_record_physical, WriteOptions};
let fs = 360.0;
let mut x = Vec::new(); // frame by frame: signal 0, signal 1, ...
for i in 0..3600 {
let t = 2.0 * std::f64::consts::PI * i as f64 / fs;
x.push(t.sin());
x.push(0.5 * t.cos());
}
let opt = WriteOptions {
gains: vec![1000.0],
adcres: vec![16],
units: vec![Some("mV".into())],
names: vec!["A".into(), "B".into()],
..Default::default()
};
write_record_physical("sine", &x, 2, fs, &opt)?;
This writes the header sine.hea and the signal file sine.dat. A
trailing .hea of the record name is removed, as by the readers
(write_record("sine.hea", ...) also writes sine.hea and sine.dat). The
sampling frequency, the gain and ADC resolution of each signal, and the
units of each calibrated signal are required; there are no defaults. The
sampling frequency must be finite and > 0, and the gains finite. A gain
of 0 marks a signal as uncalibrated, which is only allowed for samples given
in ADC units; negative gains are allowed. Per-signal options take one value
for all signals, or a vector with one value per signal. The record name
must not be empty, there must be at least one signal, base_time, if
given, must be a valid time, and the metadata entries must be valid,
with no two keys that differ only in letter case and none that is also the
key of an entry in comments. All of this is checked before any file is
touched. (Text is bytes, and ADC values are i32, so the checks of the
other languages for header text that is not Latin-1, and for ADC values that
are not whole numbers within the range of a 32-bit integer, have no
counterpart in Rust.) The files are written to temporary files in the
destination directory and replace the existing files only when both are
complete (see Errors and warnings).
| Argument or option | Meaning |
|---|---|
samples |
the samples, frame by frame: nsig values per frame |
nsig |
the number of signals |
fs |
sampling frequency in Hz |
fmt |
storage format, such as 16, 212, 24 or 516 (FLAC); default 16. The samples must fit it (see Errors and warnings); a FLAC file holds at most 8 signals |
gains, adcres, units |
gain, ADC resolution in bits, and physical units |
baselines, adczeros, names |
optional signal specifications |
base_time, comments |
start time ("hh:mm:ss dd/mm/yyyy") and info strings |
metadata |
Key: value entries, written after the comments as # Key: value: a Vec of (key, value) pairs (see Header metadata) |
samps_per_frame |
samples per frame of each signal, for a multi-frequency record |
dir |
an existing directory in which to create the files, for this call only; record must then be a plain name |
legacy |
behave as the original package (see Legacy mode) |
invalid_warning |
warn about the samples that will be read back as invalid (missing) samples (default true; see Errors and warnings) |
write_annotationsThis example writes annotations for the record sine written by the
previous example:
use wfdbx::{write_annotations, Annotations};
let ann = Annotations {
samples: vec![100, 460, 820],
symbols: vec!["N".into(), "N".into(), "V".into()],
..Default::default()
};
write_annotations("sine", "qrs", &ann, 0.0, None)?; // writes sine.qrs
The arguments are record, annotator, the annotations, fs and an
optional existing output directory. The annotation types are given as
symbols or as codes; one of them is required when there is at least one
annotation (“symbols or codes must be given”), and with no annotations an
empty file is written. If both are given (as read_annotations returns them), each
mnemonic must be that of the code at its position. subtypes, chans,
nums and aux are optional. Each of
these fields is either empty or has one entry per annotation; anything else
is an error, reported before the file is created. As in C, the subtype,
channel and annotator number are stored in one byte each (other values wrap
around). An fs other than 0 is stored in the file as the frequency of its
sample numbers; it must be finite and > 0. Annotation times must not be
i64::MIN or i64::MAX, which the annotation file format reserves
(“annotation time T cannot be written”), and codes must be from 0 to 58: 59
to 63 are the control codes of the file format (“annotation code 61 is
reserved”), and other values do not fit in it (“annotation code 64 is out of
range (0 to 58)”). All of this is checked before the file is touched; the
file is written to a temporary file and replaces an existing one only when
it is complete.
If the annotations are not in time order, the file is sorted when it is
written. write_annotations_with takes an AnnWriteOptions with the fields
dir and sort: sort: Some(false) keeps the given order (without
the library’s advice to run sortann) and Some(true) always sorts, for
that call only. With sort: None, the
WFDBANNSORT environment variable decides (sorting is on unless it is 0).
Many datasets store facts about a recording in the header, as comment
lines of the form # Key: value, such as # Age: 63 or # Dx: 164889003.
These functions read and change them, without touching the rest of the
header:
wfdbx::read_metadata("rec")?; // [("Age", "63"), ("Sex", "F")], as bytes
wfdbx::get_metadata("rec", "age")?; // Some(b"63"), or None if there is none
wfdbx::set_metadata("rec", "Age", "64")?; // replaces the line, or adds "# Age: 64"
wfdbx::remove_metadata("rec", "Sex")?; // 1: the number of lines removed
wfdbx::update_metadata("rec", &[("Age", Some("65")), ("Dx", None)])?; // several changes; None removes
Keys and values are bytes: the functions take &str, &[u8] or
b"...", and return Vec<u8> (read_metadata returns a Vec of
(Vec<u8>, Vec<u8>) pairs).
getinfo reads it) that contains a colon. The record line is
the first line that holds something other than spaces, tabs and CRs and
does not begin with #, as the header parser finds it: an empty or blank
line before it, and the comments before it, are not read (in every
mode). The
key is the text before the first colon, the value the rest of the line,
both without spaces at either end: #Age:45, # Age: 45 and
# Age : 45 all give ("Age", "45"). Comment lines without a colon are
free comments: they are not returned, and never changed. Only the .hea
file is read (not a .info file)."age" finds
Age. A key must not be empty, and must not contain :, #, a line
break or a NUL byte; it may contain spaces ("Date of birth"). If a
header holds two entries whose keys differ only in letter case (written
by other software), reading returns the first, with a warning
(“WFDB-x: header of record REC has more than one entry for key KEY
(lines L1, L2); the first is used”, on the standard error, as the
checksum warning; wfdbquiet() turns it off); set_metadata and
update_metadata refuse to change that key (an error), and
remove_metadata removes both.# Key: value (# Key: if the value
is empty), with the spelling of the key given. A changed entry keeps its
position; a new one goes after the last comment line of the header. Every
other byte of the header is kept, including its line endings. The header
is replaced in one step (through a temporary file in its directory, which
then replaces it, keeping its permissions), and all the arguments are
checked first, so an error leaves it unchanged. If nothing changes,
nothing is written.read_header finds it. The _with
variants (read_metadata_with, get_metadata_with, set_metadata_with,
remove_metadata_with, update_metadata_with) take a MetadataOptions,
whose input_dir and network work as in ReadOptions, for that call
only. A change is written to the header itself, or, with
dir: Some(...), to a header of the same name in that existing directory
(the signal files are not copied; the header is written there even if
nothing changes). A header read from a URL can only be written
elsewhere, with dir. For a multi-segment record, the master header is
read or changed, unless a segment is named.use wfdbx::{set_metadata_with, MetadataOptions};
wfdbx::wfdbx_set_input_dir("tests/data"); // the sample record 100s
std::fs::create_dir_all("copy")?;
let opt = MetadataOptions { dir: Some("copy".into()), ..Default::default() };
set_metadata_with("100s", "Age", "69", &opt)?; // writes copy/100s.hea; tests/data is unchanged
The errors are those of the other high-level functions (wfdbx::record::Error),
such as “can’t read header for record REC”, “metadata key ‘a:b’ must not
contain ‘:’” or “header of record REC has more than one entry for key Age
(lines 4, 6); remove them first”. The documentation of the metadata
module (cargo doc --open) gives the full rules.
Metadata often holds information about patients. WFDB-x stores it as plain text, exactly as given, and does not de-identify it.
getvec then reports the checksum error, -4, for the incomplete last
frame); an incomplete frame is never returned. read_record allocates
memory for the samples as it reads them (at most 2^26 values in advance),
not for the length that the header gives. An annotation
file that ends early, or cannot be read to its end, gives the error
“can’t read annotation file ANN of record REC (getann status -3)”.from in read_record or read_annotations gives an error
(“read_record: from must not be negative”, “read_annotations: from must
not be negative”), before any file is opened. Whatever the outcome,
read_record and read_annotations close their input files before they
return."0:10" needs the
sampling frequency, and physical_signal needs the gain of the signal.read_record
returns all the data and prints a warning on the standard error:
“WFDB-x: checksum error in record REC: the signal data do not match the
checksums in its header”. checksum_warning: false turns the warning off;
rec.checksum_ok is false either way. Checksums are only checked when a
read reaches the end of the record.Key: value entries hold a key more than once (ignoring
letter case) gives the warning “WFDB-x: header of record REC has more
than one entry for key KEY (lines L1, L2); the first is used” when its
metadata is read (see Header metadata).write_record
reads nothing, so it replaces any existing files of the same name.write_record and write_annotations check all their arguments before
they touch any file, and write their files to temporary files in the
destination directory (.NAME.XXXXXXXX.tmp), renamed over the
destinations once all of them are complete, with the permissions of the
files they replace: a failure while writing (a full disk, for example)
leaves the existing files as they were, and no temporary file is left. A
destination that is a symbolic link is followed (the file it leads to is
replaced); one that is not a regular file (a device, a FIFO) is written
directly, as before.write_record) or
“write error on annotator ANN for record REC” (write_annotations), also
in legacy mode; so does an annotation that putann cannot write.write_record checks its arguments in this order: the record name, the
number of signals, samps_per_frame (each value must be at least 1:
“samps_per_frame must hold integers >= 1”), the number of values of each
per-signal option, the number of samples (a multiple of the frame size),
the gains, the ADC resolutions, the units, the gains of signals given in
physical units, the sampling frequency, finite gains, the samples in
physical units, whether the samples fit the storage format, the metadata
and the base time.write_record refuses the samples that the storage format cannot hold
(before WFDB-x 1.2.3, putvec wrapped them around without an error). The
samples of each signal are checked in order (for a multi-frequency
signal, its samples of each frame, frame after frame): a FLAC format
(508, 516, 524) holds at most 8 signals (“write_record: format 516 (FLAC)
stores at most 8 signals in a file; the record has 12”); format 8 stores
the difference between consecutive samples, from -128 to 127, and no
invalid sample (“write_record: signal K changes by D between two samples,
more than format 8 can store (-128 to 127)”, “write_record: signal K has
an invalid sample, which format 8 cannot store”); the other formats,
except 0, store their range of bits, such as -2048 to 2047 for format 212
(“write_record: signal K has a sample (V) outside the range of format F
(LO to HI)”; WFDB_INVALID_SAMPLE is always accepted).WFDB_INVALID_SAMPLE, is read back as an invalid (missing) sample:
write_record and write_record_physical then print a warning on the
standard error, “WFDB-x: N sample(s) of signal K will be read back as
invalid (missing) samples (format F stores an invalid sample as LO)”
(the NaN samples of write_record_physical are not counted: they mark
missing samples deliberately). invalid_warning: false turns the
warning off.Error::message() and Display give the text of an error. The WFDB
library functions that a high-level function calls may also print their own
messages on the standard error, such as “init: can’t open header for record
REC”. wfdbquiet() turns these messages off.
The high-level functions call wfdbquit(), which closes anything opened
with the WFDB library functions. They leave the time resolution of output
annotators (getafreq()) and the getvec mode (getgvmode()) as they were,
also after reading an EDF record (whose opening sets both, as in C), and
also when they fail.
check_recordwfdbx::wfdbx_set_input_dir("tests/data"); // the sample record 100s
let report = wfdbx::check_record("100s", &["atr"])?;
println!("{report}");
check_record(record, annotators) checks the header of a record, its signal
specifications, every sample of its signal files, and the annotation files
named. It reports each problem it finds. It returns an error only for an
empty record name, never for a problem in the record. check_record_with
also takes a ReadOptions, whose input_dir and network apply to that
call. It checks for problems similar to those found by Benjamin Moody’s
wfdbcheck.
The result is a CheckReport. report.ok is true if there is no error.
report.issues lists the problems. Each Issue has a severity
(Severity::Error, Warning or Info), a code (such as "checksum"), a
where ("" for the record, "signal 1", "segment s2",
"annotator atr"; the field is issue.r#where) and a message.
| Severity | Meaning |
|---|---|
error |
the record is damaged or does not follow the WFDB format: programs will read wrong data or fail |
warning |
the record can be read, but something is missing or unusual, and may give wrong results |
info |
a fact worth knowing, such as invalid (missing) samples |
Printed (Display), the report has one line per problem, or the line
no problems found:
warning: signal 0: the units are not specified
error: signal 1: the checksum does not match: the header gives 12, the samples sum to 13
error: annotator atr: 3 annotations are out of order; the first is at sample 50, after sample 100
report.lines() returns these lines as a vector. The lines are the same in
the seven languages of WFDB-x. The checks run in normal mode, even if legacy mode is on, and the library
prints no messages during the call.
What is checked:
The sample record 100s gives two warnings: its header gives no units.
WFDB-x reads and writes files exactly where they are named, and nowhere else:
| The name is | It is read from | It is written to |
|---|---|---|
a full path: /data/mitdb/100, C:\data\100 |
that path | that path |
a home path: ~/data/100 |
the user’s home directory | the user’s home directory |
a URL: https://.../100 |
that URL, if network access is on | (URLs cannot be written) |
anything else: 100, mitdb/100, ../data/100 |
the input directory | the output directory |
In this table, /data/mitdb stands for a local copy of the MIT-BIH
Arrhythmia Database, and 100 for its record 100.
The input and output directories are both the current directory unless they are set:
| Setting | Function | Option of the high-level functions | Environment variable |
|---|---|---|---|
input directory: a local directory, ~/... or a URL |
wfdbx_set_input_dir(d) |
ReadOptions::input_dir |
WFDBX_INPUT_DIR |
| output directory | wfdbx_set_output_dir(d) |
WriteOptions::dir, AnnWriteOptions::dir |
WFDBX_OUTPUT_DIR |
| network access (off by default) | wfdbx_set_network(1) |
ReadOptions::network |
WFDBX_NETWORK=1 |
A function call overrides the environment variable, and an option applies to
that one call. Each set function returns the previous setting.
wfdbx_get_input_dir(), wfdbx_get_output_dir() and wfdbx_get_network()
return the current settings. Passing None to wfdbx_set_input_dir or
wfdbx_set_output_dir, or -1 to wfdbx_set_network, restores the
default.
In this example, /data/mitdb stands for a local copy of the MIT-BIH
Arrhythmia Database in /data/mitdb; the annotations are written to a
directory out:
use std::path::Path;
use wfdbx::{read_record, write_annotations, Annotations, ReadOptions};
wfdbx::wfdbx_set_input_dir("/data/mitdb");
let rec = read_record("100", &ReadOptions::default())?; // reads /data/mitdb/100.hea and 100.dat
let ann = Annotations {
samples: vec![100, 460],
symbols: vec!["N".into(), "N".into()],
..Default::default()
};
write_annotations("100", "qrs", &ann, rec.fs, Some(Path::new("out")))?; // writes out/100.qrs
The signal files that a header names are read from the header’s directory.
A record name is given without the .hea extension (the readers,
write_annotations and write_record also accept a trailing .hea, which
is removed). It is one of:
/data/mitdb/100) or in the home directory
(~/Documents/100s), used as given;100s, data/100s), read from the input directory if
one is set, otherwise from the current directory, and written to the
output directory if one is set, otherwise to the current directory
(missing directories are created);https://physionet.org/files/mitdb/1.0.0/100), read only if
network access is on; a URL cannot be written.The input_dir option is optional, and applies to relative names only, not
to full paths or URLs. A writer given a name with a path creates the files
in that directory, and the header names the record and its signal file
without the path (rec, rec.dat), so that the record opens under any
path that leads to it; in legacy mode, as in the original, the header gives
the name with its path. With the dir option, the name must be a plain
name (rec): the files are created in dir, and the record opens from that
directory under its plain name.
A record name is made of letters, digits, _, - and ~. / separates
directories, and the names of directories may contain any characters, such
as dots: my.data/rec is a valid record name, rec.1 is not. A name whose
last part contains a dot is the name of a file: 100s.hea is read as the
header of record 100s, and 100s.edf as an EDF file.
Records can be written under any such name: write_record("my.data/rec",
...) creates my.data/rec.hea and my.data/rec.dat. The header names the
record and its signal file without the directory (rec, rec.dat), so the
record can be read under any path that leads to it. Annotator names may
contain letters, digits, _, - and ~.
Set the input directory to the URL of the database and turn network access
on. The URL of a database is https://physionet.org/files/<database>/<version>/.
This example reads record 100 of the MIT-BIH Arrhythmia Database:
use wfdbx::{read_record, ReadOptions};
let opt = ReadOptions {
input_dir: Some("https://physionet.org/files/mitdb/1.0.0/".into()),
network: Some(true),
..Default::default()
};
let rec = read_record("100", &opt)?;
Network access uses the curl program, which must be installed. A remote
file that has been read is kept for the rest of the session; one that could
not be read is requested again the next time it is opened. A download gives
up when it cannot connect, or receives nothing, for 30 seconds. Only
http:// and https:// URLs can be read: a name that begins with another
URL scheme, such as ftp:// or sftp://, is refused, with or without
network access, and never read as a local file name (mount the server as a
local folder, e.g. with sshfs or rclone, or download the files).
WFDB-x never assumes a sampling frequency, gain, ADC resolution or units
that a record does not give. Where one is needed, it must be given: in
write_record, as the fs argument of read_annotations, or as an option
of the tools (see below). A record whose header gives no frequency has fs =
0. See
DIVERGENCES.md
for the reasons.
Each tool is a separate program. After cargo install --path rust, run the
tools by name. These commands, run in the tests/data directory of a copy
of the repository, read the sample record 100s (the first minute of record
100 of the MIT-BIH Arrhythmia Database, included with WFDB-x):
rdsamp -r 100s -t 10 -p # print the first 10 seconds, in physical units
rdann -r 100s -a atr # print the annotations
wfdbdesc 100s # describe the record
The tools have the options and output of the original WFDB tools, which are
described in the WFDB Applications Guide
(third_party/wag.pdf):
rdsamp, wrsamp, wfdbdesc, sigamp, snip, xform,
sampfreq, signame, signum, wfdbcat, wfdbwhichwfdbtime, time2secrdann, wrann, sortann, ann2rr, rr2annedf2mit, mit2edf, rdedfann, wav2mit, mit2wav,
wfdb2mat, a2m, m2a, ad2m, md2a, ahaecg2mit, makeid, readid,
revise, parsescpThe tools use the environment variables of the table above, also for the
files that they read or create themselves: the -i file of wrsamp,
edf2mit, wav2mit, a2m and ad2m, the input files of ahaecg2mit and
the -S script of xform are read from the input directory, and the WAV
file of mit2wav, the EDF file of mit2edf, the master header of snip -m
and the .des, .ecg, .key and .txt files of parsescp are created in
the output directory, as the records they read and write are. Such a file
cannot be read from a URL. wfdbx_input_path(name) and
wfdbx_output_path(name) return the path where a program of its own should
open such a file.
To read record 100 of the MIT-BIH Arrhythmia Database from PhysioNet:
export WFDBX_INPUT_DIR=https://physionet.org/files/mitdb/1.0.0/
export WFDBX_NETWORK=1
rdsamp -r 100 -t 0:05
Tools that write signal parameters require them as options:
| Tool | Required options |
|---|---|
wrsamp (with -o) |
-F frequency, -G gain, -R ADC resolution, and -U units for calibrated signals |
wav2mit |
-G gain, and -U units unless the gain is 0 |
ad2m, ahaecg2mit |
-F, -G, -R, and -U unless the gain is 0 |
a2m, rdann, ann2rr, sortann, m2a |
-F frequency, when the record’s header does not give one |
For example, wrsamp -F 250 -G 200 -R 16 -U mV -i samples.txt -o rec
writes the record rec from samples.txt, which stands for a text file of
samples, one frame per line.
Every function of the WFDB library is available under its C name, with the
same arguments and return values, so programs and documentation written for
the C library carry over directly. The library’s state, such as the open
record and annotators, is held by a Wfdb value, and each function is a
method of it. Where a C function fills an array, the Rust function fills a
slice passed by the caller. This example reads the sample record 100s (see
the quick start) from tests/data; run it from the root of the repository:
use wfdbx::{Anninfo, Annotation, Siginfo, Wfdb, WFDB_READ};
let mut w = Wfdb::new();
w.wfdbx_set_input_dir("tests/data");
let mut si = vec![Siginfo::default(); 2];
let nsig = w.isigopen("100s", &mut si, 2); // open 2 signals; si receives their specifications
let mut v = vec![0; nsig.max(0) as usize];
while w.getvec(&mut v) > 0 { // v receives one sample per signal
// ...
}
let mut a = Annotation::default();
w.annopen("100s", &[Anninfo { name: "atr".into(), stat: WFDB_READ }], 1);
while w.getann(0, &mut a) == 0 {
println!("{} {}", a.time, String::from_utf8_lossy(&w.annstr(a.anntyp as i32)));
}
w.wfdbquit();
Differences from C that come from the language:
wfdbx::isigopen(...). The free functions use a default Wfdb
value, one per thread. Separate Wfdb values do not share any state, so
several records can be open at once, in one thread or in several.&str, &[u8] or b"...", and
results are Vec<u8>. C’s NULL is None; pass wfdbx::NULL for a
NULL argument.int is i32, long and WFDB_Time are i64, WFDB_Sample is
i32, and char is i8.Siginfo, Calinfo, Anninfo, Annotation,
Seginfo) have the C field names. Default::default() gives C’s zero
values.aux is None, or bytes whose first byte is the length
of the text that follows, as in C.getvecs(&mut buf, n) reads up to n frames at once into buf. It
returns the number of frames it read, or the getvec status if it read
none.wfdberror() returns the
last message, and wfdbquiet() stops the messages from being printed.wfdbx_write_errors() returns the number of write errors since the
program started: the output files that the library could not write, flush
or close (found when they are closed, as by wfdbquit()).
wfdbx_note_write_error() counts one more, for a file that a program
writes itself, and wfdbx_fclose_output(file, pname, name) closes such a
file, reporting “PNAME: write error on NAME” and counting it. The tools
exit with status 1 after a write error, or when the standard output
cannot be written (“PROGRAM: write error on standard output”); legacy
mode keeps the original status, 0.wfdbquit() closes the open record and annotators; the directory and
network settings are kept, and so are the time resolution of output
annotators (getafreq()), which opening an EDF record sets to its
sampling frequency when none is set, and the getvec mode (getgvmode()),
to which it adds WFDB_HIGHRES, as in C. The high-level functions leave
both as they were.The annotation codes (NORMAL, PVC, …) and the functions of
<wfdb/ecgmap.h> (isqrs, map1, map2, …) are also provided. The
cstd module provides the C standard functions that the tools use, such as
a printf that formats numbers exactly as the GNU C library does. Run
cargo doc --open in the rust directory for the documentation of every
function.
Legacy mode reproduces the original WFDB package exactly, including its assumed signal parameters and its search for files. Use it only to reproduce old results:
--legacy as the first argument, as in rdsamp --legacy -r 100s,
run in the directory of the sample record 100s (tests/data in a copy
of the repository). Legacy mode finds files through the WFDB path, which begins with the
current directory, and does not use the input directory (the files that
a tool reads itself are opened as named, relative to the current
directory); the output directory, if set, is a prefix of every output
name, those of the files that a tool creates itself included;legacy: true in ReadOptions or WriteOptions,
or true as the legacy argument of read_header and read_annotations;wfdbx_set_legacy(1), or w.wfdbx_set_legacy(1) for
one Wfdb value.DIVERGENCES.md describes what changes in legacy mode.
cargo build --release).
read_record reads signal files in large blocks and is the fastest way to
read a record. For a program that processes a record as a stream,
getvecs reads many frames at once; it is faster than calling getvec
for each frame.
BENCHMARKS.md
compares the seven languages and wfdb-python.PATH runs.unsafe.From the rust directory of the repository:
cargo test --release
This runs the unit tests, the integration tests and the examples in the documentation. To run the cross-language conformance suite, which compares the tools with the original C tools, build the tools and run the harness from the root of the repository:
cargo build --release --manifest-path rust/Cargo.toml
python3 tests/conformance/harness.py check --impl rust
python3 tests/conformance/harness.py check --legacy --impl rust # legacy mode
DEVELOPING.md describes how the crate is organized, for developers of WFDB-x.
WFDB-x implements version 10.7.0 of the WFDB Software Package.
The WFDB library and tools were written by George B. Moody, with revisions by
Benjamin Moody and contributions from many others; parsescp was written by
George B. Moody and Edna S. Moody. Each module names the authors of the C
code it implements, and
AUTHORS.md
gives the details. The Rust implementation was developed by Reza Sameni
(2026), through AI-assisted development, cross-language
verification and unit testing.
WFDB-x is an independent reimplementation. Its Rust code is a translation of the C code of the WFDB Software Package into Rust, written for WFDB-x; it is not code by the original WFDB developers. It follows the original’s logic and behavior, with the changes described in DIVERGENCES.md. The authors of the original C code are credited in each file and in AUTHORS.md.
The WFDB-x library is distributed under the GNU Lesser General Public
License, version 2.1 or (at your option) any later version
(LICENSE): programs under any license, proprietary ones
included, may use it on the LGPL’s terms. The command-line tools (the
apps and the wfdbx command), translations of the tools of the original
package, are distributed under the GNU General Public License, version 3 or
(at your option) any later version
(LICENSE-GPL). Each source file states its license.
The crate’s license expression is therefore LGPL-2.1-or-later AND
GPL-3.0-or-later AND BSD-3-Clause. The file
NOTICE,
included in the crate (in the rust directory) with both licenses
(LICENSE, LICENSE-GPL), summarizes this and gives the copyright notice
and license (BSD-3-Clause) of libFLAC 1.4.3, whose stream decoder the FLAC
decoder of the crate translates.