Replace monthly GMI bar chart with daily scatter + rolling avg + linear trend/CI

Adds daily_gmi() and gmi_trend(): per-day GMI as scatter points, a 7-day
rolling average line, and a linear regression trend line with a 95%
confidence band (normal approximation, no scipy dependency, consistent
with the existing preference for a lean container image).

Recomputed from the already-fetched period data on each page load; the
existing 1-hour Streamlit cache on load_range_data() already avoids
repeated Nightscout calls for the same period, so no separate storage
layer is needed at the current max. 90-day report window.

Closes gitea #19.
This commit is contained in:
2026-08-01 22:57:54 +02:00
parent 9e11dd8d73
commit 374ead70e5
3 changed files with 173 additions and 11 deletions
+56 -11
View File
@@ -9,12 +9,13 @@ from reporting import (
basal_stats,
bolus_stats,
day_of_week_pattern,
gmi_percent,
gmi_trend,
overnight_daytime_split,
site_interval_stats,
)
from ui_common import (
get_colors,
hex_to_rgba,
load_range_data,
render_completeness_warning,
select_period_days,
@@ -46,26 +47,70 @@ render_completeness_warning(glucose, start, end)
colors = get_colors()
events = classify_treatments(treatments)
# --- FR7: geschat HbA1c (GMI) per maand ------------------------------------
st.subheader("Geschat HbA1c (GMI) per maand")
monthly = glucose.copy()
monthly["month"] = monthly["timestamp"].dt.to_period("M").astype(str)
monthly_gmi = monthly.groupby("month")["mmol"].mean().apply(gmi_percent)
# --- FR7: geschat HbA1c (GMI) trend ----------------------------------------
st.subheader("Geschat HbA1c (GMI) trend")
trend = gmi_trend(glucose)
if len(monthly_gmi) >= 1:
fig = go.Figure(
go.Bar(x=monthly_gmi.index, y=monthly_gmi.to_numpy(), marker_color=colors["glucose"])
if len(trend) >= 2:
fig = go.Figure()
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(
paper_bgcolor="rgba(0,0,0,0)",
plot_bgcolor="rgba(0,0,0,0)",
font_color=colors["text"],
height=280,
height=320,
margin={"l": 10, "r": 10, "t": 10, "b": 10},
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")
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.")
# --- FR8: nacht vs. overdag --------------------------------------------------