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 Kotlin implementation. Website (wfdb-x.org) and the other languages · WFDB-x at a glance (overview figure) · Benchmarks · User’s manual (PDF) (chapter “Kotlin” for this language) · How to cite WFDB-x
wfdbx reads and writes physiological signal records in the WFDB format used
by PhysioNet. It is a complete Kotlin 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 Kotlin implementation exists for mobile application developers and JVM developers. With Kotlin Multiplatform, the same code runs on Android, on iOS and on every JVM, so an app that records or displays physiological signals can read and write WFDB records on the device with the WFDB engine that the other implementations of WFDB-x use, with identical behavior, natively in its own language: no native libraries to build for each platform, no JNI, and no other runtime (such as Python) to embed. Server and desktop programs in Kotlin or Java get the same library from Maven coordinates.
The library is written in common Kotlin code (Kotlin 2.1). Its only
dependency is the Kotlin standard library. The JVM artifact is compiled for
Java 11, so it runs on every current JVM and on Android (API level 26 or
later); the iOS targets (iosArm64, iosSimulatorArm64, iosX64) are
declared in the same project (see Notes).
All three are written in Kotlin; none of them calls the C library.
readRecord, readHeader,
readAnnotations, writeRecord, writeAnnotations and checkRecord
read and write whole records and annotation files in one call
(readMetadata, setMetadata and the other
header metadata functions read and change the
Key: value entries of a header), with
named arguments: the samples are an IntArray (and a DoubleArray in
physical units), frame by frame, and errors throw a WfdbException.
They read signal files in large blocks, so they are the fastest way to
read a record. They are an addition of WFDB-x: the C package has no
equivalent. Use them for new code, apps and analysis.isigopen,
getvec, getann, putann, …), with the C behavior: frame by
frame and annotation by annotation, with 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, run
on the JVM through the bin/wfdbx launcher (kotlin/bin/wfdbx 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.
The library is built from a copy of the repository and installed in the
local Maven repository (below); its Gradle coordinates are
io.github.alphanumericslab:wfdbx:1.2.3. In a Gradle project (an Android
app, a JVM program, or the common source set of a Kotlin Multiplatform
project) that lists mavenLocal() among its repositories, add it to the
dependencies:
dependencies {
implementation("io.github.alphanumericslab:wfdbx:1.2.3")
}
To build it from a copy of the repository and install it in the local Maven
repository (~/.m2), run, in the kotlin directory:
./gradlew publishToMavenLocal
and add mavenLocal() to the repositories of the project. The build uses
the Gradle wrapper of the repository (Gradle 8.14, which downloads itself),
and needs a JDK 11 or later.
The build downloads the Kotlin Gradle plugin and its dependencies from Maven
Central. Where Maven Central cannot be reached (or is rate-limited), the
Gradle property mavenMirror names another Maven repository, for plugins and
dependencies alike: give it on the command line, or set it once in
~/.gradle/gradle.properties:
./gradlew -PmavenMirror=https://maven-central.storage-download.googleapis.com/maven2/ publishToMavenLocal
# or, in ~/.gradle/gradle.properties:
# mavenMirror=https://maven-central.storage-download.googleapis.com/maven2/
The command-line tools need the runnable jar of the tools,
build/libs/wfdbx-all.jar:
./gradlew fatJar
bin/wfdbx --list # the tools
Every jar of the build (the library jars, the sources jars and
build/libs/wfdbx-all.jar) holds the licenses and the notice of the
repository in META-INF: LICENSE (LGPL-2.1-or-later), LICENSE-GPL
(GPL-3.0-or-later) and NOTICE (the licenses, and the BSD-3-Clause notice
of libFLAC, from which the FLAC decoder is translated). The POM of the
published artifacts lists the three licenses.
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. Run it from the root of the
repository, or change the directory in its first line.
import wfdbx.*
fun main() {
// Read the sample record from the repository's tests/data directory.
wfdbxSetInputDir("tests/data")
val rec = readRecord("100s", physical = true) // samples in ADC and physical units
println("${rec.fs} ${rec.names} ${rec.nsamples}") // 360.0 [MLII, V5] 21600
val hdr = readHeader("100s") // the header only, no samples
println("${hdr.nsig} ${hdr.nsamples}") // 2 21600
val ann = readAnnotations("100s", "atr") // beat annotations
println("${ann.size} ${ann.symbols.take(3)} ${ann.samples.take(3)}") // 75 [+, N, N] [18, 77, 370]
// Write the first 10 seconds as a new record in the current directory.
val x = readRecord("100s", to = TimeSpec("0:10")) // ADC units
writeRecord("copy", x.samples, x.nsig, x.fs, fmt = 16, gains = x.gains.toDoubleArray(),
baselines = x.baselines.toIntArray(), adcres = intArrayOf(11), units = listOf("mV"),
names = x.names)
}
Each high-level function is a top-level function, which uses a default
library state (one per thread), and an extension function of Wfdb, the
class that holds the library state: w.readRecord(...) uses the state w.
The options are named arguments with default values.
The examples of this section read the sample record 100s (the first
minute of record 100 of the MIT-BIH Arrhythmia Database, in tests/data of
a copy of the repository), after wfdbxSetInputDir("tests/data"), as in the
quick start; rand is a record that the examples write themselves.
readRecordval rec = readRecord("100s", channels = listOf("MLII"), from = TimeSpec("0:10"),
to = TimeSpec("0:20"), physical = true)
rec.nsamples // 3600: 10 s of lead MLII
rec.physical!![0] // its first sample, in mV
| Argument | Meaning | Default |
|---|---|---|
record |
record name, without .hea (a trailing .hea is also accepted): a full path, absolute or ~/..., used as given; a relative path, read from the input directory if one is set, otherwise from the current directory; or a URL, read only if network access is on (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 TimeSpec (see below; from is not negative) |
the whole record |
physical |
also return the samples in physical units (Record.physical) |
false |
highres |
read a multi-frequency record at its highest sampling frequency | false |
pad |
replace invalid samples by the previous valid one | false |
checksumWarning |
warn when the data do not match the header’s checksums | true |
onWarning |
receives the warning, instead of the standard error | null |
inputDir, network |
input directory (a local directory, ~/... or a URL, under which relative record names are read; it does not apply to full paths or URLs) and network access, for this call only |
the current settings |
legacy |
behave as the original package (see Legacy mode) | false |
A TimeSpec is a sample number, TimeSpec(3600), or a WFDB time string,
TimeSpec("0:10") (10 seconds), TimeSpec("1:02:03.5"), TimeSpec("s3600")
(sample 3600) or TimeSpec("e") (the end of the record). A sample number
is a whole number by its type; null (the default to) reads to the end.
On any error once the record is open, its input signals are closed, and
the getvec mode and the settings changed for the call are restored.
The result is a Record. The samples are in one IntArray, frame by frame,
as the library returns them: samples[i * nsig + s] is sample i of signal
s (rec.at(i, s)), so that a record of millions of samples is one array,
without a copy per signal. rec.signal(s) returns the samples of one signal
(IntArray), and rec.physicalSignal(s) the same in physical units
(DoubleArray).
| Field | Contents |
|---|---|
samples |
IntArray of ADC units, frame by frame: nsig values per frame |
physical |
with physical = true, a DoubleArray of the same samples in physical units, (adc - baseline) / gain; otherwise null |
nsig, nsamples |
the number of signals and of samples per signal |
fs |
sampling frequency in Hz (0 if the header gives none) |
names, units, gains, baselines |
description (without the white space that a header may put at either end of it), physical units, gain (ADC units per physical unit; 0 means uncalibrated) and baseline of each signal |
siginfo |
the full signal specifications (Siginfo objects) |
signalNumbers |
the number, in the record, of each signal returned |
sampfrom |
the number of the first sample returned |
baseTime |
start time and date as text, to the second, such as "[14:00:03 21/06/2024]" |
baseTimeMs |
start time in milliseconds after midnight, such as 50403500 for 14:00:03.500 |
baseDate |
start date as a BaseDate(year, month, day) |
comments |
the info strings of the header |
checksumOk |
false if the data do not match the header’s checksums |
baseTime, baseTimeMs and baseDate are null when the header does not
give them. Invalid samples are WFDB_INVALID_SAMPLE, or NaN in physical
units.
readHeaderreadHeader(record) reads the header of a record without opening its
signal files. It returns a Header with these fields: name, fs,
counterFreq, baseCounter, nsamples (0 if the header does not give the
length), nsig, siginfo, baseTime, baseTimeMs, baseDate,
comments and metadata. comments are the info strings of the
header: its lines that begin with # after the record line, without the
#. The record line is found as the header parser finds it: the first
line that holds something other than spaces, tabs, CRs and LFs and does
not begin (after them) with #, so that an empty or blank first line, and
the comments before the record line, are skipped (in legacy mode, as in
C’s getinfo, the record line is the first line that does not begin with
#). metadata is the list of the Key: value
entries of the header (of the .hea file only, not of a .info file), in
file order, as readMetadata returns them (see
Header metadata; it is empty for an EDF file). The
others have the same meaning as in Record. record, inputDir, network
and legacy work as in readRecord, and onWarning receives the warning
about a key that the header gives more than once.
val hdr = readHeader("100s")
for (s in hdr.siginfo) {
println("${s.desc?.latin1()} ${s.fmt} ${s.gain} ${s.units?.latin1()}") // MLII 212 200.0 null
}
(The fields of Siginfo are byte strings, as in C; latin1(), from
wfdbx.cstd, converts them.)
readAnnotationsval ann = readAnnotations("100s", "atr", from = TimeSpec("0:10"), to = TimeSpec("0:30"))
val beats = (0 until ann.size).filter { isqrs(ann.codes[it]) != 0 }.map { ann.samples[it] }
record, inputDir, network and legacy work as in readRecord.
annotator is the file suffix ("atr" reads 100s.atr). The optional fs
gives the sampling frequency when the record has no header, or its header
gives none. from must not be a negative sample number
(“readAnnotations: from must not be negative”), and fs must be > 0;
both are checked before any file is opened (WfdbArgumentException).
Whatever happens, the annotation file is closed before the function
returns. The result is an Annotations object, in file order:
| Field | Contents |
|---|---|
samples |
annotation times, in samples (LongArray) |
codes, symbols |
annotation codes (NORMAL, PVC, …) and their mnemonics ("N", "V", …) |
subtypes, chans, nums |
subtype, channel and annotator number of each annotation (IntArray) |
aux |
auxiliary text of each annotation, or null |
fs |
the frequency in which samples are counted (0 if unknown) |
size |
the number of annotations |
writeRecordval rnd = java.util.Random(1)
val x = DoubleArray(3600 * 2) { rnd.nextGaussian() } // 3600 frames of 2 signals, in mV
writeRecord("rand", x, nsig = 2, fs = 360.0, fmt = 16, gains = doubleArrayOf(1000.0),
adcres = intArrayOf(16), units = listOf("mV"), names = listOf("A", "B"))
This writes the header rand.hea and the signal file rand.dat, in the
output directory if one is set, otherwise in the current directory. The
record name (a trailing .hea is removed, as by the readers) is a plain name (rec) or a relative path (data/rec), created
in the output directory (missing directories included), or a full path,
absolute or ~/..., used as given; with a path, 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). A URL cannot be written. The
sampling frequency (finite and > 0), the gain (finite) and ADC resolution
of each signal, and the units of each calibrated signal are required; there
are no defaults. The record name must not be empty, there must be at least
one signal, and a baseTime that cannot be parsed is an error. The
samples are given frame by frame, as Record.samples: an IntArray of ADC
units, or a DoubleArray of physical units, converted to ADC units as
round(x * gain) + baseline (NaN becomes an invalid sample; an infinite
value, or one beyond the range of 32-bit integers in ADC units, is an error).
A gain of 0 marks a signal as uncalibrated, which is only allowed for
samples in ADC units; negative gains are allowed. Per-signal arguments take one value for all signals,
or one value per signal; anything else is an error. The record name, the
signal names, the units and the comments are text of one byte per
character (ISO-8859-1): a character beyond U+00FF is an error (“writeRecord:
units ‘μV’ has a character that is not Latin-1 (one byte per character)”).
Every argument is checked before any file is created or changed (an
invalid argument throws a WfdbArgumentException), in this order: the
record name (not empty, Latin-1), the number of signals, the number of
values of each per-signal argument, the gains, the ADC resolutions
(“writeRecord: ADC resolution(s) must be specified”), the units, the
sampling frequency, the signal names and units (Latin-1), the samples
(their number, then whether they fit the storage format), the
comments (Latin-1), the metadata and the base time.
Every sample, in ADC units (after the conversion from physical units),
must fit the storage format: within its range (“writeRecord: signal 0 has a
sample (40000) outside the range of format 16 (-32768 to 32767)”), and, in
format 8, within -128 to 127 of the sample before it in the same signal
(“writeRecord: signal K changes by D between two samples, more than format
8 can store (-128 to 127)”); format 8 cannot store an invalid sample
(“writeRecord: signal K has an invalid sample, which format 8 cannot
store”), and a FLAC file (formats 508, 516, 524) holds at most 8 signals
(“writeRecord: format 516 (FLAC) stores at most 8 signals in a file; the
record has 12”). (Until WFDB-x 1.2.3, such samples were silently wrapped
around: 40000 read back as -25536.) The smallest value of a format (-32768
in format 16, -2048 in format 212) is the value of an invalid (missing)
sample: samples equal to it, or to WFDB_INVALID_SAMPLE, are read back as
invalid, and writeRecord gives a warning for them (“1 sample(s) of signal
0 will be read back as invalid (missing) samples (format 16 stores an
invalid sample as -32768)”), except for the NaN samples of physical units,
which mark missing samples deliberately; invalidWarning = false omits
it. The warning goes to onWarning, or, without it, to the standard error
(“WFDB-x: “ + the warning), unless wfdbquiet was called. The signal file and the
header are then written to temporary files in the directory of each file
(.NAME.XXXXXXXX.tmp, readable and writable by their owner only while
they are written), which replace the existing files only when both are
complete, with the permissions of the files they replace (or of any new
file); after an error, they are removed, and the existing files are left as
they were. A symbolic link is kept (the file it leads to is replaced); a
destination that is not a regular file (a device, a FIFO) is written
directly. The output signals are closed whatever happens.
| Argument | Meaning |
|---|---|
samples, nsig |
the samples, frame by frame, and the number of signals |
fs |
sampling frequency in Hz |
fmt |
storage format, such as 16, 212, 24 or 516 (FLAC; at most 8 signals); default 16. Every sample must fit it (above) |
gains, adcres, units |
gain, ADC resolution in bits, and physical units |
baselines, adczeros, names |
optional signal specifications |
baseTime, comments |
start time ("hh:mm:ss dd/mm/yyyy") and info strings |
sampsPerFrame |
samples per frame of each signal, for a multi-frequency record |
dir |
directory (which must exist) in which to create the files, for this call only; the record name must then be a plain name, and the record opens from that directory under it |
legacy |
behave as the original package (see Legacy mode) |
metadata |
Key: value entries, written after the comments as # Key: value: a list of pairs, listOf("Age" to "63") (a map converts with toList(); see Header metadata). The keys must be unique, ignoring letter case, and none may be the key of an entry in comments; this is checked before any file is created |
invalidWarning |
warn when samples will be read back as invalid (missing) samples; default true |
onWarning |
receives the warnings (null: they are printed on the standard error, as the checksum warning of readRecord) |
writeAnnotationswriteAnnotations("rand", "qrs", longArrayOf(100, 460, 820), symbols = listOf("N", "N", "V"))
The file RECORD.ANNOTATOR is created where writeRecord would create the
record (a trailing .hea of the record name is also accepted); with dir,
the record name must be a plain name, and the file is DIR/RECORD.ANNOTATOR.
Give the annotation types as symbols or as codes (“symbols or codes
must be given” otherwise; with no annotations, neither is needed, and an
empty file is written). Both may be given (as
readAnnotations returns them) if they agree: the code of each symbol must
be the code at its position (an empty symbol agrees with any code), or
“writeAnnotations: symbols and codes disagree at annotation K” (K counting
from 0) is thrown. subtypes, chans, nums and aux are optional.
Each of these arguments, when given, has one value per annotation; anything
else is an error. fs, if given, must be finite and > 0. The times must
be from -2^63 + 1 to 2^63 - 2: the smallest and the largest 64-bit
integers are reserved by the file format (“writeAnnotations: annotation
time T cannot be written (annotation times must be integers from
-9223372036854775807 to 9223372036854775806)”). The codes must be from 0 to
58: 59 to 63 are the control codes of the file format (“writeAnnotations:
annotation code C is reserved (59 to 63 are the control codes of the
annotation file format)”), and other values do not fit (“… annotation
code C is out of range (0 to 58)”). The aux texts are Latin-1 text (“aux
text ‘X’ has a character that is not Latin-1 (one byte per character)”).
These checks (WfdbArgumentException) are made before the file is created
or changed, in this order: the number of values, fs, the times, the
codes, the agreement of symbols and codes, the aux texts. The file is
written to a temporary file, as writeRecord writes its files, which
replaces the existing file only when it is complete (and, if needed,
sorted); an error that the library reports while writing it is an error
(“write error on annotator ANN for record REC”). As in C, the subtype, channel and annotator number are stored
in one byte each (other values wrap around). An Annotations object can also be written
as it is: writeAnnotations("rand", "atr2", ann). If the annotations are
not in time order, the file is sorted when it is written. sort = false
keeps the given order (without the library’s advice to run sortann) and
sort = true always sorts, for that call only. Without sort, 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:
readMetadata("rec") // [(Age, 63), (Sex, F)]
getMetadata("rec", "age") // "63" (null if there is none)
setMetadata("rec", "Age", "64") // replaces the line, or adds "# Age: 64"
removeMetadata("rec", "Sex") // 1: the number of lines removed
updateMetadata("rec", mapOf("Age" to "65", "Dx" to null)) // several changes; null removes
getinfo reads it) that contains a colon. The record line is
the first line that holds something other than white space and does not
begin (after white space) with #, as the header parser finds it, in
every mode; comments and blank lines before it are never read or
changed. 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). readMetadata returns a
List<Pair<String, String>>, in file order; text is one character per
byte (ISO-8859-1), as elsewhere in the high-level API."age" finds
Age. A key must not be empty, and must not contain :, # or a line
break; 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 (“header of record
REC has more than one entry for key KEY (lines 3, 7); the first is
used”), printed on the standard error as WFDB-x: followed by the
message unless wfdbquiet() was called, or given to
onWarning = { msg -> ... }; setMetadata and updateMetadata refuse
to change that key, and removeMetadata 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,
readable and writable by its owner only until it is complete, then
renamed over it, keeping its permissions), and all the arguments are
checked first, so an error leaves it unchanged. If nothing changes,
nothing is written.updateMetadata takes a list of pairs,
listOf("Age" to "65", "Dx" to null), or a map; the changes are applied
in order, and a null value removes the entry. No two keys may differ
only in letter case.readHeader finds it (record,
inputDir and network work the same way, for one call; see
Record names). A change is written to the
header itself, or, with dir = ..., to a header of the same name in that
directory, which must exist (the signal files are not copied). 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.dir, an empty
record name or "-") throws a WfdbArgumentException (a subclass of
WfdbException), such as “metadata key ‘a:b’ must not contain ‘:’”. A
header that cannot be read or written, or a dir that does not exist,
throws a WfdbException: “can’t read header for record REC”, “can’t
write header for record REC”, “directory DIR does not exist”.Metadata often holds information about patients. WFDB-x stores it as plain
text, exactly as given, and does not de-identify it. The KDoc comments of
Metadata.kt give the full rules.
WfdbException, whose
message says what failed, such as “can’t read record 100s”. The header
metadata functions, the writers (writeRecord, writeAnnotations) and
the checks of readAnnotations throw its subclass
WfdbArgumentException for an invalid argument.readRecord never
returns an incomplete frame); checkRecord reports such a file as
length_short (“the signal files end after N frames, before the M given
in the header”). A checksum error alone stays a warning (below).
readRecord allocates at most 2^26 values in advance, whatever length the
header gives, and grows its array as the frames are read.WfdbException. For example, a time string such as
"0:10" needs the sampling frequency (“sampling frequency of record REC is
not specified”), and physical = true needs the gain of each signal
(“signal 1 of record REC is uncalibrated”).readRecord
returns all the data and warns: “checksum error in record REC: the signal
data do not match the checksums in its header”. By default the warning is
printed on the standard error, as WFDB-x: followed by the message,
unless wfdbquiet() was called; onWarning = { msg -> ... } receives it
instead, and checksumWarning = false turns it off. rec.checksumOk is
false either way. Checksums are only checked when a read reaches the end
of the record.writeRecord
reads nothing, so it replaces any existing files of the same name.writeRecord) or “write error
on annotator ANN for record REC” (writeAnnotations), also in legacy
mode; the existing files are then left as they were, and no temporary
file is left.writeRecord checks sampsPerFrame before anything else: each value
must be at least 1 (“writeRecord: sampsPerFrame must hold integers >=
1”), and the number of samples must be a multiple of the frame size.The high-level functions call wfdbquit(), which closes anything opened
with the WFDB library functions on the same Wfdb state. 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); fs of writeAnnotations applies to that file
only.
checkRecordval report = checkRecord("100s", listOf("atr"))
println(report)
checkRecord(record, annotators) checks the header of a record, its signal
specifications, every sample of its signal files, and the annotation files
named (record, inputDir and network work as in readRecord). It
reports each problem it finds. It never throws an exception for a problem
in the record. 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") 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, 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 list. 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.
The high-level functions have the same behavior in every language of WFDB-x, under the naming conventions of each:
| Kotlin | Python, C++, Rust, Julia, R, MATLAB/Octave |
|---|---|
readRecord |
read_record |
readHeader |
read_header |
readAnnotations |
read_annotations |
writeRecord (IntArray) |
write_record |
writeRecord (DoubleArray) |
write_record(..., physical=True) (Python, R, MATLAB), write_record_physical (C++, Rust, Julia) |
writeAnnotations |
write_annotations |
checkRecord |
check_record |
readMetadata, getMetadata, setMetadata, removeMetadata, updateMetadata |
read_metadata, get_metadata, set_metadata, remove_metadata, update_metadata |
wfdbxSetInputDir, wfdbxGetInputDir |
wfdbx_set_input_dir, wfdbx_get_input_dir |
wfdbxSetOutputDir, wfdbxGetOutputDir |
wfdbx_set_output_dir, wfdbx_get_output_dir |
wfdbxSetNetwork, wfdbxGetNetwork |
wfdbx_set_network, wfdbx_get_network |
wfdbxSetLegacy, wfdbxGetLegacy |
wfdbx_set_legacy, wfdbx_get_legacy |
from, to (TimeSpec) |
sampfrom, sampto (Python, R, MATLAB); from, to (C++, Rust, Julia) |
inputDir, checksumWarning, invalidWarning, sampsPerFrame, baseTime |
input_dir, checksum_warning, invalid_warning, samps_per_frame, base_time |
Record.checksumOk, baseTimeMs, baseDate |
checksum_ok, base_time_ms, base_date |
WfdbArgumentException (a WfdbException) |
ValueError (Python) for the header metadata functions |
WfdbException |
IOError/ValueError (Python), wfdbx::Error (C++), wfdbx::record::Error (Rust), WfdbError (Julia), an R error, a MATLAB error |
The WFDB library functions (isigopen, getvec, …) have their C names in
every language, Kotlin included.
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 |
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 |
wfdbxSetInputDir(d) |
inputDir = |
WFDBX_INPUT_DIR |
| output directory | wfdbxSetOutputDir(d) |
dir = |
WFDBX_OUTPUT_DIR |
| network access (off by default) | wfdbxSetNetwork(true) |
network = true |
WFDBX_NETWORK=1 |
A function call overrides the environment variable, and an option applies to
that one call. wfdbxGetInputDir(), wfdbxGetOutputDir() and
wfdbxGetNetwork() return the current settings; each set function returns
the previous setting, and passing null to wfdbxSetInputDir or
wfdbxSetOutputDir restores the default.
On Android and iOS, the current directory is not a useful place for files:
set the input and output directories to a directory of the app, such as
context.filesDir.path on Android.
In this example, /data/mitdb stands for a local copy of the MIT-BIH
Arrhythmia Database, and 100 is its record 100:
wfdbxSetInputDir("/data/mitdb")
val rec = readRecord("100") // reads /data/mitdb/100.hea and 100.dat
writeAnnotations("100", "qrs", longArrayOf(100, 460), symbols = listOf("N", "N"),
dir = "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 and the
writers, writeRecord and writeAnnotations, 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 inputDir 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: writeRecord("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 ~. Record, annotator and directory
names are converted to bytes with the platform’s file name encoding; text in
the files (descriptions, units, info strings, aux strings) is read and
written as ISO-8859-1, one character per byte, so that every byte
round-trips.
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 from
PhysioNet:
val rec = readRecord("100", inputDir = "https://physionet.org/files/mitdb/1.0.0/", network = true)
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. On the JVM and Android, files are downloaded with
java.net.HttpURLConnection (an Android app needs the INTERNET
permission); on iOS, with NSURLSession. 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
writeRecord, as the fs argument of readAnnotations, 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.
The tools run on the JVM, through the launcher bin/wfdbx (a POSIX shell
script that runs java -cp build/libs/wfdbx-all.jar wfdbx.cli.MainKt TOOL
...; build the jar first with ./gradlew fatJar, which also makes
build/libs/wfdbx-all.jsa, a class data sharing archive with which the JVM
starts faster). A link to bin/wfdbx named after a tool runs that tool.
These examples, run in the kotlin directory, read the sample record:
export WFDBX_INPUT_DIR=../tests/data
bin/wfdbx rdsamp -r 100s -t 10 -p # print the first 10 seconds, in physical units
bin/wfdbx rdann -r 100s -a atr # print the annotations
bin/wfdbx --list # list the tools
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
bin/wfdbx 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, bin/wfdbx wrsamp -F 250 -G 200 -R 16 -U mV -i samples.txt -o rec
writes a new record rec from the text file samples.txt.
The tools are also functions of the library (wfdbx.apps: w.rdsampMain(argv),
…), but they may end the process (with exit, as C programs do); use the
high-level functions or the library functions in an app.
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. Where a C function fills an array, the Kotlin function fills an array passed by the caller:
import wfdbx.*
import wfdbx.cstd.latin1
val w = Wfdb() // the library state
w.wfdbx_set_input_dir("tests/data")
val si = Array(2) { Siginfo() }
val nsig = w.isigopen("100s", si, 2) // open 2 signals; si receives their specifications
val v = IntArray(nsig)
while (w.getvec(v) > 0) { // v receives one sample per signal
// ...
}
val a = Annotation()
w.annopen("100s", arrayOf(Anninfo("atr", WFDB_READ)), 1)
while (w.getann(0, a) == 0) {
println("${a.time} ${w.annstr(a.anntyp).latin1()}")
}
w.wfdbquit()
Differences from C that come from the language:
Wfdb object, and each function is an extension function of it
(w.isigopen(...)). Every function is also a top-level function with the
same name and arguments, such as isigopen(...), which uses a default
Wfdb, one per thread. Separate Wfdb objects do not share any state,
so several records can be open at once, in one thread or in several; a
Wfdb object must be used by one thread at a time.ByteArray), as in C: WFDB files are not UTF-8. Most
functions also take a String (one character per byte, ISO-8859-1);
"..".latin1() and bytes.latin1() (in wfdbx.cstd) convert. C’s
NULL is null.int and WFDB_Sample are Int, long and WFDB_Time are Long,
and double is Double. The char fields of the structures are Int
properties that keep C’s range (anntyp, subtyp, num: -128 to 127;
chan: 0 to 255).Siginfo, Calinfo, Anninfo, Annotation,
Seginfo) have the C field names and C’s zero values. An annotation’s
aux is null, or bytes whose first byte is the length of the text that
follows, as in C.getvecs(out, n) reads up to n frames at once into out. 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.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
(readRecord, readHeader, readAnnotations, writeRecord,
writeAnnotations, checkRecord) leave both as they were.wfdbx_input_path(name) and wfdbx_output_path(name) (WFDB-x) return
the path under which a program opens a file that it reads or creates
itself, where the library would: under the input or output directory
(the missing directories of an output path are created), ~ expanded,
a full path as given; null for a URL; "-" as given. In legacy mode,
an input name is used as given, and the output directory is a prefix of
any output name.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.printf and scanf families are in wfdbx.cstd (cformat,
sscanf, …), formatting numbers exactly as the GNU C library does.The annotation codes (NORMAL, PVC, …) and the functions of
<wfdb/ecgmap.h> (isqrs, map1, map2, …) are also provided. The
functions are documented in KDoc comments.
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
../../kotlin/bin/wfdbx 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;wfdbxSetLegacy(true), or w.wfdbxSetLegacy(true)
for one Wfdb object.DIVERGENCES.md describes what changes in legacy mode.
iosArm64, iosSimulatorArm64, iosX64) are
declared, with a small platform layer for iOS (POSIX files and
NSURLSession). They can only be built on macOS. The tests of the
repository build the JVM code, and check the common code, which is all of
the library’s logic, with ./gradlew compileCommonMainKotlinMetadata; they
do not compile the native code (iOS, and Linux through
-Pwfdbx.linux=true).
Legacy-mode sorting of annotation files runs the sortann program in a
new JVM (java -cp ...), which an Android or iOS app cannot do (the
in-process sort of normal mode works everywhere).java.nio.file; give it paths in the app’s storage.Wfdb per thread. Use
separate Wfdb objects to work on several records in parallel.readRecord and writeRecord work on whole blocks of
samples, and are the fastest way to read and write a record. For code
that processes a record as a stream, getvecs reads many frames per
call, and is faster than a getvec loop. On the JVM, the first call in a
process is slower than the next ones, which run code compiled by the JIT
compiler.
BENCHMARKS.md
compares the seven languages and wfdb-python.From the kotlin directory of the repository:
./gradlew jvmTest # the unit tests, on the JVM
./gradlew compileCommonMainKotlinMetadata # the common code uses only common APIs
The unit tests check the library functions with expected values printed by
the C library, the high-level functions, the bulk paths against the
per-frame paths, the header metadata functions against the cases shared by
all the languages (tests/metadata), and checkRecord against the reports of the Python
implementation on the corpus made by python/tests/check_corpus.py (this
test is skipped, with a message, when python3 with NumPy is not
available). Network access is tested against a local HTTP server. To run
the cross-language conformance suite, which compares the tools with the
original C tools, build the jar and run the harness from the root of the
repository:
(cd kotlin && ./gradlew fatJar)
python3 tests/conformance/harness.py check --impl kotlin -j8
python3 tests/conformance/harness.py check --impl kotlin -j8 --legacy # legacy mode
DEVELOPING.md describes how the implementation 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 file names the authors of the C
code it implements, and
AUTHORS.md
gives the details. The Kotlin implementation was developed by Reza Sameni
(2026), through AI-assisted development, cross-language
verification and unit testing.
WFDB-x is an independent reimplementation. Its Kotlin code is a translation of the C code of the WFDB Software Package into Kotlin, 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 FLAC decoder is translated from libFLAC, and carries libFLAC’s
BSD-3-Clause notice; NOTICE
gives the licenses and that notice, and is in META-INF of every jar, with
LICENSE and LICENSE-GPL.