{ "cells": [ { "cell_type": "markdown", "metadata": { "cell_marker": "\"\"\"", "pycharm": { "name": "#%% md\n" } }, "source": [ "# CFT Example" ] }, { "cell_type": "markdown", "metadata": { "pycharm": { "name": "#%% md\n" } }, "source": [ "
\n", "This example illustrates how to process data collected when conducting the Cold Face Test (CFT) procedure, i.e., how to compute parameters quantifying the reaction to the CFT and how to create specialized CFT plots.\n", "
" ] }, { "cell_type": "markdown", "metadata": { "pycharm": { "name": "#%% md\n" } }, "source": [ "## Setup and Helper Functions" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "pycharm": { "name": "#%%\n" }, "tags": [] }, "outputs": [], "source": [ "from pathlib import Path\n", "\n", "import re\n", "\n", "import pandas as pd\n", "import numpy as np\n", "\n", "from fau_colors import cmaps\n", "\n", "import biopsykit as bp\n", "from biopsykit.protocols import CFT\n", "\n", "import matplotlib.pyplot as plt\n", "import seaborn as sns\n", "\n", "%matplotlib widget\n", "%load_ext autoreload\n", "%autoreload 2" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "pycharm": { "name": "#%%\n" }, "tags": [] }, "outputs": [], "source": [ "plt.close(\"all\")\n", "\n", "palette = sns.color_palette(cmaps.faculties)\n", "sns.set_theme(context=\"notebook\", style=\"ticks\", font=\"sans-serif\", palette=palette)\n", "\n", "plt.rcParams[\"figure.figsize\"] = (8, 4)\n", "plt.rcParams[\"pdf.fonttype\"] = 42\n", "plt.rcParams[\"mathtext.default\"] = \"regular\"\n", "\n", "palette" ] }, { "cell_type": "markdown", "metadata": { "pycharm": { "name": "#%% md\n" } }, "source": [ "## Create CFT Object" ] }, { "cell_type": "markdown", "metadata": { "pycharm": { "name": "#%% md\n" } }, "source": [ "The [CFT](https://biopsykit.readthedocs.io/en/latest/api/biopsykit.protocols.cft.html#biopsykit.protocols.cft.CFT) is a protocol and thus part of the Protocol API of `BioPsyKit`." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "pycharm": { "name": "#%%\n" }, "tags": [] }, "outputs": [], "source": [ "cft = CFT()\n", "cft" ] }, { "cell_type": "markdown", "metadata": { "pycharm": { "name": "#%% md\n" } }, "source": [ "## Load Data" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "pycharm": { "name": "#%%\n" }, "tags": [] }, "outputs": [], "source": [ "hr_dict = bp.example_data.get_mist_hr_example()\n", "\n", "# Alternatively: Use your own data\n", "# hr_dict = bp.io.ecg.load_hr_phase_dict(\"\")\n", "df_hr = hr_dict[\"MIST3\"]\n", "\n", "df_hr.head()" ] }, { "cell_type": "markdown", "metadata": { "pycharm": { "name": "#%% md\n" } }, "source": [ "## Plot Course of Heart Rate" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "pycharm": { "name": "#%%\n" }, "tags": [] }, "outputs": [], "source": [ "fig, ax = plt.subplots()\n", "bp.signals.ecg.plotting.hr_plot(df_hr, ax=ax)\n", "fig.tight_layout()" ] }, { "cell_type": "markdown", "metadata": { "pycharm": { "name": "#%% md\n" } }, "source": [ "Note: See [plotting.hr_plot()](https://biopsykit.readthedocs.io/en/latest/api/biopsykit.signals.ecg.plotting.html#biopsykit.signals.ecg.plotting.hr_plot) for further information!" ] }, { "cell_type": "markdown", "metadata": { "pycharm": { "name": "#%% md\n" } }, "source": [ "## Compute CFT Parameter" ] }, { "cell_type": "markdown", "metadata": { "pycharm": { "name": "#%% md\n" } }, "source": [ "Extract CFT Interval, i.e., the interval of the data where the Cold Face stimulus was actually applied (corresponds to the duration of the \"CFT\" phase specified in the [CFT](https://biopsykit.readthedocs.io/en/latest/api/biopsykit.protocols.cft.html#biopsykit.protocols.cft.CFT) object) and compute baseline heart rate (the mean heart rate during the \"Baseline\" phase specified in the [CFT](https://biopsykit.readthedocs.io/en/latest/api/biopsykit.protocols.cft.html#biopsykit.protocols.cft.CFT) object).\n", "\n", "Note: See [CFT.extract_cft_interval()](https://biopsykit.readthedocs.io/en/latest/api/biopsykit.protocols.cft.html#biopsykit.protocols.cft.CFT.extract_cft_interval) and [CFT.compute_cft_parameter()](https://biopsykit.readthedocs.io/en/latest/api/biopsykit.protocols.cft.html#biopsykit.protocols.cft.CFT.compute_cft_parameter) for further information!" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "pycharm": { "name": "#%%\n" }, "tags": [] }, "outputs": [], "source": [ "df_cft = cft.extract_cft_interval(df_hr)\n", "bl = cft.baseline_hr(df_hr)\n", "\n", "print(\"Baseline Heart Rate: {:.2f}\".format(bl))\n", "df_cft.head()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "pycharm": { "name": "#%%\n" }, "tags": [] }, "outputs": [], "source": [ "cft.compute_cft_parameter(df_hr)" ] }, { "cell_type": "markdown", "metadata": { "pycharm": { "name": "#%% md\n" } }, "source": [ "## Plot CFT Data" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "pycharm": { "name": "#%%\n" }, "tags": [ "nbsphinx-thumbnail" ] }, "outputs": [], "source": [ "fig, ax = plt.subplots()\n", "\n", "cft.cft_plot(data=df_hr, ax=ax)\n", "fig.tight_layout()" ] }, { "cell_type": "markdown", "metadata": { "pycharm": { "name": "#%% md\n" } }, "source": [ "Note: See [CFT.cft_plot()](https://biopsykit.readthedocs.io/en/latest/api/biopsykit.protocols.html#biopsykit.protocols.CFT.cft_plot) for further information!" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "pycharm": { "name": "#%%\n" } }, "outputs": [], "source": [] } ], "metadata": { "jupytext": { "encoding": "# -*- coding: utf-8 -*-", "text_representation": { "extension": ".py", "format_name": "sphinx", "format_version": "1.1", "jupytext_version": "1.13.0" } }, "kernelspec": { "display_name": "biopsykit", "language": "python", "name": "biopsykit" }, "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.9.13" } }, "nbformat": 4, "nbformat_minor": 4 }