Skip to content

Instantly share code, notes, and snippets.

@wmvanvliet
Last active August 28, 2020 10:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wmvanvliet/c8e3aa6f312da5a82d75ba44b38badcc to your computer and use it in GitHub Desktop.
Save wmvanvliet/c8e3aa6f312da5a82d75ba44b38badcc to your computer and use it in GitHub Desktop.
Testing plotting of dipoles using MNE-Python and PySurfer
import mne
import surfer
from mayavi import mlab
# Various filenames we'll be needing
data_path = mne.datasets.sample.data_path()
subjects_dir = f'{data_path}/subjects'
fname_trans = f'{data_path}/MEG/sample/sample_audvis_raw-trans.fif'
fname_cov = f'{data_path}/MEG/sample/sample_audvis-cov.fif'
fname_evokeds = f'{data_path}/MEG/sample/sample_audvis-ave.fif'
fname_bem = f'{subjects_dir}/sample/bem/sample-5120-5120-5120-bem-sol.fif'
fname_trans_avg = mne.datasets.fetch_fsaverage() + '/bem/fsaverage-trans.fif'
# Sensor level files
evoked = mne.read_evokeds(fname_evokeds, condition='Left Auditory')
evoked.apply_baseline()
cov = mne.read_cov(fname_cov)
trans = mne.read_trans(fname_trans)
bem = mne.read_bem_solution(fname_bem)
# Fit a dipole
evoked_cropped = evoked.copy().crop(0.07, 0.08)
dip, _ = mne.fit_dipole(evoked_cropped, cov, bem, trans)
# Plot the location of the dipole in relationship to other things
fig1 = mlab.figure(size=(1000, 1000))
# The head
mne.viz.plot_alignment(
evoked.info,
trans,
'sample',
subjects_dir,
surfaces=['brain', 'head'],
meg=False,
eeg=False,
coord_frame='mri', # Note we are in MRI coordinates
fig=fig1,
)
# Tune opacity of the brain
fig1.children[-1].children[0].children[0].children[0].actor.property.opacity = 0.5
fig1.children[-2].children[0].children[0].children[0].actor.property.opacity = 0.5
# Field patterns
maps = mne.make_field_map(evoked, trans, 'sample', subjects_dir, ch_type='meg')
evoked.plot_field(maps, time=dip.times[dip.gof.argmax()], fig=fig1)
# Remove helmet surface so we can see things better
fig1.children[-3].children[0].children[0].children[0].actor.visible = False
# Finally, the dipole itself
dip[dip.gof.argmax()].plot_locations(
trans,
'sample',
subjects_dir,
mode='arrow',
coord_frame='mri', # Note we are in MRI coordinates
fig=fig1,
)
# Now try plotting things with PySurfer.
# First we try plotting in MRI coordinates on the sample brain
# No no no, this morphs to RAS, not surface RAS, completely different things
# dip_pos_mri = mne.head_to_mri(dip[dip.gof.argmax()].pos, 'sample', trans, subjects_dir)
# Yes yes yes, this morphs to surface RAS
dip_pos_mri = mne.transforms.apply_trans(trans, dip[dip.gof.argmax()].pos)
fig2 = mlab.figure(size=(1000, 1000))
brain = surfer.Brain(
'sample',
subjects_dir=subjects_dir,
hemi='rh',
surf='pial',
alpha=0.2,
figure=fig2,
)
brain.add_foci(dip_pos_mri, hemi='rh')
# Next we try plotting in MNI coordinates on the fsaverage brain
dip_pos_mni = mne.head_to_mni(dip[dip.gof.argmax()].pos, 'sample', trans, subjects_dir)
fig3 = mlab.figure(size=(1000, 1000))
brain = surfer.Brain(
'sample',
subjects_dir=subjects_dir,
hemi='rh',
surf='pial',
alpha=0.2,
figure=fig3,
)
brain.add_foci(dip_pos_mni, hemi='rh')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment