Skip to content

Commit 2cdd5fd

Browse files
authored
Implement tests for normalize_bound function
1 parent 2b253d8 commit 2cdd5fd

1 file changed

Lines changed: 62 additions & 0 deletions

File tree

‎tests/test_normalize_bound.py‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
"""Integer dynamic-range regressions for signal normalization."""
2+
3+
import numpy as np
4+
import pytest
5+
6+
import wfdb
7+
from wfdb import processing
8+
9+
10+
@pytest.mark.parametrize("dtype", [np.int8, np.int16, np.int32, np.int64])
11+
@pytest.mark.parametrize("bounds", [(-1, 1), (0, 1), (-3, 7)])
12+
def test_signed_integer_full_range(dtype, bounds):
13+
info = np.iinfo(dtype)
14+
sig = np.array([info.min + 1, 0, info.max], dtype=dtype)
15+
original = sig.copy()
16+
with np.errstate(over="raise", divide="raise", invalid="raise"):
17+
result = processing.normalize_bound(sig, *bounds)
18+
np.testing.assert_allclose(result, [bounds[0], sum(bounds) / 2, bounds[1]])
19+
np.testing.assert_array_equal(sig, original)
20+
21+
22+
@pytest.mark.parametrize("dtype", [np.uint8, np.uint16, np.uint32, np.uint64])
23+
def test_unsigned_integer_full_range(dtype):
24+
info = np.iinfo(dtype)
25+
sig = np.array([0, info.max], dtype=dtype)
26+
np.testing.assert_allclose(processing.normalize_bound(sig, -1, 1), [-1, 1])
27+
28+
29+
def test_normalize_digital_record(tmp_path):
30+
digital = np.array([[-30000], [0], [30000]], dtype=np.int16)
31+
wfdb.wrsamp(
32+
"normalize",
33+
fs=250,
34+
units=["mV"],
35+
sig_name=["ECG"],
36+
d_signal=digital,
37+
fmt=["16"],
38+
adc_gain=[200],
39+
baseline=[0],
40+
write_dir=str(tmp_path),
41+
)
42+
record = wfdb.rdrecord(str(tmp_path / "normalize"), physical=False, return_res=16)
43+
with np.errstate(over="raise"):
44+
result = processing.normalize_bound(record.d_signal[:, 0], -1, 1)
45+
np.testing.assert_allclose(result, [-1, 0, 1])
46+
47+
48+
def test_float_signal_retains_formula():
49+
sig = np.array([-3.5, -0.25, 1.75, 4.5])
50+
lb, ub = -3, 7
51+
mid = ub - (ub - lb) / 2
52+
lo, hi = np.min(sig), np.max(sig)
53+
center = hi - (hi - lo) / 2
54+
coef = (ub - lb) / (hi - lo)
55+
expected = sig * coef - center * coef + mid
56+
np.testing.assert_array_equal(processing.normalize_bound(sig, lb, ub), expected)
57+
58+
59+
def test_integer_list_input():
60+
np.testing.assert_allclose(
61+
processing.normalize_bound([-30000, 0, 30000], -1, 1), [-1, 0, 1]
62+
)

0 commit comments

Comments
 (0)