Source code for neurokit2.emg.emg_eventrelated
# -*- coding: utf-8 -*-
from warnings import warn
import numpy as np
from ..epochs.eventrelated_utils import (
_eventrelated_addinfo,
_eventrelated_sanitizeinput,
_eventrelated_sanitizeoutput,
)
from ..misc import NeuroKitWarning
# =============================================================================
# Internals
# =============================================================================
def _emg_eventrelated_features(epoch, output={}):
# Sanitize input
if "EMG_Activity" not in epoch or "EMG_Amplitude" not in epoch:
warn(
"Input does not have an `EMG_Activity` column or `EMG_Amplitude` column."
" Will skip computation of EMG amplitudes.",
category=NeuroKitWarning,
)
return output
# Peak amplitude and Time of peak
activations = len(np.where(epoch["EMG_Onsets"][epoch.index > 0] == 1)[0])
activated_signal = np.where(epoch["EMG_Activity"][epoch.index > 0] == 1)
mean = np.array(epoch["EMG_Amplitude"][epoch.index > 0].iloc[activated_signal]).mean()
maximum = np.array(epoch["EMG_Amplitude"][epoch.index > 0].iloc[activated_signal]).max()
index_time = np.where(epoch["EMG_Amplitude"][epoch.index > 0] == maximum)[0]
time = np.array(epoch["EMG_Amplitude"][epoch.index > 0].index[index_time])[0]
output["EMG_Amplitude_Mean"] = mean
output["EMG_Amplitude_Max"] = maximum
output["EMG_Amplitude_SD"] = np.std(
epoch["EMG_Amplitude"][epoch.index > 0].iloc[activated_signal]
)
output["EMG_Amplitude_Max_Time"] = time
output["EMG_Bursts"] = activations
return output