A comprehensive system for predicting future blood glucose levels for patients with diabetes, with validation tools to ensure accuracy and clinical safety.
This project provides:
- Blood Glucose Prediction APIs: Real-time APIs that predict future glucose levels based on current and past data.
- Validation Framework: Tools to validate prediction accuracy against historical data.
- Web Interface: User-friendly interface for accessing predictions and visualizing results.
Sample Clarke Error Grid Analysis showing prediction clinical accuracy
This system requires two components running simultaneously:
- Backend API Server (port 8001) - Handles predictions
- Web Interface (port 5001) - Provides user interface
Both components must be running for the system to work properly.
# Terminal 1: Start the API server (MUST use uvicorn)
python -m uvicorn simple_glucose_api:app --host 0.0.0.0 --port 8001
# Terminal 2: Start the web interface
python web_interface.pyThen access the web interface at: http://localhost:5001
- Username: admin
- Password: admin
Before running, ensure you have all required packages:
# Install all dependencies
pip install -r requirements.txt
# If you encounter issues with FastAPI or uvicorn
pip install fastapi uvicorn.
├── simple_glucose_api.py # Simplified prediction API
├── glucose_prediction_api.py # ML model-based prediction API
├── test_simple_api.py # Testing script for simple API
├── validate_api.py # Prediction validation framework
├── compare_api_predictions.py # API comparison tool
├── web_interface.py # Web interface for the prediction system
│
├── models/ # Trained machine learning models
│ ├── 570/ # Models for patient 570
│ │ ├── rf_15min.pkl # Random Forest model for 15-min predictions
│ │ └── ... # Other models for different horizons
│ ├── 575/ # Models for patient 575
│ └── ... # Models for other patients
│
├── features/ # Feature datasets
│ ├── 570_test_features.csv # Test features for patient 570
│ ├── 570_train_features.csv # Training features for patient 570
│ ├── 2018_test_features.csv # 2018 test dataset (multiple patients)
│ ├── 2020_test_features.csv # 2020 test dataset (multiple patients)
│ └── ... # Other feature files
│
├── templates/ # HTML templates for web interface
│ ├── index.html # Main page template
│ ├── login.html # Login page template
│ └── ... # Other templates
│
├── static/ # Static assets for web interface
│ ├── css/ # CSS stylesheets
│ ├── js/ # JavaScript files
│ └── img/ # Images
│
├── documentation/ # Documentation files
│ ├── VALIDATION_README.md # Validation guide
│ ├── API_VALIDATION_OVERVIEW.md # Validation suite overview
│ └── VALIDATION_RESULTS_REPORT.md # Validation results analysis
│
├── .gitignore # Git ignore file
├── requirements.txt # Project dependencies
├── LICENSE # MIT license
└── README.md # This file
Two types of prediction APIs are available:
-
Simple Glucose API (
simple_glucose_api.py): Rule-based prediction that doesn't require pre-trained models. Provides quick, reliable predictions. -
ML-based API (
glucose_prediction_api.py): Uses trained machine learning models for more accurate predictions. Requires compatible scikit-learn version.
Both APIs provide predictions for multiple time horizons (15, 30, 45, 60 minutes into the future).
# Clone the repository
git clone https://github.com/yourusername/Blood-Glucose-Level-Prediction.git
cd Blood-Glucose-Level-Prediction
# Create and activate 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.txt
# Generate sample data (since the actual dataset requires a DUA)
python create_sample_data.pyTo quickly run through the entire workflow, use the demo script:
./run_demo.shThis script will:
- Check and install dependencies
- Generate sample data
- Preprocess the data
- Start the prediction API
- Test the API with sample data
For a more detailed walkthrough, see END_TO_END_DEMO.md.
This project uses the OhioT1DM dataset format, which contains data for people with type 1 diabetes including:
- CGM blood glucose readings
- Insulin doses
- Meal information
- Self-reported life events
For legal and ethical reasons, the actual OhioT1DM dataset is not included in this repository as it requires a Data Use Agreement (DUA). Instead, we provide:
- A sample data generator (
create_sample_data.py) to create synthetic data in the same format - A data cleaning tool (
clean_data.py) for managing dataset files - A convenient management script (
manage_data.sh) for all data operations
# Generate synthetic sample data
./manage_data.sh create-sample
# Remove real data but keep sample data
./manage_data.sh clean-keep
# Display all commands
./manage_data.sh helpSee SAMPLE_DATA_README.md for detailed information on working with the dataset.
# Start the API server (IMPORTANT: must use uvicorn)
python -m uvicorn simple_glucose_api:app --host 0.0.0.0 --port 8001
# In a separate terminal, start the web interface
python web_interface.py
# Test the API directly (optional)
python test_simple_api.pyThe API will be available at http://localhost:8001/ and the web interface at http://localhost:5001/.
- "ModuleNotFoundError: No module named 'fastapi'": Install FastAPI using
pip install fastapi uvicorn - "Connection refused" in web interface: Make sure the API server is running on port 8001
- Cannot connect to web interface: Ensure Flask is installed with
pip install flask requests - Wrong prediction results: Check that you're using the correct API endpoint and data format
python simple_glucose_api.py is not sufficient. You must use the uvicorn command as shown above.
- GET / - API information
- GET /patients - List available patient IDs
- GET /horizons?patient_id=X - List available prediction horizons
- POST /predict - Make a prediction
Example prediction request:
{
"patient_id": 570,
"glucose_value": 180.0,
"glucose_diff": 2.5,
"glucose_diff_rate": 0.5,
"hour": 14,
"day_of_week": 2,
"insulin_dose": 0,
"insulin_dose_1h": 0,
"carbs_1h": 0
}The web interface provides a user-friendly dashboard for interacting with the API:
# Make sure the API is running first
python -m uvicorn simple_glucose_api:app --host 0.0.0.0 --port 8001
# Then start the web interface (in a separate terminal)
python web_interface.py- Open your browser and navigate to: http://localhost:5001
- Log in with the default credentials:
- Username: admin
- Password: admin
- Interactive dashboard for glucose predictions
- Real-time charts and visualizations
- Patient selection and management
- Historical prediction tracking
- User authentication and role management
For more details about the web interface, see WEB_INTERFACE_README.md.
The project includes a comprehensive validation framework to assess prediction accuracy:
# Validate against test data
./validate_api.py --test_file features/570_test_features.csv --patient_ids 570 --horizons 15,30 --api_url http://localhost:8001See VALIDATION_README.md for detailed usage instructions.
- Mean Absolute Error (MAE)
- Root Mean Squared Error (RMSE)
- R² score
- In-range accuracy (70-180 mg/dL)
- Clarke Error Grid analysis
./compare_api_predictions.py --test_file features/570_test_features.csv --patient_id 570 --api_url_1 http://localhost:8000 --api_url_2 http://localhost:8001 --api_1_name "ML-Based" --api_2_name "Simple"The Simple Glucose Prediction API has been validated with the following results:
- 15-min Horizon: RMSE 18.87 mg/dL, 91.25% Clarke Zone A
- 30-min Horizon: RMSE 29.84 mg/dL, 82.00% Clarke Zone A
Average API response time: 1.86ms
- The project uses a synthetic dataset that mimics the structure and format of the Ohio T1DM Dataset
- For research purposes, the actual OhioT1DM dataset requires a Data Use Agreement (DUA)
- See SAMPLE_DATA_README.md for information on the sample data and how to obtain the real dataset
- Clarke Error Grid implementation adapted from multiple sources
This project is licensed under the MIT License - see the LICENSE file for details.