{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "# Rapid eye movements analysis" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Introductory notes:\n", "This notebook presents REMs detection functionality.\n", "\n", "Recommended reading:\n", "1. [YASA: FAQ](https://raphaelvallat.com/yasa/build/html/faq.html#event-detection)\n", "2. [YASA: Rapid Eye Movements (REMs) detection](https://github.com/raphaelvallat/yasa/blob/master/notebooks/07_REMs_detection.ipynb)\n", "3. [YASA: REMResults class](https://raphaelvallat.com/yasa/build/html/generated/yasa.REMResults.html#yasa.REMResults)" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Import module" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2024-08-06T08:14:08.680302Z", "start_time": "2024-08-06T08:14:08.665883Z" } }, "outputs": [], "source": [ "from sleepeegpy.pipeline import RapidEyeMovementsPipe\n", "from os import makedirs,path" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## REMs detection" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you wish to change the path for output_dir ot input dir, change it below.\n", "If no such folders, they will be created automatically." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2024-08-06T08:13:39.962293Z", "start_time": "2024-08-06T08:13:39.958601Z" } }, "outputs": [], "source": [ "output_dir = \"output_folder\" # Output path and name can be changed here\n", "input_dir = \"input_files\" # input files dir can be changed here\n", "makedirs(input_dir, exist_ok=True)\n", "makedirs(output_dir, exist_ok=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Add required files and data\n", "* Put all your files in the input folder.\n", "* Modify your eeg file name below. The file can be any format supported by the mne.read_raw() function.\n", "* Modify your hypnogram file name below\n", "* Make sure the hypno_freq is the right frequency.\n", "* For more information about the supported formats, see [mne documentation](https://mne.tools/stable/generated/mne.io.Raw.html)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2024-08-06T08:13:42.518760Z", "start_time": "2024-08-06T08:13:42.515322Z" } }, "outputs": [], "source": [ "eeg_file_name= \"resampled_raw.fif\" #None # add your eeg_path here\n", "hypnogram_filename = \"staging.txt\" # Hypnogram filename can be changed here (file must be in the input dir)\n", "hypno_freq = 1" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2024-08-06T08:13:45.998074Z", "start_time": "2024-08-06T08:13:43.208440Z" } }, "outputs": [], "source": [ "path_to_eeg = path.join(input_dir,eeg_file_name)\n", "hypnogram_path = path.join(input_dir,hypnogram_filename)\n", "\n", "assert path.isfile(path_to_eeg) and path.isfile(hypnogram_path), f\"{path_to_eeg} or {hypnogram_path} not exist\"\n", "rems_pipe = RapidEyeMovementsPipe(\n", " path_to_eeg=path_to_eeg,\n", " output_dir=output_dir,\n", " path_to_hypno=hypnogram_path,\n", " hypno_freq=hypno_freq,\n", ")" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "Please refer to the YASA's [documentation](https://raphaelvallat.com/yasa/build/html/generated/yasa.rem_detect.html) for details about the arguments." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2024-08-06T08:13:52.198217Z", "start_time": "2024-08-06T08:13:49.549609Z" } }, "outputs": [], "source": [ "rems_pipe.detect(\n", " reference=\"average\",\n", " loc_chname=\"E46\",\n", " roc_chname=\"E238\",\n", " include=4,\n", " freq_rem=(0.5, 5),\n", " duration=(0.3, 1.2),\n", " amplitude=(50, 325),\n", " remove_outliers=False,\n", " save=True,\n", ")" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "Please refer to the YASA's [documentation](https://raphaelvallat.com/yasa/build/html/generated/yasa.REMResults.html#yasa.REMResults.summary) for details about the arguments." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2024-08-06T08:13:55.786477Z", "start_time": "2024-08-06T08:13:55.770089Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " | Start | \n", "Peak | \n", "End | \n", "Duration | \n", "LOCAbsValPeak | \n", "ROCAbsValPeak | \n", "LOCAbsRiseSlope | \n", "ROCAbsRiseSlope | \n", "LOCAbsFallSlope | \n", "ROCAbsFallSlope | \n", "Stage | \n", "
---|---|---|---|---|---|---|---|---|---|---|---|
0 | \n", "11050.544 | \n", "11050.904 | \n", "11051.096 | \n", "0.552 | \n", "95.514371 | \n", "30.423478 | \n", "130.506125 | \n", "69.111908 | \n", "567.494181 | \n", "147.051950 | \n", "4 | \n", "
1 | \n", "11094.860 | \n", "11095.436 | \n", "11095.712 | \n", "0.852 | \n", "112.146842 | \n", "40.051066 | \n", "200.957633 | \n", "50.196786 | \n", "420.903964 | \n", "135.024205 | \n", "4 | \n", "
2 | \n", "11095.964 | \n", "11096.508 | \n", "11097.108 | \n", "1.144 | \n", "83.861166 | \n", "33.723120 | \n", "64.100500 | \n", "46.741223 | \n", "186.273207 | \n", "46.131956 | \n", "4 | \n", "
3 | \n", "11482.476 | \n", "11482.772 | \n", "11483.136 | \n", "0.660 | \n", "59.031067 | \n", "45.284152 | \n", "75.523086 | \n", "237.193022 | \n", "142.850213 | \n", "153.438315 | \n", "4 | \n", "
4 | \n", "15971.100 | \n", "15971.276 | \n", "15971.560 | \n", "0.460 | \n", "61.745377 | \n", "60.780729 | \n", "572.829439 | \n", "183.557890 | \n", "102.135926 | \n", "308.358956 | \n", "4 | \n", "
... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "
80 | \n", "23049.292 | \n", "23049.600 | \n", "23049.904 | \n", "0.612 | \n", "104.243540 | \n", "28.876271 | \n", "330.751289 | \n", "95.426070 | \n", "314.121445 | \n", "92.583937 | \n", "4 | \n", "
81 | \n", "23056.604 | \n", "23057.204 | \n", "23057.520 | \n", "0.916 | \n", "77.727534 | \n", "32.451615 | \n", "58.396074 | \n", "63.557268 | \n", "256.425427 | \n", "99.318708 | \n", "4 | \n", "
82 | \n", "23075.852 | \n", "23076.452 | \n", "23076.592 | \n", "0.740 | \n", "116.082740 | \n", "33.233345 | \n", "120.322005 | \n", "19.386448 | \n", "859.366479 | \n", "219.808008 | \n", "4 | \n", "
83 | \n", "23076.672 | \n", "23077.272 | \n", "23077.544 | \n", "0.872 | \n", "129.179400 | \n", "56.446968 | \n", "116.311652 | \n", "61.487780 | \n", "395.069480 | \n", "239.110792 | \n", "4 | \n", "
84 | \n", "23077.856 | \n", "23078.224 | \n", "23078.712 | \n", "0.856 | \n", "112.166788 | \n", "44.556604 | \n", "274.326404 | \n", "129.250414 | \n", "220.010889 | \n", "106.752248 | \n", "4 | \n", "
85 rows × 11 columns
\n", "