Q Peak Detection Example#

Setup and imports#

[1]:
import matplotlib.pyplot as plt
import seaborn as sns
from fau_colors import cmaps

from pepbench.algorithms.ecg import QPeakExtractionVanLien2013
from pepbench.example_data import get_example_dataset
from pepbench.plotting.algorithms import plot_q_peak_extraction_vanlien2013

%matplotlib inline
%load_ext autoreload
%autoreload 2

Set plotting style and figure size#

[2]:
plt.close("all")
palette = sns.color_palette(cmaps.faculties)
sns.set_theme(context="notebook", style="ticks", font="sans-serif", palette=palette)
plt.rcParams["figure.figsize"] = (10, 5)

Load Example Dataset#

[4]:
dataset = get_example_dataset(return_clean=True)
dataset
[4]:

ExampleDataset [2 groups/rows]

participant
0 VP_001
1 VP_002

Get the first datapoint

[7]:
datapoint = list(dataset)[0]
datapoint
[7]:

ExampleDataset [1 groups/rows]

participant
0 VP_001

Run Q-peak extraction (Van Lien 2013)#

[8]:
q_algo = QPeakExtractionVanLien2013()
# use the computed heartbeat segmentation
q_algo.extract(ecg=datapoint.ecg, heartbeats=datapoint.heartbeats, sampling_rate_hz=datapoint.sampling_rate_ecg)
[8]:
QPeakExtractionVanLien2013(handle_missing_events='warn', time_interval_ms=40)

Inspect detected points (first rows)#

[9]:
q_algo.points_.head()
[9]:
q_peak_sample nan_reason
heartbeat_id
0 145 <NA>
1 572 <NA>
2 993 <NA>
3 1389 <NA>
4 1782 <NA>

Plot a few heartbeats to visualise detected Q-peaks#

determine the first three heartbeat_ids available in the reference_heartbeats index

[10]:
hb_ids = list(datapoint.reference_heartbeats.index.get_level_values("heartbeat_id").unique())[:3]
print("Plotting heartbeat ids:", hb_ids)
fig, ax = plot_q_peak_extraction_vanlien2013(datapoint, heartbeat_subset=hb_ids, normalize_time=True)
plt.show()
Plotting heartbeat ids: [0, 1, 2]

Notes:#

  • You can swap QPeakExtractionVanLien2013 for other algorithms available in pepbench.algorithms.ecg (e.g., QPeakExtractionMartinez2004Neurokit, QPeakExtractionForouzanfar2018).

  • To use non-reference heartbeats, use datapoint.heartbeats instead of datapoint.reference_heartbeats.

  • get_example_dataset() returns the packaged demo dataset, which is suitable for lightweight algorithm exploration and plotting.

[ ]:

Download Notebook
(Right-Click -> Save Link As...)