-
-
Save anonymous/7a1af9bbbb7115c5541e19b057b2dbb5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"user": { | |
"height": 1.75, | |
"target_kg": 70, | |
"target_date": "2017-08-01", | |
"old": 25 | |
}, | |
"activity": { | |
"2017-06-03": { | |
"カロリー": { | |
"昼": { | |
"チキンクリスプ": { | |
"kcal": 350, | |
"num": 2 | |
} | |
}, | |
"夜": { | |
"天下一品": 1000, | |
"アジフライ": 176, | |
"米": 100 | |
}, | |
"自転車": -2400 | |
}, | |
"体重": 78.4, | |
"体脂肪率": 25.4 | |
}, | |
"2017-06-04": { | |
"カロリー": { | |
"昼": { | |
"アジの開き": 166, | |
"米": 284, | |
"味噌汁": 25 | |
}, | |
"夜": { | |
"生どら焼き": 290, | |
"ロースステーキ": 800, | |
"小松菜": { | |
"kcal": 6, | |
"num": 2 | |
}, | |
"味噌汁": 25 | |
}, | |
"自転車": -600 | |
}, | |
"体重": 78.7, | |
"体脂肪率": 24.9 | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from datetime import datetime | |
import json | |
json_dict = json.load(open("weight.json", "r")) | |
user = json_dict["user"] | |
dataset = json_dict["activity"] | |
kcal_kg = 7200 # 出典 http://www.tanita.co.jp/health/detail/28 | |
std_kg = 22 * user["height"]**2 # BMI22を元にした標準体重 (67.4kg) | |
target_kg = user["target_kg"] # 目標の体重 | |
target_date = user["target_date"] # 目標の期限 | |
def bmr(kg): | |
# ハリスベネディクト基礎代謝 http://www.kintore.info/kisotaisya_mass/ | |
return 66.5 + kg * 13.8 + user["height"] * 500.0 - user["old"] * 6.8 | |
def bmi(kg): | |
return kg / user["height"]**2 | |
def accumulate_kcal(data): | |
if set(["num", "kcal"]) <= data.keys(): | |
return data["num"] * data["kcal"] | |
result = 0.0 | |
for k, v in data.items(): | |
if isinstance(v, (int, float)): | |
result += v | |
elif isinstance(v, dict): | |
result += accumulate_kcal(v) | |
return result | |
def remained_days(d1): | |
fmt = "%Y-%m-%d" | |
if isinstance(d1, str): | |
d1 = datetime.strptime(d1, fmt) | |
d2 = datetime.strptime(target_date, fmt) | |
return (d2 - d1).days | |
values = {} | |
def append(key, x): | |
if key not in values.keys(): | |
values[key] = [x] | |
else: | |
values[key].append(x) | |
print(""" | |
標準体重: {} kg | |
目標体重: {} kg | |
期限 {} (残り{}日) | |
""".format(std_kg, target_kg, target_date, remained_days(datetime.today()))) | |
index = [] | |
for date, data in dataset.items(): | |
actual_kg = data["体重"] | |
remained_kcal = (actual_kg - target_kg) * kcal_kg | |
index.append(date) | |
base_kcal = bmr(actual_kg) | |
actual_kcal = accumulate_kcal(data["カロリー"]) - base_kcal | |
append("消費カロリー(kcal)", actual_kcal) | |
append("残カロリー(kcal)", remained_kcal) | |
append("目標消費カロリー(kcal)", - remained_kcal / remained_days(date)) | |
append("体重(kg)", actual_kg) | |
append("予想体重(kg)", actual_kg + actual_kcal / kcal_kg) | |
append("BMI", bmi(actual_kg)) | |
append("BMR", base_kcal) | |
# 表を出力 | |
import pandas | |
from tabulate import tabulate | |
df = pandas.DataFrame(values, index=index) | |
print(tabulate(df, headers='keys', tablefmt='psql')) | |
# グラフを描画 | |
import matplotlib | |
import matplotlib.pyplot as plt | |
import matplotlib.font_manager as fm | |
import matplotlib.dates as mdates | |
plt.style.use('ggplot') | |
# フリーフォントの Ricty を使用: https://github.com/mzyy94/RictyDiminished-for-Powerline | |
font_path = next(filter(lambda f: "Ricty" in f, fm.findSystemFonts())) | |
font_prop = fm.FontProperties(fname=font_path) | |
matplotlib.rcParams['font.family'] = font_prop.get_name() | |
df[["残カロリー(kcal)", "体重(kg)"]].plot(subplots=True) | |
plt.savefig("weight.svg") | |
print("グラフを weight.svg に保存しました") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment