{ "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", " | 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", "fixed | \n", "0.1 | \n", "2 | \n", "60 | \n", "5 | \n", "30 | \n", "150 | \n", "160.0 | \n", "... | \n", "1085327 | \n", "8.92 | \n", "2.889873 | \n", "0.032337 | \n", "1332.130159 | \n", "0.096100 | \n", "11.259724 | \n", "4 | \n", "8.915112 | \n", "[0.20687287374966] | \n", "
1 | \n", "0 | \n", "1 | \n", "fixed | \n", "0.1 | \n", "2 | \n", "60 | \n", "5 | \n", "30 | \n", "150 | \n", "160.0 | \n", "... | \n", "156833 | \n", "9.01 | \n", "2.896972 | \n", "0.030319 | \n", "507.058730 | \n", "0.096353 | \n", "10.512551 | \n", "5 | \n", "9.011115 | \n", "[0.18720813295457783] | \n", "
2 | \n", "1 | \n", "0 | \n", "fixed | \n", "0.1 | \n", "2 | \n", "60 | \n", "4 | \n", "30 | \n", "150 | \n", "160.0 | \n", "... | \n", "12228280 | \n", "1.13 | \n", "0.481869 | \n", "0.004839 | \n", "2772.647222 | \n", "0.018917 | \n", "14.211680 | \n", "2 | \n", "1.125127 | \n", "[0.25226081305566683] | \n", "
3 | \n", "1 | \n", "1 | \n", "fixed | \n", "0.1 | \n", "2 | \n", "60 | \n", "4 | \n", "30 | \n", "150 | \n", "160.0 | \n", "... | \n", "11929826 | \n", "1.47 | \n", "0.530435 | \n", "0.005325 | \n", "2275.941667 | \n", "0.019180 | \n", "14.039983 | \n", "5 | \n", "1.474938 | \n", "[0.2175077245444831] | \n", "
4 | \n", "2 | \n", "0 | \n", "fixed | \n", "0.1 | \n", "2 | \n", "60 | \n", "5 | \n", "30 | \n", "150 | \n", "160.0 | \n", "... | \n", "2718517 | \n", "5.80 | \n", "2.290880 | \n", "0.024061 | \n", "1115.077778 | \n", "0.074855 | \n", "10.494722 | \n", "7 | \n", "5.801397 | \n", "[0.1968767555121499] | \n", "
5 | \n", "2 | \n", "1 | \n", "fixed | \n", "0.1 | \n", "2 | \n", "60 | \n", "5 | \n", "30 | \n", "150 | \n", "160.0 | \n", "... | \n", "33486 | \n", "7.98 | \n", "2.853851 | \n", "0.029816 | \n", "291.877778 | \n", "0.106638 | \n", "8.833273 | \n", "8 | \n", "7.984592 | \n", "[0.19687521906926572] | \n", "
6 rows × 26 columns
\n", "