Type 1 Diabetes Data Analysis from DIY Loop system
Project description
SweetiePy - Type 1 Diabetes Data Analysis
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"
- Double-check credentials: Verify username/password in MongoDB Atlas dashboard
- Check cluster name: Make sure cluster name in
MONGODB_URImatches your Atlas cluster - Test with MongoDB Compass: Try connecting with the GUI first to verify credentials
- Database permissions: Ensure your user has read access to
myCGMitcdatabase
Import Errors
"Module not found" errors
- Make sure you installed:
pip install sweetiepyoruv 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_dataframehas 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
.envfiles 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:
- ๐ Explore patterns - Find your optimal times and settings
- ๐ Create visualizations - Use matplotlib/plotly for charts
- ๐ Share insights - Help others in the diabetes community
- ๐ Build more - Contribute new analysis features
Happy analyzing! ๐ฉธ๐
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a4b47d8b8546926ea1df43bae6c7916b4d36814e8dd73908948194fc4ddd1e9a
|
|
| MD5 |
216490d1be95de4a7a03cabf60b18c1e
|
|
| BLAKE2b-256 |
14bb95102475625668b2a14d4e41d7d763b2f8f466c8a25459758d986bfff718
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
12bc9a410b2032250a97d25306a29638a8516cbf1cc8b096cdb8e42827cc0d9c
|
|
| MD5 |
b195ec2185606739e681a4548d8c61d5
|
|
| BLAKE2b-256 |
accbea940e4c7fe703e5416c6190a170e2b7b91dd8dff3f126aeca610d5c95ab
|