Repository navigation
Expand file tree
/
Copy pathtest_plot.py
More file actions
168 lines (141 loc) · 5.46 KB
/
Copy pathtest_plot.py
File metadata and controls
168 lines (141 loc) · 5.46 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
164
165
166
167
168
import matplotlib.pyplot as plt
import numpy as np
import wfdb
from wfdb.plot import plot
def assert_axes_match_signal(axes, signal, t_divisor=1):
"""Check that axis limits are reasonable for plotting a signal array.
Parameters
----------
axes : matplotlib.axes.Axes
An Axes object.
signal : numpy.ndarray
A one-dimensional array of sample values.
t_divisor : float, optional
The intended plotting resolution (number of samples of `signal`
per unit of the X axis.)
"""
xmin, xmax = axes.get_xlim()
tmin = 0
tmax = (len(signal) - 1) / t_divisor
# The range from tmin to tmax should fit within the plot.
assert xmin <= tmin, f"X range is [{xmin}, {xmax}]; expected [{tmin}, {tmax}]"
assert xmax >= tmax, f"X range is [{xmin}, {xmax}]; expected [{tmin}, {tmax}]"
# The padding on left and right sides should be approximately equal.
assert abs(xmin - tmin - (tmax - xmax)) <= (tmax - tmin) / 10 + 1 / t_divisor, (
f"X range is [{xmin}, {xmax}]; expected [{tmin}, {tmax}]"
)
ymin, ymax = axes.get_ylim()
vmin = np.nanmin(signal)
vmax = np.nanmax(signal)
# The range from vmin to vmax should fit within the plot.
assert ymin <= vmin, f"Y range is [{ymin}, {ymax}]; expected [{vmin}, {vmax}]"
assert ymax >= vmax, f"Y range is [{ymin}, {ymax}]; expected [{vmin}, {vmax}]"
# The padding on top and bottom should be approximately equal.
assert abs(ymin - vmin - (vmax - ymax)) <= (vmax - vmin) / 10, (
f"Y range is [{ymin}, {ymax}]; expected [{vmin}, {vmax}]"
)
class TestPlotWfdb:
"""Tests for the wfdb.plot_wfdb function"""
def test_physical_smooth(self):
"""Plot a record with physical, single-frequency data"""
record = wfdb.rdrecord(
"sample-data/100",
sampto=1000,
physical=True,
smooth_frames=True,
)
assert record.p_signal is not None
annotation = wfdb.rdann("sample-data/100", "atr", sampto=1000)
fig = wfdb.plot_wfdb(
record,
annotation,
time_units="samples",
ecg_grids="all",
return_fig=True,
)
plt.close(fig)
assert len(fig.axes) == record.n_sig
for ch in range(record.n_sig):
assert_axes_match_signal(fig.axes[ch], record.p_signal[:, ch])
def test_digital_smooth(self):
"""Plot a record with digital, single-frequency data"""
record = wfdb.rdrecord(
"sample-data/drive02",
sampto=1000,
physical=False,
smooth_frames=True,
)
assert record.d_signal is not None
fig = wfdb.plot_wfdb(record, time_units="seconds", return_fig=True)
plt.close(fig)
assert len(fig.axes) == record.n_sig
for ch in range(record.n_sig):
assert_axes_match_signal(fig.axes[ch], record.d_signal[:, ch], record.fs)
def test_physical_multifrequency(self):
"""Plot a record with physical, multi-frequency data"""
record = wfdb.rdrecord(
"sample-data/wave_4",
sampto=10,
physical=True,
smooth_frames=False,
)
assert record.e_p_signal is not None
fig = wfdb.plot_wfdb(record, time_units="seconds", return_fig=True)
plt.close(fig)
assert len(fig.axes) == record.n_sig
for ch in range(record.n_sig):
assert_axes_match_signal(
fig.axes[ch],
record.e_p_signal[ch],
record.fs * record.samps_per_frame[ch],
)
def test_digital_multifrequency(self):
"""Plot a record with digital, multi-frequency data"""
record = wfdb.rdrecord(
"sample-data/multi-segment/041s/041s",
sampto=1000,
physical=False,
smooth_frames=False,
)
assert record.e_d_signal is not None
fig = wfdb.plot_wfdb(record, time_units="seconds", return_fig=True)
plt.close(fig)
assert len(fig.axes) == record.n_sig
for ch in range(record.n_sig):
assert_axes_match_signal(
fig.axes[ch],
record.e_d_signal[ch],
record.fs * record.samps_per_frame[ch],
)
class TestPlotInternal:
"""Unit tests for internal wfdb.plot.plot functions"""
def test_get_plot_dims(self):
sampfrom = 0
sampto = 3000
record = wfdb.rdrecord(
"sample-data/100", physical=True, sampfrom=sampfrom, sampto=sampto
)
ann = wfdb.rdann("sample-data/100", "atr", sampfrom=sampfrom, sampto=sampto)
sig_len, n_sig, n_annot, n_subplots = plot._get_plot_dims(
signal=record.p_signal, ann_samp=[ann.sample]
)
assert sig_len == sampto - sampfrom
assert n_sig == record.n_sig
assert n_annot == 1
assert n_subplots == record.n_sig
def test_create_figure_single_subplots(self):
n_subplots = 1
fig, axes = plot._create_figure(
n_subplots, sharex=True, sharey=True, figsize=None
)
assert fig is not None
assert axes is not None
assert len(axes) == n_subplots
def test_create_figure_multiple_subplots(self):
n_subplots = 5
fig, axes = plot._create_figure(
n_subplots, sharex=True, sharey=True, figsize=None
)
assert fig is not None
assert axes is not None
assert len(axes) == n_subplots