# eliaslozadam/proyect_adapt-ecg architecture

> Architecture diagram of the GitHub repository eliaslozadam/proyect_adapt-ecg, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-15). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/eliaslozadam/proyect_adapt-ecg
- Repository: https://github.com/eliaslozadam/proyect_adapt-ecg

## Overview

1. **Overall system shape**
   ADAPT-ECG is a research-oriented ECG classification platform built around a **1D CNN in PyTorch** plus a **continual re-training loop**. The repo is organized as a pipeline: external ECG data comes in, signals are preprocessed, the model predicts arrhythmia classes, and later retraining updates the model using a replay buffer to reduce forgetting.

2. **External data boundary**
   The primary external dependency is the **MIT-BIH Arrhythmia Database from PhysioNet**. The repository treats this as the canonical source of ECG signals and annotations. This is the main ingress boundary between the project and the outside world.

3. **Data ingestion and preprocessing**
   The runtime data layer lives in `src/data/`:
   - `ingest.py` handles loading/reading ECG data
   - `preprocess.py` handles signal conditioning, notably the **bandpass filtering and normalization** described in the README  
   This layer is the first internal boundary after raw dataset access and feeds both inference and training workflows.

4. **Modeling and continual learning core**
   The core AI behavior is centered on a **1D convolutional neural network** for ECG classification. The repository strongly suggests a staged lifecycle:
   - base model training
   - re-training / adaptation
   - evaluation  
   The continuous-learning mechanism is backed by a **Replay Buffer**, which is explicitly stored as `models/replay_buffer.npz`.

5. **Model artifacts and persistence**
   The `models/` directory is the persistence boundary for learned state and results:
   - `ecg_cnn_base.pth` and `ADAPT-ECG-RETRAINED.pth` are the main model weights
   - `ecg_cnn_base_meta.json` stores model metadata
   - `retrain_history.json`, `fase3_resultados.json`, and `fase4_resultados.json` capture training/evaluation outputs  
   This indicates the system is largely file-artifact driven rather than relying on a centralized database for ML state.

6. **User-facing interface**
   The interactive front end is a **Streamlit app** in `src/ui/app.py`, with Streamlit configuration in `.streamlit/config.toml`. This UI is the presentation boundary for visualization and interactive inference, likely wrapping the preprocessing + model pipeline for users.

7. **Configuration and environment control**
   `src/config/settings.py` centralizes application settings, while `.env.example` suggests environment-based configuration for paths or runtime parameters. This separation matters because the repo mixes notebooks, scripts, and UI, so configuration is the main way to keep the pipeline reproducible across entry points.

8. **Experimentation and phase-based workflow**
   The `notebooks/` folder shows the project’s research workflow:
   - `fase2_entrenamiento.ipynb`
   - `fase3_reentrenamiento.ipynb`
   - `fase4_evaluacion.ipynb`  
   These notebooks map directly onto the project’s experimental phases and likely correspond to the artifacts in `models/`.

9. **Automation and orchestration**
   The `scripts/` directory contains the operational glue:
   - `run_phase1.py`, `train.ps1`, `retrain.ps1`, `run_api.ps1`
   - report/document generation scripts such as `generar_informe_cnn.py`, `generar_informe_reentrenamiento.py`, `generar_marco_teorico.py`, `generar_notas_sesion.py`
   - conversion utilities like `md_to_docx.py` and `md_to_pdf.py`  
   These scripts form the batch/automation layer around the core ML pipeline and documentation workflow.

10. **Database/schema boundary**
   There is a `db/schema.sql`, but the repository’s main architecture does not appear database-centric from the file layout. It likely defines a supporting schema or an auxiliary persistence layer, while the dominant runtime state remains in model files, JSON, and NPZ artifacts.

11. **Documentation and deliverables**
   A large part of the repo is documentation-heavy: `docs/` contains technical reports, architecture diagrams, and Markdown sources; `Residencia_Profesional (1)/` contains thesis/report LaTeX assets, compiled PDFs, and institutional templates. This means the repo serves both as a functional ML system and as an academic deliverable generator.

12. **How the main data flow fits together**
   The cleanest architectural diagram should show this sequence:
   **MIT-BIH ECG data → ingestion/preprocessing → CNN inference/training → retraining with replay buffer → saved weights/results → Streamlit UI and generated reports**.  
   The key boundaries are the external dataset, the preprocessing layer, the model/retraining core, and the presentation/reporting layer.

Main entry points: [`notebooks/fase3_reentrenamiento.ipynb`](https://github.com/eliaslozadam/proyect_adapt-ecg/blob/main/notebooks/fase3_reentrenamiento.ipynb) (Retrain), [`src/data/preprocess.py`](https://github.com/eliaslozadam/proyect_adapt-ecg/blob/main/src/data/preprocess.py) (Preprocess), [`scripts/generar_informe_cnn.py`](https://github.com/eliaslozadam/proyect_adapt-ecg/blob/main/scripts/generar_informe_cnn.py) (Report scripts), [`src/data/ingest.py`](https://github.com/eliaslozadam/proyect_adapt-ecg/blob/main/src/data/ingest.py) (Ingest)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_group_external["External data"]
  node_node_mitbih[("MIT-BIH<br/>external ECG dataset")]
end

subgraph group_group_data["Data pipeline"]
  node_node_ingest["Ingest<br/>data loader<br/>[ingest.py]"]
  node_node_preprocess["Preprocess<br/>signal conditioning<br/>[preprocess.py]"]
  node_node_settings["Settings<br/>config<br/>[settings.py]"]
end

subgraph group_group_model["Model core"]
  node_node_train_base["Base train<br/>cnn training"]
  node_node_cnn{{"ECG CNN<br/>1D PyTorch model"}}
  node_node_replay[("Replay buffer<br/>continual memory<br/>[replay_buffer.npz]")]
  node_node_retrain["Retrain<br/>continual learning"]
  node_node_eval["Evaluate<br/>model evaluation"]
end

subgraph group_group_interface["Interface"]
  node_node_ui["Streamlit UI<br/>presentation app<br/>[app.py]"]
end

subgraph group_group_ops["Automation"]
  node_node_run_phase1["Phase 1<br/>orchestration script<br/>[run_phase1.py]"]
  node_node_auto_retrain["Auto retrain<br/>automation<br/>[auto_retrain.py]"]
  node_node_scripts_reports["Report scripts<br/>report generation"]
end

subgraph group_group_artifacts["Artifacts"]
  node_node_base_model[("Base weights<br/>model artifact<br/>[ecg_cnn_base.pth]")]
  node_node_retrained_model[("Retained weights<br/>model artifact")]
  node_node_results["Results JSON<br/>run outputs"]
end

subgraph group_group_docs["Docs"]
  node_node_docs_tech["Tech docs<br/>documentation"]
  node_node_docs_report["Thesis pack<br/>academic deliverable"]
end

node_node_mitbih -->|"raw records"| node_node_ingest
node_node_ingest -->|"signals"| node_node_preprocess
node_node_settings -.->|"paths/config"| node_node_ingest
node_node_settings -.->|"params"| node_node_preprocess
node_node_preprocess -->|"clean data"| node_node_train_base
node_node_train_base -->|"fit"| node_node_cnn
node_node_cnn -->|"save"| node_node_base_model
node_node_base_model -->|"start point"| node_node_retrain
node_node_replay -->|"memory"| node_node_retrain
node_node_retrain -->|"save"| node_node_retrained_model
node_node_retrain -->|"logs"| node_node_results
node_node_retrained_model -->|"evaluate"| node_node_eval
node_node_preprocess -->|"interactive input"| node_node_ui
node_node_cnn -->|"predictions"| node_node_ui
node_node_run_phase1 -.->|"orchestrate"| node_node_train_base
node_node_auto_retrain -.->|"trigger"| node_node_retrain
node_node_scripts_reports -.->|"generate"| node_node_docs_tech
node_node_scripts_reports -.->|"generate"| node_node_docs_report
node_node_results -->|"inputs"| node_node_scripts_reports
node_node_ui -.->|"surface outputs"| node_node_results

click node_node_ingest "https://github.com/eliaslozadam/proyect_adapt-ecg/blob/main/src/data/ingest.py"
click node_node_preprocess "https://github.com/eliaslozadam/proyect_adapt-ecg/blob/main/src/data/preprocess.py"
click node_node_settings "https://github.com/eliaslozadam/proyect_adapt-ecg/blob/main/src/config/settings.py"
click node_node_train_base "https://github.com/eliaslozadam/proyect_adapt-ecg/blob/main/notebooks/fase2_entrenamiento.ipynb"
click node_node_replay "https://github.com/eliaslozadam/proyect_adapt-ecg/blob/main/models/replay_buffer.npz"
click node_node_retrain "https://github.com/eliaslozadam/proyect_adapt-ecg/blob/main/notebooks/fase3_reentrenamiento.ipynb"
click node_node_eval "https://github.com/eliaslozadam/proyect_adapt-ecg/blob/main/notebooks/fase4_evaluacion.ipynb"
click node_node_ui "https://github.com/eliaslozadam/proyect_adapt-ecg/blob/main/src/ui/app.py"
click node_node_run_phase1 "https://github.com/eliaslozadam/proyect_adapt-ecg/blob/main/scripts/run_phase1.py"
click node_node_auto_retrain "https://github.com/eliaslozadam/proyect_adapt-ecg/blob/main/scripts/auto_retrain.py"
click node_node_scripts_reports "https://github.com/eliaslozadam/proyect_adapt-ecg/blob/main/scripts/generar_informe_cnn.py"
click node_node_base_model "https://github.com/eliaslozadam/proyect_adapt-ecg/blob/main/models/ecg_cnn_base.pth"
click node_node_retrained_model "https://github.com/eliaslozadam/proyect_adapt-ecg/blob/main/models/ADAPT-ECG-RETRAINED.pth"
click node_node_results "https://github.com/eliaslozadam/proyect_adapt-ecg/blob/main/models/retrain_history.json"
click node_node_docs_tech "https://github.com/eliaslozadam/proyect_adapt-ecg/blob/main/docs/ADAPT-ECG_Documentacion_Tecnica.md"
click node_node_docs_report "https://github.com/eliaslozadam/proyect_adapt-ecg/blob/main/Residencia_Profesional (1)/Residencia_Main.tex"

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_node_mitbih,node_node_docs_tech,node_node_docs_report toneBlue
class node_node_ingest,node_node_preprocess,node_node_settings toneAmber
class node_node_train_base,node_node_cnn,node_node_replay,node_node_retrain,node_node_eval toneMint
class node_node_ui toneRose
class node_node_run_phase1,node_node_auto_retrain,node_node_scripts_reports toneIndigo
class node_node_base_model,node_node_retrained_model,node_node_results toneTeal
```

## Components

### External data

Outside ECG source used as the canonical dataset boundary.

- **MIT-BIH**: external ECG dataset · PhysioNet arrhythmia data and annotations used as the source boundary.

### Data pipeline

Ingestion and preprocessing for ECG signals.

- [Ingest](https://github.com/eliaslozadam/proyect_adapt-ecg/blob/main/src/data/ingest.py): data loader · Reads ECG records and organizes raw inputs for downstream processing. · `src/data/ingest.py`
- [Preprocess](https://github.com/eliaslozadam/proyect_adapt-ecg/blob/main/src/data/preprocess.py): signal conditioning · Bandpass filtering, normalization, and other ECG conditioning steps. · `src/data/preprocess.py`
- [Settings](https://github.com/eliaslozadam/proyect_adapt-ecg/blob/main/src/config/settings.py): config · Central application settings for paths and runtime parameters. · `src/config/settings.py`

### Model core

Training, retraining, replay, and evaluation around the CNN.

- [Base train](https://github.com/eliaslozadam/proyect_adapt-ecg/blob/main/notebooks/fase2_entrenamiento.ipynb): cnn training · Initial training stage for the 1D CNN classifier. · `notebooks/fase2_entrenamiento.ipynb`
- **ECG CNN**: 1D PyTorch model · Primary classifier that predicts arrhythmia classes from ECG signals.
- [Replay buffer](https://github.com/eliaslozadam/proyect_adapt-ecg/blob/main/models/replay_buffer.npz): continual memory · Stored replay samples used to reduce catastrophic forgetting during retraining. · `models/replay_buffer.npz`
- [Retrain](https://github.com/eliaslozadam/proyect_adapt-ecg/blob/main/notebooks/fase3_reentrenamiento.ipynb): continual learning · Adaptation loop that updates the model with replay-assisted retraining. · `notebooks/fase3_reentrenamiento.ipynb`
- [Evaluate](https://github.com/eliaslozadam/proyect_adapt-ecg/blob/main/notebooks/fase4_evaluacion.ipynb): model evaluation · Assessment stage for retrained and base models. · `notebooks/fase4_evaluacion.ipynb`

### Interface

Interactive UI and runtime entrypoints for users.

- [Streamlit UI](https://github.com/eliaslozadam/proyect_adapt-ecg/blob/main/src/ui/app.py): presentation app · Interactive interface for exploration, inference, and visualization. · `src/ui/app.py`

### Automation

Scripts and notebooks that orchestrate experiments, retraining, and reporting.

- [Phase 1](https://github.com/eliaslozadam/proyect_adapt-ecg/blob/main/scripts/run_phase1.py): orchestration script · Entry script for the first phase of the pipeline. · `scripts/run_phase1.py`
- [Auto retrain](https://github.com/eliaslozadam/proyect_adapt-ecg/blob/main/scripts/auto_retrain.py): automation · Automation around retraining and periodic adaptation. · `scripts/auto_retrain.py`
- [Report scripts](https://github.com/eliaslozadam/proyect_adapt-ecg/blob/main/scripts/generar_informe_cnn.py): report generation · Scripts that generate reports, notes, and document conversions. · `scripts/generar_informe_cnn.py`

### Artifacts

Persisted models, histories, and report outputs.

- [Base weights](https://github.com/eliaslozadam/proyect_adapt-ecg/blob/main/models/ecg_cnn_base.pth): model artifact · Persisted initial CNN weights. · `models/ecg_cnn_base.pth`
- [Retained weights](https://github.com/eliaslozadam/proyect_adapt-ecg/blob/main/models/ADAPT-ECG-RETRAINED.pth): model artifact · Persisted retrained CNN weights. · `models/ADAPT-ECG-RETRAINED.pth`
- [Results JSON](https://github.com/eliaslozadam/proyect_adapt-ecg/blob/main/models/retrain_history.json): run outputs · Training and evaluation result files and history logs. · `models/retrain_history.json`

### Docs

Technical and academic documentation deliverables.

- [Tech docs](https://github.com/eliaslozadam/proyect_adapt-ecg/blob/main/docs/ADAPT-ECG_Documentacion_Tecnica.md): documentation · Technical documentation and architecture writeups. · `docs/ADAPT-ECG_Documentacion_Tecnica.md`
- [Thesis pack](https://github.com/eliaslozadam/proyect_adapt-ecg/blob/HEAD/Residencia_Profesional%20(1)/Residencia_Main.tex): academic deliverable · Residencia Profesional thesis, templates, and compiled deliverables. · `Residencia_Profesional (1)/Residencia_Main.tex`

## Connections

- MIT-BIH → Ingest: raw records
- Ingest → Preprocess: signals
- Settings → Ingest: paths/config
- Settings → Preprocess: params
- Preprocess → Base train: clean data
- Base train → ECG CNN: fit
- ECG CNN → Base weights: save
- Base weights → Retrain: start point
- Replay buffer → Retrain: memory
- Retrain → Retained weights: save
- Retrain → Results JSON: logs
- Retained weights → Evaluate: evaluate
- Preprocess → Streamlit UI: interactive input
- ECG CNN → Streamlit UI: predictions
- Phase 1 → Base train: orchestrate
- Auto retrain → Retrain: trigger
- Report scripts → Tech docs: generate
- Report scripts → Thesis pack: generate
- Results JSON → Report scripts: inputs
- Streamlit UI → Results JSON: surface outputs
