Use python-fitbit
to interact with the Fitbit API and download activity data. Convert data to pandas dataframe and write to disk.
In [4]:
%load_ext pypath_magic
In [2]:
%pypath -a /Users/rbussman/Projects/sleep-bit
In [1]:
from src.data import get_fitbit
import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns
sns.set_context('poster')
In [30]:
resources = [
'activities/calories',
'activities/caloriesBMR',
'activities/steps',
'activities/distance',
'activities/minutesSedentary',
'activities/minutesLightlyActive',
'activities/minutesFairlyActive',
'activities/minutesVeryActive',
# 'activities/activityCalories'
]
df_activity = pd.DataFrame({})
for resource in resources:
activity = resource.split('/')[1]
resource_name = "-".join(resource.split('/'))
activity_time_series = authd_client.time_series(resource, base_date='today', end_date='2016-11-09')
df_activity[activity] = [resource_dict['value'] for resource_dict in activity_time_series[resource_name]]
df_activity['datetime'] = [resource_dict['dateTime'] for resource_dict in activity_time_series[resource_name]]
In [32]:
df_activity['datetime'] = pd.to_datetime(df_activity['datetime'])
df_activity = df_activity.set_index('datetime')
In [33]:
df_activity.head()
Out[33]:
In [34]:
data_path = os.path.join(os.getcwd(), os.pardir, 'data', 'interim', 'activity_data.csv')
df_activity.to_csv(data_path, index_label='datetime')
In [62]:
intraday = authd_client.intraday_time_series(
'activities/steps', base_date='2017-08-03', detail_level='1min')
In [63]:
intraday.keys()
Out[63]:
In [77]:
intraday.keys()
Out[77]:
In [80]:
intraday['activities-steps-intraday'].keys()
Out[80]:
In [82]:
intraday['activities-steps-intraday']['datasetInterval']
Out[82]:
In [83]:
import json
In [84]:
with open('test.json', 'w') as testfile:
json.dump(intraday, testfile)
In [65]:
df_day = pd.DataFrame(intraday['activities-steps-intraday']['dataset'])
In [66]:
df_day['time'] = pd.to_datetime(df_day['time'])
df_day = df_day.set_index('time')
In [67]:
df15min = df_day.resample('10T').sum()
In [68]:
df15min.plot()
Out[68]:
In [69]:
intraday = authd_client.intraday_time_series(
'activities/steps', base_date='2017-08-04', detail_level='1min')
In [70]:
df_day4 = pd.DataFrame(intraday['activities-steps-intraday']['dataset'])
In [71]:
df_day4['time'] = pd.to_datetime(df_day4['time'])
df_day4 = df_day4.set_index('time')
In [72]:
dfday415min = df_day4.resample('10T').sum()
In [75]:
plt.plot(dfday415min)
plt.plot(df15min)
Out[75]:
In [ ]: