This document demonstrates the complete workflow for blood glucose level prediction using the sample data.
Before starting, make sure to install the required dependencies:
# Create a virtual environment (optional but recommended)
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install dependencies
pip install -r requirements.txtAll required packages are listed in the requirements.txt file, including:
- fastapi and uvicorn for the API
- pandas and numpy for data processing
- scikit-learn for model training and evaluation
- matplotlib and seaborn for visualization
- requests for API testing
The first step is to generate synthetic sample data in the same format as the OhioT1DM dataset:
python create_sample_data.pyThis will create synthetic XML files in the DATA directory:
- DATA/2018/Train/ - Training data for 2018 dataset
- DATA/2018/Test/ - Testing data for 2018 dataset
- DATA/2020/Train/ - Training data for 2020 dataset
- DATA/2020/Test/ - Testing data for 2020 dataset
Each file contains glucose readings, insulin doses, and meal information.
Next, we preprocess the raw XML data to extract structured information and save it as CSV files:
python src/data/preprocess.pyThis creates:
- processed_data/2018/train/ - Processed training data
- processed_data/2018/test/ - Processed testing data
- processed_data/2020/train/ - Processed training data
- processed_data/2020/test/ - Processed testing data
The preprocessing script also generates features for machine learning models:
- features/559_train_features.csv
- features/559_test_features.csv
- features/563_train_features.csv
- features/563_test_features.csv
- features/570_train_features.csv
- features/570_test_features.csv
- features/540_train_features.csv
- features/540_test_features.csv
- features/544_train_features.csv
- features/544_test_features.csv
- features/552_train_features.csv
- features/552_test_features.csv
As well as combined features:
- features/2018_train_features.csv
- features/2018_test_features.csv
- features/2020_train_features.csv
- features/2020_test_features.csv
These features include:
- Current glucose value
- Glucose difference (rate of change)
- Time-based features (hour, day of week)
- Recent insulin doses
- Recent carbohydrate intake
- Target values for different prediction horizons
The Simple Glucose API provides predictions based on the current glucose value and other features:
python simple_glucose_api.pyThis starts a FastAPI-based web service on http://localhost:8001/
You can test the API using the test script:
python test_simple_api.py --patient_id 559 --test_file features/559_test_features.csvOr you can make requests directly:
import requests
import json
import pandas as pd
# Load features data
features_df = pd.read_csv('features/559_train_features.csv')
sample = features_df.iloc[100]
# Prepare request data
request_data = {
"patient_id": int(sample["patient_id"]),
"glucose_value": float(sample["glucose_value"]),
"glucose_diff": float(sample["glucose_diff"]),
"hour": int(sample["hour"]),
"day_of_week": int(sample["day_of_week"]),
"insulin_dose_1h": float(sample["insulin_dose_1h"]),
"insulin_dose_2h": float(sample["insulin_dose_2h"]),
"insulin_dose_4h": float(sample["insulin_dose_4h"]),
"carbs_1h": float(sample["carbs_1h"]),
"carbs_2h": float(sample["carbs_2h"]),
"carbs_4h": float(sample["carbs_4h"])
}
# Make the prediction
response = requests.post("http://localhost:8001/predict", json=request_data)
prediction = response.json()
print(json.dumps(prediction, indent=2))For more accurate predictions, you could train advanced models using the sample data:
python train_improved.py --horizons 15,30,45,60 --batch_size 128 --epochs 10This would train neural network models (LSTM, GRU) for different prediction horizons.
To evaluate prediction accuracy, you can use the validation script:
python validate_api.py --test_file features/559_test_features.csv --patient_ids 559 --horizons 15,30 --api_url http://localhost:8001This will calculate metrics like MAE, RMSE, and Clarke Error Grid analysis.
For a user-friendly interface, you can run the web interface:
python web_interface.pyThis provides a browser-based UI for making predictions and visualizing results.
This workflow demonstrates how to:
- Generate synthetic data that mimics the structure of the OhioT1DM dataset
- Preprocess the data and extract features
- Make predictions using the API
- Evaluate the accuracy of predictions
For research purposes, you should obtain the actual OhioT1DM dataset by signing the Data Use Agreement as described in SAMPLE_DATA_README.md.