Source code for neurokit2.ecg.ecg_intervalrelated
# -*- coding: utf-8 -*-
import numpy as np
import pandas as pd
from ..hrv import hrv
# =============================================================================
# Internals
# =============================================================================
def _ecg_intervalrelated_formatinput(data, output={}):
# Sanitize input
colnames = data.columns.values
if len([i for i in colnames if "ECG_Rate" in i]) == 0:
raise ValueError(
"NeuroKit error: ecg_intervalrelated(): Wrong input,"
"we couldn't extract heart rate. Please make sure"
"your DataFrame contains an `ECG_Rate` column."
)
signal = data["ECG_Rate"].values
output["ECG_Rate_Mean"] = np.mean(signal)
return output
def _ecg_intervalrelated_hrv(data, sampling_rate, output={}):
# Sanitize input
colnames = data.columns.values
if len([i for i in colnames if "ECG_R_Peaks" in i]) == 0:
raise ValueError(
"NeuroKit error: ecg_intervalrelated(): Wrong input,"
"we couldn't extract R-peaks. Please make sure"
"your DataFrame contains an `ECG_R_Peaks` column."
)
# Transform rpeaks from "signal" format to "info" format.
rpeaks = np.where(data["ECG_R_Peaks"].values)[0]
rpeaks = {"ECG_R_Peaks": rpeaks}
results = hrv(rpeaks, sampling_rate=sampling_rate)
for column in results.columns:
# Add and convert to float
output[column] = results[[column]].values
return output