{ "cells": [ { "attachments": {}, "cell_type": "markdown", "id": "85bd8cac", "metadata": {}, "source": [ "## Harmonicity Metrics Computation on MNE Epochs File\n", "\n", "In this notebook, we demonstrate how to compute harmonicity metrics from an MNE epochs file using the Biotuner library. Harmonicity metrics provide insights into the periodic structure of EEG signals and can be useful for various applications in neuroscience research. Biotuner is a powerful tool for extracting these metrics by analyzing the harmonic architecture of M/EEG data." ] }, { "cell_type": "code", "execution_count": 1, "id": "dcd01d5a", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "c:\\users\\user\\github\\biotuner\\biotuner\\metrics.py:659: RuntimeWarning: divide by zero encountered in double_scalars\n", " harm_temp.append(1 / delta_norm)\n", "c:\\Users\\User\\anaconda3\\envs\\biotuner\\lib\\site-packages\\numpy\\lib\\function_base.py:380: RuntimeWarning: Mean of empty slice.\n", " avg = a.mean(axis)\n", "c:\\Users\\User\\anaconda3\\envs\\biotuner\\lib\\site-packages\\numpy\\core\\_methods.py:189: RuntimeWarning: invalid value encountered in double_scalars\n", " ret = ret.dtype.type(ret / rcount)\n", "c:\\users\\user\\github\\biotuner\\biotuner\\peaks_extraction.py:827: RuntimeWarning: invalid value encountered in true_divide\n", " coh /= temp2\n", "c:\\Users\\User\\anaconda3\\envs\\biotuner\\lib\\site-packages\\numpy\\core\\fromnumeric.py:3440: RuntimeWarning: Mean of empty slice.\n", " return _methods._mean(a, axis=axis, dtype=dtype,\n" ] } ], "source": [ "# Import necessary modules\n", "import mne\n", "from mne.datasets import multimodal\n", "from mne import make_fixed_length_events\n", "from biotuner.biotuner2d import biotuner_mne\n", "\n", "# Define the biotuner parameters\n", "bt_dict = {\n", " 'peaks_function': 'EMD',\n", " 'precision': 1,\n", " 'fmin': 2,\n", " 'fmax': 90,\n", " 'n_peaks': 5,\n", " 'harm_thresh': 30,\n", " 'delta_lim': 100,\n", "}\n", "\n", "# Path to the multimodal data\n", "data_path = multimodal.data_path()\n", "\n", "# Filepath to the multimodal data\n", "raw_file = str(data_path.joinpath('multimodal_raw.fif'))\n", "\n", "# Suppress output\n", "mne.set_log_level('ERROR')\n", "\n", "# Load the raw data\n", "raw = mne.io.read_raw_fif(raw_file, preload=True)\n", "\n", "# Filter the data\n", "raw.filter(l_freq=2., h_freq=90.)\n", "\n", "# Get the names of the MEG channels\n", "meg_channels = mne.pick_types(raw.info, meg=True)\n", "\n", "# Keep only the first 10 MEG channels\n", "meg_channels = meg_channels[:10]\n", "\n", "# Update 'sf' in the bt_dict\n", "bt_dict['sf'] = raw.info['sfreq']\n", "\n", "# Create events\n", "events = make_fixed_length_events(raw, start=0, stop=None, duration=2.0)\n", "\n", "# Epoch the data\n", "tmin = 0.0 # start of each epoch (0s before the trigger)\n", "tmax = 2.0 # end of each epoch (2s after the trigger)\n", "\n", "# Make sure only 10 epochs are created\n", "if len(events) > 3:\n", " events = events[:3]\n", "\n", "# Pick only the first 10 MEG channels\n", "raw.pick_channels([raw.ch_names[i] for i in meg_channels])\n", "\n", "# Epoch the data\n", "epochs = mne.Epochs(raw, events, tmin=tmin, tmax=tmax, baseline=None, preload=True)\n", "\n", "# Compute biotuner metrics on epochs file\n", "bt_df = biotuner_mne(epochs, bt_dict, savefile=True, savename='multimodal_raw')\n" ] }, { "cell_type": "code", "execution_count": 2, "id": "f462d048", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " | trial | \n", "electrode | \n", "peaks_function | \n", "precision | \n", "fmin | \n", "fmax | \n", "n_peaks | \n", "harm_thresh | \n", "delta_lim | \n", "sf | \n", "... | \n", "sum_q_for_all_intervals | \n", "harm_sim | \n", "matrix_harm_sim | \n", "matrix_cons | \n", "matrix_denom | \n", "cons | \n", "tenney | \n", "harm_fit | \n", "harmsim | \n", "subharm_tension | \n", "
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | \n", "0 | \n", "0 | \n", "EMD | \n", "1 | \n", "2 | \n", "90 | \n", "5 | \n", "30 | \n", "100 | \n", "600.61499 | \n", "... | \n", "52 | \n", "33.76 | \n", "27.408042 | \n", "0.331773 | \n", "3078.000000 | \n", "0.243590 | \n", "2.914161 | \n", "11 | \n", "33.760684 | \n", "[0.040245430660774836] | \n", "
1 | \n", "0 | \n", "1 | \n", "EMD | \n", "1 | \n", "2 | \n", "90 | \n", "5 | \n", "30 | \n", "100 | \n", "600.61499 | \n", "... | \n", "28703 | \n", "38.92 | \n", "44.497354 | \n", "0.623413 | \n", "3325.200000 | \n", "0.625000 | \n", "3.035390 | \n", "15 | \n", "38.918388 | \n", "[0.08681323594992725] | \n", "
2 | \n", "0 | \n", "2 | \n", "EMD | \n", "1 | \n", "2 | \n", "90 | \n", "5 | \n", "30 | \n", "100 | \n", "600.61499 | \n", "... | \n", "14493 | \n", "33.10 | \n", "3.744248 | \n", "0.038370 | \n", "4463.000000 | \n", "0.365224 | \n", "5.114485 | \n", "10 | \n", "33.099289 | \n", "[0.08750497897263645] | \n", "
3 | \n", "0 | \n", "3 | \n", "EMD | \n", "1 | \n", "2 | \n", "90 | \n", "5 | \n", "30 | \n", "100 | \n", "600.61499 | \n", "... | \n", "373 | \n", "23.85 | \n", "10.532544 | \n", "0.109504 | \n", "61.000000 | \n", "0.351923 | \n", "3.408947 | \n", "12 | \n", "23.846154 | \n", "[0.03407356717408203] | \n", "
4 | \n", "0 | \n", "4 | \n", "EMD | \n", "1 | \n", "2 | \n", "90 | \n", "5 | \n", "30 | \n", "100 | \n", "600.61499 | \n", "... | \n", "6643025 | \n", "31.47 | \n", "13.313946 | \n", "0.155624 | \n", "3825.750000 | \n", "0.268640 | \n", "6.229364 | \n", "5 | \n", "31.474884 | \n", "[0.1350282792829261] | \n", "
5 | \n", "0 | \n", "5 | \n", "EMD | \n", "1 | \n", "2 | \n", "90 | \n", "5 | \n", "30 | \n", "100 | \n", "600.61499 | \n", "... | \n", "241 | \n", "24.70 | \n", "11.466942 | \n", "0.119611 | \n", "47.333333 | \n", "0.440909 | \n", "3.312544 | \n", "12 | \n", "24.696970 | \n", "[0.03335222463092775] | \n", "
6 | \n", "0 | \n", "6 | \n", "EMD | \n", "1 | \n", "2 | \n", "90 | \n", "5 | \n", "30 | \n", "100 | \n", "600.61499 | \n", "... | \n", "518689 | \n", "12.02 | \n", "8.476437 | \n", "0.090718 | \n", "3956.666667 | \n", "0.343832 | \n", "5.180535 | \n", "11 | \n", "12.015883 | \n", "[0.11689849920521807] | \n", "
7 | \n", "0 | \n", "7 | \n", "EMD | \n", "1 | \n", "2 | \n", "90 | \n", "5 | \n", "30 | \n", "100 | \n", "600.61499 | \n", "... | \n", "29897 | \n", "36.77 | \n", "11.390282 | \n", "0.119260 | \n", "5422.500000 | \n", "0.433333 | \n", "4.150084 | \n", "9 | \n", "36.774950 | \n", "[0.09721535992779774] | \n", "
8 | \n", "0 | \n", "8 | \n", "EMD | \n", "1 | \n", "2 | \n", "90 | \n", "5 | \n", "30 | \n", "100 | \n", "600.61499 | \n", "... | \n", "1453 | \n", "21.22 | \n", "13.350769 | \n", "0.146396 | \n", "53.466667 | \n", "0.472394 | \n", "4.368141 | \n", "9 | \n", "21.222480 | \n", "[0.0945973868157042] | \n", "
9 | \n", "0 | \n", "9 | \n", "EMD | \n", "1 | \n", "2 | \n", "90 | \n", "5 | \n", "30 | \n", "100 | \n", "600.61499 | \n", "... | \n", "518 | \n", "40.59 | \n", "12.854671 | \n", "0.135105 | \n", "59.000000 | \n", "0.325490 | \n", "3.763756 | \n", "9 | \n", "40.588235 | \n", "[0.08667506229176597] | \n", "
10 | \n", "1 | \n", "0 | \n", "EMD | \n", "1 | \n", "2 | \n", "90 | \n", "5 | \n", "30 | \n", "100 | \n", "600.61499 | \n", "... | \n", "6294041 | \n", "13.19 | \n", "9.868479 | \n", "0.107444 | \n", "3310.133333 | \n", "0.411584 | \n", "4.694222 | \n", "10 | \n", "13.188127 | \n", "[0.094220521933636] | \n", "
11 | \n", "1 | \n", "1 | \n", "EMD | \n", "1 | \n", "2 | \n", "90 | \n", "5 | \n", "30 | \n", "100 | \n", "600.61499 | \n", "... | \n", "83 | \n", "42.68 | \n", "40.753629 | \n", "0.579590 | \n", "2516.533333 | \n", "0.424242 | \n", "3.309644 | \n", "14 | \n", "42.676768 | \n", "[0.0950166640113226] | \n", "
12 | \n", "1 | \n", "2 | \n", "EMD | \n", "1 | \n", "2 | \n", "90 | \n", "5 | \n", "30 | \n", "100 | \n", "600.61499 | \n", "... | \n", "2663695 | \n", "30.63 | \n", "6.615343 | \n", "0.069626 | \n", "2461.333333 | \n", "0.357101 | \n", "5.246129 | \n", "10 | \n", "30.633896 | \n", "[0.11812990118576759] | \n", "
13 | \n", "1 | \n", "3 | \n", "EMD | \n", "1 | \n", "2 | \n", "90 | \n", "5 | \n", "30 | \n", "100 | \n", "600.61499 | \n", "... | \n", "362 | \n", "54.89 | \n", "33.688886 | \n", "0.419370 | \n", "2359.972222 | \n", "0.419048 | \n", "4.273919 | \n", "9 | \n", "54.894180 | \n", "[0.12751767843497627] | \n", "
14 | \n", "1 | \n", "4 | \n", "EMD | \n", "1 | \n", "2 | \n", "90 | \n", "5 | \n", "30 | \n", "100 | \n", "600.61499 | \n", "... | \n", "7008125 | \n", "13.71 | \n", "8.305882 | \n", "0.088653 | \n", "3880.666667 | \n", "0.378200 | \n", "5.334104 | \n", "10 | \n", "13.711387 | \n", "[0.12551460309881624] | \n", "
15 | \n", "1 | \n", "5 | \n", "EMD | \n", "1 | \n", "2 | \n", "90 | \n", "5 | \n", "30 | \n", "100 | \n", "600.61499 | \n", "... | \n", "246 | \n", "35.98 | \n", "8.321897 | \n", "0.087179 | \n", "4186.500000 | \n", "0.442495 | \n", "3.967141 | \n", "12 | \n", "35.983187 | \n", "[0.16609746768320133] | \n", "
16 | \n", "1 | \n", "6 | \n", "EMD | \n", "1 | \n", "2 | \n", "90 | \n", "5 | \n", "30 | \n", "100 | \n", "600.61499 | \n", "... | \n", "34970 | \n", "36.71 | \n", "27.389032 | \n", "0.352668 | \n", "3735.533333 | \n", "0.264411 | \n", "5.394392 | \n", "9 | \n", "36.705859 | \n", "[0.1402171529768576] | \n", "
17 | \n", "1 | \n", "7 | \n", "EMD | \n", "1 | \n", "2 | \n", "90 | \n", "5 | \n", "30 | \n", "100 | \n", "600.61499 | \n", "... | \n", "2967794 | \n", "30.64 | \n", "6.704366 | \n", "0.070518 | \n", "3414.166667 | \n", "0.390667 | \n", "4.125512 | \n", "11 | \n", "30.639777 | \n", "[0.0816930528050395] | \n", "
18 | \n", "1 | \n", "8 | \n", "EMD | \n", "1 | \n", "2 | \n", "90 | \n", "5 | \n", "30 | \n", "100 | \n", "600.61499 | \n", "... | \n", "59 | \n", "56.67 | \n", "23.393769 | \n", "0.258938 | \n", "3471.000000 | \n", "0.397917 | \n", "3.593157 | \n", "10 | \n", "56.666667 | \n", "[0.12140059506352861] | \n", "
19 | \n", "1 | \n", "9 | \n", "EMD | \n", "1 | \n", "2 | \n", "90 | \n", "5 | \n", "30 | \n", "100 | \n", "600.61499 | \n", "... | \n", "5917909 | \n", "10.10 | \n", "6.558216 | \n", "0.069720 | \n", "3939.133333 | \n", "0.352273 | \n", "6.133720 | \n", "7 | \n", "10.097506 | \n", "[0.16524134542296162] | \n", "
20 | \n", "2 | \n", "0 | \n", "EMD | \n", "1 | \n", "2 | \n", "90 | \n", "5 | \n", "30 | \n", "100 | \n", "600.61499 | \n", "... | \n", "349 | \n", "32.82 | \n", "25.944670 | \n", "0.292576 | \n", "3368.600000 | \n", "0.209524 | \n", "3.750084 | \n", "15 | \n", "32.817460 | \n", "[0.15660936139458068] | \n", "
21 | \n", "2 | \n", "1 | \n", "EMD | \n", "1 | \n", "2 | \n", "90 | \n", "5 | \n", "30 | \n", "100 | \n", "600.61499 | \n", "... | \n", "1826 | \n", "31.06 | \n", "14.386101 | \n", "0.160324 | \n", "3061.888889 | \n", "0.275084 | \n", "5.297506 | \n", "6 | \n", "31.055556 | \n", "[0.1443963218278032] | \n", "
22 | \n", "2 | \n", "2 | \n", "EMD | \n", "1 | \n", "2 | \n", "90 | \n", "5 | \n", "30 | \n", "100 | \n", "600.61499 | \n", "... | \n", "66 | \n", "46.67 | \n", "24.512396 | \n", "0.272902 | \n", "1656.333333 | \n", "0.408333 | \n", "3.427142 | \n", "10 | \n", "46.666667 | \n", "[0.10506273525832888] | \n", "
23 | \n", "2 | \n", "3 | \n", "EMD | \n", "1 | \n", "2 | \n", "90 | \n", "5 | \n", "30 | \n", "100 | \n", "600.61499 | \n", "... | \n", "843 | \n", "20.02 | \n", "12.219981 | \n", "0.129773 | \n", "37.933333 | \n", "0.424866 | \n", "4.347529 | \n", "10 | \n", "20.022282 | \n", "[0.08233474456933955] | \n", "
24 | \n", "2 | \n", "4 | \n", "EMD | \n", "1 | \n", "2 | \n", "90 | \n", "5 | \n", "30 | \n", "100 | \n", "600.61499 | \n", "... | \n", "2745255 | \n", "19.83 | \n", "21.547547 | \n", "0.295447 | \n", "3129.266667 | \n", "0.210909 | \n", "3.705701 | \n", "14 | \n", "19.831489 | \n", "[0.09423720971902239] | \n", "
25 | \n", "2 | \n", "5 | \n", "EMD | \n", "1 | \n", "2 | \n", "90 | \n", "5 | \n", "30 | \n", "100 | \n", "600.61499 | \n", "... | \n", "232 | \n", "35.39 | \n", "8.155326 | \n", "0.085397 | \n", "3795.500000 | \n", "0.410833 | \n", "4.125512 | \n", "11 | \n", "35.388889 | \n", "[0.044989875320305316] | \n", "
26 | \n", "2 | \n", "6 | \n", "EMD | \n", "1 | \n", "2 | \n", "90 | \n", "5 | \n", "30 | \n", "100 | \n", "600.61499 | \n", "... | \n", "124 | \n", "38.43 | \n", "35.991951 | \n", "0.465078 | \n", "3027.133333 | \n", "0.476190 | \n", "2.990912 | \n", "11 | \n", "38.425926 | \n", "[0.13364980249370828] | \n", "
27 | \n", "2 | \n", "7 | \n", "EMD | \n", "1 | \n", "2 | \n", "90 | \n", "5 | \n", "30 | \n", "100 | \n", "600.61499 | \n", "... | \n", "8296287 | \n", "30.73 | \n", "20.636476 | \n", "0.247780 | \n", "3694.777778 | \n", "0.336508 | \n", "4.101061 | \n", "11 | \n", "30.734219 | \n", "[0.14876591904748074] | \n", "
28 | \n", "2 | \n", "8 | \n", "EMD | \n", "1 | \n", "2 | \n", "90 | \n", "5 | \n", "30 | \n", "100 | \n", "600.61499 | \n", "... | \n", "880809 | \n", "30.21 | \n", "6.031381 | \n", "0.063156 | \n", "3972.833333 | \n", "0.256258 | \n", "3.993197 | \n", "12 | \n", "30.206086 | \n", "[0.10044430391744703] | \n", "
29 | \n", "2 | \n", "9 | \n", "EMD | \n", "1 | \n", "2 | \n", "90 | \n", "5 | \n", "30 | \n", "100 | \n", "600.61499 | \n", "... | \n", "2490511 | \n", "13.44 | \n", "13.450237 | \n", "0.151170 | \n", "4338.666667 | \n", "0.399194 | \n", "3.974835 | \n", "10 | \n", "13.439635 | \n", "[0.11772585065374275] | \n", "
30 rows × 28 columns
\n", "