[d8937e]: / test / test_preprocessors.py

Download this file

274 lines (217 with data), 7.8 kB

  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
271
272
273
"""
"""
import itertools
from numbers import Real
from typing import Tuple
import numpy as np
import pytest
import torch
from torch_ecg._preprocessors import (
BandPass,
BaselineRemove,
MinMaxNormalize,
NaiveNormalize,
Normalize,
PreProcessor,
PreprocManager,
Resample,
ZScoreNormalize,
preprocess_multi_lead_signal,
preprocess_single_lead_signal,
)
from torch_ecg.cfg import CFG, DEFAULTS
test_sig = torch.randn(12, 80000).numpy()
class DummyPreProcessor(PreProcessor):
def __init__(self) -> None:
super().__init__()
def apply(self, sig: np.ndarray, fs: Real) -> Tuple[np.ndarray, int]:
return sig, fs
def test_preproc_manager():
sig = test_sig.copy()
ppm = PreprocManager()
assert ppm.empty
sig, fs = ppm(sig, 200)
ppm.add_(BandPass(0.5, 40))
assert not ppm.empty
ppm.add_(Resample(100), pos=0)
ppm.add_(ZScoreNormalize())
ppm.add_(BaselineRemove(), pos=1)
assert len(ppm.preprocessors) == 4
del ppm.preprocessors[-1]
assert len(ppm.preprocessors) == 3
ppm.add_(NaiveNormalize())
ppm.add_(DummyPreProcessor(), pos=0)
sig = test_sig.copy()
sig, fs = ppm(sig, 200)
config = CFG(
random=True,
resample={"fs": 500},
bandpass={"filter_type": "fir"},
normalize={"method": "min-max"},
baseline_remove={"window1": 0.3, "window2": 0.7},
)
ppm = PreprocManager.from_config(config)
assert ppm.random
sig = test_sig.copy()
sig, fs = ppm(sig, 200)
ppm.random = False
ppm.rearrange(
new_ordering=[
"baseline_remove",
"resample",
"bandpass",
"normalize",
]
)
ppm.random = True
with pytest.warns(RuntimeWarning, match="The preprocessors are applied in random order"):
ppm.rearrange(
new_ordering=[
"bandpass",
"baseline_remove",
"resample",
"normalize",
]
)
ppm.random = False
with pytest.raises(AssertionError, match="Duplicate preprocessor names"):
ppm.rearrange(
new_ordering=[
"bandpass",
"baseline_remove",
"resample",
"normalize",
"bandpass",
]
)
with pytest.raises(AssertionError, match="Number of preprocessors mismatch"):
ppm.rearrange(
new_ordering=[
"bandpass",
"baseline_remove",
"resample",
]
)
with pytest.raises(ValueError, match="Invalid input ECG signal"):
ppm(DEFAULTS.RNG.normal(size=(1, 1, 1, 5000)), 200)
config = {}
with pytest.warns(
RuntimeWarning,
match="No preprocessors added to the manager\\. You are using a dummy preprocessor",
):
ppm = PreprocManager.from_config(config)
assert ppm.empty
sig = test_sig.copy()
sig, fs = ppm(sig, 200)
assert str(ppm) == repr(ppm)
del ppm, sig, fs
def test_bandpass():
sig = test_sig.copy()
bp = BandPass(0, 40)
sig, fs = bp(sig, 200)
bp = BandPass(0.5, None)
sig, fs = bp(sig, 200)
assert str(bp) == repr(bp)
def test_baseline_remove():
sig = test_sig.copy()
br = BaselineRemove()
sig, fs = br(sig, 200)
br = BaselineRemove(0.3, 0.9)
sig, fs = br(sig, 200)
with pytest.warns(RuntimeWarning, match="values of `window1` and `window2` are switched"):
br = BaselineRemove(0.9, 0.3)
assert str(br) == repr(br)
def test_normalize():
sig = test_sig.copy()
std = 0.5 * np.ones(sig.shape[0])
norm = Normalize(std=std, per_channel=True)
sig, fs = norm(sig, 200)
with pytest.raises(AssertionError, match="standard deviation should be positive"):
norm = Normalize(std=0)
with pytest.raises(AssertionError, match="standard deviations should all be positive"):
norm = Normalize(std=np.zeros(sig.shape[0]))
assert str(norm) == repr(norm)
norm = MinMaxNormalize(per_channel=True)
sig, fs = norm(sig, 200)
assert str(norm) == repr(norm)
norm = NaiveNormalize(per_channel=True)
sig, fs = norm(sig, 200)
assert str(norm) == repr(norm)
norm = ZScoreNormalize(per_channel=True)
sig, fs = norm(sig, 200)
assert str(norm) == repr(norm)
def test_resample():
sig = test_sig.copy()
rsmp = Resample(fs=500)
sig, fs = rsmp(sig, 200)
rsmp = Resample(siglen=5000)
sig, fs = rsmp(sig, 200)
with pytest.raises(AssertionError, match="one and only one of `fs` and `siglen` should be set"):
rsmp = Resample(fs=500, siglen=5000)
with pytest.raises(AssertionError, match="one and only one of `fs` and `siglen` should be set"):
rsmp = Resample()
assert str(rsmp) == repr(rsmp)
def test_preprocess_multi_lead_signal():
sig = torch.randn(12, 8000).numpy()
fs = 200
grid = itertools.product(
["lead_first", "channel_last"], # sig_fmt
[None, [0.2, 0.6]], # bl_win
[None, [0.5, 45], [-np.inf, 40], [1, fs]], # band_fs
["butter", "fir"], # filter_type
)
for sig_fmt, bl_win, band_fs, filter_type in grid:
if sig_fmt == "channel_last":
filt_sig = sig.transpose(1, 0)
else:
filt_sig = sig.copy()
filt_sig = preprocess_multi_lead_signal(
filt_sig,
fs,
sig_fmt=sig_fmt,
bl_win=bl_win,
band_fs=band_fs,
filter_type=filter_type,
)
with pytest.raises(AssertionError, match="multi-lead signal should be 2d or 3d array"):
preprocess_multi_lead_signal(sig[0], fs)
with pytest.raises(AssertionError, match="multi-lead signal should be 2d or 3d array"):
preprocess_multi_lead_signal(torch.randn(1, 1, 12, 8000).numpy(), fs)
with pytest.raises(AssertionError, match="multi-lead signal format `xxx` not supported"):
preprocess_multi_lead_signal(sig, fs, sig_fmt="xxx")
with pytest.raises(AssertionError, match="Invalid frequency band"):
preprocess_multi_lead_signal(sig, fs, band_fs=[1, 0.5])
with pytest.raises(AssertionError, match="Invalid frequency band"):
preprocess_multi_lead_signal(sig, fs, band_fs=[0, fs])
with pytest.raises(ValueError, match="Unsupported filter type `xxx`"):
preprocess_multi_lead_signal(sig, fs, band_fs=[0.5, 45], filter_type="xxx")
def test_preprocess_single_lead_signal():
sig = torch.randn(8000).numpy()
fs = 200
grid = itertools.product(
[None, [0.2, 0.6]], # bl_win
[None, [0.5, 45], [-np.inf, 40], [1, fs]], # band_fs
["butter", "fir"], # filter_type
)
for bl_win, band_fs, filter_type in grid:
filt_sig = preprocess_single_lead_signal(
sig,
fs,
bl_win=bl_win,
band_fs=band_fs,
filter_type=filter_type,
)
with pytest.raises(AssertionError, match="single-lead signal should be 1d array"):
preprocess_single_lead_signal(sig[np.newaxis, ...], fs)
with pytest.raises(AssertionError, match="Invalid frequency band"):
preprocess_single_lead_signal(sig, fs, band_fs=[1, 0.5])
with pytest.raises(AssertionError, match="Invalid frequency band"):
preprocess_single_lead_signal(sig, fs, band_fs=[0, fs])
with pytest.raises(ValueError, match="Unsupported filter type `xxx`"):
preprocess_single_lead_signal(sig, fs, band_fs=[0.5, 45], filter_type="xxx")
def test_base_preprocessor():
with pytest.raises(
TypeError,
match=f"Can't instantiate abstract class {PreProcessor.__name__}",
):
PreProcessor()