For some reason, I recently got the popular wearable terminal fitbit charge2, so I'd like to play with it. Since fitbit has an official API, we will use this to get the heart rate in Python and graph it.
【environment】 ・ OS X EL Capitan · Python 3.5.2
Get the ID etc. required for API
Log in to https://dev.fitbit.com/login with your fitbit account and create a new application from the ** REGISTER AN APP * tab.
item name | Contents |
---|---|
Application Name | Application name(suitable) |
Description | Application description(suitable) |
Application Website | URL of the application(suitable) |
Organization | Affiliation organization |
Organization Website | URL of your organization |
OAuth 2.0 Application Type | App type(Select Client or Personal) |
Callback URL | http://127.0.0.1:8080/Enter |
Default Access Type | Since the data is not changed here, Read-Select Only |
When you create the application, you can get the (OAuth 2.0) Client ID and Client Secret from ** MANAGE MY APPS **, so make a note of this.
Open the ** Edit Application Settings ** page from * MANAGE MY APPS * and set OAuth 2.0 Application Type to Personal. If you want to get time series data such as minutes, you need to set it to Personal. (Reference: https://dev.fitbit.com/build/reference/web-api/activity/)
It's a little troublesome from here. Start the terminal. Move to an appropriate directory and copy and paste the following command to the terminal to execute it.
Terminal
git clone https://github.com/orcasgit/python-fitbit.git
How to use the terminal here If you get an error with the git command here
Next, copy and paste the following command to the terminal and execute it. Replace [OAuth 2.0 Client ID] and [Client Secret] with the values you got earlier.
python-fitbit/gather_keys_oauth2.py 【OAuth 2.0 Client ID】 【Client Secret】
Then the browser will start and Since such an authentication screen is displayed, check the necessary items and [Allow](I checked all for the time being).
When you return to the terminal,
Terminal
access_token = XXXXXXXXXXXXXXXXXXXXXX
refresh_token == YYYYYYYYYYYYYYYYYYYYYYYYY
Is displayed, so make a note of this. Obtained ・ Client ID ・ Client Secret ・ Ass and Ken ・ Refresh Token Make a note of the value of and save it.
Python coding was done in Jupyter.
Because it uses the python-fitbit library
Terminal
pip install fitbit
I will do it. python-fitbit GitHub python-fitbit commentary
Data acquisition
import fitbit
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
#ID etc. that you wrote down
CLIENT_ID = "XXXXXXXXXXXXXXXXXXXXXXX"
CLIENT_SECRET = "XXXXXXXXXXXXXXXXXXXXXXX"
ACCESS_TOKEN = "XXXXXXXXXXXXXXXXXXXXXXX"
REFRESH_TOKEN = "XXXXXXXXXXXXXXXXXXXXXXX"
#Date you want to get
DATE = "2017-01-31"
#ID etc. settings
authd_client = fitbit.Fitbit(CLIENT_ID, CLIENT_SECRET
,access_token=ACCESS_TOKEN, refresh_token=REFRESH_TOKEN)
#Get heart rate (in 1 second)
data_sec = authd_client.intraday_time_series('activities/heart', DATE, detail_level='1sec') #'1sec', '1min', or '15min'
heart_sec = data_sec["activities-heart-intraday"]["dataset"]
heart_sec[:10]
Output result
[{'time': '00:00:02', 'value': 56},
{'time': '00:00:07', 'value': 55},
{'time': '00:00:12', 'value': 54},
{'time': '00:00:17', 'value': 54},
{'time': '00:00:32', 'value': 54},
{'time': '00:00:37', 'value': 56},
{'time': '00:00:42', 'value': 55},
{'time': '00:00:52', 'value': 58},
{'time': '00:01:07', 'value': 58},
{'time': '00:01:09', 'value': 57}]
I have a heart rate! I'm getting data for 24 hours from midnight, but I'm not getting data every second without getting everything, but rather I'm getting less seconds.
By setting the argument detail_level to "1min" and "15min", you can get it in 1-minute or 15-minute units.
Data frame of dict type data
heart_df = pd.DataFrame.from_dict(heart_sec)
print(heart_df.shape)
heart_df.head()
From_dict is very convenient because it converts dict type data to dataframe in one shot.
index to time series data
heart_df.index = pd.to_datetime([DATE + " " + t for t in heart_df.time])
heart_df.head()
Convert index to time series data because it is easy to plot.
1. 1. Create a string that connects date and time in list comprehension notation
2. Convert string list to datetimeIndex with to_datetime.
3. 3. Change the index of the dataframe.
plot
heart_df.plot(y="value", figsize=(20,5))
heart_df.head()
Your heart rate is low during sleep from 1:30 to 8:00. Well analysis is coming again!
"Kimchi pot where you can eat 1/2 of the vegetables you need in a day" @ Seven-Eleven
Recommended Posts