# saravanan2005real/sleep-apnea-prediction-using-ecg architecture

> Architecture diagram of the GitHub repository saravanan2005real/sleep-apnea-prediction-using-ecg, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-27). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/saravanan2005real/sleep-apnea-prediction-using-ecg
- Repository: https://github.com/saravanan2005real/sleep-apnea-prediction-using-ecg

## Overview

#### 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`](https://github.com/saravanan2005real/sleep-apnea-prediction-using-ecg/blob/main/app.py) (Streamlit dashboard), [`best_sleep_apnea_model.pkl`](https://github.com/saravanan2005real/sleep-apnea-prediction-using-ecg/blob/main/best_sleep_apnea_model.pkl) (Trained RF artifact), [`main.py`](https://github.com/saravanan2005real/sleep-apnea-prediction-using-ecg/blob/main/main.py) (Feature engineering), [`requirements.txt`](https://github.com/saravanan2005real/sleep-apnea-prediction-using-ecg/blob/main/requirements.txt) (Python dependencies)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_serving["Interactive Screening"]
  node_ecg["Raw ECG input<br/>signal input<br/>[app.py]"]
  node_app["Streamlit dashboard<br/>application entry<br/>[app.py]"]
  node_windowing["30-second windows<br/>signal preparation<br/>[app.py]"]
  node_features["12-feature vector<br/>feature contract<br/>[app.py]"]
  node_artifact[("Trained RF artifact<br/>serialized model")]
  node_severity["Severity result<br/>screening output<br/>[app.py]"]
  node_chatbot["Chatbot support<br/>education assistant<br/>[chatbot.py]"]
end

subgraph group_ml["Offline ML Lifecycle"]
  node_dataset[("PhysioNet Apnea-ECG<br/>training data<br/>[main.py]")]
  node_training["Feature engineering<br/>training preparation<br/>[main.py]"]
  node_evaluation["RF training and evaluation<br/>ML lifecycle<br/>[main.py]"]
end

node_user(("User<br/>actor"))
node_dependencies["Python dependencies<br/>runtime dependencies<br/>[requirements.txt]"]

node_user -->|"interacts with"| node_app
node_app -->|"accepts or selects"| node_ecg
node_ecg -->|"segments"| node_windowing
node_windowing -->|"extracts"| node_features
node_features -->|"predicts with"| node_artifact
node_artifact -->|"class and probability"| node_severity
node_severity -->|"visualizes"| node_app
node_app -->|"provides access to"| node_chatbot
node_dataset -->|"source records"| node_training
node_training -->|"feature vectors"| node_evaluation
node_evaluation -->|"persists best model"| node_artifact
node_dependencies -.->|"runtime packages"| node_app
node_dependencies -.->|"ML and ECG packages"| node_evaluation
node_features -.->|"shared feature semantics"| node_training

click node_ecg "https://github.com/saravanan2005real/sleep-apnea-prediction-using-ecg/blob/main/app.py"
click node_app "https://github.com/saravanan2005real/sleep-apnea-prediction-using-ecg/blob/main/app.py"
click node_windowing "https://github.com/saravanan2005real/sleep-apnea-prediction-using-ecg/blob/main/app.py"
click node_features "https://github.com/saravanan2005real/sleep-apnea-prediction-using-ecg/blob/main/app.py"
click node_artifact "https://github.com/saravanan2005real/sleep-apnea-prediction-using-ecg/blob/main/best_sleep_apnea_model.pkl"
click node_severity "https://github.com/saravanan2005real/sleep-apnea-prediction-using-ecg/blob/main/app.py"
click node_chatbot "https://github.com/saravanan2005real/sleep-apnea-prediction-using-ecg/blob/main/chatbot.py"
click node_dataset "https://github.com/saravanan2005real/sleep-apnea-prediction-using-ecg/blob/main/main.py"
click node_training "https://github.com/saravanan2005real/sleep-apnea-prediction-using-ecg/blob/main/main.py"
click node_evaluation "https://github.com/saravanan2005real/sleep-apnea-prediction-using-ecg/blob/main/main.py"
click node_dependencies "https://github.com/saravanan2005real/sleep-apnea-prediction-using-ecg/blob/main/requirements.txt"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_ecg,node_app,node_windowing,node_features,node_artifact,node_severity,node_chatbot toneBlue
class node_dataset,node_training,node_evaluation toneAmber
class node_user,node_dependencies toneNeutral
```

## Components

### Interactive Screening

Streamlit serving path for preliminary screening.

- [Raw ECG input](https://github.com/saravanan2005real/sleep-apnea-prediction-using-ecg/blob/main/app.py): signal input · Continuous ECG record supplied or selected in the dashboard. · `app.py`
- [Streamlit dashboard](https://github.com/saravanan2005real/sleep-apnea-prediction-using-ecg/blob/main/app.py): application entry · Interactive screening interface and pipeline orchestrator. · `app.py`
- [30-second windows](https://github.com/saravanan2005real/sleep-apnea-prediction-using-ecg/blob/main/app.py): signal preparation · Fixed-duration ECG segments for tabular inference. · `app.py`
- [12-feature vector](https://github.com/saravanan2005real/sleep-apnea-prediction-using-ecg/blob/main/app.py): feature contract · Statistical, energy, zero-crossing, and HRV-style features. · `app.py`
- [Trained RF artifact](https://github.com/saravanan2005real/sleep-apnea-prediction-using-ecg/blob/main/best_sleep_apnea_model.pkl): serialized model · Deployable Random Forest model loaded by serving. · `best_sleep_apnea_model.pkl`
- [Severity result](https://github.com/saravanan2005real/sleep-apnea-prediction-using-ecg/blob/main/app.py): screening output · Normal, Mild, Moderate, or Severe user-facing result. · `app.py`
- [Chatbot support](https://github.com/saravanan2005real/sleep-apnea-prediction-using-ecg/blob/main/chatbot.py): education assistant · Sleep-apnea questions support outside the classification flow. · `chatbot.py`

### Offline ML Lifecycle

Training and evaluation are separate from serving.

- [PhysioNet Apnea-ECG](https://github.com/saravanan2005real/sleep-apnea-prediction-using-ecg/blob/main/main.py): training data · `main.py`
- [Feature engineering](https://github.com/saravanan2005real/sleep-apnea-prediction-using-ecg/blob/main/main.py): training preparation · Uses the same 12-feature compatibility contract as inference. · `main.py`
- [RF training and evaluation](https://github.com/saravanan2005real/sleep-apnea-prediction-using-ecg/blob/main/main.py): ML lifecycle · Trains, evaluates, and selects the best Random Forest. · `main.py`

### Other components

- **User**: actor
- [Python dependencies](https://github.com/saravanan2005real/sleep-apnea-prediction-using-ecg/blob/main/requirements.txt): runtime dependencies · Defines Streamlit, scikit-learn, and WFDB requirements. · `requirements.txt`

## Connections

- 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
