Details for fitbit_ActivitiesAnalysis.ipynb

Published by Wenqiu999

Description

Activities analysis based on Fitbit data

0

Tags & Data Sources

fitbit connection activities Fitbit Connection

Comments

Please log in to comment.

Notebook
Last updated 4 weeks ago

This notebook is created to analyze activities data collected by fitbit. It used public data as a demonstrate to show how the plots look like. You can use the code below to analyze your own activities data. And This notebook is free to reuse and adapt, distributed under an MIT license: https://opensource.org/licenses/MIT

**Note: ** This notebook currently includes empty data (e.g. from dates the device wasn't worn) in averages - this may result in erroneous/misleading results. Fixes welcome and please share with wenqiu_cao@my.uri.edu.

In [1]:
import pandas as pd
import json
import numpy as np
import matplotlib.pyplot as plt
import seaborn
import urllib
import requests
import os
import tempfile
from datetime import datetime
import seaborn as sns


user_details = api.exchange_oauth2_member(os.environ.get('OH_ACCESS_TOKEN'))
for i in user_details['data']:
    if i['basename'] == 'fitbit-data.json' and i['source'] == 'direct-sharing-102':
        fitbit = requests.get(i['download_url']).json()

"""
data = requests.get('https://www.openhumans.org/api/public/project/102/datafiles/').json()
for i in data['results']:
    if i['id'] == 7217621:
        fitbit = requests.get(i['download_url']).json()
"""
In [2]:
DateTime = []

You can choose the year you want to explore. And extract the activities data out of the fitbit data.

In [3]:
year = '2018'
In [4]:
def GetData(obj,year,key):
    name = key.split('-')[-1]
    name = []
    for day in fitbit[obj][year][key]:
        name.append(day['value'])
    return name

for day in fitbit['minutes-to-sleep'][year]['sleep-minutesToFallAsleep']:
    DateTime.append(datetime.strptime(day['dateTime'],'%Y-%m-%d'))
In [5]:
DateTime = pd.DataFrame(DateTime)
In [6]:
activitiesData = pd.DataFrame(
    data = {
        'minutesLightlyActive': GetData('tracker-minutes-lightly-active',year,'activities-tracker-minutesLightlyActive'),
        'minutesSedentary': GetData('tracker-minutes-sedentary',year,'activities-tracker-minutesSedentary'),
        'steps': GetData('tracker-steps',year,'activities-tracker-steps'),
        'calories': GetData('tracker-calories',year,'activities-tracker-calories'),
        'weight': GetData('weight',year,'body-weight'),
        'elevation': GetData('tracker-elevation',year,'activities-tracker-elevation') ,
        'distance': GetData('tracker-distance',year,'activities-tracker-distance'),
        'minutesFairlyActive': GetData('tracker-minutes-fairly-active',year,'activities-tracker-minutesFairlyActive') ,
        'minutesVeryActive': GetData('tracker-minutes-very-active',year,'activities-tracker-minutesVeryActive') ,
        'floors': GetData('tracker-floors',year,'activities-tracker-floors') 

        
    }
)
In [7]:
activitiesData = pd.concat([DateTime, activitiesData], axis=1)
activitiesData.rename(columns={activitiesData.columns[0]: 'DateTime'}, inplace=True)
activitiesData = activitiesData.set_index('DateTime', drop= False)
In [8]:
activitiesData[['weight','minutesLightlyActive', 'steps', 'minutesSedentary', 'calories', 'elevation','distance','minutesFairlyActive','minutesVeryActive','floors']] = activitiesData[['weight','minutesLightlyActive', 'steps', 'minutesSedentary', 'calories', 'elevation','distance','minutesFairlyActive','minutesVeryActive','floors']].astype(float)
activitiesData['Day of week'] = activitiesData.index.weekday_name
weekday = ['Monday','Tuesday','Wednesday','Thursday','Friday']
weekend = ['Saturday','Sunday']
activitiesData.loc[activitiesData['Day of week'].isin(weekday), 'IsWeekday'] = 'TRUE'
activitiesData.loc[activitiesData['Day of week'].isin(weekend), 'IsWeekday'] = 'FALSE'
dayGroupedData = activitiesData.groupby(['Day of week']).mean()
dayTypeGroupedData = activitiesData.groupby(['IsWeekday']).mean()

Now we can start with descriptive analysis of the variables in your activities data.

In [9]:
activitiesData[['weight','minutesLightlyActive', 'steps', 'minutesSedentary', 'calories', 'elevation','distance','minutesFairlyActive','minutesVeryActive','floors']] = activitiesData[['weight','minutesLightlyActive', 'steps', 'minutesSedentary', 'calories', 'elevation','distance','minutesFairlyActive','minutesVeryActive','floors']].astype(float)
cols = ['steps', 'minutesSedentary', 'calories', 'elevation','distance','floors']
activitiesData[cols].describe()
Out[9]:
steps minutesSedentary calories elevation distance floors
count 365.000000 365.000000 365.000000 365.000000 365.000000 365.000000
mean 12328.230137 745.432877 3296.857534 101.682192 9.316905 33.517808
std 3821.891628 153.477202 437.778077 74.987382 2.880027 24.606080
min 0.000000 429.000000 1831.000000 0.000000 0.000000 0.000000
25% 10882.000000 677.000000 3069.000000 48.000000 8.288960 16.000000
50% 12625.000000 728.000000 3318.000000 88.000000 9.656330 29.000000
75% 14215.000000 777.000000 3555.000000 128.000000 10.676750 42.000000
max 25082.000000 1440.000000 5829.000000 448.000000 19.754160 147.000000

Let's explore the weekly pattern within the data first.

In [10]:
dayofweek = ['Mon','Tue','Wed','Thu','Fri','Sat','Sun']
axes = activitiesData.groupby('Day of week')[cols].mean().plot(kind='bar', figsize=(10, 45), subplots=True)
plt.xlabel('Day of Week', fontsize=10)
plt.xticks(np.arange(len(dayofweek)),dayofweek)
plt.show()

There are 3 different levels of activity. Let's take a look at these levels.

In [11]:
cols_level = ['minutesLightlyActive', 'minutesFairlyActive','minutesVeryActive']
activitiesData[cols_level].describe()
Out[11]:
minutesLightlyActive minutesFairlyActive minutesVeryActive
count 365.000000 365.000000 365.000000
mean 169.317808 39.213699 59.342466
std 51.816326 21.009766 26.984030
min 0.000000 0.000000 0.000000
25% 141.000000 25.000000 45.000000
50% 170.000000 38.000000 60.000000
75% 196.000000 52.000000 77.000000
max 365.000000 124.000000 158.000000
In [12]:
fig = plt.figure(figsize = (6,6))
labels=['LightActive', 'FairlyActive','VeryActive']
plt.pie(activitiesData[cols_level].mean(), colors = ['lightskyblue', 'yellowgreen','salmon'], autopct='%1.1f%%', labels=labels, textprops=dict(color="w"))
plt.title('Levels of activity', fontsize=14)
plt.legend()
plt.show()

Do these levels vary based on different days?

In [13]:
fig = plt.figure(figsize = (14,5))
plt.bar((dayGroupedData.index), dayGroupedData['minutesFairlyActive'],  width = 0.4, alpha =0.5, color='dodgerblue', label='Minutes Fairly Active')
plt.bar((dayGroupedData.index), dayGroupedData['minutesVeryActive'], width = 0.4, alpha =0.5, color='slateblue', label='Minutes Very Active', )
plt.bar((dayGroupedData.index), dayGroupedData['minutesLightlyActive'],width = 0.4, alpha =0.5, color='lightskyblue', label='Minutes Lightly Active')
plt.xticks(np.arange(len(dayofweek)),dayofweek)
plt.legend()
plt.show()

Do you activities vary on weekends vs weekdays?

In [14]:
for name, ax in zip(cols, axes):
    ax = activitiesData.boxplot(column = name, by = 'IsWeekday', vert = True, widths = 0.4)
    ax.set_title(name)
    if ax != axes[-1]:
        ax.set_xlabel('')
plt.show()

Let's explore if there is any monthly pattern of your activities.

In [15]:
activitiesData['Month'] = activitiesData.index.month
In [16]:
fig, axes = plt.subplots(3, 1, figsize=(12,10), sharex=True)
for name, ax in zip(cols, axes):
    sns.boxplot(data=activitiesData, x='Month', y=name, ax=ax)
    ax.set_ylabel('Minutes')
    ax.set_title(name)
    if ax != axes[-1]:
        ax.set_xlabel('')
plt.show()
/opt/conda/lib/python3.6/site-packages/seaborn/categorical.py:454: FutureWarning: remove_na is deprecated and is a private function. Do not use.
  box_data = remove_na(group_data)
/opt/conda/lib/python3.6/site-packages/seaborn/categorical.py:454: FutureWarning: remove_na is deprecated and is a private function. Do not use.
  box_data = remove_na(group_data)
/opt/conda/lib/python3.6/site-packages/seaborn/categorical.py:454: FutureWarning: remove_na is deprecated and is a private function. Do not use.
  box_data = remove_na(group_data)
/opt/conda/lib/python3.6/site-packages/seaborn/categorical.py:454: FutureWarning: remove_na is deprecated and is a private function. Do not use.
  box_data = remove_na(group_data)
/opt/conda/lib/python3.6/site-packages/seaborn/categorical.py:454: FutureWarning: remove_na is deprecated and is a private function. Do not use.
  box_data = remove_na(group_data)
/opt/conda/lib/python3.6/site-packages/seaborn/categorical.py:454: FutureWarning: remove_na is deprecated and is a private function. Do not use.
  box_data = remove_na(group_data)
/opt/conda/lib/python3.6/site-packages/seaborn/categorical.py:454: FutureWarning: remove_na is deprecated and is a private function. Do not use.
  box_data = remove_na(group_data)
/opt/conda/lib/python3.6/site-packages/seaborn/categorical.py:454: FutureWarning: remove_na is deprecated and is a private function. Do not use.
  box_data = remove_na(group_data)
/opt/conda/lib/python3.6/site-packages/seaborn/categorical.py:454: FutureWarning: remove_na is deprecated and is a private function. Do not use.
  box_data = remove_na(group_data)
/opt/conda/lib/python3.6/site-packages/seaborn/categorical.py:454: FutureWarning: remove_na is deprecated and is a private function. Do not use.
  box_data = remove_na(group_data)
/opt/conda/lib/python3.6/site-packages/seaborn/categorical.py:454: FutureWarning: remove_na is deprecated and is a private function. Do not use.
  box_data = remove_na(group_data)
/opt/conda/lib/python3.6/site-packages/seaborn/categorical.py:454: FutureWarning: remove_na is deprecated and is a private function. Do not use.
  box_data = remove_na(group_data)
/opt/conda/lib/python3.6/site-packages/seaborn/categorical.py:454: FutureWarning: remove_na is deprecated and is a private function. Do not use.
  box_data = remove_na(group_data)
/opt/conda/lib/python3.6/site-packages/seaborn/categorical.py:454: FutureWarning: remove_na is deprecated and is a private function. Do not use.
  box_data = remove_na(group_data)
/opt/conda/lib/python3.6/site-packages/seaborn/categorical.py:454: FutureWarning: remove_na is deprecated and is a private function. Do not use.
  box_data = remove_na(group_data)
/opt/conda/lib/python3.6/site-packages/seaborn/categorical.py:454: FutureWarning: remove_na is deprecated and is a private function. Do not use.
  box_data = remove_na(group_data)
/opt/conda/lib/python3.6/site-packages/seaborn/categorical.py:454: FutureWarning: remove_na is deprecated and is a private function. Do not use.
  box_data = remove_na(group_data)
/opt/conda/lib/python3.6/site-packages/seaborn/categorical.py:454: FutureWarning: remove_na is deprecated and is a private function. Do not use.
  box_data = remove_na(group_data)
/opt/conda/lib/python3.6/site-packages/seaborn/categorical.py:454: FutureWarning: remove_na is deprecated and is a private function. Do not use.
  box_data = remove_na(group_data)
/opt/conda/lib/python3.6/site-packages/seaborn/categorical.py:454: FutureWarning: remove_na is deprecated and is a private function. Do not use.
  box_data = remove_na(group_data)
/opt/conda/lib/python3.6/site-packages/seaborn/categorical.py:454: FutureWarning: remove_na is deprecated and is a private function. Do not use.
  box_data = remove_na(group_data)
/opt/conda/lib/python3.6/site-packages/seaborn/categorical.py:454: FutureWarning: remove_na is deprecated and is a private function. Do not use.
  box_data = remove_na(group_data)
/opt/conda/lib/python3.6/site-packages/seaborn/categorical.py:454: FutureWarning: remove_na is deprecated and is a private function. Do not use.
  box_data = remove_na(group_data)
/opt/conda/lib/python3.6/site-packages/seaborn/categorical.py:454: FutureWarning: remove_na is deprecated and is a private function. Do not use.
  box_data = remove_na(group_data)
/opt/conda/lib/python3.6/site-packages/seaborn/categorical.py:454: FutureWarning: remove_na is deprecated and is a private function. Do not use.
  box_data = remove_na(group_data)
/opt/conda/lib/python3.6/site-packages/seaborn/categorical.py:454: FutureWarning: remove_na is deprecated and is a private function. Do not use.
  box_data = remove_na(group_data)
/opt/conda/lib/python3.6/site-packages/seaborn/categorical.py:454: FutureWarning: remove_na is deprecated and is a private function. Do not use.
  box_data = remove_na(group_data)
/opt/conda/lib/python3.6/site-packages/seaborn/categorical.py:454: FutureWarning: remove_na is deprecated and is a private function. Do not use.
  box_data = remove_na(group_data)
/opt/conda/lib/python3.6/site-packages/seaborn/categorical.py:454: FutureWarning: remove_na is deprecated and is a private function. Do not use.
  box_data = remove_na(group_data)
/opt/conda/lib/python3.6/site-packages/seaborn/categorical.py:454: FutureWarning: remove_na is deprecated and is a private function. Do not use.
  box_data = remove_na(group_data)
/opt/conda/lib/python3.6/site-packages/seaborn/categorical.py:454: FutureWarning: remove_na is deprecated and is a private function. Do not use.
  box_data = remove_na(group_data)
/opt/conda/lib/python3.6/site-packages/seaborn/categorical.py:454: FutureWarning: remove_na is deprecated and is a private function. Do not use.
  box_data = remove_na(group_data)
/opt/conda/lib/python3.6/site-packages/seaborn/categorical.py:454: FutureWarning: remove_na is deprecated and is a private function. Do not use.
  box_data = remove_na(group_data)
/opt/conda/lib/python3.6/site-packages/seaborn/categorical.py:454: FutureWarning: remove_na is deprecated and is a private function. Do not use.
  box_data = remove_na(group_data)
/opt/conda/lib/python3.6/site-packages/seaborn/categorical.py:454: FutureWarning: remove_na is deprecated and is a private function. Do not use.
  box_data = remove_na(group_data)
/opt/conda/lib/python3.6/site-packages/seaborn/categorical.py:454: FutureWarning: remove_na is deprecated and is a private function. Do not use.
  box_data = remove_na(group_data)

Are there any correlations among your activities or the levels of your activity?

In [17]:
correlations = activitiesData[['minutesLightlyActive', 'minutesFairlyActive','minutesVeryActive','steps', 'minutesSedentary', 'calories', 'elevation','distance','floors']].corr()
fig = plt.figure(figsize=(15,15)) 
sns.heatmap(correlations,cmap=plt.cm.Greys, linewidths=0.05,vmax=1, vmin=0 ,annot=True,annot_kws={'size':10,'weight':'bold'})
ax.set_title('Activities correlation')
plt.show()
In [ ]: