Part VII: Health, Biosignals, and Wearable AI
Chapter 29: ECG and Cardiac AI

Explainability and false-alarm triage

"The model was right about the arrhythmia and wrong about the loose electrode. The nurse could not tell which, so she stopped trusting both."

A Chastened AI Agent

Prerequisites

This section assumes the beat morphology and lead geometry of Sections 29.1 and the beat-level versus record-level decision granularity of Section 29.4. It leans on calibration, confidence, and conformal prediction from Chapter 18, on the general interpretability and root-cause toolkit for time series in Chapter 67, and on the signal-quality and denoising vocabulary of Chapter 6. No cardiology background beyond Chapter 28 is required. Amplitude and rate figures are engineering anchors, not clinical thresholds.

The Big Picture

A cardiac model that is accurate on a benchmark can still be useless, or dangerous, in a ward. The reason is almost never the accuracy number: it is trust and alarm burden. A critical-care monitor that fires hundreds of arrhythmia alerts per patient per day trains staff to ignore all of them, so the one true ventricular tachycardia is silenced by reflex. A watch that flags atrial fibrillation with no way for a clinician to see why sends its wearer to an emergency room with a screenshot and no context. This section is about the two engineering disciplines that decide whether an ECG model survives contact with real users. Explainability answers "on what evidence did the model decide?" so a human can accept or override it. False-alarm triage answers "which alerts deserve a human's attention?" so the true positives are not drowned. The two are the same problem viewed from two ends: an alarm you cannot explain is an alarm you cannot triage.

Alarm fatigue is the real deployment failure

The dominant clinical failure mode of cardiac monitoring is not the missed beat; it is the flood of false ones. Studies of intensive-care telemetry have repeatedly found that the large majority of arrhythmia alarms are false or clinically irrelevant, driven by a lead coming loose, a patient brushing teeth, muscle tremor, or a single premature beat that meets a rule but means nothing. When most alarms are noise, clinicians habituate: response times lengthen, volumes get turned down, and alarms get pre-emptively disabled. This is alarm fatigue, and it is a documented cause of patient harm. The lesson for a model builder is that the operating-point metric that matters is rarely sensitivity alone. In a low-prevalence stream, positive predictive value dominates the human experience.

The arithmetic is unforgiving. Suppose a detector runs on a continuous stream where a genuine actionable event occurs at prevalence \(\pi = 0.001\) per analysis window. With sensitivity \(0.99\) and a specificity that sounds excellent at \(0.99\), the positive predictive value is

$$\text{PPV} = \frac{\pi\,\text{Sens}}{\pi\,\text{Sens} + (1-\pi)(1-\text{Spec})} = \frac{0.001\cdot0.99}{0.001\cdot0.99 + 0.999\cdot0.01} \approx 0.09.$$

Nine in ten alerts are false at a specificity that would headline any paper. This is the same base-rate trap that governs any rare-event detector, and it is why the classical change-detection framing of Chapter 12 obsesses over false-alarm rate per hour rather than per-window specificity. The fix is not a better single classifier; it is a system that suppresses, defers, and explains.

Key Insight

In a rare-event stream, halving the false-positive rate helps the human far more than adding another point of sensitivity. At \(\pi = 0.001\), moving specificity from \(0.99\) to \(0.999\) lifts PPV from about \(9\%\) to about \(50\%\): a tenfold reduction in wasted human attention for the same catch rate. Design the operating point around alarms-per-patient-per-day, then verify sensitivity is still adequate. The reverse order builds an accurate model that no one will keep switched on.

Explaining an ECG decision: attribution that lands on P, QRS, and T

Explainability for ECG has a rare advantage over most deep-learning interpretability: the ground-truth vocabulary already exists. A cardiologist reasons in terms of named landmarks (a widened QRS, a flattened P, ST-segment elevation, a prolonged QT), so a faithful explanation should point at those same regions of the trace. This turns a vague "which pixels mattered" question into a checkable one: does the model's evidence overlap the physiological feature a clinician would cite?

The workhorse tools are gradient-based attribution over the time axis. Grad-CAM adapted to 1D convolutions produces a per-sample importance curve you can overlay on the waveform; Integrated Gradients attributes the prediction to input samples relative to a baseline (a flat or median-beat reference) with the appealing completeness property that attributions sum to the score difference. For the transformer and state-space ECG models of Section 29.5, attention weights and attention-rollout give a complementary view of which beats and which intervals the model pooled over. The key discipline, developed generally in Chapter 67, is to not trust a saliency map because it looks plausible. Validate it: perturb the QRS and confirm the attribution and the prediction both move; run a sanity check where randomizing the top layer's weights should destroy the map. A saliency method that survives neither test is decoration.

The highest-value explanation is often not a heatmap but a concept: report that the model's decision is driven by "QRS duration above threshold" or "irregular RR intervals," phrased in the clinician's ontology. Prototype and concept-based models learn this directly, matching a query beat against learned exemplars ("this looks like these three training beats labeled PVC"), which a human can inspect far faster than an abstract gradient field.

Practical Example: the loose-electrode false positive

A telemetry vendor deployed a deep arrhythmia model on step-down units and saw an asystole alert fire on a patient who was, per the bedside nurse, sitting up and talking. The record-level classifier had high confidence. The team added a 1D Grad-CAM overlay to the alert card. On the flagged strip, the importance mass sat not on any beat but on a flat, high-frequency-noise segment where a lead had partly detached: the model had learned that "flatline plus baseline wander equals asystole," a shortcut that is correct for true asystole and catastrophic for a detached electrode. The explanation did two things at once. It told the nurse this alert was an artifact, and it told the engineers to add a signal-quality gate upstream, described next. The fix cut asystole false alarms on that unit by a large margin without touching the classifier's weights.

Triage as a pipeline: quality gates and confidence cascades

False-alarm triage is best built as a cascade of cheap-to-expensive stages, each empowered to suppress or defer rather than to alarm. The first and most cost-effective stage is a signal-quality index (SQI). Before any classifier runs, ask whether the window even contains an interpretable ECG. Cheap SQI features include the fraction of the window that is flat or railed, the power ratio between the QRS band (roughly 5 to 40 Hz) and out-of-band noise, kurtosis, and the agreement between two independent QRS detectors on the same strip. A window that fails SQI is not classified as normal or abnormal; it is labeled "uninterpretable" and either suppressed or routed for re-acquisition. Because most artifact-driven false alarms originate in low-quality windows, this single gate removes a large share of them before the model is even consulted, and it is exactly the kind of gating the wearable pipelines of Chapter 30 apply to motion-corrupted PPG.

The second stage is a confidence and uncertainty gate. A well-calibrated probability (Chapter 18) lets you route the model's own low-confidence outputs to a human or to a second opinion instead of alarming automatically. Conformal prediction gives this a distribution-free guarantee: rather than emit a point label, emit a prediction set, and treat a window whose set contains more than one arrhythmia class as "ambiguous, escalate." A third stage can add temporal persistence, requiring an abnormal call to hold across several consecutive windows before it becomes an alarm, which trades a few seconds of latency for a large drop in one-off false triggers. The design question at each stage is the cost asymmetry: a suppressed true ventricular tachycardia is far more expensive than a suppressed premature beat, so persistence and confidence thresholds should be class-specific, not global.

import numpy as np

def signal_quality_index(x, fs):
    """Cheap SQI in [0,1]: high when the window looks like a clean ECG."""
    # 1) fraction of samples that are flat (lead-off / saturation)
    flat = np.mean(np.abs(np.diff(x)) < 1e-4)
    # 2) in-band (QRS, 5-40 Hz) vs total power via a crude spectral ratio
    X = np.abs(np.fft.rfft(x - x.mean()))**2
    f = np.fft.rfftfreq(len(x), 1/fs)
    inband = X[(f >= 5) & (f <= 40)].sum()
    ratio = inband / (X.sum() + 1e-12)
    return float(np.clip(ratio * (1 - flat), 0, 1))

def triage(x, fs, model, sqi_min=0.5, p_high=0.9, p_low=0.6):
    """Return an action, not just a label, for one analysis window."""
    q = signal_quality_index(x, fs)
    if q < sqi_min:
        return "SUPPRESS_UNINTERPRETABLE", q     # bad signal: re-acquire, do not alarm
    p = model.predict_proba(x)                   # calibrated abnormal-class prob.
    if p >= p_high:
        return "ALARM", p                        # confident and clean -> alert human
    if p >= p_low:
        return "ESCALATE_REVIEW", p              # ambiguous -> queue for a second look
    return "CLEAR", p                            # confident normal -> stay silent

# Every abnormal-looking window is now sorted into alarm / review / suppress,
# instead of every model positive becoming an alarm.
A minimal three-way triage gate. The signal_quality_index catches artifact windows (the loose-electrode case above) before the model runs; the confidence bands then split model positives into confident alarms and human-reviewed ambiguities. In production the thresholds are class-specific and the SQI adds a second QRS-detector-agreement term.

The code above is deliberately spartan so the control flow is visible. Notice that the model is only one of four decisions: the SQI gate, the two confidence thresholds, and (omitted here) temporal persistence do most of the alarm reduction. This is the recurring shape of a trustworthy monitor: the classifier proposes, and the surrounding logic disposes.

Library Shortcut

You do not hand-roll SQI or conformal gating in production. NeuroKit2 exposes nk.ecg_quality() for a validated per-beat quality score (multiple methods, physiologically grounded) in one call, replacing roughly 40 to 60 lines of spectral and detector-agreement code. For the confidence gate, MAPIE wraps any fitted classifier with MapieClassifier to emit conformal prediction sets at a target error rate in about five lines, instead of the ~100 lines of split-conformal calibration and quantile bookkeeping you would otherwise write and test. Both let you spend your effort on the class-specific cost thresholds, which are genuinely yours to choose, rather than on re-deriving standard machinery.

From explanation to threshold: cost-sensitive operating points

Explainability and triage meet at the operating point. Once explanations have let you audit why the model fires, you set thresholds by explicit cost rather than by a default \(0.5\). Let \(C_{\text{FN}}\) be the cost of a missed event and \(C_{\text{FP}}\) the cost of a false alarm (including its contribution to fatigue). The Bayes-optimal threshold on a calibrated probability is

$$p^\star = \frac{C_{\text{FP}}}{C_{\text{FP}} + C_{\text{FN}}},$$

so a class where a miss is catastrophic (ventricular tachycardia) gets a low threshold and fires readily, while a benign-but-noisy class (isolated ectopy) gets a high one. Because these thresholds only make sense on a calibrated score, the calibration work of Chapter 18 is not optional polish; it is what makes the cost model coherent. The final honesty check is evaluation design: alarm-reduction numbers must be measured at fixed sensitivity, patient-by-patient rather than beat-by-beat, so a system cannot look good by suppressing alarms on the easy patients while missing events on the hard ones. That patient-level, leakage-safe framing is the subject of the next section.

Exercise

Take a single-lead arrhythmia model from Section 29.2 or the lab and wrap it in the three-way triage gate above, using nk.ecg_quality() for the SQI term. On a held-out patient-level split, plot alarms-per-hour and sensitivity as you sweep the SQI threshold from 0 to 1 with the confidence bands fixed. Identify the SQI value that halves the alarm rate, and report how much sensitivity (if any) you paid for it. Then repeat with class-specific confidence thresholds derived from a cost ratio \(C_{\text{FN}}/C_{\text{FP}} = 20\) for a life-threatening class. Which lever, quality gating or cost-aware thresholds, buys more alarm reduction on your data?

Self-Check

  1. A detector reports \(99\%\) specificity on a stream with event prevalence \(0.002\). Roughly what fraction of its alarms are false, and why does adding sensitivity barely change that number?
  2. A 1D Grad-CAM map for a "normal" call places all its importance on the T wave and none on the QRS. Give one perturbation test that would tell you whether to trust this explanation.
  3. Why must a signal-quality gate output a third label ("uninterpretable") rather than folding bad windows into either "normal" or "abnormal"?

What's Next

In Section 29.7, we turn the triage and calibration choices of this section into a defensible study. We cover clinical validation design: patient-level and site-level splits that respect the leakage traps, prospective versus retrospective evaluation, the reference-standard and adjudication problem, subgroup reporting, and the regulatory framing that decides whether a cardiac model is a research demo or a device a clinician is allowed to rely on.