读取 MIT-BIH 心律数据

安装 wfdb

pip install wfdb

这个包是专门用来读取 PhysioNet 这个网站的数据集的内容的。具体内容可以参考文档

下载数据集

在自己的项目目录下克隆:

git clone https://github.com/Nospoko/qrs-tutorial.git
cd qrs-tutorial

这个包是用于下载数据集的,但是这个包目前只基于 Python2.7 ,不嫌麻烦的话,可以自己进行简单的修改。mitdb.pydownload.py 两个文件是我进行修改过的,可以在这个链接上查看。修改完后,可以成功下载 MIT-BIH 的心律数据集,其他数据集没有尝试。修改完成后,编写下面的代码下载数据集:

import wfdb as wf
import numpy as np
from datasets import mitdb as dm

# Load paths of avaliable data files
records = dm.get_records()
print('There are {} record files'.format(len(records)))

读取心律数据

这部分参考 demo.ipynb

使用 rdrecord 函数读取数据记录
# Select one of them
path = records[0]
print('Loading file:', path)

# read the record and plot the data by using 'rdrecord' function
record = wf.rdrecord(path)
wf.plot_wfdb(record=record, 
              title='Record 100 from MIT-BIH Arrhythmia Database')
2759738-8207a52e0ae42d8d.png
使用简化的 rdsamp 函数读取某些通道和截取部分
signals, fields = wf.rdsamp(path)
print(fields)
=============
{'fs': 360,
 'sig_len': 650000,
 'n_sig': 2,
 'base_date': None,
 'base_time': None,
 'units': ['mV', 'mV'],
 'sig_name': ['MLII', 'V5'],
 'comments': ['69 M 1085 1629 x1', 'Aldomet, Inderal']}
使用 rdann 读取注释
annotation = wf.rdann(path, 'atr')
annotation.fs ### 获取采样频率
===============
360

读取记录和注释

# read a wfdb record and annotation. 
# Plot all channels, and the annotation on top of channel 0

record = wf.rdrecord(path, sampto=15000)
annotation = wf.rdann(path, 'atr', sampto=15000)

wf.plot_wfdb(record=record, annotation=annotation,
               title='Record 100 from MIT-BIH Arrhythmia Database',
               time_units='seconds')
2759738-608502bc8ffba854.png

读取一小片段 ECG 数据

ECG 信号的实际数值存储在属性 p_signal 数组中,我们可以从其中一个通道绘制一个小片段:

from matplotlib import pyplot as plt

# Select one of the channels (there are two)
chid = 0
data = record.p_signal
channel = data[:, chid]

print ('ECG channel type:', record.sig_name[chid])

# Plot only the first 2000 samples
howmany = 2000

# Calculate time values in seconds
times = np.arange(howmany, dtype = 'float') / record.fs
plt.plot(times, channel[ : howmany])
plt.xlabel('Time [s]')
plt.show()
2759738-3b5f361880f7f1db.png

想获取更多的信息可以使用 help(),比如想获得更多的 record 信息:

2759738-cd59dffcb4bf5d89.png

参考

[1]. wfdb-python

[2]. Nospoko/qrs-tutorial

[3]. Machine Learning for medicine: QRS detection in a single channel ECG signal (Part 1: data-set creation)

猜你喜欢

转载自blog.csdn.net/weixin_34262482/article/details/87323143