Python-MNE全套教程(官网翻译)-入门04:info数据结构

本教程将介绍mne.Info数据结构,它记录了各种数据的细节,并附加到Raw、Epochs和Evoked对象中。

老样子,先加载:

import mne

sample_data_folder = mne.datasets.sample.data_path()
sample_data_raw_file = (
    sample_data_folder / "MEG" / "sample" / "sample_audvis_filt-0-40_raw.fif"
)
raw = mne.io.read_raw_fif(sample_data_raw_file)

把info打出来看看

print(raw.info)

<Info | 14 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)
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

其实,为了查看或编辑Info对象,并不一定要加载Raw对象,我们可以使用mne.io.read_info()将所有相关信息提取到一个独立的Info对象中:

info = mne.io.read_info(sample_data_raw_file)
print(info)

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 <Info | 14 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) 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

正如上面的,Info对象有很多信息:

  • 关于系统(gantry angle, HPI details, sensor digitizations, channel names, …)
  • 关于实验(project name and ID, subject information, recording date, experimenter name or ID, …)
  • 关于数据(sampling frequency, applied filter frequencies, bad channels, projectors, …)

查询info对象

就像查字典一样就行:

print(info.keys())
print(info["ch_names"])

dict_keys([‘file_id’, ‘events’, ‘hpi_results’, ‘hpi_meas’, ‘subject_info’, ‘device_info’, ‘helium_info’, ‘hpi_subsystem’, ‘proc_history’, ‘meas_id’, ‘experimenter’, ‘description’, ‘proj_id’, ‘proj_name’, ‘meas_date’, ‘utc_offset’, ‘sfreq’, ‘highpass’, ‘lowpass’, ‘line_freq’, ‘gantry_angle’, ‘chs’, ‘dev_head_t’, ‘ctf_head_t’, ‘dev_ctf_t’, ‘dig’, ‘bads’, ‘ch_names’, ‘nchan’, ‘projs’, ‘comps’, ‘acq_pars’, ‘acq_stim’, ‘custom_ref_applied’, ‘xplotter_layout’, ‘kit_system_id’])
[…]# 这里写不下了我

大多数字段包含int、float或list数据类型,但chs字段特别值得一提:它包含一个字典列表,每个通道一个字典,其中包含除了通道记录的数据之外的所有关于通道的信息。这里我们显示了第一个通道字典的键:

print(info["chs"][0].keys())

dict_keys([‘scanno’, ‘logno’, ‘kind’, ‘range’, ‘cal’, ‘coil_type’, ‘loc’, ‘unit’, ‘unit_mul’, ‘ch_name’, ‘coord_frame’])

获取通道的索引子集

其实就是在通道名称和存储这些通道的索引之间进行转换。用到的两个函数是mne.pick_channels()mne.pick_types()pick_channels()最少要接受两个参数:所有通道名称的列表、要获取的通道名称列表,当然,也可以提供一个空列表来表示要获取的通道,然后指定要排除哪些通道:

print(mne.pick_channels(info["ch_names"], include=["MEG 0312", "EEG 005"]))

print(mne.pick_channels(info["ch_names"], include=[], exclude=["MEG 0312", "EEG 005"]))

[ 25 319]
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
18 19 20 21 22 23 24 26 27 28 29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
307 308 309 310 311 312 313 314 315 316 317 318 320 321 322 323 324 325
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
362 363 364 365 366 367 368 369 370 371 372 373 374 375]

pick_types()的工作方式不同,因为通道类型不能仅从通道名称来确定。因此,pick_types()需要一个Info对象,而不仅仅是一个通道名称列表,并且每个通道类型都有布尔关键词参数。默认行为是只选择MEG通道,并排除Info对象的bad字段中已经标记为“bad”的任何通道。

因此,如示例所见,如果要获得所有且仅获取所有EEG通道指标,还包含坏EEG通道,我们必须通过meg=False和exclude=[]:

print(mne.pick_types(info, meg=False, eeg=True, exclude=[]))

[315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
369 370 371 372 373 374]

注意pick_type()的meg和fnirs参数同时接受字符串和布尔值(因为这两个通道又分别有两个子通道嘛),以允许仅选择mag或grad通道(通过meg='mag’或meg=‘grad’)或仅选择氧血红蛋白或去氧血红蛋白通道(通过fnirs='hbo’或fnirs=‘hbr’)。

从Info对象中选择通道的第三种方法是使用mne.pick_channels_regexp()对通道名应用正则表达式匹配,如:

print(mne.pick_channels_regexp(info["ch_names"], "^E.G"))

这里我们匹配了EOG和EEG通道

[315, 316, 317, 318, 319, 320, 321, 322, 323, 324, 325, 326, 327, 328, 329, 330, 331, 332, 333, 334, 335, 336, 337, 338, 339, 340, 341, 342, 343, 344, 345, 346, 347, 348, 349, 350, 351, 352, 353, 354, 355, 356, 357, 358, 359, 360, 361, 362, 363, 364, 365, 366, 367, 368, 369, 370, 371, 372, 373, 374, 375]

不会正则的可以复习复习:正则表达式的python官方文档

请注意,这里容易犯错:pick_channels()pick_channels_regexp()都对通道名称列表进行操作,因此它们不知道哪些通道在info['bad ']中被标记为“bad”。请谨慎使用,以免意外选择不良频道。

获取通道类型信息

这样就行:

print(mne.channel_type(info, 25))

grad

要一次获得多个通道类型,可以嵌入channel_type(),或者使用Raw、Epochs或Evoked实例的get_channel_types()方法:

picks = (25, 76, 77, 319)
print([mne.channel_type(info, x) for x in picks])
print(raw.get_channel_types(picks=picks))

[‘grad’, ‘grad’, ‘mag’, ‘eeg’]
[‘grad’, ‘grad’, ‘mag’, ‘eeg’]

或者,我们可以使用channel_indices_by_type()获取的索引,它返回一个以通道类型作为键名的字典,并将通道索引列表作为值:

ch_idx_by_type = mne.channel_indices_by_type(info)
print(ch_idx_by_type.keys())
print(ch_idx_by_type["eog"])

dict_keys([‘eeg’, ‘csd’, ‘stim’, ‘eog’, ‘ecg’, ‘emg’, ‘misc’, ‘resp’, ‘chpi’, ‘exci’, ‘ias’, ‘syst’, ‘seeg’, ‘dipole’, ‘gof’, ‘bio’, ‘ecog’, ‘dbs’, ‘temperature’, ‘gsr’, ‘ref_meg’, ‘mag’, ‘grad’, ‘hbo’, ‘hbr’, ‘fnirs_cw_amplitude’, ‘fnirs_fd_ac_amplitude’, ‘fnirs_fd_phase’, ‘fnirs_od’, ‘eyegaze’, ‘pupil’])
[375]

从Info对象中删除通道

如下所示,保留meg和eeg,删掉其他的:

print(info["nchan"])
eeg_indices = mne.pick_types(info, meg=False, eeg=True)
print(mne.pick_info(info, eeg_indices)["nchan"])

376
59

默认情况下,pick_info()将在修改原始Info对象之前复制它;如果您想原地修改,请传递参数copy=False。

  • 21
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值