Repository navigation
Commit 9475b8a
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>21 files changed
Lines changed: 1282 additions & 0 deletions
File tree
- changelog.d
- docs
- src/wfdb/processing
- tests
- target-output/bxb
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| 11 | + | |
11 | 12 | | |
12 | 13 | | |
13 | 14 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
49 | 49 | | |
50 | 50 | | |
51 | 51 | | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
| 9 | + | |
9 | 10 | | |
10 | 11 | | |
11 | 12 | | |
| |||
0 commit comments