.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plot_2_compute_parc.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_plot_2_compute_parc.py: Create multiple MEG informed cortical parcellations for further analysis ======================================================================= Compute the lead-field based MEG informed cortical parcellations that will be used to investigate the test the proposed method. .. GENERATED FROM PYTHON SOURCE LINES 9-10 Import the required packages .. GENERATED FROM PYTHON SOURCE LINES 10-23 .. code-block:: Python import os import os.path as op import numpy as np import pickle from mne import (read_forward_solution, pick_types_forward, convert_forward_solution, read_source_spaces, read_labels_from_annot) from mne.datasets import sample from megicparc import compute_distance_matrix, compute_parcellation .. GENERATED FROM PYTHON SOURCE LINES 24-25 Define input parameters for the flame algorithm running in megicperc .. GENERATED FROM PYTHON SOURCE LINES 25-36 .. code-block:: Python gamma_tot = np.arange(0, 1.01, 0.2) knn_tot = [10, 20, 30, 40] theta = 0.05 parc = 'aparc' sensors_meg = 'grad' folder_fl = op.join('..', 'data', 'data_mne_sample') string_target_file = op.join(folder_fl, '{:s}_flame_grad_k{:d}_gamma{:1.2f}_theta{:1.2f}.pkl') .. GENERATED FROM PYTHON SOURCE LINES 37-38 Load lead-field matrix and source-space .. GENERATED FROM PYTHON SOURCE LINES 38-53 .. code-block:: Python data_path = sample.data_path() subjects_dir = op.join(data_path, 'subjects') subject = 'sample' fwd_file = op.join(data_path, 'MEG', subject, 'sample_audvis-meg-eeg-oct-6-fwd.fif') src_file = op.join(folder_fl, 'source_space_distance-src.fif') fwd = read_forward_solution(fwd_file) fwd = pick_types_forward(fwd, meg=sensors_meg, eeg=False, ref_meg=False, exclude='bads') fwd = convert_forward_solution(fwd, surf_ori=True, force_fixed=True, use_cps=True) src = read_source_spaces(src_file) fwd['src'] = src .. rst-class:: sphx-glr-script-out .. code-block:: none Reading forward solution from /u/29/sommars1/unix/mne_data/MNE-sample-data/MEG/sample/sample_audvis-meg-eeg-oct-6-fwd.fif... Reading a source space... Computing patch statistics... Patch information added... Distance information added... [done] Reading a source space... Computing patch statistics... Patch information added... Distance information added... [done] 2 source spaces read Desired named matrix (kind = 3523) not available Read MEG forward solution (7498 sources, 306 channels, free orientations) Desired named matrix (kind = 3523) not available Read EEG forward solution (7498 sources, 60 channels, free orientations) Forward solutions combined: MEG, EEG Source spaces transformed to the forward solution coordinate frame 203 out of 366 channels remain after picking Average patch normals will be employed in the rotation to the local surface coordinates.... Converting to surface-based source orientations... [done] Reading a source space... Computing patch statistics... Patch information added... Distance information added... [done] Reading a source space... Computing patch statistics... Patch information added... Distance information added... [done] 2 source spaces read .. GENERATED FROM PYTHON SOURCE LINES 54-55 Load the cortical atlas .. GENERATED FROM PYTHON SOURCE LINES 55-62 .. code-block:: Python label_lh = read_labels_from_annot(subject=subject, parc=parc, hemi='lh', subjects_dir=subjects_dir) label_rh = read_labels_from_annot(subject=subject, parc=parc, hemi='rh', subjects_dir=subjects_dir) label = label_lh + label_rh .. rst-class:: sphx-glr-script-out .. code-block:: none Reading labels from parcellation... read 34 labels from /u/29/sommars1/unix/mne_data/MNE-sample-data/subjects/sample/label/lh.aparc.annot Reading labels from parcellation... read 34 labels from /u/29/sommars1/unix/mne_data/MNE-sample-data/subjects/sample/label/rh.aparc.annot .. GENERATED FROM PYTHON SOURCE LINES 63-64 Compute and save MEG-informed parcellations .. GENERATED FROM PYTHON SOURCE LINES 64-86 .. code-block:: Python for gamma in gamma_tot: sort_dist = compute_distance_matrix(fwd, gamma=gamma, theta=theta, labels=label) for knn in knn_tot: target_file = string_target_file.format( subject, knn, gamma, theta) if op.exists(target_file): print('The following file already exists: %s' %target_file) else: flame_data = compute_parcellation(sort_dist, k_nn=knn) # - Save print('Saving %s' % target_file) aux_f = open(target_file, 'wb') pickle.dump(flame_data, aux_f, protocol=2) aux_f.close() del flame_data del sort_dist .. rst-class:: sphx-glr-script-out .. code-block:: none Using leadfield of size = (203, 7498) Computing distance matrix for gamma=0.00 theta=0.05 Computing cosine distance Normalizing leadfield for grad sensors The following file already exists: ../data/data_mne_sample/sample_flame_grad_k10_gamma0.00_theta0.05.pkl The following file already exists: ../data/data_mne_sample/sample_flame_grad_k20_gamma0.00_theta0.05.pkl The following file already exists: ../data/data_mne_sample/sample_flame_grad_k30_gamma0.00_theta0.05.pkl The following file already exists: ../data/data_mne_sample/sample_flame_grad_k40_gamma0.00_theta0.05.pkl Using leadfield of size = (203, 7498) Computing distance matrix for gamma=0.20 theta=0.05 Reading cortical distance from src Adding anatomical constraints Computing cosine distance Normalizing leadfield for grad sensors The following file already exists: ../data/data_mne_sample/sample_flame_grad_k10_gamma0.20_theta0.05.pkl The following file already exists: ../data/data_mne_sample/sample_flame_grad_k20_gamma0.20_theta0.05.pkl The following file already exists: ../data/data_mne_sample/sample_flame_grad_k30_gamma0.20_theta0.05.pkl The following file already exists: ../data/data_mne_sample/sample_flame_grad_k40_gamma0.20_theta0.05.pkl Using leadfield of size = (203, 7498) Computing distance matrix for gamma=0.40 theta=0.05 Reading cortical distance from src Adding anatomical constraints Computing cosine distance Normalizing leadfield for grad sensors The following file already exists: ../data/data_mne_sample/sample_flame_grad_k10_gamma0.40_theta0.05.pkl The following file already exists: ../data/data_mne_sample/sample_flame_grad_k20_gamma0.40_theta0.05.pkl The following file already exists: ../data/data_mne_sample/sample_flame_grad_k30_gamma0.40_theta0.05.pkl The following file already exists: ../data/data_mne_sample/sample_flame_grad_k40_gamma0.40_theta0.05.pkl Using leadfield of size = (203, 7498) Computing distance matrix for gamma=0.60 theta=0.05 Reading cortical distance from src Adding anatomical constraints Computing cosine distance Normalizing leadfield for grad sensors The following file already exists: ../data/data_mne_sample/sample_flame_grad_k10_gamma0.60_theta0.05.pkl The following file already exists: ../data/data_mne_sample/sample_flame_grad_k20_gamma0.60_theta0.05.pkl The following file already exists: ../data/data_mne_sample/sample_flame_grad_k30_gamma0.60_theta0.05.pkl The following file already exists: ../data/data_mne_sample/sample_flame_grad_k40_gamma0.60_theta0.05.pkl Using leadfield of size = (203, 7498) Computing distance matrix for gamma=0.80 theta=0.05 Reading cortical distance from src Adding anatomical constraints Computing cosine distance Normalizing leadfield for grad sensors The following file already exists: ../data/data_mne_sample/sample_flame_grad_k10_gamma0.80_theta0.05.pkl The following file already exists: ../data/data_mne_sample/sample_flame_grad_k20_gamma0.80_theta0.05.pkl The following file already exists: ../data/data_mne_sample/sample_flame_grad_k30_gamma0.80_theta0.05.pkl The following file already exists: ../data/data_mne_sample/sample_flame_grad_k40_gamma0.80_theta0.05.pkl Using leadfield of size = (203, 7498) Computing distance matrix for gamma=1.00 theta=0.05 Reading cortical distance from src Adding anatomical constraints The following file already exists: ../data/data_mne_sample/sample_flame_grad_k10_gamma1.00_theta0.05.pkl The following file already exists: ../data/data_mne_sample/sample_flame_grad_k20_gamma1.00_theta0.05.pkl The following file already exists: ../data/data_mne_sample/sample_flame_grad_k30_gamma1.00_theta0.05.pkl The following file already exists: ../data/data_mne_sample/sample_flame_grad_k40_gamma1.00_theta0.05.pkl .. GENERATED FROM PYTHON SOURCE LINES 87-89 Compute and save MEG-informed parcellations for theta=0 and k = 30 .. GENERATED FROM PYTHON SOURCE LINES 89-112 .. code-block:: Python theta_aux = 0 knn_aux = 30 for gamma in gamma_tot: sort_dist = compute_distance_matrix(fwd, gamma=gamma, theta=theta_aux, labels=label) target_file = string_target_file.format( subject, knn_aux, gamma, theta_aux) if op.exists(target_file): print('The following file already exists: %s' %target_file) else: flame_data = compute_parcellation(sort_dist, k_nn=knn_aux) # - Save print('Saving %s' % target_file) aux_f = open(target_file, 'wb') pickle.dump(flame_data, aux_f, protocol=2) aux_f.close() del flame_data del sort_dist "" .. rst-class:: sphx-glr-script-out .. code-block:: none Using leadfield of size = (203, 7498) Computing distance matrix for gamma=0.00 theta=0.00 Computing cosine distance Normalizing leadfield for grad sensors The following file already exists: ../data/data_mne_sample/sample_flame_grad_k30_gamma0.00_theta0.00.pkl Using leadfield of size = (203, 7498) Computing distance matrix for gamma=0.20 theta=0.00 Reading cortical distance from src Computing cosine distance Normalizing leadfield for grad sensors The following file already exists: ../data/data_mne_sample/sample_flame_grad_k30_gamma0.20_theta0.00.pkl Using leadfield of size = (203, 7498) Computing distance matrix for gamma=0.40 theta=0.00 Reading cortical distance from src Computing cosine distance Normalizing leadfield for grad sensors The following file already exists: ../data/data_mne_sample/sample_flame_grad_k30_gamma0.40_theta0.00.pkl Using leadfield of size = (203, 7498) Computing distance matrix for gamma=0.60 theta=0.00 Reading cortical distance from src Computing cosine distance Normalizing leadfield for grad sensors The following file already exists: ../data/data_mne_sample/sample_flame_grad_k30_gamma0.60_theta0.00.pkl Using leadfield of size = (203, 7498) Computing distance matrix for gamma=0.80 theta=0.00 Reading cortical distance from src Computing cosine distance Normalizing leadfield for grad sensors The following file already exists: ../data/data_mne_sample/sample_flame_grad_k30_gamma0.80_theta0.00.pkl Using leadfield of size = (203, 7498) Computing distance matrix for gamma=1.00 theta=0.00 Reading cortical distance from src The following file already exists: ../data/data_mne_sample/sample_flame_grad_k30_gamma1.00_theta0.00.pkl '' .. rst-class:: sphx-glr-timing **Total running time of the script:** (1 minutes 32.347 seconds) .. _sphx_glr_download_auto_examples_plot_2_compute_parc.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_2_compute_parc.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_2_compute_parc.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_