此时应该已经下载好MNE,并导入相应的包
import mne
其余的包是需要而导入
本文是参照官方文档来记录的
加载数据概述(Data Load)
我们使用MNE提供的数据库来学习,其中数据文件的扩展为.fif文件。我们来看看官方教程里面的代码。
sample_data_folder = mne.datasets.sample.data_path()
sample_data_raw_file = os.path.join(sample_data_folder, 'MEG', 'sample',
'sample_audvis_filt-0-40_raw.fif')
raw = mne.io.read_raw_fif(sample_data_raw_file)
- 其中
mne.datasets.sample.data_path()
可以下载这个官方的数据集(如果本地该文件下的目录没有下载的话)。然后这个函数会返回该数据集文件在本地的目录。 os.path.join
是生成我们用的数据集文件所在地具体路径。mne.io.read_raw_fif()
函数就是MNE中读取fif文件中数据的函数。
这里教程使用的是经过滤波器滤波和下采样版本的数据,即sample_audvis_filt-0-40_raw.fif 上述代码的输出:
Opening raw data file /home/circleci/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.
这里这个projection
先不管他,我目前也是看不懂,这个会在后面的详细章节中讲到。反正现在我们需要知道的是这个输出反映了函数读取数据过程中的一些信息。而关于我们读取的数据的详细信息可以用通过打印raw
对象以及其属性获得更加详细的信息。
print(raw)
print(raw.info)
上述代码的输出为:
<Raw | sample_audvis_filt-0-40_raw.fif, 376 x 41700 (277.7 s), ~3.3 MB, data not loaded>
<Info | 15 non-empty values
bads: 2 items (MEG 2443, EEG 053)
ch_names: MEG 0113, MEG 0112, MEG 0111, MEG 0122, MEG 0123, MEG 0121, MEG ...
chs: 204 Gradiometers, 102 Magnetometers, 9 Stimulus, 60 EEG, 1 EOG
custom_ref_applied: False
dev_head_t: MEG device -> head transform
dig: 146 items (3 Cardinal, 4 HPI, 61 EEG, 78 Extra)
file_id: 4 items (dict)
highpass: 0.1 Hz
hpi_meas: 1 item (list)
hpi_results: 1 item (list)
lowpass: 40.0 Hz
meas_date: 2002-12-03 19:01:10 UTC
meas_id: 4 items (dict)
nchan: 376
projs: PCA-v1: off, PCA-v2: off, PCA-v3: off, Average EEG reference: off
sfreq: 150.2 Hz
>
- 第一行<Raw|>就简单的反映了数据的形状以及大小
- 下面的是
raw.info
显示的详细信息里面我们可以看到有坏导啊、通道名啊、通道类型及数量啊、滤波器范围啊等等等。这里先不详细介绍,同样后面章节详细展开。我们接着往下走。 raw对象有一些内置的画图函数,比如plot
、plot_sensors
、plot_psd
等等,后续详细讲解。
预处理概述(Preprocessing)
MNE支持多种预处理方法,比如麦克斯韦滤波(Maxwell Filtering)、信号空间投影(Signal-Space Projection)、独立成分分析(Independent Components Analysis)、滤波(Filtering)、下采样(Downsampling)等。该文档中先使用了ICA独立成分分析,代码如下。
# set up and fit the ICA
ica = mne.preprocessing.ICA(n_components=20, random_state=97, max_iter=800)
ica.fit(raw)
ica.exclude = [1, 2] # details on how we picked these are omitted here
# 这里忽略移除这两个成分的原因
ica.plot_properties(raw, picks=ica.exclude)
orig_raw = raw.copy()
raw.load_data()
ica.apply(raw)
# show some frontal channels to clearly illustrate the artifact removal
chs = ['MEG 0111', 'MEG 0121', 'MEG 0131', 'MEG 0211', 'MEG 0221', 'MEG 0231',
'MEG 0311', 'MEG 0321', 'MEG 0331', 'MEG 1511', 'MEG 1521', 'MEG 1531',
'EEG 001', 'EEG 002', 'EEG 003', 'EEG 004', 'EEG 005', 'EEG 006',
'EEG 007', 'EEG 008']
chan_idxs = [raw.ch_names.index(ch) for ch in chs]
orig_raw.plot(order=chan_idxs, start=12, duration=4)
raw.plot(order=chan_idxs, start=12, duration=4)
- 上述代码就是将ICA应用在raw对象上,并与未应用的原对象做了图示对比。
- 这里需要注意的是
raw.load_data()
,apply()
方法要求将所有的数据都加载进内存(默认情况下只加载需要的数据)。
检测实验事件(Detecting Experiment Event)
文档的数据集包括几个“刺激”通道,这些通道记录了从刺激传递计算机发送的电信号(作为短暂的直流移位/方波脉冲)。这些脉冲(通常称为“触发器”)在该数据集中用于标记实验事件:刺激开始、刺激类型和参与者反应(按键)。各个STIM通道组合到一个通道上,以这样的方式,该通道上的电压水平可以明确地解码为特定事件类型。在较旧的Neuromag系统(例如用于记录样本数据的系统)上,该求和通道被称为STI 014,因此我们可以将该通道名称传递给mne。find_events函数用于恢复刺激事件的时间和身份。代码如下:
events = mne.find_events(raw, stim_channel='STI 014')
print(events[:5]) # show the first 5
代码输出为:
319 events found
Event IDs: [ 1 2 3 4 5 32]
[[6994 0 2]
[7086 0 3]
[7192 0 1]
[7304 0 4]
[7413 0 2]]
- 第一列表示样本号
- 最后一列表示事件ID
- 中间这一列通常被忽略 我们通常将事件ID和事件含义通过字典映射起来:
event_dict = {'auditory/left': 1, 'auditory/right': 2, 'visual/left': 3,
'visual/right': 4, 'smiley': 5, 'buttonpress': 32}
- 其中
/
符号,可以让我们只通过部分条件描述符来得到数据,具体来说就是我们通过event_dict['auditory']
可以得到ID为1和2的数据,通过event_dict['left']
可以获得ID为1和3的数据。 还有一个方便的功能plot_events()
,用于可视化记录期间的事件分布(以确保事件检测按预期工作)。而且通过raw.info
获得采样率,代码如下:
fig = mne.viz.plot_events(events, event_id=event_dict, sfreq=raw.info['sfreq'],
first_samp=raw.first_samp)
输出如下:
对于与事件无关的范例(例如,静态数据rest-state的分析),可以通过使用
mne.make_fixed_length_events
创建事件来提取规则间隔(可能重叠)的数据跨度。制作固定长度的事件。
这里着实开始不懂了,下面列一下题目,我们讲下一节