-
-
Notifications
You must be signed in to change notification settings - Fork 437
/
Copy pathtests_emg.py
190 lines (152 loc) · 6.12 KB
/
tests_emg.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import biosppy
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import pytest
import scipy.stats
import neurokit2 as nk
# =============================================================================
# EMG
# =============================================================================
def test_emg_simulate():
emg1 = nk.emg_simulate(duration=20, length=5000, burst_number=1)
assert len(emg1) == 5000
emg2 = nk.emg_simulate(duration=20, length=5000, burst_number=15)
assert scipy.stats.median_abs_deviation(emg1) < scipy.stats.median_abs_deviation(
emg2
)
emg3 = nk.emg_simulate(duration=20, length=5000, burst_number=1, burst_duration=2.0)
# pd.DataFrame({"EMG1":emg1, "EMG3": emg3}).plot()
assert len(nk.signal_findpeaks(emg3, height_min=1.0)["Peaks"]) > len(
nk.signal_findpeaks(emg1, height_min=1.0)["Peaks"]
)
def test_emg_activation():
emg = nk.emg_simulate(duration=10, burst_number=3)
cleaned = nk.emg_clean(emg)
emg_amplitude = nk.emg_amplitude(cleaned)
activity_signal, info = nk.emg_activation(emg_amplitude)
assert set(activity_signal.columns.to_list()) == set(list(info.keys()))
assert len(info["EMG_Onsets"]) == len(info["EMG_Offsets"])
for i, j in zip(info["EMG_Onsets"], info["EMG_Offsets"]):
assert i < j
def test_emg_clean():
sampling_rate = 1000
emg = nk.emg_simulate(duration=20, sampling_rate=sampling_rate)
emg_cleaned = nk.emg_clean(emg, sampling_rate=sampling_rate)
assert emg.size == emg_cleaned.size
# Comparison to biosppy (https://github.com/PIA-Group/BioSPPy/blob/e65da30f6379852ecb98f8e2e0c9b4b5175416c3/biosppy/signals/emg.py)
original, _, _ = biosppy.tools.filter_signal(
signal=emg,
ftype="butter",
band="highpass",
order=4,
frequency=100,
sampling_rate=sampling_rate,
)
emg_cleaned_biosppy = nk.signal_detrend(original, order=0)
assert np.allclose((emg_cleaned - emg_cleaned_biosppy).mean(), 0, atol=1e-6)
def test_emg_plot():
sampling_rate = 1000
emg = nk.emg_simulate(duration=10, sampling_rate=1000, burst_number=3)
emg_summary, info = nk.emg_process(emg, sampling_rate=sampling_rate)
# Plot data over samples.
nk.emg_plot(emg_summary, info)
fig = plt.gcf()
assert len(fig.axes) == 2
titles = ["Raw and Cleaned Signal", "Muscle Activation"]
for ax, title in zip(fig.get_axes(), titles):
assert ax.get_title() == title
assert fig.get_axes()[1].get_xlabel() == "Time (seconds)"
np.testing.assert_array_equal(fig.axes[0].get_xticks(), fig.axes[1].get_xticks())
plt.close(fig)
def test_emg_eventrelated():
emg = nk.emg_simulate(duration=20, sampling_rate=1000, burst_number=3)
emg_signals, info = nk.emg_process(emg, sampling_rate=1000)
epochs = nk.epochs_create(
emg_signals,
events=[3000, 6000, 9000],
sampling_rate=1000,
epochs_start=-0.1,
epochs_end=1.9,
)
emg_eventrelated = nk.emg_eventrelated(epochs)
# Test amplitude features
no_activation = np.where(emg_eventrelated["EMG_Activation"] == 0)[0][0]
assert int(pd.DataFrame(emg_eventrelated.values[no_activation]).isna().sum()) == 5
assert np.alltrue(
np.nansum(np.array(emg_eventrelated["EMG_Amplitude_Mean"]))
< np.nansum(np.array(emg_eventrelated["EMG_Amplitude_Max"]))
)
assert len(emg_eventrelated["Label"]) == 3
# Test warning on missing columns
with pytest.warns(
nk.misc.NeuroKitWarning, match=r".*does not have an `EMG_Onsets`.*"
):
first_epoch_key = list(epochs.keys())[0]
first_epoch_copy = epochs[first_epoch_key].copy()
del first_epoch_copy["EMG_Onsets"]
nk.emg_eventrelated({**epochs, first_epoch_key: first_epoch_copy})
with pytest.warns(
nk.misc.NeuroKitWarning, match=r".*does not have an `EMG_Activity`.*"
):
first_epoch_key = list(epochs.keys())[0]
first_epoch_copy = epochs[first_epoch_key].copy()
del first_epoch_copy["EMG_Activity"]
nk.emg_eventrelated({**epochs, first_epoch_key: first_epoch_copy})
with pytest.warns(
nk.misc.NeuroKitWarning, match=r".*does not have an.*`EMG_Amplitude`.*"
):
first_epoch_key = list(epochs.keys())[0]
first_epoch_copy = epochs[first_epoch_key].copy()
del first_epoch_copy["EMG_Amplitude"]
nk.emg_eventrelated({**epochs, first_epoch_key: first_epoch_copy})
def test_emg_intervalrelated():
emg = nk.emg_simulate(duration=40, sampling_rate=1000, burst_number=3)
emg_signals, info = nk.emg_process(emg, sampling_rate=1000)
columns = ["EMG_Activation_N", "EMG_Amplitude_Mean"]
# Test with signal dataframe
features_df = nk.emg_intervalrelated(emg_signals)
assert all(
elem in columns for elem in np.array(features_df.columns.values, dtype=str)
)
assert features_df.shape[0] == 1 # Number of rows
# Test with dict
columns.append("Label")
epochs = nk.epochs_create(
emg_signals, events=[0, 20000], sampling_rate=1000, epochs_end=20
)
features_dict = nk.emg_intervalrelated(epochs)
assert all(
elem in columns for elem in np.array(features_dict.columns.values, dtype=str)
)
assert features_dict.shape[0] == 2 # Number of rows
@pytest.mark.parametrize(
"method_cleaning, method_activation, threshold",
[
("none", "threshold", "default"),
("biosppy", "pelt", 0.5),
("biosppy", "mixture", 0.05),
("biosppy", "biosppy", "default"),
("biosppy", "silva", "default"),
],
)
def test_emg_report(tmp_path, method_cleaning, method_activation, threshold):
sampling_rate = 250
emg = nk.emg_simulate(
duration=30,
sampling_rate=sampling_rate,
random_state=0,
)
d = tmp_path / "sub"
d.mkdir()
p = d / "myreport.html"
signals, _ = nk.emg_process(
emg,
sampling_rate=sampling_rate,
report=str(p),
method_cleaning=method_cleaning,
method_activation=method_activation,
threshold=threshold,
)
assert p.is_file()
assert "EMG_Activity" in signals.columns