forked from mne-tools/mne-python
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MRG: Add subject warping (mne-tools#3613)
* WIP: Add spherical harmonic transformations
- Loading branch information
Showing
26 changed files
with
930 additions
and
425 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,13 +7,13 @@ | |
matrix. It's an excellent quality check to see if baseline signals match | ||
the assumption of Gaussian white noise from which we expect values around | ||
0 with less than 2 standard deviations. Covariance estimation and diagnostic | ||
plots are based on [1]. | ||
plots are based on [1]_. | ||
References | ||
---------- | ||
[1] Engemann D. and Gramfort A. (2015) Automated model selection in covariance | ||
estimation and spatial whitening of MEG and EEG signals, vol. 108, | ||
328-342, NeuroImage. | ||
.. [1] Engemann D. and Gramfort A. (2015) Automated model selection in | ||
covariance estimation and spatial whitening of MEG and EEG signals, vol. | ||
108, 328-342, NeuroImage. | ||
""" | ||
# Authors: Alexandre Gramfort <[email protected]> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
""" | ||
================================= | ||
Warp head shapes between subjects | ||
================================= | ||
In this example, we warp data from one subject (sample) to another (fsaverage) | ||
using a spherical harmonic approximation of surfaces, followed by thin-plate | ||
spline (TPS) warping of the surface coordinates, as described in [1]_. | ||
References | ||
---------- | ||
.. [1] Darvas F, Ermer JJ, Mosher JC, Leahy RM (2006). "Generic head models for | ||
atlas-based EEG source analysis." Human Brain Mapping 27:129-143 | ||
""" | ||
# Author: Eric Larson <[email protected]> | ||
# | ||
# License: BSD (3-clause) | ||
|
||
import os.path as op | ||
|
||
import mne | ||
|
||
fsaverage_path = op.join(op.dirname(mne.__file__), 'data', 'fsaverage') | ||
data_path = mne.datasets.sample.data_path() | ||
subjects_dir = op.join(data_path, 'subjects') | ||
|
||
############################################################################### | ||
# Load the source digitization, destination surfaces, and source surfaces | ||
# (with everything in head coordinates): | ||
|
||
# Digitization | ||
info = mne.io.read_info(op.join(data_path, 'MEG', 'sample', | ||
'sample_audvis_raw.fif')) | ||
hsp = mne.bem.get_fitting_dig(info, ('cardinal', 'extra')) | ||
|
||
# Destination head surface | ||
fsaverage_surfs = [mne.read_bem_surfaces(op.join(fsaverage_path, | ||
'fsaverage-%s.fif' % kind))[0] | ||
for kind in ('head', 'inner_skull-bem')] | ||
fsaverage_trans = mne.read_trans(op.join(fsaverage_path, | ||
'fsaverage-trans.fif')) | ||
for surf in fsaverage_surfs: | ||
mne.surface.transform_surface_to(surf, 'head', fsaverage_trans, copy=False) | ||
|
||
# Some source surfaces to transform as examples | ||
sample_surfs = mne.read_bem_surfaces( | ||
op.join(subjects_dir, 'sample', 'bem', 'sample-5120-bem.fif')) | ||
sample_trans = mne.read_trans(op.join(data_path, 'MEG', 'sample', | ||
'sample_audvis_raw-trans.fif')) | ||
for surf in sample_surfs: | ||
mne.surface.transform_surface_to(surf, 'head', sample_trans, copy=False) | ||
|
||
############################################################################### | ||
# Transform surfaces using TPS warping: | ||
|
||
warp = mne.transforms.SphericalSurfaceWarp() | ||
warp.fit(source=hsp, destination=fsaverage_surfs[0]['rr']) | ||
for surf in sample_surfs: | ||
surf['rr'] = warp.transform(surf['rr']) | ||
# recompute our normals (only used for viz here) | ||
mne.surface.complete_surface_info(surf, copy=False) | ||
hsp = warp.transform(hsp) | ||
|
||
############################################################################### | ||
# Plot the transformed surfaces and digitization (blue) on template (black): | ||
|
||
from mayavi import mlab # noqa | ||
|
||
t_color = (0.1, 0.3, 1) | ||
fig = mlab.figure(size=(400, 600), bgcolor=(1., 1., 1.)) | ||
for surf, color in zip(fsaverage_surfs + sample_surfs, | ||
[(0., 0., 0.)] * len(fsaverage_surfs) + | ||
[t_color] * len(sample_surfs)): | ||
mesh = mlab.pipeline.triangular_mesh_source( | ||
*surf['rr'].T, triangles=surf['tris']) | ||
mesh.data.point_data.normals = surf['nn'] | ||
mesh.data.cell_data.normals = None | ||
surf = mlab.pipeline.surface(mesh, figure=fig, reset_zoom=True, | ||
opacity=0.33, color=color) | ||
surf.actor.property.backface_culling = True | ||
mlab.points3d(hsp[:, 0], hsp[:, 1], hsp[:, 2], color=t_color, | ||
scale_factor=0.005, opacity=0.25) | ||
mlab.view(45, 90) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.