加算波形の三次元表示

Python 2.7です。

In [1]:
import sys
print(sys.version_info)
del sys
sys.version_info(major=2, minor=7, micro=13, releaselevel='final', serial=0)

データの読み込み

In [2]:
import mne
mne.set_log_level('INFO')
# filename='H:\\FS\\hashizume_akira\\041029\\SEF_median_ave.fif'
filename='c:\\170207\\hashizume_akira\\041029\\SEF_median_ave.fif'
meg=mne.read_evokeds(filename,proj=True)[0] # リストですけど1個しかありません
print(meg)
This filename (c:\170207\hashizume_akira\041029\SEF_median_ave.fif) does not conform to MNE naming conventions. All evoked files should end with -ave.fif or -ave.fif.gz
Reading c:\170207\hashizume_akira\041029\SEF_median_ave.fif ...
    Read a total of 8 projection items:
        grad_ssp_upright_040205.fif : PCA-v1 (1 x 306)  idle
        grad_ssp_upright_040205.fif : PCA-v2 (1 x 306)  idle
        grad_ssp_upright_040205.fif : PCA-v3 (1 x 306)  idle
        mag_ssp_upright_040205_V5.fif : PCA-v1 (1 x 306)  idle
        mag_ssp_upright_040205_V5.fif : PCA-v2 (1 x 306)  idle
        mag_ssp_upright_040205_V5.fif : PCA-v3 (1 x 306)  idle
        mag_ssp_upright_040205_V5.fif : PCA-v4 (1 x 306)  idle
        mag_ssp_upright_040205_V5.fif : PCA-v5 (1 x 306)  idle
    Found the data of interest:
        t =     -50.92 ...     500.20 ms (Category # 1)
        0 CTF compensation matrices available
        nave = 61 - aspect type = 100
<ipython-input-2-05eba3f90d2f>:5: RuntimeWarning: This filename (c:\170207\hashizume_akira\041029\SEF_median_ave.fif) does not conform to MNE naming conventions. All evoked files should end with -ave.fif or -ave.fif.gz
  meg=mne.read_evokeds(filename,proj=True)[0] # リストですけど1個しかありません
Created an SSP operator (subspace dimension = 8)
8 projection items activated
SSP projectors applied...
No baseline correction applied
<Evoked  |  comment : 'Category # 1', kind : average, time : [-0.050918, 0.500198], n_epochs : 61, n_channels x n_times : 315 x 553, ~7.0 MB>
In [3]:
# 脳磁図波形の前処置
tmin,tmax=-0.05,0.1
def showwave(meg,tmin,tmax):
    meg.copy().crop(tmin=tmin,tmax=tmax).plot();

# 元波形
showwave(meg,tmin,tmax)

# 基線補正
meg.apply_baseline(baseline=(None,0))
showwave(meg,tmin,tmax)

# 2~100Hzの周波数フィルタ
x=meg.data
Fs=meg.info['sfreq']
mne.filter.band_pass_filter(x=x,Fs=Fs,Fp1=2,Fp2=100)
showwave(meg,tmin,tmax)
Applying baseline correction (mode: mean)
Setting up band-pass filter from 2 - 1e+02 Hz
l_trans_bandwidth chosen to be 2.0 Hz
h_trans_bandwidth chosen to be 25.0 Hz
Filter length of 3305 samples (3.300 sec) selected
filter_length (3305) is longer than the signal (553), distortion is likely. Reduce filter length or filter a longer signal.
C:\Users\akira\Anaconda2\lib\site-packages\mne\utils.py:638: DeprecationWarning: Function band_pass_filter is deprecated; band_pass_filter is deprecated and will be removed in 0.15, use filter_data instead.
  warnings.warn(msg, category=DeprecationWarning)
<ipython-input-3-eabd8c84a409>:16: RuntimeWarning: filter_length (3305) is longer than the signal (553), distortion is likely. Reduce filter length or filter a longer signal.
  mne.filter.band_pass_filter(x=x,Fs=Fs,Fp1=2,Fp2=100)

MNE-Cで以下のコマンドを入力します。
cd subject
mne_wathershed_bem --atlas
cd ./bem/watershed
mne_convert_surface --surf inner_skull_surface --surfout ../inner_skull.surf
mne_convert_surface --surf outer_skull_surface --surfout ../outer_skull.surf
mne_convert_surface --surf outer_skin_surface --surfout ../outer_skuk.surf

In [4]:
# FreeSurferで切り出して作成したメッシュの確認
# subjects_dir='H:\\FS'
subjects_dir='c:\\170207'
subject='hashizume_akira'
mne.viz.plot_bem(subject=subject,subjects_dir=subjects_dir,brain_surfaces='white',orientation='coronal');
Using surface: c:\170207\hashizume_akira\bem\inner_skull.surf
Using surface: c:\170207\hashizume_akira\bem\outer_skull.surf
Using surface: c:\170207\hashizume_akira\bem\outer_skin.surf
In [5]:
# センサと頭皮の座標合わせ
# mne_analyzeなどで座標合わせしてください
trans=subjects_dir+'\\'+subject+'\\041029\\041029-trans.fif'
In [6]:
# jupyterではうまく描画できません
# mne.viz.plot_trans(meg.info,trans,subject=subject,dig=True,meg_sensors=True,subjects_dir=subjects_dir);

python consoleでは以下の図が描画されます。


python console用です。
import mne
filename='c:\\170207\\hashizume_akira\\041029\\SEF_median_ave.fif'
trans=subjects_dir+'\\'+subject+'\\041029\\041029-trans.fif'
meg=mne.read_evokeds(filename,proj=True)[0]
subjects_dir='c:\\170207'
subject='hashizume_akira'
mne.viz.plot_trans(meg.info,trans,subject=subject,dig=True,meg_sensors=True,subjects_dir=subjects_dir);

In [7]:
# 格子点の設定
src=mne.setup_source_space(subject,spacing='oct6',subjects_dir=subjects_dir,add_dist=False,overwrite=True)
print(src)
Setting up the source space with the following parameters:

SUBJECTS_DIR = c:\170207
Subject      = hashizume_akira
Surface      = white
Octahedron subdivision grade 6

Parameters 'fname' and 'overwrite' are deprecated and will be removed in version 0.16. In version 0.15 fname will default to None. Use mne.write_source_spaces instead.
Overwriting existing file.
>>> 1. Creating the source space...

Doing the octahedral vertex picking...
Loading c:\170207\hashizume_akira\surf\lh.white...
Mapping lh hashizume_akira -> oct (6) ...
    Triangle neighbors and vertex normals...
<ipython-input-7-9a336d0bafdd>:2: RuntimeWarning: Parameters 'fname' and 'overwrite' are deprecated and will be removed in version 0.16. In version 0.15 fname will default to None. Use mne.write_source_spaces instead.
  src=mne.setup_source_space(subject,spacing='oct6',subjects_dir=subjects_dir,add_dist=False,overwrite=True)
Loading geometry from c:\170207\hashizume_akira\surf\lh.sphere...
    Triangle neighbors and vertex normals...
Setting up the triangulation for the decimated surface...
loaded lh.white 4098/160170 selected to source space (oct = 6)

Loading c:\170207\hashizume_akira\surf\rh.white...
Mapping rh hashizume_akira -> oct (6) ...
    Triangle neighbors and vertex normals...
Loading geometry from c:\170207\hashizume_akira\surf\rh.sphere...
    Triangle neighbors and vertex normals...
Setting up the triangulation for the decimated surface...
loaded rh.white 4098/162626 selected to source space (oct = 6)

Overwriting existing file.
    Write a source space...
    [done]
    Write a source space...
    [done]
    2 source spaces written
Wrote c:\170207\hashizume_akira\bem\hashizume_akira-oct-6-src.fif
You are now one step closer to computing the gain matrix
<SourceSpaces: [<surface (lh), n_vertices=160170, n_used=4098, coordinate_frame=MRI (surface RAS)>, <surface (rh), n_vertices=162626, n_used=4098, coordinate_frame=MRI (surface RAS)>]>

クラスSourceSpace srcを細かく見てみます。

In [8]:
print('src list: %d' %len(src))
print(src[0].keys())
print('nearest, dist, dist, dist_limit, pinfo, nearest_dist, patch_inds: None') # NoneType
print('nn: %d x %d' %src[0]['nn'].shape+ ' + %d x %d' %src[1]['nn'].shape)
print('rr: %d x %d' %src[0]['rr'].shape+ '+ %d x %d' %src[1]['rr'].shape)
print('inuse: %d' %src[0]['inuse'].shape+' + %d' %src[1]['inuse'].shape)
print('nuse: %d' %src[0]['nuse']+ ' + %d' %src[1]['nuse'])
print('vertno: %d' %len(src[0]['vertno'])+' + %d' %len(src[1]['vertno']))
print('nuse_tri: %d' %src[0]['nuse_tri']+' + %d' %src[1]['nuse_tri'])
print('coord_frame: %d' %src[0]['coord_frame'])
print('ntri: %d' %src[0]['ntri']+' + %d' %src[1]['ntri'])
print('np: %d' %src[0]['np']+' + %d' %src[1]['np'])
print('subject_his_id: '+src[0]['subject_his_id'])
print('use_tris: %d x %d' %src[0]['use_tris'].shape)
print('type: '+src[0]['type']+' + '+src[1]['type'])
print('id: %d + %d' %(src[0]['id'], src[1]['id']))
print('tris: %d x %d' %src[0]['tris'].shape +' + %d x %d' %src[1]['tris'].shape)
src list: 2
['nearest_dist', 'dist', 'use_tris', 'nearest', 'nuse', 'dist_limit', 'pinfo', 'nuse_tri', 'tris', 'patch_inds', 'nn', 'rr', 'inuse', 'vertno', 'id', 'coord_frame', 'subject_his_id', 'np', 'type', 'ntri']
nearest, dist, dist, dist_limit, pinfo, nearest_dist, patch_inds: None
nn: 160170 x 3 + 162626 x 3
rr: 160170 x 3+ 162626 x 3
inuse: 160170 + 162626
nuse: 4098 + 4098
vertno: 4098 + 4098
nuse_tri: 8192 + 8192
coord_frame: 5
ntri: 320336 + 325248
np: 160170 + 162626
subject_his_id: hashizume_akira
use_tris: 8192 x 3
type: surf + surf
id: 101 + 102
tris: 320336 x 3 + 325248 x 3

srcは本来
mne.write_source_spaces(subject-oct-6-src.fif)
で保存するですが、上mne.setup_source_spaceを実行するとsubject-oct-6-src.fifという名前で保存されています。

In [9]:
del(src)
# srcの読み込み
src_name=subjects_dir+'\\'+subject+'\\bem\\'+subject+'-oct-6-src.fif'
src=mne.read_source_spaces(src_name);
print(src)
    Reading a source space...
    [done]
    Reading a source space...
    [done]
    2 source spaces read
<SourceSpaces: [<surface (lh), n_vertices=160170, n_used=4098, coordinate_frame=MRI (surface RAS)>, <surface (rh), n_vertices=162626, n_used=4098, coordinate_frame=MRI (surface RAS)>]>
In [10]:
# 格子点の確認
mne.viz.plot_bem(subject=subject,subjects_dir=subjects_dir,brain_surfaces='white',src=src,orientation='coronal');
Using surface: c:\170207\hashizume_akira\bem\inner_skull.surf
Using surface: c:\170207\hashizume_akira\bem\outer_skull.surf
Using surface: c:\170207\hashizume_akira\bem\outer_skin.surf