從 MNE-Python 的 class 取得數據

從原始檔 'de00110.con' 進行前處理:

import mne
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

# 本機端儲存 de00110.con 檔案的目錄
data_path = '/folder1/folder2/doc'

# 匯入 CON 檔
sid = 1
confile = data_path + '/de%.3d10.con' % sid  #
raw = mne.io.kit.read_raw_kit(confile, stim = [192, 193, 194], slope = '+')

# MNE-Python 會將原本分散在各 trigger channel 的標記合併到
# 同一個 channel 上 (`STI 014`)。後面的分析就利用 STI 014 
# 產生事件 onsets 的列表
events = mne.find_events(raw, stim_channel='STI 014')

# preload raw MEG data
raw.load_data()

# low pass fillter,由於這筆資料要做 time-frequency 
# ananlysis, 所以不執行低通濾波
#raw.filter(0, 60, phase= 'zero-double')

# duration of epochs 指定事件標記前後的時間範圍,從`raw`的資
# 料切割每次事發生時 tmin ~ tmax 的一小段訊號,以秒為單位
tmin = -0.1         # pre stimulis interval (in seconds) #
tmax = 0.6          # post stimulus interval #

# artifact rejection criteria 以 3pT 作為拒絕 epochs 的標準
reject = dict(mag=3e-12) # +/- 3pT

# 以文字標記事件類別。dictionary 的 `key` 可以自訂。value 必
# 須是 events 物件第三欄出現過的整數
event_id = {'AM40Hz':1, 'Tone1kHz': 2}

# KIT 的硬體組共有 255 個channel, 執行 epoching 的時候只要
# 留下 157 個 MEG sensor 的資料
picks = mne.pick_types(raw.info, meg= True, stim = False)    # channels to use in epochs #

# 基準線校正。`None` 等同於使用 tmin 的值
baseline = (None, 0) # what to use as baseline comparison - here it's pre-stim interval #

# 這邊才是真的開始執行 epoching
epochs = mne.Epochs(raw, events, event_id, tmin, tmax, proj = False,
                    picks = picks, baseline=baseline, preload = True,
                    reject=reject)
epochs = epochs.resample(1000, npad='auto')


# saving an epoch object into a fif file
epochs.save(data_path + '/%.3d_raw_epo.fif' % sid, overwrite= True)

# `epochs['AM40Hz']` 的意思是從 epochs 裡面,篩選出 'AM40Hz' 
# 這一類的資料; `.average()` 則是 epochs 底下的一種 method,
# 將選擇之後的 epochs 平均在一起
# 所以下面兩行指令會產生兩種平均波形
evoked1 = epochs['AM40Hz'].average()
evoked2 = epochs['Tone1kHz'].average()
# 如果不打算分類,而是要看所有事件合併再一起的總平均:
# evoked = epochs.average()

# saving multiple evoked objects into a fif file
mne.write_evokeds('%.3d-ave.fif' % sid, [evoked1, evoked2])
# saving an epoch object into a fif file
epochs.save(data_path + '/%.3d_raw_epo.fif' % sid, overwrite= True)

# artifact rejection 的結果可以另外存成一個csv檔案
drop_log = pd.DataFrame(epochs.drop_log)
drop_log.to_csv('s%.3d_droplog.csv' % sid)

到這邊為止,用到三種mne-pyton建立的 class,分別是 rawepochsevoked:

print(type(raw))
print(type(epochs))
print(type(evoked1))

<class 'mne.io.kit.kit.RawKIT'>
<class 'mne.epochs.Epochs'>
<class 'mne.evoked.EvokedArray'>

這三個物件底下都有一項 numpy ndarray, 裡面儲存 MEG 資料。從 mne-python 的 class 取得資料的方法如下:

# 請注意有時候要加上括弧,有時候不用括弧
raw.get_data()
epochs.get_data()
evoked1.data

由於上述三項指令的輸出都是 numpy ndarray,因此會繼承一些 numpy 的 methods

# 描述各個 ndarray 的維度
print(raw.get_data().shape)
print(epochs.get_data().shape)
print(evoked1.data.shape)

(257, 1075000) >> channels x samples
(193, 157, 700) >> events x channels x times
(157, 700) >> channels x times

後續要使用 HHT 的話,要從 epochs.get_data() 這筆資料開始

Select a repo