This guide provides a comprehensive walkthrough of the MIT-BIH Cardiac Playground & OKF-RAG System web application. MIT-BIH stands for Massachusetts Institute of Technology – Beth Israel Hospital (now Beth Israel Deaconess Medical Center, Boston). The MIT-BIH Arrhythmia Database (1975–1979) contains 48 half-hour two-channel ambulatory ECG recordings from 47 subjects, annotated by multiple cardiologists, and remains the gold-standard benchmark for arrhythmia detection research.
This application is designed for clinical researchers, data scientists, and engineers to explore signal processing, deep learning fine-tuning, unsupervised anomaly detection, explainable AI (XAI), and clinical retrieval.
If you are running the project locally or inside a container, you can launch the app and access it using Google Chrome Dev (or your preferred web browser):
- Start the App: Ensure the Streamlit server is running by executing:
or
just start
uv run mit-bih
- Open Chrome Dev: Open your terminal and run Chrome Dev with security flags if you plan to do advanced remote diagnostics, or simply launch it normally:
or open Google Chrome Dev from your desktop environment and navigate to:
google-chrome-unstable http://localhost:8501
http://localhost:8501
graph TD
A[Streamlit Web App] --> B[Tab 1: Signal Explorer & Classifier]
A --> C[Tab 2: Pan-Tompkins Sandbox]
A --> D[Tab 3: PyTorch & PEFT LoRA]
A --> E[Tab 4: OKF Knowledge Base]
A --> F[Tab 5: RAG Chat Assistant]
A --> G[Tab 6: Apache Spark Pipeline]
The Explorer tab serves as the primary interface for visualizing ECG signals, running QRS peak detectors, and obtaining model classification predictions.
- Dynamic Dataset Headers & Clinical Details:
- Whenever you select a dataset (e.g.,
mitdb,afdb,nstdb) from the sidebar, the tab header updates automatically to display the full medical name of the database. - A descriptive clinical alert box displays details regarding the records, origin, and characteristics of the selected database.
- Whenever you select a dataset (e.g.,
- ECG Waveform & Detected R-Peaks:
- Renders a multi-second interactive Plotly signal chart overlaid with detected R-peaks (triangle markers) and clinical annotations (circular markers).
- Heartbeat Classification Breakdown Table:
- Lists every detected heartbeat index, its location in seconds, the predicted AAMI class (N, S, V, F, Q), classification confidence, and unsupervised anomaly metrics.
- Cells containing abnormal clinical classes (like Ventricular
Vor SupraventricularS) are highlighted in red and orange respectively.
- Grad-CAM Explainable AI (XAI) Focus Heatmap:
- When a heartbeat is selected, checking the
🔍 Show Grad-CAM Focus Heatmapbox displays a Plotly signal trace colored by the model's neural attention weight (gradient-weighted class activation mapping). Red regions indicate segments that the model heavily relied on for its classification decision.
- When a heartbeat is selected, checking the
- Unsupervised Anomaly Overlay:
- Checking the anomaly highlight box calculates the reconstruction Mean Squared Error (MSE) from a convolutional autoencoder trained strictly on normal beats.
- Flags anomalies (
⚠️ Yes) in the beats table when MSE exceeds the sidebar slider threshold. - Plots the original waveform (solid line) and the autoencoder's reconstructed trace (dashed line) together to visually display morphological divergence.
- LLM-Based AI Clinical Explainer:
- Features a button
"💬 Explain Heartbeat with gemma4". When clicked, it packages AAMI predictions, model confidence, pre/post-RR intervals, and autoencoder MSE scores into a structured cardiology prompt. - Queries the local Ollama server running
gemma4to return an expert clinical diagnosis, diagnostic rationale, and recommendations.
- Features a button
Suggested Screenshot File: screenshots/explorer_tab.png
This tab provides a step-by-step visual sandbox to debug the Pan-Tompkins algorithm:
-
Interactive Tuning Sliders:
-
Bandpass Low Cutoff (Hz) (Default:
5.0): Filters low-frequency baseline drift and respiratory artifacts. -
Bandpass High Cutoff (Hz) (Default:
15.0): Filters high-frequency muscle noise and powerline interference. -
Integration Window Width (ms) (Default:
150): Adjusts the width of the moving window integration.
-
Bandpass Low Cutoff (Hz) (Default:
-
Intermediate Waveforms:
- Plots the signal at each stage: Raw Signal
$\rightarrow$ Bandpass Filtered$\rightarrow$ Derivative Filtered$\rightarrow$ Squared Signal$\rightarrow$ Moving Window Integration.
- Plots the signal at each stage: Raw Signal
-
Reset Button:
- Clicking
"🔄 Reset Filters"instantly restores all bandpass cutoffs and window widths to standard clinical defaults.
- Clicking
Suggested Screenshot File: screenshots/pan_tompkins_sandbox.png
Tab 3 provides a control panel to train models directly from the UI:
- Target Selector:
- Choose between fine-tuning the PyTorch 1D ResNet with PEFT LoRA, fine-tuning the Hugging Face ResNet, or training the Unsupervised Anomaly Autoencoder.
- Hyperparameter Sliders:
- Configure Training Epochs, Learning Rate, and Batch Size.
- Training Console:
- Displays real-time training progress logs and loss curves.
- Reset Button:
- Click
"🔄 Reset Hyperparameters"to restore standard clinical training rates and epochs.
- Click
Suggested Screenshot File: screenshots/peft_training_tab.png
These tabs allow clinical inquiries against the Open Knowledge Format (OKF) Bundle:
- Knowledge Catalog Index:
- Browses through 20 reference guides, concepts, and runbooks covering arrhythmia classification, noise testing, and sleep polysomnography.
- RAG Chat Panel:
- Enter questions regarding QRS detection or cardiac pathology.
- TF-IDF Hallucination Guardrails: Irrelevant queries (e.g., asking about history, pop culture, or unrelated fields) trigger a strict guardrail intercept (
⚠️ Guardrail Alert: Query Blocked) since their similarity index falls below0.05.
Suggested Screenshot File: screenshots/rag_chat_tab.png
To run a live simulation of a bedside patient monitor scrolling waveform with real-time on-the-fly classifications:
- Go to the Signal Explorer (Tab 1) and scroll down to Live Signal Playback Sandbox.
- Adjust Playback Speed (steps/sec) and Visible Samples Window.
- Click
▶️ Play Animation. - Observe the waveform drawing itself from left to right.
- Watch R-peaks get detected on-the-fly, with clinical letters (🟢 N, 🔴 V, 🟠 S, 🟣 F, ⚪ Q) flashing directly above peaks as they scroll across the screen!
- Click ⏸️ Pause Animation at any time, or click "🔄 Reset Playback" to restore default simulation rates.
For portability and reusability, trained models are saved in the project root:
-
models/trained/$\rightarrow$ Random Forest classifiers. -
models/weights/peft_adapter/$\rightarrow$ PyTorch LoRA adapter configuration and weights. -
models/weights/hf_resnet_finetuned.pt$\rightarrow$ Hugging Face ResNet weights. -
models/weights/autoencoder.pt$\rightarrow$ Unsupervised Autoencoder weights.