KDnuggets Using Predictive Algorithms to Track Real Time Health Trends

How to build a real-time health dashboard for tracking a person blood pressure readings, do time series analysis, and then graph the trends over time using predictive algorithms.

By Chris Hannam, Algorithmia Community Contributor.

Create a real time health dashboard with predictive algorithms

We’ve shown how to use predictive algorithms to track economic development. In this tutorial, we’re going to build a real-time health dashboard for tracking a person’s blood pressure readings, do time series analysis, and then graph the trends over time using predictive algorithms. This tutorial is the starting point for creating your own personal health dashboard using time series algorithms and predictive APIs.

We’ll be creating this dashboard in Python, using the Withings API for our data, the Forecastand Simple Moving Average microservices from Algorithmia, and Plotly to graph the data.

tl;dr here’s the GitHub repo for running Algorithmia tasks against Withings data using Python.

What Gets Measure, Gets Managed

 
Why blood pressure data? A friend of mine was diagnosed with high blood pressure and was determined to lower it using data. According to CDC statistics as many as 1 in 3 Americans suffer from high blood pressure, which can contribute to a higher risk for heart disease and stroke.

I’m a Python programmer, and thought I could build a simple, serverless health dashboard to help my friend measure and understand his blood pressure.

The first step was to establish a routine of measuring the blood pressure and logging it using a cheap blood pressure monitor and the Withings app. We’ll then use the Withings API to access our data for the health dashboard (Withings also makes a wifi-enabled blood pressure cuff for those that don’t want to manually log their data).

My friend has been logging their heart rate, systolic and diastolic blood pressure in the morning and night for the last five months. Below is a snapshot from the dashboard offered by Withings.

Withings Blood Pressure Graph

The graphs are OK, but we both found them confusing and not very helpful for tracking trends. I also wanted to be able to use predictive algorithms to forecast the future based on the past.

Here’s how we’ll build our own health dashboard instead.

Python, APIs, and Graphing

 
I set up a basic Flask app to fetch the blood pressure data from the Withings API, process the data, and graph it client side. To access the data, I used a Withings Python library(available on PyPi). For graphing, I choose Plot.ly. In just a few lines in the HTML you can quickly create powerful graphs.

The first task was to extract the raw data from Withings. Using the Python lib made this pretty simple. Where it got a little tricky was converting the fetched into something Plotly could graph. I went for a simple approach to build a string of text to render in a template using Jinja2 as part of Flask.

We’ll define our function to fetch the data from the Withings API (I’ve removed some code for brevity, but the repo has everything you need to get started). We call the Withings API to get our measurement data, and then iterate through the response to sort through the measurement dates and times. We’ll build up both an object to be used for graphing, as well as one of raw data we can pass to Algorithmia to run their predictive algorithms on.

As with most simple projects, Bootstrap made the perfect tool for rendering the HTML with the graphs embedded into the normal row layout.

Blood Pressure and Pulse Simple Moving Average

To build the graphs, we create an object in the following format in our Flask app when fetching the Withings data:

And then, to generate the graph we pass Plot.ly the x and y coordinates from our data. We use x as the index, and y as the diastolic, systolic, or pulse value like so:

Now that we have our graphs, we can see that our blood pressure data has some unpredictable peaks, which makes trends hard to spot. I’ve used R for time series data in the past, but have never used anything in Python. This is where Algorithmia comes in.