Skip to content

Commit

Permalink
ErrorBar Update Two
Browse files Browse the repository at this point in the history
Created the kwargs pipeline, initialized errorbars in summaryplot as 0. Now the user will define errorbars as the level of confidence if they wish to use them and then they will play a role in conducting the standard error.
  • Loading branch information
BrandonReno committed Feb 17, 2021
1 parent f9c931c commit 219b4e6
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 17 deletions.
11 changes: 4 additions & 7 deletions neurokit2/hrv/hrv.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from .hrv_utils import _hrv_get_rri, _hrv_sanitize_input


def hrv(peaks, sampling_rate=1000, show=False, errorbar=False):
def hrv(peaks, sampling_rate=1000, show=False, **kwargs):
"""Computes indices of Heart Rate Variability (HRV).
Computes HRV indices in the time-, frequency-, and nonlinear domain. Note that a minimum duration
Expand All @@ -29,9 +29,6 @@ def hrv(peaks, sampling_rate=1000, show=False, errorbar=False):
show : bool, optional
If True, returns the plots that are generates for each of the domains.
errorbar : bool, optional
If true, returns on the histogram plot an error bar for each bin.
Returns
-------
DataFrame
Expand Down Expand Up @@ -84,12 +81,12 @@ def hrv(peaks, sampling_rate=1000, show=False, errorbar=False):
# Indices for plotting
out_plot = out.copy(deep=False)

_hrv_plot(peaks, out_plot, sampling_rate, errorbar)
_hrv_plot(peaks, out_plot, sampling_rate, **kwargs)

return out


def _hrv_plot(peaks, out, sampling_rate=1000, err=False):
def _hrv_plot(peaks, out, sampling_rate=1000, **kwargs):

fig = plt.figure(constrained_layout=False)
spec = gs.GridSpec(ncols=2, nrows=2, height_ratios=[1, 1], width_ratios=[1, 1])
Expand All @@ -110,7 +107,7 @@ def _hrv_plot(peaks, out, sampling_rate=1000, err=False):
# Distribution of RR intervals
peaks = _hrv_sanitize_input(peaks)
rri = _hrv_get_rri(peaks, sampling_rate=sampling_rate, interpolate=False)
ax_distrib = summary_plot(rri, errorbar=err, ax=ax_distrib)
ax_distrib = summary_plot(rri, ax=ax_distrib, **kwargs)

# Poincare plot
out.columns = [col.replace("HRV_", "") for col in out.columns]
Expand Down
10 changes: 4 additions & 6 deletions neurokit2/hrv/hrv_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from .hrv_utils import _hrv_get_rri, _hrv_sanitize_input


def hrv_time(peaks, sampling_rate=1000, show=False, errorbar=False):
def hrv_time(peaks, sampling_rate=1000, show=False, **kwargs):
"""Computes time-domain indices of Heart Rate Variability (HRV).
See references for details.
Expand All @@ -23,8 +23,6 @@ def hrv_time(peaks, sampling_rate=1000, show=False, errorbar=False):
least twice as high as the highest frequency in vhf. By default 1000.
show : bool
If True, will plot the distribution of R-R intervals.
errorbar : bool, optional
If true, returns on the histogram plot an error bar for each bin.
Returns
-------
Expand Down Expand Up @@ -121,15 +119,15 @@ def hrv_time(peaks, sampling_rate=1000, show=False, errorbar=False):
out["HTI"] = len(rri) / np.max(bar_y) # HRV Triangular Index

if show:
_hrv_time_show(rri, errorbar)
_hrv_time_show(rri, **kwargs)

out = pd.DataFrame.from_dict(out, orient="index").T.add_prefix("HRV_")
return out


def _hrv_time_show(rri, errorbar, **kwargs):
def _hrv_time_show(rri, **kwargs):

fig = summary_plot(rri, errorbar, **kwargs)
fig = summary_plot(rri, **kwargs)
plt.xlabel("R-R intervals (ms)")
fig.suptitle("Distribution of R-R intervals")

Expand Down
9 changes: 5 additions & 4 deletions neurokit2/stats/summary.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
import numpy as np
import scipy.stats as st

from .density import density
from .rescale import rescale


def summary_plot(x, errorbar=False, **kwargs):
def summary_plot(x, errorbars=0, **kwargs):
"""Descriptive plot.
Visualize a distribution with density, histogram, boxplot and rugs plots all at once.
Expand All @@ -32,9 +33,9 @@ def summary_plot(x, errorbar=False, **kwargs):
# Histogram
counts, bins = np.histogram(x, **kwargs)
bin_centers = 0.5*(bins[1:] + bins[:-1])
menStd = np.sqrt(counts)
if errorbar:
ax.errorbar(bin_centers, counts, yerr=menStd, ecolor="#228B22", fmt='.', capsize=5, capthick=2)
if errorbars > 0:
samperr = np.std(counts) / np.sqrt(counts) * (st.norm.ppf(1-(1-errorbars)/2))
ax.errorbar(bin_centers, counts, yerr=samperr, ecolor="#FF8C00", fmt='.', capsize=5, capthick=2)
ax.hist(bins[:-1], bins, weights=counts, color="#2196F3", edgecolor="white", zorder=1, **kwargs)

# Density
Expand Down

0 comments on commit 219b4e6

Please sign in to comment.