Skip to content

Commit 8c005b5

Browse files
committed
add gain tests for baseline outside boundary
1 parent 4cc0267 commit 8c005b5

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed

tests/test_record.py

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1335,6 +1335,106 @@ def test_physical_conversion(self):
13351335
atol=(0.05 / gain),
13361336
)
13371337

1338+
def test_adc_gain_max_boundary(self):
1339+
"""
1340+
Exercise the MAX_I32 clamp path: use a tiny range around a huge negative signal
1341+
so the computed baseline would exceed MAX_I32. Verify we hit that branch and
1342+
that a write/read round-trip preserves the physical signal within expected
1343+
quantization and the formula produces the expected result.
1344+
"""
1345+
# Tiny range around a large negative value forces baseline > MAX_I32
1346+
base_value = -1e10
1347+
p_signal = np.array(
1348+
[
1349+
[base_value],
1350+
[base_value + 0.5],
1351+
[base_value + 1.0],
1352+
[base_value + 1.5],
1353+
]
1354+
)
1355+
1356+
# Write the record
1357+
wfdb.wrsamp(
1358+
"test_negative_signal",
1359+
fs=250,
1360+
sig_name=["ECG"],
1361+
units=["mV"],
1362+
p_signal=p_signal,
1363+
fmt=["16"],
1364+
write_dir=self.temp_path,
1365+
)
1366+
1367+
# Read it back
1368+
record = wfdb.rdrecord(
1369+
os.path.join(self.temp_path, "test_negative_signal"),
1370+
physical=True,
1371+
)
1372+
1373+
# Round-trip physical signal should match within quantization tolerance
1374+
np.testing.assert_allclose(
1375+
record.p_signal,
1376+
p_signal,
1377+
rtol=1e-4,
1378+
atol=1e4, # Larger atol for large magnitude values
1379+
)
1380+
1381+
# Verify baseline was clamped to MAX_I32
1382+
self.assertEqual(record.baseline[0], 2147483647) # MAX_I32
1383+
1384+
# Confirm the formula is correct
1385+
expected_gain = (2147483647 - (-32768)) / abs(base_value)
1386+
np.testing.assert_allclose(record.adc_gain[0], expected_gain, rtol=1e-3)
1387+
1388+
def test_adc_gain_min_boundary(self):
1389+
"""
1390+
Exercise the MIN_I32 clamp path: use a tiny range around a huge positive signal
1391+
so the computed baseline would drop below MIN_I32. Verify we hit that branch and
1392+
that a write/read round-trip preserves the physical signal within expected
1393+
quantization and the formula produces the expected result..
1394+
"""
1395+
# Tiny range around a large positive value forces baseline < MIN_I32
1396+
base_value = 1e10
1397+
p_signal = np.array(
1398+
[
1399+
[base_value],
1400+
[base_value + 0.5],
1401+
[base_value + 1.0],
1402+
[base_value + 1.5],
1403+
]
1404+
)
1405+
1406+
# Write the record
1407+
wfdb.wrsamp(
1408+
"test_positive_signal",
1409+
fs=250,
1410+
sig_name=["ECG"],
1411+
units=["mV"],
1412+
p_signal=p_signal,
1413+
fmt=["16"],
1414+
write_dir=self.temp_path,
1415+
)
1416+
1417+
# Read it back
1418+
record = wfdb.rdrecord(
1419+
os.path.join(self.temp_path, "test_positive_signal"),
1420+
physical=True,
1421+
)
1422+
1423+
# Round-trip physical signal should match within quantization tolerance
1424+
np.testing.assert_allclose(
1425+
record.p_signal,
1426+
p_signal,
1427+
rtol=1e-4,
1428+
atol=1e4,
1429+
)
1430+
1431+
# Verify baseline was clamped to MIN_I32
1432+
self.assertEqual(record.baseline[0], -2147483648)
1433+
1434+
# Confirm the formula is correct
1435+
expected_gain = (32767 - (-2147483648)) / (base_value + 1.5)
1436+
np.testing.assert_allclose(record.adc_gain[0], expected_gain, rtol=1e-3)
1437+
13381438
@classmethod
13391439
def setUpClass(cls):
13401440
cls.temp_directory = tempfile.TemporaryDirectory()

0 commit comments

Comments
 (0)