深度学习Heartpy心电图分析

本文介绍了使用Python的heartpy库处理心电图(ECG)信号,包括读取XML文件中的心电信号,进行滤波处理以去除噪声,以及绘制十二导联心电图。此外,还提到了NeuroKit2和hrv库在心电图分析中的应用,如心率变异性和信号处理。示例代码详细展示了数据读取、滤波和绘制心电图的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


在这里插入图片描述

想学习架构师构建流程请跳转:Java架构师系统架构设计

1 heartpy介绍

在这里插入图片描述

该库提供了处理以下几种信号的方法:来自智能手表和智能手环的常规PPG信号和常规(或含噪)ECG信号,具体可查看文档,文档地址

安装方法

pip install heartpy

HeartPy V1.2 has landed! The structure of the package has been reworked to be in separate modules now in preparation of the next big update, which will feature many analysis expansions and the first steps towards a GUI for HeartPy. HeartPy has been growing steadily and had reached the point where it became cluttered and unwieldy to keep in a single file. The API remains unchanged.

An ‘Examples’ folder has been added to the repo which will be expanded soon. Now there’s two notebooks explaining how to analyse ppg signals from smartwatches and smart rings.

Colorblind support has been added, see this notebook in the examples folder

文档中有很多案例和使用课程
翻译有点太直了什么笔记本笔记本电脑…
在这里插入图片描述

2 使用Pands读取数据

2.1 数据说明

xml为各个心电厂商的心电文件,为了不泄露信息,只展示电信号振幅数据,网上找了一些案例数据,因为画波形不是很难

在这里插入图片描述

用浏览器打开是这个样子。

在这里插入图片描述

我们调用Python中的LXML库来解析,文件操作用OS和GLOB库。

from lxml import etree#导入lxml库
import os
import glob
path = 'D:\ECG' #设置路径
path_list = os.listdir(path) #获取目录中的内容
path_list.sort(key=lambda x:int(x.split('.')[0])) #整理文件数据及类型
path_file_number = glob.glob('D:\ECG\*.xml') #获取此路径下的所有XML文件并返回一个List
local = 0
sj = open(os.path.join(path,path_list[local]),'rb') #从第一个开始打开文件
tree = etree.parse(sj) #将xml解析为树结构
root = tree.getroot() #获得该树的树根

由于存储的心电数据是基于临床十二导联记录下来的,故XML文件里会有12个类似于,…,这样的12个节点,我们仅需对这12个节点做操作即可,开头的节点删去。

for child in root[1:2]: #从第二个节点开始操作
    lst1 = []
    lst1 = child.text
    lst1 = lst1.split(" ")
    lst1 = lst1[:-1:] #由于发现心电数据中最后一位总是有空格存在,故删掉最后一位。
    I = [ int(float(i)) for i in lst1 ] #将心电数据先转化为浮点型再转化为整型,用于绘制心电图
    #print(I)
for child in root[2:3]:
    lst2 = []
    lst2 = child.text
    lst2 = lst2.split(" ")
    lst2 = lst2[:-1:]
    II = [ int(float(i)) for i in lst2 ]
    #print(II)
for child in root[3:4]:
    lst3 = []
    lst3 = child.text
    lst3 = lst3.split(" ")
    lst3 = lst3[:-1:]
    III = [ int(float(i)) for i in lst3 ]
    #print(III)
for child in root[4:5]:
    lst4 = []
    lst4 = child.text
    lst4 = lst4.split(" ")
    lst4 = lst4[:-1:]
    AVF = [ int(float(i)) for i in lst4 ]
    #print(AVF)  
for child in root[5:6]:
    lst5 = []
    lst5 = child.text
    lst5 = lst5.split(" ")
    lst5 = lst5[:-1:]
    AVR = [ int(float(i)) for i in lst5 ]
    #print(AVR)
for child in root[6:7]:
    lst6 = []
    lst6 = child.text
    lst6 = lst6.split(" ")
    lst6 = lst6[:-1:]
    AVL = [ int(float(i)) for i in lst6 ]
    #print(AVL)
for child in root[7:8]:
    lst7 = []
    lst7 = child.text
    lst7 = lst7.split(" ")
    lst7 = lst7[:-1:]
    V1 = [ int(float(i)) for i in lst7 ]
    #print(V1)
for child in root[8:9]:
    lst8 = []
    lst8 = child.text
    lst8 = lst8.split(" ")
    lst8 = lst8[:-1:]
    V2 = [ int(float(i)) for i in lst8 ]
    #print(V2)
for child in root[9:10]:
    lst9 = []
    lst9 = child.text
    lst9 = lst9.split(" ")
    lst9 = lst9[:-1:]
    V3 = [ int(float(i)) for i in lst9 ]
    #print(V3)
for child in root[10:11]:
    lst10 = []
    lst10 = child.text
    lst10 = lst10.split(" ")
    lst10 = lst10[:-1:]
    V4 = [ int(float(i)) for i in lst10 ]
    #print(V4)
for child in root[11:12]:
    lst11 = []
    lst11 = child.text
    lst11 = lst11.split(" ")
    lst11 = lst11[:-1:]
    V5 = [ int(float(i)) for i in lst11 ]
    #print(V5)
for child in root[12:13]:
    lst12 = []
    lst12 = child.text
    lst12 = lst12.split(" ") 
    lst12 = lst12[:-1:]
    V6 = [ int(float(i)) for i in lst12 ]
    #print(V6)

我们截取其中一个节点的信息,可以看到已经成功解析并读取上来。
在这里插入图片描述

2.2 心电图的绘制

成功得到心电数据之后,我们调用Python中的2D绘图库Matplotlib来进行心电图的绘制。

导入相关模块

import matplotlib.pyplot as  plt
import matplotlib
import numpy as  np
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg,NavigationToolbar2Tk
from matplotlib.backend_bases import key_press_handler
from matplotlib.figure import Figure

我们将心电数据中的12个节点依次绘制出心电信号曲线,组成一张完整的十二导联心电图。

a = plt.subplot(12,1,1); #12张中的第一张
plt.plot(np.linspace(0, 100 * np.pi, 5000),I) #5000个数据就给5000个点
plt.ylabel('I')
plt.plot()
plt.grid()
 
a = plt.subplot(12,1,2);
plt.plot(np.linspace(0, 100 * np.pi, 5000),II)
plt.ylabel('II')
plt.plot()
plt.grid()
 
a = plt.subplot(12,1,3);
plt.plot(np.linspace(0, 100 * np.pi, 5000),III)
plt.ylabel('III')
plt.plot()
plt.grid()
 
a = plt.subplot(12,1,4);
plt.plot(np.linspace(0, 100 * np.pi, 5000),AVF)
plt.ylabel('AVF')
plt.plot()
plt.grid()
 
a = plt.subplot(12,1,5);
plt.plot(np.linspace(0, 100 * np.pi, 5000),AVR)
plt.ylabel('AVR')
plt.plot()
plt.grid()
 
a = plt.subplot(12,1,6);
plt.plot(np.linspace(0, 100 * np.pi, 5000),AVL)
plt.ylabel('AVL')
plt.plot()
plt.grid()
 
a = plt.subplot(12,1,7);
plt.plot(np.linspace(0, 100 * np.pi, 5000),V1)
plt.ylabel('V1')
plt.plot()
plt.grid()
 
a = plt.subplot(12,1,8);
plt.plot(np.linspace(0, 100 * np.pi, 5000),V2)
plt.ylabel('V2')
plt.plot()
plt.grid()
 
a = plt.subplot(12,1,9);
plt.plot(np.linspace(0, 100 * np.pi, 5000),V3)
plt.ylabel('V3')
plt.plot()
plt.grid()
 
a = plt.subplot(12,1,10);
plt.plot(np.linspace(0, 100 * np.pi, 5000),V4) 
plt.ylabel('V4')
plt.plot()
plt.grid()
 
a = plt.subplot(12,1,11);
plt.plot(np.linspace(0, 100 * np.pi, 5000),V5)
plt.ylabel('V5')
plt.plot()
plt.grid()
 
a = plt.subplot(12,1,12);
plt.plot(np.linspace(0, 100 * np.pi, 5000),V6)
plt.ylabel('V6')
plt.plot()
plt.grid()
plt.show()

结果:
在这里插入图片描述

3 心电滤波

滤波的目的是去出噪音,使心电波形更加平滑。这里用到了第三方库heartpy,它是专门用于处理心电数据的python库。

import heartpy as hp
import pandas as pd
import matplotlib.pyplot as plt

"""
   高通滤波
   低通滤波
   带通滤波
"""
#高通滤波
def high_filter():
    path = 'D:\ECG'
    path_list = os.listdir(path)
    path_list.sort(key=lambda x:int(x.split('.')[0]))
    #print(path_list)
    global local
    local = local
    fw = open(os.path.join(path,path_list[local]),'rb')
    tree = etree.parse(fw)
    root = tree.getroot()
    for child in root[1:2]:
        lst1 = []
        lst1 = child.text
        lst1 = lst1.split(" ")
        lst1 = lst1[:-1:]
        I = [ int(float(i)) for i in lst1 ]
        I = hp.filter_signal(I, cutoff=0.75, sample_rate=500.0, order=3, filtertype='highpass')
        #print(I)
 
#低通滤波
        I = hp.filter_signal(I, cutoff=15, sample_rate=500.0, order=3, filtertype='lowpass')
 
#带通滤波
        I = hp.filter_signal(I, cutoff=[0.75, 15], sample_rate=500.0, order=3, filtertype='bandpass')

正常
在这里插入图片描述
带滤波
在这里插入图片描述

4 心电特性数据读取处理

还是一样利用Heartpy这个专门处理心电数据的第三方库来帮我们处理

    I = hp.scale_data(I)
    working_data, measures = hp.process(I, 500.0)
 
    print(working_data)
    print(measures)

通过输出结果我们可以很轻易地从里面提取到有用的信息,例如R峰的定位、各类波的间期持续时间、以及得到HRV分析的一些常用数据指标。

在这里插入图片描述
可以从中看到一些心率,心率间隔,rr间期之类的。

5 ECG信号处理相关的开源Python库

5.1 NeuroKit2

在这里插入图片描述
在这里插入图片描述

详细信息,可以查看具体文档https://neuropsychology.github.io/NeuroKit/

安装方法

conda install neurokit2
# 或者
pip install neurokit2

该库提供了一些比较有用的ECG处理方向

  1. 基于ECG信号的呼吸分析
  2. 呼吸率变异分析
  3. 心率变异分析
  4. P,Q,S,T的定位
  5. 输出模拟信号:ECG,RSP,EMG和EDA

5.2 hrv

在这里插入图片描述

该库主要针对心率变异性分析,具体可查看文档https://hrv.readthedocs.io/en/latest/index.html

### 关于心电图大模型的Python实现 在生物医学领域,特别是针对心电图(ECG/EKG)的大规模建模和分析Python 提供了许多强大的工具库和支持框架。以下是一些常用的 Python 工具库以及其实现方法: #### 1. **TensorFlow 和 Keras** TensorFlow 是一种广泛使用的深度学习框架,其高级 API Keras 可用于构建复杂的神经网络模型。对于心电图数据的分类或异常检测任务,可以采用卷积神经网络 (CNN) 或长短时记忆网络 (LSTM)[^2]。 ```python import tensorflow as tf from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense, Conv1D, MaxPooling1D, Flatten, LSTM # 构建 CNN-LSTM 混合模型 model = Sequential() model.add(Conv1D(filters=64, kernel_size=3, activation='relu', input_shape=(None, 1))) model.add(MaxPooling1D(pool_size=2)) model.add(LSTM(100, return_sequences=False)) model.add(Dense(1, activation='sigmoid')) # 编译模型 model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy']) # 打印模型结构 model.summary() ``` 此代码片段展示了如何结合一维卷积层和 LSTM 层来处理时间序列数据,适用于心电图信号的特征提取和分类任务[^2]。 --- #### 2. **PyTorch** PyTorch 是另一种流行的深度学习框架,支持动态计算图并提供灵活的张量操作接口。它同样适合开发复杂的心电图分析模型。 ```python import torch import torch.nn as nn class ECGModel(nn.Module): def __init__(self): super(ECGModel, self).__init__() self.conv1d = nn.Conv1d(in_channels=1, out_channels=64, kernel_size=3) self.lstm = nn.LSTM(input_size=64, hidden_size=100, num_layers=2, batch_first=True) self.fc = nn.Linear(100, 1) def forward(self, x): x = torch.relu(self.conv1d(x)) x, _ = self.lstm(x) x = self.fc(x[:, -1, :]) return torch.sigmoid(x) # 初始化模型 model = ECGModel() # 查看模型参数 print(model) ``` 这段 PyTorch 代码定义了一个简单的 CNN-LSTM 结构,能够适应不同长度的时间序列输入[^2]。 --- #### 3. **Scikit-learn** 虽然 Scikit-learn 主要面向传统机器学习算法,但它也可以作为预处理工具配合其他深度学习框架一起使用。例如,在应用深度学习之前,可以通过 PCA 或 t-SNE 对高维度的心电图数据降维[^4]。 ```python from sklearn.decomposition import PCA import numpy as np # 假设 X 是原始心电图数据矩阵 pca = PCA(n_components=50) X_reduced = pca.fit_transform(X) # 输出降维后的形状 print(f"Reduced shape: {X_reduced.shape}") ``` 通过这种方式减少冗余信息后,再将其传递给更深层次的学习模型可能获得更好的效果[^4]。 --- #### 4. **Heartpy** `heartpy` 是一个专注于心率变异性 (HRV) 分析的小型开源项目,尽管它的功能相对有限,但对于初学者来说是一个不错的起点[^1]。 安装命令如下: ```bash pip install heartpy ``` 基本用法示例: ```python import heartpy as hp data = ... # 加载您的心电图数据 working_data, measures = hp.process(data, sample_rate=100.0) # 显示测量指标 for key, value in measures.items(): print(f"{key}: {value}") ``` 该库提供了简单易用的功能来进行基础统计分析,但不涉及任何深度学习部分。 --- #### 5. **Mne-Python** 如果目标扩展至脑电图或其他类型的生理信号,则 `mne-python` 将成为不可或缺的选择之一。不过需要注意的是,这个包更多关注 EEG 数据而非纯粹意义上的 ECG[^3]。 安装方式: ```bash conda install mne -c conda-forge ``` 或者直接 pip 安装: ```bash pip install mne ``` --- ### 总结 以上列举了几种主流的技术路线及其对应的 Python 实现方案。具体选择取决于实际应用场景和技术背景需求。无论是 TensorFlow/Keras 还是 PyTorch 都非常适合搭建高性能的心电图识别系统;而 scikit-learn 则可充当辅助角色帮助优化前期准备工作流程效率最大化[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

赵广陆

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值