|
| 1 | +"""Tests for the early stopping recommendation engine.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from trainpulse.early_stopping import EarlyStopping, EarlyStopResult, recommend_patience |
| 8 | + |
| 9 | + |
| 10 | +# ────────────────────────────────────────────────────────────────────── |
| 11 | +# EarlyStopResult dataclass |
| 12 | +# ────────────────────────────────────────────────────────────────────── |
| 13 | + |
| 14 | + |
| 15 | +class TestEarlyStopResult: |
| 16 | + def test_fields(self): |
| 17 | + r = EarlyStopResult(step=0, value=0.5, should_stop=False, improved=True, best_value=0.5, best_step=0) |
| 18 | + assert r.step == 0 |
| 19 | + assert r.value == 0.5 |
| 20 | + assert r.should_stop is False |
| 21 | + assert r.improved is True |
| 22 | + assert r.best_value == 0.5 |
| 23 | + assert r.best_step == 0 |
| 24 | + |
| 25 | + |
| 26 | +# ────────────────────────────────────────────────────────────────────── |
| 27 | +# EarlyStopping — core behaviour |
| 28 | +# ────────────────────────────────────────────────────────────────────── |
| 29 | + |
| 30 | + |
| 31 | +class TestEarlyStoppingMinMode: |
| 32 | + def test_first_step_always_improves(self): |
| 33 | + es = EarlyStopping(patience=3, min_delta=0.0) |
| 34 | + r = es.step(1.0) |
| 35 | + assert r.improved is True |
| 36 | + assert r.should_stop is False |
| 37 | + assert r.best_value == 1.0 |
| 38 | + assert r.best_step == 0 |
| 39 | + |
| 40 | + def test_monotonic_decrease_never_stops(self): |
| 41 | + es = EarlyStopping(patience=3, min_delta=0.0) |
| 42 | + for i in range(20): |
| 43 | + r = es.step(1.0 - i * 0.01) |
| 44 | + assert r.should_stop is False |
| 45 | + assert es.should_stop is False |
| 46 | + |
| 47 | + def test_patience_exhausted(self): |
| 48 | + es = EarlyStopping(patience=3, min_delta=0.0) |
| 49 | + es.step(1.0) # best |
| 50 | + es.step(1.1) |
| 51 | + es.step(1.2) |
| 52 | + r = es.step(1.3) # 3rd non-improvement → stop |
| 53 | + assert r.should_stop is True |
| 54 | + assert es.should_stop is True |
| 55 | + |
| 56 | + def test_improvement_resets_counter(self): |
| 57 | + es = EarlyStopping(patience=3, min_delta=0.0) |
| 58 | + es.step(1.0) |
| 59 | + es.step(1.1) # +1 |
| 60 | + es.step(1.2) # +2 |
| 61 | + es.step(0.5) # improvement → reset |
| 62 | + assert es.steps_without_improvement == 0 |
| 63 | + assert es.best_value == 0.5 |
| 64 | + |
| 65 | + def test_min_delta_respected(self): |
| 66 | + es = EarlyStopping(patience=3, min_delta=0.1) |
| 67 | + es.step(1.0) |
| 68 | + r = es.step(0.95) # only 0.05 better, below delta |
| 69 | + assert r.improved is False |
| 70 | + r = es.step(0.89) # 0.11 better than best (1.0) → improved |
| 71 | + assert r.improved is True |
| 72 | + |
| 73 | + def test_best_step_tracks_correctly(self): |
| 74 | + es = EarlyStopping(patience=5, min_delta=0.0) |
| 75 | + es.step(1.0) # step 0 |
| 76 | + es.step(0.8) # step 1 |
| 77 | + es.step(0.9) # step 2 |
| 78 | + es.step(0.7) # step 3 |
| 79 | + assert es.best_step == 3 |
| 80 | + assert es.best_value == 0.7 |
| 81 | + |
| 82 | + |
| 83 | +class TestEarlyStoppingMaxMode: |
| 84 | + def test_max_mode_improves_upward(self): |
| 85 | + es = EarlyStopping(patience=3, min_delta=0.0, mode="max") |
| 86 | + es.step(0.5) |
| 87 | + r = es.step(0.8) |
| 88 | + assert r.improved is True |
| 89 | + assert es.best_value == 0.8 |
| 90 | + |
| 91 | + def test_max_mode_stops_on_decline(self): |
| 92 | + es = EarlyStopping(patience=2, min_delta=0.0, mode="max") |
| 93 | + es.step(0.9) |
| 94 | + es.step(0.8) |
| 95 | + r = es.step(0.7) |
| 96 | + assert r.should_stop is True |
| 97 | + |
| 98 | + def test_max_mode_min_delta(self): |
| 99 | + es = EarlyStopping(patience=3, min_delta=0.1, mode="max") |
| 100 | + es.step(1.0) |
| 101 | + r = es.step(1.05) # only +0.05, below delta |
| 102 | + assert r.improved is False |
| 103 | + |
| 104 | + |
| 105 | +# ────────────────────────────────────────────────────────────────────── |
| 106 | +# EarlyStopping — edge cases & validation |
| 107 | +# ────────────────────────────────────────────────────────────────────── |
| 108 | + |
| 109 | + |
| 110 | +class TestEarlyStoppingEdgeCases: |
| 111 | + def test_patience_one(self): |
| 112 | + es = EarlyStopping(patience=1, min_delta=0.0) |
| 113 | + es.step(1.0) |
| 114 | + r = es.step(1.0) # no improvement |
| 115 | + assert r.should_stop is True |
| 116 | + |
| 117 | + def test_invalid_patience_raises(self): |
| 118 | + with pytest.raises(ValueError, match="patience"): |
| 119 | + EarlyStopping(patience=0) |
| 120 | + |
| 121 | + def test_invalid_mode_raises(self): |
| 122 | + with pytest.raises(ValueError, match="mode"): |
| 123 | + EarlyStopping(mode="average") # type: ignore[arg-type] |
| 124 | + |
| 125 | + def test_properties_before_any_step(self): |
| 126 | + es = EarlyStopping() |
| 127 | + assert es.best_value is None |
| 128 | + assert es.best_step == 0 |
| 129 | + assert es.should_stop is False |
| 130 | + assert es.steps_without_improvement == 0 |
| 131 | + |
| 132 | + def test_step_indices_increment(self): |
| 133 | + es = EarlyStopping(patience=10) |
| 134 | + results = [es.step(float(i)) for i in range(5)] |
| 135 | + assert [r.step for r in results] == [0, 1, 2, 3, 4] |
| 136 | + |
| 137 | + def test_equal_values_are_not_improvement_min(self): |
| 138 | + es = EarlyStopping(patience=3, min_delta=0.0) |
| 139 | + es.step(1.0) |
| 140 | + r = es.step(1.0) |
| 141 | + assert r.improved is False |
| 142 | + |
| 143 | + def test_equal_values_are_not_improvement_max(self): |
| 144 | + es = EarlyStopping(patience=3, min_delta=0.0, mode="max") |
| 145 | + es.step(1.0) |
| 146 | + r = es.step(1.0) |
| 147 | + assert r.improved is False |
| 148 | + |
| 149 | + |
| 150 | +# ────────────────────────────────────────────────────────────────────── |
| 151 | +# recommend_patience |
| 152 | +# ────────────────────────────────────────────────────────────────────── |
| 153 | + |
| 154 | + |
| 155 | +class TestRecommendPatience: |
| 156 | + def test_returns_default_for_short_history(self): |
| 157 | + assert recommend_patience([]) == 5 |
| 158 | + assert recommend_patience([0.5]) == 5 |
| 159 | + |
| 160 | + def test_monotonically_decreasing_loss(self): |
| 161 | + history = [1.0 - 0.01 * i for i in range(100)] |
| 162 | + p = recommend_patience(history) |
| 163 | + # Every step improves → gaps are all 1 → patience should be small. |
| 164 | + assert 3 <= p <= 10 |
| 165 | + |
| 166 | + def test_flat_loss_gives_high_patience(self): |
| 167 | + history = [1.0] * 50 |
| 168 | + p = recommend_patience(history) |
| 169 | + # No improvements at all → conservative patience. |
| 170 | + assert p >= 5 |
| 171 | + |
| 172 | + def test_noisy_loss_with_occasional_drops(self): |
| 173 | + # Spiky loss that drops every ~20 steps. |
| 174 | + import random |
| 175 | + |
| 176 | + rng = random.Random(42) |
| 177 | + history: list[float] = [] |
| 178 | + base = 1.0 |
| 179 | + for i in range(100): |
| 180 | + if i > 0 and i % 20 == 0: |
| 181 | + base -= 0.1 |
| 182 | + history.append(base + rng.uniform(-0.02, 0.02)) |
| 183 | + p = recommend_patience(history) |
| 184 | + # Gaps ~20 steps → patience should be at least 10. |
| 185 | + assert p >= 10 |
| 186 | + |
| 187 | + def test_result_is_always_positive_int(self): |
| 188 | + p = recommend_patience([10.0, 9.0, 8.0]) |
| 189 | + assert isinstance(p, int) |
| 190 | + assert p >= 1 |
0 commit comments