Repository navigation
Closed
Description
How can the inverse solution( or "stcs") of mixed source space can be split into inverse solution of surface surce space and inverse solution of volume source space seperately. So that these two can be visualised seperately over MRI?
please find 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_source_space, setup_volume_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_mixed_src = op.join(bem_dir, '%s-oct-6-mixed-src.fif' % subject)
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'
# Get a surface-based source space.
surf = setup_source_space(subject, spacing='ico5', add_dist=False, subjects_dir=subjects_dir)
# Get a volume-based source space.
labels_vol = ['Left-Hippocampus', 'Right-Hippocampus']
hippocampus= setup_volume_source_space(subject, mri=fname_aseg, pos=3.0, bem=fname_model,
volume_label=labels_vol, mindist=3.0, subjects_dir=subjects_dir, verbose=True)
# Generate the mixed source space
src = surf+hippocampus
n = sum(src[i]['nuse'] for i in range(len(src)))
print('the src space contains %d spaces and %d points' % (len(src), n))
# Compute the fwd matrix
fwd = make_forward_solution(fname_evoked, fname_trans, src, fname_bem, mindist=5.0,
meg=True, eeg=False, n_jobs=1)
leadfield = fwd['sol']['data']
print("Leadfield size : %d sensors x %d dipoles" % leadfield.shape)
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))
# 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 solution and for each epoch
snr = 3.0 # use smaller SNR for raw data
inv_method = 'MNE' # sLORETA, MNE, dSPM
lambda2 = 1.0 / snr ** 2
# Compute inverse operator
inverse_operator = make_inverse_operator(evoked.info, fwd, noise_cov,
loose=1.0, depth=0.8, fixed=False)
stcs = apply_inverse(evoked, inverse_operator, lambda2, inv_method,
pick_ori=None)
####################################################################
# Export result as a 4D nifti object
img = stcs.as_volume(src, mri_resolution=False) # set True for full MRI resolution
# Save it as a nifti file
# nib.save(img, 'mne_%s_inverse.nii.gz' % method)
t1_fname = data_path + '/subjects/sample/mri/T1.mgz'
# Plotting with nilearn
plot_stat_map(index_img(img, 61), t1_fname, threshold=8., title='%s (t=%.1f s.)' % (method, stc.times[61]))
plt.show()
Metadata
Metadata
Assignees
Labels
No labels
Activity
agramfort commentedon May 24, 2018
@poojaprabhu9 please don't put code on .docx files but like I did above or using https://gist.github.com/
basically the need is to go from the stcs above into 2 stcs one for the surface par and one for the volume part.
I would go with:
for the background the objective is to include deep structures in MNE inverse and be able to get a surface stc that can be visualized with stc.plot
any volunteer to help? maybe @SherazKhan ?
poojaprabhu9 commentedon May 24, 2018
Sure @agramfort . I will try with that.
Thanks
larsoner commentedon Jan 7, 2021
We have
.volumeand.surfacemethods nowadays, so this should be fixed