Created
December 7, 2014 02:10
-
-
Save tdhopper/891ce697166038fdcc30 to your computer and use it in GitHub Desktop.
Generates smoothed weight changed statistics using exponentially-weighted moving average.
This file contains hidden or 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
import dateutil | |
import pandas as pd | |
import random | |
from os.path import expanduser, join | |
home = expanduser("~") | |
with open(join(home, "Dropbox/Text Notes/Weight.txt"), "r") as f: | |
lines = f.readlines() | |
def parse_line(line): | |
s = line.split(" ") | |
weight = float(s[0]) | |
date = dateutil.parser.parse(' '.join(s[1:4])) | |
return date, weight | |
weight = pd.DataFrame([parse_line(l) for l in lines], columns=["date", "weight"]) \ | |
.set_index("date") \ | |
.resample("1D", how="mean") | |
weight["missing"] = weight.weight.isnull() | |
weight.weight = weight.weight.interpolate(method="linear") | |
std = weight.weight.diff().dropna().std() | |
noise = weight.missing.map(lambda missing: random.normalvariate(0, std) if missing else 0) | |
weight.weight = weight.weight + noise | |
smoothed = pd.ewma(weight.weight, span=30) | |
current = smoothed[-1] | |
stats = """ | |
Weight (lbs): %(weight).1f | |
Total Δ: %(total).1f | |
1 Week Δ: %(week).1f | |
1 Month Δ: %(month).1f | |
1 Year Δ: %(year).1f | |
""".strip() % {"weight": current, | |
"total": current - smoothed[0], | |
"week": current - smoothed[-8], | |
"month": current - smoothed[-32], | |
"year": current - smoothed[-366], | |
} | |
with open(join(home, "Dropbox/Text Notes/Weight Stats.txt"), "w") as f: | |
f.write(stats) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment