{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# How to read raw files by batch" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Visual inspections of individual files are usually useful to detect problems that occurred during the acquisition. However, at the analysis level, it is convenient to read files, and calculate the variables of interest, by batch." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In `pyActigraphy`, there is a dedicated class to read files by batch: [RawReader](https://ghammad.github.io/pyActigraphy/RawReader.html#pyActigraphy.io.RawReader). Objects from that class can easily be instanciated using the [read_raw](https://ghammad.github.io/pyActigraphy/_autosummary/pyActigraphy.io.read_raw.html#pyActigraphy.io.read_raw) function." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's see now how to use it." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Define imported packages and file path" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "ExecuteTime": { "end_time": "2023-01-30T15:41:46.327155Z", "start_time": "2023-01-30T15:41:44.633568Z" } }, "outputs": [], "source": [ "import pyActigraphy" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "ExecuteTime": { "end_time": "2023-01-30T15:41:46.330210Z", "start_time": "2023-01-30T15:41:46.328528Z" } }, "outputs": [], "source": [ "import os" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "ExecuteTime": { "end_time": "2023-01-30T15:41:46.333405Z", "start_time": "2023-01-30T15:41:46.331641Z" } }, "outputs": [], "source": [ "# retrieve path to example files\n", "fpath = os.path.join(os.path.dirname(pyActigraphy.__file__),'tests/data/')" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "ExecuteTime": { "end_time": "2023-01-30T15:41:46.336437Z", "start_time": "2023-01-30T15:41:46.334806Z" } }, "outputs": [], "source": [ "import plotly.graph_objs as go" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Read files by batch" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As raw file extensions (such as .csv or .txt) do not allow to infer the type of device used to acquire the data, it is necessary to specify the \"reader type\" upon reading the files:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "ExecuteTime": { "end_time": "2023-01-30T15:41:46.428326Z", "start_time": "2023-01-30T15:41:46.337386Z" } }, "outputs": [], "source": [ "# Read test files\n", "raw = pyActigraphy.io.read_raw(fpath+'example_0*.AWD', reader_type='AWD')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the example above, we use a wildcard \"*\"; it substitutes any character and it is a convenient way to specify multiple files at once. Any file whose name contains \"example_0\", with a \".AWD\" extension will be read. More info on wildcards: [Wikipedia](https://en.wikipedia.org/wiki/Wildcard_character)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `raw` object that has been instanciated contains a list of objects that one would have otherwise had to create by hand using individual file functions such as `read_raw_awd`." ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "ExecuteTime": { "end_time": "2023-01-30T15:41:46.433922Z", "start_time": "2023-01-30T15:41:46.429362Z" } }, "outputs": [ { "data": { "text/plain": [ "6" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Check how many files have been read\n", "len(raw.readers)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Indeed, there are 6 example files of type '.AWD' in the test data directory of the `pyActigraphy`:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "ExecuteTime": { "end_time": "2023-01-30T15:41:46.439849Z", "start_time": "2023-01-30T15:41:46.436095Z" } }, "outputs": [ { "data": { "text/plain": [ "['example_04',\n", " 'example_05',\n", " 'example_02',\n", " 'example_03',\n", " 'example_01',\n", " 'example_01_mask']" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "raw.names()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Analyse files by batch" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There are two ways to analyse files that have been read by batch:\n", "\n", "* the needed function already exists at the `RawReader` class level\n", "* the needed function does not exist and one needs to loop over the files" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Direct analysis" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For convenience, a certain number of functions, available at the individual reader level, have been extended to the `RawReader` class." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For example:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "ExecuteTime": { "end_time": "2023-01-30T15:41:46.449351Z", "start_time": "2023-01-30T15:41:46.442320Z" } }, "outputs": [ { "data": { "text/plain": [ "{'example_04': Timedelta('21 days 17:39:00'),\n", " 'example_05': Timedelta('15 days 01:43:00'),\n", " 'example_02': Timedelta('12 days 18:53:00'),\n", " 'example_03': Timedelta('14 days 21:36:00'),\n", " 'example_01': Timedelta('12 days 18:41:00'),\n", " 'example_01_mask': Timedelta('12 days 18:41:00')}" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Check the duration of the recording\n", "raw.duration()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "These functions return a dictionary whose keys are the individual file names and values are the required outputs (here, the duration of the individual recordings)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Most rest-activity rhythm related variables are available:" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "ExecuteTime": { "end_time": "2023-01-30T15:41:46.483322Z", "start_time": "2023-01-30T15:41:46.451156Z" } }, "outputs": [ { "data": { "text/plain": [ "{'example_04': 0.2551624866267735,\n", " 'example_05': 0.6595438549841431,\n", " 'example_02': 0.5237758757150337,\n", " 'example_03': 0.3720781873681576,\n", " 'example_01': 0.527656245354158,\n", " 'example_01_mask': 0.44565929945478583}" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "raw.IS()" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "ExecuteTime": { "end_time": "2023-01-30T15:41:47.201192Z", "start_time": "2023-01-30T15:41:46.484579Z" } }, "outputs": [ { "data": { "text/plain": [ "{'example_04': 0.03815016462875074,\n", " 'example_05': 0.030282109908435897,\n", " 'example_02': 0.026240794466679603,\n", " 'example_03': 0.023099082226598566,\n", " 'example_01': 0.033762534539447164,\n", " 'example_01_mask': 0.036345931354120085}" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "raw.kAR(0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Manual analysis" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As already mentioned, the \"raw\" object is a container. It is possible to simply loop over the objects contained in the \"raw\" object:" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "ExecuteTime": { "end_time": "2023-01-30T15:41:47.206264Z", "start_time": "2023-01-30T15:41:47.202292Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Object type: . Name: example_04. Duration of the recording: 21 days 17:39:00. Number of acquisition points: 31299\n", "Object type: . Name: example_05. Duration of the recording: 15 days 01:43:00. Number of acquisition points: 21703\n", "Object type: . Name: example_02. Duration of the recording: 12 days 18:53:00. Number of acquisition points: 18413\n", "Object type: . Name: example_03. Duration of the recording: 14 days 21:36:00. Number of acquisition points: 21456\n", "Object type: . Name: example_01. Duration of the recording: 12 days 18:41:00. Number of acquisition points: 18401\n", "Object type: . Name: example_01_mask. Duration of the recording: 12 days 18:41:00. Number of acquisition points: 18401\n" ] } ], "source": [ "for iread in raw.readers:\n", " print(\"Object type: {}. Name: {}. Duration of the recording: {}. Number of acquisition points: {}\".format(type(iread),iread.name,iread.duration(),len(iread.data)))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "So, for a more advanced analysis of the actigraphy recordings, simply replace the \"print\" statement by our analysis code." ] }, { "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 }