---
# System prepended metadata

title: '腦科學研究中在MNE-python中常用的物件(Raw,Epoch,Evoked,Time-frequency)'

---

Cheong Tong Hou

# 腦科學研究中在MNE-python中常用的物件(Raw,Epoch,Evoked,Time-frequency)




# Raw 是資料來源
    
Raw:
* mne.io.Raw(fname, allow_maxshield=False, preload=False,on_split_missing='raise', verbose=None)

raw data 有各式各樣的來源檔，較常見的是(.cnt、.ctf、.cdt、.edf)，亦可以從csv檔中讀取數據
讀取各式各樣的檔案有各自對應的function
Example:
* .cdt---mne.io.read_raw_cnt(...) 
.ctf---mne.io.read_raw_ctf(...)
.cdt---mne.io.read_raw_curry(...)
.edf---mne.io.read_raw_edf(...)
.cvs---mne.io.RawArray(...)

# Epoch 是由已經處理的raw所取得
Epoch:
* mne.Epochs(raw, events, event_id=None, tmin=- 0.2, tmax=0.5, baseline=(None, 0), picks=None, preload=False, reject=None, flat=None, proj=True, decim=1, reject_tmin=None, reject_tmax=None, detrend=None, on_missing='raise', reject_by_annotation=True, metadata=None, event_repeated='error', verbose=None)

Epoch是raw data的時間序列中有作maker的時段進行分段，Epoch可由實驗設計或個人需求而設定時間長度


# Evoked 平均後的Epoch物件
Evoked:
* mne.Evoked(fname, condition=None, proj=True, kind='average', allow_maxshield=False, verbose=None)

Example:
* evoked=epochs['event'].average()

Evoked是把Epoch的各個或單個maker作平均後去觀察腦波狀況 

# Time-frequency
Time-frequency常用的function有3個:
* mne.time_frequency.AverageTFR(...)
* mne.time_frequency.EpochsTFR(...)
* mne.time_frequency.CrossSpectralDensity(...)

Time-frequency在腦科學的領域中用於病理上的觀察，亦可以用於一般腦波數據上去觀察event中的腦波頻率的變化

```
上述的4個class中，底下其實還有各種的子集，並且對作為輸入的數據有很大的彈性，
若想了解更多可以到mne的官網上查看或是與我們相互交流^_^
```
# 視覺呈現 
```
在腦科學視覺呈現上，較常呈現的是Evoked、topographic、time-frequency
```
* Evoked:mne.viz.plot_evoked(...)
* Topographic:mne.viz.plot_topomap(...)
* Time-frequency:mne.viz.plot_tfr_topomap(...)

# 機械學習在腦科學中的角色
```
import numpy as np
import matplotlib.pyplot as plt

from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression

import mne
from mne.datasets import sample
from mne.decoding import (SlidingEstimator, GeneralizingEstimator, Scaler,
                          cross_val_multiscore, LinearModel, get_coef,
                          Vectorizer, CSP)

data_path='(the path your data save)'                         
file=data_path+'/(file_name)'     

epochs= mne.read_epochs(file, preload= True)
                          
a=epochs['event_a'].get_data()
b=epochs['event_b'].get_data()

ds=np.concatenate((a,b),axis=0)
temp_a=np.ones(a.shape[0])
temp_b=np.zeros(b.shape[0])
rds=np.concatenate((temp_a,temp_b),axis=0)

# start train
clf = make_pipeline(StandardScaler(), LogisticRegression(solver='lbfgs'))
time_decod = SlidingEstimator(clf, n_jobs=-1, scoring='roc_auc', verbose=True)
scores = cross_val_multiscore(time_decod, ds, rs, cv=5, n_jobs=-1)
scores = np.mean(scores, axis=0)

# plot
fig, ax = plt.subplots()
ax.plot(epochs.times, scores, label='score')
ax.axhline(.5, color='k', linestyle='--', label='chance')
ax.set_xlabel('Times')
ax.set_ylabel('AUC')  # Area Under the Curve
ax.legend()
ax.axvline(.0, color='k', linestyle='-')
ax.set_title('Sensor space decoding')