<jupyter_start><jupyter_text>Notebook for playing around with `loseit` weekly summary data<jupyter_code>import numpy as np
import datetime as DT
from datetime import date as Date

import scipy.stats as stats
from scipy.optimize import fsolve
from scipy.signal import medfilt
from mealta import data
# -- plotting --
import matplotlib as mpl 
import matplotlib.pyplot as plt 
mpl.rcParams['text.usetex'] = True
mpl.rcParams['font.family'] = 'serif'
mpl.rcParams['axes.linewidth'] = 1.5
mpl.rcParams['axes.xmargin'] = 1
mpl.rcParams['xtick.labelsize'] = 'x-large'
mpl.rcParams['xtick.major.size'] = 5
mpl.rcParams['xtick.major.width'] = 1.5
mpl.rcParams['ytick.labelsize'] = 'x-large'
mpl.rcParams['ytick.major.size'] = 5
mpl.rcParams['ytick.major.width'] = 1.5
mpl.rcParams['legend.frameon'] = False
loseit = data.Calories()
daily = loseit.daily()
days = [Date.fromisoformat(_date) for _date in daily['date']]
fig = plt.figure(figsize=(20,5))
sub = fig.add_subplot(111)
sub.plot(days, daily['meal'], c='C0', label='Calorie Intake')
sub.plot(days, daily['exercise'], c='C1', label='Exercise')
sub.plot(days, daily['meal'] - daily['exercise'], c='k', ls='--')

sub.legend(loc='upper left', fontsize=20, ncol=2, handletextpad=0.2)
sub.set_xlabel('date', fontsize=25)
sub.set_xlim(days[0], days[-1])
sub.set_ylabel('calories', fontsize=25)
sub.set_ylim(0., 3000)
w_loseit = data.Weights()
weights = w_loseit.read()
days_w = [Date.fromisoformat(_date) for _date in weights['date']]
fig = plt.figure(figsize=(20,5))
sub = fig.add_subplot(111)
sub.plot(days_w, weights['weight'], c='C0')
sub.plot(days_w, medfilt(weights['weight'], 5), c='k', ls='--')

sub.set_xlabel('date', fontsize=25)
sub.set_xlim(days[0], days_w[0])
sub.set_ylabel('weights', fontsize=25)
sub.set_ylim(150., 162)
date0 = days[0]
i_days = [(day - date0).days for day in days]
i_days_w = [(Date.fromisoformat(day) - date0).days for day in weights['date']]
def Intake(t): 
    return np.interp(t, i_days, daily['meal'])

def Expenditure(t): 
    return np.interp(t, i_days, daily['exercise'])

def Weights(t):
    return np.interp(t, i_days_w[::-1], medfilt(weights['weight'], 11)[::-1])
def daily_calories_noexercise(t, basal): 
    return Intake(t) - basal

def daily_calories(t, basal, fexp=1.): 
    return Intake(t) - basal - fexp * Expenditure(t)
def BMR(dt=14, t_delay=0, exercise=True, fexp=1.): 
    
    t_basal, basal_metabolism = [], []
    for tt in range(t_delay, len(days)-dt)[::3]: 
        t0 = tt - t_delay
        t_basal.append(date0 + DT.timedelta(t0+0.5*dt))

        dweight = Weights(tt+dt) - Weights(tt)
        dcalories = dweight * 3000.
        
        if not exercise: 
            _fweight = lambda met: np.sum([daily_calories_noexercise(tt, met) for tt in range(t0, t0+dt)]) - dcalories
        else: 
            _fweight = lambda met: np.sum([daily_calories(tt, met, fexp=fexp) for tt in range(t0, t0+dt)]) - dcalories

        basal_metabolism.append(fsolve(_fweight, 2500.)[0])
    return t_basal, basal_metabolism
t_bmr, bmr = BMR(dt=14, t_delay=0, exercise=False)

fig = plt.figure(figsize=(20,5))
sub = fig.add_subplot(111)

sub.plot(days, daily['meal'], c='k', ls=':')
sub.plot(days, medfilt(daily['meal'], 5), c='k', label='Calorie Intake')

sub.plot(t_bmr, bmr, label='BMR assuming no exercise')

for cal in [1600., 1800., 2000.]: 
    sub.plot([days[0], days[-1]], [cal, cal], lw=0.5, ls='--', label='%.f' % cal)

sub.legend(loc='lower right', fontsize=20, ncol=5, handletextpad=0.2)
sub.set_xlabel('date', fontsize=25)
sub.set_xlim(days[0], days[-1])
sub.set_ylabel('calories', fontsize=25)
sub.set_ylim(0., 3000)
fig = plt.figure(figsize=(20,5))
sub = fig.add_subplot(111)

sub.plot(days, daily['meal'] - daily['exercise'], c='k', ls=':')
sub.plot(days, medfilt(daily['meal'] - daily['exercise'], 5), c='k', label='Calorie Intake - Exercise')


t_bmr, bmr = BMR(dt=14, t_delay=0, exercise=True, fexp=1)
sub.plot(t_bmr, bmr, label='BMR')

for cal in [1400., 1600., 1800.]: 
    sub.plot([days[0], days[-1]], [cal, cal], lw=0.5, ls='--', label='%.f' % cal)

sub.legend(loc='lower right', fontsize=20, ncol=5, handletextpad=0.2)
sub.set_xlabel('date', fontsize=25)
sub.set_xlim(days[0], days[-1])
sub.set_ylabel('calories', fontsize=25)
sub.set_ylim(0., 3000)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1912861136999068, 'fraction_numerical': 0.03960857409133271, 'mean_word_length': 4.9625, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '586243', 'license': 'MIT', 'n_tokens_mistral': 1857, 'n_tokens_neox': 1708, 'n_words': 350, 'path': 'nb/loseit.ipynb', 'repo_name': 'changhoonhahn/mealta', 'size': '4292'}