{ "cells": [ { "cell_type": "markdown", "id": "c8a1e072b9c99117", "metadata": {}, "source": [ "# B Point Detection Example" ] }, { "cell_type": "markdown", "id": "b03dc1d8ba136bed", "metadata": {}, "source": [ "## Setup and imports" ] }, { "cell_type": "code", "execution_count": 1, "id": "9a9e1e779129bad1", "metadata": {}, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "import seaborn as sns\n", "from fau_colors import cmaps\n", "\n", "from pepbench.algorithms.icg import BPointExtractionPale2021, CPointExtractionScipyFindPeaks\n", "from pepbench.example_data import get_example_dataset\n", "\n", "%matplotlib inline\n", "%load_ext autoreload\n", "%autoreload 2" ] }, { "cell_type": "markdown", "id": "e1432ca952a994b", "metadata": {}, "source": [ "## Plotting style" ] }, { "cell_type": "code", "execution_count": 2, "id": "706add995e246b0c", "metadata": {}, "outputs": [], "source": [ "plt.close(\"all\")\n", "palette = sns.color_palette(cmaps.faculties)\n", "sns.set_theme(context=\"notebook\", style=\"ticks\", font=\"sans-serif\", palette=palette)\n", "plt.rcParams[\"figure.figsize\"] = (10, 5)" ] }, { "cell_type": "markdown", "id": "375327a5d0584159", "metadata": {}, "source": [ "## Load Example Dataset" ] }, { "cell_type": "code", "execution_count": 3, "id": "6360c9f4e3a7fe50", "metadata": {}, "outputs": [ { "data": { "text/html": [ "

ExampleDataset [2 groups/rows]

\n", "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
participant
0VP_001
1VP_002
\n", "
" ], "text/plain": [ "ExampleDataset [2 groups/rows]\n", "\n", " participant\n", " 0 VP_001\n", " 1 VP_002" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dataset = get_example_dataset(return_clean=True)\n", "dataset" ] }, { "cell_type": "markdown", "id": "f10fbf98af3d65a6", "metadata": {}, "source": [ "Get the first datapoint" ] }, { "cell_type": "code", "execution_count": 5, "id": "d07a4130457ab975", "metadata": {}, "outputs": [ { "data": { "text/html": [ "

ExampleDataset [1 groups/rows]

\n", "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
participant
0VP_001
\n", "
" ], "text/plain": [ "ExampleDataset [1 groups/rows]\n", "\n", " participant\n", " 0 VP_001" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "datapoint = list(dataset)[0]\n", "datapoint" ] }, { "cell_type": "markdown", "id": "d76f77d9640e3129", "metadata": {}, "source": [ "## Run C-point extraction (used by some B-point algorithms)" ] }, { "cell_type": "code", "execution_count": 6, "id": "f7fa44514f40a15b", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "CPointExtractionScipyFindPeaks(handle_missing_events='warn', window_c_correction=3)" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c_algo = CPointExtractionScipyFindPeaks()\n", "# use the computed heartbeat segmentation\n", "c_algo.extract(icg=datapoint.icg, heartbeats=datapoint.heartbeats, sampling_rate_hz=datapoint.sampling_rate_icg)" ] }, { "cell_type": "markdown", "id": "713e3a1c6db302ce", "metadata": {}, "source": [ "## Run B-point extraction (Pale et al. 2021)" ] }, { "cell_type": "code", "execution_count": 7, "id": "7a73377e6d0ee406", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "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')" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b_algo = BPointExtractionPale2021()\n", "# use the same heartbeat segmentation (datapoint.heartbeats) when extracting\n", "b_algo.extract(\n", " icg=datapoint.icg,\n", " heartbeats=datapoint.heartbeats,\n", " c_points=c_algo.points_,\n", " sampling_rate_hz=datapoint.sampling_rate_icg,\n", ")" ] }, { "cell_type": "markdown", "id": "f10c1f8dc8686d45", "metadata": {}, "source": [ "## Inspect detected points (first rows)" ] }, { "cell_type": "code", "execution_count": 8, "id": "b665b4cf4ddaa9a1", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
b_point_samplenan_reason
heartbeat_id
0179NaN
1533NaN
21009NaN
31466NaN
41873NaN
\n", "
" ], "text/plain": [ " b_point_sample nan_reason\n", "heartbeat_id \n", "0 179 NaN\n", "1 533 NaN\n", "2 1009 NaN\n", "3 1466 NaN\n", "4 1873 NaN" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b_algo.points_.head()" ] }, { "cell_type": "markdown", "id": "9998c0c956a73601", "metadata": {}, "source": [ "## Plot a few heartbeats to visualise detected B-points\n", "determine the first five heartbeat_ids available in the reference_heartbeats index" ] }, { "cell_type": "code", "execution_count": 9, "id": "fc4a1a89f1cf3181", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Plotting heartbeat ids: [0, 1, 2]\n" ] } ], "source": [ "hb_ids = list(datapoint.reference_heartbeats.index.get_level_values(\"heartbeat_id\").unique())[:3]\n", "print(\"Plotting heartbeat ids:\", hb_ids)\n", "# fig, ax = plot_b_point_extraction_pale2021(datapoint, heartbeat_subset=hb_ids, normalize_time=True)" ] }, { "cell_type": "markdown", "id": "84fc286daf4b9f42", "metadata": {}, "source": [ "### Notes:\n", "- You can swap `BPointExtractionPale2021` for other algorithms available in `pepbench.algorithms.icg`.\n", "- To use non-reference heartbeats, use `datapoint.heartbeats` instead of `datapoint.reference_heartbeats`.\n", "- For the bundled example dataset, to access raw (uncleaned) signals call `get_example_dataset(return_clean=False)` when instantiating.\n" ] }, { "cell_type": "markdown", "id": "dff34bddc6eb549b", "metadata": {}, "source": [ "detection + ploting signal and detected b points" ] }, { "cell_type": "code", "execution_count": null, "id": "ccfa953ef82574e", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "pepbench", "language": "python", "name": "pepbench" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.14" } }, "nbformat": 4, "nbformat_minor": 5 }