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..b6b96082 --- /dev/null +++ b/src/wfdb/processing/beat_comparison.py @@ -0,0 +1,879 @@ +"""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: + # (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 + 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 + vf_index = _Intervals(vf) + shutdowns = [s for s in shutdowns if not vf_index.contains(s.start)] + return beats, vf, shutdowns + + +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): + """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 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). 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. + 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 + 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: + # 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} + 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, 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 + 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 + 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 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 tshut_index.contains(r.time, closed=True) else 5 + ] += 1 + else: + 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 + # 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( + # 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 + # 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 + ) + 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). If 0, it stops when either file has no more beats. + 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 00000000..c6afde25 Binary files /dev/null and b/tests/target-output/bxb/bxb250.atr differ 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 00000000..dafc0004 Binary files /dev/null and b/tests/target-output/bxb/bxb250.tst differ 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-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.atr b/tests/target-output/bxb/bxb360.atr new file mode 100644 index 00000000..1454bee4 Binary files /dev/null and b/tests/target-output/bxb/bxb360.atr differ 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.qrs b/tests/target-output/bxb/bxb360.qrs new file mode 100644 index 00000000..3d626219 Binary files /dev/null and b/tests/target-output/bxb/bxb360.qrs differ diff --git a/tests/target-output/bxb/bxb360.tst b/tests/target-output/bxb/bxb360.tst new file mode 100644 index 00000000..2d65a368 Binary files /dev/null and b/tests/target-output/bxb/bxb360.tst differ diff --git a/tests/target-output/bxb/sd.out b/tests/target-output/bxb/sd.out new file mode 100644 index 00000000..66c1e9e0 --- /dev/null +++ b/tests/target-output/bxb/sd.out @@ -0,0 +1,5 @@ +Record Nx Sx Vx Fx Qx % beats % N % S % V % F Total Shutdown + missed missed missed missed missed Time + 100 0 0 0 0 0 0.00 0.00 0.00 0.00 - 0 seconds 1872 29 1 0 0 +bxb360 0 0 0 0 0 0.00 0.00 0.00 0.00 0.00 1 seconds 142 75 79 33 57 +bxb250 25 19 9 5 16 17.68 22.12 29.69 13.64 21.74 26 seconds 113 64 66 23 45 diff --git a/tests/target-output/bxb/sumstats-bxb.txt b/tests/target-output/bxb/sumstats-bxb.txt new file mode 100644 index 00000000..e268b0fb --- /dev/null +++ b/tests/target-output/bxb/sumstats-bxb.txt @@ -0,0 +1,11 @@ +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 +____________________________________________________________________________________________________________________________________ +Sum 2061 39 20 108 21 9 90 0 5 6 9 4 92 5 11 48 35 34 40 +Gross 93.96 98.47 63.01 79.31 53.57 85.71 +Average 84.13 95.56 41.64 78.39 42.60 86.53 226.49 +Total QRS complexes: 2599 Total VEBs: 146 Total SVEBs: 168 + +Summary of results from 3 records diff --git a/tests/target-output/bxb/sumstats-sd.txt b/tests/target-output/bxb/sumstats-sd.txt new file mode 100644 index 00000000..9c35d6db --- /dev/null +++ b/tests/target-output/bxb/sumstats-sd.txt @@ -0,0 +1,9 @@ +Record Nx Sx Vx Fx Qx % beats % N % S % V % F Total Shutdown + missed missed missed missed missed Time + 100 0 0 0 0 0 0.00 0.00 0.00 0.00 - 0 seconds 1872 29 1 0 0 +bxb360 0 0 0 0 0 0.00 0.00 0.00 0.00 0.00 1 seconds 142 75 79 33 57 +bxb250 25 19 9 5 16 17.68 22.12 29.69 13.64 21.74 26 seconds 113 64 66 23 45 +________________________________________________________________________ +Sum 25 19 9 5 16 27 seconds + +Summary of results from 3 records diff --git a/tests/test_beat_comparison.py b/tests/test_beat_comparison.py new file mode 100644 index 00000000..8478454d --- /dev/null +++ b/tests/test_beat_comparison.py @@ -0,0 +1,218 @@ +"""Tests for the beat-by-beat comparison of annotations (bxb). + +The expected outputs in tests/target-output/bxb were made with the bxb and +sumstats programs of the WFDB Software Package (10.7.0), from the reference +(.atr) and test (.tst, .qrs, and sample-data/100.qrs) annotation files there. +If bxb is installed, test_matches_wfdb_bxb also compares many generated cases +with it. +""" + +import os +import shutil +import subprocess +from pathlib import Path + +import numpy as np +import pandas as pd +import pytest + +import wfdb +from wfdb.processing import bxb, bxb_record, sumstats +from wfdb.processing.beat_comparison import LINE_HEADINGS + +TARGET = Path(__file__).parent / "target-output" / "bxb" +SAMPLE_DATA = Path(__file__).parent.parent / "sample-data" + +# The records compared for the expected outputs, and the options of each +CASES = [ + (SAMPLE_DATA / "100", "qrs", {}), + (TARGET / "bxb360", "tst", {}), + (TARGET / "bxb250", "tst", {"start": 81000, "stop": 110000, "window": 0.12}), +] + + +def results(): + return [bxb_record(str(path), "atr", test, **kw) for path, test, kw in CASES] + + +@pytest.mark.parametrize( + ("case", "expected"), + [ + (CASES[0], "100-report.txt"), + (CASES[1], "bxb360-report.txt"), + (CASES[2], "bxb250-report.txt"), + # A test file that ends before the reference file, compared as with + # bxb -t 0, which stops at the end of either file + ((TARGET / "bxb360", "qrs", {"stop": 0}), "bxb360-stop0-report.txt"), + ], + ids=["100-qrs", "vf-and-shutdowns", "250hz-with-options", "stop-0"], +) +def test_report_matches_bxb(case, expected): + """The report is the same as that of bxb -S.""" + path, test, kw = case + result = bxb_record(str(path), "atr", test, **kw) + assert result.report() == (TARGET / expected).read_text() + + +def test_stop_0_stops_at_the_end_of_either_file(): + reference = wfdb.Annotation( + "rec", "atr", sample=np.array([360, 720, 1080]), symbol=["N"] * 3, fs=360 + ) + test = wfdb.Annotation( + "rec", "tst", sample=np.array([360, 720]), symbol=["N"] * 2, fs=360 + ) + + result = bxb(reference, test, start=0, stop=0) + + # Both test beats are matched, and the reference beat after the end of the + # test file is not counted as missed + assert result.count("N", "n") == 2 + assert result.count("N", "o") == 0 + assert bxb(reference, test, start=0, stop=1080).count("N", "o") == 1 + + +def test_line_format_matches_bxb(): + """The line-format reports are the same as those of bxb -L.""" + lines = [r.lines() for r in results()] + assert LINE_HEADINGS[0] + "".join(a + "\n" for a, b in lines) == ( + (TARGET / "bxb.out").read_text() + ) + assert LINE_HEADINGS[1] + "".join(b + "\n" for a, b in lines) == ( + (TARGET / "sd.out").read_text() + ) + + +def test_sumstats_matches_wfdb_sumstats(): + summary, shutdown = sumstats(results()) + assert summary == (TARGET / "sumstats-bxb.txt").read_text() + assert shutdown == (TARGET / "sumstats-sd.txt").read_text() + + +def test_matrix_and_statistics(): + result = bxb_record(str(SAMPLE_DATA / "100"), "atr", "qrs") + + matrix = result.matrix + assert list(matrix.index) == list("NSVFQOX") + assert list(matrix.columns) == list("nsvfqox") + assert matrix.loc["N", "n"] == 1872 + assert matrix.loc["S", "n"] == 29 + # Test beats with no reference beat have no "no test beat" columns + assert pd.isna(matrix.loc["O", "o"]) and pd.isna(matrix.loc["X", "x"]) + + stats = result.statistics() + assert stats["QRS sensitivity"] == (1902, 1902) + assert stats["SVEB sensitivity"] == (0, 29) + assert stats["VEB positive predictivity"] == (0, 0) + assert result.rms_rr_error == pytest.approx(1.96, abs=0.005) + assert result.shutdown_seconds == 0 + + +def test_annotations_with_symbols_only(): + """Annotations read without label_store are compared by their symbols.""" + reference = wfdb.rdann(str(SAMPLE_DATA / "100"), "atr") + test = wfdb.rdann(str(SAMPLE_DATA / "100"), "qrs") + assert reference.label_store is None + + result = bxb(reference, test, sig_len=650000) + + expected = bxb_record(str(SAMPLE_DATA / "100"), "atr", "qrs") + assert result.counts == expected.counts + assert result.rr_errors == expected.rr_errors + + +def test_undefined_rr_error(): + """With a single beat, there are no RR intervals to compare.""" + one = wfdb.Annotation("rec", "atr", sample=np.array([200000]), symbol=["N"], fs=360) + result = bxb(one, one, sig_len=650000) + assert result.rms_rr_error is None + assert "RMS RR interval error: -\n" in result.report() + + +def test_sampling_frequency_is_needed(): + ann = wfdb.Annotation("rec", "atr", sample=np.array([200000]), symbol=["N"]) + with pytest.raises(ValueError, match="sampling frequency"): + bxb(ann, ann) + + +def _random_case(rng, fs): + """Reference and test annotations with most of what bxb handles.""" + scale = fs / 360 + n = 300 + sig_len = int(rng.integers(600, 1500) * fs) + t0 = int(rng.integers(0, sig_len - int(n * 330 * scale))) + base = t0 + np.cumsum(rng.integers(int(150 * scale), int(500 * scale), n)) + beats = [1, 1, 1, 2, 5, 5, 6, 8, 4, 13, 12, 10, 34] + codes = rng.choice(beats, n) + ref = [(int(t), int(c), 0) for t, c in zip(base, codes)] + tst = [] + for t, c in zip(base, codes): + if rng.random() < 0.07: + continue + jitter = int(rng.normal(0, 25 * scale)) if rng.random() < 0.3 else 0 + tst.append( + (int(t) + jitter, c if rng.random() < 0.8 else int(rng.choice(beats)), 0) + ) + if rng.random() < 0.05: + tst.append( + (int(t) + int(rng.integers(30, 200) * scale), int(rng.choice(beats)), 0) + ) + for f in (ref, tst): + for _ in range(int(rng.integers(0, 5))): + f.append((int(rng.choice(base)) + 7, int(rng.choice([16, 28, 37])), 0)) + if rng.random() < 0.4: # a VF episode + a = int(rng.choice(base)) + 3 + f += [(a, 32, 0), (a + int(rng.integers(2, 15) * fs), 33, 0)] + if rng.random() < 0.5: # a shutdown, paired or AHA style + a = int(rng.choice(base)) + 5 + b = a + int(rng.integers(1, 20) * fs) + f[:] = [e for e in f if not a < e[0] < b] + if rng.random() < 0.7: + f += [(a, 14, int(rng.choice([48, -1]))), (b, 14, 0)] + else: + f.append(((a + b) // 2, 14, -1)) + return sig_len, sorted(ref), sorted(tst) + + +def _write(directory, name, extension, annotations): + sample, code, subtype = (np.array(x) for x in zip(*annotations)) + wfdb.Annotation( + name, extension, sample=sample, label_store=code, subtype=subtype + ).wrann(write_dir=str(directory)) + + +@pytest.mark.skipif(shutil.which("bxb") is None, reason="WFDB bxb is not installed") +@pytest.mark.parametrize("seed", range(4)) +def test_matches_wfdb_bxb(tmp_path, seed): + """Generated cases give the same reports as the installed WFDB bxb.""" + rng = np.random.default_rng(seed) + env = {**os.environ, "WFDB": str(tmp_path)} + for k in range(50): + fs = int(rng.choice([128, 250, 257, 360, 500, 1000])) + sig_len, ref, tst = _random_case(rng, fs) + name = f"r{k}" + (tmp_path / f"{name}.hea").write_text( + f"{name} 1 {fs} {sig_len}\n{name}.dat 16 200 11 1024 0 0 0 ECG\n" + ) + _write(tmp_path, name, "atr", ref) + _write(tmp_path, name, "tst", tst) + args, kw = [], {} + if rng.random() < 0.3: + kw["start"] = int(rng.integers(0, sig_len // 2)) + args += ["-f", f"s{kw['start']}"] + u = rng.random() + if u < 0.2: + kw["stop"] = int(rng.integers(sig_len // 2, sig_len)) + args += ["-t", f"s{kw['stop']}"] + elif u < 0.35: + kw["stop"] = 0 + args += ["-t", "0"] + if rng.random() < 0.3: + kw["window"] = float(rng.choice([0.05, 0.1, 0.2])) + args += ["-w", str(kw["window"])] + + run = ["bxb", "-r", name, "-a", "atr", "tst", *args] + expected = subprocess.run( + [*run, "-S", "-"], cwd=tmp_path, env=env, capture_output=True, text=True + ).stdout + result = bxb_record(str(tmp_path / name), "atr", "tst", **kw) + assert result.report() == expected, f"seed {seed}, case {k}, {args}"