-
-
Notifications
You must be signed in to change notification settings - Fork 437
/
Copy pathtests_ppg.py
270 lines (220 loc) · 7.47 KB
/
tests_ppg.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# -*- coding: utf-8 -*-
import itertools
import matplotlib.pyplot as plt
import numpy as np
import pytest
import neurokit2 as nk
durations = (20, 200, 300)
sampling_rates = (25, 50, 500)
heart_rates = (50, 120)
freq_modulations = (0.1, 0.4)
params = [durations, sampling_rates, heart_rates, freq_modulations]
params_combis = list(itertools.product(*params))
@pytest.mark.parametrize(
"duration, sampling_rate, heart_rate, freq_modulation", params_combis
)
def test_ppg_simulate(duration, sampling_rate, heart_rate, freq_modulation):
ppg = nk.ppg_simulate(
duration=duration,
sampling_rate=sampling_rate,
heart_rate=heart_rate,
frequency_modulation=freq_modulation,
ibi_randomness=0,
drift=0,
motion_amplitude=0,
powerline_amplitude=0,
burst_amplitude=0,
burst_number=0,
random_state=42,
random_state_distort=42,
show=False,
)
assert ppg.size == duration * sampling_rate
signals, _ = nk.ppg_process(ppg, sampling_rate=sampling_rate)
if sampling_rate > 25:
assert np.allclose(signals["PPG_Rate"].mean(), heart_rate, atol=1)
# Ensure that the heart rate fluctuates in the requested range.
groundtruth_range = freq_modulation * heart_rate
observed_range = np.percentile(signals["PPG_Rate"], 90) - np.percentile(
signals["PPG_Rate"], 10
)
assert np.allclose(
groundtruth_range, observed_range, atol=groundtruth_range * 0.15
)
# TODO: test influence of different noise configurations
@pytest.mark.parametrize(
"ibi_randomness, std_heart_rate",
[(0.1, 3), (0.2, 5), (0.3, 8), (0.4, 11), (0.5, 14), (0.6, 19)],
)
def test_ppg_simulate_ibi(ibi_randomness, std_heart_rate):
ppg = nk.ppg_simulate(
duration=20,
sampling_rate=50,
heart_rate=70,
frequency_modulation=0,
ibi_randomness=ibi_randomness,
drift=0,
motion_amplitude=0,
powerline_amplitude=0,
burst_amplitude=0,
burst_number=0,
random_state=42,
show=False,
)
assert ppg.size == 20 * 50
signals, _ = nk.ppg_process(ppg, sampling_rate=50)
assert np.allclose(signals["PPG_Rate"].mean(), 70, atol=1.5)
# Ensure that standard deviation of heart rate
assert np.allclose(signals["PPG_Rate"].std(), std_heart_rate, atol=1)
# TODO: test influence of different noise configurations
def test_ppg_simulate_legacy_rng():
ppg = nk.ppg_simulate(
duration=30,
sampling_rate=250,
heart_rate=70,
frequency_modulation=0.2,
ibi_randomness=0.1,
drift=0.1,
motion_amplitude=0.1,
powerline_amplitude=0.01,
random_state=654,
random_state_distort="legacy",
show=False,
)
# Run simple checks to verify that the signal is the same as that generated with version 0.2.3
# before the introduction of the new random number generation approach
assert np.allclose(np.mean(ppg), 0.6598246992405254)
assert np.allclose(np.std(ppg), 0.4542274696384863)
assert np.allclose(
np.mean(np.reshape(ppg, (-1, 1500)), axis=1),
[0.630608661400, 0.63061887029, 0.60807993168, 0.65731025466, 0.77250577818],
)
def test_ppg_clean():
sampling_rate = 500
ppg = nk.ppg_simulate(
duration=30,
sampling_rate=sampling_rate,
heart_rate=180,
frequency_modulation=0.01,
ibi_randomness=0.1,
drift=1,
motion_amplitude=0.5,
powerline_amplitude=0.1,
burst_amplitude=1,
burst_number=5,
random_state=42,
show=False,
)
ppg_cleaned_elgendi = nk.ppg_clean(
ppg, sampling_rate=sampling_rate, method="elgendi"
)
assert ppg.size == ppg_cleaned_elgendi.size
# Assert that bandpass filter with .5 Hz lowcut and 8 Hz highcut was applied.
fft_raw = np.abs(np.fft.rfft(ppg))
fft_elgendi = np.abs(np.fft.rfft(ppg_cleaned_elgendi))
freqs = np.fft.rfftfreq(ppg.size, 1 / sampling_rate)
assert np.sum(fft_raw[freqs < 0.5]) > np.sum(fft_elgendi[freqs < 0.5])
assert np.sum(fft_raw[freqs > 8]) > np.sum(fft_elgendi[freqs > 8])
def test_ppg_findpeaks():
sampling_rate = 500
# Test Elgendi method
ppg = nk.ppg_simulate(
duration=30,
sampling_rate=sampling_rate,
heart_rate=60,
frequency_modulation=0.01,
ibi_randomness=0.1,
drift=1,
motion_amplitude=0.5,
powerline_amplitude=0.1,
burst_amplitude=1,
burst_number=5,
random_state=42,
show=True,
)
ppg_cleaned_elgendi = nk.ppg_clean(
ppg, sampling_rate=sampling_rate, method="elgendi"
)
info_elgendi = nk.ppg_findpeaks(
ppg_cleaned_elgendi, sampling_rate=sampling_rate, show=True
)
peaks = info_elgendi["PPG_Peaks"]
assert peaks.size == 29
assert np.abs(peaks.sum() - 219764) < 5 # off by no more than 5 samples in total
# Test MSPTD method
info_msptd = nk.ppg_findpeaks(
ppg, sampling_rate=sampling_rate, method="bishop", show=True
)
peaks = info_msptd["PPG_Peaks"]
assert peaks.size == 29
assert np.abs(peaks.sum() - 219665) < 30 # off by no more than 30 samples in total
@pytest.mark.parametrize(
"method_cleaning, method_peaks",
[("elgendi", "elgendi"), ("nabian2018", "elgendi"), ("elgendi", "bishop")],
)
def test_ppg_report(tmp_path, method_cleaning, method_peaks):
sampling_rate = 100
ppg = nk.ppg_simulate(
duration=30,
sampling_rate=sampling_rate,
heart_rate=60,
frequency_modulation=0.01,
ibi_randomness=0.1,
drift=1,
motion_amplitude=0.5,
powerline_amplitude=0.1,
burst_amplitude=1,
burst_number=5,
random_state=42,
show=True,
)
d = tmp_path / "sub"
d.mkdir()
p = d / "myreport.html"
signals, _ = nk.ppg_process(
ppg,
sampling_rate=sampling_rate,
report=str(p),
method_cleaning=method_cleaning,
method_peaks=method_peaks,
)
assert p.is_file()
def test_ppg_intervalrelated():
sampling_rate = 100
ppg = nk.ppg_simulate(
duration=500,
sampling_rate=sampling_rate,
heart_rate=70,
frequency_modulation=0.025,
ibi_randomness=0.15,
drift=0.5,
motion_amplitude=0.25,
powerline_amplitude=0.25,
burst_amplitude=0.5,
burst_number=3,
random_state=0,
show=True,
)
# Process the data
df, info = nk.ppg_process(ppg, sampling_rate=sampling_rate)
epochs = nk.epochs_create(
df, events=[0, 15000], sampling_rate=sampling_rate, epochs_end=150
)
epochs_ppg_intervals = nk.ppg_intervalrelated(epochs)
assert "PPG_Rate_Mean" in epochs_ppg_intervals.columns
ppg_intervals = nk.ppg_intervalrelated(df)
assert "PPG_Rate_Mean" in ppg_intervals.columns
def test_ppg_plot():
ppg = nk.ppg_simulate(duration=60, sampling_rate=250)
ppg_summary, info = nk.ppg_process(ppg, sampling_rate=250)
# Plot data over seconds.
nk.ppg_plot(ppg_summary, info)
fig = plt.gcf()
assert len(fig.axes) == 3
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)
# Make sure it works with cropped data
nk.ppg_plot(ppg_summary[0:1000], info)
fig = plt.gcf()
assert fig.get_axes()[2].get_xlabel() == "Time (seconds)"