# -*- coding: utf-8 -*-
from warnings import warn
import numpy as np
import pandas as pd
from ..misc import NeuroKitWarning
from .eda_autocor import eda_autocor
from .eda_sympathetic import eda_sympathetic
# =============================================================================
# Internals
# =============================================================================
def _eda_intervalrelated(data, output={}, sampling_rate=1000, method_sympathetic="posada", **kwargs):
"""Format input for dictionary."""
# Sanitize input
colnames = data.columns.values
# SCR Peaks
if "SCR_Peaks" not in colnames:
warn(
"We couldn't find an `SCR_Peaks` column. Returning NaN for N peaks.",
category=NeuroKitWarning,
)
output["SCR_Peaks_N"] = np.nan
else:
output["SCR_Peaks_N"] = np.nansum(data["SCR_Peaks"].values)
# Peak amplitude
if "SCR_Amplitude" not in colnames:
warn(
"We couldn't find an `SCR_Amplitude` column. Returning NaN for peak amplitude.",
category=NeuroKitWarning,
)
output["SCR_Peaks_Amplitude_Mean"] = np.nan
else:
peaks_idx = data["SCR_Peaks"] == 1
# Mean amplitude is only computed over peaks. If no peaks, return NaN
if peaks_idx.sum() > 0:
output["SCR_Peaks_Amplitude_Mean"] = np.nanmean(data[peaks_idx]["SCR_Amplitude"].values)
else:
output["SCR_Peaks_Amplitude_Mean"] = np.nan
# Get variability of tonic
if "EDA_Tonic" in colnames:
output["EDA_Tonic_SD"] = np.nanstd(data["EDA_Tonic"].values)
# EDA Sympathetic
output.update({"EDA_Sympathetic": np.nan, "EDA_SympatheticN": np.nan}) # Default values
if len(data) > sampling_rate * 64:
if "EDA_Clean" in colnames:
output.update(
eda_sympathetic(
data["EDA_Clean"],
sampling_rate=sampling_rate,
method=method_sympathetic,
)
)
elif "EDA_Raw" in colnames:
# If not clean signal, use raw
output.update(
eda_sympathetic(
data["EDA_Raw"],
sampling_rate=sampling_rate,
method=method_sympathetic,
)
)
# EDA autocorrelation
output.update({"EDA_Autocorrelation": np.nan}) # Default values
if len(data) > sampling_rate * 30: # 30 seconds minimum (NOTE: somewhat arbitrary)
if "EDA_Clean" in colnames:
output["EDA_Autocorrelation"] = eda_autocor(data["EDA_Clean"], sampling_rate=sampling_rate, **kwargs)
elif "EDA_Raw" in colnames:
# If not clean signal, use raw
output["EDA_Autocorrelation"] = eda_autocor(data["EDA_Raw"], sampling_rate=sampling_rate, **kwargs)
return output