← Files Fitness LedgerARCHIVED FILE

skills/nutrition-ledger/scripts/body_trend.py

667 Bytes · Oct 5, 2026 · 18:33 UTC

↓ Download file

"""Smoothed body-weight trend helpers."""

from statistics import mean
from typing import Any, Iterable, Mapping


def smoothed_weights(weights: Iterable[Mapping[str, Any]], window: int = 7) -> list[dict[str, Any]]:
    rows = sorted((w for w in weights if w.get("weight_lb") is not None), key=lambda w: str(w.get("date")))
    result = []
    for index, row in enumerate(rows):
        values = [float(item["weight_lb"]) for item in rows[max(0, index - window + 1):index + 1]]
        result.append({"date": row.get("date"), "weight_lb": row.get("weight_lb"), "smoothed_weight_lb": round(mean(values), 3), "sample_size": len(values)})
    return result

SHA-256: 6d1be8a6da2ee8330fa0c082f480f2553a1e6c876a48ad9746c680e3beed7929