mne-tools / mne-python

MNE: Magnetoencephalography (MEG) and Electroencephalography (EEG) in Python
https://mne.tools
BSD 3-Clause "New" or "Revised" License
2.7k stars 1.31k forks source link

problem in visualising the inverse source solution when more than one volume labels are used #5224

Closed poojaprabhu9 closed 6 years ago

poojaprabhu9 commented 6 years ago

How can the code can be modified to visualize the inverse solution of volume source space on MRI when two volume labels are given. For example in the code give: labels_vol = ['Left-Hippocampus', 'Right-Hippocampus']

visualization of volume source space on MRI.docx

agramfort commented 6 years ago

@poojaprabhu9 please edit the issue as I did here https://github.com/mne-tools/mne-python/issues/5223 to avoid the .docx file. Tell us when it's done.

poojaprabhu9 commented 6 years ago

@agramfort . Sorry for Docx file. Here is the code.

import numpy as np  
import os.path as op
import matplotlib.pyplot as plt
import mne
from mne.datasets import sample
from mne import setup_volume_source_space, setup_source_space
from mne import make_forward_solution
from mne.minimum_norm import make_inverse_operator, apply_inverse
from nilearn.plotting import plot_stat_map
from nilearn.image import index_img
# Set dir
data_path = sample.data_path()
subject = 'sample'
data_dir = op.join(data_path, 'MEG', subject)
subjects_dir = op.join(data_path, 'subjects')
bem_dir = op.join(subjects_dir, subject, 'bem')
# Set file names
fname_aseg = op.join(subjects_dir, subject, 'mri', 'aseg.mgz')
fname_model = op.join(bem_dir, '%s-5120-bem.fif' % subject)
fname_bem = op.join(bem_dir, '%s-5120-bem-sol.fif' % subject)
fname_evoked = data_dir + '/sample_audvis-ave.fif'
fname_trans = data_dir + '/sample_audvis_raw-trans.fif'
fname_fwd = data_dir + '/sample_audvis-meg-oct-6-mixed-fwd.fif'
fname_cov = data_dir + '/sample_audvis-shrunk-cov.fif'
#########################################################################
labels_vol = ['Left-Hippocampus']  # labels_vol = ['Left-Hippocampus', ‘Right-Hippocampus’]
sphere=(0,0,0,120)
src = setup_volume_source_space(subject, mri=fname_aseg, pos=2.0, sphere=sphere,
                                        volume_label=labels_vol, subjects_dir=subjects_dir, 
                                        verbose=True)
fwd = make_forward_solution(fname_evoked, fname_trans, src, fname_bem, mindist=5.0,  
                            meg=True, eeg=False, n_jobs=1)
src_fwd = fwd['src']
n = sum(src_fwd[i]['nuse'] for i in range(len(src_fwd)))
print('the fwd src space contains %d spaces and %d points' % (len(src_fwd), n))
# Compute inverse solution 
snr = 3.0           
inv_method = 'MNE' # or MNE
lambda2 = 1.0 / snr ** 2
# Load data
condition = 'Left Auditory'
evoked = mne.read_evokeds(fname_evoked, condition=condition,
                          baseline=(None, 0))
noise_cov = mne.read_cov(fname_cov)
# Compute inverse operator
inverse_operator = make_inverse_operator(evoked.info, fwd, noise_cov,
                                         depth=None, fixed=False)
stcs = apply_inverse(evoked, inverse_operator, lambda2, inv_method,
                     pick_ori=None)
######################################################################
src = fwd['src']
img = stcs.as_volume(src, mri_resolution=False)  # set True for full MRI resolution
t1_fname = data_path + '/subjects/sample/mri/T1.mgz'
# Plotting with nilearn ######################################################
plot_stat_map(index_img(img, 200), t1_fname, threshold=2*1e-9,
              title='%s (t=%.1f s.)' % (inv_method, stcs.times[61]))
plt.show()
agramfort commented 6 years ago

we have a regression on this with the new SourceMorph PR :(

see this gist https://gist.github.com/agramfort/394e0155945261ccf3faeea0f66d8dbe to replicate the problem.

Basically we now again only take the first label from the VolSourceEstimate ...

cc @poojaprabhu9