{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Sleep Analyzer Example" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "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.\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Setup and Helper Functions" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from pathlib import Path\n", "\n", "import re\n", "\n", "import pandas as pd\n", "import numpy as np\n", "\n", "from fau_colors import cmaps\n", "import biopsykit as bp\n", "\n", "import matplotlib.pyplot as plt\n", "import seaborn as sns\n", "\n", "%matplotlib widget\n", "%load_ext autoreload\n", "%autoreload 2" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.close(\"all\")\n", "\n", "palette = sns.color_palette(cmaps.faculties)\n", "sns.set_theme(context=\"notebook\", style=\"ticks\", font=\"sans-serif\", palette=palette)\n", "\n", "plt.rcParams[\"figure.figsize\"] = (8, 4)\n", "plt.rcParams[\"pdf.fonttype\"] = 42\n", "plt.rcParams[\"mathtext.default\"] = \"regular\"\n", "\n", "palette" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def display_dict_structure(dict_to_display):\n", " _display_dict_recursive(dict_to_display)\n", "\n", "\n", "def _display_dict_recursive(dict_to_display):\n", " if isinstance(dict_to_display, dict):\n", " display(dict_to_display.keys())\n", " _display_dict_recursive(list(dict_to_display.values())[0])\n", " else:\n", " display(\"Dataframe shape: {}\".format(dict_to_display.shape))\n", " display(dict_to_display.head())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Sleep Analyzer Summary" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sleep_summary = bp.example_data.get_sleep_analyzer_summary_example()\n", "sleep_summary.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Sleep Analyzer Raw Data" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sleep_data = bp.example_data.get_sleep_analyzer_raw_example()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "display_dict_structure(sleep_data)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for key in sleep_data:\n", " display(pd.concat([sleep_data[key].head(n=1), sleep_data[key].tail(n=1)]))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "nbsphinx-thumbnail" ] }, "outputs": [], "source": [ "fig, ax = plt.subplots()\n", "sleep_data[\"2020-10-17\"].plot(ax=ax)\n", "fig.tight_layout()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "biopsykit", "language": "python", "name": "biopsykit" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.3" } }, "nbformat": 4, "nbformat_minor": 4 }