Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017eEAG8HD3mm8gsKXgk8uU8
222 lines
11 KiB
Python
222 lines
11 KiB
Python
"""Regression tests for the varga-resolution research scripts
|
|
(TASK-rectification-varga-resolution-research-20260930).
|
|
|
|
Pure-function tests run on synthetic data. The reproduction test reads the
|
|
committed baseline JSON and locks the numbers the brief was written from; it is
|
|
skipped when the JSON has not been generated in this checkout.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
from scripts.research import varga_resolution_lib as vr
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
BASELINE = ROOT / "docs" / "research" / "varga_resolution_baseline_2026_09_30.json"
|
|
|
|
|
|
def _signs(pattern: dict[str, list[int]], radius: int) -> dict[int, dict[str, int]]:
|
|
"""Build {offset: {varga: sign}} from per-varga sign lists covering -radius..radius."""
|
|
out: dict[int, dict[str, int]] = {}
|
|
for position, offset in enumerate(range(-radius, radius + 1)):
|
|
out[offset] = {name: values[position] for name, values in pattern.items()}
|
|
return out
|
|
|
|
|
|
class ClockTests(unittest.TestCase):
|
|
def test_offset_nearest_across_midnight(self) -> None:
|
|
self.assertEqual(vr.offset_of("00:05", "23:55"), 10)
|
|
self.assertEqual(vr.offset_of("23:50", "00:10"), -20)
|
|
self.assertEqual(vr.offset_of("12:00", "12:00"), 0)
|
|
|
|
def test_lmt_era_flag(self) -> None:
|
|
self.assertTrue(vr.is_lmt_era({"birth": {"date": "1859-05-22", "time": "04:55"}}))
|
|
self.assertFalse(vr.is_lmt_era({"birth": {"date": "1900-01-01", "time": "04:55"}}))
|
|
|
|
|
|
class SegmentTests(unittest.TestCase):
|
|
def setUp(self) -> None:
|
|
# radius 3 → offsets -3..3; D9 changes sign at offset 0, D10 at offset -2 and +2.
|
|
self.signs = _signs({
|
|
"D1": [4, 4, 4, 4, 4, 4, 4],
|
|
"D9": [7, 7, 7, 8, 8, 8, 8],
|
|
"D10": [1, 2, 2, 2, 2, 3, 3],
|
|
"D12": [0, 0, 1, 1, 2, 2, 3],
|
|
}, 3)
|
|
|
|
def test_single_varga_segments(self) -> None:
|
|
d9 = vr.segments_for(self.signs, ("D9",))
|
|
self.assertEqual([(s["start"], s["end"], s["key"]) for s in d9], [(-3, -1, (7,)), (0, 3, (8,))])
|
|
self.assertEqual(vr.segment_index_of(d9, 0), 1)
|
|
self.assertEqual(vr.segment_index_of(d9, -3), 0)
|
|
self.assertIsNone(vr.segment_index_of(d9, 9))
|
|
|
|
def test_joint_segments_split_on_any_change(self) -> None:
|
|
joint = vr.segments_for(self.signs, ("D1", "D9", "D10"))
|
|
self.assertEqual([(s["start"], s["end"]) for s in joint], [(-3, -3), (-2, -1), (0, 1), (2, 3)])
|
|
self.assertEqual(vr.distinct_signs(self.signs, 3, ("D1",)), 1)
|
|
self.assertEqual(vr.distinct_signs(self.signs, 3, ("D10",)), 3)
|
|
self.assertEqual(vr.distinct_signs(self.signs, 1, ("D9", "D10")), 2)
|
|
|
|
def test_count_table_shape(self) -> None:
|
|
table = vr.count_table({"a": self.signs, "b": self.signs}, radii=(1, 3))
|
|
self.assertEqual(table["3"]["D1"], {"single": 2, "n": 2, "mean": 1.0, "max": 1})
|
|
self.assertEqual(table["1"]["D9"]["mean"], 2.0)
|
|
self.assertEqual(table["3"]["D1xD9xD10"]["max"], 4)
|
|
|
|
|
|
class ShareTests(unittest.TestCase):
|
|
def setUp(self) -> None:
|
|
self.true_time = "10:00"
|
|
self.signs = _signs({"D9": [7, 7, 7, 8, 8, 8, 8]}, 3)
|
|
self.segments = vr.segments_for(self.signs, ("D9",))
|
|
|
|
def test_segment_shares_and_truth(self) -> None:
|
|
weights = {"09:58": 1.0, "09:59": 1.0, "10:00": 3.0, "10:02": 1.0}
|
|
share = vr.segment_shares(weights, self.segments, self.true_time)
|
|
self.assertEqual(share["truth_segment"], 1)
|
|
self.assertEqual(share["top_segment"], 1)
|
|
self.assertTrue(share["top_is_truth"])
|
|
self.assertTrue(share["truth_retained"])
|
|
self.assertAlmostEqual(share["top_share"], 4 / 6, places=6)
|
|
self.assertEqual(share["alive_segments"], 2)
|
|
|
|
def test_truth_segment_can_be_lost(self) -> None:
|
|
share = vr.segment_shares({"09:57": 2.0}, self.segments, self.true_time)
|
|
self.assertFalse(share["truth_retained"])
|
|
self.assertFalse(share["top_is_truth"])
|
|
self.assertEqual(share["alive_segments"], 1)
|
|
|
|
def test_minute_weights_modes(self) -> None:
|
|
state = {
|
|
"valid": [
|
|
{"time": "09:58", "score": 4.0, "cluster_times": ["09:58", "10:00"]},
|
|
{"time": "10:02", "score": -1.0, "cluster_times": ["10:02"]},
|
|
],
|
|
"public": [],
|
|
"eliminated": set(),
|
|
"scores": {"09:58": 4.0, "10:02": -1.0},
|
|
}
|
|
raw = vr.minute_weights(state, "raw")
|
|
self.assertEqual(raw, {"09:58": 2.0, "10:00": 2.0, "10:02": 0.0})
|
|
percent = vr.minute_weights(state, "percent")
|
|
self.assertAlmostEqual(percent["09:58"] + percent["10:00"], 1.0)
|
|
uniform = vr.minute_weights(state, "uniform")
|
|
self.assertEqual(set(uniform.values()), {1.0})
|
|
|
|
|
|
class InformationGainTests(unittest.TestCase):
|
|
def setUp(self) -> None:
|
|
self.true_time = "10:00"
|
|
self.signs = _signs({"D9": [7, 7, 7, 8, 8, 8, 8]}, 3)
|
|
self.segments = vr.segments_for(self.signs, ("D9",))
|
|
self.weights = {"09:57": 1.0, "09:58": 1.0, "09:59": 1.0, "10:00": 1.0, "10:01": 1.0, "10:02": 1.0, "10:03": 1.0}
|
|
|
|
@staticmethod
|
|
def _probe(yes: list[str], no: list[str]) -> dict:
|
|
return {"expected_outcomes": [
|
|
{"answer_class": "yes", "supports": yes, "conflicts": no},
|
|
{"answer_class": "no", "supports": no, "conflicts": yes},
|
|
]}
|
|
|
|
def test_probe_aligned_with_segment_boundary_has_full_gain(self) -> None:
|
|
aligned = self._probe(["09:57", "09:58", "09:59"], ["10:00", "10:01", "10:02", "10:03"])
|
|
within = self._probe(["10:00", "10:01"], ["10:02", "10:03"])
|
|
gain_aligned = vr.segment_information_gain(aligned, self.weights, self.segments, self.true_time)
|
|
gain_within = vr.segment_information_gain(within, self.weights, self.segments, self.true_time)
|
|
self.assertGreater(gain_aligned, gain_within)
|
|
# aligned probe resolves the two-segment variable completely: gain == H(prior)
|
|
self.assertAlmostEqual(gain_aligned, vr._entropy([3.0, 4.0]), places=5)
|
|
self.assertAlmostEqual(gain_within, 0.0, places=6)
|
|
|
|
def test_selection_prefers_aligned_probe_first(self) -> None:
|
|
public = [{"time": t, "score": 1.0, "cluster_times": [t]} for t in self.weights]
|
|
within = self._probe(["10:00", "10:01"], ["10:02", "10:03"])
|
|
aligned = self._probe(["09:57", "09:58", "09:59"], ["10:00", "10:01", "10:02", "10:03"])
|
|
state = vr.select_probes_by_segment(public, [within, aligned], self.segments, self.true_time, ask_count=1)
|
|
self.assertIs(state["asked"][0], aligned)
|
|
self.assertEqual(state["answered"], 1)
|
|
|
|
def test_stop_rule_stops_before_asking(self) -> None:
|
|
public = [{"time": t, "score": (5.0 if t >= "10:00" else 0.0), "cluster_times": [t]} for t in self.weights]
|
|
aligned = self._probe(["09:57", "09:58", "09:59"], ["10:00", "10:01", "10:02", "10:03"])
|
|
state = vr.select_probes_by_segment(public, [aligned], self.segments, self.true_time, ask_count=1, stop_share=0.8)
|
|
self.assertEqual(state["asked"], [])
|
|
|
|
|
|
class ThresholdTests(unittest.TestCase):
|
|
def test_full_fit_table(self) -> None:
|
|
points = [(0.95, True), (0.85, True), (0.75, False), (0.65, True), (0.55, False)]
|
|
table = {row["threshold"]: row for row in vr.threshold_table(points)}
|
|
self.assertEqual(table[0.9], {"threshold": 0.9, "n": 1, "accuracy": 1.0})
|
|
self.assertEqual(table[0.5]["n"], 5)
|
|
self.assertAlmostEqual(table[0.5]["accuracy"], 0.6)
|
|
|
|
def test_loo_threshold_never_sees_the_held_out_case(self) -> None:
|
|
points = [(0.95, True)] * 6 + [(0.55, False)] * 6
|
|
result = vr.loo_threshold(points, target=0.8, min_training=3)
|
|
self.assertEqual(result["confident_n"], 6)
|
|
self.assertEqual(result["confident_accuracy"], 1.0)
|
|
self.assertEqual(result["not_confident_n"], 6)
|
|
self.assertEqual(result["no_threshold_folds"], 0)
|
|
|
|
def test_loo_threshold_reports_when_no_threshold_reaches_target(self) -> None:
|
|
points = [(0.9, False), (0.9, False), (0.9, True), (0.6, False), (0.6, False), (0.6, True)]
|
|
result = vr.loo_threshold(points, target=0.9, min_training=3)
|
|
self.assertEqual(result["confident_n"], 0)
|
|
self.assertEqual(result["no_threshold_folds"], 6)
|
|
|
|
|
|
class FlipTests(unittest.TestCase):
|
|
def test_flipped_answers_invert_exactly_k(self) -> None:
|
|
probes = [
|
|
{"expected_outcomes": [{"answer_class": "yes", "supports": ["10:00"], "conflicts": ["10:02"]}]},
|
|
{"expected_outcomes": [{"answer_class": "no", "supports": ["10:00"], "conflicts": ["10:02"]}]},
|
|
{"expected_outcomes": [{"answer_class": "yes", "supports": ["10:02"], "conflicts": []}]},
|
|
]
|
|
base = [vr.optimal_answer(p, "10:00") for p in probes]
|
|
flipped = vr.flipped_answers(probes, "10:00", flips=1, seed="s")
|
|
changed = [i for i, (a, b) in enumerate(zip(base, flipped)) if a != b]
|
|
self.assertEqual(len(changed), 1)
|
|
self.assertIn(flipped[changed[0]], {"yes", "no"})
|
|
# deterministic for the same seed
|
|
self.assertEqual(flipped, vr.flipped_answers(probes, "10:00", flips=1, seed="s"))
|
|
|
|
|
|
@unittest.skipUnless(BASELINE.exists(), "baseline JSON not generated in this checkout")
|
|
class BaselineReproductionTests(unittest.TestCase):
|
|
"""Locks the two tables the brief was written from (77 v5 cases, raman / mean)."""
|
|
|
|
@classmethod
|
|
def setUpClass(cls) -> None:
|
|
cls.payload = json.loads(BASELINE.read_text(encoding="utf-8"))
|
|
|
|
def test_table1_signs_per_window(self) -> None:
|
|
table = self.payload["table1_signs_per_window"]
|
|
self.assertEqual(table["10"]["D1"]["single"], 64)
|
|
self.assertEqual(table["15"]["D1"]["single"], 51)
|
|
self.assertEqual(table["30"]["D1"]["single"], 37)
|
|
self.assertEqual(table["60"]["D1"]["single"], 10)
|
|
self.assertEqual(table["15"]["D9"]["mean"], 3.22)
|
|
self.assertEqual(table["15"]["D1xD9xD10"]["mean"], 5.27)
|
|
self.assertEqual(table["10"]["D1xD9xD10"]["mean"], 3.84)
|
|
|
|
def test_table2_delivery_interval(self) -> None:
|
|
table = self.payload["table2_delivery_interval"]
|
|
self.assertEqual(table["10"]["truth_in_range"], 76)
|
|
self.assertEqual(table["10"]["D1"]["single"], 75)
|
|
self.assertEqual(table["10"]["D9"]["le2"], 68)
|
|
self.assertEqual(table["10"]["D9"]["mode_is_truth"], 68)
|
|
self.assertEqual(table["10"]["D10"]["le2"], 63)
|
|
self.assertEqual(table["10"]["D10"]["mode_is_truth"], 65)
|
|
self.assertEqual(table["10"]["D1xD9xD10"]["single"], 15)
|
|
self.assertEqual(table["30"]["D9"]["le2"], 24)
|
|
self.assertEqual(table["60"]["D10"]["mode_is_truth"], 25)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|