Type 1 Diabetes Data Analysis from DIY Loop system
Project description
Loopy Basic - Type 1 Diabetes Data Analysis
A Python proof of concept project for accessing and analyzing Type 1 diabetes data from a DIY Loop system stored in MongoDB Atlas. Focuses on CGM pattern analysis for diabetes management optimization.
Project Status
Stages 1-3 Complete ✅ - Database connection, CGM data access, and time-range queries implemented
Project Structure
src/loopy/
├── connection/ # Database connectivity
│ └── mongodb.py
├── data/ # Data access modules
│ └── cgm.py
└── utils/ # Utilities and debugging
└── debug.py
docs/ # Analysis documentation
dev/ # Development and analysis scripts
├── exploratory/ # Exploratory analysis notebooks
├── reports/ # Analysis reports
└── usage_example.py # Complete usage demonstration
tests/ # Test modules
Installation & Setup
1. Clone and Install
# Clone the repository
git clone <repository-url>
cd loopy-basic
# Install dependencies
uv sync
# Install package in editable mode for development
uv pip install -e .
2. Package Usage
Once installed, you can import and use the package from anywhere:
from loopy.data.cgm import CGMDataAccess
from loopy.connection.mongodb import MongoDBConnection
# Initialize and use
cgm = CGMDataAccess()
cgm.connect()
df = cgm.get_dataframe_for_period('last_week')
3. MongoDB Atlas Connection Setup
Create Environment File
Copy the example environment file:
cp .env.example .env
Configure MongoDB Credentials
Edit .env with your MongoDB Atlas credentials:
MONGODB_USERNAME=your_actual_username
MONGODB_PW=your_actual_password
MONGODB_URI=mongodb+srv://<username>:<password>@cluster0.yourcluster.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0
MONGODB_DATABASE=myCGMitc
Important Notes:
- Keep the
<username>and<password>placeholders in the URI exactly as shown - the code automatically replaces them - Only change the cluster URL part (after the @ symbol) to match your MongoDB Atlas cluster
- Provide your actual username and password in the separate MONGODB_USERNAME and MONGODB_PW variables
- Ensure the database user has read access to the
myCGMitcdatabase
Test Connection
uv run python -m src.loopy.connection.mongodb
You should see output like:
✓ Connected to MongoDB database: myCGMitc
Available databases: ['myCGMitc', 'test', 'admin', 'local']
Collections in myCGMitc: ['entries', 'treatments', 'food', 'settings', 'devicestatus', 'auth_roles', 'auth_subjects', 'activity', 'profile']
✓ Disconnected from MongoDB
Database Schema
The myCGMitc database contains the following collections:
entries- CGM/blood glucose readings (primary data for analysis)treatments- Insulin doses and medical treatmentsfood- Food intake and carbohydrate datasettings- Loop system configurationdevicestatus- Device status and connectivity infoprofile- User profile and basal rate settingsactivity- Activity and exercise logsauth_roles,auth_subjects- Authentication data
entries Collection Schema (CGM Data)
Collection Stats:
- Total documents: 243,047 CGM readings
- Date range: March 2023 to July 2025 (~2 years of data)
- Device: Dexcom CGM ("share2")
- Data actively updated (real-time)
Document Structure:
{
"_id": "ObjectId",
"sgv": 163, // Blood glucose value (mg/dL)
"date": 1678724324000.0, // Unix timestamp (milliseconds)
"dateString": "2023-03-13T16:18:44.000Z", // ISO formatted date
"trend": 4, // Glucose trend indicator (1-7)
"direction": "Flat", // Trend direction text
"device": "share2", // CGM device identifier
"type": "sgv", // Sensor glucose value type
"utcOffset": 0, // UTC offset
"sysTime": "2023-03-13T16:18:44.000Z" // System timestamp
}
Key Fields:
sgv- Primary glucose reading in mg/dLdate- Unix timestamp for sorting and time-based queriesdirection- Trend indicators: "Flat", "FortyFiveUp", "FortyFiveDown", "SingleUp", "SingleDown", "DoubleUp", "DoubleDown"trend- Numeric trend value (1-7 scale)
Database Indexes:
- Optimized indexes on
date,sgv,dateString,typefor efficient queries
Development Stages
✅ Stage 1: Database Connection
- MongoDB Atlas connection with environment variables
- Basic authentication and connection testing
- Database and collection discovery
✅ Stage 2: CGM Data Access
- Connect to
entriescollection for glucose readings - Explore document structure and schema
- Implement basic data retrieval queries
- Verify data format and field analysis
✅ Stage 3: Time-Range Queries
- Implement date/time filtering for CGM data
- Add functions to query specific time periods (24h, week, month, custom ranges)
- Test with various time ranges and validate results
- Summary statistics and data validation
✅ Stage 4: Data Processing & DataFrame Integration
- Convert MongoDB documents to pandas DataFrames with PyArrow backend
- Implement efficient data cleaning and validation
- Handle timestamp conversions and timezone management
- Prepare data for time-series analysis
🔄 Stage 5: Pattern Analysis & Temporal Insights (Next)
- Time-of-day pattern analysis (hourly glucose trends)
- Day-of-week pattern identification
- Weekly and monthly trend analysis
- Statistical summaries for specific time periods
- Data preparation for marimo notebook exploration
📋 Upcoming Stages
- Stage 6: Treatment correlation analysis (future goal)
Key Files & Commands
Core Modules:
src/loopy/connection/mongodb.py- Main MongoDB connection modulesrc/loopy/data/cgm.py- CGM data access and time-range queriessrc/loopy/utils/debug.py- Connection debugging utilitiesdocs/analysis_patterns.md- Analysis methodology documentation.env.example- Environment variable templateCLAUDE.md- Development guidance for AI assistants
Testing Commands:
# Test database connection
uv run python -m src.loopy.connection.mongodb
# Test CGM data access and time-range queries
uv run python -m src.loopy.data.cgm
# Debug connection issues
uv run python -m src.loopy.utils.debug
# Start marimo notebook for exploration
uv run marimo edit dev/exploratory/analysis.py
# Run usage example (3 months of CGM data)
uv run python dev/usage_example.py
Troubleshooting
If you encounter authentication errors:
- Verify credentials in MongoDB Atlas dashboard
- Ensure database user has appropriate permissions
- Check for extra spaces in
.envfile - Test connection with MongoDB Compass first
- Run
uv run python -m src.loopy.utils.debugfor detailed diagnostics
Development Standards
This project follows Python best practices for professional coding:
- Code Quality: Type hints, comprehensive docstrings, error handling
- Reproducibility: Pinned dependencies, environment configuration, deterministic workflows
- Documentation: Detailed docstrings, inline comments, complete setup instructions
- Testing: Input validation, data quality checks, comprehensive testing
Quick Start Example
from loopy.data.cgm import CGMDataAccess
from datetime import datetime, timedelta
# Initialize CGM data access
cgm = CGMDataAccess()
cgm.connect()
# Get last week's data as a cleaned DataFrame
df = cgm.get_dataframe_for_period('last_week')
# Basic analysis
analysis = cgm.analyze_dataframe(df)
print(f"Average glucose: {analysis['basic_stats']['avg_glucose']:.1f} mg/dL")
print(f"Time in range: {analysis['time_in_range']['normal_percent']:.1f}%")
cgm.disconnect()
Security Notes
- Never commit the
.envfile to version control - Use read-only database connections when possible
- Store connection credentials securely
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
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 loopy_basic-0.1.0.tar.gz.
File metadata
- Download URL: loopy_basic-0.1.0.tar.gz
- Upload date:
- Size: 15.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bb19196d3d8d5a4b67f5bdbe2a6679c899895c8aebe4e7a9e33cc98abc6e3920
|
|
| MD5 |
cdeb6b7036f00316fb225a76274dd117
|
|
| BLAKE2b-256 |
ab72757b1869d8024e33676d626720d2681bd2409c9f499895437c198719f780
|
File details
Details for the file loopy_basic-0.1.0-py3-none-any.whl.
File metadata
- Download URL: loopy_basic-0.1.0-py3-none-any.whl
- Upload date:
- Size: 12.6 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 |
cda84ef914569b397a0e93b081bf40406bd0df1a536bdcb58e5f92c86d6f1acf
|
|
| MD5 |
05a125ff3656317d8f3eb2c2374d9595
|
|
| BLAKE2b-256 |
09aeb8d3b55a46c591d1dda3a6255b3d1568f2286113021d29de6cbd21f4b1d0
|