Glucose Data Analysis Workflow
Executive Summary
This document describes the complete workflow for processing continuous glucose monitoring (CGM) data from LibreView exports through a series of automated Python scripts to generate comprehensive analysis reports. The system consists of five main processing stages (B1 through E1) plus automation tools.
Key Features:
- Automated pipeline from raw CSV to professional reports
- Standardized folder structure (B-Data-Analyzer)
- Multiple analysis types: time series, AGP, subtypes
- Batch processing capability
- HTML, PDF, and QMD report generation
System Architecture
Folder Structure
The entire system follows a standardized B-Data-Analyzer folder structure:
Top Folder/
βββ B-Data-Analyzer/
βββ A-Data/ # Raw data storage
β βββ {name}_glucose_{mm-dd-yyyy}/ # CSV-named folder
β βββ {name}_glucose_{mm-dd-yyyy}.csv # Original LibreView CSV
β βββ B1-Glucose-Data/ # Daily CSV files
β βββ B2-Yearly-Analysis/ # Yearly reports
β βββ C1-Daily-CSV-to-Subtypes-Analysis/ # Subtype analysis
β βββ D1-Daily-CSV-to-AGP-Report/ # AGP reports
β
βββ B-Engines/ # Processing scripts
β βββ B1-COMPLETE-AUTO.py # Stage 1: CSV splitter
β βββ B2-COMPLETE-AUTO.py # Stage 2: Yearly analysis
β βββ C1-COMPLETE-AUTO.py # Stage 3: Subtype analysis
β βββ D1-COMPLETE-AUTO.py # Stage 4: AGP analysis
β βββ E1-COMPLETE-AUTO.py # Stage 5: QMD reports
β βββ automate.xlsx # Automation config
β βββ excel_automate.py # Automation helper
β βββ run_all.py # Batch processor
β
βββ R-Reports/ # Final output location
βββ {name}-Report-{yyyy-mm-dd--hh-mm}/ # Timestamped reports
βββ Glucose_Analysis_Report_*.qmd
βββ Glucose_Analysis_Report_*.html
βββ Glucose_Analysis_Report_*.pdf
βββ index.html
Data Flow Diagram
βββββββββββββββ
β LibreView β
β Web Export β
ββββββββ¬βββββββ
β Raw CSV Download
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Part A: User Action β
β User downloads: {name}_glucose_{mm-dd-yyyy}.csv β
β Places in: B-Data-Analyzer/A-Data/ β
ββββββββ¬βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Part B1: Daily CSV File Generator β
β Input: {name}_glucose_{mm-dd-yyyy}.csv β
β Output: B1-Glucose-Data/ (daily CSV files) β
β Action: Splits raw data into individual daily files β
ββββββββ¬βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
ββββββββββββββββββ¬βββββββββββββββββ¬ββββββββββββββββββββ
β β β β
βΌ βΌ βΌ βΌ
ββββββββββββ ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ
β B2: β β C1: β β D1: β β E1: β
β Yearly β β Subtype β β AGP β β QMD β
β Analysis β β Analysis β β Reports β β Reports β
ββββββββββββ ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ
β β β β
ββββββββββββββββββ΄βββββββββββββββββ΄ββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββ
β R-Reports/ β
β Final Deliverables β
βββββββββββββββββββββββββββ
Processing Stages
Part A: Data Acquisition (User Manual Step)
Objective: Obtain raw glucose data from LibreView platform
Steps:
- Log into LibreView web portal
- Navigate to data export section
- Select date range for export
- Download CSV file
- Save to
B-Data-Analyzer/A-Data/folder - Create folder named:
{name}_glucose_{mm-dd-yyyy} - Place CSV file inside this folder
Expected Output:
A-Data/
βββ yahyanazer_glucose_1-26-2026/
βββ yahyanazer_glucose_1-26-2026.csv
File Format:
- Name Pattern:
{name}_glucose_{mm-dd-yyyy}.csv - Example:
yahyanazer_glucose_1-26-2026.csv - Encoding: UTF-8
- Structure: LibreView standard export format
Part B1: Daily CSV File Generator
Script: B1-COMPLETE-AUTO.py
Objective: Convert single LibreView CSV into individual daily CSV files
Process Flow:
- Read Source CSV
- Extracts patient name from cell E1
- Reads columns A through S
- Identifies Historic Glucose and Scan Glucose columns
- Build Glucose Selected
- Prioritizes Historic Glucose over Scan Glucose
- Combines both sources into single column
- Parses timestamps to datetime format
- Select Date Range
- Prompts user for begin/end year-month
- Or reads from automate.xlsx in automation mode
- Generate Daily Files
- Creates one CSV file per day
- Format:
Data-YYYY-MM-DD.csv - Columns:
timestamp,glucose_mg_dL - Timestamp format:
YYYY-MM-DD HH:MM:SS
- Create Summary Report
- Generates
B1-Summary-Report.html - Lists all created files
- Shows data preview
- Generates
Input:
A-Data/yahyanazer_glucose_1-26-2026/
βββ yahyanazer_glucose_1-26-2026.csv
Output:
A-Data/yahyanazer_glucose_1-26-2026/
βββ B1-Glucose-Data/
βββ Data-2022-01-01.csv
βββ Data-2022-01-02.csv
βββ ...
βββ B1-Summary-Report.html
Automation Configuration:
Input1: Path to CSV file
Input2: Begin date (YYYY-MM)
Input3: End date (YYYY-MM)
Part B2: Yearly Analysis Reports
Script: B2-COMPLETE-AUTO.py
Objective: Generate comprehensive yearly glucose analysis with interactive charts
Analysis Components:
- All Data Over Time
- Complete timeline of all glucose readings
- Trend visualization
- Pattern identification
- Daily Overlay (24-Hour Spaghetti Plot)
- All days overlaid on 24-hour axis
- Identifies typical daily patterns
- Shows variability across days
- Subtype Metrics Bar Chart
- Four Type 2 diabetes classifications
- CGM performance metrics
- Time in Range (TIR) analysis
- Glucose Histogram
- Distribution of glucose values
- Normal curve overlay
- Frequency analysis
- Summary Statistics
- Mean, median, standard deviation
- Percentiles (5th, 25th, 75th, 95th)
- Coefficient of variation
- Estimated A1C/GMI
Output Structure:
Each analysis is generated for: - Each year in the dataset - Overall (all data combined)
Input:
A-Data/yahyanazer_glucose_1-26-2026/
βββ yahyanazer_glucose_1-26-2026.csv
Output:
A-Data/yahyanazer_glucose_1-26-2026/
βββ B2-Yearly-Analysis/
βββ Glucose-Report-{name}-{timestamp}.html
βββ 2022-01-AllDataOverTime.png
βββ 2022-02-DailyOverlay.png
βββ 2022-03-SubtypeMetrics.png
βββ 2022-04-Histogram.png
βββ 2022-05-SummaryStats.png
βββ Overall-01-AllDataOverTime.png
βββ Overall-02-DailyOverlay.png
βββ ... (all PNG charts)
Automation Configuration:
Input1: Path to CSV file
Input2: (not used)
Input3: (not used)
Part C1: Subtype Analysis
Script: C1-COMPLETE-AUTO.py
Objective: Analyze glucose patterns to identify Type 2 diabetes subtypes
Analysis Categories:
- Muscle Insulin Resistance
- Post-meal glucose spikes
- Delayed glucose clearance
- Exercise response patterns
- Hepatic Insulin Resistance
- Fasting glucose levels
- Dawn phenomenon
- Overnight glucose production
- Beta Cell Dysfunction
- Initial insulin response
- Peak timing analysis
- Glucose recovery patterns
- Impaired Incretin Action
- Meal-related glucose dynamics
- GLP-1 response indicators
- Post-prandial patterns
Processing:
- Reads daily CSV files from B1-Glucose-Data
- Analyzes each day independently
- Detects meal events automatically
- Calculates subtype scores
- Generates individual day reports
- Creates aggregate summary
Input:
A-Data/yahyanazer_glucose_1-26-2026/
βββ B1-Glucose-Data/
βββ Data-2022-01-01.csv
βββ Data-2022-01-02.csv
βββ ...
Output:
A-Data/yahyanazer_glucose_1-26-2026/
βββ C1-Daily-CSV-to-Subtypes-Analysis/
βββ CGM-Report-Data-2022-01-01/
β βββ meal_events.csv
β βββ subtype_scores.json
β βββ analysis_report.html
βββ CGM-Report-Data-2022-01-02/
βββ ...
βββ report-{timestamp}.csv
βββ report-timeseries-{timestamp}.csv
Automation Configuration:
Input1: Path to B1-Glucose-Data folder
Input2: (not used)
Input3: (not used)
Part D1: AGP (Ambulatory Glucose Profile) Reports
Script: D1-COMPLETE-AUTO.py
Objective: Generate standard AGP reports following clinical guidelines
AGP Components:
- AGP Profile Chart
- Median glucose line (50th percentile)
- Interquartile range (25th-75th percentile)
- 5th-95th percentile range
- 24-hour overlay format
- Daily Glucose Profiles
- Individual days overlaid
- Pattern visualization
- Variability assessment
- Time in Range (TIR) Bar
- Very Low: <54 mg/dL
- Low: 54-70 mg/dL
- Target: 70-180 mg/dL
- High: 180-250 mg/dL
- Very High: >250 mg/dL
- Statistics Summary
- GMI (Glucose Management Indicator)
- Average glucose
- Glucose variability (CV%)
- Time in ranges
- A1C/GMI Trends
- 90-day rolling averages
- Daily A1C estimates
- Trend visualization
- Historical progression
Input:
A-Data/yahyanazer_glucose_1-26-2026/
βββ B1-Glucose-Data/
βββ Data-2022-01-01.csv
βββ Data-2022-01-02.csv
βββ ...
Output:
A-Data/yahyanazer_glucose_1-26-2026/
βββ D1-Daily-CSV-to-AGP-Report/
βββ AGP-Report-{timestamp}.html
βββ 2022-01-AGP-Profile.png
βββ 2022-02-Daily-Profiles.png
βββ 2022-03-TIR-Bar.png
βββ 2022-04-Statistics.png
βββ 2022-05-A1C-Trends.png
βββ Overall-01-AGP-Profile.png
βββ ... (all PNG charts)
Automation Configuration:
Input1: Path to B1-Glucose-Data folder
Input2: (not used)
Input3: (not used)
Part E1: QMD Report Generator
Script: E1-COMPLETE-AUTO.py
Objective: Create comprehensive final reports combining all analyses
Report Generation Process:
- Scan Source Folders
- B2-Yearly-Analysis (PNG charts)
- C1-Daily-CSV-to-Subtypes-Analysis (results)
- D1-Daily-CSV-to-AGP-Report (PNG charts)
- Extract Years
- Identifies all years in data
- Includes βOverallβ analysis
- Sorts chronologically
- Generate QMD Content
- Creates Quarto markdown document
- Embeds all PNG charts
- Organizes by year
- Adds descriptions and context
- Render Outputs
- Converts QMD to HTML
- Converts QMD to PDF
- Creates navigation index.html
- Preserves source QMD
- Copy Source Data
- Includes B2, C1, D1 folders
- Creates self-contained package
- Enables offline viewing
Name Extraction:
From input folder: yahyanazer_glucose_1-26-2026 Extract base name: yahyanazer_glucose Create report folder: yahyanazer_glucose-Report-2026-01-28--19-34
Input:
A-Data/yahyanazer_glucose_1-26-2026/
βββ B2-Yearly-Analysis/
βββ C1-Daily-CSV-to-Subtypes-Analysis/
βββ D1-Daily-CSV-to-AGP-Report/
Output:
R-Reports/yahyanazer_glucose-Report-2026-01-28--19-34/
βββ Glucose_Analysis_Report_2026-01-28--19-34.qmd
βββ Glucose_Analysis_Report_2026-01-28--19-34.html
βββ Glucose_Analysis_Report_2026-01-28--19-34.pdf
βββ index.html
βββ B2-Yearly-Analysis/ (copied)
βββ C1-Daily-CSV-to-Subtypes-Analysis/ (copied)
βββ D1-Daily-CSV-to-AGP-Report/ (copied)
Automation Configuration:
Input1: Path to CSV-named folder
Input2: (not used)
Input3: (not used)
Automation System
automate.xlsx Configuration
The automation system uses an Excel file to configure batch processing.
Excel Structure:
| Run | Program | Input1 | Input2 | Input3 |
|---|---|---|---|---|
| TRUE | B1-COMPLETE-AUTO.py | C:β¦_glucose_1-26-2026.csv | 2022-01 | 2025-12 |
| TRUE | B2-COMPLETE-AUTO.py | C:β¦_glucose_1-26-2026.csv | ||
| TRUE | C1-COMPLETE-AUTO.py | C:β¦-Glucose-Data | ||
| TRUE | D1-COMPLETE-AUTO.py | C:β¦-Glucose-Data | ||
| TRUE | E1-COMPLETE-AUTO.py | C:β¦_glucose_1-26-2026 |
Column Definitions:
- Run: TRUE/FALSE - enable/disable program
- Program: Script filename
- Input1: Primary input path
- Input2: Secondary input (optional)
- Input3: Tertiary input (optional)
Helper Module: excel_automate.py
Provides automation support functions: - check_automation(script_path) - Reads Excel config - validate_file_exists(path, description) - Validates file paths - validate_folder_exists(path, description) - Validates folder paths
Batch Processing: run_all.py
Objective: Execute all enabled programs in sequence
Features:
- Sequential Execution
- Runs programs in order listed
- Waits for each to complete
- Captures output and errors
- Progress Reporting
- Real-time console output
- Success/failure status
- Execution time tracking
- HTML Summary Report
- Saved in
Reports/folder - Lists all programs
- Shows statistics
- Includes error details
- Saved in
- Error Handling
- Continues on failures
- Logs all errors
- Returns appropriate exit codes
Usage:
python run_all.pyOutput:
Reports/
βββ Batch-Report-2026-01-30-14-30-00.html
- Summary statistics
- Program results
- Error logs
- Execution times
Usage Workflows
Workflow 1: Manual Processing (GUI Mode)
Use Case: Interactive processing with user prompts
Steps:
Prepare Data
Place CSV in: B-Data-Analyzer/A-Data/{name}_glucose_{date}/Run B1 (Daily CSV Generator)
python B1-COMPLETE-AUTO.py- Select CSV file when prompted
- Choose date range
- Wait for completion
Run B2 (Yearly Analysis)
python B2-COMPLETE-AUTO.py- Select same CSV file
- Wait for charts generation
Run C1 (Subtype Analysis)
python C1-COMPLETE-AUTO.py- Select B1-Glucose-Data folder
- Enter estimate_average (default: 100)
- Wait for analysis
Run D1 (AGP Reports)
python D1-COMPLETE-AUTO.py- Select B1-Glucose-Data folder
- Wait for AGP generation
Run E1 (Final Reports)
python E1-COMPLETE-AUTO.py- Select CSV-named folder
- Wait for QMD rendering
- Report opens in browser
Result:
R-Reports/{name}-Report-{timestamp}/
βββ All final deliverables
Workflow 2: Automated Processing (Excel Mode)
Use Case: Batch processing without user interaction
Steps:
Configure automate.xlsx
- Set Run=TRUE for desired programs
- Fill in Input1, Input2, Input3 paths
- Save Excel file
Run Batch Processor
python run_all.pyReview Results
- Check console output
- Open batch report in Reports/
- Verify R-Reports/ output
Advantages:
- No manual intervention required
- Process multiple patients in sequence
- Reproducible results
- Audit trail via batch reports
Workflow 3: Individual Stage Processing
Use Case: Run specific analysis stages only
Examples:
Update AGP reports only:
python D1-COMPLETE-AUTO.py
# Select B1-Glucose-Data folderRegenerate final report:
python E1-COMPLETE-AUTO.py
# Select CSV-named folder containing B2, C1, D1Reprocess with different date range:
python B1-COMPLETE-AUTO.py
# Select CSV, choose new date range
# Then re-run C1, D1, E1 as neededTechnical Requirements
Software Requirements
Python Environment:
- Python 3.8 or higher
- UTF-8 encoding support
Required Libraries:
# Core
pandas
numpy
matplotlib
scipy
# GUI (for manual mode)
tkinter
# Document generation (for E1)
quarto (external command-line tool)
# Automation
openpyxl # For reading Excel filesInstallation:
pip install pandas numpy matplotlib scipy openpyxl --break-system-packagesHardware Requirements
- Minimum:
- 4 GB RAM
- 500 MB disk space
- Dual-core processor
- Recommended:
- 8 GB RAM
- 2 GB disk space
- Quad-core processor
Operating System
- Windows 10/11
- macOS 10.15+
- Linux (Ubuntu 20.04+)
Data Specifications
Input Data Format (LibreView CSV)
Required Columns:
- Device Timestamp: Date/time of reading
- Historic Glucose mg/dL: Retrospective readings
- Scan Glucose mg/dL: On-demand readings
- Patient Info (Cell E1): Patient name/identifier
Expected Structure:
Row 1: Metadata (including patient name in E1)
Row 2: Column headers
Row 3+: Data rows
Encoding:
- UTF-8 with or without BOM
- CRLF or LF line endings
Output Data Formats
Daily CSV Files (B1):
timestamp,glucose_mg_dL
2022-01-01 00:15:00,120
2022-01-01 00:30:00,118
Analysis Reports:
- HTML: Self-contained with embedded CSS/JS
- PDF: Requires Quarto installation
- PNG Charts: 1200x800 pixels, 300 DPI
- JSON: Structured analysis results
Quality Assurance
Data Validation
B1 Stage:
B2-D1 Stages:
E1 Stage:
Error Handling
All scripts include: - Try-catch blocks for common errors - Informative error messages - Graceful degradation - Debug output (when enabled) - User-friendly GUI error dialogs
Troubleshooting
Common Issues
Issue 1: βNo CSV selectedβ or βNo folder selectedβ
Cause: User canceled file/folder picker dialog
Solution: - Run script again - Select appropriate file/folder - Or use automation mode to skip GUI
Issue 2: βNo valid timestamps foundβ
Cause: CSV format unexpected or corrupted
Solution: - Verify CSV is from LibreView export - Check file encoding (should be UTF-8) - Ensure row 2 contains column headers - Look for βDevice Timestampβ column
Issue 3: Charts not generating in B2/D1
Cause: matplotlib not installed or display issues
Solution:
pip install matplotlib --break-system-packagesIssue 4: QMD not rendering to PDF
Cause: Quarto not installed
Solution: - Install Quarto from https://quarto.org - Add to system PATH - Restart terminal/command prompt
Issue 5: βB-Data-Analyzer folder not foundβ
Cause: E1 cannot locate parent folder structure
Solution: - Ensure folder structure follows specification - Script will use fallback location - Check console for actual output path
Best Practices
Data Organization
- Consistent Naming
- Always use pattern:
{name}_glucose_{mm-dd-yyyy} - Example:
johndoe_glucose_1-26-2026
- Always use pattern:
- Backup Original Data
- Keep copy of raw CSV files
- Store outside B-Data-Analyzer structure
- Document data source and export date
- Regular Processing
- Process new data monthly or quarterly
- Archive old reports in separate location
- Maintain change log
Automation Setup
- Test Individual Scripts First
- Run each script manually before automation
- Verify output quality
- Adjust parameters as needed
- Use Relative Paths When Possible
- Store scripts and data on same drive
- Easier migration between systems
- Monitor Batch Reports
- Review HTML reports after each batch run
- Check for errors or warnings
- Validate output completeness
Report Review
- Sanity Check Outputs
- Verify date ranges are correct
- Check for data gaps
- Review glucose value ranges
- Clinical Validation
- Compare with known A1C values
- Verify TIR percentages reasonable
- Look for data quality issues
- Documentation
- Note any anomalies or issues
- Document data quality concerns
- Keep processing logs
Appendix A: File Naming Conventions
Input Files
- LibreView CSV:
{name}_glucose_{mm-dd-yyyy}.csv- Example:
yahyanazer_glucose_1-26-2026.csv
- Example:
Output Folders
- CSV Folder:
{name}_glucose_{mm-dd-yyyy}- Example:
yahyanazer_glucose_1-26-2026
- Example:
- Report Folder:
{name}-Report-{yyyy-mm-dd--hh-mm}- Example:
yahyanazer_glucose-Report-2026-01-28--19-34
- Example:
Output Files
- Daily CSV:
Data-{yyyy-mm-dd}.csv- Example:
Data-2022-01-15.csv
- Example:
- PNG Charts:
{Year}-{Number}-{ChartType}.png- Example:
2022-01-AllDataOverTime.png
- Example:
- Reports:
Glucose_Analysis_Report_{timestamp}.{ext}- Example:
Glucose_Analysis_Report_2026-01-28--19-34.html
- Example:
Appendix B: Excel Automation Examples
Example 1: Single Patient Processing
| Run | Program | Input1 | Input2 | Input3 |
|---|---|---|---|---|
| TRUE | B1-COMPLETE-AUTO.py | C:-Data-Analyzer-Data_glucose_1-15-2026.csv | 2023-01 | 2026-01 |
| TRUE | B2-COMPLETE-AUTO.py | C:-Data-Analyzer-Data_glucose_1-15-2026.csv | ||
| TRUE | C1-COMPLETE-AUTO.py | C:-Data-Analyzer-Data_glucose_1-15-2026-Glucose-Data | ||
| TRUE | D1-COMPLETE-AUTO.py | C:-Data-Analyzer-Data_glucose_1-15-2026-Glucose-Data | ||
| TRUE | E1-COMPLETE-AUTO.py | C:-Data-Analyzer-Data_glucose_1-15-2026 |
Example 2: Multiple Patients (Sequential)
| Run | Program | Input1 | Input2 | Input3 |
|---|---|---|---|---|
| TRUE | B1-COMPLETE-AUTO.py | C:β¦_glucose_1-15-2026.csv | 2023-01 | 2026-01 |
| TRUE | B2-COMPLETE-AUTO.py | C:β¦_glucose_1-15-2026.csv | ||
| TRUE | E1-COMPLETE-AUTO.py | C:β¦_glucose_1-15-2026 | ||
| TRUE | B1-COMPLETE-AUTO.py | C:β¦_glucose_1-20-2026.csv | 2023-01 | 2026-01 |
| TRUE | B2-COMPLETE-AUTO.py | C:β¦_glucose_1-20-2026.csv | ||
| TRUE | E1-COMPLETE-AUTO.py | C:β¦_glucose_1-20-2026 |
Appendix C: Quick Reference Commands
Installation
# Install Python dependencies
pip install pandas numpy matplotlib scipy openpyxl --break-system-packages
# Install Quarto (for PDF generation)
# Download from: https://quarto.org/docs/get-started/Manual Execution
# Run individual scripts
python B1-COMPLETE-AUTO.py
python B2-COMPLETE-AUTO.py
python C1-COMPLETE-AUTO.py
python D1-COMPLETE-AUTO.py
python E1-COMPLETE-AUTO.pyBatch Execution
# Run all enabled programs
python run_all.pyDebug Mode
# Enable in script header
DEBUG = TrueAppendix D: Contact and Support
Author
Yahya Nazer, PhD - Website: Diabetes-Type2.com - Email: Available on website - Specialization: Industrial Automation, Data Analysis, Diabetes Management
Project Information
- Project: Glucose Data Analysis Automation System
- Version: 2.0
- Last Updated: January 30, 2026
- Repository: B-Data-Analyzer
Documentation
- This workflow document (QMD)
- Individual script docstrings
- Code comments
- README files
Revision History
| Version | Date | Changes |
|---|---|---|
| 1.0 | 2025-01 | Initial workflow implementation |
| 2.0 | 2026-01-30 | Updated folder structure to B-Data-Analyzer/R-Reports |
| Added E1 base name extraction | ||
| Standardized all folder names | ||
| Added automation support to all scripts |
Appendix E: Detection Algorithm
How the Program Identifies 4 Subtypes of Type 2 Diabetes (Heuristic)
This program analyzes CGM (Continuous Glucose Monitoring) data to infer which of four physiological patterns your glucose responses most resemble:
- Muscle insulin resistance (Muscle IR)
- Hepatic (liver) insulin resistance (Hepatic IR)
- Ξ²-cell dysfunction
- Impaired incretin action
Inputs & Assumptions
- CSV columns (required):
timestampβ ISO-like datetime string (local or UTC; script localizes to a chosen TZ).glucose_mg_dLβ glucose in mg/dL (if mmol/L, convert Γ18 or run script with mmol option in extended versions).
- Sampling: Any cadence is accepted; data are resampled to 5-minute intervals with light interpolation.
- Meals: If no meal markers exist, meals are auto-detected from rapid rises in CGM.
Processing Pipeline (High-Level)
- Load & normalize
- Parse timestamps; localize/convert timezone.
- Ensure glucose is in mg/dL.
- Resample to 5-minute grid; fill small gaps.
- Meal detection (heuristic)
- Flag a meal if thereβs a rise β₯ 30 mg/dL within ~45 minutes or an average slope β₯ 0.35 mg/dL/min, enforcing β₯90 minutes between detected meals.
- Per-meal metrics For each detected (or provided) meal, compute:
- Baseline (median in the 30 min pre-meal window)
- Peak and Ξ = peak β baseline
- Time-to-peak (minutes from meal to peak)
- Time above 140 mg/dL and time above 180 mg/dL
- Time to return to (baseline + 10 mg/dL)
- AUC(0β180 min) above baseline
- Overnight fasting metrics
- Median and mean between 03:00β06:00 local time (proxy for hepatic output).
- Subtype scoring (heuristics)
- Compute four scores (0β1), then normalize them to sum β 1.
- The top score is reported as the primary pattern; overlapping patterns are common.
Heuristic Criteria (What the Program Looks For)
Thresholds below are rules of thumb chosen to match typical physiology and to be robust to noisy consumer CGM data. They can be tuned.
1) Muscle Insulin Resistance (Muscle IR)
Idea: Glucose disposal by muscle is sluggish β late peaks and slow clearance.
- Signals used - Share of meals with time-to-peak > 60 min
- Share with return to baseline+10 > 180 min
- Share with time > 140 mg/dL > 120 min - Score contribution
- 0.4(late_peak_share) + 0.4(slow_return_share) + 0.2*(long_140_share)
2) Hepatic (Liver) Insulin Resistance
Idea: Liver continues to release glucose (gluconeogenesis) β elevated fasting and pre-meal baselines. - Signals used
- Overnight fasting median: - β₯130 β +1.00 - 120β129 β +0.75 - 110β119 β +0.50 - 100β109 β +0.25
- Share of meals with baseline β₯ 110 mg/dL β
+0.6*(high_baseline_share) - Score contribution - Sum of fasting bump + baseline bump
3) Ξ²-Cell Dysfunction
Idea: Insulin secretion is insufficient/slow β very tall, early spikes.
- Signals used - Share with Ξ β₯ 80 mg/dL (large excursion)
- Share with time-to-peak β€ 45 min (early sharp rise)
- Share with peak β₯ 200 mg/dL
- Score contribution
0.5*(large_spike_share) + 0.3*(early_peak_share) + 0.2*(very_high_peak_share)
4) Impaired Incretin Action
Idea: Gut hormones (GLP-1/GIP) arenβt amplifying post-meal insulin β prolonged, lingering spikes despite moderate peaks.
- Signals used - Share with time > 180 mg/dL > 45 min
- Share with return to baseline+10 > 150 min
- Share with AUC(0β180) β₯ 8000 mgΒ·dLΒ·min - Score contribution
0.4*(long_180_share) + 0.4*(long_return_share) + 0.2*(high_auc_share)
Normalization: After computing all contributions, scores are normalized to sum β 1. If all contributions are zero, each subtype defaults to 0.25.
Outputs
cgm_meal_metrics.csvβ one row per meal with all metrics above.cgm_subtype_summary.jsonβ fasting metrics, normalized subtype scores, and suggested top pattern.cgm_subtype_report.htmlβ visual report (overall trace, meal markers, bar chart of scores).
The program also names the output folder as
CGM-Report-<CSVName>-<yyyy-mm-dd--hh-mm>and opens the HTML report automatically.
Interpreting the Results
- Scores reflect patterns, not diagnoses. Itβs common to see mixed features (e.g., Muscle IR and Ξ²-cell).
- High fasting/pre-meal β likely Hepatic IR.
- Tall, early peaks β likely Ξ²-cell dysfunction.
- Late peaks & long return β likely Muscle IR.
- Spikes that linger (high AUC) β likely Incretin impairment.
Use these insights to tailor lifestyle & diet strategies, and discuss with a healthcare professional.
Limitations & Caveats
- Heuristic thresholds: chosen to be practical; feel free to tune for your cohort.
- Meal detection noise: auto-detection can mislabel snacks/graze patterns; providing real meal markers improves accuracy.
- Sensor artifacts: compression lows, missed scans, and calibration shifts can distort metrics.
- Context matters: meds (e.g., GLP-1 RAs), sleep, stress, and activity strongly influence curves.
Quick Pseudocode
if no meal flags: meals := rising_edge_detector(glucose, riseβ₯30 within \~45 min OR slopeβ₯0.35) enforce 90 min between meals
for meal in meals: baseline := median(glucose in \[β30, 0\] min) peak := max(glucose in \[0, +240\] min) delta := peak β baseline t_peak := minutes to peak t\>140, t\>180 := durations above thresholds t_return := minutes until glucose β€ baseline+10 auc0_180 := AUC(glucose β baseline, 0..180) with negatives clipped to 0
fasting := median/mean(glucose between 03:00β06:00)
scores := 0 for each subtype scores += rules for hepatic (fasting + high baseline share) scores += rules for muscle (late peak, slow return, long 140) scores += rules for beta-cell (large delta, early peak, high peak) scores += rules for incretin (long 180, long return, high AUC) normalize scores to sum β 1