MNE SSP learning recording——Example: Environmental noise reduction from empty-room recordings(https://mne.tools/stable/auto_tutorials/preprocessing/50_artifact_correction_ssp.html#tut-artifact-ssp)
记录以下在这次学习中出现的问题(未解决),欢迎指出问题!
1. pycharm上编写如下代码:
import os
import numpy as np
import matplotlib.pyplot as plt
import mne
from mne.preprocessing import (create_eog_epochs, create_ecg_epochs,
compute_proj_ecg, compute_proj_eog)
sample_data_folder = mne.datasets.sample.data_path()
sample_data_raw_file = os.path.join(sample_data_folder, 'MEG', 'sample',
'sample_audvis_raw.fif')
# sample_data_raw_file = sample_data_raw_file.replace("\\", "/")
raw = mne.io.read_raw_fif(sample_data_raw_file)
system_projs = raw.info['projs']
raw.del_proj()
empty_room_file = os.path.join(sample_data_folder, 'MEG', 'sample',
'ernoise_raw.fif')
empty_room_raw = mne.io.read_raw_fif(empty_room_file)
empty_room_raw.del_proj()
for average in (False, True):
empty_room_raw.plot_psd(average=average, dB=False, xscale='log')
plt.show()
empty_room_projs = mne.compute_proj_raw(empty_room_raw, n_grad=3, n_mag=3)
mne.viz.plot_projs_topomap(empty_room_projs, colorbar=True, vlim='joint',
info=empty_room_raw.info)
plt.show()
fig, axs = plt.subplots(2, 3)
for idx, _projs in enumerate([system_projs, empty_room_projs[3:]]):
mne.viz.plot_projs_topomap(_projs, axes=axs[idx], colorbar=True,
vlim='joint', info=empty_room_raw.info)
plt.show()
mags = mne.pick_types(raw.info, meg='mag')
for title, projs in [('system', system_projs),
('subject-specific', empty_room_projs[3:])]:
raw.add_proj(projs, remove_existing=True)
fig = raw.plot(proj=True, order=mags, duration=1, n_channels=2)
fig.subplots_adjust(top=0.9) # make room for title
fig.suptitle('{} projectors'.format(title), size='xx-large', weight='bold')
plt.show()
运行结果如下——(运行结果与官网一致,这里引用官网原图)
问题出现在下面这里:
我的代码在pycharm里面运行后就只有第一排的三个图,第二排是空白的,之后还以一个空白的图片,如下所示,
尝试切换pycharm sci-view设置选项也无法解决,后注意到官网这里的mne版本
果断使用pip3 install -U mne==0.24.0安装新版本的mne后运行提示没用import
pooch package,又在pip3 install pooch且imoprt pooch后运行还是没有改变,因该不是mne版本问题!做到这,我怀疑是编译器pycharm的问题,于是使用anaconda安装好mne0.24.0版本和pooch package后重新编写上述代码,还是一样的效果——
与pycharm不同在于(顺序和官网一样了)
但是这里怎么变这么多了
老问题
想着之前好像安装pydot模块也遇到过这样的伪版本问题,怕不是清华镜像的问题,上https://anaconda.org/查看搜索mne看看,
下载一大堆库…
再试试
还是如上一样的运行结果…
但是不管怎样,最终结果还是和官网一样的——
做到这里后又仔细看了几遍官网关于的该example的讲解,还是没发现端倪,这里希望是换源的影响,这里我用的是清华源,不知道有没有遇到同样问题的并成功解决该问题或者成功做出该example的大佬指点一下,感激不尽!!!
今日又仔细看了一下,想了一些思路,只能把文章中的图分别输出,关于python中matplotlib.pyplot.subplots api和enumerate方法(不太看懂 😦 )也去看了一下,仍没有找出问题,下面说一下我今天的解决思路(未果!,可能是matplotlib的版本差异,我的是3.3.3版本的,大家如果不是这个版本的使用mne官方的源码实现出来了无比告诉我,非常感谢 😃)
官网预期效果图——
自己实现的效果图——
分析源码:
fig, axs = plt.subplots(2, 3)
for idx, _projs in enumerate([system_projs, empty_room_projs[3:]]):
mne.viz.plot_projs_topomap(_projs, axes=axs[idx], colorbar=True,
vlim='joint', info=empty_room_raw.info)
1. 首先我事先在这个for循环中print(idx),发现两个值0和1,遂使用以下语句看看能不能得到期望的图,结果还是gg:
mne.viz.plot_projs_topomap(system_projs, axes=axs[0], colorbar=True,
vlim='joint', info=empty_room_raw.info)
mne.viz.plot_projs_topomap(empty_room_projs[3:], axes=axs[1], colorbar=True,
vlim='joint', info=empty_room_raw.info)
输出还是带有空白
2. 这次想着如何去第1个思路基础上去掉空白:
fig, axs = plt.subplots(2, 3)
for idx, _projs in enumerate([system_projs, empty_room_projs[3:]]):
if idx==0:
mne.viz.plot_projs_topomap(system_projs, axes=axs[0], colorbar=True,
vlim='joint', info=empty_room_raw.info)
else:
break
输出如下——
虽然第二个空白图像消失了,但是下方的empty_room_projs[3:]却怎么也放不上去,实在不知道如何处理了,同求大佬指点迷津!
3. 想着图像拼接,也没能实现,想着保存到本地,也实现不了,保存的是一片空白,保存了个寂寞 😦!我查了viz.plot_projs_topomap()这个方法返回的的确是figure对象,这么这么难用呢?请大佬指点迷津!
4. 最后的两个图今天才发现问题,问什么是两个图???坑爹的pycharm,应该是我换了mne版本的问题!
下面是我最好修改好的代码,只能分别显示两个projs图像!见谅!希望能和大家一起讨论,找出解决方案!
import os
import numpy as np
import matplotlib.pyplot as plt
import mne
import PIL.Image as Image
import pooch
from mne.preprocessing import (create_eog_epochs, create_ecg_epochs,
compute_proj_ecg, compute_proj_eog)
sample_data_folder = mne.datasets.sample.data_path()
sample_data_raw_file = os.path.join(sample_data_folder, 'MEG', 'sample',
'sample_audvis_raw.fif')
raw = mne.io.read_raw_fif(sample_data_raw_file)
system_projs = raw.info['projs']
raw.del_proj()
empty_room_file = os.path.join(sample_data_folder, 'MEG', 'sample',
'ernoise_raw.fif')
empty_room_raw = mne.io.read_raw_fif(empty_room_file)
empty_room_raw.del_proj() # 去除投影
for average in (False, True):
empty_room_raw.plot_psd(average=average, dB=False, xscale='log')
empty_room_projs = mne.compute_proj_raw(empty_room_raw, n_grad=3, n_mag=3)
mne.viz.plot_projs_topomap(empty_room_projs, colorbar=True, vlim='joint',
info=empty_room_raw.info)
# fig, axs = plt.subplots(2, 3)
# for idx, _projs in enumerate([system_projs, empty_room_projs[3:]]):
# if idx==0:
# mne.viz.plot_projs_topomap(system_projs, axes=axs[0], colorbar=True,
# vlim='joint', info=empty_room_raw.info)
# # elif idx==1:
# # mne.viz.plot_projs_topomap(empty_room_projs[3:], axes=axs[0], colorbar=True,
# # vlim='joint', info=empty_room_raw.info)
# else:
# break
mne.viz.plot_projs_topomap(system_projs, colorbar=True,
vlim='joint', info=empty_room_raw.info)
mne.viz.plot_projs_topomap(empty_room_projs[3:], colorbar=True,
vlim='joint', info=empty_room_raw.info)
# for idx, _projs in enumerate([system_projs, empty_room_projs[3:]]):
# # if idx==0:
# # idx=0
# mne.viz.plot_projs_topomap(system_projs, axes=axs[1], colorbar=True,
# vlim='joint', info=empty_room_raw.info)
# elif idx==1:
# idx=1
# mne.viz.plot_projs_topomap(_projs, axes=axs[idx], colorbar=True,
# vlim='joint', info=empty_room_raw.info)
# else:
# break
# idx = 1
# mne.viz.plot_projs_topomap(empty_room_projs[3:], colorbar=True,
# vlim='joint', info=empty_room_raw.info)
mags = mne.pick_types(raw.info, meg='mag')
for title, projs in [('system', system_projs),
('subject-specific', empty_room_projs[3:])]:
raw.add_proj(projs, remove_existing=True)
fig = raw.plot(proj=True, order=mags, duration=1, n_channels=2)
fig.subplots_adjust(top=0.9) # make room for title
fig.suptitle('{} projectors'.format(title), size='xx-large', weight='bold')
plt.show()
events = mne.find_events(raw, stim_channel='STI 014')
event_id = {'auditory/left': 1}
# NOTE: appropriate rejection criteria are highly data-dependent
reject = dict(mag=4000e-15, # 4000 fT
grad=4000e-13, # 4000 fT/cm
eeg=150e-6, # 150 µV
eog=250e-6) # 250 µV
# time range where we expect to see the auditory N100: 50-150 ms post-stimulus
times = np.linspace(0.05, 0.15, 5)
epochs = mne.Epochs(raw, events, event_id, proj='delayed', reject=reject)
fig = epochs.average().plot_topomap(times, proj='interactive')
plt.show()