说明
转到末尾下载完整示例代码。
对表面源估计进行变形#
本示例演示如何把单个受试者的 mne.SourceEstimate 变形到共同参考空间。我们使用 mne.SourceMorph 完成,预计算的数据将基于 FreeSurfer 球面配准得到的皮层球面表示进行变形(https://surfer.nmr.mgh.harvard.edu/fswiki/SurfaceRegAndTemplates)[1]。该变换用于把受试者的表面顶点映射到参考顶点。这里使用 ‘fsaverage’ 作为参考空间(见 https://surfer.nmr.mgh.harvard.edu/fswiki/FsAverage)。
变换将应用于表面源估计。会生成一幅图,展示 ‘fsaverage’ 的球面与膨胀表面表示,并叠加变形后的表面源估计。
说明
关于变形的背景知识,参见 Morphing and averaging source estimates。
# Author: Tommy Clausner <tommy.clausner@gmail.com>
#
# License: BSD-3-Clause
# Copyright the MNE-Python contributors.
import mne
from mne.datasets import sample
print(__doc__)
设置路径
data_path = sample.data_path()
sample_dir = data_path / "MEG" / "sample"
subjects_dir = data_path / "subjects"
fname_src = subjects_dir / "sample" / "bem" / "sample-oct-6-src.fif"
fname_fwd = sample_dir / "sample_audvis-meg-oct-6-fwd.fif"
fname_fsaverage_src = subjects_dir / "fsaverage" / "bem" / "fsaverage-ico-5-src.fif"
fname_stc = sample_dir / "sample_audvis-meg"
加载示例数据
# Read stc from file
stc = mne.read_source_estimate(fname_stc, subject="sample")
为 SourceEstimate 设置 SourceMorph#
在 MNE 中,表面源估计只用顶点列表表示源空间(见 SourceEstimate 数据结构)。该列表可从 mne.SourceSpaces(src)或 STC 本身获得。若使用源空间,请确保使用正向或逆算子中的源空间,因为正向计算时可能因靠近 BEM 内颅骨表面而排除部分顶点:
src_orig = mne.read_source_spaces(fname_src)
print(src_orig) # n_used=4098, 4098
fwd = mne.read_forward_solution(fname_fwd)
print(fwd["src"]) # n_used=3732, 3766
print([len(v) for v in stc.vertices])
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
<SourceSpaces: [<surface (lh), n_vertices=155407, n_used=4098>, <surface (rh), n_vertices=156866, n_used=4098>] MRI (surface RAS) coords, subject 'sample', ~27.5 MiB>
Reading forward solution from /home/circleci/mne_data/MNE-sample-data/MEG/sample/sample_audvis-meg-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 (FIFF_MNE_FORWARD_SOLUTION_GRAD)) not available
Read MEG forward solution (7498 sources, 306 channels, free orientations)
Source spaces transformed to the forward solution coordinate frame
<SourceSpaces: [<surface (lh), n_vertices=155407, n_used=3732>, <surface (rh), n_vertices=156866, n_used=3766>] head coords, subject 'sample', ~31.0 MiB>
[3732, 3766]
还需要指定要变形到的顶点集。可用 spacing 参数指定,但为保持一致性,最好传入 src_to 参数。
说明
由于 mne.compute_source_morph() 的默认值为 spacing=5、subject_to='fsaverage',本示例中其实可以省略下面的 src_to 与 subject_to 参数。ico-5 的 fsaverage 源空间包含特殊值 [np.arange(10242)] * 2,但对其他间距或其他受试者通常并非如此。因此建议始终传入目标 src 以保持一致。
为 SourceEstimate 初始化 SourceMorph
src_to = mne.read_source_spaces(fname_fsaverage_src)
print(src_to[0]["vertno"]) # special, np.arange(10242)
morph = mne.compute_source_morph(
stc,
subject_from="sample",
subject_to="fsaverage",
src_to=src_to,
subjects_dir=subjects_dir,
)
Reading a source space...
[done]
Reading a source space...
[done]
2 source spaces read
[ 0 1 2 ... 10239 10240 10241]
surface source space present ...
Computing morph matrix...
Left-hemisphere map read.
Right-hemisphere map read.
17 smooth iterations done.
14 smooth iterations done.
[done]
[done]
对 (Vector) SourceEstimate 应用变形#
把源估计数据作为第一个参数传给上面计算出的 morph,即可对源估计数据应用变形。
绘制结果#
# Define plotting parameters
surfer_kwargs = dict(
hemi="lh",
subjects_dir=subjects_dir,
clim=dict(kind="value", lims=[8, 12, 15]),
views="lateral",
initial_time=0.09,
time_unit="s",
size=(800, 800),
smoothing_steps=5,
)
# As spherical surface
brain = stc_fsaverage.plot(surface="sphere", **surfer_kwargs)
# Add title
brain.add_text(0.1, 0.9, "Morphed to fsaverage (spherical)", "title", font_size=16)

作为膨胀表面
brain_inf = stc_fsaverage.plot(surface="inflated", **surfer_kwargs)
# Add title
brain_inf.add_text(0.1, 0.9, "Morphed to fsaverage (inflated)", "title", font_size=16)

从磁盘读写 SourceMorph#
调用 morph.save 可以保存 SourceMorph 实例。该方法可指定文件名,morph 将以 “.h5” 格式保存;若未提供扩展名,会在相应文件名后追加 “-morph.h5”:
该方法可指定保存 morph 的文件名,morph 将以“.h5”格式保存;若未提供扩展名,会在相应文件名后追加“-morph.h5”:
>>> morph.save('my-file-name')
使用 mne.read_source_morph() 可读取已保存的源 morph:
>>> morph = mne.read_source_morph('my-file-name-morph.h5')
环境配置正确后,无需提供 subject_from 或 subjects_dir 等信息,因为它们可从数据推断,并默认变形到 ‘fsaverage’。此外,SourceMorph 无需创建实例并赋给变量即可使用:mne.compute_source_morph() 与 mne.SourceMorph.apply() 可以方便地串成一行。综合起来,直接变形数据的最短写法是:
surface source space present ...
Computing morph matrix...
Left-hemisphere map read.
Right-hemisphere map read.
17 smooth iterations done.
14 smooth iterations done.
[done]
[done]
更多示例请查看使用 SourceMorph.apply 的示例。
参考文献#
脚本总运行时间:(0 分 6.123 秒)