Skip to content

HeroicKrishna160905/Project-Endocrine

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

33 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Project–Endocrine

A Research-Grade Artificial Pancreas System (APS) Simulator

🌐 Overview

Project–Endocrine is a complete, modular, research-grade Artificial Pancreas System (APS) simulator combining:

  • Physiological glucose–insulin modeling
  • Adaptive PID-based insulin control
  • Safety-critical logic (IOB, suspension, max-rate limits)
  • Reinforcement Learning (RL) environment
  • Cohort-level virtual patient testing (10–200 subjects)
  • Publication-level visualization and evaluation tools

This simulator aims to provide an open, interpretable, and experiment-friendly APS research platformβ€”ideal for:

  • Biomedical engineering students
  • Control systems researchers
  • RL researchers
  • Medical device prototyping
  • Teaching and simulation environments

🧠 Background: Type-1 Diabetes and Closed-Loop Control

Type-1 Diabetes (T1D) results in absence of endogenous insulin, requiring lifelong external insulin delivery. Maintaining glucose in the target range (70–180 mg/dL) is challenging due to:

  • Meal disturbances
  • Sensor noise, delays, drift
  • Large inter-patient variability
  • Non-linear insulin action
  • Uncertainty in digestion and absorption
  • Noisy CGM readings

An Artificial Pancreas System (APS) automates insulin delivery using:

  • A continuous glucose sensor (CGM)
  • An insulin pump
  • A control algorithm

Project–Endocrine models this entire loop end-to-end.

βš™οΈ System Architecture

       β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
       β”‚         Meal Input            β”‚
       β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                      ↓
           β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
           β”‚   Gut Absorption   β”‚
           β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                      ↓
          β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
          β”‚  Glucose Dynamics    │───► CGM Sensor (Noise)
          β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                     ↑
     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
     β”‚        Insulin Action (X)            β”‚
     β”‚  PK/PD + Delays + Nonlinear Effects  β”‚
     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                        ↑
                β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                β”‚  Controller   β”‚
                β”‚PID / RL / APIDβ”‚
                β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                       ↑
           β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
           β”‚  Safety Layer (IOB,   β”‚
           β”‚ Suspend, Rate Limits) β”‚
           β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                      ↑
                Insulin Delivery

Fully modular β€” each block can be replaced, extended, or studied independently.

🧬 Physiological Model

The simulator includes a nonlinear glucose–insulin model:

Glucose Model

  • Plasma glucose compartment
  • Endogenous glucose production (EGP)
  • Meal appearance via two-stage gut absorption pools
  • Nonlinear insulin-dependent & insulin-independent clearance

Insulin Model

  • Subcutaneous insulin absorption
  • Plasma insulin with clearance
  • Insulin effect (X) compartment
  • Full PK/PD chain

Sensor Model (CGM)

  • Gaussian noise
  • Filtering
  • Measurement bias (optional)

Example subject:

πŸŽ›οΈ Control Architecture

Adaptive PID Controller

  • Proportional: responds to immediate deviation
  • Integral: corrects long-term bias
  • Derivative: anticipates rate-of-change
  • Uses filtered CGM input
  • Includes anti-windup and clamp logic

Adaptive Basal Learning

Learns patient-specific basal patterns:

Safety Layer

  • Predictive low-glucose suspend
  • Negative-trend veto
  • Max insulin rate limiter
  • Insulin-On-Board (IOB) constraints
  • Hard safety clamps

πŸ€– Reinforcement Learning

Project–Endocrine exposes a Gym-style RL environment:

  • reset(), step() API
  • Full state vector extraction
  • Reward functions (TIR-weighted, risk index, smoothness)
  • Multi-patient training
  • Deterministic and stochastic modes

Supports algorithms such as:

  • PPO
  • SAC
  • TD3
  • Model-Based RL

πŸ‘₯ Cohort Simulation (10–200 Subjects)

Each virtual subject varies in:

  • insulin sensitivity
  • carb absorption rate
  • glucose clearance
  • insulin clearance
  • basal metabolic drift
  • noise profile

Cohort Mean Β± STD

Sample traces (first 6 subjects)

Full cohort (200 subjects)

πŸ“Š Cohort Performance Metrics

TIR distribution (200 subjects)

Subject-wise TIR

Glycemic Variability

Mean Glucose Distribution

Mean Glucose vs CV%

TIR / TAR / TBR Boxplot

πŸŽ₯ Simulation Demo

download.mp4

πŸš€ Installation

git clone https://github.com/HeroicKrishna160905/Project-Endocrine.git
cd Project-Endocrine
pip install -r requirements.txt
Or [launch in Google Colab](https://colab.research.google.com/) (recommended).

## πŸ§ͺ Quickstart

### Single Patient Simulation
```python
from endocrine.simulator import APS_Simulator

sim = APS_Simulator()
results = sim.run()
sim.plot()

Cohort Evaluation

from endocrine.evaluation import run_cohort

stats, df = run_cohort(n_subjects=200)
print(stats)
df.head()

🧩 Project Structure

Project-Endocrine/
β”‚
β”œβ”€β”€ endocrine/
β”‚   β”œβ”€β”€ physiology/      # PK/PD model, glucose, insulin
β”‚   β”œβ”€β”€ controller/      # PID, adaptive basal, safety
β”‚   β”œβ”€β”€ rl_env/          # Gym-style RL environment
β”‚   β”œβ”€β”€ evaluation/      # Cohort simulation + metrics
β”‚   └── utils/
β”‚
β”œβ”€β”€ notebooks/
β”‚   β”œβ”€β”€ v6_3_realism.ipynb
β”‚   └── v6_3_RL.ipynb
β”‚
β”œβ”€β”€ assets/              # Figures, demo video
β”œβ”€β”€ results/             # Saved outputs
β”œβ”€β”€ README.md
└── LICENSE

🧾 Key Contributions

Project–Endocrine provides:

  • βœ” A fully custom glucose–insulin PK/PD model (Not dependent on UVA/Padova or simglucose)
  • βœ” A modular adaptive PID controller Designed for interpretability & robustness.
  • βœ” A complete safety layer implementation (LOWS, IOB, rate limits, rapid-drop veto)
  • βœ” A Gym-compatible RL environment Allowing direct integration with PPO/SAC/TD3.
  • βœ” Realistic cohort simulation 200-subject evaluation with variability.
  • βœ” Publication-grade visualizations All included as reproducible notebooks.
  • βœ” Academic grounding Supported by nonlinear control literature β€” e.g., observer-based LMI designs from our professor's paper (2019).

πŸ“š References

Key Literature

  • UVA/Padova T1D Simulator
  • GluCoEnv: High-performance APS RL environment
  • Control-IQ / DiAs APS systems
  • PID/MPC control for APS
  • Reinforcement Learning for glucose control

Our Professor's Related Work

Observer-based nonlinear control for glycemic regulation, using LMI-stabilized designs:

  • 2019 – "Observer based nonlinear control: An LMI approach." (Full text provided internally)

πŸ“ License

MIT License Open for academic, research, and personal use.

🀝 Contributions

Pull requests, ideas, and discussions are welcome.

❀️ Acknowledgments

  • Inspired by global diabetes research.
  • Built with guidance from professors and real control theory literature.
  • Developed with attention to openness, reproducibility, and scientific rigor.

About

A complete Artificial Pancreas simulator : a closed-loop insulin control system combining physiological modeling, PID regulation, adaptive basal learning, safety constraints, and reinforcement learning for intelligent glucose management.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors