dy1901 / ecg_plot Goto Github PK
View Code? Open in Web Editor NEWPlot standard multi lead ECG/EKG chart with Python
License: MIT License
Plot standard multi lead ECG/EKG chart with Python
License: MIT License
Dear Experts,
Plotting a specific ECG trace produces the following error:
`ValueError: x and y must have same first dimension, but have shapes (511,) and (510,)'
Here is a minimal example to reproduce the error:
import ecg_plot
ecgTrace = [-0.735, -0.754, -0.762, -0.759, -0.771, -0.781, -0.784, -0.786,
-0.791, -0.791, -0.796, -0.786, -0.769, -0.769, -0.774, -0.774,
-0.791, -0.828, -0.845, -0.815, -0.796, -0.813, -0.82 , -0.818,
-0.835, -0.84 , -0.825, -0.818, -0.833, -0.83 , -0.808, -0.801,
-0.791, -0.771, -0.769, -0.784, -0.811, -0.823, -0.82 , -0.833,
-0.83 , -0.823, -0.847, -0.862, -0.869, -0.867, -0.815, -0.767,
-0.781, -0.864, -1.023, -1.113, -1.003, -0.867, -0.847, -0.847,
-0.828, -0.837, -0.84 , -0.813, -0.806, -0.815, -0.815, -0.818,
-0.808, -0.781, -0.786, -0.813, -0.806, -0.779, -0.784, -0.808,
-0.798, -0.757, -0.752, -0.767, -0.732, -0.698, -0.696, -0.669,
-0.645, -0.647, -0.657, -0.674, -0.698, -0.728, -0.779, -0.815,
-0.818, -0.845, -0.874, -0.835, -0.818, -0.862, -0.869, -0.854,
-0.852, -0.813, -0.796, -0.825, -0.82 , -0.801, -0.815, -0.823,
-0.811, -0.813, -0.815, -0.806, -0.803, -0.82 , -0.828, -0.82 ,
-0.811, -0.811, -0.82 , -0.82 , -0.808, -0.808, -0.801, -0.791,
-0.796, -0.798, -0.786, -0.776, -0.774, -0.779, -0.803, -0.84 ,
-0.842, -0.847, -0.869, -0.837, -0.801, -0.823, -0.833, -0.813,
-0.813, -0.801, -0.754, -0.669, -0.664, -0.857, -1.069, -1.055,
-0.891, -0.803, -0.82 , -0.837, -0.825, -0.815, -0.815, -0.808,
-0.818, -0.818, -0.803, -0.798, -0.791, -0.781, -0.779, -0.776,
-0.776, -0.764, -0.747, -0.757, -0.759, -0.73 , -0.703, -0.671,
-0.63 , -0.615, -0.601, -0.579, -0.579, -0.581, -0.591, -0.642,
-0.667, -0.671, -0.725, -0.757, -0.73 , -0.73 , -0.747, -0.737,
-0.723, -0.725, -0.735, -0.725, -0.698, -0.696, -0.72 , -0.725,
-0.715, -0.718, -0.715, -0.71 , -0.725, -0.735, -0.725, -0.725,
-0.732, -0.728, -0.73 , -0.74 , -0.735, -0.73 , -0.73 , -0.73 ,
-0.728, -0.72 , -0.72 , -0.723, -0.701, -0.669, -0.652, -0.645,
-0.642, -0.649, -0.671, -0.686, -0.703, -0.74 , -0.747, -0.71 ,
-0.72 , -0.745, -0.713, -0.684, -0.696, -0.703, -0.679, -0.613,
-0.554, -0.615, -0.828, -0.989, -0.916, -0.762, -0.728, -0.737,
-0.72 , -0.715, -0.696, -0.664, -0.669, -0.691, -0.686, -0.664,
-0.647, -0.662, -0.684, -0.664, -0.64 , -0.662, -0.664, -0.623,
-0.625, -0.635, -0.601, -0.579, -0.569, -0.542, -0.508, -0.471,
-0.464, -0.486, -0.481, -0.471, -0.498, -0.549, -0.596, -0.618,
-0.618, -0.61 , -0.608, -0.615, -0.618, -0.625, -0.627, -0.61 ,
-0.598, -0.605, -0.598, -0.581, -0.586, -0.591, -0.586, -0.598,
-0.598, -0.576, -0.588, -0.608, -0.591, -0.576, -0.559, -0.542,
-0.562, -0.571, -0.552, -0.544, -0.559, -0.564, -0.562, -0.547,
-0.542, -0.547, -0.535, -0.52 , -0.508, -0.503, -0.535, -0.569,
-0.557, -0.562, -0.593, -0.576, -0.535, -0.537, -0.547, -0.549,
-0.557, -0.518, -0.43 , -0.396, -0.471, -0.618, -0.728, -0.703,
-0.598, -0.542, -0.525, -0.52 , -0.525, -0.518, -0.498, -0.488,
-0.498, -0.5 , -0.481, -0.464, -0.471, -0.481, -0.471, -0.449,
-0.461, -0.466, -0.425, -0.405, -0.425, -0.403, -0.356, -0.332,
-0.303, -0.295, -0.305, -0.269, -0.234, -0.264, -0.298, -0.313,
-0.33 , -0.364, -0.396, -0.398, -0.393, -0.413, -0.425, -0.432,
-0.437, -0.425, -0.405, -0.393, -0.393, -0.41 , -0.4 , -0.374,
-0.371, -0.371, -0.364, -0.371, -0.371, -0.361, -0.352, -0.356,
-0.371, -0.383, -0.398, -0.408, -0.388, -0.376, -0.403, -0.413,
-0.374, -0.349, -0.344, -0.327, -0.337, -0.374, -0.359, -0.315,
-0.298, -0.298, -0.31 , -0.342, -0.364, -0.376, -0.386, -0.374,
-0.376, -0.393, -0.359, -0.313, -0.313, -0.303, -0.249, -0.19 ,
-0.186, -0.327, -0.552, -0.581, -0.396, -0.278, -0.308, -0.327,
-0.308, -0.303, -0.315, -0.322, -0.32 , -0.308, -0.288, -0.278,
-0.298, -0.293, -0.254, -0.254, -0.256, -0.225, -0.225, -0.244,
-0.229, -0.212, -0.19 , -0.154, -0.132, -0.117, -0.095, -0.081,
-0.061, -0.061, -0.107, -0.146, -0.156, -0.188, -0.229, -0.244,
-0.244, -0.225, -0.222, -0.242, -0.225, -0.205, -0.227, -0.237,
-0.222, -0.21 , -0.203, -0.205, -0.195, -0.173, -0.195, -0.227,
-0.195, -0.171, -0.195, -0.212, -0.193, -0.171, -0.176, -0.183,
-0.166, -0.181, -0.208, -0.193, -0.19 , -0.198, -0.164, -0.149,
-0.156, -0.134, -0.132, -0.151, -0.127, -0.115, -0.159, -0.19 ,
-0.193, -0.198, -0.178, -0.161, -0.178, -0.188, -0.183, -0.203,
-0.198, -0.146, -0.076, -0.01 , -0.076, -0.315]
ecg_plot.plot_1(ecgTrace, sample_rate=100, title = 'ECG', ecg_amp=1)
ecg_plot.show()
Followed example code, plot shows up fine in plot viewer within Spyder but saved image is blank white square.
Code below - including creating array of simulated data
import neurokit2 as nk, pandas as pd, numpy as np, ecg_plot
ls_dfs = []
for i in range(0,12):
lead = pd.DataFrame({'lead {}'.format(i+1):nk.signal_simulate(duration=10, sampling_rate=500, random_state=i)})
ls_dfs.append(lead)
df = pd.concat(ls_dfs, axis=1)
df = df.T
df = df.values
ecg_plot.plot_12(df,sample_rate=500,title="")
ecg_plot.save_as_png('example_ecg',"")
In case if you have different values which need be plot for ECG let say some numbers like -23.000... or 3.000. It will be nice to have this function:
def normalize_data(ecg_data):
"""Normalize ECG data to the range of -1 to 1."""
ecg_data = (ecg_data - np.min(ecg_data)) / (np.max(ecg_data) - np.min(ecg_data))
ecg_data = 2 * ecg_data - 1 # Normalize to -1 to 1
return ecg_data
def plot_12(
ecg,
sample_rate=500,
title='ECG 12',
lead_index=lead_index,
lead_order=None,
columns=2,
speed=50,
voltage=20,
line_width=0.6
):
# Normalizing ECG data
ecg = [normalize_data(lead) for lead in ecg]
if not lead_order:
lead_order = list(range(0, len(ecg)))
leads = len(lead_order)
seconds = len(ecg[0]) / sample_rate
Hi for a personal project I am using your tool and i have a problem. When I'm calling plot_1 or plot_12 functions some parameters are missing. If you check the pypi page of the tool in parameters table the parameters below the "columns" are missing. Can you help me about that? Thank you
Hi, i'm trying to plot a CSV file obtained from an XML
my script is the following:
import ecg_plot
import pandas as pd
ecg = pd.read_csv('ecg_test.csv')
ecg_plot.plot(ecg,title = 'ECG test')
ecg_plot.show()
and this is the error i've got:
`The above exception was the direct cause of the following exception:
ecg_plot.plot(ecg,title = 'ECG test')
File "/usr/lib/python3.9/site-packages/ecg_plot/ecg_plot.py", line 113, in plot
secs = len(ecg[0])/sample_rate
File "/usr/lib/python3.9/site-packages/pandas/core/frame.py", line 2906, in __getitem__
indexer = self.columns.get_loc(key)
File "/usr/lib/python3.9/site-packages/pandas/core/indexes/base.py", line 2900, in get_loc
raise KeyError(key) from err
KeyError: 0
`
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.