forked from PIA-Group/BioSPPy
-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathecg.py
More file actions
163 lines (132 loc) · 4.49 KB
/
ecg.py
File metadata and controls
163 lines (132 loc) · 4.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# -*- coding: utf-8 -*-
"""
biosppy.inter_plotting.ecg
-------------------
This module provides an interactive display option for the ECG plot.
:copyright: (c) 2015-2023 by Instituto de Telecomunicacoes
:license: BSD 3-clause, see LICENSE for more details.
"""
# Imports
from matplotlib import gridspec
import matplotlib
# from matplotlib.backends.backend_wx import *
import matplotlib.pyplot as plt
import numpy as np
from tkinter import *
import os
from biosppy import utils
MAJOR_LW = 2.5
MINOR_LW = 1.5
MAX_ROWS = 10
def plot_ecg(
ts=None,
raw=None,
filtered=None,
rpeaks=None,
templates_ts=None,
templates=None,
heart_rate_ts=None,
heart_rate=None,
path=None,
show=True,
):
"""Create a summary plot from the output of signals.ecg.ecg.
Parameters
----------
ts : array
Signal time axis reference (seconds).
raw : array
Raw ECG signal.
filtered : array
Filtered ECG signal.
rpeaks : array
R-peak location indices.
templates_ts : array
Templates time axis reference (seconds).
templates : array
Extracted heartbeat templates.
heart_rate_ts : array
Heart rate time axis reference (seconds).
heart_rate : array
Instantaneous heart rate (bpm).
path : str, optional
If provided, the plot will be saved to the specified file.
show : bool, optional
If True, show the plot immediately.
"""
# creating a root widget
root_tk = Tk()
root_tk.resizable(False, False) # default
fig_raw, axs_raw = plt.subplots(3, 1, sharex=True)
fig_raw.suptitle("ECG Summary")
# raw signal plot (1)
axs_raw[0].plot(ts, raw, linewidth=MAJOR_LW, label="Raw", color="C0")
axs_raw[0].set_ylabel("Amplitude")
axs_raw[0].legend()
axs_raw[0].grid()
# filtered signal with R-Peaks (2)
axs_raw[1].plot(ts, filtered, linewidth=MAJOR_LW, label="Filtered", color="C0")
ymin = np.min(filtered)
ymax = np.max(filtered)
alpha = 0.1 * (ymax - ymin)
ymax += alpha
ymin -= alpha
# adding the R-Peaks
axs_raw[1].vlines(
ts[rpeaks], ymin, ymax, color="m", linewidth=MINOR_LW, label="R-peaks"
)
axs_raw[1].set_ylabel("Amplitude")
axs_raw[1].legend(loc="upper right")
axs_raw[1].grid()
# heart rate (3)
axs_raw[2].plot(heart_rate_ts, heart_rate, linewidth=MAJOR_LW, label="Heart Rate")
axs_raw[2].set_xlabel("Time (s)")
axs_raw[2].set_ylabel("Heart Rate (bpm)")
axs_raw[2].legend()
axs_raw[2].grid()
canvas_raw = matplotlib.backends.backend_tkagg.FigureCanvasTkAgg(fig_raw, master=root_tk)
canvas_raw.get_tk_widget().grid(
row=0, column=0, columnspan=1, rowspan=6, sticky="w"
)
canvas_raw.draw()
toolbarFrame = Frame(master=root_tk)
toolbarFrame.grid(row=6, column=0, columnspan=1, sticky=W)
toolbar = matplotlib.backends.backend_tkagg.NavigationToolbar2Tk(canvas_raw, toolbarFrame)
toolbar.update()
fig = fig_raw
fig_2 = plt.Figure()
gs = gridspec.GridSpec(6, 1)
axs_2 = fig_2.add_subplot(gs[:, 0])
axs_2.plot(templates_ts, templates.T, "m", linewidth=MINOR_LW, alpha=0.7)
axs_2.set_xlabel("Time (s)")
axs_2.set_ylabel("Amplitude")
axs_2.set_title("Templates")
axs_2.grid()
grid_params = {"row": 0, "column": 1, "columnspan": 2, "rowspan": 6, "sticky": "w"}
canvas_2 = matplotlib.backends.backend_tkagg.FigureCanvasTkAgg(fig_2, master=root_tk)
canvas_2.get_tk_widget().grid(**grid_params)
canvas_2.draw()
toolbarFrame_2 = Frame(master=root_tk)
toolbarFrame_2.grid(row=6, column=1, columnspan=1, sticky=W)
toolbar_2 = matplotlib.backends.backend_tkagg.NavigationToolbar2Tk(canvas_2, toolbarFrame_2)
toolbar_2.update()
if show:
# window title
root_tk.wm_title("BioSPPy: ECG signal")
# save to file
if path is not None:
path = utils.normpath(path)
root, ext = os.path.splitext(path)
ext = ext.lower()
if ext not in ["png", "jpg"]:
path_block_1 = "{}-summary{}".format(root, ".png")
path_block_2 = "{}-templates{}".format(root, ".png")
else:
path_block_1 = "{}-summary{}".format(root, ext)
path_block_2 = "{}-templates{}".format(root, ext)
fig.savefig(path_block_1, dpi=200, bbox_inches="tight")
fig_2.savefig(path_block_2, dpi=200, bbox_inches="tight")
mainloop()
else:
# close
plt.close(fig)