Details for confinement-measures-oura.ipynb

Published by gedankenstuecke

Description

How do the lockdowns/confinements related to COVID-19 influence our activity and sleep? This notebook looks into it!

0

Tags & Data Sources

confinement lockdown activity sleep Oura Connect

Comments

Would people be interested in similar plots for other data sources (e.g. fitbit?)

Please log in to comment.

Notebook
Last updated 5 months, 3 weeks ago

Confinement effects as measured by Oura

The Oura Ring gives you daily scores for activity, readiness and sleep. Let's see how those values change over time, comparing before and during the confinement. You can easily run this notebook on your data. You just need to connect your Oura ring to Open Humans!

Below's the boring processing code.

Plotting the effects

We're loading R for the visualization and aggregate the data points to get weekly averages, to smoothen out some of the day to day noise:

/opt/conda/lib/python3.6/site-packages/rpy2/rinterface/__init__.py:186: RRuntimeWarning: 
Attaching package: ‘lubridate’


  warnings.warn(x, RRuntimeWarning)
/opt/conda/lib/python3.6/site-packages/rpy2/rinterface/__init__.py:186: RRuntimeWarning: The following object is masked from ‘package:base’:

    date


  warnings.warn(x, RRuntimeWarning)
        week     type    value
1 2018-11-04 activity 93.00000
2 2018-11-11 activity 87.14286
3 2018-11-18 activity 91.14286
4 2018-11-25 activity 85.71429
5 2018-12-02 activity 71.14286
6 2018-12-09 activity 88.42857

Now we can make our plots. Right now it grabs data going back to 2019-11-01 and uses 2020-03-15 as the confinemenet data. You can change those values below to make it work for your own situation.

/opt/conda/lib/python3.6/site-packages/rpy2/rinterface/__init__.py:186: RRuntimeWarning: `geom_smooth()` using method = 'loess'

  warnings.warn(x, RRuntimeWarning)

Notebook
Last updated 5 months, 3 weeks ago

Confinement effects as measured by Oura

The Oura Ring gives you daily scores for activity, readiness and sleep. Let's see how those values change over time, comparing before and during the confinement. You can easily run this notebook on your data. You just need to connect your Oura ring to Open Humans!

Below's the boring processing code.

In [1]:
from ohapi import api
import os
import requests
import tempfile

user_details = api.exchange_oauth2_member(os.environ.get('OH_ACCESS_TOKEN'))
for i in user_details['data']:
    if i['source'] == 'direct-sharing-184' and i['basename'] == 'oura-data.json':
        break
import json 
oura = json.loads(requests.get(i['download_url']).content)
In [2]:
dates = []
values = []
value_type = []

for sdate in oura['sleep']:
    dates.append(sdate['summary_date'])
    values.append(sdate['score'])
    value_type.append('sleep')
    
for sdate in oura['activity']:
    dates.append(sdate['summary_date'])
    values.append(sdate['score'])
    value_type.append('activity')    

for sdate in oura['readiness']:
    dates.append(sdate['summary_date'])
    values.append(sdate['score'])
    value_type.append('readiness')
    
import pandas as pd
dataframe = pd.DataFrame(
    data = {
        'date': dates,
        'value': values,
        'type': value_type
    }
)

Plotting the effects

We're loading R for the visualization and aggregate the data points to get weekly averages, to smoothen out some of the day to day noise:

In [3]:
%load_ext rpy2.ipython
In [4]:
%%R -i dataframe 
library(lubridate)
dataframe$date <- as.Date(dataframe$date)
dataframe$week <- floor_date(dataframe$date,unit='week')
df_agg <- aggregate(value~week+type,data=dataframe,FUN=mean)
head(df_agg)
/opt/conda/lib/python3.6/site-packages/rpy2/rinterface/__init__.py:186: RRuntimeWarning: 
Attaching package: ‘lubridate’


  warnings.warn(x, RRuntimeWarning)
/opt/conda/lib/python3.6/site-packages/rpy2/rinterface/__init__.py:186: RRuntimeWarning: The following object is masked from ‘package:base’:

    date


  warnings.warn(x, RRuntimeWarning)
        week     type    value
1 2018-11-04 activity 93.00000
2 2018-11-11 activity 87.14286
3 2018-11-18 activity 91.14286
4 2018-11-25 activity 85.71429
5 2018-12-02 activity 71.14286
6 2018-12-09 activity 88.42857

Now we can make our plots. Right now it grabs data going back to 2019-11-01 and uses 2020-03-15 as the confinemenet data. You can change those values below to make it work for your own situation.

In [5]:
%%R -w 6 -h 6 --units in -r 200
library(ggplot2)

ggplot(subset(df_agg, df_agg$week > as.Date('2019-11-01')), aes(x=week,y=value)) + 
    geom_vline(xintercept=as.Date('2020-03-15'), color='red') +
    geom_line() + theme_minimal() + 
    geom_smooth(se = FALSE,color='grey') + 
    scale_y_continuous('score') + 
    facet_grid(type ~ .) + labs(
    title = "Personal effects of the confinement", 
    subtitle = "Red bar denotes start of confinement",
    caption = 'Scores for "activity", "readiness" and "sleep" as given by the Oura ring.\n Black lines are weekly averages. Grey lines are loess fit.'
  ) + theme(text = element_text(size=12)) + 
    theme(plot.caption= element_text(size=9))
/opt/conda/lib/python3.6/site-packages/rpy2/rinterface/__init__.py:186: RRuntimeWarning: `geom_smooth()` using method = 'loess'

  warnings.warn(x, RRuntimeWarning)