MNE-pythonのversionチェック

2017年4月の時点でMNE-pythonはpython 2.7で動きます。python 3でも動きますが、三次元画像処理はできません。

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

MNEの準備とraw-fifファイルと読み書き

In [2]:
import mne # mneを組み込む
from mne.datasets import sample
mne.set_log_level('INFO');
data_path=sample.data_path()
raw_fname=data_path+'\\MEG\\sample\\sample_audvis_filt-0-40_raw.fif'
print(raw_fname)
C:\Users\akira\mne_data\MNE-sample-data\MEG\sample\sample_audvis_filt-0-40_raw.fif
In [3]:
raw=mne.io.read_raw_fif(raw_fname) # raw_fifファイルの読み込み
print(raw)
print(raw.info)
Opening raw data file C:\Users\akira\mne_data\MNE-sample-data\MEG\sample\sample_audvis_filt-0-40_raw.fif...
    Read a total of 4 projection items:
        PCA-v1 (1 x 102)  idle
        PCA-v2 (1 x 102)  idle
        PCA-v3 (1 x 102)  idle
        Average EEG reference (1 x 60)  idle
    Range : 6450 ... 48149 =     42.956 ...   320.665 secs
Ready.
Current compensation grade : 0
<Raw  |  sample_audvis_filt-0-40_raw.fif, n_channels x n_times : 376 x 41700 (277.7 sec), ~3.7 MB, data not loaded>
<Info | 19 non-empty fields
    bads : list | MEG 2443, EEG 053
    buffer_size_sec : numpy.float64 | 13.3196808772
    ch_names : list | MEG 0113, MEG 0112, MEG 0111, MEG 0122, MEG 0123, ...
    chs : list | 376 items (EOG: 1, EEG: 60, STIM: 9, GRAD: 204, MAG: 102)
    comps : list | 0 items
    custom_ref_applied : bool | False
    dev_head_t : 'mne.transforms.Transform | 3 items
    dig : list | 146 items
    events : list | 0 items
    file_id : dict | 4 items
    highpass : float | 0.10000000149 Hz
    hpi_meas : list | 1 items
    hpi_results : list | 1 items
    lowpass : float | 40.0 Hz
    meas_date : numpy.ndarray | 2002-12-04 04:01:10
    meas_id : dict | 4 items
    nchan : int | 376
    projs : list | PCA-v1: off, PCA-v2: off, PCA-v3: off, ...
    sfreq : float | 150.153747559 Hz
    acq_pars : NoneType
    acq_stim : NoneType
    ctf_head_t : NoneType
    description : NoneType
    dev_ctf_t : NoneType
    experimenter : NoneType
    hpi_subsystem : NoneType
    kit_system_id : NoneType
    line_freq : NoneType
    proj_id : NoneType
    proj_name : NoneType
    subject_info : NoneType
    xplotter_layout : NoneType
>
In [4]:
print(raw.ch_names)
[u'MEG 0113', u'MEG 0112', u'MEG 0111', u'MEG 0122', u'MEG 0123', u'MEG 0121', u'MEG 0132', u'MEG 0133', u'MEG 0131', u'MEG 0143', u'MEG 0142', u'MEG 0141', u'MEG 0213', u'MEG 0212', u'MEG 0211', u'MEG 0222', u'MEG 0223', u'MEG 0221', u'MEG 0232', u'MEG 0233', u'MEG 0231', u'MEG 0243', u'MEG 0242', u'MEG 0241', u'MEG 0313', u'MEG 0312', u'MEG 0311', u'MEG 0322', u'MEG 0323', u'MEG 0321', u'MEG 0333', u'MEG 0332', u'MEG 0331', u'MEG 0343', u'MEG 0342', u'MEG 0341', u'MEG 0413', u'MEG 0412', u'MEG 0411', u'MEG 0422', u'MEG 0423', u'MEG 0421', u'MEG 0432', u'MEG 0433', u'MEG 0431', u'MEG 0443', u'MEG 0442', u'MEG 0441', u'MEG 0513', u'MEG 0512', u'MEG 0511', u'MEG 0523', u'MEG 0522', u'MEG 0521', u'MEG 0532', u'MEG 0533', u'MEG 0531', u'MEG 0542', u'MEG 0543', u'MEG 0541', u'MEG 0613', u'MEG 0612', u'MEG 0611', u'MEG 0622', u'MEG 0623', u'MEG 0621', u'MEG 0633', u'MEG 0632', u'MEG 0631', u'MEG 0642', u'MEG 0643', u'MEG 0641', u'MEG 0713', u'MEG 0712', u'MEG 0711', u'MEG 0723', u'MEG 0722', u'MEG 0721', u'MEG 0733', u'MEG 0732', u'MEG 0731', u'MEG 0743', u'MEG 0742', u'MEG 0741', u'MEG 0813', u'MEG 0812', u'MEG 0811', u'MEG 0822', u'MEG 0823', u'MEG 0821', u'MEG 0913', u'MEG 0912', u'MEG 0911', u'MEG 0923', u'MEG 0922', u'MEG 0921', u'MEG 0932', u'MEG 0933', u'MEG 0931', u'MEG 0942', u'MEG 0943', u'MEG 0941', u'MEG 1013', u'MEG 1012', u'MEG 1011', u'MEG 1023', u'MEG 1022', u'MEG 1021', u'MEG 1032', u'MEG 1033', u'MEG 1031', u'MEG 1043', u'MEG 1042', u'MEG 1041', u'MEG 1112', u'MEG 1113', u'MEG 1111', u'MEG 1123', u'MEG 1122', u'MEG 1121', u'MEG 1133', u'MEG 1132', u'MEG 1131', u'MEG 1142', u'MEG 1143', u'MEG 1141', u'MEG 1213', u'MEG 1212', u'MEG 1211', u'MEG 1223', u'MEG 1222', u'MEG 1221', u'MEG 1232', u'MEG 1233', u'MEG 1231', u'MEG 1243', u'MEG 1242', u'MEG 1241', u'MEG 1312', u'MEG 1313', u'MEG 1311', u'MEG 1323', u'MEG 1322', u'MEG 1321', u'MEG 1333', u'MEG 1332', u'MEG 1331', u'MEG 1342', u'MEG 1343', u'MEG 1341', u'MEG 1412', u'MEG 1413', u'MEG 1411', u'MEG 1423', u'MEG 1422', u'MEG 1421', u'MEG 1433', u'MEG 1432', u'MEG 1431', u'MEG 1442', u'MEG 1443', u'MEG 1441', u'MEG 1512', u'MEG 1513', u'MEG 1511', u'MEG 1522', u'MEG 1523', u'MEG 1521', u'MEG 1533', u'MEG 1532', u'MEG 1531', u'MEG 1543', u'MEG 1542', u'MEG 1541', u'MEG 1613', u'MEG 1612', u'MEG 1611', u'MEG 1622', u'MEG 1623', u'MEG 1621', u'MEG 1632', u'MEG 1633', u'MEG 1631', u'MEG 1643', u'MEG 1642', u'MEG 1641', u'MEG 1713', u'MEG 1712', u'MEG 1711', u'MEG 1722', u'MEG 1723', u'MEG 1721', u'MEG 1732', u'MEG 1733', u'MEG 1731', u'MEG 1743', u'MEG 1742', u'MEG 1741', u'MEG 1813', u'MEG 1812', u'MEG 1811', u'MEG 1822', u'MEG 1823', u'MEG 1821', u'MEG 1832', u'MEG 1833', u'MEG 1831', u'MEG 1843', u'MEG 1842', u'MEG 1841', u'MEG 1912', u'MEG 1913', u'MEG 1911', u'MEG 1923', u'MEG 1922', u'MEG 1921', u'MEG 1932', u'MEG 1933', u'MEG 1931', u'MEG 1943', u'MEG 1942', u'MEG 1941', u'MEG 2013', u'MEG 2012', u'MEG 2011', u'MEG 2023', u'MEG 2022', u'MEG 2021', u'MEG 2032', u'MEG 2033', u'MEG 2031', u'MEG 2042', u'MEG 2043', u'MEG 2041', u'MEG 2113', u'MEG 2112', u'MEG 2111', u'MEG 2122', u'MEG 2123', u'MEG 2121', u'MEG 2133', u'MEG 2132', u'MEG 2131', u'MEG 2143', u'MEG 2142', u'MEG 2141', u'MEG 2212', u'MEG 2213', u'MEG 2211', u'MEG 2223', u'MEG 2222', u'MEG 2221', u'MEG 2233', u'MEG 2232', u'MEG 2231', u'MEG 2242', u'MEG 2243', u'MEG 2241', u'MEG 2312', u'MEG 2313', u'MEG 2311', u'MEG 2323', u'MEG 2322', u'MEG 2321', u'MEG 2332', u'MEG 2333', u'MEG 2331', u'MEG 2343', u'MEG 2342', u'MEG 2341', u'MEG 2412', u'MEG 2413', u'MEG 2411', u'MEG 2423', u'MEG 2422', u'MEG 2421', u'MEG 2433', u'MEG 2432', u'MEG 2431', u'MEG 2442', u'MEG 2443', u'MEG 2441', u'MEG 2512', u'MEG 2513', u'MEG 2511', u'MEG 2522', u'MEG 2523', u'MEG 2521', u'MEG 2533', u'MEG 2532', u'MEG 2531', u'MEG 2543', u'MEG 2542', u'MEG 2541', u'MEG 2612', u'MEG 2613', u'MEG 2611', u'MEG 2623', u'MEG 2622', u'MEG 2621', u'MEG 2633', u'MEG 2632', u'MEG 2631', u'MEG 2642', u'MEG 2643', u'MEG 2641', u'STI 001', u'STI 002', u'STI 003', u'STI 004', u'STI 005', u'STI 006', u'STI 014', u'STI 015', u'STI 016', u'EEG 001', u'EEG 002', u'EEG 003', u'EEG 004', u'EEG 005', u'EEG 006', u'EEG 007', u'EEG 008', u'EEG 009', u'EEG 010', u'EEG 011', u'EEG 012', u'EEG 013', u'EEG 014', u'EEG 015', u'EEG 016', u'EEG 017', u'EEG 018', u'EEG 019', u'EEG 020', u'EEG 021', u'EEG 022', u'EEG 023', u'EEG 024', u'EEG 025', u'EEG 026', u'EEG 027', u'EEG 028', u'EEG 029', u'EEG 030', u'EEG 031', u'EEG 032', u'EEG 033', u'EEG 034', u'EEG 035', u'EEG 036', u'EEG 037', u'EEG 038', u'EEG 039', u'EEG 040', u'EEG 041', u'EEG 042', u'EEG 043', u'EEG 044', u'EEG 045', u'EEG 046', u'EEG 047', u'EEG 048', u'EEG 049', u'EEG 050', u'EEG 051', u'EEG 052', u'EEG 053', u'EEG 054', u'EEG 055', u'EEG 056', u'EEG 057', u'EEG 058', u'EEG 059', u'EEG 060', u'EOG 061']
In [5]:
start,stop=raw.time_as_index([100,115]) # 100~115秒で切り出し
data,times=raw[:,start:stop]
print(data.shape) # MATLABだとsize(data)
print(times.shape) # MATLABだとsize(times)
data,times=raw[2:20:3,start:stop] # MATLABだと2:3:20
raw.plot();# 初期設定はduration=10秒 starrt=0.0秒 N_channels=20 chずつ・・・などです
(376L, 2252L)
(2252L,)

jupyterでは絵になってしまうのでできませんが、カーソルバーで表示されるチャンネルや時間幅を変更できます。ProjボタンでSSPのon/offが選択できます。

In [6]:
picks=mne.pick_types(raw.info,meg=True,eeg=False,stim=True,exclude='bads')
raw.save('sample_audvis_meg_raw.fif',tmin=0,tmax=150,picks=picks,overwrite=True) # 0~150秒のデータを保存
Overwriting existing file.
Writing C:\Users\akira\sample_audvis_meg_raw.fif
Closing C:\Users\akira\sample_audvis_meg_raw.fif [done]

raw-fifから加算

In [7]:
events=mne.find_events(raw,stim_channel='STI 014'); # トリガー信号のチャンネルを指定します。
print(events[:5])
319 events found
Events id: [ 1  2  3  4  5 32]
[[6994    0    2]
 [7086    0    3]
 [7192    0    1]
 [7304    0    4]
 [7413    0    2]]
In [8]:
mne.set_config('MNE_STIM_CHANNEL','STI101',set_env=True) # トリガーチャンネルの初期設定
In [9]:
event_id=dict(aud_l=1,aud_r=2) # トリガー信号 聴覚左が1、聴覚右が2
tmin,tmax=-0.2,0.5 # -0.2~0.5秒
In [10]:
raw.info['bads']+=['MEG 2443','EEG 053'];
In [11]:
picks=mne.pick_types(raw.info,meg=True,eeg=True,eog=True,stim=False,exclude='bads');
In [12]:
baseline=(None,0) # 基線の補正
In [13]:
reject=dict(grad=4000e-13,mag=4e-12,eog=150e-6) # 高振幅は除外
In [14]:
epochs=mne.Epochs(raw,events,event_id,tmin,tmax,proj=True,picks=picks,baseline=baseline,preload=False,reject=reject
    )
print(epochs);
145 matching events found
Created an SSP operator (subspace dimension = 4)
4 projection items activated
<Epochs  |  n_events : 145 (good & bad), tmin : -0.199795213158 (s), tmax : 0.499488032896 (s), baseline : (None, 0), ~3.7 MB, data not loaded,
 'aud_l': 72, 'aud_r': 73>
In [15]:
epochs_data=epochs['aud_l'].get_data()
print(epochs_data.shape)
Loading data for 72 events and 106 original time points ...
    Rejecting  epoch based on EOG : [u'EOG 061']
    Rejecting  epoch based on EOG : [u'EOG 061']
    Rejecting  epoch based on EOG : [u'EOG 061']
    Rejecting  epoch based on EOG : [u'EOG 061']
    Rejecting  epoch based on EOG : [u'EOG 061']
    Rejecting  epoch based on MAG : [u'MEG 1711']
    Rejecting  epoch based on EOG : [u'EOG 061']
    Rejecting  epoch based on EOG : [u'EOG 061']
    Rejecting  epoch based on EOG : [u'EOG 061']
    Rejecting  epoch based on EOG : [u'EOG 061']
    Rejecting  epoch based on EOG : [u'EOG 061']
    Rejecting  epoch based on EOG : [u'EOG 061']
    Rejecting  epoch based on EOG : [u'EOG 061']
    Rejecting  epoch based on EOG : [u'EOG 061']
    Rejecting  epoch based on EOG : [u'EOG 061']
    Rejecting  epoch based on EOG : [u'EOG 061']
    Rejecting  epoch based on EOG : [u'EOG 061']
17 bad epochs dropped
(55L, 365L, 106L)
In [16]:
from scipy import io 
print(type(epochs_data))
print(epochs_data.shape)
io.savemat('epochs_data.mat',dict(epochs_data=epochs_data,oned_as='row')) # MATLABのデータとして保存
<type 'numpy.ndarray'>
(55L, 365L, 106L)

MATLABで開と55×365×106の三次元配列epochs_rowという文字列のdataとoned_asが読み込まれます。

In [17]:
epochs.save('sample-epo.fif') # FIFファイル形式で保存できます。
Loading data for 145 events and 106 original time points ...
    Rejecting  epoch based on EOG : [u'EOG 061']
    Rejecting  epoch based on EOG : [u'EOG 061']
    Rejecting  epoch based on EOG : [u'EOG 061']
    Rejecting  epoch based on EOG : [u'EOG 061']
    Rejecting  epoch based on EOG : [u'EOG 061']
    Rejecting  epoch based on EOG : [u'EOG 061']
    Rejecting  epoch based on EOG : [u'EOG 061']
    Rejecting  epoch based on EOG : [u'EOG 061']
    Rejecting  epoch based on MAG : [u'MEG 1711']
    Rejecting  epoch based on EOG : [u'EOG 061']
    Rejecting  epoch based on EOG : [u'EOG 061']
    Rejecting  epoch based on EOG : [u'EOG 061']
    Rejecting  epoch based on MAG : [u'MEG 1711']
    Rejecting  epoch based on EOG : [u'EOG 061']
    Rejecting  epoch based on EOG : [u'EOG 061']
    Rejecting  epoch based on EOG : [u'EOG 061']
    Rejecting  epoch based on EOG : [u'EOG 061']
    Rejecting  epoch based on EOG : [u'EOG 061']
    Rejecting  epoch based on EOG : [u'EOG 061']
    Rejecting  epoch based on EOG : [u'EOG 061']
    Rejecting  epoch based on EOG : [u'EOG 061']
    Rejecting  epoch based on EOG : [u'EOG 061']
    Rejecting  epoch based on EOG : [u'EOG 061']
    Rejecting  epoch based on EOG : [u'EOG 061']
    Rejecting  epoch based on EOG : [u'EOG 061']
    Rejecting  epoch based on EOG : [u'EOG 061']
    Rejecting  epoch based on EOG : [u'EOG 061']
    Rejecting  epoch based on EOG : [u'EOG 061']
    Rejecting  epoch based on EOG : [u'EOG 061']
29 bad epochs dropped
Loading data for 1 events and 106 original time points ...
Loading data for 116 events and 106 original time points ...

sample-epo.fifはデータ形式が三次元配列のFIFFファイルです。 Neuromagのアプリケーションでは読めません。 Source Modellingで開くとIncorrect number of channels in data matrix!と表示され読めません。 Graphで開くとUNKNOWN PACKING FORMAT 0 in file /home/neurosurgery/Deksotp/sample-epo.fif Reported by Graph routine df_open_continuousと表示されます。

In [18]:
del epochs; # 変数を消去します。
save_epochs=mne.read_epochs('sample-epo.fif') # 読み込みます。
Reading sample-epo.fif ...
    Read a total of 4 projection items:
        PCA-v1 (1 x 102) active
        PCA-v2 (1 x 102) active
        PCA-v3 (1 x 102) active
        Average EEG reference (1 x 60) active
    Found the data of interest:
        t =    -199.80 ...     499.49 ms (None)
        0 CTF compensation matrices available
116 matching events found
Created an SSP operator (subspace dimension = 4)
116 matching events found
Created an SSP operator (subspace dimension = 4)
4 projection items activated
In [19]:
evoked=save_epochs['aud_l'].average() # 加算平均します。
print(evoked)
evoked.plot();
<Evoked  |  comment : 'aud_l', kind : average, time : [-0.199795, 0.499488], n_epochs : 55, n_channels x n_times : 364 x 106, ~3.9 MB>

試行毎の最大値を求めます。

In [20]:
print(len(save_epochs['aud_l'])) # aud_lの回数 
max_in_each_epoch=[e.max() for e in save_epochs['aud_l']] # eはsave_epochs['aud_l][0~55].get_data()
print(max_in_each_epoch[:4]) # 最初の4つだけ表示
x=save_epochs['aud_l'][0].get_data();
print(x.max())
55
[1.9375166762439395e-05, 1.6405517190615966e-05, 1.8545377077138902e-05, 2.0412808107036183e-05]
1.93751667624e-05

脳波のスケールが大きいんで、脳波ばかりが選ばれてます。

加算波形の読み書き・波形の差分

In [21]:
evoked_fname=data_path+'\\MEG\\sample/sample_audvis-ave.fif'
evoked1=mne.read_evokeds(evoked_fname,condition='Left Auditory',baseline=(None,0),proj=True) # projはSSP
Reading C:\Users\akira\mne_data\MNE-sample-data\MEG\sample/sample_audvis-ave.fif ...
    Read a total of 4 projection items:
        PCA-v1 (1 x 102) active
        PCA-v2 (1 x 102) active
        PCA-v3 (1 x 102) active
        Average EEG reference (1 x 60) active
    Found the data of interest:
        t =    -199.80 ...     499.49 ms (Left Auditory)
        0 CTF compensation matrices available
        nave = 55 - aspect type = 100
Projections have already been applied. Setting proj attribute to True.
Applying baseline correction (mode: mean)
In [22]:
evoked2=mne.read_evokeds(evoked_fname,condition='Right Auditory',baseline=(None,0),proj=True)
Reading C:\Users\akira\mne_data\MNE-sample-data\MEG\sample/sample_audvis-ave.fif ...
    Read a total of 4 projection items:
        PCA-v1 (1 x 102) active
        PCA-v2 (1 x 102) active
        PCA-v3 (1 x 102) active
        Average EEG reference (1 x 60) active
    Found the data of interest:
        t =    -199.80 ...     499.49 ms (Right Auditory)
        0 CTF compensation matrices available
        nave = 61 - aspect type = 100
Projections have already been applied. Setting proj attribute to True.
Applying baseline correction (mode: mean)
In [23]:
print([evoked1.nave,evoked2.nave]) # 左聴覚刺激と右聴覚刺激の回数
contrast=mne.combine_evoked([evoked1,evoked2],weights=[0.5,-0.5])
contrast=mne.combine_evoked([evoked1,-evoked2],weights='equal') # この書き方も可能です
print(contrast)
[55, 61]
<Evoked  |  comment : '0.500 * Left Auditory + 0.500 * -Right Auditory', kind : average, time : [-0.199795, 0.499488], n_epochs : 116, n_channels x n_times : 376 x 421, ~4.9 MB>
In [24]:
average=mne.combine_evoked([evoked1,evoked2],weights='nave')
print(contrast)
<Evoked  |  comment : '0.500 * Left Auditory + 0.500 * -Right Auditory', kind : average, time : [-0.199795, 0.499488], n_epochs : 116, n_channels x n_times : 376 x 421, ~4.9 MB>
In [25]:
epochs_eq=save_epochs.copy(); # 参照渡しなのでコピーにします。
epochs_eq=epochs_eq.equalize_event_counts(['aud_l','aud_r'])#左右の刺激回数を同じにする
print(epochs_eq) # 2つの返り値あり
print(epochs_eq[0]) # 1つめの帰り値 
print(epochs_eq[1]) #  2つめの返り値 余った試行
evoked1,evoked2=epochs_eq[0]['aud_l'].average(),epochs_eq[0]['aud_r'].average()
print(evoked1)
print(evoked2)
contrast=mne.combine_evoked([evoked1,evoked2],weights=[0.5,-0.5])
print(contrast)
Dropped 6 epochs
(<EpochsFIF  |  n_events : 110 (all good), tmin : -0.199795213158 (s), tmax : 0.499488032896 (s), baseline : (-0.1997952163219452, 0.0), ~36.1 MB, data loaded,
 u'aud_l': 55, u'aud_r': 55>, array([ 26,  43,  48,  91,  96, 101], dtype=int64))
<EpochsFIF  |  n_events : 110 (all good), tmin : -0.199795213158 (s), tmax : 0.499488032896 (s), baseline : (-0.1997952163219452, 0.0), ~36.1 MB, data loaded,
 u'aud_l': 55, u'aud_r': 55>
[ 26  43  48  91  96 101]
<Evoked  |  comment : 'aud_l', kind : average, time : [-0.199795, 0.499488], n_epochs : 55, n_channels x n_times : 364 x 106, ~3.9 MB>
<Evoked  |  comment : 'aud_r', kind : average, time : [-0.199795, 0.499488], n_epochs : 55, n_channels x n_times : 364 x 106, ~3.9 MB>
<Evoked  |  comment : '0.500 * aud_l + -0.500 * aud_r', kind : average, time : [-0.199795, 0.499488], n_epochs : 110, n_channels x n_times : 364 x 106, ~3.9 MB>
In [26]:
epochs_eq=save_epochs.copy(); # 参照渡しなのでコピーにします。
epochs_eq=epochs_eq.equalize_event_counts(['aud_l','aud_r'])
Dropped 6 epochs

時間周波数解析 induced power and inter trial coherence

In [27]:
import numpy as np
n_cycles=2 # Morlet waveletの周波数
freqs=np.arange(7,30,3) # MATLABだと7:3:30
from mne.time_frequency import tfr_morlet
power,itc=tfr_morlet(save_epochs,freqs=freqs,n_cycles=n_cycles,return_itc=True,decim=3,n_jobs=2) # n_jobsはCPUのスレッド数
print(power)
print(itc) # inter trial coherence 試行間でのcoherence
power.plot([power.ch_names.index('MEG 1332')]);
itc.plot([itc.ch_names.index('MEG 1332')]);
[Parallel(n_jobs=2)]: Done  14 tasks      | elapsed:    1.2s
[Parallel(n_jobs=2)]: Done 364 out of 364 | elapsed:    3.3s finished
<AverageTFR  |  time : [-0.199795, 0.499488], freq : [7.000000, 28.000000], nave : 116, channels : 364, ~4.4 MB>
<AverageTFR  |  time : [-0.199795, 0.499488], freq : [7.000000, 28.000000], nave : 116, channels : 364, ~4.4 MB>
No baseline correction applied
No baseline correction applied

逆問題 MNEとdSPM

In [28]:
from mne.minimum_norm import apply_inverse,read_inverse_operator
fname_inv=data_path+'\\MEG\\sample\\sample_audvis-meg-oct-6-meg-inv.fif'
inverse_operator=read_inverse_operator(fname_inv)
Reading inverse operator decomposition from C:\Users\akira\mne_data\MNE-sample-data\MEG\sample\sample_audvis-meg-oct-6-meg-inv.fif...
    Reading inverse operator info...
    [done]
    Reading inverse operator decomposition...
    [done]
    305 x 305 full covariance (kind = 1) found.
    Read a total of 4 projection items:
        PCA-v1 (1 x 102) active
        PCA-v2 (1 x 102) active
        PCA-v3 (1 x 102) active
        Average EEG reference (1 x 60) active
    Noise covariance matrix read.
    22494 x 22494 diagonal covariance (kind = 2) found.
    Source covariance matrix read.
    22494 x 22494 diagonal covariance (kind = 6) found.
    Orientation priors read.
    22494 x 22494 diagonal covariance (kind = 5) found.
    Depth priors read.
    Did not find the desired covariance matrix (kind = 3)
    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
    Read a total of 4 projection items:
        PCA-v1 (1 x 102) active
        PCA-v2 (1 x 102) active
        PCA-v3 (1 x 102) active
        Average EEG reference (1 x 60) active
    Source spaces transformed to the inverse solution coordinate frame
In [29]:
snr=3.0
lambda2=1.0/snr**2
method="dSPM"
stc=apply_inverse(evoked,inverse_operator,lambda2,method) # まずは加算波形の電流源推定
Preparing the inverse operator for use...
    Scaled noise and source covariance from nave = 1 to nave = 55
    Created the regularized inverter
    Created an SSP operator (subspace dimension = 3)
    Created the whitener using a full noise covariance matrix (3 small eigenvalues omitted)
    Computing noise-normalization factors (dSPM)...
[done]
Picked 305 channels from the data
Computing inverse...
(eigenleads need to be weighted)...
combining the current components...
(dSPM)...
[done]
In [30]:
print(stc) # Source Time Course 電流源の経時変化
print(stc.subject)
print(type(stc.data),stc.data.shape,stc.shape); # 頂点×時間
print(type(stc.times),stc.times.shape); # 時間
print(type(stc.vertices),len(stc.vertices));# 1つの頂点に自由度2の電流双極子
stc.save('mne_dSPM_inverse')
<SourceEstimate  |  7498 vertices, subject : sample, tmin : -199.795213158 (ms), tmax : 499.488032896 (ms), tstep : 6.65984043861 (ms), data size : 7498 x 106>
sample
(<type 'numpy.ndarray'>, (7498L, 106L), (7498L, 106L))
(<type 'numpy.ndarray'>, (106L,))
(<type 'list'>, 2)
Writing STC to disk...
[done]
In [31]:
fname_label=data_path+'\\MEG\\sample\\labels\\Aud-lh.label'
label=mne.read_label(fname_label)
print(label)
print(label.pos.shape) # 座標
print(label.values.shape) # 頂点の値
<Label  |  unknown, u'Aud-lh', lh : 1097 vertices>
(1097L, 3L)
(1097L,)

自発波形データからメッシュの一部だけの電流源推定を行います。

In [32]:
from mne.minimum_norm import apply_inverse_raw
start,stop=raw.time_as_index([0,15]) # 0~15秒の時間幅を取得
stc=apply_inverse_raw(raw,inverse_operator,lambda2,method,label,start,stop)
Preparing the inverse operator for use...
    Scaled noise and source covariance from nave = 1 to nave = 1
    Created the regularized inverter
    Created an SSP operator (subspace dimension = 3)
    Created the whitener using a full noise covariance matrix (3 small eigenvalues omitted)
    Computing noise-normalization factors (dSPM)...
[done]
Picked 305 channels from the data
Computing inverse...
(eigenleads need to be weighted)...
combining the current components...
[done]
In [33]:
print(stc)
stc.save('mne_dSPM_raw_inverse_Aud')
<SourceEstimate  |  33 vertices, subject : sample, tmin : 0.0 (ms), tmax : 14991.3008273 (ms), tstep : 6.65984043861 (ms), data size : 33 x 2252>
Writing STC to disk...
[done]
In [34]:
import matplotlib.pyplot as plt
%matplotlib inline
fig=plt.figure();
plt.plot(stc.times,stc.data.T,'b');
plt.xlim([stc.times[0],stc.times[-1]]);