| """ |
| tests/unit/test_sa.py |
| ββββββββββββββββββββββββ |
| Unit tests for the Simulated Annealing utility functions. |
| """ |
| from __future__ import annotations |
|
|
| import numpy as np |
| import pytest |
|
|
| from src.infrastructure.processing.sa_helpers import ( |
| compute_sample_entropy, |
| longest_plateau, |
| run_simulated_annealing, |
| ) |
|
|
|
|
| def test_longest_plateau() -> None: |
| |
| signal1 = np.array([1.0, 2.0, 3.0, 4.0, 5.0]) |
| assert longest_plateau(signal1) == 0 |
|
|
| |
| signal2 = np.array([1.0, 2.0, 2.0, 2.0, 3.0]) |
| |
| |
| assert longest_plateau(signal2) == 2 |
|
|
| |
| signal3 = np.array([5.0, 5.0, 5.0, 5.0, 5.0, 5.0]) |
| |
| assert longest_plateau(signal3) == 5 |
|
|
| |
| assert longest_plateau(np.array([1.0])) == 0 |
|
|
|
|
| def test_compute_sample_entropy() -> None: |
| |
| signal1 = np.zeros(100) |
| assert compute_sample_entropy(signal1) == 0.0 |
|
|
| |
| t = np.linspace(0, 10, 100) |
| signal2 = np.sin(t) |
| ent2 = compute_sample_entropy(signal2) |
| assert ent2 > 0.0 |
|
|
| |
| rng = np.random.default_rng(42) |
| signal3 = rng.normal(0, 1.0, 100) |
| ent3 = compute_sample_entropy(signal3) |
| |
| assert ent3 > 0.0 |
|
|
|
|
| def test_run_simulated_annealing() -> None: |
| |
| n_seg = 10 |
| rng = np.random.default_rng(42) |
| |
| |
| ppg = np.zeros((n_seg, 224), dtype=np.float32) |
| ecg = np.zeros((n_seg, 224), dtype=np.float32) |
| |
| for i in range(n_seg): |
| t = np.linspace(0, 2*np.pi, 224) |
| if i == 0: |
| |
| ppg[i] = rng.normal(0, 1.0, 224) |
| ecg[i] = np.sin(t) |
| elif i == 1: |
| |
| ppg[i] = np.sin(t) |
| ppg[i, 50:100] = 0.5 |
| ecg[i] = np.sin(t) |
| else: |
| |
| ppg[i] = np.sin(t) + rng.normal(0, 0.01, 224) |
| ecg[i] = np.sin(t*2) + rng.normal(0, 0.01, 224) |
|
|
| |
| sbp_preds = np.array([120.0]*n_seg) |
| dbp_preds = np.array([80.0]*n_seg) |
| |
| sbp_preds[0] = 160.0 |
| sbp_preds[1] = 180.0 |
|
|
| |
| result = run_simulated_annealing( |
| ppg_segments=ppg, |
| ecg_segments=ecg, |
| sbp_preds=sbp_preds, |
| dbp_preds=dbp_preds, |
| n_steps=100, |
| alpha=0.05, |
| ) |
|
|
| |
| assert "optimal_lo" in result |
| assert "optimal_hi" in result |
| assert "optimal_max_plateau" in result |
| assert "best_loss" in result |
| assert "clean_indices" in result |
| assert "history" in result |
|
|
| |
| clean_indices = result["clean_indices"] |
| assert 0 not in clean_indices |
| assert 1 not in clean_indices |
| assert len(clean_indices) == 8 |
|
|