Skip to content

Commit 2dea5c0

Browse files
committed
Added predictions for separate components
1 parent 43131c8 commit 2dea5c0

7 files changed

Lines changed: 1344 additions & 0 deletions

File tree

README.md

Lines changed: 234 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,154 @@ Combines the `generate_prediction` and `get_prediction_dates` functions to retur
6969

7070
-------------------------
7171

72+
### Generate Insulin Prediction
73+
74+
`generate_insulin_prediction(json_file, len=72)`
75+
76+
Generates a glucose prediction based on insulin effects only.
77+
78+
- **Parameters**:
79+
- `json_file`: The JSON data input. See python tests and test files for example inputs.
80+
- `len` (optional): The number of prediction values to generate. Defaults to 72.
81+
- **Returns**: A list of prediction values based only on insulin effects.
82+
83+
-------------------------
84+
85+
### Generate Carbs Prediction
86+
87+
`generate_carbs_prediction(json_file, len=72)`
88+
89+
Generates a glucose prediction based on carbohydrate effects only.
90+
91+
- **Parameters**:
92+
- `json_file`: The JSON data input. See python tests and test files for example inputs.
93+
- `len` (optional): The number of prediction values to generate. Defaults to 72.
94+
- **Returns**: A list of prediction values based only on carbohydrate effects.
95+
96+
-------------------------
97+
98+
### Generate Momentum Prediction
99+
100+
`generate_momentum_prediction(json_file, len=72)`
101+
102+
Generates a glucose prediction based on glucose momentum effects only.
103+
104+
- **Parameters**:
105+
- `json_file`: The JSON data input. See python tests and test files for example inputs.
106+
- `len` (optional): The number of prediction values to generate. Defaults to 72.
107+
- **Returns**: A list of prediction values based only on glucose momentum effects.
108+
109+
-------------------------
110+
111+
### Generate Retrospection Prediction
112+
113+
`generate_retrospection_prediction(json_file, len=72)`
114+
115+
Generates a glucose prediction based on retrospective correction effects only.
116+
117+
- **Parameters**:
118+
- `json_file`: The JSON data input. See python tests and test files for example inputs.
119+
- `len` (optional): The number of prediction values to generate. Defaults to 72.
120+
- **Returns**: A list of prediction values based only on retrospective correction effects.
121+
122+
-------------------------
123+
124+
### Get Insulin Prediction Dates
125+
126+
`get_insulin_prediction_dates(json_file)`
127+
128+
Fetches prediction dates for insulin-only predictions.
129+
130+
- **Parameters**:
131+
- `json_file`: The JSON data input. See python tests and test files for example inputs.
132+
- **Returns**: A list of prediction dates as strings for insulin-only predictions.
133+
134+
-------------------------
135+
136+
### Get Carbs Prediction Dates
137+
138+
`get_carbs_prediction_dates(json_file)`
139+
140+
Fetches prediction dates for carbohydrate-only predictions.
141+
142+
- **Parameters**:
143+
- `json_file`: The JSON data input. See python tests and test files for example inputs.
144+
- **Returns**: A list of prediction dates as strings for carbohydrate-only predictions.
145+
146+
-------------------------
147+
148+
### Get Momentum Prediction Dates
149+
150+
`get_momentum_prediction_dates(json_file)`
151+
152+
Fetches prediction dates for glucose momentum-only predictions.
153+
154+
- **Parameters**:
155+
- `json_file`: The JSON data input. See python tests and test files for example inputs.
156+
- **Returns**: A list of prediction dates as strings for glucose momentum-only predictions.
157+
158+
-------------------------
159+
160+
### Get Retrospection Prediction Dates
161+
162+
`get_retrospection_prediction_dates(json_file)`
163+
164+
Fetches prediction dates for retrospective correction-only predictions.
165+
166+
- **Parameters**:
167+
- `json_file`: The JSON data input. See python tests and test files for example inputs.
168+
- **Returns**: A list of prediction dates as strings for retrospective correction-only predictions.
169+
170+
-------------------------
171+
172+
### Get Insulin Prediction Values and Dates
173+
174+
`get_insulin_prediction_values_and_dates(json_file)`
175+
176+
Combines insulin-only prediction generation and date retrieval.
177+
178+
- **Parameters**:
179+
- `json_file`: The JSON data input. See python tests and test files for example inputs.
180+
- **Returns**: A tuple containing a list of insulin-only prediction values and a list of prediction dates.
181+
182+
-------------------------
183+
184+
### Get Carbs Prediction Values and Dates
185+
186+
`get_carbs_prediction_values_and_dates(json_file)`
187+
188+
Combines carbohydrate-only prediction generation and date retrieval.
189+
190+
- **Parameters**:
191+
- `json_file`: The JSON data input. See python tests and test files for example inputs.
192+
- **Returns**: A tuple containing a list of carbohydrate-only prediction values and a list of prediction dates.
193+
194+
-------------------------
195+
196+
### Get Momentum Prediction Values and Dates
197+
198+
`get_momentum_prediction_values_and_dates(json_file)`
199+
200+
Combines glucose momentum-only prediction generation and date retrieval.
201+
202+
- **Parameters**:
203+
- `json_file`: The JSON data input. See python tests and test files for example inputs.
204+
- **Returns**: A tuple containing a list of glucose momentum-only prediction values and a list of prediction dates.
205+
206+
-------------------------
207+
208+
### Get Retrospection Prediction Values and Dates
209+
210+
`get_retrospection_prediction_values_and_dates(json_file)`
211+
212+
Combines retrospective correction-only prediction generation and date retrieval.
213+
214+
- **Parameters**:
215+
- `json_file`: The JSON data input. See python tests and test files for example inputs.
216+
- **Returns**: A tuple containing a list of retrospective correction-only prediction values and a list of prediction dates.
217+
218+
-------------------------
219+
72220
### Get Glucose Effect Velocity
73221

74222
`get_glucose_effect_velocity(json_file, len=72)`
@@ -326,6 +474,92 @@ See linux_setup.sh
326474

327475
Run command `pytest`.
328476

477+
## Examples and Plotting
478+
479+
### Prediction Trajectories Visualization
480+
481+
The repository includes a comprehensive plotting script that demonstrates the different prediction trajectories generated by each algorithm effects option.
482+
483+
#### Running the Plotting Script
484+
485+
```bash
486+
python examples/plot_predictions.py
487+
```
488+
489+
This script will:
490+
- Load test data from `python_tests/test_files/generate_prediction_input.json`
491+
- Generate predictions using all 5 prediction methods:
492+
- All effects combined (baseline)
493+
- Insulin effects only
494+
- Carbohydrate effects only
495+
- Glucose momentum effects only
496+
- Retrospective correction effects only
497+
- Create comprehensive visualizations showing:
498+
- **Main trajectory plot**: All prediction curves on one graph with historical glucose context
499+
- **Individual effects comparison**: Side-by-side subplots comparing each effect type to the baseline
500+
- **Event annotations**: Carbohydrate entries and insulin bolus doses marked on the timeline
501+
- Calculate and display summary statistics for each prediction method
502+
- Save high-quality PNG and PDF versions of the plots to `examples/plots/`
503+
504+
#### Requirements
505+
506+
Install plotting dependencies:
507+
```bash
508+
pip install matplotlib
509+
```
510+
511+
Or install all requirements:
512+
```bash
513+
pip install -r requirements.txt
514+
```
515+
516+
#### Output
517+
518+
The script generates:
519+
- **Interactive plots**: Displayed during execution for immediate analysis
520+
- **High-quality exports**:
521+
- `examples/plots/glucose_prediction_trajectories.png/pdf` - Main comparison plot
522+
- `examples/plots/algorithm_effects_comparison.png/pdf` - Individual effects subplots
523+
- **Console statistics**: Detailed numerical summary of prediction characteristics
524+
525+
#### Understanding the Plots
526+
527+
- **All Effects (Blue)**: The complete Loop algorithm prediction combining all factors
528+
- **Insulin Only (Purple)**: Shows glucose impact from insulin doses alone
529+
- **Carbs Only (Orange)**: Shows glucose impact from carbohydrate entries alone
530+
- **Momentum Only (Red)**: Shows glucose trends based on recent glucose velocity
531+
- **Retrospection Only (Green)**: Shows adjustments based on prediction vs. actual glucose discrepancies
532+
533+
This visualization helps understand how each algorithm component contributes to the final glucose prediction and can be valuable for:
534+
- **Research**: Analyzing individual algorithm component behavior
535+
- **Education**: Understanding how different factors influence glucose predictions
536+
- **Validation**: Verifying that the new prediction functions work correctly
537+
- **Clinical insight**: Seeing the relative impact of insulin, carbs, and other factors
538+
539+
### Basic Usage Example
540+
541+
```python
542+
# Generate predictions with specific algorithm effects
543+
from loop_to_python_api.api import *
544+
545+
# Initialize exception handlers (recommended)
546+
initialize_exception_handlers()
547+
548+
# Load your input data (JSON format)
549+
input_data = {...} # Your prediction input data
550+
551+
# Generate different types of predictions
552+
all_values, all_dates = get_prediction_values_and_dates(input_data)
553+
insulin_values, insulin_dates = get_insulin_prediction_values_and_dates(input_data)
554+
carbs_values, carbs_dates = get_carbs_prediction_values_and_dates(input_data)
555+
momentum_values, momentum_dates = get_momentum_prediction_values_and_dates(input_data)
556+
retro_values, retro_dates = get_retrospection_prediction_values_and_dates(input_data)
557+
558+
# Compare the different prediction approaches
559+
print(f"All effects prediction range: {min(all_values):.1f} - {max(all_values):.1f} mg/dL")
560+
print(f"Insulin-only prediction range: {min(insulin_values):.1f} - {max(insulin_values):.1f} mg/dL")
561+
```
562+
329563

330564
## Debugging Advice and Disclaimers
331565

0 commit comments

Comments
 (0)