Skip to content
 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

T4 Glucose Counterfactual Model

This project modifies the original T4 model to handle non-binary counterfactuals for blood glucose prediction based on insulin dosage and timing. It enables "what-if" analysis to predict how blood glucose levels would evolve under different insulin dosage and timing scenarios.

Features

  • Predict coutnerfactual blood glucose trajectories based on insulin dose and timing interventions
  • Support for continuous treatment values of insulin dosage (rather than just binary on/off) and timing before meal
  • Analysis of how treatment modifications affect glucose levels for 3-5 hours
  • Visualization tools for counterfactual comparison

Setup

  1. Clone the repository:
git clone https://github.com/yourusername/t4-glucose.git
cd t4-glucose
  1. Install dependencies:
pip install -r requirements.txt

Training the Model

To train the model on synthetic glucose data:

python train_glucose_model.py --days 60 --epochs 50 --batch_size 64 --device cuda

This will:

  1. Generate 60 days of synthetic glucose data
  2. Train the T4 model for 50 epochs
  3. Save the best model to checkpoints/glucose_model_epochs50_seed42.pt

Options:

  • --days: Number of days of synthetic data to generate (default: 60)
  • --epochs: Number of training epochs (default: 50)
  • --batch_size: Batch size for training (default: 64)
  • --lr: Learning rate (default: 0.001)
  • --seed: Random seed for reproducibility (default: 42)
  • --device: Device to run model on (default: cuda if available, otherwise cpu)

Evaluating Counterfactuals

To evaluate the trained model with counterfactual scenarios:

python evaluate_counterfactuals.py --checkpoint checkpoints/glucose_model_epochs50_seed42.pt --mode both --scenarios 10

This will:

  1. Generate counterfactual scenarios for both insulin dosage and timing modifications
  2. Evaluate the model's predictions for each scenario
  3. Create visualizations of the counterfactual predictions
  4. Save the results to counterfactuals/results/

Options:

  • --checkpoint: Path to the trained model checkpoint (required)
  • --mode: Type of counterfactuals to evaluate: 'dose', 'timing', or 'both' (default: both)
  • --scenarios: Number of scenarios to evaluate (default: 10)
  • --seed: Random seed for reproducibility (default: 42)
  • --device: Device to run model on (default: cuda if available, otherwise cpu)

Visualization Examples

After evaluation, you'll find visualizations for each counterfactual scenario in the counterfactuals/results directory:

  • dose/: Insulin dose counterfactuals with variations of 0.5x to 1.5x the original dose
  • timing/: Insulin timing counterfactuals with variations from 30 minutes earlier to 30 minutes later
  • Summary plots showing the average effect across all scenarios

Model Architecture

The model is based on the T4 architecture with these modifications:

  1. Treatment representation: Two-dimensional with dosage and timing values
  2. Encoder: Modified to handle 2D treatments and output dual propensity scores
  3. Decoder: Specialized output branches for dosage and timing effects

See docs/GLUCOSE_COUNTERFACTUALS.md for a detailed explanation of the modifications.

Data

The model uses synthetic glucose data generated by simulation/simple_glucose_gen.py with:

  • Blood glucose levels (mg/dL)
  • Insulin doses and timing relative to meals
  • Carbohydrate intake
  • Exercise and stress factors

Real-world applications would replace this with actual continuous glucose monitoring (CGM) data.

Examples

For a quick demonstration of the model's capabilities:

# Train a simple model with a smaller dataset for faster results
python train_glucose_model.py --days 10 --epochs 10 --batch_size 32

# Run a basic evaluation
python evaluate_counterfactuals.py --checkpoint checkpoints/glucose_model_epochs10_seed42.pt --mode both --scenarios 3

# View the results
open counterfactuals/results/dose/scenario_1_combined.png
open counterfactuals/results/timing/scenario_1_combined.png

About

Code for paper "Estimating Trustworthy Treatment Effects for Antibiotic Stewardship in Sepsis"

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages