Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Commit

Permalink
Fix onset calculation for BVP module (#70)
Browse files Browse the repository at this point in the history
* Fix onset calculation on BVP signals

- New onset detection function added (based on the work of Elgendi et al. 2013)
- BVP function now calls "find_onsets_elgendi2013" to calculate onsets.
- Change previous (and now deprecated) "find_onsets" function name to "find_onsets_zong03" to adequately refer to the method used.

* Scipy import added

* Addind PPG and ABP module

- Added new modules: PPG and ABP
- Deprecated module: BVP (now use PPG)

* Small typos fixed

- Small typos fixed
- Changed name of onset calculation function from 'find_onsets_zong03' to 'find_onsets_zong2003' to use same naming as 'find_onsets_elgendi2013'
  • Loading branch information
LeafarCoder committed Sep 11, 2020
1 parent b47ac24 commit 1c15cc7
Show file tree
Hide file tree
Showing 12 changed files with 665 additions and 143 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -9,7 +9,7 @@ methods geared towards the analysis of biosignals.

Highlights:

- Support for various biosignals: BVP, ECG, EDA, EEG, EMG, Respiration
- Support for various biosignals: PPG, ECG, EDA, EEG, EMG, Respiration
- Signal analysis primitives: filtering, frequency analysis
- Clustering
- Biometrics
Expand Down
2 changes: 1 addition & 1 deletion biosppy/__init__.py
Expand Up @@ -16,4 +16,4 @@
from .__version__ import __version__

# allow lazy loading
from .signals import bvp, ecg, eda, eeg, emg, resp, tools
from .signals import abp, bvp, ppg, ecg, eda, eeg, emg, resp, tools
187 changes: 186 additions & 1 deletion biosppy/plotting.py
Expand Up @@ -209,6 +209,99 @@ def plot_spectrum(signal=None, sampling_rate=1000., path=None, show=True):
plt.close(fig)


def plot_ppg(ts=None,
raw=None,
filtered=None,
onsets=None,
heart_rate_ts=None,
heart_rate=None,
path=None,
show=False):
"""Create a summary plot from the output of signals.ppg.ppg.
Parameters
----------
ts : array
Signal time axis reference (seconds).
raw : array
Raw PPG signal.
filtered : array
Filtered PPG signal.
onsets : array
Indices of PPG pulse onsets.
heart_rate_ts : array
Heart rate time axis reference (seconds).
heart_rate : array
Instantaneous heart rate (bpm).
path : str, optional
If provided, the plot will be saved to the specified file.
show : bool, optional
If True, show the plot immediately.
"""

fig = plt.figure()
fig.suptitle('PPG Summary')

# raw signal
ax1 = fig.add_subplot(311)

ax1.plot(ts, raw, linewidth=MAJOR_LW, label='Raw')

ax1.set_ylabel('Amplitude')
ax1.legend()
ax1.grid()

# filtered signal with onsets
ax2 = fig.add_subplot(312, sharex=ax1)

ymin = np.min(filtered)
ymax = np.max(filtered)
alpha = 0.1 * (ymax - ymin)
ymax += alpha
ymin -= alpha

ax2.plot(ts, filtered, linewidth=MAJOR_LW, label='Filtered')
ax2.vlines(ts[onsets], ymin, ymax,
color='m',
linewidth=MINOR_LW,
label='Onsets')

ax2.set_ylabel('Amplitude')
ax2.legend()
ax2.grid()

# heart rate
ax3 = fig.add_subplot(313, sharex=ax1)

ax3.plot(heart_rate_ts, heart_rate, linewidth=MAJOR_LW, label='Heart Rate')

ax3.set_xlabel('Time (s)')
ax3.set_ylabel('Heart Rate (bpm)')
ax3.legend()
ax3.grid()

# make layout tight
fig.tight_layout()

# save to file
if path is not None:
path = utils.normpath(path)
root, ext = os.path.splitext(path)
ext = ext.lower()
if ext not in ['png', 'jpg']:
path = root + '.png'

fig.savefig(path, dpi=200, bbox_inches='tight')

# show
if show:
plt.show()
else:
# close
plt.close(fig)


def plot_bvp(ts=None,
raw=None,
filtered=None,
Expand Down Expand Up @@ -302,6 +395,98 @@ def plot_bvp(ts=None,
plt.close(fig)


def plot_abp(ts=None,
raw=None,
filtered=None,
onsets=None,
heart_rate_ts=None,
heart_rate=None,
path=None,
show=False):
"""Create a summary plot from the output of signals.abp.abp.
Parameters
----------
ts : array
Signal time axis reference (seconds).
raw : array
Raw ABP signal.
filtered : array
Filtered ABP signal.
onsets : array
Indices of ABP pulse onsets.
heart_rate_ts : array
Heart rate time axis reference (seconds).
heart_rate : array
Instantaneous heart rate (bpm).
path : str, optional
If provided, the plot will be saved to the specified file.
show : bool, optional
If True, show the plot immediately.
"""

fig = plt.figure()
fig.suptitle('ABP Summary')

# raw signal
ax1 = fig.add_subplot(311)

ax1.plot(ts, raw, linewidth=MAJOR_LW, label='Raw')

ax1.set_ylabel('Amplitude')
ax1.legend()
ax1.grid()

# filtered signal with onsets
ax2 = fig.add_subplot(312, sharex=ax1)

ymin = np.min(filtered)
ymax = np.max(filtered)
alpha = 0.1 * (ymax - ymin)
ymax += alpha
ymin -= alpha

ax2.plot(ts, filtered, linewidth=MAJOR_LW, label='Filtered')
ax2.vlines(ts[onsets], ymin, ymax,
color='m',
linewidth=MINOR_LW,
label='Onsets')

ax2.set_ylabel('Amplitude')
ax2.legend()
ax2.grid()

# heart rate
ax3 = fig.add_subplot(313, sharex=ax1)

ax3.plot(heart_rate_ts, heart_rate, linewidth=MAJOR_LW, label='Heart Rate')

ax3.set_xlabel('Time (s)')
ax3.set_ylabel('Heart Rate (bpm)')
ax3.legend()
ax3.grid()

# make layout tight
fig.tight_layout()

# save to file
if path is not None:
path = utils.normpath(path)
root, ext = os.path.splitext(path)
ext = ext.lower()
if ext not in ['png', 'jpg']:
path = root + '.png'

fig.savefig(path, dpi=200, bbox_inches='tight')

# show
if show:
plt.show()
else:
# close
plt.close(fig)

def plot_eda(ts=None,
raw=None,
filtered=None,
Expand Down Expand Up @@ -507,7 +692,7 @@ def plot_resp(ts=None,
resp_rate=None,
path=None,
show=False):
"""Create a summary plot from the output of signals.bvp.bvp.
"""Create a summary plot from the output of signals.ppg.ppg.
Parameters
----------
Expand Down
4 changes: 2 additions & 2 deletions biosppy/signals/__init__.py
Expand Up @@ -5,7 +5,7 @@
This package provides methods to process common
physiological signals (biosignals):
* Blood Volume Pulse (BVP)
* Photoplethysmogram (PPG)
* Electrocardiogram (ECG)
* Electrodermal Activity (EDA)
* Electroencephalogram (EEG)
Expand All @@ -20,4 +20,4 @@
from __future__ import absolute_import, division, print_function

# allow lazy loading
from . import bvp, ecg, eda, eeg, emg, resp, tools
from . import abp, bvp, ppg, ecg, eda, eeg, emg, resp, tools

0 comments on commit 1c15cc7

Please sign in to comment.