Code Monkey home page Code Monkey logo

sleep-analysis's Introduction

sleep-analysis

A Python project to analyze my Fitbit sleep data.

Overview

Spring quarter sleep durations versus bedtimes

This is a Python 3 project to obtain and analyze data on my sleep patterns, which has been collected nightly since July 2015 on my Fitbit Charge HR. The first part of the project is a script to access my nightly sleep logs; this script can be used by anyone hoping to access their data. The second part is the data analysis, in which I look into my spring sleep habits. This README focuses on the first part only.

Goals of this project included:

  • Learning to use Fitbit's API
  • Learning about the OAuth 2.0 protocol
  • Introducing myself to data analysis in Python
  • Gaining insight into my sleep patterns

Note: Fitbit is a registered trademark and service mark of Fitbit, Inc. My sleep analysis project is designed for personal use with the Fitbit platform. This product is not put out by Fitbit, and Fitbit does not service or warrant the functionality of this product.

Part 1: Obtaining Minute-by-Minute Sleep Data

The first part of this project involves accessing my nightly, minute-by-minute sleep data from Fitbit's servers. Fitbit does not usually provide this through their smartphone or desktop apps; users can only generally see summary statistics. In order to access this data, I had to use Fitbit's API myself.

Instructions

Clone the repo. Go to the Fitbit Developers website, register for an account, and register an app with them. The app will be for "Personal" use, which gives access to intraday sleep data.

Create a file in a new directory, credentials/client_secret.json. (Note that this directory is listed in .gitignore.) This file should have:

{
  "web": {
    "client_id": "",
    "client_secret": "",
    "redirect_uris": [ "" ],
    "auth_uri": "https://api.fitbit.com/1/user/[client_id]/sleep/date",
    "token_uri": "https://api.fitbit.com/oauth2/token"
  }
}

Fill in the client_id, client_secret, and redirect_uris according to what you see on the Fitbit Developers console. Replace [client_id] with your client ID in auth_uri, and leave token_uri as is.

Finally, you are ready to run the app from the command line as python fitbit.py <start date>, with <start date> being optional, formatted as yyyy-mm-dd. This will write up to 150 days' worth of minute-by-minute sleep data to the directory /logs/.

How Data Access Works

Accessing the data roughly follows this flow:

  • Try to read an access token from a file.
  • If no access token exists, generate a new one (this requires the user to authorize the script, which occurs in get_token()).
  • Try to read sleep data using this token. If the token has expired (this would happen if it was read from a file created some time ago), use the refresh token to generate and save a new one.
  • Read the sleep data with the new token.

The files are saved to a directory logs with names yyyy-mm-dd.json.

Usage Notes

Fitbit limits API usage to 150 requests per hour. If you have more than 150 days of data, the script will write 150, then exit and tell you the start date for the top of the next hour.

If the start date is not entered, a default date of 150 days ago is chosen.

Part 2: Data Analysis

Refer to spring-sleep-analysis.ipynb for the data analysis. This was done through a Jupyter notebook, making use of my spring quarter sleep data. Further detail is provided in that file; the image above is taken from the notebook.

sleep-analysis's People

Contributors

tuchandra avatar ozskywalker avatar

Watchers

James Cloos avatar Katy Chow Haynie avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.