feat: enhance nutrition data processing and visualization with new correlation insights
- Refactored the `calculate_lag_correlation` function to normalize lag payloads and improve correlation calculations for various nutrition metrics. - Introduced a new function `build_nutrition_correlation_heuristic_items` to generate heuristic insights based on merged nutrition data, enhancing user understanding of dietary impacts on weight and body composition. - Updated the `get_nutrition_history_viz_bundle` function to include daily calorie balance and protein vs. lean mass data, providing a comprehensive view of nutrition trends. - Enhanced the frontend to visualize calorie balance and protein vs. lean mass insights, improving the user experience with clear graphical representations of dietary correlations.
This commit is contained in:
1 parent
45fb506a5e
commit
7ac9752c3d
9 files changed
+777
-195
No files matched your search
@@ -16,7 +16,7 @@ from db import get_db, get_cursor, r2d
|
||||
from auth import require_auth, check_feature_access, increment_feature_usage
|
||||
from routers.profiles import get_pid
|
||||
from feature_logger import log_feature_usage
|
||||
from caliper_composition import compute_lean_fat_kg, nearest_weight_kg_from_map
|
||||
from data_layer.nutrition_body_merge import build_merged_daily_nutrition_body_rows
|
||||
|
||||
router = APIRouter(prefix="/api/nutrition", tags=["nutrition"])
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -179,38 +179,9 @@ def get_nutrition_by_date(date: str, x_profile_id: Optional[str]=Header(default=
|
||||
|
||||
@router.get("/correlations")
|
||||
def nutrition_correlations(x_profile_id: Optional[str]=Header(default=None), session: dict=Depends(require_auth)):
|
||||
"""Get nutrition data correlated with weight and body fat."""
|
||||
"""Get nutrition data correlated with weight and body fat (Layer 1 Merge, siehe nutrition_body_merge)."""
|
||||
pid = get_pid(x_profile_id)
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
cur.execute("SELECT * FROM nutrition_log WHERE profile_id=%s ORDER BY date",(pid,))
|
||||
nutr={r['date']:r2d(r) for r in cur.fetchall()}
|
||||
cur.execute("SELECT date,weight FROM weight_log WHERE profile_id=%s ORDER BY date",(pid,))
|
||||
wlog={r['date']:r['weight'] for r in cur.fetchall()}
|
||||
cur.execute("SELECT date,lean_mass,body_fat_pct FROM caliper_log WHERE profile_id=%s ORDER BY date",(pid,))
|
||||
cals=sorted([r2d(r) for r in cur.fetchall()],key=lambda x:x['date'])
|
||||
all_dates=sorted(set(list(nutr)+list(wlog)))
|
||||
mi,last_cal,cal_by_date=0,{},{}
|
||||
for d in all_dates:
|
||||
while mi<len(cals) and cals[mi]['date']<=d: last_cal=cals[mi]; mi+=1
|
||||
if last_cal: cal_by_date[d]=last_cal
|
||||
result=[]
|
||||
for d in all_dates:
|
||||
if d not in nutr and d not in wlog: continue
|
||||
row={'date':d}
|
||||
if d in nutr: row.update({k:float(nutr[d][k]) if nutr[d][k] is not None else None for k in ['kcal','protein_g','fat_g','carbs_g']})
|
||||
if d in wlog: row['weight']=float(wlog[d])
|
||||
if d in cal_by_date:
|
||||
lm = cal_by_date[d].get('lean_mass')
|
||||
bf = cal_by_date[d].get('body_fat_pct')
|
||||
if bf is not None and lm is None:
|
||||
wkg = nearest_weight_kg_from_map(wlog, d)
|
||||
if wkg is not None:
|
||||
lm, _fat = compute_lean_fat_kg(wkg, float(bf))
|
||||
row['lean_mass'] = float(lm) if lm is not None else None
|
||||
row['body_fat_pct'] = float(bf) if bf is not None else None
|
||||
result.append(row)
|
||||
return result
|
||||
return build_merged_daily_nutrition_body_rows(pid)
|
||||
|
||||
|
||||
@router.get("/weekly")
|
||||
|
||||
Reference in new issue
Block a user