Skip to content

Commit d517b85

Browse files
committed
refactoring plot functions
1 parent b7f69bc commit d517b85

File tree

2 files changed

+164
-18
lines changed

2 files changed

+164
-18
lines changed

wfdb/plot/plot.py

Lines changed: 152 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,164 @@
88
from ..io.annotation import Annotation
99

1010

11-
def plot_record(record=None, title=None, annotation=None, time_units='samples',
12-
sig_style='', ann_style='r*', plot_ann_sym=False, figsize=None,
13-
return_fig=False, ecg_grids=[]):
11+
def plot_items(signal=None, annotation=None, fs=None, sig_units=None,
12+
time_units='samples', chan_name=None, title=None,
13+
sig_style=[''], ann_style=['r*'], ecg_grids=[], figsize=None,
14+
return_fig=False):
1415
"""
15-
Subplot and label each channel of a WFDB Record.
16-
Optionally, subplot annotation locations over selected channels.
16+
Subplot individual channels of signals and/or annotations.
17+
18+
Parameters
19+
----------
20+
signal : 1d or 2d numpy array, optional
21+
The uniformly sampled signal to be plotted. If signal.ndim is 1, it is
22+
assumed to be a one channel signal. If it is 2, axes 0 and 1, must
23+
represent time and channel number respectively.
24+
annotation: list, optional
25+
A list of annotation locations to plot, with each list item
26+
corresponding to a different channel. List items may be:
27+
- 1d numpy array, with values representing sample indices
28+
- list, with values representing sample indices
29+
- None. For channels in which nothing is to be plotted.
30+
If `signal` is defined, the annotation locations will be overlaid on
31+
the signals, with the list index corresponding to the signal channel.
32+
The length of `annotation` does not have to match the number of
33+
channels of `signal`.
34+
fs : int or float, optional
35+
The sampling frequency of the signals and/or annotations. Used to
36+
calculate time intervals if `time_units` is not 'samples'.
37+
sig_units : list, optional
38+
List of strings specifying the units of each signal channel.
39+
time_units : str, optional
40+
The x axis unit. Allowed options are: 'samples', 'seconds', 'minutes',
41+
and 'hours'.
42+
chan_name : list, optional
43+
A list of strings, representing the channel names. This
44+
title : str, optional
45+
The title of the graph.
46+
sig_style : list, optional
47+
A list of strings, specifying the style of the matplotlib plot for each
48+
signal channel. If the list has a length of 1, the style will be used
49+
for all channels.
50+
ann_style : list, optional
51+
A list of strings, specifying the style of the matplotlib plot for each
52+
annotation channel. If the list has a length of 1, the style will be
53+
used for all channels.
54+
ecg_grids : list, optional
55+
List of integers specifying channels in which to plot ecg grids. May be
56+
set to [] for no channels, or 'all' for all channels. Major grids at
57+
0.5mV, and minor grids at 0.125mV. All channels to be plotted with
58+
grids must have units equal to 'uV', 'mV', or 'V'.
59+
figsize : tuple, optional
60+
Tuple pair specifying the width, and height of the figure. It is the
61+
'figsize' argument passed into matplotlib.pyplot's `figure` function.
62+
return_fig : bool, optional
63+
Whether the figure is to be returned as an output argument.
64+
65+
Returns
66+
-------
67+
figure : matplotlib figure, optional
68+
The matplotlib figure generated. Only returned if the 'return_fig'
69+
option is set to True.
70+
71+
Notes
72+
-----
73+
At least one of `signal` or `annotation` must be defined, or there will be
74+
nothing to plot.
75+
76+
Examples
77+
--------
78+
>>> record = wfdb.rdrecord('sample-data/100', sampto=3000)
79+
>>> annotation = wfdb.rdann('sample-data/100', 'atr', sampto=3000)
80+
81+
wfdb.plot_record(record, annotation=annotation, title='Record 100 from MIT-BIH Arrhythmia Database',
82+
time_units='seconds', figsize=(10,4), ecg_grids='all')
83+
84+
85+
Add ann_sym option?
86+
87+
"""
88+
89+
90+
# Create figure
91+
ax, fig = create_figure()
92+
93+
if signal:
94+
plot_signal(signal, ax, fig)
95+
96+
if annotation:
97+
plot_annotation(annotation, ax, fig)
98+
99+
if ecg_grids:
100+
plot_ecg_grids()
101+
102+
# Add title and axis labels.
103+
label_figure()
104+
105+
106+
107+
if return_fig:
108+
return figure
109+
110+
111+
112+
113+
def check_plot_inputs():
114+
if signal is None and annotation is None:
115+
raise Exception('Nothing to plot.')
116+
117+
118+
119+
120+
121+
122+
def plot_items(signal=None, annotation=None, fs=None, time_units='samples',
123+
chan_name=None, title=None, sig_style=[''], ann_style=['r*'],
124+
figsize=None, return_fig=False, ecg_grids=[]):
125+
126+
127+
def plot_wfdb(record=None, annotation=None, time_units='samples',
128+
plot_physical=True, plot_ann_sym=False,
129+
130+
131+
title=None, sig_style=[''], ann_style=['r*'],
132+
133+
134+
135+
136+
figsize=None,
137+
return_fig=False, ecg_grids=[]):
138+
"""
139+
Subplot individual channels of a wfdb record and/or annotation.
140+
141+
This function implements the base functionality of the `plot_items`
142+
function, while allowing direct input of wfdb objects.
17143
18144
Parameters
19145
----------
20146
record : wfdb Record
21-
The Record object, whose p_signal attribute is to be plotted.
147+
The Record object, whose p_signal or d_signal attribute is to be
148+
plotted.
22149
title : str, optional
23150
The title of the graph.
24-
annotation : list, wfdb Annotation, or numpy array, optional
25-
A list containing wfdb Annotation objects, numpy arrays, or None. The
26-
locations of the Annotation objects' `sample` attribute, or the
27-
locations of the numpy arrays' values, will be overlaid on the signals.
28-
The list index of each item corresponds to the signal channel that each
29-
annotation set will be plotted on. For channels without annotation.to
30-
plot, put None in the list. This argument may also be just an Annotation
31-
object or numpy array, which will be plotted over channel 0.
151+
annotation : list, or wfdb Annotation, optional
152+
One of the following:
153+
1. Same as the `annotation` argument of the `plot_items` function. A
154+
list of annotation locations to plot, with each list item
155+
corresponding to a different channel. List items may be:
156+
- 1d numpy array, with values representing sample indices
157+
- list, with values representing sample indices
158+
- None. For channels in which nothing is to be plotted.
159+
If `signal` is defined, the annotation locations will be overlaid on
160+
the signals, with the list index corresponding to the signal
161+
channel. The length of `annotation` does not have to match the
162+
number of channels of `signal`.
163+
2. A wfdb Annotation object. The `chan` attribute decides the channel
164+
number of each sample.
32165
time_units : str, optional
33166
The x axis unit. Allowed options are: 'samples', 'seconds', 'minutes',
34-
and 'hours'.
167+
and 'hours'. If this option is not 'samples', either the `record` or
168+
`annotation` argument must be a valid `fs` attribute.
35169
sig_style : str, or list, optional
36170
String, or list of strings, specifying the styling of the matplotlib
37171
plot for the signals. If it is a string, each channel will have the same
@@ -71,6 +205,9 @@ def plot_record(record=None, title=None, annotation=None, time_units='samples',
71205
72206
"""
73207

208+
209+
a, b, c = extract_wfdb_items()
210+
74211
# Check the validity of items used to make the plot
75212
# Return the x axis time values to plot for the record (and annotation if any)
76213
t, tann, annplot = check_plot_items(record, title, annotation, time_units, sig_style, ann_style)

wfdb/processing/evaluate.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,10 @@ def compare_annotations(ref_sample, test_sample, window_width):
164164

165165
return comparitor
166166

167-
168-
def plot_comparitor(comparitor, sig=None):
167+
# def plot_record(record=None, title=None, annotation=None, time_units='samples',
168+
# sig_style='', ann_style='r*', plot_ann_sym=False, figsize=None,
169+
# return_fig=False, ecg_grids=[]):
170+
def plot_comparitor(comparitor, sig=None, sig_style='', title=None, figsize=None, return_fig=False):
169171
"""
170172
Plot two sets of annotations
171173
@@ -176,4 +178,11 @@ def plot_comparitor(comparitor, sig=None):
176178
sig : numpy array, optional
177179
The underlying signal of the two sets of annotations to plot.
178180
"""
179-
return
181+
182+
183+
fig=plt.figure(figsize=figsize)
184+
185+
186+
187+
if return_fig:
188+
return fig

0 commit comments

Comments
 (0)