{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "# Spindles analysis" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Introductory notes:\n", "This notebook presents spindles detection and analysis functionality.\n", "\n", "Recommended reading:\n", "1. [YASA: FAQ](https://raphaelvallat.com/yasa/build/html/faq.html#event-detection)\n", "2. [YASA: Spindles detection on specific sleep stages](https://github.com/raphaelvallat/yasa/blob/master/notebooks/03_spindles_detection_NREM_only.ipynb)\n", "3. [YASA: SpindlesResults class](https://raphaelvallat.com/yasa/build/html/generated/yasa.SpindlesResults.html#yasa.SpindlesResults)\n", "4. [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", "5. [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": {}, "outputs": [], "source": [ "from sleepeegpy.pipeline import SpindlesPipe\n", "from joblib import parallel_backend\n", "\n", "parallel_backend(\"loky\", n_jobs=-1)\n", "from os import makedirs, path" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Spindles 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": {}, "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": {}, "outputs": [], "source": [ "eeg_file_name = \"resampled_raw.fif\" # 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": {}, "outputs": [], "source": [ "path_to_eeg = path.join(input_dir, eeg_file_name)\n", "hypnogram_path = path.join(input_dir, hypnogram_filename)\n", "spindles_pipe = SpindlesPipe(\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.spindles_detect.html) for details about the arguments." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "jupyter": { "is_executing": true } }, "outputs": [], "source": [ "spindles_pipe.detect(\n", " picks=[\"eeg\"],\n", " reference=\"average\",\n", " include=(2, 3),\n", " freq_sp=(12, 15),\n", " freq_broad=(1, 30),\n", " duration=(0.5, 2),\n", " min_distance=500,\n", " thresh={\"corr\": 0.65, \"rel_pow\": 0.2, \"rms\": 1.5},\n", " multi_only=False,\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.SpindlesResults.html#yasa.SpindlesResults.summary) for details about the arguments." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " | Count | \n", "Density | \n", "Duration | \n", "Amplitude | \n", "RMS | \n", "AbsPower | \n", "RelPower | \n", "Frequency | \n", "Oscillations | \n", "Symmetry | \n", "
---|---|---|---|---|---|---|---|---|---|---|
Stage | \n", "\n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " |
2 | \n", "48125 | \n", "439.497717 | \n", "0.802510 | \n", "35.844591 | \n", "7.849986 | \n", "1.739719 | \n", "0.402655 | \n", "13.149206 | \n", "10.165049 | \n", "0.513328 | \n", "
3 | \n", "22294 | \n", "116.722513 | \n", "0.751883 | \n", "40.712861 | \n", "9.045872 | \n", "1.862345 | \n", "0.384414 | \n", "13.194922 | \n", "9.552705 | \n", "0.516585 | \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", "-1.000 | \n", "10.0 | \n", "165.443858 | \n", "183.038525 | \n", "357.247596 | \n", "407.714768 | \n", "367.061131 | \n", "403.271235 | \n", "439.417678 | \n", "457.963162 | \n", "... | \n", "191.327237 | \n", "191.225602 | \n", "181.691803 | \n", "208.996170 | \n", "198.116414 | \n", "177.822697 | \n", "192.377679 | \n", "169.344080 | \n", "137.058994 | \n", "431.558397 | \n", "
1 | \n", "-0.996 | \n", "10.0 | \n", "169.920365 | \n", "188.051328 | \n", "368.793126 | \n", "421.133352 | \n", "377.604580 | \n", "416.300607 | \n", "454.302290 | \n", "473.374693 | \n", "... | \n", "197.382219 | \n", "197.558606 | \n", "188.066255 | \n", "215.492143 | \n", "204.044079 | \n", "183.797165 | \n", "198.590959 | \n", "175.259107 | \n", "142.024406 | \n", "445.426355 | \n", "
2 | \n", "-0.992 | \n", "10.0 | \n", "174.239568 | \n", "192.901037 | \n", "380.086587 | \n", "434.450949 | \n", "387.836302 | \n", "429.071681 | \n", "468.968133 | \n", "488.561540 | \n", "... | \n", "203.319116 | \n", "203.775189 | \n", "194.335510 | \n", "221.796086 | \n", "209.799195 | \n", "189.672437 | \n", "204.685274 | \n", "181.083009 | \n", "146.922135 | \n", "459.071033 | \n", "
3 | \n", "-0.988 | \n", "10.0 | \n", "178.391438 | \n", "197.575344 | \n", "391.093778 | \n", "447.647728 | \n", "397.730075 | \n", "441.553881 | \n", "483.380503 | \n", "503.488717 | \n", "... | \n", "209.123305 | \n", "209.858019 | \n", "200.480026 | \n", "227.885826 | \n", "215.364812 | \n", "195.433170 | \n", "210.645100 | \n", "186.798909 | \n", "151.736835 | \n", "472.466480 | \n", "
4 | \n", "-0.984 | \n", "10.0 | \n", "182.367373 | \n", "202.063155 | \n", "401.782197 | \n", "460.704946 | \n", "407.262053 | \n", "453.719253 | \n", "497.506821 | \n", "518.123475 | \n", "... | \n", "214.781062 | \n", "215.790568 | \n", "206.480974 | \n", "233.740487 | \n", "220.725338 | \n", "201.064896 | \n", "216.455858 | \n", "192.390601 | \n", "156.453546 | \n", "485.589296 | \n", "
... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "
50095 | \n", "0.984 | \n", "20.0 | \n", "61.349063 | \n", "65.048047 | \n", "85.063819 | \n", "113.433422 | \n", "88.394031 | \n", "124.506086 | \n", "149.952411 | \n", "121.336701 | \n", "... | \n", "42.527688 | \n", "66.486529 | \n", "41.358480 | \n", "43.371996 | \n", "44.445922 | \n", "38.431979 | \n", "55.741899 | \n", "38.668228 | \n", "32.639920 | \n", "133.629536 | \n", "
50096 | \n", "0.988 | \n", "20.0 | \n", "62.286521 | \n", "66.031386 | \n", "87.079793 | \n", "115.201462 | \n", "89.856116 | \n", "126.484048 | \n", "152.059416 | \n", "121.359426 | \n", "... | \n", "42.937436 | \n", "67.461370 | \n", "41.915039 | \n", "43.619696 | \n", "45.178900 | \n", "38.646578 | \n", "56.871620 | \n", "39.109777 | \n", "32.910099 | \n", "133.434442 | \n", "
50097 | \n", "0.992 | \n", "20.0 | \n", "62.730903 | \n", "66.501097 | \n", "88.450080 | \n", "116.113594 | \n", "90.658314 | \n", "127.473423 | \n", "152.945087 | \n", "120.539369 | \n", "... | \n", "43.032323 | \n", "67.901196 | \n", "42.151561 | \n", "43.561366 | \n", "45.569074 | \n", "38.588749 | \n", "57.545199 | \n", "39.264615 | \n", "32.936681 | \n", "132.337261 | \n", "
50098 | \n", "0.996 | \n", "20.0 | \n", "62.651405 | \n", "66.423002 | \n", "89.105845 | \n", "116.103670 | \n", "90.741003 | \n", "127.405634 | \n", "152.538994 | \n", "118.845436 | \n", "... | \n", "42.792571 | \n", "67.772755 | \n", "42.045046 | \n", "43.179045 | \n", "45.587985 | \n", "38.243719 | \n", "57.726269 | \n", "39.112192 | \n", "32.705906 | \n", "130.307051 | \n", "
50099 | \n", "1.000 | \n", "20.0 | \n", "62.032828 | \n", "65.779450 | \n", "88.999047 | \n", "115.132624 | \n", "90.066964 | \n", "126.244182 | \n", "150.810430 | \n", "116.272835 | \n", "... | \n", "42.208679 | \n", "67.060129 | \n", "41.583470 | \n", "42.464753 | \n", "45.218892 | \n", "37.605378 | \n", "57.393721 | \n", "38.641578 | \n", "32.211962 | \n", "127.340661 | \n", "
50100 rows × 259 columns
\n", "