Files

163 lines
6.1 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""每日热量需求估算器。
算法:
1. BMR 采用 Mifflin-St Jeor 公式
2. TDEE 用两种方法交叉验证
法一(活动系数法):TDEE = BMR * PAL,PAL 按每日步数查表
法二(步数能耗法):TDEE = BMR * 1.3 + 0.5 * 体重(kg) * 步行距离(km)
3. 按目标(增重 / 维持 / 减重)对 TDEE 做盈余或赤字调整
用法示例:
python calorie_calculator.py --sex female --age 30 --height 160 --weight 50 \\
--steps 10000 --goal gain
"""
import argparse
# 目标 -> 热量调整比例
SURPLUS = {
"gain": 0.12, # 增重:+12%
"maintain": 0.00, # 维持
"lose": -0.175, # 减重:-17.5%(本方案不适用,仅作完整实现)
}
# 每日步数 -> (PAL 下限, PAL 上限)
PAL_TABLE = [
(0, 5000, 1.20, 1.30), # 久坐
(5000, 7500, 1.30, 1.40), # 轻度
(7500, 10000, 1.40, 1.50), # 轻度偏上
(10000, 12500, 1.50, 1.60), # 中度
(12500, 15000, 1.60, 1.70), # 中度偏上
(15000, float("inf"), 1.70, 1.90), # 高度
]
BASE_PAL = 1.30 # 不含额外走路的基础日常活动水平
STRIDE_RATIO = 0.415 # 步幅 ≈ 0.415 × 身高
WALK_KCAL_PER_KG_KM = 0.5 # 平路步行(约 5 km/h)的净能耗
PROTEIN_PER_KG = (1.2, 1.6) # 蛋白质推荐区间 g/(kg 体重)
def bmr_mifflin(sex, weight_kg, height_cm, age):
"""Mifflin-St Jeor 基础代谢率(kcal/天)。"""
base = 10 * weight_kg + 6.25 * height_cm - 5 * age
return base + 5 if sex == "male" else base - 161
def pal_range(steps):
"""按每日步数返回活动系数区间。"""
for lo, hi, pal_lo, pal_hi in PAL_TABLE:
if lo <= steps < hi:
return pal_lo, pal_hi
return PAL_TABLE[-1][2], PAL_TABLE[-1][3]
def stride_m(height_cm):
"""由身高估算步幅(米)。"""
return STRIDE_RATIO * height_cm / 100
def bmi_category(bmi):
"""中国成人 BMI 分级。"""
if bmi < 18.5:
return "偏瘦"
if bmi < 24:
return "正常" + ("(贴近偏瘦边缘)" if bmi < 20 else "")
if bmi < 28:
return "超重"
return "肥胖"
def estimate(sex, age, height_cm, weight_kg, steps=10000, goal="gain", surplus=None):
"""返回完整的估算结果字典。"""
bmr = bmr_mifflin(sex, weight_kg, height_cm, age)
pal_lo, pal_hi = pal_range(steps)
pal_mid = (pal_lo + pal_hi) / 2
tdee_pal = bmr * pal_mid
distance_km = steps * stride_m(height_cm) / 1000
walk_kcal = WALK_KCAL_PER_KG_KM * weight_kg * distance_km
tdee_steps = bmr * BASE_PAL + walk_kcal
tdee = (tdee_pal + tdee_steps) / 2
ratio = SURPLUS[goal] if surplus is None else surplus
target = tdee * (1 + ratio)
bmi = weight_kg / (height_cm / 100) ** 2
return {
"bmr": bmr,
"pal_lo": pal_lo,
"pal_hi": pal_hi,
"pal_mid": pal_mid,
"distance_km": distance_km,
"walk_kcal": walk_kcal,
"tdee_pal": tdee_pal,
"tdee_steps": tdee_steps,
"tdee": tdee,
"ratio": ratio,
"target": target,
"protein_low": PROTEIN_PER_KG[0] * weight_kg,
"protein_high": PROTEIN_PER_KG[1] * weight_kg,
"bmi": bmi,
"bmi_label": bmi_category(bmi),
}
def format_report(r, sex, age, height_cm, weight_kg, steps, goal):
goal_label = {"gain": "增重", "maintain": "维持", "lose": "减重"}[goal]
sign = "+" if r["ratio"] >= 0 else ""
return "\n".join([
"=" * 46,
"每日热量需求估算",
"=" * 46,
f"输入:{('男' if sex == 'male' else '女')},{age} 岁,"
f"{height_cm:.1f} cm,{weight_kg:.1f} kg,每天 {steps} 步",
f"BMI:{r['bmi']:.1f}({r['bmi_label']})",
"",
f"1) 基础代谢 BMR : {r['bmr']:.0f} kcal/天",
f"2) 步行距离 : {r['distance_km']:.1f} km"
f"(走路净消耗约 {r['walk_kcal']:.0f} kcal)",
f" 活动系数 PAL : {r['pal_mid']:.2f}"
f"(区间 {r['pal_lo']:.2f}–{r['pal_hi']:.2f})",
"",
f"3) 维持热量(系数法) : {r['tdee_pal']:.0f} kcal/天",
f" 维持热量(步数能耗法) : {r['tdee_steps']:.0f} kcal/天",
f" 维持热量(两法取中) : {r['tdee']:.0f} kcal/天",
"",
f"4) 目标:{goal_label},热量调整 {sign}{r['ratio'] * 100:.0f}%",
f" 建议每日摄入 : {r['target']:.0f} kcal/天",
f" 建议蛋白质 : {r['protein_low']:.0f}–{r['protein_high']:.0f} g/天",
"",
"说明:以上为估算值,请用连续 2 周晨起空腹体重均值校准;",
" 体重无变化则每天再加 150–200 kcal,逐步逼近 0.25–0.4 kg/周。",
"=" * 46,
])
def main():
parser = argparse.ArgumentParser(description="每日热量需求估算(BMR / TDEE)")
parser.add_argument("--sex", required=True, choices=["male", "female"],
help="性别:male / female")
parser.add_argument("--age", type=float, required=True, help="年龄(岁)")
parser.add_argument("--height", type=float, required=True, help="身高(cm)")
parser.add_argument("--weight", type=float, required=True, help="体重(kg)")
parser.add_argument("--steps", type=int, default=10000, help="每日步数,默认 10000")
parser.add_argument("--goal", choices=["gain", "maintain", "lose"], default="gain",
help="目标:gain 增重 / maintain 维持 / lose 减重,默认 gain")
parser.add_argument("--surplus", type=float, default=None,
help="自定义热量调整比例,如 0.15 表示 +15%%")
args = parser.parse_args()
result = estimate(args.sex, args.age, args.height, args.weight,
steps=args.steps, goal=args.goal, surplus=args.surplus)
print(format_report(result, args.sex, args.age, args.height, args.weight,
args.steps, args.goal))
if __name__ == "__main__":
main()