Sleep Analyzer Example¶
This example illustrates how to import data (summary and raw data) from the Withings Sleep Analyzer, how to get sleep endpoints from the summary data and how to split the raw data (collected over several nights) into single nights.
Setup and Helper Functions¶
[1]:
from pathlib import Path
import re
import pandas as pd
import numpy as np
from fau_colors import cmaps
import biopsykit as bp
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
%load_ext autoreload
%autoreload 2
[2]:
plt.close("all")
palette = sns.color_palette(cmaps.faculties)
sns.set_theme(context="notebook", style="ticks", font="sans-serif", palette=palette)
plt.rcParams["figure.figsize"] = (8, 4)
plt.rcParams["pdf.fonttype"] = 42
plt.rcParams["mathtext.default"] = "regular"
palette
[2]:
[3]:
def display_dict_structure(dict_to_display):
_display_dict_recursive(dict_to_display)
def _display_dict_recursive(dict_to_display):
if isinstance(dict_to_display, dict):
display(dict_to_display.keys())
_display_dict_recursive(list(dict_to_display.values())[0])
else:
display("Dataframe shape: {}".format(dict_to_display.shape))
display(dict_to_display.head())
Sleep Analyzer Summary¶
[4]:
sleep_summary = bp.example_data.get_sleep_analyzer_summary_example()
sleep_summary.head()
[4]:
sleep_onset | wake_onset | recording_start | recording_end | total_time_light_sleep | total_time_deep_sleep | total_time_rem_sleep | total_time_awake | number_wake_bouts | sleep_onset_latency | getup_latency | total_time_snoring | count_snoring_episodes | heart_rate_avg | heart_rate_min | heart_rate_max | total_duration | wake_after_sleep_onset | total_sleep_duration | net_sleep_duration | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
date | ||||||||||||||||||||
2020-10-10 00:00:00+02:00 | 2020-10-11 02:26:00+02:00 | 2020-10-11 09:47:00+02:00 | 2020-10-11 02:04:00+02:00 | 2020-10-11 09:59:00+02:00 | 263 | 89 | 82 | 41 | 1 | 22 | 12 | 19 | 1 | 68 | 55 | 82 | 28500 | 7 | 441 | 434 |
2020-10-11 00:00:00+02:00 | 2020-10-12 00:52:00+02:00 | 2020-10-12 07:15:00+02:00 | 2020-10-12 00:22:00+02:00 | 2020-10-12 07:15:00+02:00 | 120 | 165 | 96 | 30 | 1 | 30 | 0 | 0 | 0 | 72 | 58 | 87 | 24780 | 0 | 383 | 383 |
2020-10-12 00:00:00+02:00 | 2020-10-13 00:02:00+02:00 | 2020-10-13 07:21:00+02:00 | 2020-10-12 23:52:00+02:00 | 2020-10-13 07:21:00+02:00 | 272 | 108 | 59 | 10 | 0 | 10 | 0 | 0 | 0 | 51 | 43 | 72 | 26940 | 0 | 439 | 439 |
2020-10-15 00:00:00+02:00 | 2020-10-16 01:16:00+02:00 | 2020-10-16 07:56:00+02:00 | 2020-10-15 23:56:00+02:00 | 2020-10-16 07:56:00+02:00 | 254 | 97 | 43 | 86 | 1 | 80 | 0 | 0 | 0 | 66 | 55 | 75 | 28800 | 6 | 400 | 394 |
2020-10-17 00:00:00+02:00 | 2020-10-17 23:59:00+02:00 | 2020-10-18 10:29:00+02:00 | 2020-10-17 23:19:00+02:00 | 2020-10-18 10:29:00+02:00 | 397 | 72 | 91 | 83 | 8 | 40 | 0 | 0 | 0 | 65 | 47 | 82 | 40200 | 43 | 630 | 587 |
Sleep Analyzer Raw Data¶
[5]:
sleep_data = bp.example_data.get_sleep_analyzer_raw_example()
[6]:
display_dict_structure(sleep_data)
dict_keys(['2020-10-10', '2020-10-11', '2020-10-12', '2020-10-15', '2020-10-17', '2020-10-19', '2020-10-21', '2020-10-23'])
'Dataframe shape: (486, 4)'
heart_rate | respiration_rate | sleep_state | snoring | |
---|---|---|---|---|
time | ||||
2020-10-11 02:04:00+02:00 | 52.0 | 15.0 | 0.0 | 0.0 |
2020-10-11 02:05:00+02:00 | 61.0 | 9.0 | 0.0 | 0.0 |
2020-10-11 02:06:00+02:00 | 65.0 | 16.0 | 0.0 | 0.0 |
2020-10-11 02:07:00+02:00 | 66.0 | 17.0 | 0.0 | 0.0 |
2020-10-11 02:08:00+02:00 | 65.0 | 11.0 | 0.0 | 0.0 |
[7]:
for key in sleep_data:
display(pd.concat([sleep_data[key].head(n=1), sleep_data[key].tail(n=1)]))
heart_rate | respiration_rate | sleep_state | snoring | |
---|---|---|---|---|
time | ||||
2020-10-11 02:04:00+02:00 | 52.0 | 15.0 | 0.0 | 0.0 |
2020-10-11 10:09:00+02:00 | 65.0 | 17.0 | 0.0 | 0.0 |
heart_rate | respiration_rate | sleep_state | snoring | |
---|---|---|---|---|
time | ||||
2020-10-12 00:22:00+02:00 | 68.0 | 13.0 | 0.0 | 0.0 |
2020-10-12 07:14:00+02:00 | 70.0 | 18.0 | 1.0 | 0.0 |
heart_rate | respiration_rate | sleep_state | snoring | |
---|---|---|---|---|
time | ||||
2020-10-12 20:47:00+02:00 | 65.0 | 11.0 | 0.0 | 0.0 |
2020-10-13 07:20:00+02:00 | 57.0 | 20.0 | 1.0 | 0.0 |
heart_rate | respiration_rate | sleep_state | snoring | |
---|---|---|---|---|
time | ||||
2020-10-15 23:53:00+02:00 | 70.0 | 14.0 | 0.0 | 0.0 |
2020-10-16 08:01:00+02:00 | 50.0 | 12.0 | 0.0 | 0.0 |
heart_rate | respiration_rate | sleep_state | snoring | |
---|---|---|---|---|
time | ||||
2020-10-17 23:14:00+02:00 | 64.0 | 12.0 | 0.0 | 0.0 |
2020-10-18 10:45:00+02:00 | 58.0 | 11.0 | 0.0 | 0.0 |
heart_rate | respiration_rate | sleep_state | snoring | |
---|---|---|---|---|
time | ||||
2020-10-20 00:36:00+02:00 | 69.0 | 15.0 | 0.0 | 0.0 |
2020-10-20 16:19:00+02:00 | 61.0 | 11.0 | 0.0 | 0.0 |
heart_rate | respiration_rate | sleep_state | snoring | |
---|---|---|---|---|
time | ||||
2020-10-21 23:36:00+02:00 | 69.0 | 13.0 | 0.0 | 0.0 |
2020-10-22 08:35:00+02:00 | 65.0 | 20.0 | 0.0 | 0.0 |
heart_rate | respiration_rate | sleep_state | snoring | |
---|---|---|---|---|
time | ||||
2020-10-23 22:03:00+02:00 | 66.0 | 11.0 | 0.0 | 0.0 |
2020-10-24 10:33:00+02:00 | 64.0 | 10.0 | 0.0 | 0.0 |
[8]:
fig, ax = plt.subplots()
sleep_data["2020-10-17"].plot(ax=ax)
fig.tight_layout()
[ ]: