{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# How to read data from DataFrames with pyActigraphy" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Imported packages and input data" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The usual suspects:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "ExecuteTime": { "end_time": "2023-01-30T15:46:20.797012Z", "start_time": "2023-01-30T15:46:20.640738Z" } }, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "ExecuteTime": { "end_time": "2023-01-30T15:46:21.029765Z", "start_time": "2023-01-30T15:46:20.798230Z" } }, "outputs": [], "source": [ "import pandas as pd" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "ExecuteTime": { "end_time": "2023-01-30T15:46:22.362016Z", "start_time": "2023-01-30T15:46:21.031406Z" } }, "outputs": [], "source": [ "import pyActigraphy" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this example, let's generate some input data:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "NB: if you already have your data under a pandas.DataFrame format, jump directly to the next section." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "ExecuteTime": { "end_time": "2023-01-30T15:46:22.365103Z", "start_time": "2023-01-30T15:46:22.363452Z" } }, "outputs": [], "source": [ "N = 1440*7 # 7 days of acquisition at a frequency of 60s." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "ExecuteTime": { "end_time": "2023-01-30T15:46:22.370183Z", "start_time": "2023-01-30T15:46:22.366077Z" } }, "outputs": [], "source": [ "activity = np.random.normal(10,1,N)\n", "light = np.random.normal(100,10,N)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "ExecuteTime": { "end_time": "2023-01-30T15:46:22.373357Z", "start_time": "2023-01-30T15:46:22.371173Z" } }, "outputs": [], "source": [ "non_wear = np.empty(N)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "ExecuteTime": { "end_time": "2023-01-30T15:46:22.376814Z", "start_time": "2023-01-30T15:46:22.374774Z" } }, "outputs": [], "source": [ "# Set up a segment of spurious inactivity\n", "activity[2060:2160] = 0.0\n", "non_wear[2060:2160] = 1.0" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "ExecuteTime": { "end_time": "2023-01-30T15:46:22.382434Z", "start_time": "2023-01-30T15:46:22.380545Z" } }, "outputs": [], "source": [ "d = {'Activity': activity, 'Light': light, 'Non-wear': non_wear}" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "ExecuteTime": { "end_time": "2023-01-30T15:46:22.387956Z", "start_time": "2023-01-30T15:46:22.384391Z" } }, "outputs": [], "source": [ "index = pd.date_range(start='01-01-2020',freq='60s',periods=N)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "ExecuteTime": { "end_time": "2023-01-30T15:46:22.393274Z", "start_time": "2023-01-30T15:46:22.389782Z" } }, "outputs": [], "source": [ "data = pd.DataFrame(index=index,data=d)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "ExecuteTime": { "end_time": "2023-01-30T15:46:22.406545Z", "start_time": "2023-01-30T15:46:22.394383Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
ActivityLightNon-wear
2020-01-01 00:00:008.550218104.9999530.000000e+00
2020-01-01 00:01:0010.92339395.0885182.194245e-314
2020-01-01 00:02:008.144349103.8724274.052524e-319
2020-01-01 00:03:009.285714113.352058NaN
2020-01-01 00:04:0010.159414116.4006668.331307e-316
............
2020-01-07 23:55:009.421655102.2258612.072129e-309
2020-01-07 23:56:0010.57121497.8949707.900019e+305
2020-01-07 23:57:008.522185126.5303278.157913e-312
2020-01-07 23:58:0010.488261101.4074905.367234e-303
2020-01-07 23:59:0011.041405100.3725271.333605e+241
\n", "

10080 rows × 3 columns

\n", "
" ], "text/plain": [ " Activity Light Non-wear\n", "2020-01-01 00:00:00 8.550218 104.999953 0.000000e+00\n", "2020-01-01 00:01:00 10.923393 95.088518 2.194245e-314\n", "2020-01-01 00:02:00 8.144349 103.872427 4.052524e-319\n", "2020-01-01 00:03:00 9.285714 113.352058 NaN\n", "2020-01-01 00:04:00 10.159414 116.400666 8.331307e-316\n", "... ... ... ...\n", "2020-01-07 23:55:00 9.421655 102.225861 2.072129e-309\n", "2020-01-07 23:56:00 10.571214 97.894970 7.900019e+305\n", "2020-01-07 23:57:00 8.522185 126.530327 8.157913e-312\n", "2020-01-07 23:58:00 10.488261 101.407490 5.367234e-303\n", "2020-01-07 23:59:00 11.041405 100.372527 1.333605e+241\n", "\n", "[10080 rows x 3 columns]" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Manual creation of a BaseRaw object" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "ExecuteTime": { "end_time": "2023-01-30T15:46:22.410078Z", "start_time": "2023-01-30T15:46:22.407864Z" } }, "outputs": [], "source": [ "from pyActigraphy.io import BaseRaw" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "help(BaseRaw)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Set activity and light data (if available)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "ExecuteTime": { "end_time": "2023-01-30T15:46:22.414671Z", "start_time": "2023-01-30T15:46:22.411146Z" } }, "outputs": [], "source": [ "raw = BaseRaw(\n", " name=\"myName\", \n", " uuid='DeviceId', \n", " format='Pandas', \n", " axial_mode=None,\n", " start_time=data.index[0],\n", " period=(data.index[-1]-data.index[0]),\n", " frequency=data.index.freq,\n", " data=data['Activity'],\n", " light=data['Light']\n", ")" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "ExecuteTime": { "end_time": "2023-01-30T15:46:22.421343Z", "start_time": "2023-01-30T15:46:22.416023Z" } }, "outputs": [ { "data": { "text/plain": [ "2020-01-01 00:00:00 8.550218\n", "2020-01-01 00:01:00 10.923393\n", "2020-01-01 00:02:00 8.144349\n", "2020-01-01 00:03:00 9.285714\n", "2020-01-01 00:04:00 10.159414\n", " ... \n", "2020-01-07 23:55:00 9.421655\n", "2020-01-07 23:56:00 10.571214\n", "2020-01-07 23:57:00 8.522185\n", "2020-01-07 23:58:00 10.488261\n", "2020-01-07 23:59:00 11.041405\n", "Freq: 60S, Name: Activity, Length: 10080, dtype: float64" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "raw.data" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Set up a mask" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Most devices that have a wear sensor return this information as a binary time series with \"1\" when the device is most likely not worn and \"0\" otherwise.\n", "In pyActigraphy, this information can be used to create a mask and thus invalidate the corresponding data points (set to \"0\" most probably). However, the mask, the value \"1\" correspond to \"no masking\". So, depending on your \"non-wear\" data, be careful to transform them appropriately:" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "ExecuteTime": { "end_time": "2023-01-30T15:46:22.425960Z", "start_time": "2023-01-30T15:46:22.422846Z" } }, "outputs": [], "source": [ "# Here, I assume that 0: the device is worn, 1: device not worn. \n", "# As mentioned aboce, for the mask, 1: no masking. (NB: it is a convolution: data*mask)\n", "raw.mask = np.abs(data['Non-wear']-1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Tests" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "ExecuteTime": { "end_time": "2023-01-30T15:46:22.430376Z", "start_time": "2023-01-30T15:46:22.427707Z" } }, "outputs": [ { "data": { "text/plain": [ "<604800 * Seconds>" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "raw.duration()" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "ExecuteTime": { "end_time": "2023-01-30T15:46:22.435614Z", "start_time": "2023-01-30T15:46:22.431691Z" } }, "outputs": [ { "data": { "text/plain": [ "10080" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "raw.length()" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "ExecuteTime": { "end_time": "2023-01-30T15:46:22.464546Z", "start_time": "2023-01-30T15:46:22.436610Z" } }, "outputs": [ { "data": { "text/plain": [ "14232.551520242358" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "raw.ADAT(binarize=False)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "ExecuteTime": { "end_time": "2023-01-30T15:46:22.471681Z", "start_time": "2023-01-30T15:46:22.465690Z" } }, "outputs": [ { "data": { "text/plain": [ "1.0878760497043665" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "raw.IV(binarize=False)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "ExecuteTime": { "end_time": "2023-01-30T15:46:22.475334Z", "start_time": "2023-01-30T15:46:22.472677Z" } }, "outputs": [], "source": [ "# If you want to mask the data\n", "raw.mask_inactivity = True" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "ExecuteTime": { "end_time": "2023-01-30T15:46:22.489791Z", "start_time": "2023-01-30T15:46:22.477058Z" } }, "outputs": [ { "data": { "text/plain": [ "2.0276331847662488" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# For a gaussian noise, IV should be close to 2.\n", "raw.IV(binarize=False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The masking seems to work!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Et voilà!" ] } ], "metadata": { "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" } }, "nbformat": 4, "nbformat_minor": 2 }