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.6.0.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.6.0-py3-none-any.whl (86.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: cgmpy-0.6.0.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.6.0.tar.gz
Algorithm Hash digest
SHA256 c149831288c3e669c474c8d61393ed46c7c05b0f30b1beb6e9529d26bdd7445b
MD5 7f4c44ca991389ffde10200560348c75
BLAKE2b-256 1be28420052e87d300e3c2b64b03467e4bf72849e418c085b6df6d14b9d6c4d9

See more details on using hashes here.

File details

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

File metadata

  • Download URL: cgmpy-0.6.0-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.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 959429ce5f990eaea7c44e69381dbd5e40b1f9391856f1ecf7d3429c5b12b53b
MD5 107fcaa3189dacb1224a1991beafe9cf
BLAKE2b-256 8012a9c3084ce2093cf67a3fb8f99a4fcd5e5347002222c68fb8e705990c9e8b

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