赞
踩
参考文章:
https://mp.weixin.qq.com/s/LXHP7dDOyhvCWi8vKWH5lw
https://www.pianshen.com/article/7715313899/
定义:Raw数据类型的对象用来存储连续型数据,核心数据以2维numpy array(分别是channels和samples)的形式存储,除此之外还包含了Info对象。
raw对象返回两个array:所选信道以及时间段内的数据和时间点,我们可以将它们分别赋值给data以及times
创建的过程非常简单
导包
设置存放地址
加载文件
# 引入python库
import mne
from mne.datasets import sample
import matplotlib.pyplot as plt
# sample的存放地址
data_path = sample.data_path()
# 该fif文件存放地址
fname = data_path + '/MEG/sample/sample_audvis_raw.fif'
# 存在文件则直接加载,否则从网上下载该数据
raw = mne.io.read_raw_fif(fname)
当然,为了节约空间,也可以对数据通过crop()
来进行切割,这里我采用的是不切割的版本
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, verbose=False).crop(tmax=60)
对于raw的信息(如channels数量、采样频率等),如果要查看则可用print(raw)
和print(raw.info)
print(raw)
结果:
print(raw.info)
结果:
Raw的访问方式有多种,它们实质上没有区别,但是在表达上会有一些区别,具体还是要根据实际情况来选择最适合的方式,这里我们先对两种情况进行分析:
其过程为:
代码:
sfreq = raw.info['sfreq'] # 采样频率
data, times = raw[:5, int(sfreq * 1):int(sfreq * 3)] # 从前五个channels的1~3s提取data和times
_ = plt.plot(times, data.T)
_ = plt.title('Sample channels')
plt.show()
运行结果:
其过程为:
代码:
# 获取10-20秒内的良好的MEG数据
picks = mne.pick_types(raw.info, meg=True, exclude='bads') # 选择良好的MEG信号,排除bads
t_idx = raw.time_as_index([10., 20.]) # 时间设置为10~20秒内的
data, times = raw[picks, t_idx[0]:t_idx[1]]
plt.plot(times, data.T)
plt.title("Sample channels")
plt.show()
运行结果:
通过调用一些已经封装好的函数,我们可以直接绘制其图像,这里举几个例子:
代码:
# 图一:绘制各通道的功率谱密度
raw.plot_psd()
plt.show()
# 图二:绘制通道频谱图作为topography
raw.plot_psd_topo()
plt.show()
# 图三:绘制SSP矢量图
raw.plot_projs_topomap()
plt.show()
# 图四:绘制电极位置
raw.plot_sensors()
plt.show()
结果:
图一:
图二:
图三:
图四:
对于这种图,我们需要对其进行手动的绘制,这就要求我们通过mne.io.RawArray类来手动创建Raw对象。
数据对应的单位:
V: eeg, eog, seeg, emg, ecg, bio, ecog
T: mag
T/m: grad
M: hbo, hbr
Am: dipole
AU: misc
构建一个Raw对象时,需要准备两种数据,一种是data数据,一种是Info数据,其中data数据是一个二维数据,形状为(n_channels,n_times)
这里我们看来自脑机接口社区上的两个案例:
代码:
import mne import numpy as np import matplotlib.pyplot as plt """ 生成一个大小为5x1000的二维随机数据 其中5代表5个通道,1000代表times """ data = np.random.randn(5, 1000) """ 创建info结构, 内容包括:通道名称和通道类型 设置采样频率为:sfreq=100 """ info = mne.create_info( ch_names=['MEG1', 'MEG2', 'EEG1', 'EEG2', 'EOG'], ch_types=['grad', 'grad', 'eeg', 'eeg', 'eog'], sfreq=100 ) # 利用mne.io.RawArray类创建Raw对象 custom_raw = mne.io.RawArray(data, info) print(custom_raw) """ 对图形进行缩放 对于实际的EEG / MEG数据,应使用不同的比例因子。 对通道eeg、grad,eog的数据进行2倍缩放 """ scalings = {'eeg': 2, 'grad': 2, 'eog': 2} custom_raw.plot(n_channels=5, scalings=scalings, title='Data from arrays', show=True, block=True) plt.show()
结果:
custom_raw
:
缩放图:
如果没有neo库,则需要先在控制台进行安装,安装代码为
pip install neo
代码:
import numpy as np import neo import mne import matplotlib.pyplot as plt """ 构建正余弦数据模拟mag,grad信号 其中采样频率为1000Hz,时间为0到10s. """ # 创建任意数据 sfreq = 1000 # 采样频率 times = np.arange(0, 10, 0.001) # Use 10000 samples (10s) sin = np.sin(times * 10) # 乘以 10 缩短周期 cos = np.cos(times * 10) sinX2 = sin * 2 cosX2 = cos * 2 # 数组大小为 4 X 10000. data = np.array([sin, cos, sinX2, cosX2]) # 定义 channel types and names. ch_types = ['mag', 'mag', 'grad', 'grad'] ch_names = ['sin', 'cos', 'sinX2', 'cosX2'] # 创建info对象 info = mne.create_info(ch_names=ch_names, sfreq=sfreq, ch_types=ch_types) # 利用mne.io.RawArray创建raw对象 raw = mne.io.RawArray(data, info) """ 对图形进行缩放 对于实际的EEG / MEG数据,应使用不同的比例因子。 对通道mag的数据进行2倍缩小,对grad的数据进行1.7倍缩小 """ scalings = {'mag': 2, 'grad': 1.7} raw.plot(n_channels=4, scalings=scalings, title='Data from arrays', show=True, block=True) """ 可以采用自动缩放比例 只要设置scalings='auto'即可 """ scalings = 'auto' raw.plot(n_channels=4, scalings=scalings, title='Auto-scaled Data from arrays', show=True, block=True) plt.show()
结果:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。