Skip to main content

Modular Python library for Continuous Glucose Monitoring (CGM) data analysis

Project description

CGMPy — Continuous Glucose Monitoring Analysis

CI PyPI version Python versions License: MIT Code style: Ruff Documentation Status: Beta

A modular Python library for analyzing Continuous Glucose Monitoring (CGM) data, with a strong focus on clinical metrics, pregnancy-specific workflows, and reproducible research.


What is CGMPy?

CGMPy provides a clean, type-hinted Python API for:

  • Loading CGM data from CSV, Parquet, or in-memory pandas.DataFrame with automatic device detection (Dexcom Clarity, FreeStyle Libre / Libreview, Tandem, Medtronic CareLink).
  • Processing & validating glucose time series (glucometric ranges, gap detection, interval normalization).
  • Computing clinical metrics following international consensus:
    • Basic statistics: mean, median, GMI, glucose CV.
    • Time in range (TIR): TIR, TAR, TBR with customizable targets (diabetes vs. pregnancy).
    • Glycemic variability: SD, CV, MAGE, MODD, CONGA, J-Index, LBGI, HBGI, GRI.
    • Pregnancy-specific metrics for gestational diabetes.
  • Visualizing ambulatory glucose profiles (AGP), daily trends, and statistical summaries.
  • Comparing results against the AGATA reference implementation for validation.

The library is modular: every component (loader, processor, analyzer, exporter, plotter, metrics) can be used independently or composed through the high-level GlucoseAnalysis facade.


Installation

# Stable release
pip install cgmpy

# With AGATA integration (optional, requires agata)
pip install cgmpy[agata]

# With documentation / development tools
pip install cgmpy[dev,docs]

CGMPy requires Python ≥ 3.10.

From source

git clone https://github.com/javierpa95/cgmpy.git
cd cgmpy
pip install -e .[dev]

Quickstart

from cgmpy import GlucoseAnalysis

# One-line analysis
analysis = GlucoseAnalysis("path/to/glucose.csv")
report = analysis.get_comprehensive_report()
print(f"Time in Range: {report['time_in_range']['tir']}%")

# Generate the standard Ambulatory Glucose Profile
analysis.plot_comprehensive_dashboard()

Lower-level modular API

from cgmpy.data import ModularGlucoseData
from cgmpy.metrics import ModularGlucoseMetrics
from cgmpy.metrics.targets import get_targets

data = ModularGlucoseData("cgm_data.csv")
data.filter_by_date_range("2024-01-01", "2024-01-31")

# Compute metrics with pregnancy-specific cutoffs
targets = get_targets("pregnancy")
metrics = ModularGlucoseMetrics(data, targets=targets)
print(metrics.time_in_range())
print(metrics.variability().mage())

Features

Domain Capabilities
Data ingestion CSV, Parquet, DataFrame. Auto delimiter, header detection, device-specific loaders.
Validation Glucose range check, time interval regularity, gap analysis, data quality scoring.
Basic metrics Mean, median, GMI, SD, IQR, % time in any custom range.
Time in range TIR, TAR (level 1/2), TBR (level 1/2) with international consensus cutoffs.
Variability CV, MAGE, MODD, CONGA, J-Index, LBGI, HBGI, GRI, MAG, ADRR.
Pregnancy Dedicated GestationalDiabetes metrics, pregnancy glucose targets (63–140 mg/dL).
Visualization AGP (Ambulatory Glucose Profile), daily traces, statistical dashboards.
AGATA parity AgataAnalysis wrapper for side-by-side comparison with the AGATA reference.
Export To Parquet (optimized) / CSV / Excel, with deduplication and type coercion.

Documentation


Clinical & Research Use

CGMPy is built to support clinical research workflows in diabetes and gestational diabetes. If you use CGMPy in a publication, please cite the version you used (see CITATION.cff).

Important — Not a medical device. CGMPy is a research and analysis tool. It is not a medical device and must not be used as a substitute for professional medical advice. Always validate clinical interpretations with a qualified healthcare provider.


Contributing

We welcome contributions! Please read CONTRIBUTING.md for guidelines on reporting bugs, proposing features, and submitting pull requests. By participating, you agree to abide by the Code of Conduct.

Quick links:


Security & Privacy

CGMPy can process sensitive health data. Please review SECURITY.md for:

  • How to report vulnerabilities.
  • The strict policy on committing Protected Health Information (PHI).
  • How to anonymize datasets before sharing examples or bug reports.

Never commit real patient data, real CGM exports with personal identifiers, or any HIPAA/GDPR-protected data. Examples and tests must use synthetic or fully anonymized data only.


License

CGMPy is released under the MIT License. See LICENSE for details.

Copyright © 2024–2026 Javier Peñate Arrieta.


Acknowledgments

  • The clinical metric definitions follow international consensus guidelines (Battelino et al., Diabetes Care 2019; Bergenstal et al., J. Diabetes Sci. Technol. 2013).
  • The AGATA library is the reference implementation used for cross-validation.
  • Inspired by R packages like iglu and Python packages in the diabetes research ecosystem.

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

cgmpy-0.5.2.tar.gz (947.7 kB view details)

Uploaded Source

Built Distribution

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

cgmpy-0.5.2-py3-none-any.whl (86.9 kB view details)

Uploaded Python 3

File details

Details for the file cgmpy-0.5.2.tar.gz.

File metadata

  • Download URL: cgmpy-0.5.2.tar.gz
  • Upload date:
  • Size: 947.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for cgmpy-0.5.2.tar.gz
Algorithm Hash digest
SHA256 358ab0cba10bf42eb8a55e61b9b480b37792c670d78227cbb400d9063a4bbce1
MD5 4ff5cac73429d9052a252e42ce848069
BLAKE2b-256 4984e331d5cdc78ec365fb402523e7429f15d3752811c9815504a29ba4b146a6

See more details on using hashes here.

File details

Details for the file cgmpy-0.5.2-py3-none-any.whl.

File metadata

  • Download URL: cgmpy-0.5.2-py3-none-any.whl
  • Upload date:
  • Size: 86.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for cgmpy-0.5.2-py3-none-any.whl
Algorithm Hash digest
SHA256 626686c35ed7015d639c96c87488f0c329272c52d28a5461e0ebee985686974c
MD5 6e5d8e1706568328a45c6e0921afe9f4
BLAKE2b-256 21bc3b971b16affe1f6b706694f00225a97926ae897508acf397d2525a91af94

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