Plotting .mat ECG to 50mm ECG visualisation

67 views Asked by At

I'm trying to graph .mat files from this database https://data.mendeley.com/datasets/7dybx7wyfn/3 and saving them as an .svg or .png using ecg-plot 0.2.8 (https://pypi.org/project/ecg-plot/).

I'm been at it for a couple of days without any luck, underneath is my method:

  1. Download the database
  2. Writing a py script inside the folder containing .mat files to try to save a single ecg graph (see picture and code): enter image description here

Script:

import ecg_plot
ecg = '228m (10).mat'
ecg_plot.plot_12(ecg, sample_rate = 500, title = 'ECG 12')
ecg_plot.save_as_png('example_ecg', 'tmp/')

This gives the resulting error:

    (base) agfr@Ages-MacBook-Pro 1 NSR % python3 main.py 
Traceback (most recent call last):
  File "/Users/agfr/Documents/MLII/1 NSR/main.py", line 4, in <module>
    ecg_plot.plot_12(ecg, sample_rate = 500, title = 'ECG 12')
  File "/Users/agfr/opt/anaconda3/lib/python3.9/site-packages/ecg_plot/ecg_plot.py", line 83, in plot_12
    _ax_plot(t_ax, np.arange(0, len(ecg[t_lead])*step, step), ecg[t_lead], seconds)
  File "/Users/agfr/opt/anaconda3/lib/python3.9/site-packages/ecg_plot/ecg_plot.py", line 27, in _ax_plot
    ax.plot(x,y, linewidth=lwidth)
  File "/Users/agfr/opt/anaconda3/lib/python3.9/site-packages/matplotlib/axes/_axes.py", line 1632, in plot
    lines = [*self._get_lines(*args, data=data, **kwargs)]
  File "/Users/agfr/opt/anaconda3/lib/python3.9/site-packages/matplotlib/axes/_base.py", line 312, in __call__
    yield from self._plot_args(this, kwargs)
  File "/Users/agfr/opt/anaconda3/lib/python3.9/site-packages/matplotlib/axes/_base.py", line 449, in _plot_args
    linestyle, marker, color = _process_plot_format(fmt)
  File "/Users/agfr/opt/anaconda3/lib/python3.9/site-packages/matplotlib/axes/_base.py", line 199, in _process_plot_format
    raise ValueError(
ValueError: Unrecognized character 0 in format string

If anyone could help me understand what I'm doing wrong in the plotting the graph I would be very grateful! I've been trying to find an answer on stack overflow and google without any solutions.

2

There are 2 answers

0
mhmmdrz92 On

Firstly, since the data is in .mat format, you need to use scipy.io.loadmat to read the file. Additionally, you can utilize matplotlib for plotting the data.

from scipy.io import loadmat
import matplotlib.pyplot as plt

# Load the ECG data from the .mat file
ecg = loadmat('228m (10).mat')['val'].squeeze()

# Plot the ECG data
plt.plot(ecg)
plt.show()

Furthermore, if you intend to plot the data in a 50 mm mV ECG format, ensure that your data is in mV format. Then, adjust the scaling of the y-axis on your graph according to your requirements.

0
VComm On

Use simplify_cells=True in loadmat, that will load data as dictionary structure, which is quite handy to extract the correct data stream.

from scipy.io import loadmat

ecg_dic = loadmat('228m (10).mat',simplify_cells=True)