Skip to content

Commit

Permalink
Merge pull request #966 from stasinos/issue-964
Browse files Browse the repository at this point in the history
sparsEDA integration
  • Loading branch information
DominiqueMakowski authored Mar 24, 2024
2 parents 0ea8a2b + 0d271d8 commit 2f923ce
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 25 deletions.
40 changes: 20 additions & 20 deletions neurokit2/eda/eda_phasic.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,16 +357,16 @@ def _eda_phasic_sparsEDA(
if np.sum(np.isnan(eda_signal)) > 0:
raise AssertionError("Signal contains NaN")

# Preprocessing
signalAdd = np.zeros(len(eda_signal) + (20 * sampling_rate) + (60 * sampling_rate))
signalAdd[0 : 20 * sampling_rate] = eda_signal[0]
signalAdd[20 * sampling_rate : 20 * sampling_rate + len(eda_signal)] = eda_signal
signalAdd[20 * sampling_rate + len(eda_signal) :] = eda_signal[-1]

# Resample to 8 Hz
eda_signal = signal_resample(eda_signal, sampling_rate=sampling_rate, desired_sampling_rate=8)
new_sr = 8

# Preprocessing
signalAdd = np.zeros(len(eda_signal) + (20 * new_sr) + (60 * new_sr))
signalAdd[0 : 20 * new_sr] = eda_signal[0]
signalAdd[20 * new_sr : 20 * new_sr + len(eda_signal)] = eda_signal
signalAdd[20 * new_sr + len(eda_signal) :] = eda_signal[-1]

Nss = len(eda_signal)
Ns = len(signalAdd)
b0 = 0
Expand Down Expand Up @@ -428,7 +428,7 @@ def _eda_phasic_sparsEDA(
b0 = signalCut[0]

signalCutIn = signalCut - b0
beta, _, _, _, _, _ = lasso(R, signalCutIn, sampling_rate, Kmax, epsilon)
beta, _, _, _, _, _ = lasso(R, signalCutIn, new_sr, Kmax, epsilon)

signalEst = (np.matmul(R, beta) + b0).reshape(-1)

Expand All @@ -437,8 +437,8 @@ def _eda_phasic_sparsEDA(
# res3 = sum(remAout(40*sampling_rate+1:(60*sampling_rate)));

remAout = (signalCut - signalEst) ** 2
res2 = np.sum(remAout[20 * sampling_rate : 40 * sampling_rate])
res3 = np.sum(remAout[40 * sampling_rate : 60 * sampling_rate])
res2 = np.sum(remAout[20 * new_sr : 40 * new_sr])
res3 = np.sum(remAout[40 * new_sr : 60 * new_sr])

jump = 1
if res2 < 1:
Expand All @@ -454,19 +454,19 @@ def _eda_phasic_sparsEDA(
SCRaux[:] = SCRline.reshape([5, Lreg]).transpose()
driver = SCRaux.sum(axis=1)

b0 = np.matmul(R[jump * 20 * sampling_rate - 1, 0:6], beta[0:6, :]) + b0
b0 = np.matmul(R[jump * 20 * new_sr - 1, 0:6], beta[0:6, :]) + b0

driverAux[cutS : cutS + (jump * 20 * sampling_rate)] = driver[0 : jump * sampling_rate * 20]
slcAux[cutS : cutS + (jump * 20 * sampling_rate)] = SCL[
0 : jump * sampling_rate * 20
driverAux[cutS : cutS + (jump * 20 * new_sr)] = driver[0 : jump * new_sr * 20]
slcAux[cutS : cutS + (jump * 20 * new_sr)] = SCL[
0 : jump * new_sr * 20
].reshape(-1)
resAux[cutS : cutS + (jump * 20 * sampling_rate)] = remAout[0 : jump * sampling_rate * 20]
cutS = cutS + jump * 20 * sampling_rate
resAux[cutS : cutS + (jump * 20 * new_sr)] = remAout[0 : jump * new_sr * 20]
cutS = cutS + jump * 20 * new_sr
cutE = cutS + N

SCRaux = driverAux[pointerS:pointerE]
SCL = slcAux[pointerS:pointerE]
MSE = resAux[pointerS:pointerE]
#MSE = resAux[pointerS:pointerE]

# PP
ind = np.argwhere(SCRaux > 0).reshape(-1)
Expand All @@ -488,11 +488,11 @@ def _eda_phasic_sparsEDA(
threshold = rho * scr_max
driver[driver < threshold] = 0

# Resample
# Resample back to original sampling rate
SCR = eda_signal-SCL
SCR = signal_resample(SCR, desired_length=original_length)
SCL = signal_resample(SCL, desired_length=original_length)
MSE = signal_resample(MSE, desired_length=original_length)

return driver, SCL, MSE
return SCL, SCR


def lasso(R, s, sampling_rate, maxIters, epsilon):
Expand Down
11 changes: 9 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ build-backend = "setuptools.build_meta"

[tool.poetry]
name = "neurokit2"
# version = "0.1.0" # Needs to find a way to automatically get latest version
version = "0.2.8" # Needs to find a way to automatically get latest version
description = "The Python Toolbox for Neurophysiological Signal Processing."
authors = [
"Dominique Makowski <@DominiqueMakowski>",
Expand All @@ -24,7 +24,14 @@ keywords = ['python', 'neurokit', 'neurokit2', 'physiological', 'signal']


[tool.poetry.dependencies]
python = "*"
python = "^3.11"
scipy = "^1.11"
scikit-learn = "^1.4"
pandas = "^2.1"
matplotlib = "*"
requests = "^2.31.0"
cvxopt = "^1.3"
biosppy = "*"

[tool.poetry.dev-dependencies]
pytest = "^3.4"
5 changes: 2 additions & 3 deletions tests/tests_eda.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,8 @@ def test_eda_phasic():
highpass = nk.eda_phasic(eda, sampling_rate=sr, method="highpass")
assert len(highpass) == len(eda)

# This fails unfortunately... need to fix the sparsEDA algorithm
# sparsEDA = nk.eda_phasic(eda, sampling_rate=sr, method="sparsEDA")
# assert len(highpass) == len(eda)
sparsEDA = nk.eda_phasic(eda, sampling_rate=sr, method="sparsEDA")
assert len(sparsEDA) == len(eda)


def test_eda_peaks():
Expand Down

0 comments on commit 2f923ce

Please sign in to comment.