Skip to main content

Type 1 Diabetes Data Analysis from DIY Loop system

Project description

SweetiePy - Type 1 Diabetes Data Analysis

PyPI version Python 3.12+

A Python package for accessing and analyzing Type 1 diabetes data from a DIY Loop system stored in MongoDB Atlas. Get insights from your CGM data, correlate pump settings with glucose outcomes, and optimize your diabetes management with data-driven analysis.

โšก Quick Start (5 minutes)

1. Install SweetiePy

Option A: Using uv (recommended)

# Install uv if you don't have it
curl -LsSf https://astral.sh/uv/install.sh | sh

# Create project and install sweetiepy
mkdir my-diabetes-analysis && cd my-diabetes-analysis
uv init --name my-diabetes-analysis
uv add sweetiepy

Option B: Using pip

mkdir my-diabetes-analysis && cd my-diabetes-analysis
pip install sweetiepy

2. Configure Database

Create a .env file with your MongoDB Atlas credentials:

# Get these from your MongoDB Atlas dashboard
MONGODB_USERNAME=your_username
MONGODB_PW=your_password

# Change 'yourcluster' to your actual cluster name
MONGODB_URI=mongodb+srv://<username>:<password>@cluster0.yourcluster.mongodb.net/?retryWrites=true&w=majority

# Database name (usually this for Loop systems)
MONGODB_DATABASE=myCGMitc

โš ๏ธ Important: Keep <username> and <password> in the URI exactly as shown!

3. Test & Analyze

from sweetiepy.data.cgm import CGMDataAccess

# Test connection and get glucose data
with CGMDataAccess() as cgm:
    df = cgm.get_dataframe_for_period('last_week')
    
    print(f"๐Ÿ“Š {len(df)} glucose readings analyzed")
    print(f"๐Ÿ“… Date range: {df['datetime'].min()} to {df['datetime'].max()}")
    print(f"๐Ÿฉธ Average glucose: {df['sgv'].mean():.1f} mg/dL")
    
    # Calculate time in range manually (safe approach)
    total_readings = len(df)
    in_range = ((df['sgv'] >= 70) & (df['sgv'] <= 180)).sum()
    high = (df['sgv'] > 180).sum() 
    low = (df['sgv'] < 70).sum()
    
    print(f"๐ŸŽฏ Time in range (70-180): {in_range/total_readings*100:.1f}%")
    print(f"๐Ÿ“ˆ Time high (>180): {high/total_readings*100:.1f}%")
    print(f"๐Ÿ“‰ Time low (<70): {low/total_readings*100:.1f}%")

4. Advanced: Correlate CGM + Pump Settings

from sweetiepy.data.merged import MergedDataAccess

# Analyze how pump settings affect glucose outcomes
with MergedDataAccess() as merged:
    df = merged.get_merged_cgm_and_settings(days=7)
    correlations = merged.analyze_settings_correlation(df)
    
    print("๐Ÿ”— How settings correlate with glucose:")
    for setting, correlation in correlations['correlations'].items():
        direction = "higher" if correlation > 0 else "lower"
        print(f"   {setting}: {correlation:+.3f} ({direction} setting โ†’ higher glucose)")

๐ŸŽ‰ That's it! You're now analyzing your diabetes data with Python.


Features

  • ๐Ÿ“Š CGM Data Analysis - Access and analyze continuous glucose monitor data
  • ๐Ÿ’‰ Pump Data Integration - Query insulin doses, basal rates, and treatment data
  • ๐Ÿ”— Merged Data Analysis - Key Innovation: Synchronize CGM readings with active pump settings at each timestamp
  • ๐Ÿ“ˆ Time-Series Analysis - Built-in time-in-range calculations and statistics
  • ๐ŸŽฏ Settings Correlation - Analyze how basal rates, carb ratios, and ISF affect glucose outcomes
  • ๐Ÿ” Flexible Queries - Predefined periods or custom date ranges
  • ๐Ÿš€ High Performance - PyArrow-backed DataFrames for efficient processing
  • ๐Ÿ” Secure - Environment-based configuration for credentials

๐Ÿ”ง Troubleshooting

Connection Issues

"Connection failed" or "Authentication failed"

  1. Double-check credentials: Verify username/password in MongoDB Atlas dashboard
  2. Check cluster name: Make sure cluster name in MONGODB_URI matches your Atlas cluster
  3. Test with MongoDB Compass: Try connecting with the GUI first to verify credentials
  4. Database permissions: Ensure your user has read access to myCGMitc database

Import Errors

"Module not found" errors

  • Make sure you installed: pip install sweetiepy or uv add sweetiepy
  • Try in a fresh virtual environment
  • Check Python version: requires 3.12+

No Data Found

"No recent readings" or empty DataFrames

  • This is normal if your CGM hasn't uploaded data recently
  • Try a longer time period: get_dataframe_for_period('last_month')
  • Check your database name matches your Loop system

Code/Formatting Errors

"Format specifier missing precision" or syntax errors

  • When copying from README, watch for line breaks in f-strings
  • Use the safe manual calculation approach if analyze_dataframe has issues:
    # Safe approach - always works
    total = len(df)
    in_range = ((df['sgv'] >= 70) & (df['sgv'] <= 180)).sum()
    print(f"Time in range: {in_range/total*100:.1f}%")
    

Still having issues?

Run the connection test:

python -m sweetiepy.connection.mongodb

Expected output:

โœ“ Connected to MongoDB database: myCGMitc
Available databases: ['myCGMitc', ...]
Collections in myCGMitc: ['entries', 'treatments', ...]

๐Ÿ“š Complete Usage Examples

Basic CGM Analysis

from sweetiepy.data.cgm import CGMDataAccess

# Context manager handles connection automatically
with CGMDataAccess() as cgm:
    # Get last week's data
    df = cgm.get_dataframe_for_period('last_week')
    analysis = cgm.analyze_dataframe(df)
    
    print(f"๐Ÿ“Š Analyzed {len(df)} glucose readings")
    print(f"๐Ÿ“… Date range: {df['datetime'].min()} to {df['datetime'].max()}")
    print(f"๐Ÿฉธ Average: {analysis['basic_stats']['avg_glucose']:.1f} mg/dL")
    print(f"๐ŸŽฏ Time in range (70-180): {analysis['time_in_range']['normal_percent']:.1f}%")
    print(f"๐Ÿ“ˆ Time high (>180): {analysis['time_in_range']['high_percent']:.1f}%")
    print(f"๐Ÿ“‰ Time low (<70): {analysis['time_in_range']['low_percent']:.1f}%")

Advanced: Settings Correlation Analysis

from sweetiepy.data.merged import MergedDataAccess

# Correlate CGM readings with active pump settings
with MergedDataAccess() as merged:
    # Get CGM data with pump settings active at each timestamp
    df = merged.get_merged_cgm_and_settings(days=7)
    
    # Analyze how settings affect glucose outcomes  
    correlations = merged.analyze_settings_correlation(df)
    
    print("๐Ÿ”— Settings impact on glucose:")
    for setting, correlation in correlations['correlations'].items():
        impact = "raises" if correlation > 0 else "lowers"
        strength = "strong" if abs(correlation) > 0.5 else "moderate" if abs(correlation) > 0.3 else "weak"
        print(f"   ๐Ÿ“ˆ {setting}: {correlation:+.3f} ({strength} - higher setting {impact} glucose)")

Time Pattern Analysis

from sweetiepy.data.cgm import CGMDataAccess
import matplotlib.pyplot as plt

with CGMDataAccess() as cgm:
    df = cgm.get_dataframe_for_period('last_month')
    
    # Hourly patterns
    hourly_avg = df.groupby(df['datetime'].dt.hour)['sgv'].mean()
    
    # Find problematic hours
    high_hours = hourly_avg[hourly_avg > 180]
    print(f"โš ๏ธ  Hours with average glucose > 180: {list(high_hours.index)}")
    
    # Daily patterns
    daily_avg = df.groupby(df['datetime'].dt.day_name())['sgv'].mean()
    print(f"๐Ÿ“… Average glucose by day:\n{daily_avg.round(1)}")

๐Ÿ—ƒ๏ธ Data Overview

SweetiePy works with DIY Loop diabetes data stored in MongoDB Atlas. Your database typically contains:

  • ๐Ÿ“Š entries - CGM glucose readings (primary data - usually 200K+ readings)
  • ๐Ÿ’‰ treatments - Insulin doses, carbs, basal changes
  • โš™๏ธ profile - Pump settings (basal rates, carb ratios, ISF)
  • ๐Ÿ“ฑ devicestatus - Loop system status and connectivity
  • ๐ŸŽ food - Food logs and carbohydrate entries
  • ๐Ÿƒ activity - Exercise and activity logs

Key Data Points

  • Glucose readings: Every 1-5 minutes from your CGM
  • Insulin treatments: Bolus doses, temporary basals
  • Pump settings: Time-scheduled basal rates, carb ratios, insulin sensitivity factors
  • Treatment context: Recent insulin/carbs for each glucose reading

๐Ÿ“Š Available Analysis Types

  • Time-in-Range Analysis: Calculate % time in target, high, low ranges
  • Time Pattern Analysis: Identify hourly/daily glucose patterns
  • Settings Correlation: How basal rates, carb ratios, ISF affect glucose
  • Treatment Analysis: Insulin doses, carb entries, pump adjustments
  • Custom Queries: Flexible date ranges and time periods

๐Ÿ“‹ Installation Options

For End Users

Option A: Using uv (recommended)

# Install uv if you don't have it
curl -LsSf https://astral.sh/uv/install.sh | sh

# Create project and install sweetiepy
mkdir my-diabetes-analysis && cd my-diabetes-analysis
uv init --name my-diabetes-analysis
uv add sweetiepy

Option B: Using pip

pip install sweetiepy

For Developers

# Clone and install from source
git clone <repository-url>
cd sweetiepy
uv sync
uv pip install -e .

๐Ÿ“š API Reference

CGMDataAccess - Glucose Data

from sweetiepy.data.cgm import CGMDataAccess

with CGMDataAccess() as cgm:
    # Standard periods: 'last_24h', 'last_week', 'last_month', 'last_3_months'
    df = cgm.get_dataframe_for_period('last_week')
    analysis = cgm.analyze_dataframe(df)

PumpDataAccess - Treatment Data

from sweetiepy.data.pump import PumpDataAccess

with PumpDataAccess() as pump:
    boluses = pump.get_bolus_data(days=7)      # Insulin doses
    carbs = pump.get_carb_data(days=7)         # Carb entries  
    basals = pump.get_basal_data(days=7)       # Temp basals
    profile = pump.get_basal_profile()         # Basal schedule

MergedDataAccess - Settings Correlation

from sweetiepy.data.merged import MergedDataAccess

with MergedDataAccess() as merged:
    # CGM data + active pump settings at each timestamp
    df = merged.get_merged_cgm_and_settings(days=7)
    correlations = merged.analyze_settings_correlation(df)

๐Ÿ’ผ Package Information

  • ๐Ÿ“ฆ PyPI: sweetiepy
  • ๐Ÿ“œ License: MIT
  • ๐Ÿ Python: 3.12+
  • ๐Ÿ“ฆ Dependencies: pymongo, pandas, pyarrow, python-dotenv, python-dateutil, matplotlib, plotly

๐Ÿ”’ Security & Privacy

  • ๐Ÿ” Secure: Never commit .env files to version control
  • ๐Ÿ“ Read-only: Package only reads your data, never modifies it
  • ๐Ÿ  Local: All analysis runs on your computer - no data transmitted
  • ๐Ÿ”’ Encrypted: Uses MongoDB Atlas encrypted connections

๐Ÿค Contributing

SweetiePy is open source and community-driven! We welcome:

  • ๐Ÿ› Bug reports and feature requests
  • ๐Ÿ“ Documentation improvements
  • ๐Ÿ“Š Analysis examples and tutorials
  • ๐Ÿง  Ideas for new analysis types

This project exists to help the Type 1 diabetes community make data-driven decisions about their health.

๐ŸŽ† What's Next?

Once you're up and running with SweetiePy:

  1. ๐Ÿ” Explore patterns - Find your optimal times and settings
  2. ๐Ÿ“ˆ Create visualizations - Use matplotlib/plotly for charts
  3. ๐Ÿ“Š Share insights - Help others in the diabetes community
  4. ๐Ÿš€ Build more - Contribute new analysis features

Happy analyzing! ๐Ÿฉธ๐Ÿ“Š

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sweetiepy-1.0.1.tar.gz (302.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sweetiepy-1.0.1-py3-none-any.whl (23.5 kB view details)

Uploaded Python 3

File details

Details for the file sweetiepy-1.0.1.tar.gz.

File metadata

  • Download URL: sweetiepy-1.0.1.tar.gz
  • Upload date:
  • Size: 302.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for sweetiepy-1.0.1.tar.gz
Algorithm Hash digest
SHA256 a4b47d8b8546926ea1df43bae6c7916b4d36814e8dd73908948194fc4ddd1e9a
MD5 216490d1be95de4a7a03cabf60b18c1e
BLAKE2b-256 14bb95102475625668b2a14d4e41d7d763b2f8f466c8a25459758d986bfff718

See more details on using hashes here.

File details

Details for the file sweetiepy-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: sweetiepy-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 23.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for sweetiepy-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 12bc9a410b2032250a97d25306a29638a8516cbf1cc8b096cdb8e42827cc0d9c
MD5 b195ec2185606739e681a4548d8c61d5
BLAKE2b-256 accbea940e4c7fe703e5416c6190a170e2b7b91dd8dff3f126aeca610d5c95ab

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page