{ "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", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
trialelectrodepeaks_functionprecisionfminfmaxn_peaksharm_threshdelta_limsf...sum_q_for_all_intervalsharm_simmatrix_harm_simmatrix_consmatrix_denomconstenneyharm_fitharmsimsubharm_tension
000EMD1290530100600.61499...5233.7627.4080420.3317733078.0000000.2435902.9141611133.760684[0.040245430660774836]
101EMD1290530100600.61499...2870338.9244.4973540.6234133325.2000000.6250003.0353901538.918388[0.08681323594992725]
202EMD1290530100600.61499...1449333.103.7442480.0383704463.0000000.3652245.1144851033.099289[0.08750497897263645]
303EMD1290530100600.61499...37323.8510.5325440.10950461.0000000.3519233.4089471223.846154[0.03407356717408203]
404EMD1290530100600.61499...664302531.4713.3139460.1556243825.7500000.2686406.229364531.474884[0.1350282792829261]
505EMD1290530100600.61499...24124.7011.4669420.11961147.3333330.4409093.3125441224.696970[0.03335222463092775]
606EMD1290530100600.61499...51868912.028.4764370.0907183956.6666670.3438325.1805351112.015883[0.11689849920521807]
707EMD1290530100600.61499...2989736.7711.3902820.1192605422.5000000.4333334.150084936.774950[0.09721535992779774]
808EMD1290530100600.61499...145321.2213.3507690.14639653.4666670.4723944.368141921.222480[0.0945973868157042]
909EMD1290530100600.61499...51840.5912.8546710.13510559.0000000.3254903.763756940.588235[0.08667506229176597]
1010EMD1290530100600.61499...629404113.199.8684790.1074443310.1333330.4115844.6942221013.188127[0.094220521933636]
1111EMD1290530100600.61499...8342.6840.7536290.5795902516.5333330.4242423.3096441442.676768[0.0950166640113226]
1212EMD1290530100600.61499...266369530.636.6153430.0696262461.3333330.3571015.2461291030.633896[0.11812990118576759]
1313EMD1290530100600.61499...36254.8933.6888860.4193702359.9722220.4190484.273919954.894180[0.12751767843497627]
1414EMD1290530100600.61499...700812513.718.3058820.0886533880.6666670.3782005.3341041013.711387[0.12551460309881624]
1515EMD1290530100600.61499...24635.988.3218970.0871794186.5000000.4424953.9671411235.983187[0.16609746768320133]
1616EMD1290530100600.61499...3497036.7127.3890320.3526683735.5333330.2644115.394392936.705859[0.1402171529768576]
1717EMD1290530100600.61499...296779430.646.7043660.0705183414.1666670.3906674.1255121130.639777[0.0816930528050395]
1818EMD1290530100600.61499...5956.6723.3937690.2589383471.0000000.3979173.5931571056.666667[0.12140059506352861]
1919EMD1290530100600.61499...591790910.106.5582160.0697203939.1333330.3522736.133720710.097506[0.16524134542296162]
2020EMD1290530100600.61499...34932.8225.9446700.2925763368.6000000.2095243.7500841532.817460[0.15660936139458068]
2121EMD1290530100600.61499...182631.0614.3861010.1603243061.8888890.2750845.297506631.055556[0.1443963218278032]
2222EMD1290530100600.61499...6646.6724.5123960.2729021656.3333330.4083333.4271421046.666667[0.10506273525832888]
2323EMD1290530100600.61499...84320.0212.2199810.12977337.9333330.4248664.3475291020.022282[0.08233474456933955]
2424EMD1290530100600.61499...274525519.8321.5475470.2954473129.2666670.2109093.7057011419.831489[0.09423720971902239]
2525EMD1290530100600.61499...23235.398.1553260.0853973795.5000000.4108334.1255121135.388889[0.044989875320305316]
2626EMD1290530100600.61499...12438.4335.9919510.4650783027.1333330.4761902.9909121138.425926[0.13364980249370828]
2727EMD1290530100600.61499...829628730.7320.6364760.2477803694.7777780.3365084.1010611130.734219[0.14876591904748074]
2828EMD1290530100600.61499...88080930.216.0313810.0631563972.8333330.2562583.9931971230.206086[0.10044430391744703]
2929EMD1290530100600.61499...249051113.4413.4502370.1511704338.6666670.3991943.9748351013.439635[0.11772585065374275]
\n", "

30 rows × 28 columns

\n", "
" ], "text/plain": [ " trial electrode peaks_function precision fmin fmax n_peaks \\\n", "0 0 0 EMD 1 2 90 5 \n", "1 0 1 EMD 1 2 90 5 \n", "2 0 2 EMD 1 2 90 5 \n", "3 0 3 EMD 1 2 90 5 \n", "4 0 4 EMD 1 2 90 5 \n", "5 0 5 EMD 1 2 90 5 \n", "6 0 6 EMD 1 2 90 5 \n", "7 0 7 EMD 1 2 90 5 \n", "8 0 8 EMD 1 2 90 5 \n", "9 0 9 EMD 1 2 90 5 \n", "10 1 0 EMD 1 2 90 5 \n", "11 1 1 EMD 1 2 90 5 \n", "12 1 2 EMD 1 2 90 5 \n", "13 1 3 EMD 1 2 90 5 \n", "14 1 4 EMD 1 2 90 5 \n", "15 1 5 EMD 1 2 90 5 \n", "16 1 6 EMD 1 2 90 5 \n", "17 1 7 EMD 1 2 90 5 \n", "18 1 8 EMD 1 2 90 5 \n", "19 1 9 EMD 1 2 90 5 \n", "20 2 0 EMD 1 2 90 5 \n", "21 2 1 EMD 1 2 90 5 \n", "22 2 2 EMD 1 2 90 5 \n", "23 2 3 EMD 1 2 90 5 \n", "24 2 4 EMD 1 2 90 5 \n", "25 2 5 EMD 1 2 90 5 \n", "26 2 6 EMD 1 2 90 5 \n", "27 2 7 EMD 1 2 90 5 \n", "28 2 8 EMD 1 2 90 5 \n", "29 2 9 EMD 1 2 90 5 \n", "\n", " harm_thresh delta_lim sf ... sum_q_for_all_intervals harm_sim \\\n", "0 30 100 600.61499 ... 52 33.76 \n", "1 30 100 600.61499 ... 28703 38.92 \n", "2 30 100 600.61499 ... 14493 33.10 \n", "3 30 100 600.61499 ... 373 23.85 \n", "4 30 100 600.61499 ... 6643025 31.47 \n", "5 30 100 600.61499 ... 241 24.70 \n", "6 30 100 600.61499 ... 518689 12.02 \n", "7 30 100 600.61499 ... 29897 36.77 \n", "8 30 100 600.61499 ... 1453 21.22 \n", "9 30 100 600.61499 ... 518 40.59 \n", "10 30 100 600.61499 ... 6294041 13.19 \n", "11 30 100 600.61499 ... 83 42.68 \n", "12 30 100 600.61499 ... 2663695 30.63 \n", "13 30 100 600.61499 ... 362 54.89 \n", "14 30 100 600.61499 ... 7008125 13.71 \n", "15 30 100 600.61499 ... 246 35.98 \n", "16 30 100 600.61499 ... 34970 36.71 \n", "17 30 100 600.61499 ... 2967794 30.64 \n", "18 30 100 600.61499 ... 59 56.67 \n", "19 30 100 600.61499 ... 5917909 10.10 \n", "20 30 100 600.61499 ... 349 32.82 \n", "21 30 100 600.61499 ... 1826 31.06 \n", "22 30 100 600.61499 ... 66 46.67 \n", "23 30 100 600.61499 ... 843 20.02 \n", "24 30 100 600.61499 ... 2745255 19.83 \n", "25 30 100 600.61499 ... 232 35.39 \n", "26 30 100 600.61499 ... 124 38.43 \n", "27 30 100 600.61499 ... 8296287 30.73 \n", "28 30 100 600.61499 ... 880809 30.21 \n", "29 30 100 600.61499 ... 2490511 13.44 \n", "\n", " matrix_harm_sim matrix_cons matrix_denom cons tenney harm_fit \\\n", "0 27.408042 0.331773 3078.000000 0.243590 2.914161 11 \n", "1 44.497354 0.623413 3325.200000 0.625000 3.035390 15 \n", "2 3.744248 0.038370 4463.000000 0.365224 5.114485 10 \n", "3 10.532544 0.109504 61.000000 0.351923 3.408947 12 \n", "4 13.313946 0.155624 3825.750000 0.268640 6.229364 5 \n", "5 11.466942 0.119611 47.333333 0.440909 3.312544 12 \n", "6 8.476437 0.090718 3956.666667 0.343832 5.180535 11 \n", "7 11.390282 0.119260 5422.500000 0.433333 4.150084 9 \n", "8 13.350769 0.146396 53.466667 0.472394 4.368141 9 \n", "9 12.854671 0.135105 59.000000 0.325490 3.763756 9 \n", "10 9.868479 0.107444 3310.133333 0.411584 4.694222 10 \n", "11 40.753629 0.579590 2516.533333 0.424242 3.309644 14 \n", "12 6.615343 0.069626 2461.333333 0.357101 5.246129 10 \n", "13 33.688886 0.419370 2359.972222 0.419048 4.273919 9 \n", "14 8.305882 0.088653 3880.666667 0.378200 5.334104 10 \n", "15 8.321897 0.087179 4186.500000 0.442495 3.967141 12 \n", "16 27.389032 0.352668 3735.533333 0.264411 5.394392 9 \n", "17 6.704366 0.070518 3414.166667 0.390667 4.125512 11 \n", "18 23.393769 0.258938 3471.000000 0.397917 3.593157 10 \n", "19 6.558216 0.069720 3939.133333 0.352273 6.133720 7 \n", "20 25.944670 0.292576 3368.600000 0.209524 3.750084 15 \n", "21 14.386101 0.160324 3061.888889 0.275084 5.297506 6 \n", "22 24.512396 0.272902 1656.333333 0.408333 3.427142 10 \n", "23 12.219981 0.129773 37.933333 0.424866 4.347529 10 \n", "24 21.547547 0.295447 3129.266667 0.210909 3.705701 14 \n", "25 8.155326 0.085397 3795.500000 0.410833 4.125512 11 \n", "26 35.991951 0.465078 3027.133333 0.476190 2.990912 11 \n", "27 20.636476 0.247780 3694.777778 0.336508 4.101061 11 \n", "28 6.031381 0.063156 3972.833333 0.256258 3.993197 12 \n", "29 13.450237 0.151170 4338.666667 0.399194 3.974835 10 \n", "\n", " harmsim subharm_tension \n", "0 33.760684 [0.040245430660774836] \n", "1 38.918388 [0.08681323594992725] \n", "2 33.099289 [0.08750497897263645] \n", "3 23.846154 [0.03407356717408203] \n", "4 31.474884 [0.1350282792829261] \n", "5 24.696970 [0.03335222463092775] \n", "6 12.015883 [0.11689849920521807] \n", "7 36.774950 [0.09721535992779774] \n", "8 21.222480 [0.0945973868157042] \n", "9 40.588235 [0.08667506229176597] \n", "10 13.188127 [0.094220521933636] \n", "11 42.676768 [0.0950166640113226] \n", "12 30.633896 [0.11812990118576759] \n", "13 54.894180 [0.12751767843497627] \n", "14 13.711387 [0.12551460309881624] \n", "15 35.983187 [0.16609746768320133] \n", "16 36.705859 [0.1402171529768576] \n", "17 30.639777 [0.0816930528050395] \n", "18 56.666667 [0.12140059506352861] \n", "19 10.097506 [0.16524134542296162] \n", "20 32.817460 [0.15660936139458068] \n", "21 31.055556 [0.1443963218278032] \n", "22 46.666667 [0.10506273525832888] \n", "23 20.022282 [0.08233474456933955] \n", "24 19.831489 [0.09423720971902239] \n", "25 35.388889 [0.044989875320305316] \n", "26 38.425926 [0.13364980249370828] \n", "27 30.734219 [0.14876591904748074] \n", "28 30.206086 [0.10044430391744703] \n", "29 13.439635 [0.11772585065374275] \n", "\n", "[30 rows x 28 columns]" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bt_df" ] } ], "metadata": { "kernelspec": { "display_name": "biotuner", "language": "python", "name": "python3" }, "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.8.15" }, "vscode": { "interpreter": { "hash": "78920748c27eb6b6a66039cd6eaf38ba7b9251be5db3fee90bd349c5848cd2ed" } } }, "nbformat": 4, "nbformat_minor": 5 }