Skip to content

Commit 9475b8a

Browse files
authored
Add beat-by-beat comparison of annotations, as WFDB bxb does (development feature) (#679)
Addresses #334. This adds a beat-by-beat comparison of annotation files, as the `bxb` program of the WFDB Software Package does for ANSI/AAMI EC57 and EC38, as a **development feature**: the docs and docstrings say so, that its interface may change, and that results should be checked against `bxb` before they are used for reporting. ```python from wfdb.processing import bxb_record, sumstats result = bxb_record("sample-data/100", "atr", "qrs") # as bxb -r 100 -a atr qrs print(result.report()) # the standard report, as bxb -S result.matrix # the comparison matrix, as a DataFrame result.statistics() # each statistic as (numerator, denominator) line, shutdown_line = result.lines() # the line-format reports, as bxb -L summary, shutdown_summary = sumstats([result, ...]) # as WFDB sumstats ``` `processing.bxb(reference, test, ...)` does the same for `Annotation` objects, with `start`, `stop` and `window` (as `bxb -f`, `-t` and `-w`; `stop=0` is `-t 0`, which stops at the end of either file). ## How it was written `bxb.c` is licensed under the GPL, so this was written **without reference to its source code** (or that of the WFDB library). It is based on: - the EC57 beat-by-beat definitions; - the `bxb`, `sumstats` and evaluation documentation; - running the compiled `bxb` and `sumstats` (WFDB 10.7.0) as black boxes on many inputs, and adjusting until the results agreed. Running the programs to compare outputs doesn't make this code derived from them. The module docstring says the same. ## Validation All of these compared every matrix cell, every statistic, the RMS RR interval error and the shutdown time, as well as the full report text and both line-format lines, character for character: - **About 6000 generated cases**, all identical. The cases mixed: - jitter across the match window, missed and extra beats, and beats at the same sample; - non-beat annotations, and VF episodes in either file; - paired and AHA-style shutdowns in either file, and beats near the start and end. A third were at 360 Hz with the default options; the rest at 128 to 1000 Hz, with and without a record length, and with random `-f`, `-t` (including `-t 0`) and `-w`. - **All 48 MIT-BIH records** against five test annotators: `sqrs` and `gqrs` from the WFDB tools, `xqrs` from this package, a perturbed copy of the reference keeping every annotation type and subtype, and the reference itself. The reports, the line-format files and the `sumstats` output of both files were identical for every annotator. - **A record of 130,000 beats**, to check column widths. Some of `bxb`'s behaviour that this reproduces: - **Escape beats are SVEBs:** `e`, `j` and `n` beats count as S. - **Beats missed in shutdown:** the total leaves out S beats, but its denominator includes them. - **Positive predictivity:** extra test beats (O and X) count against the VEB and SVEB figures, F beats called V don't count against VEB, and F (but not Q) beats called S do count against SVEB. `sumstats`' gross figure leaves F out, because its columns combine F with Q. - **RR intervals:** each side forgets its previous beat at a shutdown or VF episode in its own file. - **The first test beat after the start** isn't counted if it is within the match window of the start and the next event also has a test beat. - **Without a record length**, the comparison ends at the last reference beat. - **Annotation times are converted to the record's sampling frequency** when the file has another time resolution, as the WFDB library does. The output of `sqrs` is stored at 250 Hz. Not included: the `-s`, `-l`, `-c` and `-C` report variants, the `-o` and `-O` output annotation files, and `-v`. ## Testing - `tests/test_beat_comparison.py` compares four cases with reports made by the C programs (fixtures in `tests/target-output/bxb`, with the annotation files used): record 100 against `100.qrs`; a 360 Hz record with VF and shutdowns in both files; a 250 Hz record compared with `start`, `stop` and `window`, whose test file has 1000 Hz times; and a test file that ends early, compared with `stop=0` (`bxb -t 0`). It also compares both line-format files and both `sumstats` outputs, and tests the matrix, statistics, `stop=0`, symbol-only annotations, an undefined RR error and a missing sampling frequency. - `test_matches_wfdb_bxb` compares 200 generated cases with the installed `bxb`, and is skipped when it isn't installed (as in CI). With WFDB 10.7.0, all 200 match. - `pytest`: all offline and network tests pass, also with `--resolution lowest-direct`. Ruff and the docs build pass. <sub>@tompollard 🟥 red 🦥 sloth · aspen</sub>
2 parents c7d7acc + 1939664 commit 9475b8a

21 files changed

Lines changed: 1282 additions & 0 deletions

‎.gitattributes‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
*.gz binary
99
*.mat binary
1010
*.qrs binary
11+
*.tst binary
1112
*.wabp binary
1213
*.wav binary
1314
*.wqrs binary

‎changelog.d/bxb.rst‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
**Beat-by-beat comparison of annotations, as WFDB bxb does (development feature)**
2+
``processing.bxb`` and ``processing.bxb_record`` compare a test annotation
3+
file with a reference annotation file beat by beat, as the ``bxb`` program
4+
of the WFDB Software Package does for the ANSI/AAMI EC57 and EC38
5+
standards. The result holds the comparison matrix and the QRS, VEB and SVEB
6+
sensitivity and positive predictivity, RMS RR interval error and shutdown
7+
statistics, and gives the standard and line-format reports of ``bxb``.
8+
``processing.sumstats`` combines the results of several records, as WFDB
9+
``sumstats`` does. These functions were written from the standards and the
10+
documentation of ``bxb``, and by comparing results with it, without
11+
reference to its source code. They are a development feature: their
12+
interface may change, and their results should be checked against ``bxb``
13+
before they are used for reporting.

‎docs/processing.rst‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,24 @@ Annotation Evaluators
4949
.. automodule:: wfdb.processing
5050
:no-index:
5151
:members: Comparitor, compare_annotations, benchmark_mitdb
52+
53+
54+
Beat-by-beat Comparison
55+
-----------------------
56+
57+
.. warning::
58+
59+
These functions are a development feature. They compare annotation
60+
files as the ``bxb`` and ``sumstats`` programs of the WFDB Software
61+
Package do, and were written from the ANSI/AAMI EC57 standard and the
62+
documentation of those programs, without reference to their source code.
63+
Their interface may change, and results should be checked against
64+
``bxb`` before they are used for reporting.
65+
66+
.. automodule:: wfdb.processing
67+
:no-index:
68+
:members: bxb, bxb_record, sumstats
69+
70+
.. autoclass:: wfdb.processing.BxbResult
71+
:members: count, matrix, statistics, rms_rr_error, shutdown_seconds,
72+
report, line_columns, lines

‎src/wfdb/processing/__init__.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
resample_sig,
77
resample_singlechan,
88
)
9+
from wfdb.processing.beat_comparison import BxbResult, bxb, bxb_record, sumstats
910
from wfdb.processing.evaluate import (
1011
Comparitor,
1112
benchmark_mitdb,

0 commit comments

Comments
 (0)