{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Sleep fragmentation via state transition probability" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\"Quantification of the Fragmentation of Rest-Activity Patterns in Elderly Individuals Using a State Transition Analysis\"\n", "\n", "Andrew S.P. Lim, MD, Lei Yu, PhD, Madalena D. Costa, PhD, Aron S. Buchman, MD, David A. Bennett, MD, Sue E. Leurgans, PhD, Clifford B. Saper, MD, PhD\n", "\n", "\n", "Sleep, Volume 34, Issue 11, 1 November 2011, Pages 1569–1581, https://doi.org/10.5665/sleep.1400" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "ExecuteTime": { "end_time": "2023-01-30T15:48:23.247158Z", "start_time": "2023-01-30T15:48:21.469618Z" }, "scrolled": true }, "outputs": [], "source": [ "import pyActigraphy" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "ExecuteTime": { "end_time": "2023-01-30T15:48:23.250206Z", "start_time": "2023-01-30T15:48:23.248507Z" } }, "outputs": [], "source": [ "import plotly.graph_objs as go" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "ExecuteTime": { "end_time": "2023-01-30T15:48:23.268207Z", "start_time": "2023-01-30T15:48:23.251688Z" } }, "outputs": [], "source": [ "# create objects for layout and traces\n", "layout = go.Layout(title=\"\",xaxis=dict(title=\"\"), showlegend=False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Read an individual actigraphy file" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "ExecuteTime": { "end_time": "2023-01-30T15:48:23.273848Z", "start_time": "2023-01-30T15:48:23.271125Z" } }, "outputs": [], "source": [ "# retrieve path to example files\n", "import os\n", "fpath = os.path.join(os.path.dirname(pyActigraphy.__file__),'tests/data/')" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "ExecuteTime": { "end_time": "2023-01-30T15:48:23.294264Z", "start_time": "2023-01-30T15:48:23.275769Z" } }, "outputs": [], "source": [ "raw = pyActigraphy.io.read_raw_awd(fpath+'example_01.AWD', period='7 days')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Quantify the Rest-to-activity transition probability" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "help(raw.kRA)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Rest to activity (kRA)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Before calculating the 'kRA', let's look at the distribution of the transition probabilities as a function of the length of the sustained rest periods:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "ExecuteTime": { "end_time": "2023-01-30T15:48:23.342788Z", "start_time": "2023-01-30T15:48:23.295431Z" } }, "outputs": [], "source": [ "pRA, pRA_weights = raw.pRA(0, start='00:00:00', period='8H')" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "ExecuteTime": { "end_time": "2023-01-30T15:48:23.346492Z", "start_time": "2023-01-30T15:48:23.343854Z" } }, "outputs": [], "source": [ "layout.update(title=\"Rest->Activity transition probability\",xaxis=dict(title=\"Time [min]\"), showlegend=False);" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "ExecuteTime": { "end_time": "2023-01-30T15:48:23.460500Z", "start_time": "2023-01-30T15:48:23.347369Z" } }, "outputs": [ { "data": { "text/html": [ " \n", " " ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.plotly.v1+json": { "config": { "plotlyServerURL": "https://plot.ly" }, "data": [ { "mode": "markers", "name": "", "type": "scatter", "x": [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 28, 30, 31, 33, 37, 38, 41, 47, 54 ], "y": [ 0.17846153846153845, 0.1797752808988764, 0.1598173515981735, 0.15217391304347827, 0.16025641025641027, 0.13740458015267176, 0.1592920353982301, 0.11578947368421053, 0.15476190476190477, 0.11267605633802817, 0.14285714285714285, 0.16666666666666666, 0.15555555555555556, 0.02631578947368421, 0.10810810810810811, 0.12121212121212122, 0.06896551724137931, 0.037037037037037035, 0.11538461538461539, 0.13043478260869565, 0.1, 0.1111111111111111, 0.0625, 0.13333333333333333, 0.07692307692307693, 0.041666666666666664, 0.045454545454545456, 0.2, 0.0625, 0.03571428571428571, 0.16666666666666666, 0.06666666666666667, 0.041666666666666664, 0.047619047619047616, 0.00117096018735363 ] } ], "layout": { "showlegend": false, "template": { "data": { "bar": [ { "error_x": { "color": "#2a3f5f" }, "error_y": { "color": "#2a3f5f" }, "marker": { "line": { "color": "#E5ECF6", "width": 0.5 }, "pattern": { "fillmode": "overlay", "size": 10, "solidity": 0.2 } }, "type": "bar" } ], "barpolar": [ { "marker": { "line": { "color": "#E5ECF6", "width": 0.5 }, "pattern": { "fillmode": "overlay", "size": 10, "solidity": 0.2 } }, "type": "barpolar" } ], "carpet": [ { "aaxis": { "endlinecolor": "#2a3f5f", "gridcolor": "white", "linecolor": "white", "minorgridcolor": "white", "startlinecolor": "#2a3f5f" }, "baxis": { "endlinecolor": "#2a3f5f", "gridcolor": "white", "linecolor": "white", "minorgridcolor": "white", "startlinecolor": "#2a3f5f" }, "type": "carpet" } ], "choropleth": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "type": "choropleth" } ], "contour": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "contour" } ], "contourcarpet": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "type": "contourcarpet" } ], "heatmap": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "heatmap" } ], "heatmapgl": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "heatmapgl" } ], "histogram": [ { "marker": { "pattern": { "fillmode": "overlay", "size": 10, "solidity": 0.2 } }, "type": "histogram" } ], "histogram2d": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "histogram2d" } ], "histogram2dcontour": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "histogram2dcontour" } ], "mesh3d": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "type": "mesh3d" } ], "parcoords": [ { "line": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "parcoords" } ], "pie": [ { "automargin": true, "type": "pie" } ], "scatter": [ { "fillpattern": { "fillmode": "overlay", "size": 10, "solidity": 0.2 }, "type": "scatter" } ], "scatter3d": [ { "line": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scatter3d" } ], "scattercarpet": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scattercarpet" } ], "scattergeo": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scattergeo" } ], "scattergl": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scattergl" } ], "scattermapbox": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scattermapbox" } ], "scatterpolar": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scatterpolar" } ], "scatterpolargl": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scatterpolargl" } ], "scatterternary": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scatterternary" } ], "surface": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "surface" } ], "table": [ { "cells": { "fill": { "color": "#EBF0F8" }, "line": { "color": "white" } }, "header": { "fill": { "color": "#C8D4E3" }, "line": { "color": "white" } }, "type": "table" } ] }, "layout": { "annotationdefaults": { "arrowcolor": "#2a3f5f", "arrowhead": 0, "arrowwidth": 1 }, "autotypenumbers": "strict", "coloraxis": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "colorscale": { "diverging": [ [ 0, "#8e0152" ], [ 0.1, "#c51b7d" ], [ 0.2, "#de77ae" ], [ 0.3, "#f1b6da" ], [ 0.4, "#fde0ef" ], [ 0.5, "#f7f7f7" ], [ 0.6, "#e6f5d0" ], [ 0.7, "#b8e186" ], [ 0.8, "#7fbc41" ], [ 0.9, "#4d9221" ], [ 1, "#276419" ] ], "sequential": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "sequentialminus": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ] }, "colorway": [ "#636efa", "#EF553B", "#00cc96", "#ab63fa", "#FFA15A", "#19d3f3", "#FF6692", "#B6E880", "#FF97FF", "#FECB52" ], "font": { "color": "#2a3f5f" }, "geo": { "bgcolor": "white", "lakecolor": "white", "landcolor": "#E5ECF6", "showlakes": true, "showland": true, "subunitcolor": "white" }, "hoverlabel": { "align": "left" }, "hovermode": "closest", "mapbox": { "style": "light" }, "paper_bgcolor": "white", "plot_bgcolor": "#E5ECF6", "polar": { "angularaxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" }, "bgcolor": "#E5ECF6", "radialaxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" } }, "scene": { "xaxis": { "backgroundcolor": "#E5ECF6", "gridcolor": "white", "gridwidth": 2, "linecolor": "white", "showbackground": true, "ticks": "", "zerolinecolor": "white" }, "yaxis": { "backgroundcolor": "#E5ECF6", "gridcolor": "white", "gridwidth": 2, "linecolor": "white", "showbackground": true, "ticks": "", "zerolinecolor": "white" }, "zaxis": { "backgroundcolor": "#E5ECF6", "gridcolor": "white", "gridwidth": 2, "linecolor": "white", "showbackground": true, "ticks": "", "zerolinecolor": "white" } }, "shapedefaults": { "line": { "color": "#2a3f5f" } }, "ternary": { "aaxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" }, "baxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" }, "bgcolor": "#E5ECF6", "caxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" } }, "title": { "x": 0.05 }, "xaxis": { "automargin": true, "gridcolor": "white", "linecolor": "white", "ticks": "", "title": { "standoff": 15 }, "zerolinecolor": "white", "zerolinewidth": 2 }, "yaxis": { "automargin": true, "gridcolor": "white", "linecolor": "white", "ticks": "", "title": { "standoff": 15 }, "zerolinecolor": "white", "zerolinewidth": 2 } } }, "title": { "text": "Rest->Activity transition probability" }, "xaxis": { "title": { "text": "Time [min]" } } } }, "text/html": [ "
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "go.Figure(data=go.Scatter(x=pRA.index, y=pRA, name='', mode = 'markers'), layout=layout)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\"The values kAR and kRA are metrics of the transition probabilities once sustained activity or rest have been attained\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In **_pyActigraphy_**, it is as simple as:" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "ExecuteTime": { "end_time": "2023-01-30T15:48:23.545940Z", "start_time": "2023-01-30T15:48:23.462969Z" } }, "outputs": [ { "data": { "text/plain": [ "0.12337472433074494" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "raw.kRA(0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To use the kRA (Rest-to-activity) as a a mesure of sleep fragmentation, it might be interesting to restrict the data to specific time windows (i.e. the night):" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "ExecuteTime": { "end_time": "2023-01-30T15:48:23.607303Z", "start_time": "2023-01-30T15:48:23.547184Z" } }, "outputs": [ { "data": { "text/plain": [ "0.1657160156790255" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "raw.kRA(0, start='22:00:00', period='8H')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "However, it might be tedious to estimate manually the correct time window." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The **_pyActigraphy_** package provide the users with a simple way to do this automatically;\n", "\n", "it relies on the activity onset and offset times derived from the daily activity profile." ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "ExecuteTime": { "end_time": "2023-01-30T15:48:23.697269Z", "start_time": "2023-01-30T15:48:23.608461Z" }, "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "0.15845745364361488" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "raw.kRA(0, start='AoffT', freq='15min')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Activity to rest (kAR)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "ExecuteTime": { "end_time": "2023-01-30T15:48:23.795078Z", "start_time": "2023-01-30T15:48:23.698882Z" } }, "outputs": [ { "data": { "text/plain": [ "0.03592473838340913" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "raw.kAR(0)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "ExecuteTime": { "end_time": "2023-01-30T15:48:23.840296Z", "start_time": "2023-01-30T15:48:23.797238Z" } }, "outputs": [ { "data": { "text/plain": [ "0.03431133223136525" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "raw.kAR(0, start='AonT', freq='15min')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Et voilà! Easy, isn't it?" ] } ], "metadata": { "celltoolbar": "Raw Cell Format", "kernelspec": { "display_name": "pyActi37", "language": "python", "name": "pyacti37" }, "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.7.12" }, "latex_envs": { "LaTeX_envs_menu_present": true, "autoclose": false, "autocomplete": true, "bibliofile": "biblio.bib", "cite_by": "apalike", "current_citInitial": 1, "eqLabelWithNumbers": true, "eqNumInitial": 1, "hotkeys": { "equation": "Ctrl-E", "itemize": "Ctrl-I" }, "labels_anchors": false, "latex_user_defs": false, "report_style_numbering": false, "user_envs_cfg": false }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": true, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": true, "toc_position": {}, "toc_section_display": true, "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 2 }