From 54c900c76669ab833b38e42a01e95b0bf2dfba04 Mon Sep 17 00:00:00 2001 From: Tom Pollard Date: Mon, 23 Nov 2026 21:17:00 -0500 Subject: [PATCH 1/2] Add beat-by-beat comparison of annotations, as WFDB bxb does processing.bxb and processing.bxb_record compare a test annotation file with a reference one beat by beat, as the bxb program of the WFDB Software Package does for ANSI/AAMI EC57 and EC38: the comparison matrix of AAMI beat classes, QRS, VEB and SVEB sensitivity and positive predictivity, the RMS RR interval error and shutdown statistics, and bxb's standard (-S) and line-format (-L) reports. processing.sumstats combines several records' results as WFDB sumstats does. This is a development feature. It was written from the descriptions in EC57 and the bxb documentation, and by comparing its results with those of the bxb and sumstats programs of WFDB 10.7.0, without reference to their source code, which is licensed under the GPL. The tests compare its reports with ones made by those programs. Addresses #334. --- .gitattributes | 1 + changelog.d/bxb.rst | 13 + docs/processing.rst | 21 + src/wfdb/processing/__init__.py | 1 + src/wfdb/processing/beat_comparison.py | 829 ++++++++++++++++++++++ tests/target-output/bxb/100-report.txt | 29 + tests/target-output/bxb/bxb.out | 4 + tests/target-output/bxb/bxb250-report.txt | 29 + tests/target-output/bxb/bxb250.atr | Bin 0 -> 814 bytes tests/target-output/bxb/bxb250.hea | 2 + tests/target-output/bxb/bxb250.tst | Bin 0 -> 670 bytes tests/target-output/bxb/bxb360-report.txt | 29 + tests/target-output/bxb/bxb360.atr | Bin 0 -> 816 bytes tests/target-output/bxb/bxb360.hea | 2 + tests/target-output/bxb/bxb360.tst | Bin 0 -> 792 bytes tests/target-output/bxb/sd.out | 5 + tests/target-output/bxb/sumstats-bxb.txt | 11 + tests/target-output/bxb/sumstats-sd.txt | 9 + tests/test_beat_comparison.py | 105 +++ 19 files changed, 1090 insertions(+) create mode 100644 changelog.d/bxb.rst create mode 100644 src/wfdb/processing/beat_comparison.py create mode 100644 tests/target-output/bxb/100-report.txt create mode 100644 tests/target-output/bxb/bxb.out create mode 100644 tests/target-output/bxb/bxb250-report.txt create mode 100644 tests/target-output/bxb/bxb250.atr create mode 100644 tests/target-output/bxb/bxb250.hea create mode 100644 tests/target-output/bxb/bxb250.tst create mode 100644 tests/target-output/bxb/bxb360-report.txt create mode 100644 tests/target-output/bxb/bxb360.atr create mode 100644 tests/target-output/bxb/bxb360.hea create mode 100644 tests/target-output/bxb/bxb360.tst create mode 100644 tests/target-output/bxb/sd.out create mode 100644 tests/target-output/bxb/sumstats-bxb.txt create mode 100644 tests/target-output/bxb/sumstats-sd.txt create mode 100644 tests/test_beat_comparison.py diff --git a/.gitattributes b/.gitattributes index 22ece28c..1b91e686 100644 --- a/.gitattributes +++ b/.gitattributes @@ -8,6 +8,7 @@ *.gz binary *.mat binary *.qrs binary +*.tst binary *.wabp binary *.wav binary *.wqrs binary diff --git a/changelog.d/bxb.rst b/changelog.d/bxb.rst new file mode 100644 index 00000000..2de3c844 --- /dev/null +++ b/changelog.d/bxb.rst @@ -0,0 +1,13 @@ +**Beat-by-beat comparison of annotations, as WFDB bxb does (development feature)** + ``processing.bxb`` and ``processing.bxb_record`` compare a test annotation + file with a reference annotation file beat by beat, as the ``bxb`` program + of the WFDB Software Package does for the ANSI/AAMI EC57 and EC38 + standards. The result holds the comparison matrix and the QRS, VEB and SVEB + sensitivity and positive predictivity, RMS RR interval error and shutdown + statistics, and gives the standard and line-format reports of ``bxb``. + ``processing.sumstats`` combines the results of several records, as WFDB + ``sumstats`` does. These functions were written from the standards and the + documentation of ``bxb``, and by comparing results with it, without + reference to its source code. They are a development feature: their + interface may change, and their results should be checked against ``bxb`` + before they are used for reporting. diff --git a/docs/processing.rst b/docs/processing.rst index 988211cd..960c2359 100644 --- a/docs/processing.rst +++ b/docs/processing.rst @@ -49,3 +49,24 @@ Annotation Evaluators .. automodule:: wfdb.processing :no-index: :members: Comparitor, compare_annotations, benchmark_mitdb + + +Beat-by-beat Comparison +----------------------- + +.. warning:: + + These functions are a development feature. They compare annotation + files as the ``bxb`` and ``sumstats`` programs of the WFDB Software + Package do, and were written from the ANSI/AAMI EC57 standard and the + documentation of those programs, without reference to their source code. + Their interface may change, and results should be checked against + ``bxb`` before they are used for reporting. + +.. automodule:: wfdb.processing + :no-index: + :members: bxb, bxb_record, sumstats + +.. autoclass:: wfdb.processing.BxbResult + :members: count, matrix, statistics, rms_rr_error, shutdown_seconds, + report, line_columns, lines diff --git a/src/wfdb/processing/__init__.py b/src/wfdb/processing/__init__.py index 2aadc09d..bbe31401 100644 --- a/src/wfdb/processing/__init__.py +++ b/src/wfdb/processing/__init__.py @@ -6,6 +6,7 @@ resample_sig, resample_singlechan, ) +from wfdb.processing.beat_comparison import BxbResult, bxb, bxb_record, sumstats from wfdb.processing.evaluate import ( Comparitor, benchmark_mitdb, diff --git a/src/wfdb/processing/beat_comparison.py b/src/wfdb/processing/beat_comparison.py new file mode 100644 index 00000000..e9be68a9 --- /dev/null +++ b/src/wfdb/processing/beat_comparison.py @@ -0,0 +1,829 @@ +"""Beat-by-beat comparison of annotation files, as done by the WFDB bxb program. + +.. warning:: + + This is a development feature. Its interface may change, and its results + should be checked against the WFDB ``bxb`` program before they are used + for reporting. + +The comparison follows the beat-by-beat method of ANSI/AAMI EC57 (and +EC38), as implemented by ``bxb`` in the WFDB Software Package. This code was +written from the descriptions in those standards and in the ``bxb`` +documentation, and by comparing its results with those of ``bxb`` on many +generated and real annotation files. It does not use or derive from the +``bxb`` source code, which is licensed under the GPL. + +""" + +import bisect +import math +from dataclasses import dataclass, field + +import numpy as np +import pandas as pd + +__all__ = ["BxbResult", "bxb", "bxb_record", "sumstats"] + +# AAMI beat classes of the WFDB annotation codes (label_store values) that +# are beats. Other codes are not beats. +_AAMI_CLASSES = { + "N": (1, 2, 3, 25), # NORMAL, LBBB, RBBB, BBB + "S": (4, 7, 8, 9, 11, 34, 35), # ABERR, NPC, APC, SVPB, NESC, AESC, SVESC + "V": (5, 10, 41), # PVC, VESC, RONT + "F": (6,), # FUSION + "Q": (12, 13, 30, 38), # PACE, UNKNOWN, LEARN, PFUS +} +_CLASS_OF_CODE = {code: c for c, codes in _AAMI_CLASSES.items() for code in codes} +_NOISE, _VFON, _VFOFF = 14, 32, 33 + +# Rows are reference classes (O: no reference beat; X: no reference beat, +# during a reference shutdown), and columns are test classes (o: no test +# beat; x: no test beat, during a test shutdown) +ROWS = "NSVFQOX" +COLUMNS = "nsvfqox" + + +@dataclass(frozen=True) +class _Beat: + time: int + cls: str + index: int # position in the annotation file + + +@dataclass(frozen=True) +class _Interval: + start: float + end: float + index: int # position of the annotation that starts it + resets: bool = True # whether it resets the previous beat for RR intervals + + +def _beats_and_intervals(sample, label_store, subtype, window): + """Read the beats, VF intervals and shutdown intervals of one file. + + Beats between a VF onset (``[``) and offset (``]``), in file order, are + not beats. A shutdown starts at a NOISE annotation whose subtype has both + the 16 and 32 bits set, and ends at the next NOISE annotation. If the + next annotation is not a NOISE annotation, the shutdown marks the middle + of an unreadable interval (the AHA database convention), which is taken + to start `window` after the previous annotation and to end `window` + before the next one. + + """ + beats, vf, shutdowns = [], [], [] + in_vf, vf_start = False, None + n = len(sample) + for i in range(n): + code, t = int(label_store[i]), int(sample[i]) + if code == _VFON: + if not in_vf: + in_vf, vf_start = True, i + elif code == _VFOFF: + if in_vf: + vf.append(_Interval(int(sample[vf_start]), t, vf_start)) + in_vf = False + elif code in _CLASS_OF_CODE and not in_vf: + beats.append(_Beat(t, _CLASS_OF_CODE[code], i)) + if code == _NOISE and (int(subtype[i]) & 48) == 48: + if ( + i + 1 < n + and int(label_store[i + 1]) == _NOISE + and (int(subtype[i + 1]) & 48) != 48 + ): + shutdowns.append(_Interval(t, int(sample[i + 1]), i)) + elif i + 1 < n: + start = int(sample[i - 1]) + window if i > 0 else 0 + end = int(sample[i + 1]) - window + # The previous beat is forgotten only if the next annotation + # comes after the start of the shutdown + shutdowns.append( + _Interval(start, max(start, end), i, int(sample[i + 1]) > start) + ) + else: + shutdowns.append(_Interval(t, math.inf, i)) + if in_vf: + vf.append(_Interval(int(sample[vf_start]), math.inf, vf_start)) + # A shutdown that starts during VF in the same file is ignored + shutdowns = [s for s in shutdowns if not _in(s.start, vf)] + return beats, vf, shutdowns + + +def _in(t, intervals, closed=False): + """Whether time `t` is inside one of `intervals`.""" + if closed: + return any(iv.start <= t <= iv.end for iv in intervals) + return any(iv.start <= t < iv.end for iv in intervals) + + +def _label_store(annotation): + """The annotation codes of an Annotation, from its symbols if needed.""" + if annotation.label_store is not None: + return np.asarray(annotation.label_store) + codes = annotation.convert_label_attribute("symbol", "label_store", inplace=False) + return np.asarray(codes) + + +@dataclass +class BxbResult: + """The result of a beat-by-beat comparison of two annotation files. + + Attributes + ---------- + counts : dict + For each row of the comparison matrix (reference class: N, S, V, F, + Q, or O and X for test beats with no reference beat, outside and + during a reference shutdown), the counts in each column (test class: + n, s, v, f, q, or o and x for reference beats with no test beat, + outside and during a test shutdown). Rows O and X have no o and x + columns. + rr_errors : list of float + The differences between the reference and test RR intervals, in + samples, that are used for the RMS RR interval error. + shutdown : float + The total time in test shutdown, in samples. + fs : float + The sampling frequency. + record_name : str + The name of the record, used in reports. + reference : str + The name of the reference annotator, used in reports. + test : str + The name of the test annotator, used in reports. + + """ + + counts: dict + rr_errors: list + shutdown: float + fs: float + record_name: str = "" + reference: str = "" + test: str = "" + _events: list = field(default_factory=list, repr=False) + + def count(self, rows, columns): + """The sum of the counts in the given rows and columns.""" + total = 0 + for r in rows: + for c in columns: + j = COLUMNS.index(c) + if j < len(self.counts[r]): + total += self.counts[r][j] + return total + + @property + def matrix(self): + """The comparison matrix, as a DataFrame (rows O and X have no o, x).""" + data = { + c: [self.counts[r][j] if j < len(self.counts[r]) else pd.NA for r in ROWS] + for j, c in enumerate(COLUMNS) + } + return pd.DataFrame(data, index=list(ROWS), dtype="Int64") + + def statistics(self): + """The statistics of the standard report, as (numerator, denominator). + + Returns + ------- + dict + The QRS, VEB and SVEB sensitivity and positive predictivity, and + the beats missed in shutdown, each as a (numerator, denominator) + pair of beat counts. + + """ + g = self.count + tp = g("NSVFQ", "nsvfq") + return { + "QRS sensitivity": (tp, tp + g("NSVFQ", "ox")), + "QRS positive predictivity": (tp, tp + g("OX", "nsvfq")), + "VEB sensitivity": (g("V", "v"), g("V", COLUMNS)), + "VEB positive predictivity": (g("V", "v"), g("NSVOX", "v")), + "SVEB sensitivity": (g("S", "s"), g("S", COLUMNS)), + "SVEB positive predictivity": (g("S", "s"), g("NSVFOX", "s")), + "Beats missed in shutdown": (g("NVFQ", "x"), g("NSVFQ", COLUMNS)), + "N missed in shutdown": (g("N", "x"), g("N", COLUMNS)), + "S missed in shutdown": (g("S", "x"), g("S", COLUMNS)), + "V missed in shutdown": (g("V", "x"), g("V", COLUMNS)), + "F missed in shutdown": (g("F", "x"), g("F", COLUMNS)), + } + + @property + def rms_rr_error(self): + """The RMS RR interval error in milliseconds, or None if undefined.""" + if not self.rr_errors: + return None + ms = [e / self.fs * 1000 for e in self.rr_errors] + return math.sqrt(sum(x * x for x in ms) / len(ms)) + + @property + def shutdown_seconds(self): + """The total shutdown time in whole seconds, as bxb reports it.""" + return int((round(self.shutdown) + math.ceil(self.fs / 2)) // self.fs) + + def report(self): + """The standard report, in the format of ``bxb -S``. + + Returns + ------- + str + The comparison matrix and statistics. + + """ + lines = [ + f"Beat-by-beat comparison results for record {self.record_name}", + f"Reference annotator: {self.reference}", + f" Test annotator: {self.test}", + "", + " Algorithm", + " n s v f q o x", + " ____________________________________", + ] + for r in ROWS: + lines.append(f" {r} |" + "".join(f" {x:4d}" for x in self.counts[r])) + lines.append("") + stats = self.statistics() + + def stat(label, key): + num, den = stats[key] + if den: + return f"{label:>26}: {100 * num / den:6.2f}% ({num}/{den})" + return f"{label:>26}: - ({num}/{den})" + + for key in ( + "QRS sensitivity", + "QRS positive predictivity", + "VEB sensitivity", + "VEB positive predictivity", + "SVEB sensitivity", + "SVEB positive predictivity", + ): + lines.append(stat(key, key)) + rr = self.rms_rr_error + lines.append( + f"{'RMS RR interval error':>26}: " + + (f"{rr:6.2f} ms" if rr is not None else " -") + ) + lines.append("") + for key in ( + "Beats missed in shutdown", + "N missed in shutdown", + "S missed in shutdown", + "V missed in shutdown", + "F missed in shutdown", + ): + lines.append(stat(key, key)) + lines.append(f"{'Total shutdown time':>26}: {self.shutdown_seconds:5d} seconds") + return "\n".join(lines) + "\n" + + def line_columns(self): + """The beat counts of the line-format report, as in ``bxb -L``. + + Returns + ------- + dict + The counts named as in the report's heading: the primed columns + combine F with Q, O with X, and o with x. + + """ + g = self.count + return { + "Nn'": g("N", "nfq"), + "Sn'": g("S", "nfq"), + "Vn'": g("V", "nfq"), + "Fn'": g("FQ", "nfq"), + "On'": g("OX", "nfq"), + "Ns": g("N", "s"), + "Ss": g("S", "s"), + "Vs": g("V", "s"), + "Fs'": g("FQ", "s"), + "Os'": g("OX", "s"), + "Nv": g("N", "v"), + "Sv": g("S", "v"), + "Vv": g("V", "v"), + "Fv'": g("FQ", "v"), + "Ov'": g("OX", "v"), + "No'": g("N", "ox"), + "So'": g("S", "ox"), + "Vo'": g("V", "ox"), + "Fo'": g("FQ", "ox"), + } + + def lines(self): + """The two lines of the line-format reports, as in ``bxb -L``. + + Returns + ------- + tuple of str + The line of beat counts and statistics (for the first file, + often named ``bxb.out``), and the line of shutdown statistics + (for the second, ``sd.out``). The column headings of each file + are in ``wfdb.processing.beat_comparison.LINE_HEADINGS``. + + """ + cols = self.line_columns() + return _format_line1( + self.record_name, cols, self._line_stats(), self.rms_rr_error + ), (_format_line2(self.record_name, self)) + + def _line_stats(self): + """The six statistics of the first line-format report.""" + stats = self.statistics() + return [ + stats[key] + for key in ( + "QRS sensitivity", + "QRS positive predictivity", + "VEB sensitivity", + "VEB positive predictivity", + "SVEB sensitivity", + "SVEB positive predictivity", + ) + ] + + +LINE_HEADINGS = ( + "Record Nn' Sn' Vn' Fn' On' Ns Ss Vs Fs' Os' Nv Sv Vv Fv' Ov' No' " + "So' Vo' Fo' Q Se Q +P V Se V +P S Se S +P RR err\n", + "Record Nx Sx Vx Fx Qx % beats % N % S % V % F Total " + "Shutdown\n missed missed missed missed " + "missed Time\n", +) + + +def _pct(num, den, width=7): + """A percentage in a column of `width` (a space and the number), or "-".""" + return f" {100 * num / den:{width - 1}.2f}" if den else f" {'-':>{width - 1}}" + + +def _gross_stats(c): + """The gross statistics of sumstats, from the summed line-format counts.""" + tp = sum( + c[k] + for k in ( + "Nn'", + "Sn'", + "Vn'", + "Fn'", + "Ns", + "Ss", + "Vs", + "Fs'", + "Nv", + "Sv", + "Vv", + "Fv'", + ) + ) + fn = c["No'"] + c["So'"] + c["Vo'"] + c["Fo'"] + fp = c["On'"] + c["Os'"] + c["Ov'"] + veb = c["Vn'"] + c["Vs"] + c["Vv"] + c["Vo'"] + sveb = c["Sn'"] + c["Ss"] + c["Sv"] + c["So'"] + return [ + (tp, tp + fn), + (tp, tp + fp), + (c["Vv"], veb), + (c["Vv"], c["Nv"] + c["Sv"] + c["Vv"] + c["Ov'"]), + (c["Ss"], sveb), + # F and Q beats are combined in the line format, so unlike each + # record's statistic, this leaves out F beats called S + (c["Ss"], c["Ns"] + c["Ss"] + c["Vs"] + c["Os'"]), + ] + + +def _counts_text(c): + widths = [6] + [4] * 11 + [5] + [4] * 6 + return "".join(f" {c[k]:{w - 1}d}" for k, w in zip(_LINE1_KEYS, widths)) + + +_LINE1_KEYS = ( + "Nn'", + "Sn'", + "Vn'", + "Fn'", + "On'", + "Ns", + "Ss", + "Vs", + "Fs'", + "Os'", + "Nv", + "Sv", + "Vv", + "Fv'", + "Ov'", + "No'", + "So'", + "Vo'", + "Fo'", +) + + +def _format_line1(record_name, c, stats, rr): + stats = "".join(_pct(n, d) for n, d in stats) + rr_text = f" {rr:6.2f}" if rr is not None else f" {'-':>5}" + return f"{record_name:>4}" + _counts_text(c) + stats + rr_text + + +def _format_line2(record_name, result): + g = result.count + x = [g(r, "x") for r in "NSVFQ"] + totals = [g(r, COLUMNS) for r in "NSVFQ"] + missed = _pct(g("NVFQ", "x"), sum(totals), 9) + per_class = "".join(_pct(x[k], totals[k]) for k in range(4)) + return ( + f"{record_name:>4}" + + "".join(f" {v:4d}" for v in x) + + missed + + per_class + + f" {result.shutdown_seconds:4d} seconds " + + " ".join(str(t) for t in totals) + ) + + +def bxb( + reference, + test, + fs=None, + sig_len=None, + start=None, + stop=None, + window=0.15, + record_name=None, +): + """Compare two annotation files beat by beat, as the WFDB bxb program does. + + .. warning:: + + This is a development feature. It was written without reference + to the ``bxb`` source code (see ``wfdb.processing.beat_comparison``), + its interface may change, and its results should be checked against + ``bxb`` before they are used for reporting. + + The reference and test beats are matched if they are within `window` of + each other, and tallied by their ANSI/AAMI EC57 classes (N, S, V, F and + Q). Beats in ventricular flutter or fibrillation (between ``[`` and + ``]`` annotations) and test shutdowns (marked by NOISE annotations) are + handled as by ``bxb``. + + Parameters + ---------- + reference : Annotation + The reference annotations. + test : Annotation + The test annotations. + fs : float, optional + The sampling frequency. By default, the reference annotations' `fs`. + sig_len : int, optional + The length of the record, in samples. If given, and `stop` is not, + the comparison ends at the end of the record. Otherwise it ends at + the last reference annotation. + start : int, optional + The sample at which to start the comparison. By default, 5 minutes + after the start of the record, as EC57 requires. + stop : int, optional + The sample at which to stop the comparison (inclusive). + window : float, optional + The match window, in seconds. The default, 0.15, is the one that + EC57 requires. + record_name : str, optional + The record name to use in reports. By default, that of `reference`. + + Returns + ------- + BxbResult + The comparison matrix and the data for its statistics. + + Examples + -------- + >>> ref = wfdb.rdann('sample-data/100', 'atr') + >>> test = wfdb.rdann('sample-data/100', 'qrs') + >>> result = wfdb.processing.bxb(ref, test, sig_len=650000) + >>> print(result.report()) + + """ + fs = fs if fs is not None else reference.fs + if not fs: + raise ValueError("The sampling frequency is needed: pass fs") + w = int(window * fs + 0.5) + start = int(300 * fs + 0.5) if start is None else int(start) + files, samples = [], [] + for ann in (reference, test): + sample = np.asarray(ann.sample) + # Annotation times are converted to the sampling frequency of the + # comparison, as the WFDB library does, if the file has another one + if ann.fs and ann.fs != fs: + sample = np.floor(sample * (fs / ann.fs) + 0.5).astype(np.int64) + samples.append(sample) + codes = _label_store(ann) + subtype = ( + np.zeros(len(sample), dtype=int) + if ann.subtype is None + else np.asarray(ann.subtype) + ) + files.append(_beats_and_intervals(sample, codes, subtype, w)) + (R, rvf, rshut), (A, tvf, tshut) = files + if stop is None: + if sig_len: + stop = int(sig_len) + else: + stop = int(samples[0][-1]) if len(samples[0]) else 0 + + events = _match(R, A, w) + counts = {r: [0] * (5 if r in "OX" else 7) for r in ROWS} + rr_errors = [] + + # Each side forgets its previous beat (for RR intervals) at the start of + # a shutdown or VF interval in its own file + r_resets = sorted( + [(iv.start, iv.index) for iv in rshut if iv.resets] + + [(iv.start, iv.index) for iv in rvf] + ) + a_resets = sorted( + [(iv.start, iv.index) for iv in tshut if iv.resets] + + [(iv.start, iv.index) for iv in tvf] + ) + + # The first test beat after the start is not counted if it is within the + # match window of the start and the next event also has a test beat + k0 = bisect.bisect_left([a.time for a in A], start) + skipped = None + if k0 < len(A) and A[k0].time - start <= w: + for e, after in zip(events, events[1:]): + if e[0] == "fp" and e[2] is A[k0]: + if after[0] in ("fp", "match"): + skipped = A[k0] + break + + prev_r = prev_a = None + ri = ai = 0 + counted_events = [] + for kind, r, a, other in events: + t = r.time if r else a.time + while ri < len(r_resets) and _before(r_resets[ri], r, t): + prev_r = None + ri += 1 + while ai < len(a_resets) and _before(a_resets[ai], a, t): + prev_a = None + ai += 1 + if kind == "match": + counted = start <= r.time and min(r.time, a.time) <= stop + elif kind == "fn": + counted = start <= r.time <= stop + else: + # A test beat during reference VF is not counted (but is still + # the previous test beat) + counted = ( + start <= a.time <= stop + and a is not skipped + and not _in(a.time, rvf, closed=True) + ) + if counted: + counted_events.append((kind, r, a)) + if kind == "match": + counts[r.cls][COLUMNS.index(a.cls.lower())] += 1 + elif kind == "fn": + counts[r.cls][6 if _in(r.time, tshut, closed=True) else 5] += 1 + else: + row = "X" if _in(a.time, rshut, closed=True) else "O" + counts[row]["nsvfq".index(a.cls.lower())] += 1 + if prev_r is not None and prev_a is not None: + # For an unmatched beat, the other side's interval is the one + # ending at the beat it was compared with + nr = r.time if r else _next(other, r_resets, ri) + na = a.time if a else _next(other, a_resets, ai) + if nr is not None and na is not None and nr > prev_r and na > prev_a: + rr_errors.append((nr - prev_r) - (na - prev_a)) + if r: + prev_r = r.time + if a: + prev_a = a.time + + # Test annotations are read until the first reference beat after the end + # of the comparison, so a shutdown that starts later is not counted + rt = [b.time for b in R] + k = bisect.bisect_right(rt, stop) + cutoff = rt[k] if k < len(rt) else math.inf + shutdown = sum( + max(0, (iv.end if iv.end != math.inf else stop) - iv.start) + for iv in tshut + if iv.start <= stop + or int(samples[1][iv.index]) <= stop + or int(samples[1][iv.index]) < cutoff + ) + return BxbResult( + counts=counts, + rr_errors=rr_errors, + shutdown=shutdown, + fs=fs, + record_name=record_name + if record_name is not None + else (reference.record_name or ""), + reference=reference.extension or "", + test=test.extension or "", + _events=counted_events, + ) + + +def _match(R, A, w): + """Match reference and test beats, in time order. + + Returns a list of (kind, reference beat, test beat, other) events, where + kind is "match", "fn" (a reference beat with no test beat) or "fp" (a + test beat with no reference beat), and `other` is the beat on the other + side that an unmatched beat was compared with. + + """ + events = [] + i = j = 0 + while i < len(R) or j < len(A): + cur_r = R[i] if i < len(R) else None + cur_a = A[j] if j < len(A) else None + if cur_a is None or (cur_r is not None and cur_a.time > cur_r.time + w): + events.append(("fn", cur_r, None, cur_a)) + i += 1 + continue + if cur_r is None or cur_a.time < cur_r.time - w: + events.append(("fp", None, cur_a, cur_r)) + j += 1 + continue + r, a = cur_r, cur_a + # The next test beat is preferred if it is closer to the reference + # beat, or as close and this test beat comes before it + if j + 1 < len(A): + d, d_next = abs(a.time - r.time), abs(A[j + 1].time - r.time) + if d_next < d or (d_next == d and a.time < r.time): + events.append(("fp", None, a, r)) + j += 1 + continue + # The next reference beat takes this test beat if it is closer to + # it, unless the next test beat is closer still to that reference beat + if i + 1 < len(R) and abs(R[i + 1].time - a.time) < abs(r.time - a.time): + if not ( + j + 1 < len(A) + and abs(A[j + 1].time - R[i + 1].time) < abs(a.time - R[i + 1].time) + ): + events.append(("fn", r, None, a)) + i += 1 + continue + events.append(("match", r, a, None)) + i += 1 + j += 1 + return events + + +def _before(reset, beat, t): + """Whether a reset comes before a side's beat, or by time `t` if it has none. + + Resets and beats are ordered by time, and by file order at the same time. + + """ + time, index = reset + if beat is not None: + return (time, index) < (beat.time, beat.index) + return time <= t + + +def _next(beat, resets, ri): + """The time of `beat`, unless the next of `resets` comes before it.""" + if beat is None: + return None + if ri < len(resets) and resets[ri] < (beat.time, beat.index): + return None + return beat.time + + +def bxb_record( + record_name, + reference_extension, + test_extension, + pn_dir=None, + start=None, + stop=None, + window=0.15, +): + """Compare two annotation files of a record beat by beat, as bxb does. + + .. warning:: + + This is a development feature: see `bxb`. + + The sampling frequency and length of the record are read from its + header, as by ``bxb -r record -a reference test``. + + Parameters + ---------- + record_name : str + The name of the record, which may be a path or a URI. + reference_extension : str + The annotator of the reference annotations, such as 'atr'. + test_extension : str + The annotator of the test annotations. + pn_dir : str, optional + The PhysioNet database directory of the record, if it is on PhysioNet. + start : int, optional + The sample at which to start the comparison (by default, 5 minutes). + stop : int, optional + The sample at which to stop the comparison (by default, the end of + the record). + window : float, optional + The match window, in seconds (by default, 0.15). + + Returns + ------- + BxbResult + The comparison matrix and the data for its statistics. + + """ + from wfdb.io.annotation import rdann + from wfdb.io.record import rdheader + + header = rdheader(record_name, pn_dir=pn_dir) + kwargs = dict(pn_dir=pn_dir, return_label_elements=["label_store"]) + reference = rdann(record_name, reference_extension, **kwargs) + test = rdann(record_name, test_extension, **kwargs) + return bxb( + reference, + test, + fs=header.fs, + sig_len=header.sig_len, + start=start, + stop=stop, + window=window, + record_name=header.record_name, + ) + + +def sumstats(results): + """Combine the results of several records, as WFDB sumstats does. + + .. warning:: + + This is a development feature: see `bxb`. + + Parameters + ---------- + results : list of BxbResult + The results of each record. + + Returns + ------- + tuple of str + The summaries of the two line-format reports (``bxb.out`` and + ``sd.out``): the line of each record, the sums of their counts, and + for the first, the gross and average statistics. + + """ + lines1 = [r.lines()[0] for r in results] + lines2 = [r.lines()[1] for r in results] + cols = [r.line_columns() for r in results] + total = {k: sum(c[k] for c in cols) for k in _LINE1_KEYS} + + # Averages are of each record's statistics, recomputed (as sumstats does) + # from its line-format counts, ignoring undefined ones. RR errors are + # averaged as printed. + per_record = [_gross_stats(c) for c in cols] + averages = [] + for k in range(6): + values = [100 * n / d for n, d in (s[k] for s in per_record) if d] + averages.append( + f" {sum(values) / len(values):6.2f}" if values else f" {'-':>6}" + ) + rrs = [round(r.rms_rr_error, 2) for r in results if r.rms_rr_error is not None] + rr_avg = f" {sum(rrs) / len(rrs):6.2f}" if rrs else f" {'-':>6}" + gross = "".join(_pct(n, d) for n, d in _gross_stats(total)) + tp = _gross_stats(total) + pad = " " * (4 + 6 + 4 * 17 + 5) + out1 = ( + LINE_HEADINGS[0] + + "".join(line + "\n" for line in lines1) + + "_" * 132 + + "\n" + + "Sum " + + _counts_text(total) + + "\n" + + "Gross" + + pad[5:] + + gross + + "\n" + + "Average" + + pad[7:] + + "".join(averages) + + rr_avg + + "\n" + + f"Total QRS complexes: {tp[0][1]} Total VEBs: {tp[2][1]}" + + f" Total SVEBs: {tp[4][1]}\n" + + f"\nSummary of results from {len(results)} records\n" + ) + x_sums = [sum(r.count(c, "x") for r in results) for c in "NSVFQ"] + out2 = ( + LINE_HEADINGS[1] + + "".join(line + "\n" for line in lines2) + + "_" * 72 + + "\n" + + "Sum " + + "".join(f" {v:4d}" for v in x_sums) + + " " * 37 + + f" {sum(r.shutdown_seconds for r in results):4d} seconds\n" + + f"\nSummary of results from {len(results)} records\n" + ) + return out1, out2 diff --git a/tests/target-output/bxb/100-report.txt b/tests/target-output/bxb/100-report.txt new file mode 100644 index 00000000..bf1af1ab --- /dev/null +++ b/tests/target-output/bxb/100-report.txt @@ -0,0 +1,29 @@ +Beat-by-beat comparison results for record 100 +Reference annotator: atr + Test annotator: qrs + + Algorithm + n s v f q o x + ____________________________________ + N | 1872 0 0 0 0 0 0 + S | 29 0 0 0 0 0 0 + V | 1 0 0 0 0 0 0 + F | 0 0 0 0 0 0 0 + Q | 0 0 0 0 0 0 0 + O | 0 0 0 0 0 + X | 0 0 0 0 0 + + QRS sensitivity: 100.00% (1902/1902) + QRS positive predictivity: 100.00% (1902/1902) + VEB sensitivity: 0.00% (0/1) + VEB positive predictivity: - (0/0) + SVEB sensitivity: 0.00% (0/29) +SVEB positive predictivity: - (0/0) + RMS RR interval error: 1.96 ms + + Beats missed in shutdown: 0.00% (0/1902) + N missed in shutdown: 0.00% (0/1872) + S missed in shutdown: 0.00% (0/29) + V missed in shutdown: 0.00% (0/1) + F missed in shutdown: - (0/0) + Total shutdown time: 0 seconds diff --git a/tests/target-output/bxb/bxb.out b/tests/target-output/bxb/bxb.out new file mode 100644 index 00000000..2cc0031b --- /dev/null +++ b/tests/target-output/bxb/bxb.out @@ -0,0 +1,4 @@ +Record Nn' Sn' Vn' Fn' On' Ns Ss Vs Fs' Os' Nv Sv Vv Fv' Ov' No' So' Vo' Fo' Q Se Q +P V Se V +P S Se S +P RR err + 100 1872 29 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 100.00 100.00 0.00 - 0.00 - 1.96 +bxb360 119 6 10 74 12 6 56 0 1 5 3 3 58 4 6 14 10 11 11 88.08 93.66 73.42 82.86 74.67 82.35 445.35 +bxb250 70 4 9 34 9 3 34 0 4 1 6 1 34 1 5 34 25 23 29 64.31 93.02 51.52 73.91 53.12 87.18 232.15 diff --git a/tests/target-output/bxb/bxb250-report.txt b/tests/target-output/bxb/bxb250-report.txt new file mode 100644 index 00000000..5bee9ec8 --- /dev/null +++ b/tests/target-output/bxb/bxb250-report.txt @@ -0,0 +1,29 @@ +Beat-by-beat comparison results for record bxb250 +Reference annotator: atr + Test annotator: tst + + Algorithm + n s v f q o x + ____________________________________ + N | 68 3 6 2 0 9 25 + S | 1 34 1 0 3 6 19 + V | 4 0 34 2 3 14 9 + F | 0 1 0 12 1 4 5 + Q | 2 3 1 1 18 4 16 + O | 6 1 5 1 2 + X | 0 0 0 0 0 + + QRS sensitivity: 64.31% (200/311) + QRS positive predictivity: 93.02% (200/215) + VEB sensitivity: 51.52% (34/66) + VEB positive predictivity: 73.91% (34/46) + SVEB sensitivity: 53.12% (34/64) +SVEB positive predictivity: 87.18% (34/39) + RMS RR interval error: 232.15 ms + + Beats missed in shutdown: 17.68% (55/311) + N missed in shutdown: 22.12% (25/113) + S missed in shutdown: 29.69% (19/64) + V missed in shutdown: 13.64% (9/66) + F missed in shutdown: 21.74% (5/23) + Total shutdown time: 26 seconds diff --git a/tests/target-output/bxb/bxb250.atr b/tests/target-output/bxb/bxb250.atr new file mode 100644 index 0000000000000000000000000000000000000000..c6afde255a2f7509ca91411596ecc7431e8db6e6 GIT binary patch literal 814 zcmWNP%~w)U6vba_?ZF*1xPvn|!#nGuV0Yo=L!SyN3K9w!CXk6_ej+AjJr`2g3iXQ9 z{+a%!8aA&tJ!|cK&fdS{{dwm7PJ8Ye6MXI7&|5m<93Ev1%e;X$DoEHUX^N^Oo}5o7 z`jgEdCtj;1^QdR&I|4T_)3Qv5XxLM9#HpTA52D49+lE?32}3)prp&tSkQGBJV!`c# zM<;ZPpRmC6(ZsE&JB%`0}QamB^u)a9a+}%q3e=pp`){$mzVH$hwkaj9@{t4VF9oB zSezhEY1^d%9mzYlZVCuQlzsA1MoI=W5F2j7eB`)3;2C|dI^u~B#Hs5eX%cf>(SPh9?Dvc}UyJISvj2K~? zZ{4h|qlhg;_*50uSIpZr7ezqtsLVg~zH+XIST-S>cFuHNKwVX}8a+E{t~n`J%{=X5 zPA;k_#xyT{eBfkA?u#g@O31c{Dst{jC&U-E875!Cx*W=L7ZpVv(S1`5@3>Gy6{n^u z(FI2IPF|Wkx6$Ap#cW1K@#uC?=WREM8!<2wI8-fcpdoH~pX;Hft9C@6_Hcj(_UXWU zH(6IgORrIf`tT96PogO=`HCKB5-WDBYqm^FI^v@A82&^;W_6Cf=>pOy(-~?aE2rI* tX~1{y(ZPmkB4&1JhPOD*%eE@(JYhy$#a!s#linZC`}N#Q|NHBC{{fJYyz~G7 literal 0 HcmV?d00001 diff --git a/tests/target-output/bxb/bxb250.hea b/tests/target-output/bxb/bxb250.hea new file mode 100644 index 00000000..8c50cb3a --- /dev/null +++ b/tests/target-output/bxb/bxb250.hea @@ -0,0 +1,2 @@ +bxb250 1 250 300000 +bxb250.dat 16 200 11 1024 0 0 0 ECG diff --git a/tests/target-output/bxb/bxb250.tst b/tests/target-output/bxb/bxb250.tst new file mode 100644 index 0000000000000000000000000000000000000000..dafc000467086cef9c3a0f93b9da02b2ec439ed8 GIT binary patch literal 670 zcmWlVA#CDM9Eaa7@87iy(eB>kUbl(%hG-W=yQ3SqASw_D1Y%;MVuB@G!rhTOEjjaXAL zdgj7Ouqb&tu}7v$U!lfPgwPZt;=`S|W<%(@J8L40^H8PQ zX=KcyjVl~?Hg--KKxnMkm0F-BA%V8ynHx;@CepnbGuc_1fl;EZ)EMUZqkUOm*f^_8 zGnkQHCZfJl?ubqon$j~PLRtys?rBlV@sVX5OpLrbG{n>U)T4UyiSZ50@tauGU(C80 z(`R_$5e{Glm_Mm3GnE=NeNw=$@aVXD+3}@2AgSST*OX0-W`%7zjv?fIvb0v29Fz;z z`u}_GH7dMYCu~Bn5K5X}h7FD&tzYl~EPe8Ey8Ec${Sm*(#CFNX(oB!_39jiS+Wjz6 z#`c1Zp{0(ghtqjidBg2pPi^ogBQe)@u;mDDKpq~X#vwX<)tl0o4q)64@g=lr-`?07 zFT#z<=#Z|qhDZ7Ao+sB2W^^uyPC;m8LL>0|#1L9a(O3}6csX$)!0xu+lQ7pv&8|V3 q*UX+;ur)}m!mkPmk=z)7HcQ literal 0 HcmV?d00001 diff --git a/tests/target-output/bxb/bxb360-report.txt b/tests/target-output/bxb/bxb360-report.txt new file mode 100644 index 00000000..a94d93da --- /dev/null +++ b/tests/target-output/bxb/bxb360-report.txt @@ -0,0 +1,29 @@ +Beat-by-beat comparison results for record bxb360 +Reference annotator: atr + Test annotator: tst + + Algorithm + n s v f q o x + ____________________________________ + N | 115 6 3 2 2 14 0 + S | 2 56 3 1 3 10 0 + V | 6 0 58 0 4 11 0 + F | 4 1 2 19 3 4 0 + Q | 5 0 2 1 42 7 0 + O | 5 4 3 0 0 + X | 5 1 3 2 0 + + QRS sensitivity: 88.08% (340/386) + QRS positive predictivity: 93.66% (340/363) + VEB sensitivity: 73.42% (58/79) + VEB positive predictivity: 82.86% (58/70) + SVEB sensitivity: 74.67% (56/75) +SVEB positive predictivity: 82.35% (56/68) + RMS RR interval error: 445.35 ms + + Beats missed in shutdown: 0.00% (0/386) + N missed in shutdown: 0.00% (0/142) + S missed in shutdown: 0.00% (0/75) + V missed in shutdown: 0.00% (0/79) + F missed in shutdown: 0.00% (0/33) + Total shutdown time: 1 seconds diff --git a/tests/target-output/bxb/bxb360.atr b/tests/target-output/bxb/bxb360.atr new file mode 100644 index 0000000000000000000000000000000000000000..1454bee49b7980a37ca183046f17f4ae17e75969 GIT binary patch literal 816 zcmWO4|7%rc90%~T_PpynX?c&GPu_<_&yz^^iN4&&ou?KB30j(NhD%q{OkBE>{7L7W ztcKQF=O_fG;daA`<`nfpgS&Buda-3p?RuSVL-3juHQQPPN4qrCzWFD-;AM?HV1*-A z{8j#wxEmaIUz8tVrP-j<-ZYxPmy|^Zn@s`oV#r@i-H}aZInS{+O86_x{U(M-Z~$lA zxm1Ei>SvCq7akaLBVV_Yr7`&g%|j80anf8mwL$Q`OLzFEz{>t@owP}dpxfTK+oTmL z@ThnhA3BqZ^rsd9D=dPFzdDtluG**@I%QjvOdDOis4`gd?_5*4aEEX7)M zm@}BEUsJPS%Q}8eXVgS=C*BY-UB!F&G%QkwR3dc4wIV4S!?$GZJw*~LC}*ea7CRkn zw+FnBagvhyUp4|B3K-q#UPU4DT`=Ki#I`UOoI@Um?N0S3TH+_2RXW2y3%|8%c?y?V zTYR^<;OCi{UyQk!4EpIhRnrys3t2{gunnj+O=zY)IObIGd>E%ZPqJQqi#GDJe2^`3 za%_;j{lb3*Ys|9dPq9ji3ak9S=#z`@EBGR!^YPZ`W6mPAIeeND&OF>8$w%1_Yz?yZ zWq*t8!%o@j?ia7fd*3`kCDcr(P@$9lu$R`gHb;UCr=8RL8OGic(i`XD>zb+DA^k6G6Lac>M> literal 0 HcmV?d00001 diff --git a/tests/target-output/bxb/bxb360.hea b/tests/target-output/bxb/bxb360.hea new file mode 100644 index 00000000..0b99ee96 --- /dev/null +++ b/tests/target-output/bxb/bxb360.hea @@ -0,0 +1,2 @@ +bxb360 1 360 650000 +bxb360.dat 16 200 11 1024 0 0 0 ECG diff --git a/tests/target-output/bxb/bxb360.tst b/tests/target-output/bxb/bxb360.tst new file mode 100644 index 0000000000000000000000000000000000000000..2d65a36829023d3a40222b6a7b63ee5a1954fe4f GIT binary patch literal 792 zcmWO2|4-C)90%}=dp+;&oi0Bg{Jep8EB(AJ>)yM_`*5GP>FS5RPE2v&LOv{d_}A#8PeYT=Wp@QXVZ5p<*a&^ZKXDVd$5B{< z77UQWa=gyR$rP2M8*j6#_==J$7S5Kf40l++^Pkv0+N08Z6V^MWGAL5|Zp@f86%cleoMYOa+|&p zS&YXK#s+ba-m>=K2s&83-Y-_6Rjd*Z%7LiD&$^G_CI7IRoKmboktoG`=Cvr3{i>0# z^et4uMy<-xe`Q;Q&8Epz|5ATUvIUBZ@9hk_REp;Fr0f;jLs_aphqb^t!m`RmT8%`u z;LOdrH)P?CV^OjX&^~-_?$)=4&N{O20=+DrQhMi|UILqNTo0n$S*!$ta+`VirduF( zVY4utPPUuP!D_XvI40JhTekQ$@_8{+xDMZ0z0~3MmV6ee3=Q(5&UN|)d9Mt?qJBJP zO&D$7KkgaP;E#IWAcMmty-q#ekDTDi_PNdwwfYm0rECmY`Z&IE={P+EiCOOgf0Lf( zb$Z;|=xJHWbCg1Zo?G0GCUXZH@}K2x>>_2@75*aol3k)C8>O=}$nT}K;s@`MXgB|m zrrDDsg)|AAE2$Q@J6nzC;_dE2GpSzk=kXr1ie{`Id5rx=L$np?$e8 Date: Mon, 23 Nov 2026 23:19:18 -0500 Subject: [PATCH 2/2] Support stop=0 in bxb, index intervals, and test against bxb if installed As with bxb -t 0, stop=0 now ends the comparison at the last beat of the file that ends first, counting every test shutdown. It used to stop at sample 0, so that nothing was counted. Comparing with bxb -t 0 and more generated cases also showed that, without a record length, bxb stops at the last reference beat (not annotation); that an AHA-style shutdown with no annotation before it starts one window after sample 0; that a shutdown with no annotation after it adds time only if the end of the comparison is known; and that the first test beat after the start is dropped only if the next test beat is closer to the reference beat it was compared with. Look up times in shutdown and VF intervals with an index (sorted starts and the running maximum of ends), instead of scanning every interval, so that records with many intervals take linear rather than quadratic time. Add a regression test for stop=0 (with a test file that ends early, and its report from bxb -t 0), and test_matches_wfdb_bxb, which compares 200 generated cases with the installed bxb, and is skipped if bxb is not installed. --- src/wfdb/processing/beat_comparison.py | 94 ++++++++++---- .../target-output/bxb/bxb360-stop0-report.txt | 29 +++++ tests/target-output/bxb/bxb360.qrs | Bin 0 -> 516 bytes tests/test_beat_comparison.py | 121 +++++++++++++++++- 4 files changed, 218 insertions(+), 26 deletions(-) create mode 100644 tests/target-output/bxb/bxb360-stop0-report.txt create mode 100644 tests/target-output/bxb/bxb360.qrs diff --git a/src/wfdb/processing/beat_comparison.py b/src/wfdb/processing/beat_comparison.py index e9be68a9..b6b96082 100644 --- a/src/wfdb/processing/beat_comparison.py +++ b/src/wfdb/processing/beat_comparison.py @@ -92,7 +92,8 @@ def _beats_and_intervals(sample, label_store, subtype, window): ): shutdowns.append(_Interval(t, int(sample[i + 1]), i)) elif i + 1 < n: - start = int(sample[i - 1]) + window if i > 0 else 0 + # (taking the previous annotation to be at 0 if there is none) + start = (int(sample[i - 1]) if i > 0 else 0) + window end = int(sample[i + 1]) - window # The previous beat is forgotten only if the next annotation # comes after the start of the shutdown @@ -104,15 +105,31 @@ def _beats_and_intervals(sample, label_store, subtype, window): if in_vf: vf.append(_Interval(int(sample[vf_start]), math.inf, vf_start)) # A shutdown that starts during VF in the same file is ignored - shutdowns = [s for s in shutdowns if not _in(s.start, vf)] + vf_index = _Intervals(vf) + shutdowns = [s for s in shutdowns if not vf_index.contains(s.start)] return beats, vf, shutdowns -def _in(t, intervals, closed=False): - """Whether time `t` is inside one of `intervals`.""" - if closed: - return any(iv.start <= t <= iv.end for iv in intervals) - return any(iv.start <= t < iv.end for iv in intervals) +class _Intervals: + """A set of intervals, for finding quickly whether a time is in one. + + The intervals are sorted by start, with the latest end of those starting + at or before each one, so that a lookup takes logarithmic time even if + the intervals overlap. + + """ + + def __init__(self, intervals): + intervals = sorted(intervals, key=lambda iv: iv.start) + self.starts = [iv.start for iv in intervals] + self.max_ends = list(np.maximum.accumulate([iv.end for iv in intervals])) + + def contains(self, t, closed=False): + """Whether `t` is in one of the intervals (with its end, if `closed`).""" + k = bisect.bisect_right(self.starts, t) + if k == 0: + return False + return self.max_ends[k - 1] >= t if closed else self.max_ends[k - 1] > t def _label_store(annotation): @@ -476,12 +493,15 @@ def bxb( sig_len : int, optional The length of the record, in samples. If given, and `stop` is not, the comparison ends at the end of the record. Otherwise it ends at - the last reference annotation. + the last reference beat. start : int, optional The sample at which to start the comparison. By default, 5 minutes after the start of the record, as EC57 requires. stop : int, optional - The sample at which to stop the comparison (inclusive). + The sample at which to stop the comparison (inclusive). If 0, the + comparison stops at the last beat of whichever file ends first, as + with ``bxb -t 0``. By default, it stops at the end of the record if + `sig_len` is given, and at the last reference beat otherwise. window : float, optional The match window, in seconds. The default, 0.15, is the one that EC57 requires. @@ -522,11 +542,26 @@ def bxb( ) files.append(_beats_and_intervals(sample, codes, subtype, w)) (R, rvf, rshut), (A, tvf, tshut) = files + whole_files = stop == 0 + end_known = bool(sig_len) or stop is not None + rvf_index, rshut_index, tshut_index = ( + _Intervals(rvf), + _Intervals(rshut), + _Intervals(tshut), + ) if stop is None: if sig_len: stop = int(sig_len) else: - stop = int(samples[0][-1]) if len(samples[0]) else 0 + # Without a record length, the comparison ends at the last + # reference beat + stop = R[-1].time if R else 0 + elif stop == 0: + # As with bxb -t 0, the comparison ends when either file has no more + # beats + stop = min(R[-1].time, A[-1].time) if R and A else 0 + else: + stop = int(stop) events = _match(R, A, w) counts = {r: [0] * (5 if r in "OX" else 7) for r in ROWS} @@ -544,14 +579,19 @@ def bxb( ) # The first test beat after the start is not counted if it is within the - # match window of the start and the next event also has a test beat + # match window of the start, matches no reference beat, and the next test + # beat is closer than it to the reference beat it was compared with k0 = bisect.bisect_left([a.time for a in A], start) skipped = None - if k0 < len(A) and A[k0].time - start <= w: - for e, after in zip(events, events[1:]): - if e[0] == "fp" and e[2] is A[k0]: - if after[0] in ("fp", "match"): - skipped = A[k0] + if k0 + 1 < len(A) and A[k0].time - start <= w: + for kind, _, a, other in events: + if a is A[k0]: + if ( + kind == "fp" + and other is not None + and abs(A[k0 + 1].time - other.time) < abs(a.time - other.time) + ): + skipped = a break prev_r = prev_a = None @@ -575,16 +615,18 @@ def bxb( counted = ( start <= a.time <= stop and a is not skipped - and not _in(a.time, rvf, closed=True) + and not rvf_index.contains(a.time, closed=True) ) if counted: counted_events.append((kind, r, a)) if kind == "match": counts[r.cls][COLUMNS.index(a.cls.lower())] += 1 elif kind == "fn": - counts[r.cls][6 if _in(r.time, tshut, closed=True) else 5] += 1 + counts[r.cls][ + 6 if tshut_index.contains(r.time, closed=True) else 5 + ] += 1 else: - row = "X" if _in(a.time, rshut, closed=True) else "O" + row = "X" if rshut_index.contains(a.time, closed=True) else "O" counts[row]["nsvfq".index(a.cls.lower())] += 1 if prev_r is not None and prev_a is not None: # For an unmatched beat, the other side's interval is the one @@ -604,9 +646,17 @@ def bxb( k = bisect.bisect_right(rt, stop) cutoff = rt[k] if k < len(rt) else math.inf shutdown = sum( - max(0, (iv.end if iv.end != math.inf else stop) - iv.start) + # A shutdown with no annotation after it lasts until the end of the + # comparison, if that is known (from the record length or `stop`) + ( + max(0, iv.end - iv.start) + if iv.end != math.inf + else (max(0, stop - iv.start) if end_known else 0) + ) for iv in tshut - if iv.start <= stop + # With stop=0, the test file is read to its end + if whole_files + or iv.start <= stop or int(samples[1][iv.index]) <= stop or int(samples[1][iv.index]) < cutoff ) @@ -724,7 +774,7 @@ def bxb_record( The sample at which to start the comparison (by default, 5 minutes). stop : int, optional The sample at which to stop the comparison (by default, the end of - the record). + the record). If 0, it stops when either file has no more beats. window : float, optional The match window, in seconds (by default, 0.15). diff --git a/tests/target-output/bxb/bxb360-stop0-report.txt b/tests/target-output/bxb/bxb360-stop0-report.txt new file mode 100644 index 00000000..5a4554a9 --- /dev/null +++ b/tests/target-output/bxb/bxb360-stop0-report.txt @@ -0,0 +1,29 @@ +Beat-by-beat comparison results for record bxb360 +Reference annotator: atr + Test annotator: qrs + + Algorithm + n s v f q o x + ____________________________________ + N | 78 5 2 1 1 9 0 + S | 1 34 2 1 3 9 0 + V | 5 0 35 0 2 11 0 + F | 4 0 2 16 2 4 0 + Q | 3 0 2 0 27 5 0 + O | 3 1 3 0 0 + X | 0 0 0 0 0 + + QRS sensitivity: 85.61% (226/264) + QRS positive predictivity: 97.00% (226/233) + VEB sensitivity: 66.04% (35/53) + VEB positive predictivity: 83.33% (35/42) + SVEB sensitivity: 68.00% (34/50) +SVEB positive predictivity: 85.00% (34/40) + RMS RR interval error: 448.54 ms + + Beats missed in shutdown: 0.00% (0/264) + N missed in shutdown: 0.00% (0/96) + S missed in shutdown: 0.00% (0/50) + V missed in shutdown: 0.00% (0/53) + F missed in shutdown: 0.00% (0/28) + Total shutdown time: 1 seconds diff --git a/tests/target-output/bxb/bxb360.qrs b/tests/target-output/bxb/bxb360.qrs new file mode 100644 index 0000000000000000000000000000000000000000..3d626219aa39bccc631eed258f4fd17c24048ebc GIT binary patch literal 516 zcmWO2-%C?b00;2Z&Uf1>h~0V5>O5rkRK^Z#=-uumrN;^rLrqXp#3aNVK`)^t6_rD} zH_;^@Cp0Xjw!N7R7 zvmP{ZvA&_U8CXIKkP=po_cQFAYrlvASF{Z{9ocB|}HlEyn@Rm~Y) zY=6P3)vcUlb@se@06AmSSR?hAWKHO#K}>juwFPvOC|jmSlqnITb0xN`+RPfi)FE|6 zOIo`lz03({A3-dDqYX#yct!LZOS)#wwddmA{>Sr4SbkfRQoW7IBhlW z@Jd|gmHeQX3`l%7lWdRpp$}yC8g59|wqbpA6!W(ra#U zJ=2Bn4LQq?OS5OE6+sIo%ruVbMUulQx|GEYcQe0%HtyAjXfNH0W3qI6kk?>D#Ox+< wnlJh4@JdaPxRKcL&{OM5vvGZmd_mp{!|6}qkhKg+yu*HJy@12