Compare commits
7
Commits
55883480c0
..
dev
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e08f269855 | ||
|
|
b8e01fa8c9 | ||
|
|
374ead70e5 | ||
|
|
9e11dd8d73 | ||
|
|
441bb84212 | ||
|
|
00fb81b283 | ||
|
|
699848b479 |
Vendored
+11
@@ -0,0 +1,11 @@
|
|||||||
|
pipeline {
|
||||||
|
agent any
|
||||||
|
|
||||||
|
stages {
|
||||||
|
stage('Do nothing') {
|
||||||
|
steps {
|
||||||
|
sh '/bin/true'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+78
-31
@@ -38,12 +38,31 @@ _REQUEST_TIMEOUT = 10
|
|||||||
# meest recente ~1150 metingen terug — nog geen 4 dagen — terwijl er wel
|
# meest recente ~1150 metingen terug — nog geen 4 dagen — terwijl er wel
|
||||||
# degelijk weken oudere data in Nightscout beschikbaar bleek te zijn). Eén
|
# degelijk weken oudere data in Nightscout beschikbaar bleek te zijn). Eén
|
||||||
# verzoek is dus niet genoeg voor meerdaagse rapportages; _paginated_get()
|
# verzoek is dus niet genoeg voor meerdaagse rapportages; _paginated_get()
|
||||||
# loopt terug in de tijd met `find[date][$lte]`-cursors tot de gevraagde
|
# loopt terug in de tijd met `find[...][$lte]`-cursors tot de gevraagde
|
||||||
# `since`-grens bereikt is of de server minder dan een volle pagina teruggeeft
|
# `since`-grens bereikt is of de server minder dan een volle pagina teruggeeft
|
||||||
# (= geen oudere data meer beschikbaar, wat verschilt van "afgeknipt").
|
# (= geen oudere data meer beschikbaar, wat verschilt van "afgeknipt").
|
||||||
|
#
|
||||||
|
# De twee endpoints filteren op een ANDER veld, en /treatments heeft nog een
|
||||||
|
# addertje: entries filtert/sorteert op het numerieke `date` (epoch ms) en
|
||||||
|
# geeft zonder filter gewoon de meest recente `count` terug. treatments
|
||||||
|
# filtert/sorteert op het string-veld `created_at` (ISO 8601) — filteren op
|
||||||
|
# `date` levert daar stil een lege pagina op zodra je voorbij een intern
|
||||||
|
# venster van ~4 dagen gaat, ook al bevatten de documenten prima een geldig
|
||||||
|
# `date`-veld (geverifieerd rechtstreeks in MongoDB: alle 123k+ treatments
|
||||||
|
# hebben een numeriek `date`, en zonder enig filter geeft het endpoint alsnog
|
||||||
|
# stilzwijgend niet meer dan ~4 dagen terug). Zodra je expliciet op
|
||||||
|
# `created_at` filtert — zelfs met "nu" als bovengrens op de eerste pagina —
|
||||||
|
# werkt paginering wél correct en is de volledige geschiedenis (maanden,
|
||||||
|
# geverifieerd in de database) gewoon opvraagbaar. Vandaar: de cursor wordt
|
||||||
|
# nooit weggelaten (ook niet op de allereerste pagina), en welk veld
|
||||||
|
# gebruikt wordt is per endpoint expliciet ("date" voor entries,
|
||||||
|
# "created_at" voor treatments) — zie ENTRIES_CURSOR_FIELD/TREATMENTS_CURSOR_FIELD.
|
||||||
_PAGE_SIZE = 1000
|
_PAGE_SIZE = 1000
|
||||||
_MAX_PAGES = 150 # 150 000 documenten; ruim boven wat een 90-dagenrapport nodig heeft
|
_MAX_PAGES = 150 # 150 000 documenten; ruim boven wat een 90-dagenrapport nodig heeft
|
||||||
|
|
||||||
|
ENTRIES_CURSOR_FIELD = "date"
|
||||||
|
TREATMENTS_CURSOR_FIELD = "created_at"
|
||||||
|
|
||||||
|
|
||||||
class NightscoutConfigError(RuntimeError):
|
class NightscoutConfigError(RuntimeError):
|
||||||
"""NIGHTSCOUT_URL ontbreekt of is ongeldig."""
|
"""NIGHTSCOUT_URL ontbreekt of is ongeldig."""
|
||||||
@@ -88,44 +107,62 @@ def _get(path: str, params: dict) -> list:
|
|||||||
return response.json()
|
return response.json()
|
||||||
|
|
||||||
|
|
||||||
def _paginated_get(path: str, since: datetime, until: datetime | None = None) -> list:
|
def _format_cursor(cursor_dt: datetime, cursor_field: str) -> int | str:
|
||||||
|
"""Zet een cursor-datetime om naar de vorm die het gekozen veld verwacht."""
|
||||||
|
if cursor_field == ENTRIES_CURSOR_FIELD:
|
||||||
|
return int(cursor_dt.timestamp() * 1000)
|
||||||
|
# created_at: milliseconde-precisie ISO 8601 met Z-suffix, exact het
|
||||||
|
# formaat dat Nightscout zelf in created_at opslaat.
|
||||||
|
return cursor_dt.strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3] + "Z"
|
||||||
|
|
||||||
|
|
||||||
|
def _parse_item_time(item: dict, cursor_field: str) -> datetime | None:
|
||||||
|
"""Lees het tijdstip van een document uit het veld waar dit endpoint op filtert."""
|
||||||
|
if cursor_field not in item:
|
||||||
|
return None
|
||||||
|
if cursor_field == ENTRIES_CURSOR_FIELD:
|
||||||
|
return datetime.fromtimestamp(item[cursor_field] / 1000, tz=timezone.utc)
|
||||||
|
return datetime.fromisoformat(item[cursor_field].replace("Z", "+00:00"))
|
||||||
|
|
||||||
|
|
||||||
|
def _paginated_get(
|
||||||
|
path: str, since: datetime, until: datetime | None, cursor_field: str
|
||||||
|
) -> list:
|
||||||
"""Haal alle documenten op tussen `since` en `until` (of tot nu), pagina voor pagina.
|
"""Haal alle documenten op tussen `since` en `until` (of tot nu), pagina voor pagina.
|
||||||
|
|
||||||
Zie de toelichting bij _PAGE_SIZE hierboven voor waarom dit niet één
|
Zie de toelichting bij _PAGE_SIZE hierboven voor waarom dit niet één
|
||||||
verzoek met een groot `count` kan zijn (BUG: dat leverde bij een
|
verzoek met een groot `count` kan zijn, en waarom `cursor_field` per
|
||||||
meerdaags rapport stilzwijgend alleen de laatste ~3-4 dagen op, ongeacht
|
endpoint verschilt en nooit wordt weggelaten (BUG, zowel de originele
|
||||||
de gekozen periode). Elke pagina wordt opgevraagd met `find[date][$lte]`
|
count-truncatie als treatments' verborgen ~4-dagenvenster zonder
|
||||||
net onder de oudste timestamp van de vorige pagina, zodat elk document
|
expliciet find[created_at]-filter). Elke pagina wordt opgevraagd met een
|
||||||
precies één keer terugkomt. Stopt zodra de oudste timestamp in een pagina
|
cursor net onder de oudste timestamp van de vorige pagina, zodat elk
|
||||||
op of voorbij `since` ligt, of zodra de server minder dan een volle
|
document precies één keer terugkomt. Stopt zodra de oudste timestamp in
|
||||||
pagina teruggeeft (dan is er geen oudere data meer, punt).
|
een pagina op of voorbij `since` ligt, of zodra de server minder dan een
|
||||||
|
volle pagina teruggeeft (dan is er geen oudere data meer, punt).
|
||||||
"""
|
"""
|
||||||
since_ms = int(since.timestamp() * 1000)
|
cursor_dt = until if until is not None else datetime.now(timezone.utc)
|
||||||
cursor_ms = int(until.timestamp() * 1000) if until is not None else None
|
|
||||||
|
|
||||||
results: list = []
|
results: list = []
|
||||||
for _ in range(_MAX_PAGES):
|
for _ in range(_MAX_PAGES):
|
||||||
params = {"count": _PAGE_SIZE}
|
params = {"count": _PAGE_SIZE, f"find[{cursor_field}][$lte]": _format_cursor(cursor_dt, cursor_field)}
|
||||||
if cursor_ms is not None:
|
|
||||||
params["find[date][$lte]"] = cursor_ms
|
|
||||||
page = _get(path, params)
|
page = _get(path, params)
|
||||||
if not page:
|
if not page:
|
||||||
break
|
break
|
||||||
results.extend(page)
|
results.extend(page)
|
||||||
|
|
||||||
dates = [item["date"] for item in page if "date" in item]
|
item_times = [t for t in (_parse_item_time(item, cursor_field) for item in page) if t is not None]
|
||||||
if not dates:
|
if not item_times:
|
||||||
break
|
break
|
||||||
oldest_ms = min(dates)
|
oldest_dt = min(item_times)
|
||||||
if oldest_ms <= since_ms or len(page) < _PAGE_SIZE:
|
if oldest_dt <= since or len(page) < _PAGE_SIZE:
|
||||||
break
|
break
|
||||||
cursor_ms = oldest_ms - 1
|
cursor_dt = oldest_dt - timedelta(milliseconds=1)
|
||||||
return results
|
return results
|
||||||
|
|
||||||
|
|
||||||
def _entries_since(since: datetime, until: datetime | None = None) -> pd.DataFrame:
|
def _entries_since(since: datetime, until: datetime | None = None) -> pd.DataFrame:
|
||||||
"""Haal entries op sinds `since` (en optioneel tot `until`), lokaal gefilterd."""
|
"""Haal entries op sinds `since` (en optioneel tot `until`), lokaal gefilterd."""
|
||||||
data = _paginated_get("/api/v1/entries/sgv.json", since, until)
|
data = _paginated_get("/api/v1/entries/sgv.json", since, until, ENTRIES_CURSOR_FIELD)
|
||||||
|
|
||||||
if not data:
|
if not data:
|
||||||
return pd.DataFrame(columns=["timestamp", "mmol"])
|
return pd.DataFrame(columns=["timestamp", "mmol"])
|
||||||
@@ -139,29 +176,39 @@ def _entries_since(since: datetime, until: datetime | None = None) -> pd.DataFra
|
|||||||
return df[["timestamp", "mmol"]].sort_values("timestamp").reset_index(drop=True)
|
return df[["timestamp", "mmol"]].sort_values("timestamp").reset_index(drop=True)
|
||||||
|
|
||||||
|
|
||||||
|
_TREATMENT_COLUMNS = ["timestamp", "eventType", "insulin", "carbs", "type", "absolute", "duration"]
|
||||||
|
|
||||||
|
|
||||||
def _treatments_since(since: datetime, until: datetime | None = None) -> pd.DataFrame:
|
def _treatments_since(since: datetime, until: datetime | None = None) -> pd.DataFrame:
|
||||||
"""Haal treatments op sinds `since` (en optioneel tot `until`), lokaal gefilterd."""
|
"""Haal treatments op sinds `since` (en optioneel tot `until`), lokaal gefilterd.
|
||||||
data = _paginated_get("/api/v1/treatments.json", since, until)
|
|
||||||
|
`type` en `absolute`/`duration` komen erbij naast de oorspronkelijke
|
||||||
|
kolommen: AndroidAPS registreert een SMB-microbolus niet als eigen
|
||||||
|
`eventType`, maar als `eventType == "Correction Bolus"` met
|
||||||
|
`type == "SMB"` (zie reporting.bolus_stats() / gitea #20) — en
|
||||||
|
`absolute` (E/uur) + `duration` (minuten) op Temp Basal-treatments maken
|
||||||
|
het mogelijk afgeleverde basaalinsuline te berekenen zonder het
|
||||||
|
pompprofiel te hoeven ophalen (zie reporting.basal_stats()).
|
||||||
|
"""
|
||||||
|
data = _paginated_get("/api/v1/treatments.json", since, until, TREATMENTS_CURSOR_FIELD)
|
||||||
|
|
||||||
if not data:
|
if not data:
|
||||||
return pd.DataFrame(columns=["timestamp", "eventType", "insulin", "carbs"])
|
return pd.DataFrame(columns=_TREATMENT_COLUMNS)
|
||||||
|
|
||||||
df = pd.DataFrame(data)
|
df = pd.DataFrame(data)
|
||||||
df["timestamp"] = pd.to_datetime(df["created_at"], utc=True).dt.tz_localize(None)
|
df["timestamp"] = pd.to_datetime(df["created_at"], utc=True).dt.tz_localize(None)
|
||||||
for col in ("insulin", "carbs"):
|
for col in ("insulin", "carbs", "absolute", "duration"):
|
||||||
if col not in df.columns:
|
if col not in df.columns:
|
||||||
df[col] = pd.NA
|
df[col] = pd.NA
|
||||||
df[col] = pd.to_numeric(df[col], errors="coerce")
|
df[col] = pd.to_numeric(df[col], errors="coerce")
|
||||||
if "eventType" not in df.columns:
|
for col in ("eventType", "type"):
|
||||||
df["eventType"] = ""
|
if col not in df.columns:
|
||||||
|
df[col] = ""
|
||||||
|
df[col] = df[col].fillna("")
|
||||||
df = df[df["timestamp"] >= since.replace(tzinfo=None)]
|
df = df[df["timestamp"] >= since.replace(tzinfo=None)]
|
||||||
if until is not None:
|
if until is not None:
|
||||||
df = df[df["timestamp"] <= until.replace(tzinfo=None)]
|
df = df[df["timestamp"] <= until.replace(tzinfo=None)]
|
||||||
return (
|
return df[_TREATMENT_COLUMNS].sort_values("timestamp").reset_index(drop=True)
|
||||||
df[["timestamp", "eventType", "insulin", "carbs"]]
|
|
||||||
.sort_values("timestamp")
|
|
||||||
.reset_index(drop=True)
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def fetch_entries(hours: int = 24) -> pd.DataFrame:
|
def fetch_entries(hours: int = 24) -> pd.DataFrame:
|
||||||
|
|||||||
+16
-2
@@ -4,12 +4,19 @@ import streamlit as st
|
|||||||
from auth import require_auth
|
from auth import require_auth
|
||||||
from nightscout_client import NightscoutConfigError
|
from nightscout_client import NightscoutConfigError
|
||||||
from reporting import (
|
from reporting import (
|
||||||
|
gmi_mmol_per_mol,
|
||||||
gmi_percent,
|
gmi_percent,
|
||||||
percent_cv,
|
percent_cv,
|
||||||
sensor_active_percent,
|
sensor_active_percent,
|
||||||
tir_breakdown_5band,
|
tir_breakdown_5band,
|
||||||
)
|
)
|
||||||
from ui_common import load_range_data, render_completeness_warning, select_period_days
|
from ui_common import (
|
||||||
|
build_tir_stacked_bar_figure,
|
||||||
|
get_colors,
|
||||||
|
load_range_data,
|
||||||
|
render_completeness_warning,
|
||||||
|
select_period_days,
|
||||||
|
)
|
||||||
|
|
||||||
st.set_page_config(page_title="Glucose dashboard — Samenvatting", page_icon="📊", layout="wide")
|
st.set_page_config(page_title="Glucose dashboard — Samenvatting", page_icon="📊", layout="wide")
|
||||||
require_auth()
|
require_auth()
|
||||||
@@ -37,6 +44,7 @@ active_pct = sensor_active_percent(glucose, start, end)
|
|||||||
|
|
||||||
mean_mmol = glucose["mmol"].mean()
|
mean_mmol = glucose["mmol"].mean()
|
||||||
gmi = gmi_percent(mean_mmol)
|
gmi = gmi_percent(mean_mmol)
|
||||||
|
gmi_mmol_mol = gmi_mmol_per_mol(gmi)
|
||||||
cv = percent_cv(glucose)
|
cv = percent_cv(glucose)
|
||||||
tir = tir_breakdown_5band(glucose)
|
tir = tir_breakdown_5band(glucose)
|
||||||
|
|
||||||
@@ -44,7 +52,11 @@ st.subheader("Kerncijfers")
|
|||||||
col1, col2, col3, col4 = st.columns(4)
|
col1, col2, col3, col4 = st.columns(4)
|
||||||
col1.metric("Sensordekking", f"{active_pct:.0f}%", help="Streefwaarde: ≥70%")
|
col1.metric("Sensordekking", f"{active_pct:.0f}%", help="Streefwaarde: ≥70%")
|
||||||
col2.metric("Gemiddelde glucose", f"{mean_mmol:.1f} mmol/L")
|
col2.metric("Gemiddelde glucose", f"{mean_mmol:.1f} mmol/L")
|
||||||
col3.metric("Geschat HbA1c (GMI)", f"{gmi:.1f}%", help="Bergenstal et al. 2018 — geen labwaarde")
|
col3.metric(
|
||||||
|
"Geschat HbA1c (GMI)",
|
||||||
|
f"{gmi:.1f}% ({gmi_mmol_mol:.0f} mmol/mol)",
|
||||||
|
help="Bergenstal et al. 2018 — geen labwaarde",
|
||||||
|
)
|
||||||
col4.metric("Variabiliteit (%CV)", f"{cv:.0f}%", help="Streefwaarde: ≤36%")
|
col4.metric("Variabiliteit (%CV)", f"{cv:.0f}%", help="Streefwaarde: ≤36%")
|
||||||
|
|
||||||
st.subheader("Tijd in bereik (5 klinische banden)")
|
st.subheader("Tijd in bereik (5 klinische banden)")
|
||||||
@@ -66,6 +78,8 @@ cols = st.columns(5)
|
|||||||
for col, band in zip(cols, band_labels, strict=True):
|
for col, band in zip(cols, band_labels, strict=True):
|
||||||
col.metric(band_labels[band], f"{tir[band]:.0f}%", help=f"Streefwaarde: {band_targets[band]}")
|
col.metric(band_labels[band], f"{tir[band]:.0f}%", help=f"Streefwaarde: {band_targets[band]}")
|
||||||
|
|
||||||
|
st.plotly_chart(build_tir_stacked_bar_figure(tir, get_colors()), width="stretch")
|
||||||
|
|
||||||
st.caption(
|
st.caption(
|
||||||
f"Periode: {start.strftime('%d-%m-%Y')} t/m {end.strftime('%d-%m-%Y')} "
|
f"Periode: {start.strftime('%d-%m-%Y')} t/m {end.strftime('%d-%m-%Y')} "
|
||||||
f"({len(glucose)} metingen) · streefwaarden volgens Battelino et al. 2019"
|
f"({len(glucose)} metingen) · streefwaarden volgens Battelino et al. 2019"
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ from data_processing import TARGET_HIGH, TARGET_LOW
|
|||||||
from nightscout_client import NightscoutConfigError
|
from nightscout_client import NightscoutConfigError
|
||||||
from reporting import agp_percentile_bands
|
from reporting import agp_percentile_bands
|
||||||
from ui_common import (
|
from ui_common import (
|
||||||
|
add_threshold_lines,
|
||||||
get_colors,
|
get_colors,
|
||||||
hex_to_rgba,
|
hex_to_rgba,
|
||||||
load_range_data,
|
load_range_data,
|
||||||
@@ -102,6 +103,7 @@ fig.update_layout(
|
|||||||
yaxis={"title": "mmol/L", "gridcolor": colors["grid"], "zeroline": False},
|
yaxis={"title": "mmol/L", "gridcolor": colors["grid"], "zeroline": False},
|
||||||
xaxis={"title": "Uur van de dag", "gridcolor": colors["grid"], "tickmode": "linear", "dtick": 2},
|
xaxis={"title": "Uur van de dag", "gridcolor": colors["grid"], "tickmode": "linear", "dtick": 2},
|
||||||
)
|
)
|
||||||
|
add_threshold_lines(fig, colors)
|
||||||
st.plotly_chart(fig, width="stretch")
|
st.plotly_chart(fig, width="stretch")
|
||||||
st.caption(
|
st.caption(
|
||||||
f"Periode: {start.strftime('%d-%m-%Y')} t/m {end.strftime('%d-%m-%Y')} ({len(glucose)} metingen)"
|
f"Periode: {start.strftime('%d-%m-%Y')} t/m {end.strftime('%d-%m-%Y')} ({len(glucose)} metingen)"
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ from auth import require_auth
|
|||||||
from data_processing import TARGET_HIGH, TARGET_LOW
|
from data_processing import TARGET_HIGH, TARGET_LOW
|
||||||
from nightscout_client import NightscoutConfigError
|
from nightscout_client import NightscoutConfigError
|
||||||
from ui_common import (
|
from ui_common import (
|
||||||
|
add_threshold_lines,
|
||||||
get_colors,
|
get_colors,
|
||||||
load_range_data,
|
load_range_data,
|
||||||
render_completeness_warning,
|
render_completeness_warning,
|
||||||
@@ -74,6 +75,7 @@ for i, day in enumerate(dates, start=1):
|
|||||||
col=1,
|
col=1,
|
||||||
)
|
)
|
||||||
fig.update_yaxes(title_text="mmol/L", gridcolor=colors["grid"], row=i, col=1)
|
fig.update_yaxes(title_text="mmol/L", gridcolor=colors["grid"], row=i, col=1)
|
||||||
|
add_threshold_lines(fig, colors, row=i)
|
||||||
|
|
||||||
fig.update_xaxes(title_text="Uur van de dag", range=[0, 24], dtick=4, gridcolor=colors["grid"])
|
fig.update_xaxes(title_text="Uur van de dag", range=[0, 24], dtick=4, gridcolor=colors["grid"])
|
||||||
fig.update_layout(
|
fig.update_layout(
|
||||||
|
|||||||
+73
-20
@@ -6,14 +6,16 @@ from auth import require_auth
|
|||||||
from data_processing import classify_treatments
|
from data_processing import classify_treatments
|
||||||
from nightscout_client import NightscoutConfigError
|
from nightscout_client import NightscoutConfigError
|
||||||
from reporting import (
|
from reporting import (
|
||||||
|
basal_stats,
|
||||||
bolus_stats,
|
bolus_stats,
|
||||||
day_of_week_pattern,
|
day_of_week_pattern,
|
||||||
gmi_percent,
|
gmi_trend,
|
||||||
overnight_daytime_split,
|
overnight_daytime_split,
|
||||||
site_interval_stats,
|
site_interval_stats,
|
||||||
)
|
)
|
||||||
from ui_common import (
|
from ui_common import (
|
||||||
get_colors,
|
get_colors,
|
||||||
|
hex_to_rgba,
|
||||||
load_range_data,
|
load_range_data,
|
||||||
render_completeness_warning,
|
render_completeness_warning,
|
||||||
select_period_days,
|
select_period_days,
|
||||||
@@ -45,26 +47,70 @@ render_completeness_warning(glucose, start, end)
|
|||||||
colors = get_colors()
|
colors = get_colors()
|
||||||
events = classify_treatments(treatments)
|
events = classify_treatments(treatments)
|
||||||
|
|
||||||
# --- FR7: geschat HbA1c (GMI) per maand ------------------------------------
|
# --- FR7: geschat HbA1c (GMI) trend ----------------------------------------
|
||||||
st.subheader("Geschat HbA1c (GMI) per maand")
|
st.subheader("Geschat HbA1c (GMI) trend")
|
||||||
monthly = glucose.copy()
|
trend = gmi_trend(glucose)
|
||||||
monthly["month"] = monthly["timestamp"].dt.to_period("M").astype(str)
|
|
||||||
monthly_gmi = monthly.groupby("month")["mmol"].mean().apply(gmi_percent)
|
|
||||||
|
|
||||||
if len(monthly_gmi) >= 1:
|
if len(trend) >= 2:
|
||||||
fig = go.Figure(
|
fig = go.Figure()
|
||||||
go.Bar(x=monthly_gmi.index, y=monthly_gmi.to_numpy(), marker_color=colors["glucose"])
|
fig.add_trace(
|
||||||
|
go.Scatter(
|
||||||
|
x=trend["date"], y=trend["trend_upper"], mode="lines", line={"width": 0}, showlegend=False,
|
||||||
|
hoverinfo="skip",
|
||||||
|
)
|
||||||
|
)
|
||||||
|
fig.add_trace(
|
||||||
|
go.Scatter(
|
||||||
|
x=trend["date"],
|
||||||
|
y=trend["trend_lower"],
|
||||||
|
mode="lines",
|
||||||
|
line={"width": 0},
|
||||||
|
fill="tonexty",
|
||||||
|
fillcolor=hex_to_rgba(colors["muted"], 0.2),
|
||||||
|
name="95% betrouwbaarheidsband",
|
||||||
|
hoverinfo="skip",
|
||||||
|
)
|
||||||
|
)
|
||||||
|
fig.add_trace(
|
||||||
|
go.Scatter(
|
||||||
|
x=trend["date"],
|
||||||
|
y=trend["trend"],
|
||||||
|
mode="lines",
|
||||||
|
line={"color": colors["muted"], "width": 2, "dash": "dash"},
|
||||||
|
name="Trend (lineair)",
|
||||||
|
)
|
||||||
|
)
|
||||||
|
fig.add_trace(
|
||||||
|
go.Scatter(
|
||||||
|
x=trend["date"],
|
||||||
|
y=trend["rolling_avg"],
|
||||||
|
mode="lines",
|
||||||
|
line={"color": colors["glucose"], "width": 2},
|
||||||
|
name="Voortschrijdend gemiddelde (7d)",
|
||||||
|
)
|
||||||
|
)
|
||||||
|
fig.add_trace(
|
||||||
|
go.Scatter(
|
||||||
|
x=trend["date"],
|
||||||
|
y=trend["gmi"],
|
||||||
|
mode="markers",
|
||||||
|
marker={"color": colors["glucose"], "size": 5, "opacity": 0.5},
|
||||||
|
name="Dag-GMI",
|
||||||
|
)
|
||||||
)
|
)
|
||||||
fig.update_layout(
|
fig.update_layout(
|
||||||
paper_bgcolor="rgba(0,0,0,0)",
|
paper_bgcolor="rgba(0,0,0,0)",
|
||||||
plot_bgcolor="rgba(0,0,0,0)",
|
plot_bgcolor="rgba(0,0,0,0)",
|
||||||
font_color=colors["text"],
|
font_color=colors["text"],
|
||||||
height=280,
|
height=320,
|
||||||
margin={"l": 10, "r": 10, "t": 10, "b": 10},
|
margin={"l": 10, "r": 10, "t": 10, "b": 10},
|
||||||
yaxis={"title": "GMI (%)", "gridcolor": colors["grid"]},
|
yaxis={"title": "GMI (%)", "gridcolor": colors["grid"]},
|
||||||
xaxis={"title": "Maand", "gridcolor": colors["grid"]},
|
xaxis={"title": "Datum", "gridcolor": colors["grid"]},
|
||||||
|
legend={"orientation": "h", "yanchor": "bottom", "y": 1.02, "xanchor": "left", "x": 0},
|
||||||
)
|
)
|
||||||
st.plotly_chart(fig, width="stretch")
|
st.plotly_chart(fig, width="stretch")
|
||||||
|
else:
|
||||||
|
st.info("Niet genoeg dagen met data voor een trendlijn (minstens 2 nodig).")
|
||||||
st.caption("GMI is een schatting (Bergenstal et al. 2018), geen labwaarde.")
|
st.caption("GMI is een schatting (Bergenstal et al. 2018), geen labwaarde.")
|
||||||
|
|
||||||
# --- FR8: nacht vs. overdag --------------------------------------------------
|
# --- FR8: nacht vs. overdag --------------------------------------------------
|
||||||
@@ -93,18 +139,25 @@ if not weekday_plot.empty:
|
|||||||
)
|
)
|
||||||
st.plotly_chart(fig, width="stretch")
|
st.plotly_chart(fig, width="stretch")
|
||||||
|
|
||||||
# --- FR8: bolus-insuline --------------------------------------------------
|
# --- FR8: insuline (basaal + bolus-uitsplitsing) ---------------------------
|
||||||
st.subheader("Bolus-insuline")
|
st.subheader("Insuline")
|
||||||
st.caption(
|
st.caption(
|
||||||
"Alleen bolusinsuline (maaltijd/correctie/combo/SMB) — basaalinsuline vereist "
|
"Basaal berekend uit Temp Basal-treatments (snelheid x duur), zonder pompprofiel "
|
||||||
"profielgegevens en is hier niet in verwerkt (zie docs/design-reporting.md)."
|
"nodig — een ondergrens bij periodes zonder actieve temp basal. Boluses "
|
||||||
|
"opgesplitst naar voeding, handmatige correctie en automatische SMB-microbolus "
|
||||||
|
"(gitea #20)."
|
||||||
)
|
)
|
||||||
bolus = bolus_stats(treatments)
|
bolus = bolus_stats(treatments)
|
||||||
col1, col2 = st.columns(2)
|
basal = basal_stats(treatments, days)
|
||||||
col1.metric("Totaal bolus", f"{bolus['total_units']:.1f} E")
|
total_units = bolus["total_units"] + basal["total_units"]
|
||||||
col2.metric("Aantal boluses", bolus["count"])
|
col1, col2, col3 = st.columns(3)
|
||||||
if bolus["by_type"]:
|
col1.metric("Totaal insuline", f"{total_units:.1f} E")
|
||||||
st.table({"type": list(bolus["by_type"].keys()), "eenheden": list(bolus["by_type"].values())})
|
col2.metric("Gem. per dag", f"{total_units / days:.1f} E")
|
||||||
|
col3.metric("Aantal boluses", bolus["count"])
|
||||||
|
|
||||||
|
breakdown = {"Basaal": basal["total_units"], **bolus["by_type"]}
|
||||||
|
if any(breakdown.values()):
|
||||||
|
st.table({"Categorie": list(breakdown.keys()), "Eenheden": list(breakdown.values())})
|
||||||
|
|
||||||
# --- FR8: pompsite-leeftijd vs. glucose/insuline ---------------------------
|
# --- FR8: pompsite-leeftijd vs. glucose/insuline ---------------------------
|
||||||
st.subheader("Pompsite-leeftijd vs. glucose en insuline")
|
st.subheader("Pompsite-leeftijd vs. glucose en insuline")
|
||||||
|
|||||||
+148
-4
@@ -21,8 +21,14 @@ from fpdf import FPDF
|
|||||||
from fpdf.enums import XPos, YPos
|
from fpdf.enums import XPos, YPos
|
||||||
|
|
||||||
from reporting import (
|
from reporting import (
|
||||||
|
HIGH,
|
||||||
|
LOW,
|
||||||
|
VERY_HIGH,
|
||||||
|
VERY_LOW,
|
||||||
agp_percentile_bands,
|
agp_percentile_bands,
|
||||||
completeness_warning,
|
completeness_warning,
|
||||||
|
detect_events,
|
||||||
|
gmi_mmol_per_mol,
|
||||||
gmi_percent,
|
gmi_percent,
|
||||||
percent_cv,
|
percent_cv,
|
||||||
sensor_active_percent,
|
sensor_active_percent,
|
||||||
@@ -31,12 +37,46 @@ from reporting import (
|
|||||||
|
|
||||||
SECTION_SUMMARY = "Samenvatting"
|
SECTION_SUMMARY = "Samenvatting"
|
||||||
SECTION_AGP = "AGP profiel"
|
SECTION_AGP = "AGP profiel"
|
||||||
|
SECTION_DAGOVERZICHT = "Dagoverzicht"
|
||||||
SECTION_EVENTS = "Hypo-/hyperevents"
|
SECTION_EVENTS = "Hypo-/hyperevents"
|
||||||
|
|
||||||
AVAILABLE_SECTIONS = [SECTION_SUMMARY, SECTION_AGP, SECTION_EVENTS]
|
AVAILABLE_SECTIONS = [SECTION_SUMMARY, SECTION_AGP, SECTION_DAGOVERZICHT, SECTION_EVENTS]
|
||||||
|
|
||||||
_LINE_KWARGS = {"new_x": XPos.LMARGIN, "new_y": YPos.NEXT}
|
_LINE_KWARGS = {"new_x": XPos.LMARGIN, "new_y": YPos.NEXT}
|
||||||
|
|
||||||
|
# Zelfde kleuren als het PALETTE["light"] in ui_common.py — bewust hier
|
||||||
|
# gedupliceerd (5 hex-waarden) in plaats van ui_common.py te importeren, want
|
||||||
|
# dat zou een streamlit-dependency in deze module trekken die verder bewust
|
||||||
|
# vrij van UI-framework-imports is (los te unit-testen, zie moduledocstring).
|
||||||
|
_MUTED = "#898781"
|
||||||
|
_TIR_BAND_ORDER = ["very_low", "low", "in_range", "high", "very_high"]
|
||||||
|
_TIR_BAND_LABELS = {
|
||||||
|
"very_low": "Zeer laag",
|
||||||
|
"low": "Laag",
|
||||||
|
"in_range": "In bereik",
|
||||||
|
"high": "Hoog",
|
||||||
|
"very_high": "Zeer hoog",
|
||||||
|
}
|
||||||
|
_TIR_COLORS = {
|
||||||
|
"very_low": "#8b1a1a",
|
||||||
|
"low": "#d03b3b",
|
||||||
|
"in_range": "#0ca30c",
|
||||||
|
"high": "#fab219",
|
||||||
|
"very_high": "#c76f0a",
|
||||||
|
}
|
||||||
|
_THRESHOLD_LINES = [VERY_LOW, LOW, HIGH, VERY_HIGH]
|
||||||
|
DAGOVERZICHT_MAX_DAYS = 14
|
||||||
|
|
||||||
|
|
||||||
|
def _add_threshold_lines(ax) -> None:
|
||||||
|
"""Teken de 4 klinische drempellijnen (3.0/3.9/10.0/13.9 mmol/L) op een matplotlib-as.
|
||||||
|
|
||||||
|
PDF-tegenhanger van ui_common.add_threshold_lines() voor de Plotly-versie
|
||||||
|
op de dashboardpagina's (gitea #15) — zelfde 4 waarden, zelfde grijstint.
|
||||||
|
"""
|
||||||
|
for value in _THRESHOLD_LINES:
|
||||||
|
ax.axhline(value, color=_MUTED, linewidth=0.7, linestyle=":")
|
||||||
|
|
||||||
|
|
||||||
def _agp_figure_png(glucose: pd.DataFrame) -> bytes:
|
def _agp_figure_png(glucose: pd.DataFrame) -> bytes:
|
||||||
bands = agp_percentile_bands(glucose)
|
bands = agp_percentile_bands(glucose)
|
||||||
@@ -45,6 +85,7 @@ def _agp_figure_png(glucose: pd.DataFrame) -> bytes:
|
|||||||
ax.fill_between(hours, bands["p5"], bands["p95"], alpha=0.15, color="#2a78d6", label="5e-95e pct")
|
ax.fill_between(hours, bands["p5"], bands["p95"], alpha=0.15, color="#2a78d6", label="5e-95e pct")
|
||||||
ax.fill_between(hours, bands["p25"], bands["p75"], alpha=0.3, color="#2a78d6", label="25e-75e pct")
|
ax.fill_between(hours, bands["p25"], bands["p75"], alpha=0.3, color="#2a78d6", label="25e-75e pct")
|
||||||
ax.plot(hours, bands["p50"], color="#2a78d6", linewidth=2, label="Mediaan")
|
ax.plot(hours, bands["p50"], color="#2a78d6", linewidth=2, label="Mediaan")
|
||||||
|
_add_threshold_lines(ax)
|
||||||
ax.set_xlabel("Uur van de dag")
|
ax.set_xlabel("Uur van de dag")
|
||||||
ax.set_ylabel("mmol/L")
|
ax.set_ylabel("mmol/L")
|
||||||
ax.set_xlim(0, 24)
|
ax.set_xlim(0, 24)
|
||||||
@@ -58,6 +99,69 @@ def _agp_figure_png(glucose: pd.DataFrame) -> bytes:
|
|||||||
return buf.getvalue()
|
return buf.getvalue()
|
||||||
|
|
||||||
|
|
||||||
|
def _tir_bar_figure_png(tir: dict) -> bytes:
|
||||||
|
"""Gestapelde staafgrafiek van de 5 TIR-banden — PDF-tegenhanger van
|
||||||
|
ui_common.build_tir_stacked_bar_figure() (gitea #14)."""
|
||||||
|
fig, ax = plt.subplots(figsize=(7, 1.1))
|
||||||
|
left = 0.0
|
||||||
|
for band in _TIR_BAND_ORDER:
|
||||||
|
value = tir[band]
|
||||||
|
ax.barh(0, value, left=left, color=_TIR_COLORS[band], label=_TIR_BAND_LABELS[band])
|
||||||
|
left += value
|
||||||
|
ax.set_xlim(0, 100)
|
||||||
|
ax.set_yticks([])
|
||||||
|
ax.set_xlabel("%")
|
||||||
|
ax.legend(loc="upper center", bbox_to_anchor=(0.5, -0.75), ncol=5, fontsize=7, frameon=False)
|
||||||
|
|
||||||
|
buf = io.BytesIO()
|
||||||
|
# bbox_inches="tight" i.p.v. fig.tight_layout(): de legend zit buiten de
|
||||||
|
# axes (bbox_to_anchor met negatieve y), waar tight_layout() niet voor
|
||||||
|
# kan compenseren — savefig's eigen bbox-berekening kan dat wel.
|
||||||
|
fig.savefig(buf, format="png", dpi=150, bbox_inches="tight")
|
||||||
|
plt.close(fig)
|
||||||
|
buf.seek(0)
|
||||||
|
return buf.getvalue()
|
||||||
|
|
||||||
|
|
||||||
|
def _daily_figure_png(day_df: pd.DataFrame, day_label: str) -> bytes:
|
||||||
|
"""Eén dag als kleine lijngrafiek — PDF-tegenhanger van de Dagoverzicht-pagina (gitea #16)."""
|
||||||
|
hours = day_df["timestamp"].dt.hour + day_df["timestamp"].dt.minute / 60
|
||||||
|
fig, ax = plt.subplots(figsize=(7, 1.4))
|
||||||
|
ax.plot(hours, day_df["mmol"], color="#2a78d6", linewidth=1.2)
|
||||||
|
_add_threshold_lines(ax)
|
||||||
|
ax.set_xlim(0, 24)
|
||||||
|
ax.set_xticks(range(0, 25, 4))
|
||||||
|
ax.set_ylabel("mmol/L", fontsize=8)
|
||||||
|
ax.set_title(day_label, fontsize=9, loc="left")
|
||||||
|
ax.tick_params(labelsize=7)
|
||||||
|
fig.tight_layout()
|
||||||
|
|
||||||
|
buf = io.BytesIO()
|
||||||
|
fig.savefig(buf, format="png", dpi=150)
|
||||||
|
plt.close(fig)
|
||||||
|
buf.seek(0)
|
||||||
|
return buf.getvalue()
|
||||||
|
|
||||||
|
|
||||||
|
def _event_hour_histogram_png(events: pd.DataFrame, color: str) -> bytes:
|
||||||
|
"""Aantal events per uur-van-de-dag — PDF-tegenhanger van render_event_level()'s
|
||||||
|
histogram op de Hypo/Hyper Events-pagina's (gitea #17, #18)."""
|
||||||
|
hour_counts = events["start"].dt.hour.value_counts().reindex(range(24), fill_value=0).sort_index()
|
||||||
|
fig, ax = plt.subplots(figsize=(7, 2))
|
||||||
|
ax.bar(hour_counts.index, hour_counts.to_numpy(), color=color)
|
||||||
|
ax.set_xlabel("Uur van de dag")
|
||||||
|
ax.set_ylabel("Aantal events")
|
||||||
|
ax.set_xlim(-0.5, 23.5)
|
||||||
|
ax.set_xticks(range(0, 24, 2))
|
||||||
|
fig.tight_layout()
|
||||||
|
|
||||||
|
buf = io.BytesIO()
|
||||||
|
fig.savefig(buf, format="png", dpi=150)
|
||||||
|
plt.close(fig)
|
||||||
|
buf.seek(0)
|
||||||
|
return buf.getvalue()
|
||||||
|
|
||||||
|
|
||||||
def _add_summary_section(pdf: FPDF, glucose: pd.DataFrame, start, end) -> None:
|
def _add_summary_section(pdf: FPDF, glucose: pd.DataFrame, start, end) -> None:
|
||||||
mean_mmol = glucose["mmol"].mean()
|
mean_mmol = glucose["mmol"].mean()
|
||||||
tir = tir_breakdown_5band(glucose)
|
tir = tir_breakdown_5band(glucose)
|
||||||
@@ -75,7 +179,8 @@ def _add_summary_section(pdf: FPDF, glucose: pd.DataFrame, start, end) -> None:
|
|||||||
pdf.set_font("Helvetica", "", 11)
|
pdf.set_font("Helvetica", "", 11)
|
||||||
pdf.cell(0, 6, f"Sensordekking: {active_pct:.0f}%", **_LINE_KWARGS)
|
pdf.cell(0, 6, f"Sensordekking: {active_pct:.0f}%", **_LINE_KWARGS)
|
||||||
pdf.cell(0, 6, f"Gemiddelde glucose: {mean_mmol:.1f} mmol/L", **_LINE_KWARGS)
|
pdf.cell(0, 6, f"Gemiddelde glucose: {mean_mmol:.1f} mmol/L", **_LINE_KWARGS)
|
||||||
pdf.cell(0, 6, f"Geschat HbA1c (GMI): {gmi_percent(mean_mmol):.1f}%", **_LINE_KWARGS)
|
gmi = gmi_percent(mean_mmol)
|
||||||
|
pdf.cell(0, 6, f"Geschat HbA1c (GMI): {gmi:.1f}% ({gmi_mmol_per_mol(gmi):.0f} mmol/mol)", **_LINE_KWARGS)
|
||||||
pdf.cell(0, 6, f"Variabiliteit (%CV): {percent_cv(glucose):.0f}%", **_LINE_KWARGS)
|
pdf.cell(0, 6, f"Variabiliteit (%CV): {percent_cv(glucose):.0f}%", **_LINE_KWARGS)
|
||||||
pdf.ln(2)
|
pdf.ln(2)
|
||||||
pdf.cell(0, 6, f"Zeer laag (<3.0 mmol/L): {tir['very_low']:.0f}%", **_LINE_KWARGS)
|
pdf.cell(0, 6, f"Zeer laag (<3.0 mmol/L): {tir['very_low']:.0f}%", **_LINE_KWARGS)
|
||||||
@@ -83,6 +188,9 @@ def _add_summary_section(pdf: FPDF, glucose: pd.DataFrame, start, end) -> None:
|
|||||||
pdf.cell(0, 6, f"In bereik (3.9-10.0 mmol/L): {tir['in_range']:.0f}%", **_LINE_KWARGS)
|
pdf.cell(0, 6, f"In bereik (3.9-10.0 mmol/L): {tir['in_range']:.0f}%", **_LINE_KWARGS)
|
||||||
pdf.cell(0, 6, f"Hoog (10.0-13.9 mmol/L): {tir['high']:.0f}%", **_LINE_KWARGS)
|
pdf.cell(0, 6, f"Hoog (10.0-13.9 mmol/L): {tir['high']:.0f}%", **_LINE_KWARGS)
|
||||||
pdf.cell(0, 6, f"Zeer hoog (>13.9 mmol/L): {tir['very_high']:.0f}%", **_LINE_KWARGS)
|
pdf.cell(0, 6, f"Zeer hoog (>13.9 mmol/L): {tir['very_high']:.0f}%", **_LINE_KWARGS)
|
||||||
|
pdf.ln(2)
|
||||||
|
png_bytes = _tir_bar_figure_png(tir)
|
||||||
|
pdf.image(io.BytesIO(png_bytes), w=160)
|
||||||
pdf.ln(4)
|
pdf.ln(4)
|
||||||
|
|
||||||
|
|
||||||
@@ -94,7 +202,29 @@ def _add_agp_section(pdf: FPDF, glucose: pd.DataFrame) -> None:
|
|||||||
pdf.ln(4)
|
pdf.ln(4)
|
||||||
|
|
||||||
|
|
||||||
def _add_events_section(pdf: FPDF, events_summary: dict[str, dict]) -> None:
|
def _add_dagoverzicht_section(pdf: FPDF, glucose: pd.DataFrame) -> None:
|
||||||
|
"""Eén dagelijkse mini-grafiek per kalenderdag (gitea #16), max. de meest
|
||||||
|
recente DAGOVERZICHT_MAX_DAYS dagen — voor langere periodes zou de PDF
|
||||||
|
anders onwerkbaar lang worden."""
|
||||||
|
pdf.set_font("Helvetica", "B", 13)
|
||||||
|
pdf.cell(0, 8, "Dagoverzicht", **_LINE_KWARGS)
|
||||||
|
|
||||||
|
df = glucose.copy()
|
||||||
|
df["date"] = df["timestamp"].dt.date
|
||||||
|
dates = sorted(df["date"].unique())
|
||||||
|
if len(dates) > DAGOVERZICHT_MAX_DAYS:
|
||||||
|
dates = dates[-DAGOVERZICHT_MAX_DAYS:]
|
||||||
|
pdf.set_font("Helvetica", "I", 9)
|
||||||
|
pdf.cell(0, 5, f"Beperkt tot de meest recente {DAGOVERZICHT_MAX_DAYS} dagen.", **_LINE_KWARGS)
|
||||||
|
|
||||||
|
for day in dates:
|
||||||
|
day_df = df[df["date"] == day]
|
||||||
|
png_bytes = _daily_figure_png(day_df, day.strftime("%A %d-%m-%Y"))
|
||||||
|
pdf.image(io.BytesIO(png_bytes), w=180)
|
||||||
|
pdf.ln(4)
|
||||||
|
|
||||||
|
|
||||||
|
def _add_events_section(pdf: FPDF, glucose: pd.DataFrame, events_summary: dict[str, dict]) -> None:
|
||||||
pdf.set_font("Helvetica", "B", 13)
|
pdf.set_font("Helvetica", "B", 13)
|
||||||
pdf.cell(0, 8, "Hypo-/hyperevents", **_LINE_KWARGS)
|
pdf.cell(0, 8, "Hypo-/hyperevents", **_LINE_KWARGS)
|
||||||
pdf.set_font("Helvetica", "", 11)
|
pdf.set_font("Helvetica", "", 11)
|
||||||
@@ -106,6 +236,18 @@ def _add_events_section(pdf: FPDF, events_summary: dict[str, dict]) -> None:
|
|||||||
f"{summary['events_per_week']:.1f}/week",
|
f"{summary['events_per_week']:.1f}/week",
|
||||||
**_LINE_KWARGS,
|
**_LINE_KWARGS,
|
||||||
)
|
)
|
||||||
|
pdf.ln(2)
|
||||||
|
|
||||||
|
# Alleen Level 1 krijgt een grafiek in het PDF-rapport (gitea #17, #18) —
|
||||||
|
# geen tabel met losse events, de samenvattende cijfers staan al hierboven.
|
||||||
|
if not glucose.empty:
|
||||||
|
hypo_events = detect_events(glucose, LOW, "below")
|
||||||
|
if not hypo_events.empty:
|
||||||
|
pdf.image(io.BytesIO(_event_hour_histogram_png(hypo_events, _TIR_COLORS["low"])), w=180)
|
||||||
|
hyper_events = detect_events(glucose, HIGH, "above")
|
||||||
|
if not hyper_events.empty:
|
||||||
|
pdf.image(io.BytesIO(_event_hour_histogram_png(hyper_events, _TIR_COLORS["high"])), w=180)
|
||||||
|
|
||||||
pdf.ln(4)
|
pdf.ln(4)
|
||||||
|
|
||||||
|
|
||||||
@@ -135,8 +277,10 @@ def build_report_pdf(
|
|||||||
_add_summary_section(pdf, glucose, start, end)
|
_add_summary_section(pdf, glucose, start, end)
|
||||||
if SECTION_AGP in sections:
|
if SECTION_AGP in sections:
|
||||||
_add_agp_section(pdf, glucose)
|
_add_agp_section(pdf, glucose)
|
||||||
|
if SECTION_DAGOVERZICHT in sections:
|
||||||
|
_add_dagoverzicht_section(pdf, glucose)
|
||||||
|
|
||||||
if SECTION_EVENTS in sections and events_summary:
|
if SECTION_EVENTS in sections and events_summary:
|
||||||
_add_events_section(pdf, events_summary)
|
_add_events_section(pdf, glucose, events_summary)
|
||||||
|
|
||||||
return bytes(pdf.output())
|
return bytes(pdf.output())
|
||||||
|
|||||||
+122
-6
@@ -11,6 +11,7 @@ from __future__ import annotations
|
|||||||
|
|
||||||
from itertools import pairwise
|
from itertools import pairwise
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
|
|
||||||
from nightscout_client import INSULIN_EVENT_TYPES
|
from nightscout_client import INSULIN_EVENT_TYPES
|
||||||
@@ -35,6 +36,79 @@ def gmi_percent(mean_mmol: float) -> float:
|
|||||||
return round(3.31 + 0.4311 * mean_mmol, 1)
|
return round(3.31 + 0.4311 * mean_mmol, 1)
|
||||||
|
|
||||||
|
|
||||||
|
def gmi_mmol_per_mol(gmi_pct: float) -> int:
|
||||||
|
"""Zet een GMI-percentage (DCCT/NGSP) om naar IFCC mmol/mol.
|
||||||
|
|
||||||
|
mmol/mol = 10.929 x (% - 2.15) — de standaard IFCC-omrekening
|
||||||
|
(Hoelzel et al. 2004, Clin Chem 50(1):166-174), gangbare eenheid in
|
||||||
|
Nederlandse labuitslagen naast het %-getal (gitea #25). Bijv. 6.5% =
|
||||||
|
48 mmol/mol, de bekende diagnostische drempel.
|
||||||
|
"""
|
||||||
|
return round(10.929 * (gmi_pct - 2.15))
|
||||||
|
|
||||||
|
|
||||||
|
GMI_TREND_COLUMNS = ["date", "gmi", "rolling_avg", "trend", "trend_lower", "trend_upper"]
|
||||||
|
|
||||||
|
|
||||||
|
def daily_gmi(glucose: pd.DataFrame) -> pd.DataFrame:
|
||||||
|
"""Geschat HbA1c (GMI) per kalenderdag — scatterpunten voor de trendgrafiek (gitea #19)."""
|
||||||
|
if glucose.empty:
|
||||||
|
return pd.DataFrame(columns=["date", "gmi"])
|
||||||
|
|
||||||
|
df = glucose.copy()
|
||||||
|
df["date"] = df["timestamp"].dt.date
|
||||||
|
daily_mean = df.groupby("date")["mmol"].mean()
|
||||||
|
result = daily_mean.apply(gmi_percent).reset_index()
|
||||||
|
result.columns = ["date", "gmi"]
|
||||||
|
return result.sort_values("date").reset_index(drop=True)
|
||||||
|
|
||||||
|
|
||||||
|
def gmi_trend(glucose: pd.DataFrame, rolling_window_days: int = 7) -> pd.DataFrame:
|
||||||
|
"""Dagelijkse GMI-punten plus voortschrijdend gemiddelde en een lineaire
|
||||||
|
trendlijn met 95%-betrouwbaarheidsband (gitea #19).
|
||||||
|
|
||||||
|
Gebruikt een normaalbenadering (z=1.96) voor de betrouwbaarheidsband i.p.v.
|
||||||
|
een exacte t-verdeling — dat zou een scipy-dependency toevoegen aan een
|
||||||
|
verder bewust lichte container (dezelfde afweging als het niet gebruiken
|
||||||
|
van kaleido in pdf_export.py). Bij de hier gebruikelijke steekproefgroottes
|
||||||
|
(14-90 dagpunten) is het verschil met de exacte t-kritieke waarde
|
||||||
|
verwaarloosbaar voor een visuele trendindicatie.
|
||||||
|
|
||||||
|
Elke keer opnieuw berekend uit de al-opgehaalde periode-data (geen aparte
|
||||||
|
database) — de bestaande 1-uurs Streamlit-cache op load_range_data()
|
||||||
|
voorkomt al herhaalde Nightscout-calls voor dezelfde periode. Bij een
|
||||||
|
veel langere trend (jaren i.p.v. de huidige max. 90 dagen) zou een aparte
|
||||||
|
opslag wél de moeite waard worden.
|
||||||
|
"""
|
||||||
|
daily = daily_gmi(glucose)
|
||||||
|
if len(daily) < 2:
|
||||||
|
return daily.reindex(columns=GMI_TREND_COLUMNS)
|
||||||
|
|
||||||
|
daily = daily.copy()
|
||||||
|
daily["rolling_avg"] = daily["gmi"].rolling(window=rolling_window_days, min_periods=1).mean()
|
||||||
|
|
||||||
|
x = np.arange(len(daily), dtype=float)
|
||||||
|
y = daily["gmi"].to_numpy()
|
||||||
|
slope, intercept = np.polyfit(x, y, 1)
|
||||||
|
fitted = intercept + slope * x
|
||||||
|
|
||||||
|
residuals = y - fitted
|
||||||
|
degrees_of_freedom = max(len(x) - 2, 1)
|
||||||
|
residual_std = np.sqrt(np.sum(residuals**2) / degrees_of_freedom)
|
||||||
|
x_mean = x.mean()
|
||||||
|
sum_sq_x = np.sum((x - x_mean) ** 2)
|
||||||
|
standard_error = (
|
||||||
|
np.zeros_like(x)
|
||||||
|
if sum_sq_x == 0
|
||||||
|
else residual_std * np.sqrt(1 / len(x) + (x - x_mean) ** 2 / sum_sq_x)
|
||||||
|
)
|
||||||
|
z_95 = 1.96
|
||||||
|
daily["trend"] = fitted
|
||||||
|
daily["trend_lower"] = fitted - z_95 * standard_error
|
||||||
|
daily["trend_upper"] = fitted + z_95 * standard_error
|
||||||
|
return daily[GMI_TREND_COLUMNS]
|
||||||
|
|
||||||
|
|
||||||
def percent_cv(glucose: pd.DataFrame) -> float:
|
def percent_cv(glucose: pd.DataFrame) -> float:
|
||||||
"""Glucosevariabiliteit als %CV (standaarddeviatie / gemiddelde * 100)."""
|
"""Glucosevariabiliteit als %CV (standaarddeviatie / gemiddelde * 100)."""
|
||||||
if glucose.empty:
|
if glucose.empty:
|
||||||
@@ -330,22 +404,35 @@ def day_of_week_pattern(glucose: pd.DataFrame) -> pd.DataFrame:
|
|||||||
return pd.DataFrame(rows, columns=columns)
|
return pd.DataFrame(rows, columns=columns)
|
||||||
|
|
||||||
|
|
||||||
|
SMB_TYPE = "SMB"
|
||||||
|
BASAL_EVENT_TYPE = "Temp Basal"
|
||||||
|
|
||||||
|
|
||||||
def bolus_stats(treatments: pd.DataFrame) -> dict:
|
def bolus_stats(treatments: pd.DataFrame) -> dict:
|
||||||
"""Samenvatting van bolus-insuline, opgesplitst naar type.
|
"""Samenvatting van bolus-insuline, opgesplitst naar type.
|
||||||
|
|
||||||
Let op: dit betreft alleen bolusinsuline (maaltijd/correctie/combo/SMB).
|
AndroidAPS in SMB-looping registreert een automatische micro-bolus niet
|
||||||
Basaalinsuline wordt hier niet in meegenomen — dat vereist profielgegevens
|
als eigen `eventType`, maar als `eventType == "Correction Bolus"` met
|
||||||
(zie docs/design-reporting.md, scoping-kanttekening) en levert dus geen
|
`type == "SMB"` — zonder dit onderscheid werden alle SMB's stilzwijgend
|
||||||
volledige totale-dagdosis op.
|
als "Correction Bolus" (handmatige correctie) meegeteld, wat gitea #20
|
||||||
|
meldde als "bolus stats empty/onduidelijk". Geverifieerd rechtstreeks
|
||||||
|
tegen de productiedata: bij deze gebruiker is vrijwel elke "Correction
|
||||||
|
Bolus" in werkelijkheid een automatische SMB.
|
||||||
|
|
||||||
|
Let op: dit betreft alleen bolusinsuline (voeding/correctie/combo/SMB).
|
||||||
|
Basaalinsuline zit hier niet in — zie basal_stats().
|
||||||
"""
|
"""
|
||||||
if treatments.empty:
|
if treatments.empty:
|
||||||
return {"total_units": 0.0, "count": 0, "by_type": {}}
|
return {"total_units": 0.0, "count": 0, "by_type": {}}
|
||||||
|
|
||||||
bolus = treatments[treatments["eventType"].isin(INSULIN_EVENT_TYPES) & treatments["insulin"].notna()]
|
bolus = treatments[treatments["eventType"].isin(INSULIN_EVENT_TYPES) & treatments["insulin"].notna()].copy()
|
||||||
if bolus.empty:
|
if bolus.empty:
|
||||||
return {"total_units": 0.0, "count": 0, "by_type": {}}
|
return {"total_units": 0.0, "count": 0, "by_type": {}}
|
||||||
|
|
||||||
by_type = bolus.groupby("eventType")["insulin"].sum().round(1).to_dict()
|
is_smb = (bolus["eventType"] == "Correction Bolus") & (bolus.get("type", "") == SMB_TYPE)
|
||||||
|
bolus["category"] = bolus["eventType"].where(~is_smb, SMB_TYPE)
|
||||||
|
|
||||||
|
by_type = bolus.groupby("category")["insulin"].sum().round(1).to_dict()
|
||||||
return {
|
return {
|
||||||
"total_units": round(bolus["insulin"].sum(), 1),
|
"total_units": round(bolus["insulin"].sum(), 1),
|
||||||
"count": len(bolus),
|
"count": len(bolus),
|
||||||
@@ -353,6 +440,35 @@ def bolus_stats(treatments: pd.DataFrame) -> dict:
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def basal_stats(treatments: pd.DataFrame, period_days: float) -> dict:
|
||||||
|
"""Totale basaalinsuline uit Temp Basal-treatments (rate x duur), zonder pompprofiel.
|
||||||
|
|
||||||
|
AndroidAPS zet in gesloten-lus-modus vrijwel continu Temp Basal-treatments
|
||||||
|
met een `absolute` snelheid (E/uur) en `duration` (minuten); de som van
|
||||||
|
afgeleverde eenheden daaruit benadert de daadwerkelijke basaaldosis
|
||||||
|
nauwkeurig genoeg zonder het pompprofiel te hoeven ophalen (gitea #20).
|
||||||
|
Periodes zonder actieve temp basal (dan geldt het profiel-basaaltempo)
|
||||||
|
worden hierin gemist — dit is dus een ondergrens, geen exacte totale-
|
||||||
|
dagdosis.
|
||||||
|
"""
|
||||||
|
if treatments.empty or period_days <= 0:
|
||||||
|
return {"total_units": 0.0, "avg_units_per_day": 0.0}
|
||||||
|
|
||||||
|
temp_basal = treatments[
|
||||||
|
(treatments["eventType"] == BASAL_EVENT_TYPE)
|
||||||
|
& treatments["absolute"].notna()
|
||||||
|
& treatments["duration"].notna()
|
||||||
|
]
|
||||||
|
if temp_basal.empty:
|
||||||
|
return {"total_units": 0.0, "avg_units_per_day": 0.0}
|
||||||
|
|
||||||
|
total_units = (temp_basal["absolute"] * temp_basal["duration"] / 60).sum()
|
||||||
|
return {
|
||||||
|
"total_units": round(total_units, 1),
|
||||||
|
"avg_units_per_day": round(total_units / period_days, 1),
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
SITE_INTERVAL_COLUMNS = [
|
SITE_INTERVAL_COLUMNS = [
|
||||||
"start",
|
"start",
|
||||||
"end",
|
"end",
|
||||||
|
|||||||
@@ -90,6 +90,38 @@ def test_fetch_treatments_fills_missing_columns(monkeypatch):
|
|||||||
assert result.iloc[0]["eventType"] == "Site Change"
|
assert result.iloc[0]["eventType"] == "Site Change"
|
||||||
assert pd_isna(result.iloc[0]["insulin"])
|
assert pd_isna(result.iloc[0]["insulin"])
|
||||||
assert pd_isna(result.iloc[0]["carbs"])
|
assert pd_isna(result.iloc[0]["carbs"])
|
||||||
|
assert result.iloc[0]["type"] == ""
|
||||||
|
assert pd_isna(result.iloc[0]["absolute"])
|
||||||
|
assert pd_isna(result.iloc[0]["duration"])
|
||||||
|
|
||||||
|
|
||||||
|
def test_fetch_treatments_passes_through_type_and_temp_basal_fields(monkeypatch):
|
||||||
|
"""SMB-classificatie (reporting.bolus_stats) en basaalberekening
|
||||||
|
(reporting.basal_stats) hebben deze velden nodig — zie gitea #20."""
|
||||||
|
monkeypatch.setenv("NIGHTSCOUT_URL", "https://example.com")
|
||||||
|
now = datetime.now(timezone.utc)
|
||||||
|
payload = [
|
||||||
|
{
|
||||||
|
"created_at": now.isoformat(),
|
||||||
|
"eventType": "Correction Bolus",
|
||||||
|
"insulin": 0.5,
|
||||||
|
"type": "SMB",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"created_at": now.isoformat(),
|
||||||
|
"eventType": "Temp Basal",
|
||||||
|
"absolute": 1.2,
|
||||||
|
"duration": 30,
|
||||||
|
},
|
||||||
|
]
|
||||||
|
monkeypatch.setattr(ns.requests, "get", lambda *a, **k: FakeResponse(payload))
|
||||||
|
|
||||||
|
result = ns.fetch_treatments(hours=24)
|
||||||
|
smb_row = result[result["eventType"] == "Correction Bolus"].iloc[0]
|
||||||
|
basal_row = result[result["eventType"] == "Temp Basal"].iloc[0]
|
||||||
|
assert smb_row["type"] == "SMB"
|
||||||
|
assert basal_row["absolute"] == pytest.approx(1.2)
|
||||||
|
assert basal_row["duration"] == pytest.approx(30)
|
||||||
|
|
||||||
|
|
||||||
def pd_isna(value):
|
def pd_isna(value):
|
||||||
@@ -159,7 +191,9 @@ def test_paginated_get_stops_when_a_short_page_is_returned(monkeypatch):
|
|||||||
lambda *a, **k: (calls.append(k), FakeResponse(payload))[1],
|
lambda *a, **k: (calls.append(k), FakeResponse(payload))[1],
|
||||||
)
|
)
|
||||||
|
|
||||||
result = ns._paginated_get("/api/v1/entries/sgv.json", now - timedelta(days=1))
|
result = ns._paginated_get(
|
||||||
|
"/api/v1/entries/sgv.json", now - timedelta(days=1), None, ns.ENTRIES_CURSOR_FIELD
|
||||||
|
)
|
||||||
assert len(result) == 5
|
assert len(result) == 5
|
||||||
assert len(calls) == 1 # één pagina was genoeg, geen verdere paginering nodig
|
assert len(calls) == 1 # één pagina was genoeg, geen verdere paginering nodig
|
||||||
|
|
||||||
@@ -192,7 +226,9 @@ def test_paginated_get_walks_back_multiple_pages_until_since_is_reached(monkeypa
|
|||||||
monkeypatch.setattr(ns.requests, "get", fake_get)
|
monkeypatch.setattr(ns.requests, "get", fake_get)
|
||||||
|
|
||||||
since = now - timedelta(days=365) # ruim voorbij alle test-pagina's
|
since = now - timedelta(days=365) # ruim voorbij alle test-pagina's
|
||||||
result = ns._paginated_get("/api/v1/entries/sgv.json", since)
|
result = ns._paginated_get(
|
||||||
|
"/api/v1/entries/sgv.json", since, None, ns.ENTRIES_CURSOR_FIELD
|
||||||
|
)
|
||||||
|
|
||||||
assert call_count["n"] == 4 # 3 volle pagina's + 1 korte slotpagina
|
assert call_count["n"] == 4 # 3 volle pagina's + 1 korte slotpagina
|
||||||
assert len(result) == 3 * page_size + 10
|
assert len(result) == 3 * page_size + 10
|
||||||
@@ -216,7 +252,9 @@ def test_paginated_get_stops_at_since_boundary_without_fetching_further(monkeypa
|
|||||||
monkeypatch.setattr(ns.requests, "get", fake_get)
|
monkeypatch.setattr(ns.requests, "get", fake_get)
|
||||||
|
|
||||||
since = now - timedelta(hours=6) # ruim binnen de eerste pagina
|
since = now - timedelta(hours=6) # ruim binnen de eerste pagina
|
||||||
result = ns._paginated_get("/api/v1/entries/sgv.json", since)
|
result = ns._paginated_get(
|
||||||
|
"/api/v1/entries/sgv.json", since, None, ns.ENTRIES_CURSOR_FIELD
|
||||||
|
)
|
||||||
|
|
||||||
assert calls["n"] == 1
|
assert calls["n"] == 1
|
||||||
assert len(result) == page_size # filtering op since gebeurt pas in _entries_since, niet hier
|
assert len(result) == page_size # filtering op since gebeurt pas in _entries_since, niet hier
|
||||||
@@ -255,3 +293,89 @@ def test_fetch_entries_range_retrieves_data_spanning_multiple_pages(monkeypatch)
|
|||||||
assert len(result) == len(all_entries)
|
assert len(result) == len(all_entries)
|
||||||
assert result["timestamp"].min().date() == start_date
|
assert result["timestamp"].min().date() == start_date
|
||||||
assert result["timestamp"].max().date() == end_date
|
assert result["timestamp"].max().date() == end_date
|
||||||
|
|
||||||
|
|
||||||
|
def _treatment(dt, event_type="Temp Basal"):
|
||||||
|
return {
|
||||||
|
"created_at": dt.strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3] + "Z",
|
||||||
|
"date": int(dt.timestamp() * 1000),
|
||||||
|
"eventType": event_type,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def test_fetch_treatments_range_retrieves_full_history_even_with_a_hidden_server_side_window(
|
||||||
|
monkeypatch,
|
||||||
|
):
|
||||||
|
"""Regressietest voor de echte productiebug (2026-07-31): treatments.json
|
||||||
|
filtert/sorteert intern op `created_at` (string), niet op `date`
|
||||||
|
(numeriek) zoals entries. Filteren op `date` — of helemaal geen filter
|
||||||
|
meegeven — leverde stilzwijgend hooguit de laatste ~4 dagen op, ook al
|
||||||
|
bevatten de documenten een geldig `date`-veld en stond de rest van de
|
||||||
|
geschiedenis (maanden) gewoon in de database (geverifieerd rechtstreeks
|
||||||
|
in MongoDB: 123k+ documenten, geen TTL-index, geen capped collection).
|
||||||
|
|
||||||
|
Deze test simuleert precies dat servergedrag: een find[date]-filter (of
|
||||||
|
het ontbreken van elk filter) geeft alleen de laatste 4 dagen terug;
|
||||||
|
alleen find[created_at] geeft de volledige, langere geschiedenis.
|
||||||
|
"""
|
||||||
|
monkeypatch.setenv("NIGHTSCOUT_URL", "https://example.com")
|
||||||
|
monkeypatch.delenv("NIGHTSCOUT_TOKEN", raising=False)
|
||||||
|
monkeypatch.delenv("NIGHTSCOUT_API_SECRET", raising=False)
|
||||||
|
|
||||||
|
end_date = date(2026, 7, 31)
|
||||||
|
start_date = date(2026, 5, 1) # 92 dagen, ruim voorbij het 4-dagen-venster
|
||||||
|
|
||||||
|
until = datetime.combine(end_date, datetime.max.time(), tzinfo=timezone.utc)
|
||||||
|
since = datetime.combine(start_date, datetime.min.time(), tzinfo=timezone.utc)
|
||||||
|
all_treatments = []
|
||||||
|
t = until
|
||||||
|
while t >= since:
|
||||||
|
all_treatments.append(_treatment(t))
|
||||||
|
t -= timedelta(minutes=30)
|
||||||
|
|
||||||
|
hidden_window_start = until - timedelta(days=4)
|
||||||
|
page_size = ns._PAGE_SIZE
|
||||||
|
|
||||||
|
def fake_get(*_args, **kwargs):
|
||||||
|
params = kwargs["params"]
|
||||||
|
cursor = params.get("find[created_at][$lte]")
|
||||||
|
if cursor is None:
|
||||||
|
# geen (of het verkeerde) filter -> verborgen 4-dagenvenster
|
||||||
|
pool = [e for e in all_treatments if e["created_at"] >= hidden_window_start.strftime(
|
||||||
|
"%Y-%m-%dT%H:%M:%S.%f"
|
||||||
|
)[:-3] + "Z"]
|
||||||
|
else:
|
||||||
|
pool = [e for e in all_treatments if e["created_at"] <= cursor]
|
||||||
|
pool = sorted(pool, key=lambda e: e["created_at"], reverse=True)
|
||||||
|
return FakeResponse(pool[:page_size])
|
||||||
|
|
||||||
|
monkeypatch.setattr(ns.requests, "get", fake_get)
|
||||||
|
|
||||||
|
result = ns.fetch_treatments_range(start_date, end_date)
|
||||||
|
assert len(result) == len(all_treatments)
|
||||||
|
assert result["timestamp"].min().date() == start_date
|
||||||
|
assert result["timestamp"].max().date() == end_date
|
||||||
|
|
||||||
|
|
||||||
|
def test_paginated_get_uses_created_at_field_for_treatments_cursor(monkeypatch):
|
||||||
|
"""Losstaande, gerichte assertie op het daadwerkelijke queryparameter-veld
|
||||||
|
— moet `find[created_at][$lte]` zijn, nooit `find[date][$lte]`, voor het
|
||||||
|
treatments-endpoint."""
|
||||||
|
monkeypatch.setenv("NIGHTSCOUT_URL", "https://example.com")
|
||||||
|
now = datetime.now(timezone.utc)
|
||||||
|
payload = [_treatment(now - timedelta(minutes=30 * i)) for i in range(5)]
|
||||||
|
|
||||||
|
seen_params = []
|
||||||
|
|
||||||
|
def fake_get(*_a, **kwargs):
|
||||||
|
seen_params.append(kwargs["params"])
|
||||||
|
return FakeResponse(payload)
|
||||||
|
|
||||||
|
monkeypatch.setattr(ns.requests, "get", fake_get)
|
||||||
|
|
||||||
|
ns._paginated_get(
|
||||||
|
"/api/v1/treatments.json", now - timedelta(days=1), None, ns.TREATMENTS_CURSOR_FIELD
|
||||||
|
)
|
||||||
|
|
||||||
|
assert "find[created_at][$lte]" in seen_params[0]
|
||||||
|
assert "find[date][$lte]" not in seen_params[0]
|
||||||
|
|||||||
@@ -2,7 +2,14 @@ from datetime import date
|
|||||||
|
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
|
|
||||||
from pdf_export import SECTION_AGP, SECTION_EVENTS, SECTION_SUMMARY, build_report_pdf
|
from pdf_export import (
|
||||||
|
DAGOVERZICHT_MAX_DAYS,
|
||||||
|
SECTION_AGP,
|
||||||
|
SECTION_DAGOVERZICHT,
|
||||||
|
SECTION_EVENTS,
|
||||||
|
SECTION_SUMMARY,
|
||||||
|
build_report_pdf,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def _sample_glucose():
|
def _sample_glucose():
|
||||||
@@ -10,6 +17,15 @@ def _sample_glucose():
|
|||||||
return pd.DataFrame({"timestamp": timestamps, "mmol": [6.0 + (i % 5) for i in range(48)]})
|
return pd.DataFrame({"timestamp": timestamps, "mmol": [6.0 + (i % 5) for i in range(48)]})
|
||||||
|
|
||||||
|
|
||||||
|
def _glucose_with_hypo_and_hyper_events():
|
||||||
|
"""3 dagen data met een echte hypo- (<3.9, >=15 min) en hyperepisode (>10.0)."""
|
||||||
|
timestamps = pd.date_range("2026-01-01", periods=3 * 24 * 12, freq="5min")
|
||||||
|
values = [7.0] * len(timestamps)
|
||||||
|
values[10:15] = [3.0] * 5 # 25 min hypo
|
||||||
|
values[100:106] = [12.0] * 6 # 30 min hyper
|
||||||
|
return pd.DataFrame({"timestamp": timestamps, "mmol": values})
|
||||||
|
|
||||||
|
|
||||||
def test_build_report_pdf_produces_valid_pdf_bytes():
|
def test_build_report_pdf_produces_valid_pdf_bytes():
|
||||||
glucose = _sample_glucose()
|
glucose = _sample_glucose()
|
||||||
events_summary = {
|
events_summary = {
|
||||||
@@ -39,3 +55,38 @@ def test_build_report_pdf_handles_no_sections_selected():
|
|||||||
glucose = _sample_glucose()
|
glucose = _sample_glucose()
|
||||||
pdf_bytes = build_report_pdf([], glucose, {}, date(2026, 1, 1), date(2026, 1, 2))
|
pdf_bytes = build_report_pdf([], glucose, {}, date(2026, 1, 1), date(2026, 1, 2))
|
||||||
assert pdf_bytes.startswith(b"%PDF")
|
assert pdf_bytes.startswith(b"%PDF")
|
||||||
|
|
||||||
|
|
||||||
|
def test_build_report_pdf_includes_dagoverzicht_section():
|
||||||
|
glucose = _sample_glucose()
|
||||||
|
pdf_bytes = build_report_pdf(
|
||||||
|
[SECTION_DAGOVERZICHT], glucose, {}, date(2026, 1, 1), date(2026, 1, 2)
|
||||||
|
)
|
||||||
|
assert pdf_bytes.startswith(b"%PDF")
|
||||||
|
assert len(pdf_bytes) > 500
|
||||||
|
|
||||||
|
|
||||||
|
def test_build_report_pdf_caps_dagoverzicht_at_max_days():
|
||||||
|
timestamps = pd.date_range("2026-01-01", periods=(DAGOVERZICHT_MAX_DAYS + 10) * 24, freq="1h")
|
||||||
|
glucose = pd.DataFrame({"timestamp": timestamps, "mmol": [7.0] * len(timestamps)})
|
||||||
|
pdf_bytes = build_report_pdf(
|
||||||
|
[SECTION_DAGOVERZICHT],
|
||||||
|
glucose,
|
||||||
|
{},
|
||||||
|
timestamps[0].date(),
|
||||||
|
timestamps[-1].date(),
|
||||||
|
)
|
||||||
|
assert pdf_bytes.startswith(b"%PDF")
|
||||||
|
|
||||||
|
|
||||||
|
def test_build_report_pdf_draws_hypo_and_hyper_charts_when_events_present():
|
||||||
|
glucose = _glucose_with_hypo_and_hyper_events()
|
||||||
|
events_summary = {
|
||||||
|
"Hypo Level 1": {"count": 1, "avg_duration_minutes": 25.0, "events_per_week": 2.3},
|
||||||
|
"Hyper Level 1": {"count": 1, "avg_duration_minutes": 30.0, "events_per_week": 2.3},
|
||||||
|
}
|
||||||
|
pdf_bytes = build_report_pdf(
|
||||||
|
[SECTION_EVENTS], glucose, events_summary, date(2026, 1, 1), date(2026, 1, 4)
|
||||||
|
)
|
||||||
|
assert pdf_bytes.startswith(b"%PDF")
|
||||||
|
assert len(pdf_bytes) > 500
|
||||||
|
|||||||
+128
-1
@@ -4,12 +4,17 @@ import pandas as pd
|
|||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from reporting import (
|
from reporting import (
|
||||||
|
GMI_TREND_COLUMNS,
|
||||||
agp_percentile_bands,
|
agp_percentile_bands,
|
||||||
|
basal_stats,
|
||||||
bolus_stats,
|
bolus_stats,
|
||||||
completeness_warning,
|
completeness_warning,
|
||||||
|
daily_gmi,
|
||||||
day_of_week_pattern,
|
day_of_week_pattern,
|
||||||
detect_events,
|
detect_events,
|
||||||
|
gmi_mmol_per_mol,
|
||||||
gmi_percent,
|
gmi_percent,
|
||||||
|
gmi_trend,
|
||||||
minutes_per_day,
|
minutes_per_day,
|
||||||
overnight_daytime_split,
|
overnight_daytime_split,
|
||||||
percent_cv,
|
percent_cv,
|
||||||
@@ -31,6 +36,63 @@ def test_gmi_percent_matches_bergenstal_formula():
|
|||||||
assert gmi_percent(7.0) == pytest.approx(6.3, abs=0.05)
|
assert gmi_percent(7.0) == pytest.approx(6.3, abs=0.05)
|
||||||
|
|
||||||
|
|
||||||
|
def test_gmi_mmol_per_mol_matches_known_ifcc_reference_values():
|
||||||
|
# 6.5% is de bekende diagnostische drempel = 48 mmol/mol (IFCC).
|
||||||
|
assert gmi_mmol_per_mol(6.5) == 48
|
||||||
|
assert gmi_mmol_per_mol(7.0) == 53
|
||||||
|
|
||||||
|
|
||||||
|
def test_daily_gmi_aggregates_per_calendar_day():
|
||||||
|
glucose = _series(
|
||||||
|
[
|
||||||
|
("2026-01-01 08:00", 6.0),
|
||||||
|
("2026-01-01 20:00", 8.0),
|
||||||
|
("2026-01-02 08:00", 7.0),
|
||||||
|
]
|
||||||
|
)
|
||||||
|
result = daily_gmi(glucose)
|
||||||
|
assert list(result["date"].astype(str)) == ["2026-01-01", "2026-01-02"]
|
||||||
|
assert result["gmi"].iloc[0] == pytest.approx(gmi_percent(7.0)) # gemiddelde van 6.0 en 8.0
|
||||||
|
assert result["gmi"].iloc[1] == pytest.approx(gmi_percent(7.0))
|
||||||
|
|
||||||
|
|
||||||
|
def test_daily_gmi_handles_empty_input():
|
||||||
|
empty = pd.DataFrame(columns=["timestamp", "mmol"])
|
||||||
|
result = daily_gmi(empty)
|
||||||
|
assert result.empty
|
||||||
|
assert list(result.columns) == ["date", "gmi"]
|
||||||
|
|
||||||
|
|
||||||
|
def test_gmi_trend_handles_empty_input():
|
||||||
|
empty = pd.DataFrame(columns=["timestamp", "mmol"])
|
||||||
|
result = gmi_trend(empty)
|
||||||
|
assert result.empty
|
||||||
|
assert list(result.columns) == GMI_TREND_COLUMNS
|
||||||
|
|
||||||
|
|
||||||
|
def test_gmi_trend_returns_no_line_for_a_single_day():
|
||||||
|
glucose = _series([("2026-01-01 08:00", 6.0)])
|
||||||
|
result = gmi_trend(glucose)
|
||||||
|
assert list(result.columns) == GMI_TREND_COLUMNS
|
||||||
|
assert len(result) == 1
|
||||||
|
assert pd.isna(result["trend"].iloc[0])
|
||||||
|
|
||||||
|
|
||||||
|
def test_gmi_trend_fits_increasing_linear_trend_with_band_around_it():
|
||||||
|
pairs = [(f"2026-01-{i + 1:02d} 08:00", 6.0 + i * 0.2) for i in range(10)]
|
||||||
|
glucose = _series(pairs)
|
||||||
|
result = gmi_trend(glucose, rolling_window_days=3)
|
||||||
|
|
||||||
|
assert len(result) == 10
|
||||||
|
assert result["trend"].iloc[-1] > result["trend"].iloc[0]
|
||||||
|
assert (result["trend_lower"] <= result["trend"]).all()
|
||||||
|
assert (result["trend"] <= result["trend_upper"]).all()
|
||||||
|
# Vrijwel lineaire input (gmi_percent rondt af op 1 decimaal, dus een
|
||||||
|
# kleine restfout blijft over) -> smalle betrouwbaarheidsband.
|
||||||
|
assert (result["trend_upper"] - result["trend_lower"]).max() == pytest.approx(0, abs=0.15)
|
||||||
|
assert result["rolling_avg"].iloc[-1] > result["rolling_avg"].iloc[0]
|
||||||
|
|
||||||
|
|
||||||
def test_percent_cv_handles_empty_input():
|
def test_percent_cv_handles_empty_input():
|
||||||
empty = pd.DataFrame(columns=["timestamp", "mmol"])
|
empty = pd.DataFrame(columns=["timestamp", "mmol"])
|
||||||
assert percent_cv(empty) == 0.0
|
assert percent_cv(empty) == 0.0
|
||||||
@@ -339,6 +401,7 @@ def test_bolus_stats_splits_by_event_type():
|
|||||||
"eventType": ["Meal Bolus", "Correction Bolus", "Site Change"],
|
"eventType": ["Meal Bolus", "Correction Bolus", "Site Change"],
|
||||||
"insulin": [4.0, 1.5, None],
|
"insulin": [4.0, 1.5, None],
|
||||||
"carbs": [45.0, None, None],
|
"carbs": [45.0, None, None],
|
||||||
|
"type": ["NORMAL", "NORMAL", ""],
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
result = bolus_stats(treatments)
|
result = bolus_stats(treatments)
|
||||||
@@ -347,12 +410,76 @@ def test_bolus_stats_splits_by_event_type():
|
|||||||
assert result["by_type"] == {"Meal Bolus": 4.0, "Correction Bolus": 1.5}
|
assert result["by_type"] == {"Meal Bolus": 4.0, "Correction Bolus": 1.5}
|
||||||
|
|
||||||
|
|
||||||
|
def test_bolus_stats_reclassifies_smb_correction_boluses_separately():
|
||||||
|
"""AndroidAPS registreert een automatische SMB-microbolus als
|
||||||
|
eventType == "Correction Bolus" met type == "SMB" — dit moet apart van
|
||||||
|
een handmatige correctiebolus (type != "SMB") geteld worden (gitea #20)."""
|
||||||
|
treatments = pd.DataFrame(
|
||||||
|
{
|
||||||
|
"timestamp": pd.to_datetime(["2026-01-01 08:00", "2026-01-01 12:00"]),
|
||||||
|
"eventType": ["Correction Bolus", "Correction Bolus"],
|
||||||
|
"insulin": [0.3, 0.9],
|
||||||
|
"carbs": [None, None],
|
||||||
|
"type": ["SMB", "NORMAL"],
|
||||||
|
}
|
||||||
|
)
|
||||||
|
result = bolus_stats(treatments)
|
||||||
|
assert result["by_type"] == {"SMB": 0.3, "Correction Bolus": 0.9}
|
||||||
|
|
||||||
|
|
||||||
|
def test_bolus_stats_treats_missing_type_column_as_non_smb():
|
||||||
|
treatments = pd.DataFrame(
|
||||||
|
{
|
||||||
|
"timestamp": pd.to_datetime(["2026-01-01 08:00"]),
|
||||||
|
"eventType": ["Correction Bolus"],
|
||||||
|
"insulin": [0.9],
|
||||||
|
"carbs": [None],
|
||||||
|
}
|
||||||
|
)
|
||||||
|
result = bolus_stats(treatments)
|
||||||
|
assert result["by_type"] == {"Correction Bolus": 0.9}
|
||||||
|
|
||||||
|
|
||||||
def test_bolus_stats_handles_empty_input():
|
def test_bolus_stats_handles_empty_input():
|
||||||
empty = pd.DataFrame(columns=["timestamp", "eventType", "insulin", "carbs"])
|
empty = pd.DataFrame(columns=["timestamp", "eventType", "insulin", "carbs", "type"])
|
||||||
result = bolus_stats(empty)
|
result = bolus_stats(empty)
|
||||||
assert result == {"total_units": 0.0, "count": 0, "by_type": {}}
|
assert result == {"total_units": 0.0, "count": 0, "by_type": {}}
|
||||||
|
|
||||||
|
|
||||||
|
def test_basal_stats_sums_temp_basal_rate_times_duration():
|
||||||
|
treatments = pd.DataFrame(
|
||||||
|
{
|
||||||
|
"timestamp": pd.to_datetime(["2026-01-01 08:00", "2026-01-01 08:30"]),
|
||||||
|
"eventType": ["Temp Basal", "Temp Basal"],
|
||||||
|
"absolute": [1.2, 0.6],
|
||||||
|
"duration": [30, 60],
|
||||||
|
}
|
||||||
|
)
|
||||||
|
# 1.2 E/u * 0.5u + 0.6 E/u * 1u = 0.6 + 0.6 = 1.2 E over 1 dag
|
||||||
|
result = basal_stats(treatments, period_days=1)
|
||||||
|
assert result["total_units"] == pytest.approx(1.2)
|
||||||
|
assert result["avg_units_per_day"] == pytest.approx(1.2)
|
||||||
|
|
||||||
|
|
||||||
|
def test_basal_stats_ignores_non_temp_basal_rows_and_missing_fields():
|
||||||
|
treatments = pd.DataFrame(
|
||||||
|
{
|
||||||
|
"timestamp": pd.to_datetime(["2026-01-01 08:00", "2026-01-01 08:30"]),
|
||||||
|
"eventType": ["Meal Bolus", "Temp Basal"],
|
||||||
|
"absolute": [None, None],
|
||||||
|
"duration": [None, 30],
|
||||||
|
}
|
||||||
|
)
|
||||||
|
result = basal_stats(treatments, period_days=1)
|
||||||
|
assert result == {"total_units": 0.0, "avg_units_per_day": 0.0}
|
||||||
|
|
||||||
|
|
||||||
|
def test_basal_stats_handles_empty_input():
|
||||||
|
empty = pd.DataFrame(columns=["timestamp", "eventType", "absolute", "duration"])
|
||||||
|
result = basal_stats(empty, period_days=1)
|
||||||
|
assert result == {"total_units": 0.0, "avg_units_per_day": 0.0}
|
||||||
|
|
||||||
|
|
||||||
def test_site_interval_stats_splits_by_site_change():
|
def test_site_interval_stats_splits_by_site_change():
|
||||||
glucose = _series(
|
glucose = _series(
|
||||||
[
|
[
|
||||||
|
|||||||
+30
-1
@@ -1,4 +1,12 @@
|
|||||||
from ui_common import hex_to_rgba
|
import plotly.graph_objects as go
|
||||||
|
|
||||||
|
from ui_common import (
|
||||||
|
PALETTE,
|
||||||
|
THRESHOLD_LINES,
|
||||||
|
add_threshold_lines,
|
||||||
|
build_tir_stacked_bar_figure,
|
||||||
|
hex_to_rgba,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def test_hex_to_rgba_converts_correctly():
|
def test_hex_to_rgba_converts_correctly():
|
||||||
@@ -7,3 +15,24 @@ def test_hex_to_rgba_converts_correctly():
|
|||||||
|
|
||||||
def test_hex_to_rgba_handles_hash_optional():
|
def test_hex_to_rgba_handles_hash_optional():
|
||||||
assert hex_to_rgba("2a78d6", 0.3) == "rgba(42,120,214,0.3)"
|
assert hex_to_rgba("2a78d6", 0.3) == "rgba(42,120,214,0.3)"
|
||||||
|
|
||||||
|
|
||||||
|
def test_add_threshold_lines_adds_one_line_per_clinical_threshold():
|
||||||
|
fig = go.Figure()
|
||||||
|
add_threshold_lines(fig, PALETTE["light"])
|
||||||
|
assert len(fig.layout.shapes) == len(THRESHOLD_LINES)
|
||||||
|
|
||||||
|
|
||||||
|
def test_add_threshold_lines_targets_subplot_row_when_given():
|
||||||
|
fig = go.Figure()
|
||||||
|
fig.add_trace(go.Scatter(x=[0], y=[0]))
|
||||||
|
add_threshold_lines(fig, PALETTE["light"], row=1)
|
||||||
|
assert len(fig.layout.shapes) == len(THRESHOLD_LINES)
|
||||||
|
|
||||||
|
|
||||||
|
def test_build_tir_stacked_bar_figure_has_one_bar_trace_per_band():
|
||||||
|
tir = {"very_low": 1.0, "low": 3.0, "in_range": 80.0, "high": 12.0, "very_high": 4.0}
|
||||||
|
fig = build_tir_stacked_bar_figure(tir, PALETTE["light"])
|
||||||
|
assert len(fig.data) == 5
|
||||||
|
assert [trace.y[0] for trace in fig.data] == [1.0, 3.0, 80.0, 12.0, 4.0]
|
||||||
|
assert fig.layout.barmode == "stack"
|
||||||
|
|||||||
+92
-1
@@ -15,7 +15,15 @@ import plotly.graph_objects as go
|
|||||||
import streamlit as st
|
import streamlit as st
|
||||||
|
|
||||||
from nightscout_client import fetch_entries_range, fetch_treatments_range
|
from nightscout_client import fetch_entries_range, fetch_treatments_range
|
||||||
from reporting import completeness_warning, detect_events, summarize_events
|
from reporting import (
|
||||||
|
HIGH,
|
||||||
|
LOW,
|
||||||
|
VERY_HIGH,
|
||||||
|
VERY_LOW,
|
||||||
|
completeness_warning,
|
||||||
|
detect_events,
|
||||||
|
summarize_events,
|
||||||
|
)
|
||||||
|
|
||||||
MMOL_HOVERTEMPLATE = "%{x|%d-%m %H:%M} · %{y:.1f} mmol/L<extra></extra>"
|
MMOL_HOVERTEMPLATE = "%{x|%d-%m %H:%M} · %{y:.1f} mmol/L<extra></extra>"
|
||||||
|
|
||||||
@@ -41,6 +49,14 @@ PALETTE = {
|
|||||||
"muted": "#898781",
|
"muted": "#898781",
|
||||||
"text": "#52514e",
|
"text": "#52514e",
|
||||||
"good": "#0ca30c",
|
"good": "#0ca30c",
|
||||||
|
# Losstaand van "low"/"high" hierboven (die blijven gedeeld tussen
|
||||||
|
# andere grafieken): de 5-bands TIR-grafiek (gitea #14) heeft juist
|
||||||
|
# zichtbaar onderscheid tussen laag/zeer-laag en hoog/zeer-hoog nodig.
|
||||||
|
"tir_very_low": "#8b1a1a",
|
||||||
|
"tir_low": "#d03b3b",
|
||||||
|
"tir_in_range": "#0ca30c",
|
||||||
|
"tir_high": "#fab219",
|
||||||
|
"tir_very_high": "#c76f0a",
|
||||||
},
|
},
|
||||||
"dark": {
|
"dark": {
|
||||||
"glucose": "#3987e5",
|
"glucose": "#3987e5",
|
||||||
@@ -55,9 +71,24 @@ PALETTE = {
|
|||||||
"muted": "#898781",
|
"muted": "#898781",
|
||||||
"text": "#c3c2b7",
|
"text": "#c3c2b7",
|
||||||
"good": "#0ca30c",
|
"good": "#0ca30c",
|
||||||
|
"tir_very_low": "#a32626",
|
||||||
|
"tir_low": "#d95d5d",
|
||||||
|
"tir_in_range": "#0ca30c",
|
||||||
|
"tir_high": "#fab219",
|
||||||
|
"tir_very_high": "#d9852e",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Klinische drempellijnen (Battelino et al. 2019), gedeeld tussen de AGP- en
|
||||||
|
# Dagoverzicht-pagina's (FR3, FR4 — gitea #15) zodat beide grafieken exact
|
||||||
|
# dezelfde 4 lijnen op dezelfde manier tonen.
|
||||||
|
THRESHOLD_LINES = [
|
||||||
|
(VERY_LOW, "3.0"),
|
||||||
|
(LOW, "3.9"),
|
||||||
|
(HIGH, "10.0"),
|
||||||
|
(VERY_HIGH, "13.9"),
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
def hex_to_rgba(hex_color: str, alpha: float) -> str:
|
def hex_to_rgba(hex_color: str, alpha: float) -> str:
|
||||||
"""Zet een '#rrggbb'-kleur om naar een 'rgba(r,g,b,a)'-string voor Plotly-fills."""
|
"""Zet een '#rrggbb'-kleur om naar een 'rgba(r,g,b,a)'-string voor Plotly-fills."""
|
||||||
@@ -98,6 +129,66 @@ def load_range_data(start: date, end: date):
|
|||||||
return glucose, treatments
|
return glucose, treatments
|
||||||
|
|
||||||
|
|
||||||
|
def add_threshold_lines(fig: go.Figure, colors: dict, row: int | None = None) -> None:
|
||||||
|
"""Teken de 4 klinische drempellijnen (3.0/3.9/10.0/13.9 mmol/L) op een grafiek.
|
||||||
|
|
||||||
|
Gedeeld door de AGP- en Dagoverzicht-pagina's (gitea #15). `row` wordt
|
||||||
|
doorgegeven aan `add_hline` voor subplot-figuren (Dagoverzicht); voor een
|
||||||
|
los-staande figuur (AGP) blijft dit `None`.
|
||||||
|
"""
|
||||||
|
kwargs = {} if row is None else {"row": row, "col": 1}
|
||||||
|
for value, label in THRESHOLD_LINES:
|
||||||
|
fig.add_hline(
|
||||||
|
y=value,
|
||||||
|
line={"color": colors["muted"], "width": 1, "dash": "dot"},
|
||||||
|
annotation_text=label,
|
||||||
|
annotation_position="right",
|
||||||
|
annotation_font={"size": 9, "color": colors["muted"]},
|
||||||
|
**kwargs,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
TIR_BAND_ORDER = ["very_low", "low", "in_range", "high", "very_high"]
|
||||||
|
TIR_BAND_LABELS = {
|
||||||
|
"very_low": "Zeer laag",
|
||||||
|
"low": "Laag",
|
||||||
|
"in_range": "In bereik",
|
||||||
|
"high": "Hoog",
|
||||||
|
"very_high": "Zeer hoog",
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def build_tir_stacked_bar_figure(tir: dict, colors: dict) -> go.Figure:
|
||||||
|
"""Bouw de 5-bands tijd-in-bereik als één gestapelde staafgrafiek (gitea #14).
|
||||||
|
|
||||||
|
Eén staaf, geen x-as-label, percentage op de y-as; van onder naar boven
|
||||||
|
"Zeer laag" -> "Zeer hoog" — dezelfde volgorde als de klinische banden.
|
||||||
|
"""
|
||||||
|
fig = go.Figure()
|
||||||
|
for band in TIR_BAND_ORDER:
|
||||||
|
fig.add_trace(
|
||||||
|
go.Bar(
|
||||||
|
x=[""],
|
||||||
|
y=[tir[band]],
|
||||||
|
name=TIR_BAND_LABELS[band],
|
||||||
|
marker_color=colors[f"tir_{band}"],
|
||||||
|
hovertemplate=f"{TIR_BAND_LABELS[band]}: %{{y:.0f}}%<extra></extra>",
|
||||||
|
)
|
||||||
|
)
|
||||||
|
fig.update_layout(
|
||||||
|
barmode="stack",
|
||||||
|
paper_bgcolor="rgba(0,0,0,0)",
|
||||||
|
plot_bgcolor="rgba(0,0,0,0)",
|
||||||
|
font_color=colors["text"],
|
||||||
|
height=140,
|
||||||
|
margin={"l": 10, "r": 10, "t": 10, "b": 10},
|
||||||
|
yaxis={"title": "%", "range": [0, 100], "gridcolor": colors["grid"]},
|
||||||
|
xaxis={"visible": False},
|
||||||
|
legend={"orientation": "h", "yanchor": "bottom", "y": 1.02, "xanchor": "left", "x": 0},
|
||||||
|
)
|
||||||
|
return fig
|
||||||
|
|
||||||
|
|
||||||
def render_completeness_warning(glucose: pd.DataFrame, start: date, end: date) -> None:
|
def render_completeness_warning(glucose: pd.DataFrame, start: date, end: date) -> None:
|
||||||
"""Toon reporting.completeness_warning()'s oordeel als st.warning, indien nodig.
|
"""Toon reporting.completeness_warning()'s oordeel als st.warning, indien nodig.
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user