Graphing and Predicting Blood Pressure

According to CDC statistics as many as 1 in 3 Americans suffer from high blood pressure. High blood pressure can contribute to a large range of conditions including a higher risk for heart disease and stroke.

A friend of mine was diagnosed with high blood pressure and with the help of medication was determined to lower it using data.

What gets measured, gets managed

First up they established a routine of taking their blood pressure and logging it. There are few blood pressure devices available to do this. They range from the standalone basic model to ones that talk to your phone. In my friends case, they opted for the cheaper basic standalone option.

The monitor shows the systolic, diastolic and pulse results on a simple LCD screen with no option to extract the readings to a laptop. Quickest solution was to use an app to collect the data by manually typing it in. After some experimentation we settled on the Withings app. Having an API to access the data made it a great choice. It also provided off site backup, just in case.

My friend logged their blood pressure morning and night in the Withings app and using the dashboard provided by Withings, track trends in the readings.

Over the last 5 months they have logged systolic and diastolic pressures along with heart rate from the standalone machine.

Below is a snapshot from the dashboard offered by Withings.

Withings blood pressure graph

Graph generated by Withings as part of their dashboard setup.

The graphs are OK but we both found them a little confusing. We also wanted to be able to predict future readings based on past readings.

Python, APIs and Graphing

I’m a Python programmer (mostly) and thought I could do better than the standard graphs offered by Withings.

With access via the API I set up basic Flask app to fetch the data from Withings and graph it locally. To access the data I needed I used a Withings Python lib (available on PyPi). For graphing I choose, just a few lines in the HTML and you can access a very powerful graphing tool.

First task was just 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.

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

Blood pressure graph

Added a simple moving average to attempt to smooth the graph.

Blood pressure can have some great and unpredictable peaks here and there and so trends can be hard to spot. I have been rendering time series data with R but hadn’t really anything in Python that was ready to use. This is Algorithmia comes in.

Predicting the Future

As this was a pretty quick and simple project I needed to make sense of the data as easily as possible. I explored a few services on line that did machine learning and data analysis. I slowly found out most were targeted at text classification.

Then I found Algorithmia which allows a large range of algorithms to be run on supplied data. You upload your data, they run your chosen algorithm over the data and supply you the results in realtime. They even have a Python library.

I chose two algorithms for this project:

Simple Moving Average

Simple moving average was my attempt to smooth the data and make it a bit simpler to spot trends. The raw data is very “peaky” and using the average does help but the extremes do remain.


This is where things get really fun. I had about 5 months of data to work with. Generally speaking the more data you have the better!

The entire dataset started after a course of Perindopril had been prescribed so there is no data of the high readings that triggered the course of treatment. Perindopril kicked in literally the next day reducing regular readings of > 190/85 to the much better < 140/85.

Lets see if Perindopril was going to be a viable long term treatment. Taking the existing readings I fed them into the Forecast algorithm from Algorithmia and graphed the results below.

Complete graph of blood pressure.

Complete graph of blood pressure.

Below is the output from the Forecast algorithm but using the simple moving average data instead of the raw data for the next 5 months.

Forecast results using moving average data.

Forecast results using moving average data.

Blood pressure data is harder to work from as it can be quite erratic at times. There are a number of algorithms for smoothing and normalise the data, which I intend to use to improve the predictions.


The main takeaway is that it appears my friends blood pressure isn’t going to get worse. It should stay within an acceptable range for the next few months.

Also my friend now has a set of nice graphs they can take to their doctor to discuss long term treatment. Blood pressure is something that can be influenced by a range of factors so regular reviews are important for long term management.

This was my first attempt at making forecasts and understanding the many, many ideas behind this kind data processing. I have barely scratched the service with what can be done with the data.

Sites used:

I have made the code available here Github repo.