{ "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": 7, "id": "dcd01d5a", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "[Parallel(n_jobs=1)]: Done 17 tasks | elapsed: 0.0s\n", "c:\\Users\\Antoine\\anaconda3\\envs\\biotuner\\Lib\\site-packages\\scipy\\signal\\_spectral_py.py:600: UserWarning: nperseg = 1600 is greater than input length = 1121, using nperseg = 1121\n", " freqs, _, Pxy = _spectral_helper(x, y, fs, window, nperseg, noverlap,\n", "c:\\users\\antoine\\github\\biotuner\\biotuner\\metrics.py:946: RuntimeWarning: divide by zero encountered in scalar divide\n", " harm_temp.append(1 / delta_norm)\n" ] } ], "source": [ "# Import necessary modules\n", "import mne\n", "from mne import make_fixed_length_events\n", "from biotuner.biotuner2d import biotuner_mne\n", "from mne.io import concatenate_raws, read_raw_edf\n", "from mne.datasets import eegbci\n", "\n", "raw_fnames = eegbci.load_data(1, 1)\n", "raws = [read_raw_edf(f, preload=True) for f in raw_fnames]\n", "raw = concatenate_raws(raws)\n", "\n", "# Define the biotuner parameters\n", "bt_dict = {\n", " 'peaks_function': 'fixed',\n", " 'precision': 0.1,\n", " 'fmin': 2,\n", " 'fmax': 60,\n", " 'n_peaks': 3,\n", " 'harm_thresh': 30,\n", " 'delta_lim': 150,\n", "}\n", "\n", "\n", "# Filter the data\n", "raw.filter(l_freq=2., h_freq=60.)\n", "\n", "# Get the names of the MEG channels\n", "eeg_channels = mne.pick_types(raw.info, eeg=True)\n", "\n", "# Keep only the first 10 EEG channels\n", "eeg_channels = eeg_channels[:2]\n", "\n", "# Update 'sf' in the bt_dict\n", "bt_dict['sf'] = raw.info['sfreq']\n", "\n", "\n", "# Epoch the data\n", "tmin = 0.0 # start of each epoch (0s before the trigger)\n", "tmax = 7.0 # end of each epoch (3s after the trigger)\n", "\n", "# Create events\n", "events = make_fixed_length_events(raw, start=0, stop=None, duration=tmax)\n", "\n", "# Make sure only 3 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 eeg_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": 8, "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", "
trialelectrodepeaks_functionprecisionfminfmaxn_peaksharm_threshdelta_limsf...sum_q_for_all_intervalsharm_simmatrix_harm_simmatrix_consmatrix_denomconstenneyharm_fitharmsimsubharm_tension
000fixed0.1260530150160.0...10853278.922.8898730.0323371332.1301590.09610011.25972448.915112[0.20687287374966]
101fixed0.1260530150160.0...1568339.012.8969720.030319507.0587300.09635310.51255159.011115[0.18720813295457783]
210fixed0.1260430150160.0...122282801.130.4818690.0048392772.6472220.01891714.21168021.125127[0.25226081305566683]
311fixed0.1260430150160.0...119298261.470.5304350.0053252275.9416670.01918014.03998351.474938[0.2175077245444831]
420fixed0.1260530150160.0...27185175.802.2908800.0240611115.0777780.07485510.49472275.801397[0.1968767555121499]
521fixed0.1260530150160.0...334867.982.8538510.029816291.8777780.1066388.83327387.984592[0.19687521906926572]
\n", "

6 rows × 26 columns

\n", "
" ], "text/plain": [ " trial electrode peaks_function precision fmin fmax n_peaks \\\n", "0 0 0 fixed 0.1 2 60 5 \n", "1 0 1 fixed 0.1 2 60 5 \n", "2 1 0 fixed 0.1 2 60 4 \n", "3 1 1 fixed 0.1 2 60 4 \n", "4 2 0 fixed 0.1 2 60 5 \n", "5 2 1 fixed 0.1 2 60 5 \n", "\n", " harm_thresh delta_lim sf ... sum_q_for_all_intervals harm_sim \\\n", "0 30 150 160.0 ... 1085327 8.92 \n", "1 30 150 160.0 ... 156833 9.01 \n", "2 30 150 160.0 ... 12228280 1.13 \n", "3 30 150 160.0 ... 11929826 1.47 \n", "4 30 150 160.0 ... 2718517 5.80 \n", "5 30 150 160.0 ... 33486 7.98 \n", "\n", " matrix_harm_sim matrix_cons matrix_denom cons tenney harm_fit \\\n", "0 2.889873 0.032337 1332.130159 0.096100 11.259724 4 \n", "1 2.896972 0.030319 507.058730 0.096353 10.512551 5 \n", "2 0.481869 0.004839 2772.647222 0.018917 14.211680 2 \n", "3 0.530435 0.005325 2275.941667 0.019180 14.039983 5 \n", "4 2.290880 0.024061 1115.077778 0.074855 10.494722 7 \n", "5 2.853851 0.029816 291.877778 0.106638 8.833273 8 \n", "\n", " harmsim subharm_tension \n", "0 8.915112 [0.20687287374966] \n", "1 9.011115 [0.18720813295457783] \n", "2 1.125127 [0.25226081305566683] \n", "3 1.474938 [0.2175077245444831] \n", "4 5.801397 [0.1968767555121499] \n", "5 7.984592 [0.19687521906926572] \n", "\n", "[6 rows x 26 columns]" ] }, "execution_count": 8, "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.11.11" }, "vscode": { "interpreter": { "hash": "78920748c27eb6b6a66039cd6eaf38ba7b9251be5db3fee90bd349c5848cd2ed" } } }, "nbformat": 4, "nbformat_minor": 5 }