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.

Import the required packages

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

Define input parameters for the flame algorithm running in megicperc

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')

Load lead-field matrix and source-space

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
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

Load the cortical atlas

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
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

Compute and save MEG-informed parcellations

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
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

Compute and save MEG-informed parcellations for theta=0 and k = 30

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

""
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

''

Total running time of the script: (1 minutes 32.347 seconds)

Gallery generated by Sphinx-Gallery