B Point Detection Example#

Setup and imports#

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

from pepbench.algorithms.icg import BPointExtractionPale2021, CPointExtractionScipyFindPeaks
from pepbench.example_data import get_example_dataset

%matplotlib inline
%load_ext autoreload
%autoreload 2

Plotting style#

[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#

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

ExampleDataset [2 groups/rows]

participant
0 VP_001
1 VP_002

Get the first datapoint

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

ExampleDataset [1 groups/rows]

participant
0 VP_001

Run C-point extraction (used by some B-point algorithms)#

[6]:
c_algo = CPointExtractionScipyFindPeaks()
# use the computed heartbeat segmentation
c_algo.extract(icg=datapoint.icg, heartbeats=datapoint.heartbeats, sampling_rate_hz=datapoint.sampling_rate_icg)
[6]:
CPointExtractionScipyFindPeaks(handle_missing_events='warn', window_c_correction=3)

Run B-point extraction (Pale et al. 2021)#

[7]:
b_algo = BPointExtractionPale2021()
# use the same heartbeat segmentation (datapoint.heartbeats) when extracting
b_algo.extract(
    icg=datapoint.icg,
    heartbeats=datapoint.heartbeats,
    c_points=c_algo.points_,
    sampling_rate_hz=datapoint.sampling_rate_icg,
)
[7]:
BPointExtractionPale2021(b_point_slope_threshold_01=0.11, b_point_slope_threshold_02=0.08, c_point_amplitude_fraction=0.5, handle_missing_events='warn')

Inspect detected points (first rows)#

[8]:
b_algo.points_.head()
[8]:
b_point_sample nan_reason
heartbeat_id
0 179 NaN
1 533 NaN
2 1009 NaN
3 1466 NaN
4 1873 NaN

Plot a few heartbeats to visualise detected B-points#

determine the first five heartbeat_ids available in the reference_heartbeats index

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

Notes:#

  • You can swap BPointExtractionPale2021 for other algorithms available in pepbench.algorithms.icg.

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

  • For the bundled example dataset, to access raw (uncleaned) signals call get_example_dataset(return_clean=False) when instantiating.

detection + ploting signal and detected b points

[ ]:

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