Skip to content

Commit 1dc98ed

Browse files
author
Benjamin Moody
committed
plot_signal: avoid making redundant X-coordinate arrays.
In most cases, the same X-coordinate array will be used for more than one channel; the contents of this array only depend on ch_len and ch_freq (and time_units), so we can cache these arrays in a dictionary, saving some (likely small) amount of time and memory when plotting a huge record.
1 parent 69c93a3 commit 1dc98ed

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

wfdb/plot/plot.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -408,22 +408,28 @@ def plot_signal(signal, sig_len, n_sig, fs, time_units, sig_style, axes,
408408
# Convert sampling_freq to a list if needed
409409
sampling_freq = _get_sampling_freq(sampling_freq, n_sig, fs)
410410

411+
tarrays = {}
412+
411413
# Plot the signals
412414
for ch in range(n_sig):
413415
ch_len = len(signal[ch])
414416
ch_freq = sampling_freq[ch]
415417

416418
# Figure out time indices
417-
if time_units == 'samples':
418-
t = np.linspace(0, ch_len-1, ch_len)
419-
else:
420-
downsample_factor = {
421-
'seconds': ch_freq,
422-
'minutes': ch_freq * 60,
423-
'hours': ch_freq * 3600
424-
}
425-
t = np.linspace(0, ch_len-1, ch_len)
426-
t /= downsample_factor[time_units]
419+
try:
420+
t = tarrays[ch_len, ch_freq]
421+
except KeyError:
422+
if time_units == 'samples':
423+
t = np.linspace(0, ch_len-1, ch_len)
424+
else:
425+
downsample_factor = {
426+
'seconds': ch_freq,
427+
'minutes': ch_freq * 60,
428+
'hours': ch_freq * 3600
429+
}
430+
t = np.linspace(0, ch_len-1, ch_len)
431+
t /= downsample_factor[time_units]
432+
tarrays[ch_len, ch_freq] = t
427433

428434
axes[ch].plot(t, signal[ch], sig_style[ch], zorder=3)
429435

0 commit comments

Comments
 (0)