Details for Count Missing Heart Beats.ipynb

Published by sprague

Description

Plot your most recent 1000 Apple Watch heart rate numbers along with red lines that guess where you might be missing some heart beats.

0

Tags & Data Sources

heartrate apple watch OH Data Port for Apple Health

Comments

Please log in to comment.

Notebook
Last updated 3 weeks, 3 days ago

Parsed with column specification:
cols(
  hr = col_double(),
  date = col_datetime(format = ""),
  type = col_character()
)

Make a dataframe with the last 1000 heart rates and their deviations from the average of the past 1000.

A tibble: 6 × 4
datehrdifflag
<dttm><dbl><dbl><dbl>
2020-06-17 16:01:2671-11NA
2020-06-17 15:57:2175 -7-4
2020-06-17 15:50:3677 -5-2
2020-06-17 15:45:3281 -1-4
2020-06-17 15:38:5176 -6 5
2020-06-17 15:37:3976 -6 0

Plot heart rates for the past 1000 data points and draw a red vertical line at each point where two adjacent heart rates differ by more than 50 bpm from what you'd expect. The diff calculation above corrects for the average heart rate for this individual over time, hopefully to make this more comparable across individuals

Notebook
Last updated 3 weeks, 3 days ago

In [3]:
library(tidyverse)
library(dplyr)
library(lubridate)
In [4]:
library(httr)
access_token <- Sys.getenv("OH_ACCESS_TOKEN")
url <- paste("https://www.openhumans.org/api/direct-sharing/project/exchange-member/?access_token=",access_token,sep="")
resp <- GET(url)
user <- content(resp, "parsed")
user_data <- user$data
while (!is.null(user$`next`)) {
  resp <- GET(user$`next`)
  user <- content(resp, "parsed")
  user_data <- append(user_data, user$data)
}
for (data_source in user_data){
  if (data_source$source == 'direct-sharing-453'){
    hr <- readr::read_csv(url(data_source$download_url),col_names=c("hr","date","type"))
  }
}
Parsed with column specification:
cols(
  hr = col_double(),
  date = col_datetime(format = ""),
  type = col_character()
)

Make a dataframe with the last 1000 heart rates and their deviations from the average of the past 1000.

In [92]:
missing_hr <- hr %>% head(1000) %>%
  dplyr::filter(type=="H") %>% 
  transmute(date=date,hr=hr,diff=hr-median(hr), lag = lag(hr)-hr)
In [93]:
missing_hr  %>% head()
A tibble: 6 × 4
datehrdifflag
<dttm><dbl><dbl><dbl>
2020-06-17 16:01:2671-11NA
2020-06-17 15:57:2175 -7-4
2020-06-17 15:50:3677 -5-2
2020-06-17 15:45:3281 -1-4
2020-06-17 15:38:5176 -6 5
2020-06-17 15:37:3976 -6 0

Plot heart rates for the past 1000 data points and draw a red vertical line at each point where two adjacent heart rates differ by more than 50 bpm from what you'd expect. The diff calculation above corrects for the average heart rate for this individual over time, hopefully to make this more comparable across individuals

In [96]:
missing_hr  %>% ggplot(aes(x=date,y=hr)) +
    geom_line() +
    geom_vline(xintercept = missing_hr  %>% dplyr::filter(lag - diff > 50)  %>% pull(date), color = "red") + 
    labs(title="Missing Heart Beats?", x=element_blank()) +
    theme(plot.title = element_text(color = "red", size = 24, face = "bold"),
         axis.text.x = element_text(angle = 90, hjust = 1)) +
    geom_line(aes(x=date,y=hr), color = "blue") + 
    scale_y_continuous(sec.axis = sec_axis(~ . + 50))