Skip to content

Commit

Permalink
Merge pull request #917 from mperreir/fix_eda_intervalrelated
Browse files Browse the repository at this point in the history
[Fix] `SCR_Peaks_Amplitude_Mean` should be only computed on peaks
  • Loading branch information
DominiqueMakowski authored Oct 9, 2023
2 parents 44959c6 + 984194a commit 484dd7f
Showing 1 changed file with 16 additions and 15 deletions.
31 changes: 16 additions & 15 deletions neurokit2/eda/eda_intervalrelated.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,7 @@ def eda_intervalrelated(data, sampling_rate=1000, **kwargs):
# Add label info
results[index]["Label"] = data[index]["Label"].iloc[0]

results[index] = _eda_intervalrelated(
data[index], results[index], sampling_rate=sampling_rate, **kwargs
)
results[index] = _eda_intervalrelated(data[index], results[index], sampling_rate=sampling_rate, **kwargs)

results = pd.DataFrame.from_dict(results, orient="index")

Expand All @@ -89,9 +87,7 @@ def eda_intervalrelated(data, sampling_rate=1000, **kwargs):
# =============================================================================


def _eda_intervalrelated(
data, output={}, sampling_rate=1000, method_sympathetic="posada", **kwargs
):
def _eda_intervalrelated(data, output={}, sampling_rate=1000, method_sympathetic="posada", **kwargs):
"""Format input for dictionary."""
# Sanitize input
colnames = data.columns.values
Expand All @@ -114,7 +110,12 @@ def _eda_intervalrelated(
)
output["SCR_Peaks_Amplitude_Mean"] = np.nan
else:
output["SCR_Peaks_Amplitude_Mean"] = np.nanmean(data["SCR_Amplitude"].values)
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:
Expand All @@ -126,28 +127,28 @@ def _eda_intervalrelated(
if "EDA_Clean" in colnames:
output.update(
eda_sympathetic(
data["EDA_Clean"], sampling_rate=sampling_rate, method=method_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
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
)
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
)
output["EDA_Autocorrelation"] = eda_autocor(data["EDA_Raw"], sampling_rate=sampling_rate, **kwargs)

return output

0 comments on commit 484dd7f

Please sign in to comment.