{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Plot evoked\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\nimport numpy as np\nimport seaborn as sns\nfrom systole.detection import ecg_peaks\nfrom systole.plots import plot_evoked\nfrom systole.utils import heart_rate, to_epochs\n\nfrom systole import import_dataset1\n\n# Author: Nicolas Legrand \n# Licence: Apache-2.0" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Plot evoked heart rate across two conditions using the Matplotlib backend\nHere, for the sake of example, we are going to create the same plot three time using three kind of input data: \n* The raw signal + the triggers timing (or a list of in case of multiple conditions).\n* The peaks detection + the triggers timing (or a list of in case of multiple conditions)\n* The epoched signal as a 2d NumPy array (or a list of in case of multiple conditions)\n--------------------------------------------------------------\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "ecg_df = import_dataset1(modalities=['ECG', \"Stim\"])\n\n# Get events triggers\ntriggers_idx = [\n np.where(ecg_df.stim.to_numpy() == 2)[0],\n np.where(ecg_df.stim.to_numpy() == 1)[0]\n]\n\n# Peak detection in the ECG signal using the Pan-Tompkins method\nsignal, peaks = ecg_peaks(ecg_df.ecg, method='sleepecg', sfreq=1000)\n\n# Convert to instantaneous heart rate\nrr, _ = heart_rate(peaks, kind=\"cubic\", unit=\"bpm\", input_type=\"peaks\")\n\n# Create list epochs arrays for each condition\nhr_epochs, _ = to_epochs(\n signal=rr, triggers_idx=triggers_idx, tmin=-1.0, tmax=10.0,\n apply_baseline=(-1.0, 0.0)\n )\n\nfig, axs = plt.subplots(ncols=3, figsize=(15, 5), sharey=True)\n\n# We define a common set of plotting arguments here\nplot_args = {\n \"backend\": \"matplotlib\", \"figsize\": (400, 400),\n \"palette\": [sns.xkcd_rgb[\"denim blue\"], sns.xkcd_rgb[\"pale red\"]],\n \"tmin\": -1.0, \"tmax\": 10.0, \"apply_baseline\": (-1.0, 0.0), \"decim\": 100\n }\n\n# Using the raw signal and events triggers\nplot_evoked(\n signal=ecg_df.ecg.to_numpy(), triggers_idx=triggers_idx, modality=\"ecg\",\n ax=axs[0], **plot_args\n )\n\n# Using the detected peaks and events triggers\nplot_evoked(\n rr=peaks, triggers_idx=triggers_idx, input_type=\"peaks\", ax=axs[1],\n **plot_args\n )\n\n# Using the list of epochs arrays\nplot_evoked(\n epochs=hr_epochs, ax=axs[2], **plot_args\n )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here, for the sake of example, we are going to create the same plot three times using three kind of input data:\n* The raw signal + the triggers timing (or a list of in case of multiple conditions).\n* The peaks detection + the triggers timing (or a list of in case of multiple conditions)\n* The epoched signal as a 2d NumPy array (or a list of in case of multiple conditions)\n--------------------------------------------------------------------------------------\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from bokeh.io import output_notebook\nfrom bokeh.layouts import row\nfrom bokeh.plotting import show\n\noutput_notebook()\n\n# We define a common set of plotting arguments here\nplot_args = {\n \"backend\": \"bokeh\", \"figsize\": (300, 300),\n \"palette\": [sns.xkcd_rgb[\"denim blue\"], sns.xkcd_rgb[\"pale red\"]],\n \"tmin\": -1.0, \"tmax\": 10.0, \"apply_baseline\": (-1.0, 0.0), \"decim\": 100\n }\n\n# Using the raw signal and events triggers\nraw_plot = plot_evoked(\n signal=ecg_df.ecg.to_numpy(), triggers_idx=triggers_idx, modality=\"ecg\",\n **plot_args\n )\n\n# Using the detected peaks and events triggers\npeaks_plot = plot_evoked(\n rr=peaks, triggers_idx=triggers_idx, input_type=\"peaks\", **plot_args\n )\n\n# Using the list of epochs arrays\nepochs_plots = plot_evoked(epochs=hr_epochs, **plot_args)\n\n# Create a Bokeh layout and plot the figures side by side\nshow(\n row(\n raw_plot, peaks_plot, epochs_plots\n )\n )" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "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.11.16" } }, "nbformat": 4, "nbformat_minor": 0 }