add Trends page: GMI trend, overnight/daytime, weekday pattern, bolus, site-age (FR7, FR8)
reporting.py gains overnight_daytime_split, day_of_week_pattern, bolus_stats, and site_interval_stats. The last two are explicitly scoped to bolus-only insulin (no basal reconstruction) since that needs the Nightscout profile, which is the separate profile-tuning feature's territory — documented as a caveat in both the UI and the design doc. Co-Authored-By: Claude Sonnet 5 <[email protected]>
This commit is contained in:
+133
@@ -9,8 +9,12 @@ definities waarop dit gebaseerd is (Battelino et al. 2019, Bergenstal et al.
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from itertools import pairwise
|
||||
|
||||
import pandas as pd
|
||||
|
||||
from nightscout_client import INSULIN_EVENT_TYPES
|
||||
|
||||
# Klinische drempels in mmol/L (Battelino et al. 2019, Diabetes Care 42(8):1593-1603).
|
||||
VERY_LOW = 3.0
|
||||
LOW = 3.9
|
||||
@@ -226,3 +230,132 @@ def agp_percentile_bands(glucose: pd.DataFrame, bin_minutes: int = 30) -> pd.Dat
|
||||
grouped.columns = ["p5", "p25", "p50", "p75", "p95"]
|
||||
grouped = grouped.reset_index().rename(columns={"bin": "minute_of_day"})
|
||||
return grouped[AGP_PERCENTILE_COLUMNS].sort_values("minute_of_day").reset_index(drop=True)
|
||||
|
||||
|
||||
def overnight_daytime_split(
|
||||
glucose: pd.DataFrame, overnight_start: int = 0, overnight_end: int = 6
|
||||
) -> dict[str, dict]:
|
||||
"""Tijd-in-bereik apart voor de nacht (overnight_start-overnight_end uur) en overdag."""
|
||||
empty = tir_breakdown_5band(glucose)
|
||||
if glucose.empty:
|
||||
return {"overnight": empty, "daytime": empty}
|
||||
|
||||
hour = glucose["timestamp"].dt.hour
|
||||
is_overnight = (hour >= overnight_start) & (hour < overnight_end)
|
||||
return {
|
||||
"overnight": tir_breakdown_5band(glucose[is_overnight]),
|
||||
"daytime": tir_breakdown_5band(glucose[~is_overnight]),
|
||||
}
|
||||
|
||||
|
||||
WEEKDAY_NAMES = ["Maandag", "Dinsdag", "Woensdag", "Donderdag", "Vrijdag", "Zaterdag", "Zondag"]
|
||||
|
||||
|
||||
def day_of_week_pattern(glucose: pd.DataFrame) -> pd.DataFrame:
|
||||
"""Gemiddelde glucose en tijd-in-bereik per weekdag."""
|
||||
columns = ["weekday", "mean_mmol", "in_range_pct", "count"]
|
||||
if glucose.empty:
|
||||
return pd.DataFrame(columns=columns)
|
||||
|
||||
df = glucose.copy()
|
||||
df["weekday"] = df["timestamp"].dt.dayofweek
|
||||
|
||||
rows = []
|
||||
for weekday in range(7):
|
||||
day_df = df[df["weekday"] == weekday]
|
||||
if day_df.empty:
|
||||
rows.append(
|
||||
{"weekday": WEEKDAY_NAMES[weekday], "mean_mmol": None, "in_range_pct": None, "count": 0}
|
||||
)
|
||||
continue
|
||||
tir = tir_breakdown_5band(day_df)
|
||||
rows.append(
|
||||
{
|
||||
"weekday": WEEKDAY_NAMES[weekday],
|
||||
"mean_mmol": round(day_df["mmol"].mean(), 1),
|
||||
"in_range_pct": tir["in_range"],
|
||||
"count": len(day_df),
|
||||
}
|
||||
)
|
||||
return pd.DataFrame(rows, columns=columns)
|
||||
|
||||
|
||||
def bolus_stats(treatments: pd.DataFrame) -> dict:
|
||||
"""Samenvatting van bolus-insuline, opgesplitst naar type.
|
||||
|
||||
Let op: dit betreft alleen bolusinsuline (maaltijd/correctie/combo/SMB).
|
||||
Basaalinsuline wordt hier niet in meegenomen — dat vereist profielgegevens
|
||||
(zie docs/design-reporting.md, scoping-kanttekening) en levert dus geen
|
||||
volledige totale-dagdosis op.
|
||||
"""
|
||||
if treatments.empty:
|
||||
return {"total_units": 0.0, "count": 0, "by_type": {}}
|
||||
|
||||
bolus = treatments[treatments["eventType"].isin(INSULIN_EVENT_TYPES) & treatments["insulin"].notna()]
|
||||
if bolus.empty:
|
||||
return {"total_units": 0.0, "count": 0, "by_type": {}}
|
||||
|
||||
by_type = bolus.groupby("eventType")["insulin"].sum().round(1).to_dict()
|
||||
return {
|
||||
"total_units": round(bolus["insulin"].sum(), 1),
|
||||
"count": len(bolus),
|
||||
"by_type": by_type,
|
||||
}
|
||||
|
||||
|
||||
SITE_INTERVAL_COLUMNS = [
|
||||
"start",
|
||||
"end",
|
||||
"duration_days",
|
||||
"mean_mmol",
|
||||
"in_range_pct",
|
||||
"total_bolus_units",
|
||||
"avg_bolus_units_per_day",
|
||||
"total_carbs_g",
|
||||
]
|
||||
|
||||
|
||||
def _site_interval_row(glucose: pd.DataFrame, treatments: pd.DataFrame, start, end) -> dict:
|
||||
duration_days = max((end - start).total_seconds() / 86400, 1e-9)
|
||||
window_glucose = glucose[(glucose["timestamp"] >= start) & (glucose["timestamp"] < end)]
|
||||
window_treatments = (
|
||||
treatments[(treatments["timestamp"] >= start) & (treatments["timestamp"] < end)]
|
||||
if not treatments.empty
|
||||
else treatments
|
||||
)
|
||||
bolus = bolus_stats(window_treatments)
|
||||
total_carbs = window_treatments["carbs"].fillna(0).sum() if not treatments.empty else 0.0
|
||||
tir = tir_breakdown_5band(window_glucose)
|
||||
|
||||
return {
|
||||
"start": start,
|
||||
"end": end,
|
||||
"duration_days": round(duration_days, 1),
|
||||
"mean_mmol": round(window_glucose["mmol"].mean(), 1) if not window_glucose.empty else None,
|
||||
"in_range_pct": tir["in_range"],
|
||||
"total_bolus_units": bolus["total_units"],
|
||||
"avg_bolus_units_per_day": round(bolus["total_units"] / duration_days, 1),
|
||||
"total_carbs_g": round(total_carbs, 0),
|
||||
}
|
||||
|
||||
|
||||
def site_interval_stats(
|
||||
glucose: pd.DataFrame, treatments: pd.DataFrame, site_changes: pd.DataFrame
|
||||
) -> pd.DataFrame:
|
||||
"""Statistieken per interval tussen opeenvolgende pompwissels (FR8).
|
||||
|
||||
Voor elk interval sinds een pompwissel: duur in dagen ("leeftijd" van de
|
||||
pompsite bij het einde van het interval), gemiddelde glucose,
|
||||
tijd-in-bereik, en bolus-insuline/koolhydraten in dat interval — als
|
||||
benadering van of insulinebehoefte samenhangt met de leeftijd van de
|
||||
pompsite. Zie bolus_stats() voor de kanttekening dat basaalinsuline hier
|
||||
niet in zit.
|
||||
"""
|
||||
if site_changes.empty or glucose.empty:
|
||||
return pd.DataFrame(columns=SITE_INTERVAL_COLUMNS)
|
||||
|
||||
change_times = sorted(site_changes["timestamp"])
|
||||
boundaries = [*change_times, glucose["timestamp"].max()]
|
||||
|
||||
rows = [_site_interval_row(glucose, treatments, start, end) for start, end in pairwise(boundaries)]
|
||||
return pd.DataFrame(rows, columns=SITE_INTERVAL_COLUMNS)
|
||||
|
||||
Reference in New Issue
Block a user