{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "# Slow waves analysis" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Introductory notes:\n", "This notebook presents slow waves detection functionality.\n", "\n", "Recommended reading:\n", "1. [YASA: FAQ](https://raphaelvallat.com/yasa/build/html/faq.html#event-detection)\n", "2. [YASA: Slow-waves detection](https://github.com/raphaelvallat/yasa/blob/master/notebooks/05_sw_detection.ipynb)\n", "3. [YASA: Multi-channel slow-wave detection](https://github.com/raphaelvallat/yasa/blob/master/notebooks/06_sw_detection_multi.ipynb)\n", "4. [YASA: SWResults class](https://raphaelvallat.com/yasa/build/html/generated/yasa.SWResults.html#yasa-swresults)\n", "5. [MNE: Time-frequency analysis: power and inter-trial coherence](https://mne.tools/stable/auto_tutorials/time-freq/20_sensors_time_frequency.html#time-frequency-analysis-power-and-inter-trial-coherence)\n", "6. [MNE: AverageTFR class](https://mne.tools/stable/generated/mne.time_frequency.AverageTFR.html#mne-time-frequency-averagetfr)" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Import module" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2024-08-06T08:00:43.144666Z", "start_time": "2024-08-06T08:00:41.978518Z" } }, "outputs": [], "source": [ "from sleepeegpy.pipeline import SlowWavesPipe\n", "from joblib import parallel_backend; parallel_backend(\"loky\", n_jobs=-1)\n", "from os import makedirs,path" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Slow waves 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:00:46.741183Z", "start_time": "2024-08-06T08:00:46.738557Z" } }, "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:00:47.978976Z", "start_time": "2024-08-06T08:00:47.975097Z" } }, "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:00:51.738952Z", "start_time": "2024-08-06T08:00:48.968998Z" } }, "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", "slow_waves_pipe = SlowWavesPipe(\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.sw_detect.html) for details about the arguments." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2024-08-06T08:00:57.128636Z", "start_time": "2024-08-06T08:00:54.279879Z" } }, "outputs": [], "source": [ "slow_waves_pipe.detect(\n", " picks=[\"eeg\"],\n", " reference=\"average\",\n", " include=(2, 3),\n", " freq_sw=(0.3, 1.5),\n", " dur_neg=(0.3, 1.5),\n", " dur_pos=(0.1, 1),\n", " amp_neg=(40, 200),\n", " amp_pos=(10, 150),\n", " amp_ptp=(75, 350),\n", " coupling=False,\n", " coupling_params={\"freq_sp\": (12, 16), \"p\": 0.05, \"time\": 1},\n", " remove_outliers=False,\n", " verbose=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.SWResults.html) for details about the arguments." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2024-08-06T08:01:03.567177Z", "start_time": "2024-08-06T08:01:03.551239Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " | Count | \n", "Density | \n", "Duration | \n", "ValNegPeak | \n", "ValPosPeak | \n", "PTP | \n", "Slope | \n", "Frequency | \n", "
---|---|---|---|---|---|---|---|---|
Stage | \n", "\n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " |
2 | \n", "7617 | \n", "69.561644 | \n", "1.278879 | \n", "-68.369328 | \n", "39.043891 | \n", "107.413219 | \n", "364.222711 | \n", "0.826626 | \n", "
3 | \n", "132955 | \n", "696.099476 | \n", "1.259195 | \n", "-61.891508 | \n", "46.102154 | \n", "107.993663 | \n", "379.508453 | \n", "0.843116 | \n", "
\n", " | time | \n", "freq | \n", "E1 | \n", "E10 | \n", "E100 | \n", "E101 | \n", "E102 | \n", "E103 | \n", "E104 | \n", "E105 | \n", "... | \n", "E91 | \n", "E92 | \n", "E93 | \n", "E94 | \n", "E95 | \n", "E96 | \n", "E97 | \n", "E98 | \n", "E99 | \n", "VREF | \n", "
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | \n", "-4.000 | \n", "0.5 | \n", "8327.174792 | \n", "24202.707094 | \n", "48924.736669 | \n", "39136.709413 | \n", "16565.769149 | \n", "21032.257004 | \n", "16297.561439 | \n", "14364.987680 | \n", "... | \n", "18311.186857 | \n", "11788.543531 | \n", "10151.399608 | \n", "813004.188548 | \n", "6635.561922 | \n", "9187.194787 | \n", "128842.213203 | \n", "212463.813057 | \n", "23333.863418 | \n", "23821.186402 | \n", "
1 | \n", "-3.996 | \n", "0.5 | \n", "8409.258903 | \n", "24399.368859 | \n", "49281.102763 | \n", "39406.336499 | \n", "16708.449758 | \n", "21202.065016 | \n", "16443.286671 | \n", "14494.296028 | \n", "... | \n", "18444.541127 | \n", "11887.000917 | \n", "10243.375095 | \n", "826935.307053 | \n", "6700.255489 | \n", "9311.619642 | \n", "130066.366501 | \n", "214330.539209 | \n", "23559.758544 | \n", "23974.795983 | \n", "
2 | \n", "-3.992 | \n", "0.5 | \n", "8491.942878 | \n", "24596.372510 | \n", "49637.322267 | \n", "39675.597990 | \n", "16851.524047 | \n", "21372.206949 | \n", "16589.601708 | \n", "14624.177349 | \n", "... | \n", "18577.913438 | \n", "11985.771994 | \n", "10335.765080 | \n", "841068.344747 | \n", "6765.308494 | \n", "9437.310772 | \n", "131292.465082 | \n", "216200.683458 | \n", "23786.637260 | \n", "24127.883242 | \n", "
3 | \n", "-3.988 | \n", "0.5 | \n", "8575.230882 | \n", "24793.703990 | \n", "49993.360119 | \n", "39944.465872 | \n", "16994.982900 | \n", "21542.672288 | \n", "16736.502345 | \n", "14754.629653 | \n", "... | \n", "18711.293518 | \n", "12084.854022 | \n", "10428.569612 | \n", "855405.572419 | \n", "6830.719970 | \n", "9564.273811 | \n", "132520.345609 | \n", "218074.077555 | \n", "24014.490320 | \n", "24280.428790 | \n", "
4 | \n", "-3.984 | \n", "0.5 | \n", "8659.127428 | \n", "24991.348688 | \n", "50349.181273 | \n", "40212.912122 | \n", "17138.817163 | \n", "21713.450432 | \n", "16883.984341 | \n", "14885.651000 | \n", "... | \n", "18844.671407 | \n", "12184.245884 | \n", "10521.787122 | \n", "869949.275252 | \n", "6896.489029 | \n", "9692.514330 | \n", "133749.841216 | \n", "219950.553685 | \n", "24243.307984 | \n", "24432.413277 | \n", "
... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "
200095 | \n", "3.984 | \n", "5.0 | \n", "3429.901500 | \n", "11262.392110 | \n", "8186.597511 | \n", "9503.453968 | \n", "8531.528837 | \n", "6867.098126 | \n", "4830.527870 | \n", "2719.352112 | \n", "... | \n", "7957.036605 | \n", "4867.406478 | \n", "1872.140713 | \n", "6163.467729 | \n", "8182.874264 | \n", "42196.721683 | \n", "2753.857842 | \n", "2645.100466 | \n", "6920.765242 | \n", "3162.336643 | \n", "
200096 | \n", "3.988 | \n", "5.0 | \n", "3314.909209 | \n", "11047.257251 | \n", "7949.896955 | \n", "9172.306963 | \n", "8194.028659 | \n", "6609.685367 | \n", "4635.594985 | \n", "2607.184777 | \n", "... | \n", "7734.217197 | \n", "4668.399175 | \n", "1808.386104 | \n", "6018.772218 | \n", "7918.656800 | \n", "41436.814301 | \n", "2675.732279 | \n", "2570.668662 | \n", "6734.209980 | \n", "3021.996150 | \n", "
200097 | \n", "3.992 | \n", "5.0 | \n", "3185.542215 | \n", "10759.126606 | \n", "7670.569782 | \n", "8801.089724 | \n", "7824.249030 | \n", "6324.156979 | \n", "4425.367535 | \n", "2485.573841 | \n", "... | \n", "7469.392018 | \n", "4450.939737 | \n", "1736.459351 | \n", "5839.955716 | \n", "7613.461919 | \n", "40400.796093 | \n", "2582.723509 | \n", "2482.027288 | \n", "6506.794214 | \n", "2871.973323 | \n", "
200098 | \n", "3.996 | \n", "5.0 | \n", "3043.183281 | \n", "10403.328748 | \n", "7352.799803 | \n", "8394.451726 | \n", "7426.868327 | \n", "6014.294154 | \n", "4201.815750 | \n", "2355.866277 | \n", "... | \n", "7166.566486 | \n", "4217.945319 | \n", "1657.303549 | \n", "5629.285089 | \n", "7272.190910 | \n", "39108.043828 | \n", "2476.376797 | \n", "2380.632657 | \n", "6242.906538 | \n", "2713.981375 | \n", "
200099 | \n", "4.000 | \n", "5.0 | \n", "2889.528419 | \n", "9986.653453 | \n", "7001.555157 | \n", "7957.730378 | \n", "7007.049233 | \n", "5684.317901 | \n", "3967.230976 | \n", "2219.598580 | \n", "... | \n", "6830.508999 | \n", "3972.575648 | \n", "1571.995078 | \n", "5389.834376 | \n", "6900.349854 | \n", "37583.368617 | \n", "2358.483433 | \n", "2268.170865 | \n", "5947.554626 | \n", "2549.860924 | \n", "
200100 rows × 259 columns
\n", "