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.
- 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
- Clone the repository:
git clone https://github.com/yourusername/t4-glucose.git
cd t4-glucose- Install dependencies:
pip install -r requirements.txtTo train the model on synthetic glucose data:
python train_glucose_model.py --days 60 --epochs 50 --batch_size 64 --device cudaThis will:
- Generate 60 days of synthetic glucose data
- Train the T4 model for 50 epochs
- 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)
To evaluate the trained model with counterfactual scenarios:
python evaluate_counterfactuals.py --checkpoint checkpoints/glucose_model_epochs50_seed42.pt --mode both --scenarios 10This will:
- Generate counterfactual scenarios for both insulin dosage and timing modifications
- Evaluate the model's predictions for each scenario
- Create visualizations of the counterfactual predictions
- 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)
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 dosetiming/: Insulin timing counterfactuals with variations from 30 minutes earlier to 30 minutes later- Summary plots showing the average effect across all scenarios
The model is based on the T4 architecture with these modifications:
- Treatment representation: Two-dimensional with dosage and timing values
- Encoder: Modified to handle 2D treatments and output dual propensity scores
- Decoder: Specialized output branches for dosage and timing effects
See docs/GLUCOSE_COUNTERFACTUALS.md for a detailed explanation of the modifications.
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.
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