python-fitbit data acquisition query summary

fitbit measures a variety of data such as heart rate, steps, calories, and the number of floors you've climbed! I will summarize how to get those data in python.

Please refer to Previous article to prepare the usage environment of API such as Access Token.

【environment】 ・ OS: OS X EL Capitan ・ Language: Python 3.5.2 -Library: python-fitbit

Acquisition of heart rate / exercise time series data

Use a function called * intraday_time_series *.

Example: Get step count

Preparation.py


import fitbit

#ID etc. that you wrote down
CLIENT_ID =  "XXXXXXXXXXXXXXXXXXXXXXX"
CLIENT_SECRET  = "XXXXXXXXXXXXXXXXXXXXXXX"
ACCESS_TOKEN =  "XXXXXXXXXXXXXXXXXXXXXXX"
REFRESH_TOKEN =  "XXXXXXXXXXXXXXXXXXXXXXX"
#ID etc. settings
authd_client = fitbit.Fitbit(CLIENT_ID, CLIENT_SECRET
                             ,access_token=ACCESS_TOKEN, refresh_token=REFRESH_TOKEN)

Get steps.py


#Get the number of steps (1 minute unit)
authd_client.intraday_time_series('activities/steps', base_date="2017-01-31", detail_level='1min', start_time="02:00", end_time="04:00") 

result


{'activities-steps': [{'dateTime': '2017-01-31', 'value': '0'}],
 'activities-steps-intraday': {'dataset': [{'time': '02:00:00', 'value': 0},
   {'time': '02:01:00', 'value': 0},
   {'time': '02:02:00', 'value': 0},
   {'time': '02:03:00', 'value': 0},
   {'time': '02:04:00', 'value': 0},
   {'time': '02:05:00', 'value': 0},
   ...

detail_level Interval of data to be acquired 1sec, 1min, 15min It is likely that there are missing values.

Type of data to be acquired

You can get various information in chronological order by changing the item'activities / steps' of the above function.

value Description
activities/heart Average heart rate
activities/steps Number of steps
activities/calories Calories burned
activities/floors Number of floors climbed (1st floor after climbing 3m)
activities/minutesSedentary Sitting time (minutes)
activities/minutesLightlyActive Time (minutes) of light exercise
activities/minutesFairlyActive Time (minutes) of active exercise
activities/minutesVeryActive Time (minutes) of strenuous exercise

Get sleep data

The * sleep * function is useful for sleep data.

Get sleep information.py


authd_client.sleep(date="2017-01-29")

result


{'sleep': [{'awakeCount': 1,
   'awakeDuration': 6,
   'awakeningsCount': 19,
   'dateOfSleep': '2017-01-29',
   'duration': 26700000,
   'efficiency': 93,
   'isMainSleep': True,
   'logId': 13626289712,
   'minuteData': [
    {'dateTime': '01:16:00', 'value': '3'},
    {'dateTime': '01:17:00', 'value': '3'},
    {'dateTime': '01:18:00', 'value': '3'},
    {'dateTime': '01:19:00', 'value': '3'},
    {'dateTime': '01:20:00', 'value': '3'},

    {'dateTime': '08:38:00', 'value': '1'},
    {'dateTime': '08:39:00', 'value': '1'},
    {'dateTime': '08:40:00', 'value': '2'}],
   'minutesAfterWakeup': 1,
   'minutesAsleep': 414,
   'minutesAwake': 30,
   'minutesToFallAsleep': 0,
   'restlessCount': 18,
   'restlessDuration': 25,
   'startTime': '2017-01-29T01:16:00.000',
   'timeInBed': 445}

Data contents

key name Contents
dateOfSleep Date (wake-up date)
isMainSleep Whether that sleep is the main sleep
dateOfSleep Number of wakes
startTime Bedtime
timeInBed Time spent lying in bed (minutes)
awakeCount Number of wakes
restlessCount Number of turns
minutesAsleep Sleep time (minutes)
minutesAwake Time spent in bed (fractions)
minutesToFallAsleep Time to fall asleep (minutes)
minutesAfterWakeup Time from getting up to getting up (minutes)
dateOfSleep Number of wakes
minuteData Sleep level every minute
{ 1:sleep, 2:Repeat turning over, 3:Awakening}

Reference site

python-fitbit GitHub python-fitbit documentation

Recommended Posts

python-fitbit data acquisition query summary
Numerical summary of data
Python data type summary memo
Stock price data acquisition tips
[Linux] [C / C ++] backtrace acquisition method summary
Data acquisition using python googlemap api
Data acquisition memo using Backlog API
Machine learning in Delemas (data acquisition)
Check the data summary in CASTable
Preprocessing in machine learning 2 Data acquisition
[Data science basics] Data acquisition from API