How saravanan2005real/sleep-apnea-prediction-using-ecg works
Purpose and execution
This repository is a Python/Streamlit preliminary sleep-apnea screening tool. It analyzes ECG-derived signal windows, extracts statistical and HRV-style features, applies a pretrained Random Forest classifier, and presents predicted apnea severity with visual feedback and chatbot support.
Primary runtime entry point: app.py, launched with streamlit run app.py. Model development/evaluation is separated in main.py.
Presentation and orchestration
app.py is the application boundary between users and the diagnostic pipeline. It owns the interactive Streamlit dashboard: ECG input or selection, signal/result visualizations, prediction display, severity messaging, and access to conversational support.
Grounding paths:
• app.py
• chatbot.py
The UI should be treated as a screening interface, not a clinical decision system; the README explicitly positions results as preliminary and educational.
ECG preprocessing and windowing
The inference flow starts with raw ECG data and prepares it for model consumption. The documented architecture divides signals into 30-second windows before feature extraction. This establishes the key data boundary: continuous/raw ECG input becomes a fixed-duration numerical segment suitable for a tabular ML model.
Grounding paths:
• app.py
• main.py
WFDB is a material integration for ECG-record handling according to the repository documentation and dependencies implied by the described runtime.
Feature-engineering contract
Each prepared ECG window is transformed into a 12-feature vector. The documented contract includes time-domain statistics (mean, deviation, extrema, median, quartiles), energy/RMS values, zero-crossing complexity, and difference-based HRV indicators.
Grounding paths:
• main.py
• app.py
This feature vector is the critical compatibility boundary between training and serving: inference must preserve the same feature count, ordering, preprocessing, and semantics used to train the serialized model.
Model training and evaluation
main.py is the offline ML lifecycle component. It trains and evaluates a Random Forest model using the PhysioNet Apnea-ECG dataset described in the README, then selects/persists the best model artifact. Training data acquisition and evaluation are distinct from the interactive serving path.
Grounding paths:
• main.py
• best_sleep_apnea_model.pkl
Scikit-learn is the central ML runtime. Reported model metrics belong to the documented trained-model context and should not be interpreted as live validation of arbitrary user inputs.
Serialized inference artifact
best_sleep_apnea_model.pkl is the deployable model boundary used by the dashboard. At application startup or prediction time, serving code loads this pickle and passes extracted feature vectors to the Random Forest for class/probability output.
Grounding paths:
• best_sleep_apnea_model.pkl
• app.py
The output is translated from model-level prediction into user-facing Normal, Mild, Moderate, or Severe severity categories.
Chatbot support subsystem
chatbot.py provides an integrated assistant for sleep-apnea questions. It is adjacent to, rather than part of, the ECG classification pipeline: it supports education and interaction but should not alter feature extraction, model inference, or severity classification.
Grounding paths:
• chatbot.py
• app.py
Runtime and dependencies
The application requires Python 3.8+, Streamlit for the dashboard, scikit-learn for Random Forest inference/training, and WFDB for ECG-oriented data handling. Dependency installation is defined by requirements.txt.
Grounding paths:
• requirements.txt
• app.py
• main.py
Main entry points: app.py Streamlit dashboard · best_sleep_apnea_model.pkl Trained RF artifact · main.py Feature engineering · requirements.txt Python dependencies
Components 12
Interactive Screening
Streamlit serving path for preliminary screening.
- Raw ECG input · Continuous ECG record supplied or selected in the dashboard.
app.py - Streamlit dashboard · Interactive screening interface and pipeline orchestrator.
app.py - 30-second windows · Fixed-duration ECG segments for tabular inference.
app.py - 12-feature vector · Statistical, energy, zero-crossing, and HRV-style features.
app.py - Trained RF artifact · Deployable Random Forest model loaded by serving.
best_sleep_apnea_model.pkl - Severity result · Normal, Mild, Moderate, or Severe user-facing result.
app.py - Chatbot support · Sleep-apnea questions support outside the classification flow.
chatbot.py
Offline ML Lifecycle
Training and evaluation are separate from serving.
- PhysioNet Apnea-ECG
main.py - Feature engineering · Uses the same 12-feature compatibility contract as inference.
main.py - RF training and evaluation · Trains, evaluates, and selects the best Random Forest.
main.py
Other components
- User
- Python dependencies · Defines Streamlit, scikit-learn, and WFDB requirements.
requirements.txt
Connections 14
- User → Streamlit dashboard: interacts with
- Streamlit dashboard → Raw ECG input: accepts or selects
- Raw ECG input → 30-second windows: segments
- 30-second windows → 12-feature vector: extracts
- 12-feature vector → Trained RF artifact: predicts with
- Trained RF artifact → Severity result: class and probability
- Severity result → Streamlit dashboard: visualizes
- Streamlit dashboard → Chatbot support: provides access to
- PhysioNet Apnea-ECG → Feature engineering: source records
- Feature engineering → RF training and evaluation: feature vectors
- RF training and evaluation → Trained RF artifact: persists best model
- Python dependencies → Streamlit dashboard: runtime packages
- Python dependencies → RF training and evaluation: ML and ECG packages
- 12-feature vector → Feature engineering: shared feature semantics