85 lines
3.0 KiB
Python
85 lines
3.0 KiB
Python
import math
|
|
|
|
import pytest
|
|
|
|
from regime import CONDITIONS, _metrics, run
|
|
|
|
|
|
@pytest.mark.parametrize("name", sorted(CONDITIONS))
|
|
def test_every_condition_runs(name):
|
|
res = run(name, seed=0, steps=300, window=5, bptt=10)
|
|
assert len(res["recovery"]) == 5
|
|
assert res["steady"] >= 0.0
|
|
assert res["n_switches"] >= 1
|
|
|
|
|
|
def test_rp_only_has_no_memory_and_zero_kappa():
|
|
assert CONDITIONS["rp_only"]["memory"] is False
|
|
assert CONDITIONS["grp_k0"]["kappa"] == 0.0
|
|
assert CONDITIONS["grp_flag"]["kappa"] == 0.0 and CONDITIONS["grp_flag"]["oracle"] == "change"
|
|
assert CONDITIONS["grp_regime"]["kappa"] == 0.0 and CONDITIONS["grp_regime"]["oracle"] == "regime"
|
|
|
|
|
|
def test_window_too_large_raises():
|
|
with pytest.raises(ValueError):
|
|
run("grp_k0", seed=0, steps=100, window=100)
|
|
|
|
|
|
def test_metrics_recovery_is_mean_loss_k_steps_after_switch():
|
|
# steps=20, half=10: a single switch at index 12 sits inside the measured
|
|
# second half. losses[i] = i, so recovery[k] = 12 + k exactly.
|
|
steps, window = 20, 3
|
|
losses = [float(i) for i in range(steps)]
|
|
switched = [False] * steps
|
|
switched[12] = True
|
|
arousals = [i * 0.01 for i in range(steps)]
|
|
m = _metrics(losses, switched, arousals, window)
|
|
assert m["recovery"] == [12.0, 13.0, 14.0]
|
|
assert m["n_switches"] == 1
|
|
assert m["arousal_at_switch"] == pytest.approx(0.12)
|
|
|
|
|
|
def test_metrics_ignores_switches_in_the_first_half():
|
|
steps, window = 20, 3
|
|
losses = [float(i) for i in range(steps)]
|
|
switched = [False] * steps
|
|
switched[12] = True
|
|
arousals = [i * 0.01 for i in range(steps)]
|
|
baseline = _metrics(losses, switched, arousals, window)
|
|
|
|
# a first-half switch with wildly different loss/arousal must not move any metric:
|
|
# half=10 and window=3 mean the earliest measured lookback starts at 10-3=7, so
|
|
# index 3 never enters any window computation.
|
|
losses2 = list(losses); losses2[3] = 999999.0
|
|
switched2 = list(switched); switched2[3] = True
|
|
arousals2 = list(arousals); arousals2[3] = 999.0
|
|
perturbed = _metrics(losses2, switched2, arousals2, window)
|
|
assert perturbed == baseline
|
|
|
|
|
|
def test_metrics_steady_excludes_window_steps_after_a_switch():
|
|
# steps=20, half=10, window=3: switch at 15 excludes i in [15, 15+window] = [15,18]
|
|
# from steady. If that exclusion didn't apply, the huge post-switch losses at
|
|
# 16-18 would pull steady far from 1.0.
|
|
steps, window = 20, 3
|
|
losses = [1.0] * steps
|
|
for i in (15, 16, 17, 18):
|
|
losses[i] = 999.0
|
|
switched = [False] * steps
|
|
switched[15] = True
|
|
arousals = [0.0] * steps
|
|
m = _metrics(losses, switched, arousals, window)
|
|
assert m["steady"] == pytest.approx(1.0)
|
|
|
|
|
|
def test_metrics_no_switch_in_range_gives_nan_recovery():
|
|
steps, window = 20, 3
|
|
losses = [1.0] * steps
|
|
switched = [False] * steps
|
|
arousals = [0.0] * steps
|
|
m = _metrics(losses, switched, arousals, window)
|
|
assert m["n_switches"] == 0
|
|
assert all(math.isnan(x) for x in m["recovery"])
|
|
assert math.isnan(m["arousal_at_switch"])
|
|
assert m["steady"] == pytest.approx(1.0)
|