med.glucose - Diabetes & Glucose Management
HbA1c estimation, CGM analytics, insulin dosing algorithms, DKA assessment, and hypoglycemia classification.
HbA1c / eAG Conversions
hba1c_from_glucose(mean_glucose_mgdl) → dict
Estimate HbA1c from mean glucose using the ADAG equation (Nathan et al., Diabetes Care 2008).
eA1C = (mean_glucose + 46.7) / 28.7
Returns estimated_hba1c and category (NORMAL, PREDIABETES, DIABETES).
lib.glucose.hba1c_from_glucose(154)
# {'estimated_hba1c': 7.0, 'category': 'DIABETES'}
glucose_from_hba1c(hba1c) → dict
Reverse ADAG: estimate mean glucose from HbA1c.
lib.glucose.glucose_from_hba1c(7.0)
# {'estimated_mean_glucose_mgdl': 154.2, 'estimated_mean_glucose_mmol': 8.6}
CGM Analytics
time_in_range(readings, low=70, high=180) → dict
Calculate Time In Range (TIR) from CGM readings. International consensus targets: 70-180 mg/dL.
Returns TIR, time below range (TBR), time above range (TAR), and assessment (EXCELLENT, GOOD, NEEDS_IMPROVEMENT, POOR).
readings = [85, 120, 140, 190, 210, 95, 130, 72, 180, 65]
lib.glucose.time_in_range(readings)
# {'time_in_range_pct': 70.0, 'time_below_range_pct': 10.0,
# 'time_above_range_pct': 20.0, 'assessment': 'EXCELLENT'}
gmi(mean_glucose_mgdl) → dict
Glucose Management Indicator (Bergenstal et al. 2018). Replaces "estimated A1C" for CGM data.
GMI = 3.31 + 0.02392 * mean_glucose
glycemic_variability(readings) → dict
Calculate variability metrics from CGM data:
| Metric | Description | Target |
|---|---|---|
| CV | Coefficient of variation | under 36% |
| MAGE | Mean Amplitude of Glycemic Excursions | Lower is better |
| SD | Standard deviation | - |
Returns stability: STABLE, MODERATE_VARIABILITY, or HIGH_VARIABILITY.
lib.glucose.glycemic_variability([100, 150, 200, 120, 180, 90, 160])
# {'cv_percent': 28.5, 'mage': 65.0, 'stability': 'STABLE'}
Insulin Dosing
insulin_sensitivity_factor(tdd, insulin_type="rapid") → dict
Calculate ISF (how much 1 unit drops glucose):
- Rapid-acting: 1800 rule (
ISF = 1800 / TDD) - Regular: 1500 rule (
ISF = 1500 / TDD)
carb_ratio(tdd) → dict
Insulin-to-Carb Ratio using the 500 rule (ICR = 500 / TDD).
correction_dose(current_bg, target_bg, isf) → dict
Calculate correction insulin dose: (current_BG - target_BG) / ISF.
basal_rate(tdd) → dict
Basal insulin using the 50% rule. Returns daily dose and hourly pump rate.
sliding_scale(current_bg) → dict
Traditional sliding scale dosing. Returns recommended dose and severity classification.
full_regimen(tdd, insulin_type="rapid") → dict
Complete insulin regimen from TDD: basal dose, hourly rate, ISF, and ICR.
lib.glucose.full_regimen(40)
# {'basal_units_per_day': 20.0, 'basal_hourly_rate': 0.83,
# 'isf': 45.0, 'icr': 12.5}
DKA & Hypoglycemia
dka_check(glucose, ph=7.4, bicarb=24, ketones=0) → dict
Diabetic Ketoacidosis diagnostic assessment.
| Criteria | Threshold |
|---|---|
| Glucose | over 250 mg/dL |
| pH | under 7.3 |
| Bicarbonate | under 18 mEq/L |
| Ketones | over 0.6 mmol/L |
Returns severity (MILD, MODERATE, SEVERE, NOT_DKA) and management recommendations.
lib.glucose.dka_check(350, 7.1, 12, 3.2)
# {'diagnosis': 'DKA', 'severity': 'MODERATE',
# 'management': 'IV insulin drip, fluid resuscitation, hourly monitoring'}
hypo_check(glucose) → dict
ADA hypoglycemia classification:
- Level 1 (under 70 mg/dL): Alert value
- Level 2 (under 54 mg/dL): Clinically significant
MOISSCode Example
protocol DiabetesReview {
input: Patient p;
let a1c = med.glucose.hba1c_from_glucose(154);
let regimen = med.glucose.full_regimen(40);
let dka = med.glucose.dka_check(350, 7.1, 12, 3.2);
if dka.severity != "NOT_DKA" {
alert "DKA detected" severity: critical;
}
}
See Also
- med.lab — interpret glucose and HbA1c lab panels
- med.pk — insulin and metformin dosing adjustments
- med.scores — cardiovascular risk scoring for diabetic patients
- med.icd — ICD-10 codes for diabetes diagnosis
- Diabetes CGM Dashboard — full walkthrough combining all diabetes modules