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.
Stages 1-3 Complete ✅ - Database connection, CGM data access, and time-range queries implemented
src/loopy/
├── connection/ # Database connectivity
│ └── mongodb.py
├── data/ # Data access modules
│ └── cgm.py
└── utils/ # Utilities and debugging
└── debug.py
docs/ # Analysis documentation
notebooks/ # Marimo notebooks for exploration
├── exploratory/ # Interactive analysis
└── reports/ # Analysis reports
tests/ # Test modules
This project uses uv for dependency management:
uv syncCopy the example environment file:
cp .env.example .envEdit .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=myCGMitcImportant 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
uv run python -m src.loopy.connection.mongodbYou 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
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
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
- MongoDB Atlas connection with environment variables
- Basic authentication and connection testing
- Database and collection discovery
- Connect to
entriescollection for glucose readings - Explore document structure and schema
- Implement basic data retrieval queries
- Verify data format and field analysis
- 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
- 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 and temporal insights (marimo notebooks)
- Stage 6: Treatment correlation analysis (future goal)
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 notebooks/exploratory/analysis.pyIf 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
- Never commit the
.envfile to version control - Use read-only database connections when possible
- Store connection credentials securely