# rmnkumrydv-cloud/glucotwin architecture

> Architecture diagram of the GitHub repository rmnkumrydv-cloud/glucotwin, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-17). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/rmnkumrydv-cloud/glucotwin
- Repository: https://github.com/rmnkumrydv-cloud/glucotwin

## Overview

GlucoTwin turns wearable and health-record data into patient-specific glucose forecasts, spike alerts, uncertainty bands, and what-if comparisons. The doctor’s main workflow runs from the Streamlit dashboard through TwinService to a forecasting model; FastAPI exposes a separate service interface. The supporting preparation and modeling stages preserve causal inputs and patient-level evaluation. README-documented capabilities include the optional neural model, ensemble, explanations, and personalization. Some links rely on README or source-index references rather than complete source excerpts.

Main entry points: [`src/glucotwin/service.py`](https://github.com/rmnkumrydv-cloud/glucotwin/blob/main/src/glucotwin/service.py) (TwinService), [`src/glucotwin/prepare.py`](https://github.com/rmnkumrydv-cloud/glucotwin/blob/main/src/glucotwin/prepare.py) (Data preparation), [`src/glucotwin/features.py`](https://github.com/rmnkumrydv-cloud/glucotwin/blob/main/src/glucotwin/features.py) (Feature fusion), [`src/glucotwin/train.py`](https://github.com/rmnkumrydv-cloud/glucotwin/blob/main/src/glucotwin/train.py) (Model training)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_experience["Doctor experience"]
  node_dashboard["Doctor dashboard<br/>[dashboard.py]"]
  node_api["FastAPI interface<br/>[api.py]"]
  node_service["TwinService<br/>[service.py]"]
end

subgraph group_data["Patient data"]
  node_source_data[("CGMacros data")]
  node_prep["Data preparation<br/>[prepare.py]"]
  node_wearables["Wearable stream<br/>[data.py]"]
  node_ehr["FHIR health record<br/>[ehr.py]"]
  node_quality["Data quality<br/>[quality.py]"]
end

subgraph group_modeling["Twin modeling"]
  node_fusion["Feature fusion<br/>[features.py]"]
  node_meal_model["Meal absorption<br/>[meals.py]"]
  node_trainer["Model training<br/>[train.py]"]
  node_lgbm["LightGBM twin<br/>[models.py]"]
  node_neural["P-MRGF neural twin<br/>[nn.py]"]
  node_ensemble["Forecast ensemble<br/>[ensemble.py]"]
end

subgraph group_evaluation["Validation and serving"]
  node_splits["Patient splits<br/>[splits.py]"]
  node_evaluation["Forecast evaluation<br/>[evaluate.py]"]
  node_artifacts[("Model artifacts")]
end

node_doctor(("Diabetologist"))

node_doctor -->|"uses"| node_dashboard
node_dashboard -->|"requests forecasts"| node_service
node_api -->|"serves requests"| node_service
node_source_data -->|"provides inputs"| node_prep
node_prep -->|"loads stream"| node_wearables
node_prep -->|"builds records"| node_ehr
node_prep -->|"builds dataset"| node_fusion
node_prep -->|"writes splits"| node_splits
node_prep -->|"reports quality"| node_quality
node_fusion -->|"uses static features"| node_ehr
node_fusion -->|"models absorption"| node_meal_model
node_trainer -->|"trains on features"| node_fusion
node_trainer -->|"uses folds"| node_splits
node_trainer -->|"fits models"| node_lgbm
node_trainer -->|"scores predictions"| node_evaluation
node_trainer -->|"saves models"| node_artifacts
node_neural -->|"uses features"| node_fusion
node_ensemble -.->|"blends forecasts"| node_lgbm
node_ensemble -.->|"blends forecasts"| node_neural
node_service -->|"loads and predicts"| node_lgbm
node_service -->|"reads records"| node_ehr
node_service -->|"builds forecast inputs"| node_fusion
node_service -->|"loads model data"| node_artifacts
node_service -.->|"may use blend"| node_ensemble
node_service -->|"returns forecast and explanations"| node_doctor

click node_dashboard "https://github.com/rmnkumrydv-cloud/glucotwin/blob/main/app/dashboard.py"
click node_api "https://github.com/rmnkumrydv-cloud/glucotwin/blob/main/app/api.py"
click node_prep "https://github.com/rmnkumrydv-cloud/glucotwin/blob/main/src/glucotwin/prepare.py"
click node_wearables "https://github.com/rmnkumrydv-cloud/glucotwin/blob/main/src/glucotwin/data.py"
click node_ehr "https://github.com/rmnkumrydv-cloud/glucotwin/blob/main/src/glucotwin/ehr.py"
click node_quality "https://github.com/rmnkumrydv-cloud/glucotwin/blob/main/src/glucotwin/quality.py"
click node_fusion "https://github.com/rmnkumrydv-cloud/glucotwin/blob/main/src/glucotwin/features.py"
click node_meal_model "https://github.com/rmnkumrydv-cloud/glucotwin/blob/main/src/glucotwin/meals.py"
click node_splits "https://github.com/rmnkumrydv-cloud/glucotwin/blob/main/src/glucotwin/splits.py"
click node_trainer "https://github.com/rmnkumrydv-cloud/glucotwin/blob/main/src/glucotwin/train.py"
click node_lgbm "https://github.com/rmnkumrydv-cloud/glucotwin/blob/main/src/glucotwin/models.py"
click node_neural "https://github.com/rmnkumrydv-cloud/glucotwin/blob/main/src/glucotwin/nn.py"
click node_ensemble "https://github.com/rmnkumrydv-cloud/glucotwin/blob/main/src/glucotwin/ensemble.py"
click node_evaluation "https://github.com/rmnkumrydv-cloud/glucotwin/blob/main/src/glucotwin/evaluate.py"
click node_artifacts "https://github.com/rmnkumrydv-cloud/glucotwin/tree/main/artifacts"
click node_service "https://github.com/rmnkumrydv-cloud/glucotwin/blob/main/src/glucotwin/service.py"

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_dashboard,node_api,node_service toneBlue
class node_source_data,node_prep,node_wearables,node_ehr,node_quality toneAmber
class node_fusion,node_meal_model,node_trainer,node_lgbm,node_neural,node_ensemble toneMint
class node_splits,node_evaluation,node_artifacts toneRose
class node_doctor toneIndigo
```

## Components

### Doctor experience

- [Doctor dashboard](https://github.com/rmnkumrydv-cloud/glucotwin/blob/main/app/dashboard.py): `app/dashboard.py`
- [FastAPI interface](https://github.com/rmnkumrydv-cloud/glucotwin/blob/main/app/api.py): `app/api.py`
- [TwinService](https://github.com/rmnkumrydv-cloud/glucotwin/blob/main/src/glucotwin/service.py): `src/glucotwin/service.py`

### Patient data

- **CGMacros data**
- [Data preparation](https://github.com/rmnkumrydv-cloud/glucotwin/blob/main/src/glucotwin/prepare.py): `src/glucotwin/prepare.py`
- [Wearable stream](https://github.com/rmnkumrydv-cloud/glucotwin/blob/main/src/glucotwin/data.py): `src/glucotwin/data.py`
- [FHIR health record](https://github.com/rmnkumrydv-cloud/glucotwin/blob/main/src/glucotwin/ehr.py): `src/glucotwin/ehr.py`
- [Data quality](https://github.com/rmnkumrydv-cloud/glucotwin/blob/main/src/glucotwin/quality.py): `src/glucotwin/quality.py`

### Twin modeling

- [Feature fusion](https://github.com/rmnkumrydv-cloud/glucotwin/blob/main/src/glucotwin/features.py): `src/glucotwin/features.py`
- [Meal absorption](https://github.com/rmnkumrydv-cloud/glucotwin/blob/main/src/glucotwin/meals.py): `src/glucotwin/meals.py`
- [Model training](https://github.com/rmnkumrydv-cloud/glucotwin/blob/main/src/glucotwin/train.py): `src/glucotwin/train.py`
- [LightGBM twin](https://github.com/rmnkumrydv-cloud/glucotwin/blob/main/src/glucotwin/models.py): `src/glucotwin/models.py`
- [P-MRGF neural twin](https://github.com/rmnkumrydv-cloud/glucotwin/blob/main/src/glucotwin/nn.py): `src/glucotwin/nn.py`
- [Forecast ensemble](https://github.com/rmnkumrydv-cloud/glucotwin/blob/main/src/glucotwin/ensemble.py): `src/glucotwin/ensemble.py`

### Validation and serving

- [Patient splits](https://github.com/rmnkumrydv-cloud/glucotwin/blob/main/src/glucotwin/splits.py): `src/glucotwin/splits.py`
- [Forecast evaluation](https://github.com/rmnkumrydv-cloud/glucotwin/blob/main/src/glucotwin/evaluate.py): `src/glucotwin/evaluate.py`
- [Model artifacts](https://github.com/rmnkumrydv-cloud/glucotwin/tree/main/artifacts): `artifacts`

### Other components

- **Diabetologist**

## Connections

- Diabetologist → Doctor dashboard: uses (evidence: `README.md`)
- Doctor dashboard → TwinService: requests forecasts (evidence: `app/dashboard.py`)
- FastAPI interface → TwinService: serves requests (evidence: `app/api.py`)
- CGMacros data → Data preparation: provides inputs (evidence: `README.md`)
- Data preparation → Wearable stream: loads stream (evidence: `src/glucotwin/prepare.py`)
- Data preparation → FHIR health record: builds records (evidence: `src/glucotwin/prepare.py`)
- Data preparation → Feature fusion: builds dataset (evidence: `src/glucotwin/prepare.py`)
- Data preparation → Patient splits: writes splits (evidence: `src/glucotwin/prepare.py`)
- Data preparation → Data quality: reports quality (evidence: `src/glucotwin/prepare.py`)
- Feature fusion → FHIR health record: uses static features (evidence: `README.md`)
- Feature fusion → Meal absorption: models absorption (evidence: `README.md`)
- Model training → Feature fusion: trains on features (evidence: `src/glucotwin/train.py`)
- Model training → Patient splits: uses folds (evidence: `src/glucotwin/train.py`)
- Model training → LightGBM twin: fits models (evidence: `src/glucotwin/train.py`)
- Model training → Forecast evaluation: scores predictions (evidence: `src/glucotwin/train.py`)
- Model training → Model artifacts: saves models (evidence: `src/glucotwin/train.py`)
- P-MRGF neural twin → Feature fusion: uses features (evidence: `src/glucotwin/nn.py`)
- Forecast ensemble → LightGBM twin: blends forecasts (evidence: `README.md`)
- Forecast ensemble → P-MRGF neural twin: blends forecasts (evidence: `README.md`)
- TwinService → LightGBM twin: loads and predicts (evidence: `src/glucotwin/service.py`)
- TwinService → FHIR health record: reads records (evidence: `src/glucotwin/service.py`)
- TwinService → Feature fusion: builds forecast inputs (evidence: `src/glucotwin/service.py`)
- TwinService → Model artifacts: loads model data (evidence: `src/glucotwin/service.py`)
- TwinService → Forecast ensemble: may use blend (evidence: `README.md`)
- TwinService → Diabetologist: returns forecast and explanations (evidence: `README.md`)
