Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_eda_eventrelated():
eda = nk.eda_simulate(duration=15, scr_number=3)
eda_signals, info = nk.eda_process(eda, sampling_rate=1000)
epochs = nk.epochs_create(
eda_signals, events=[5000, 10000, 15000], sampling_rate=1000, epochs_start=-0.1, epochs_end=1.9
)
eda_eventrelated = nk.eda_eventrelated(epochs)
no_activation = np.where(eda_eventrelated["EDA_SCR"] == 0)[0][0]
assert int(pd.DataFrame(eda_eventrelated.values[no_activation]).isna().sum()) == 4
assert len(eda_eventrelated["Label"]) == 3
def test_eda_peaks():
sampling_rate = 1000
eda = nk.eda_simulate(duration=30, sampling_rate=sampling_rate, scr_number=6, noise=0, drift=0.01, random_state=42)
eda_phasic = nk.eda_phasic(nk.standardize(eda), method="highpass")["EDA_Phasic"].values
signals, info = nk.eda_peaks(eda_phasic, method="gamboa2008")
onsets, peaks, amplitudes = biosppy.eda.basic_scr(eda_phasic, sampling_rate=1000)
assert np.allclose((info["SCR_Peaks"] - peaks).mean(), 0, atol=1e-5)
signals, info = nk.eda_peaks(eda_phasic, method="kim2004")
onsets, peaks, amplitudes = biosppy.eda.kbk_scr(eda_phasic, sampling_rate=1000)
assert np.allclose((info["SCR_Peaks"] - peaks).mean(), 0, atol=1)
def test_bio_process():
sampling_rate = 1000
# Create data
ecg = nk.ecg_simulate(duration=30, sampling_rate=sampling_rate)
rsp = nk.rsp_simulate(duration=30, sampling_rate=sampling_rate)
eda = nk.eda_simulate(duration=30, sampling_rate=sampling_rate, scr_number=3)
emg = nk.emg_simulate(duration=30, sampling_rate=sampling_rate, burst_number=3)
bio_df, bio_info = nk.bio_process(ecg=ecg, rsp=rsp, eda=eda, emg=emg, sampling_rate=sampling_rate)
# SCR components
scr = [val for key, val in bio_info.items() if "SCR" in key]
assert all(len(elem) == len(scr[0]) for elem in scr)
assert all(bio_info["SCR_Onsets"] < bio_info["SCR_Peaks"])
assert all(bio_info["SCR_Peaks"] < bio_info["SCR_Recovery"])
# RSP
assert all(bio_info["RSP_Peaks"] > bio_info["RSP_Troughs"])
assert len(bio_info["RSP_Peaks"]) == len(bio_info["RSP_Troughs"])
# EMG
assert all(bio_info["EMG_Offsets"] > bio_info["EMG_Onsets"])
def test_eda_process():
eda = nk.eda_simulate(duration=30, scr_number=5, drift=0.1, noise=0, sampling_rate=250)
signals, info = nk.eda_process(eda, sampling_rate=250)
assert signals.shape == (7500, 11)
assert (
np.array(
[
"EDA_Raw",
"EDA_Clean",
"EDA_Tonic",
"EDA_Phasic",
"SCR_Onsets",
"SCR_Peaks",
"SCR_Height",
"SCR_Amplitude",
"SCR_RiseTime",
"SCR_Recovery",
def test_eda_simulate():
eda1 = nk.eda_simulate(duration=10, length=None, scr_number=1, random_state=333)
assert len(nk.signal_findpeaks(eda1, height_min=0.6)["Peaks"]) == 1
eda2 = nk.eda_simulate(duration=10, length=None, scr_number=5, random_state=333)
assert len(nk.signal_findpeaks(eda2, height_min=0.6)["Peaks"]) == 5
# pd.DataFrame({"EDA1": eda1, "EDA2": eda2}).plot()
assert len(nk.signal_findpeaks(eda2, height_min=0.6)["Peaks"]) > len(
nk.signal_findpeaks(eda1, height_min=0.6)["Peaks"]
)
def test_eda_clean():
sampling_rate = 1000
eda = nk.eda_simulate(
duration=30, sampling_rate=sampling_rate, scr_number=6, noise=0.01, drift=0.01, random_state=42
)
clean = nk.eda_clean(eda, sampling_rate=sampling_rate)
assert len(clean) == len(eda)
# Comparison to biosppy (https://github.com/PIA-Group/BioSPPy/blob/master/biosppy/signals/eda.py)
eda_biosppy = nk.eda_clean(eda, sampling_rate=sampling_rate, method="biosppy")
original, _, _ = biosppy.tools.filter_signal(
signal=eda, ftype="butter", band="lowpass", order=4, frequency=5, sampling_rate=sampling_rate
)
original, _ = biosppy.tools.smoother(signal=original, kernel="boxzen", size=int(0.75 * sampling_rate), mirror=True)
# pd.DataFrame({"our":eda_biosppy, "biosppy":original}).plot()
def test_eda_plot():
sampling_rate = 1000
eda = nk.eda_simulate(duration=30, sampling_rate=sampling_rate, scr_number=6, noise=0, drift=0.01, random_state=42)
eda_summary, _ = nk.eda_process(eda, sampling_rate=sampling_rate)
# Plot data over samples.
nk.eda_plot(eda_summary)
# This will identify the latest figure.
fig = plt.gcf()
assert len(fig.axes) == 3
titles = ["Raw and Cleaned Signal", "Skin Conductance Response (SCR)", "Skin Conductance Level (SCL)"]
for (ax, title) in zip(fig.get_axes(), titles):
assert ax.get_title() == title
assert fig.get_axes()[2].get_xlabel() == "Samples"
np.testing.assert_array_equal(fig.axes[0].get_xticks(), fig.axes[1].get_xticks(), fig.axes[2].get_xticks())
plt.close(fig)
# Plot data over seconds.
nk.eda_plot(eda_summary, sampling_rate=sampling_rate)
"PPG": nk.ppg_simulate(duration=10, heart_rate=70, powerline_amplitude=0),
"RSP": nk.rsp_simulate(duration=10, respiratory_rate=15, noise=0),
"EDA": nk.eda_simulate(duration=10, scr_number=3, noise=0),
"EMG": nk.emg_simulate(duration=10, burst_number=2, noise=0)})
plot = data.plot(subplots=True, layout=(5, 1), color=['#f44336', "#E91E63", "#2196F3", "#9C27B0", "#FF9800"])
fig = plt.gcf()
fig.set_size_inches(10, 6, forward=True)
[ax.legend(loc=1) for ax in plt.gcf().axes]
fig.savefig("README_simulation.png", dpi=300, h_pad=3)
# =============================================================================
# Electrodermal Activity (EDA) processing
# =============================================================================
# Generate 10 seconds of EDA signal (recorded at 250 samples / second) with 2 SCR peaks
eda = nk.eda_simulate(duration=10, sampling_rate=250, scr_number=2, drift=0.1)
# Process it
signals, info = nk.eda_process(eda, sampling_rate=250)
# Visualise the processing
nk.eda_plot(signals, sampling_rate=None)
# Save it
plot = nk.eda_plot(signals, sampling_rate=None)
plot.set_size_inches(10, 6, forward=True)
plot.savefig("README_eda.png", dpi=300, h_pad=3)
# =============================================================================
# Cardiac activity (ECG) processing
# =============================================================================
emg = nk.emg_simulate(duration=10, burst_number=2)
# Visualise biosignals
data = pd.DataFrame({"ECG": ecg,
"PPG": ppg,
"RSP": rsp,
"EDA": eda,
"EMG": emg})
nk.signal_plot(data, subplots=True)
# Save it
data = pd.DataFrame({"ECG": nk.ecg_simulate(duration=10, heart_rate=70, noise=0),
"PPG": nk.ppg_simulate(duration=10, heart_rate=70, powerline_amplitude=0),
"RSP": nk.rsp_simulate(duration=10, respiratory_rate=15, noise=0),
"EDA": nk.eda_simulate(duration=10, scr_number=3, noise=0),
"EMG": nk.emg_simulate(duration=10, burst_number=2, noise=0)})
plot = data.plot(subplots=True, layout=(5, 1), color=['#f44336', "#E91E63", "#2196F3", "#9C27B0", "#FF9800"])
fig = plt.gcf()
fig.set_size_inches(10, 6, forward=True)
[ax.legend(loc=1) for ax in plt.gcf().axes]
fig.savefig("README_simulation.png", dpi=300, h_pad=3)
# =============================================================================
# Electrodermal Activity (EDA) processing
# =============================================================================
# Generate 10 seconds of EDA signal (recorded at 250 samples / second) with 2 SCR peaks
eda = nk.eda_simulate(duration=10, sampling_rate=250, scr_number=2, drift=0.1)
# Process it
signals, info = nk.eda_process(eda, sampling_rate=250)