.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plot_main_code.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_plot_main_code.py: Authomatic computation of the transition frequency ================================================== This example shows how to use transfreq for computing the alpha-to-theta transition frequency when only resting-state data are available. The result is compared with that obtained by using the classical Klimesch's method. .. GENERATED FROM PYTHON SOURCE LINES 10-74 .. code-block:: default import mne from transfreq import compute_transfreq_klimesch, compute_transfreq from transfreq.viz import (plot_transfreq, plot_transfreq_klimesch, plot_clusters, plot_channels) from transfreq.utils import read_sample_datapath import os.path as op import numpy as np import matplotlib.pyplot as plt # Define path to the data subj = 'transfreq_sample' data_folder = read_sample_datapath() f_name_rest = op.join(data_folder, '{}_resting.fif'.format(subj)) f_name_task = op.join(data_folder, '{}_evoked.fif'.format(subj)) # Load resting state data raw_rest = mne.io.read_raw_fif(f_name_rest) raw_rest = raw_rest.pick_types(eeg=True, exclude=raw_rest.info['bads'] + ['TP9', 'TP10', 'FT9', 'FT10']) # Load data recorded during task execution raw_task = mne.io.read_raw_fif(f_name_task) raw_task = raw_task.pick_types(eeg=True, exclude=raw_task.info['bads'] + ['TP9', 'TP10', 'FT9', 'FT10']) # List of good channels tmp_idx = mne.pick_types(raw_rest.info, eeg=True, exclude='bads') ch_names_rest = [raw_rest.ch_names[ch_idx] for ch_idx in tmp_idx] tmp_idx = mne.pick_types(raw_task.info, eeg=True, exclude='bads') ch_names_task = [raw_task.ch_names[ch_idx] for ch_idx in tmp_idx] # Define time range. The length of both recordings is set equal to the length of # the shortest one. In this way we obtain the same frequency resolution when # computing the corresponding power spectra by using the multitaper method. # This is required for applying the Klimesch's method tmin = 0 tmax = min(raw_rest.times[-1], raw_task.times[-1]) # Compute power spectra n_fft = 512*2 bandwidth = 1 fmin = 2 fmax = 30 sfreq = raw_rest.info['sfreq'] n_per_seg = int(sfreq*2) psds_rest, freqs = mne.time_frequency.psd_multitaper(raw_rest, fmin=fmin, fmax=fmax, tmin=tmin, tmax=tmax, bandwidth=bandwidth) psds_task, freqs = mne.time_frequency.psd_multitaper(raw_task, fmin=fmin, fmax=fmax, tmin=tmin, tmax=tmax, bandwidth=bandwidth) # Define channel positions ch_locs_rest = np.zeros((len(ch_names_rest), 3)) for ii in range(len(ch_names_rest)): ch_locs_rest[ii, :] = raw_rest.info['chs'][ii]['loc'][:3] .. rst-class:: sphx-glr-script-out Out: .. code-block:: none Opening raw data file /home/sara/Documenti/San_Martino_Alzhaimer/transition_frequency/transfreq/data/transfreq_sample_resting.fif... /home/sara/Documenti/San_Martino_Alzhaimer/transition_frequency/examples/plot_main_code.py:28: RuntimeWarning: This filename (/home/sara/Documenti/San_Martino_Alzhaimer/transition_frequency/transfreq/data/transfreq_sample_resting.fif) does not conform to MNE naming conventions. All raw files should end with raw.fif, raw_sss.fif, raw_tsss.fif, _meg.fif, _eeg.fif, _ieeg.fif, raw.fif.gz, raw_sss.fif.gz, raw_tsss.fif.gz, _meg.fif.gz, _eeg.fif.gz or _ieeg.fif.gz raw_rest = mne.io.read_raw_fif(f_name_rest) Range : 0 ... 29558 = 0.000 ... 59.116 secs Ready. Opening raw data file /home/sara/Documenti/San_Martino_Alzhaimer/transition_frequency/transfreq/data/transfreq_sample_evoked.fif... /home/sara/Documenti/San_Martino_Alzhaimer/transition_frequency/examples/plot_main_code.py:32: RuntimeWarning: This filename (/home/sara/Documenti/San_Martino_Alzhaimer/transition_frequency/transfreq/data/transfreq_sample_evoked.fif) does not conform to MNE naming conventions. All raw files should end with raw.fif, raw_sss.fif, raw_tsss.fif, _meg.fif, _eeg.fif, _ieeg.fif, raw.fif.gz, raw_sss.fif.gz, raw_tsss.fif.gz, _meg.fif.gz, _eeg.fif.gz or _ieeg.fif.gz raw_task = mne.io.read_raw_fif(f_name_task) Range : 0 ... 61121 = 0.000 ... 122.242 secs Ready. Using multitaper spectrum estimation with 57 DPSS windows Using multitaper spectrum estimation with 57 DPSS windows .. GENERATED FROM PYTHON SOURCE LINES 75-76 Compute the transition frequency with the default clustering method .. GENERATED FROM PYTHON SOURCE LINES 76-78 .. code-block:: default tfbox = compute_transfreq(psds_rest, freqs, ch_names=ch_names_rest) .. GENERATED FROM PYTHON SOURCE LINES 79-80 Plot results .. GENERATED FROM PYTHON SOURCE LINES 80-94 .. code-block:: default fig = plt.figure(constrained_layout=True, figsize=(15, 10)) subfigs = fig.subfigures(2, 1, wspace=0.1) ax1 = subfigs[0].subplots(1, 2) # Plot estimated transition frequency plot_transfreq(psds_rest, freqs, tfbox, ax=ax1[0]) # Plot results of the clustering approach plot_clusters(tfbox, ax=ax1[1]) # Plot locations of the two channels groups plot_channels(tfbox, ch_locs_rest, subfig=subfigs[1]) .. image-sg:: /auto_examples/images/sphx_glr_plot_main_code_001.png :alt: Method 4: 2D adjusted k-means, Method 4: 2D adjusted k-means, $G_{\theta}$, $G_{\alpha}$ :srcset: /auto_examples/images/sphx_glr_plot_main_code_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out Out: .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 95-96 Compute results with Klimesch's method .. GENERATED FROM PYTHON SOURCE LINES 96-98 .. code-block:: default tf_klimesch = compute_transfreq_klimesch(psds_rest, psds_task, freqs) .. GENERATED FROM PYTHON SOURCE LINES 99-101 Plot and compare the transition frequencies estimated with klimesch's metod and with transfreq .. GENERATED FROM PYTHON SOURCE LINES 101-106 .. code-block:: default fig, ax = plt.subplots(1, 2, figsize=(8, 4)) plot_transfreq_klimesch(psds_rest, psds_task, freqs, tf_klimesch, ax=ax[0]) plot_transfreq(psds_rest, freqs, tfbox, ax=ax[1]) fig.tight_layout() .. image-sg:: /auto_examples/images/sphx_glr_plot_main_code_002.png :alt: Klimesch's method, Method 4: 2D adjusted k-means :srcset: /auto_examples/images/sphx_glr_plot_main_code_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 13.457 seconds) .. _sphx_glr_download_auto_examples_plot_main_code.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_main_code.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_main_code.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_