医工互联

 找回密码
 注册[Register]

手机动态码快速登录

手机号快速登录

微信登录

微信扫一扫,快速登录

QQ登录

只需一步,快速开始

查看: 259|回复: 0
收起左侧

深度学习Heartpy心电图分析

[复制链接]

  离线 

发表于 2023-8-4 13:14:19 来自手机 | 显示全部楼层 |阅读模式 <

1 heartpy介绍

1.png

该库提供了处理以下几种信号的方法:来自智能手表和智能手环的常规PPG信号和常规(或含噪)ECG信号,具体可查看文档,文档地址
安装方法
  1. 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.png

2 使用Pands读取数据

2.1 数据说明

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

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

我们调用Python中的LXML库来解析,文件操作用OS和GLOB库。
  1. from lxml import etree#导入lxml库
  2. import os
  3. import glob
  4. path = 'D:\ECG' #设置路径
  5. path_list = os.listdir(path) #获取目录中的内容
  6. path_list.sort(key=lambda x:int(x.split('.')[0])) #整理文件数据及类型
  7. path_file_number = glob.glob('D:\ECG\*.xml') #获取此路径下的所有XML文件并返回一个List
  8. local = 0
  9. sj = open(os.path.join(path,path_list[local]),'rb') #从第一个开始打开文件
  10. tree = etree.parse(sj) #将xml解析为树结构
  11. root = tree.getroot() #获得该树的树根
复制代码
由于存储的心电数据是基于临床十二导联记录下来的,故XML文件里会有12个类似于,…,这样的12个节点,我们仅需对这12个节点做操作即可,开头的节点删去。
  1. for child in root[1:2]: #从第二个节点开始操作
  2.     lst1 = []
  3.     lst1 = child.text
  4.     lst1 = lst1.split(" ")
  5.     lst1 = lst1[:-1:] #由于发现心电数据中最后一位总是有空格存在,故删掉最后一位。
  6.     I = [ int(float(i)) for i in lst1 ] #将心电数据先转化为浮点型再转化为整型,用于绘制心电图
  7.     #print(I)
  8. for child in root[2:3]:
  9.     lst2 = []
  10.     lst2 = child.text
  11.     lst2 = lst2.split(" ")
  12.     lst2 = lst2[:-1:]
  13.     II = [ int(float(i)) for i in lst2 ]
  14.     #print(II)
  15. for child in root[3:4]:
  16.     lst3 = []
  17.     lst3 = child.text
  18.     lst3 = lst3.split(" ")
  19.     lst3 = lst3[:-1:]
  20.     III = [ int(float(i)) for i in lst3 ]
  21.     #print(III)
  22. for child in root[4:5]:
  23.     lst4 = []
  24.     lst4 = child.text
  25.     lst4 = lst4.split(" ")
  26.     lst4 = lst4[:-1:]
  27.     AVF = [ int(float(i)) for i in lst4 ]
  28.     #print(AVF)  
  29. for child in root[5:6]:
  30.     lst5 = []
  31.     lst5 = child.text
  32.     lst5 = lst5.split(" ")
  33.     lst5 = lst5[:-1:]
  34.     AVR = [ int(float(i)) for i in lst5 ]
  35.     #print(AVR)
  36. for child in root[6:7]:
  37.     lst6 = []
  38.     lst6 = child.text
  39.     lst6 = lst6.split(" ")
  40.     lst6 = lst6[:-1:]
  41.     AVL = [ int(float(i)) for i in lst6 ]
  42.     #print(AVL)
  43. for child in root[7:8]:
  44.     lst7 = []
  45.     lst7 = child.text
  46.     lst7 = lst7.split(" ")
  47.     lst7 = lst7[:-1:]
  48.     V1 = [ int(float(i)) for i in lst7 ]
  49.     #print(V1)
  50. for child in root[8:9]:
  51.     lst8 = []
  52.     lst8 = child.text
  53.     lst8 = lst8.split(" ")
  54.     lst8 = lst8[:-1:]
  55.     V2 = [ int(float(i)) for i in lst8 ]
  56.     #print(V2)
  57. for child in root[9:10]:
  58.     lst9 = []
  59.     lst9 = child.text
  60.     lst9 = lst9.split(" ")
  61.     lst9 = lst9[:-1:]
  62.     V3 = [ int(float(i)) for i in lst9 ]
  63.     #print(V3)
  64. for child in root[10:11]:
  65.     lst10 = []
  66.     lst10 = child.text
  67.     lst10 = lst10.split(" ")
  68.     lst10 = lst10[:-1:]
  69.     V4 = [ int(float(i)) for i in lst10 ]
  70.     #print(V4)
  71. for child in root[11:12]:
  72.     lst11 = []
  73.     lst11 = child.text
  74.     lst11 = lst11.split(" ")
  75.     lst11 = lst11[:-1:]
  76.     V5 = [ int(float(i)) for i in lst11 ]
  77.     #print(V5)
  78. for child in root[12:13]:
  79.     lst12 = []
  80.     lst12 = child.text
  81.     lst12 = lst12.split(" ")
  82.     lst12 = lst12[:-1:]
  83.     V6 = [ int(float(i)) for i in lst12 ]
  84.     #print(V6)
复制代码
我们截取其中一个节点的信息,可以看到已经成功解析并读取上来。
5.png

2.2 心电图的绘制

成功得到心电数据之后,我们调用Python中的2D绘图库Matplotlib来进行心电图的绘制。
导入相关模块
  1. import matplotlib.pyplot as  plt
  2. import matplotlib
  3. import numpy as  np
  4. from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg,NavigationToolbar2Tk
  5. from matplotlib.backend_bases import key_press_handler
  6. from matplotlib.figure import Figure
复制代码
我们将心电数据中的12个节点依次绘制出心电信号曲线,组成一张完整的十二导联心电图。
  1. a = plt.subplot(12,1,1); #12张中的第一张
  2. plt.plot(np.linspace(0, 100 * np.pi, 5000),I) #5000个数据就给5000个点
  3. plt.ylabel('I')
  4. plt.plot()
  5. plt.grid()
  6. a = plt.subplot(12,1,2);
  7. plt.plot(np.linspace(0, 100 * np.pi, 5000),II)
  8. plt.ylabel('II')
  9. plt.plot()
  10. plt.grid()
  11. a = plt.subplot(12,1,3);
  12. plt.plot(np.linspace(0, 100 * np.pi, 5000),III)
  13. plt.ylabel('III')
  14. plt.plot()
  15. plt.grid()
  16. a = plt.subplot(12,1,4);
  17. plt.plot(np.linspace(0, 100 * np.pi, 5000),AVF)
  18. plt.ylabel('AVF')
  19. plt.plot()
  20. plt.grid()
  21. a = plt.subplot(12,1,5);
  22. plt.plot(np.linspace(0, 100 * np.pi, 5000),AVR)
  23. plt.ylabel('AVR')
  24. plt.plot()
  25. plt.grid()
  26. a = plt.subplot(12,1,6);
  27. plt.plot(np.linspace(0, 100 * np.pi, 5000),AVL)
  28. plt.ylabel('AVL')
  29. plt.plot()
  30. plt.grid()
  31. a = plt.subplot(12,1,7);
  32. plt.plot(np.linspace(0, 100 * np.pi, 5000),V1)
  33. plt.ylabel('V1')
  34. plt.plot()
  35. plt.grid()
  36. a = plt.subplot(12,1,8);
  37. plt.plot(np.linspace(0, 100 * np.pi, 5000),V2)
  38. plt.ylabel('V2')
  39. plt.plot()
  40. plt.grid()
  41. a = plt.subplot(12,1,9);
  42. plt.plot(np.linspace(0, 100 * np.pi, 5000),V3)
  43. plt.ylabel('V3')
  44. plt.plot()
  45. plt.grid()
  46. a = plt.subplot(12,1,10);
  47. plt.plot(np.linspace(0, 100 * np.pi, 5000),V4)
  48. plt.ylabel('V4')
  49. plt.plot()
  50. plt.grid()
  51. a = plt.subplot(12,1,11);
  52. plt.plot(np.linspace(0, 100 * np.pi, 5000),V5)
  53. plt.ylabel('V5')
  54. plt.plot()
  55. plt.grid()
  56. a = plt.subplot(12,1,12);
  57. plt.plot(np.linspace(0, 100 * np.pi, 5000),V6)
  58. plt.ylabel('V6')
  59. plt.plot()
  60. plt.grid()
  61. plt.show()
复制代码
结果:
6.png

3 心电滤波

滤波的目的是去出噪音,使心电波形更加平滑。这里用到了第三方库heartpy,它是专门用于处理心电数据的python库。
  1. import heartpy as hp
  2. import pandas as pd
  3. import matplotlib.pyplot as plt
  4. """
  5.    高通滤波
  6.    低通滤波
  7.    带通滤波
  8. """
  9. #高通滤波
  10. def high_filter():
  11.     path = 'D:\ECG'
  12.     path_list = os.listdir(path)
  13.     path_list.sort(key=lambda x:int(x.split('.')[0]))
  14.     #print(path_list)
  15.     global local
  16.     local = local
  17.     fw = open(os.path.join(path,path_list[local]),'rb')
  18.     tree = etree.parse(fw)
  19.     root = tree.getroot()
  20.     for child in root[1:2]:
  21.         lst1 = []
  22.         lst1 = child.text
  23.         lst1 = lst1.split(" ")
  24.         lst1 = lst1[:-1:]
  25.         I = [ int(float(i)) for i in lst1 ]
  26.         I = hp.filter_signal(I, cutoff=0.75, sample_rate=500.0, order=3, filtertype='highpass')
  27.         #print(I)
  28. #低通滤波
  29.         I = hp.filter_signal(I, cutoff=15, sample_rate=500.0, order=3, filtertype='lowpass')
  30. #带通滤波
  31.         I = hp.filter_signal(I, cutoff=[0.75, 15], sample_rate=500.0, order=3, filtertype='bandpass')
复制代码
正常
7.png

带滤波
8.png

4 心电特性数据读取处理

还是一样利用Heartpy这个专门处理心电数据的第三方库来帮我们处理
  1.     I = hp.scale_data(I)
  2.     working_data, measures = hp.process(I, 500.0)
  3.     print(working_data)
  4.     print(measures)
复制代码
通过输出结果我们可以很轻易地从里面提取到有用的信息,例如R峰的定位、各类波的间期持续时间、以及得到HRV分析的一些常用数据指标。
9.png

可以从中看到一些心率,心率间隔,rr间期之类的。
5 ECG信号处理相关的开源Python库

5.1 NeuroKit2

10.png

11.png

详细信息,可以查看具体文档https://neuropsychology.github.io/NeuroKit/
安装方法
  1. conda install neurokit2
  2. # 或者
  3. pip install neurokit2
复制代码
该库提供了一些比较有用的ECG处理方向

  • 基于ECG信号的呼吸分析
  • 呼吸率变异分析
  • 心率变异分析
  • P,Q,S,T的定位
  • 输出模拟信号:ECG,RSP,EMG和EDA
5.2 hrv

12.png

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

使用道具 举报

提醒:禁止复制他人回复等『恶意灌水』行为,违者重罚!
您需要登录后才可以回帖 登录 | 注册[Register] 手机动态码快速登录 微信登录

本版积分规则

发布主题 快速回复 收藏帖子 返回列表 客服中心 搜索
简体中文 繁體中文 English 한국 사람 日本語 Deutsch русский بالعربية TÜRKÇE português คนไทย french

QQ|RSS订阅|小黑屋|处罚记录|手机版|联系我们|Archiver|医工互联 |粤ICP备2021178090号 |网站地图

GMT+8, 2024-9-20 06:09 , Processed in 0.263002 second(s), 62 queries .

Powered by Discuz!

Copyright © 2001-2023, Discuz! Team.

快速回复 返回顶部 返回列表