"""O02 example-selection study on synthetic labeled rows with known geometry. Rows are built directly as a ``LabeledSelection`` so the geometry is exact: positives and controls separate along dimension 0; a large redundant block of near-duplicate controls sits far out along dimension 1; a rare control set (``control:rare``) sits inside the main control cloud, where coverage-seeking geometry has no reason to pick it. These are synthetic fixtures; nothing here is evidence about real models. """ from __future__ import annotations import json from collections import Counter import pytest import torch from obliteratus.analysis.example_selection import ( ARMS, NOT_PROBABILITY, ArmUnavailable, Pool, allocate, feature_matrix, generator, jaccard_distances, lexical_features, uniform_stratified, ) from obliteratus.analysis.example_selection_report import RULE, recommend, seed_stats from obliteratus.analysis.example_selection_study import resolve_config, run_selection_study from obliteratus.analysis.labeled_selection import LabeledSelection pytestmark = pytest.mark.cpu WIDTH = 8 def axis(**coords): v = torch.zeros(WIDTH) for k, value in coords.items(): v[int(k[1:])] = value return v def make_labeled(spec, seed=0, noise=1.0): """``spec``: [(set, kind, role, center, n, spread)]; group = record id.""" gen = torch.Generator().manual_seed(seed) rows = [] for set_, kind, role, center, n, spread in spec: for i in range(n): value = center + torch.randn(WIDTH, generator=gen) * noise * spread rows.append((set_, kind, role, value, f"{set_}-{role}-{len(rows)}-{i}")) return LabeledSelection( manifest={"pack": {"id": "synthetic"}}, values={"s": torch.stack([r[3] for r in rows])}, index=tuple(range(len(rows))), record_ids=tuple(r[4] for r in rows), paths=tuple(f"{r[0]}.jsonl" for r in rows), sets=tuple(r[0] for r in rows), kinds=tuple(r[1] for r in rows), roles=tuple(r[2] for r in rows), groups=tuple(r[4] for r in rows), families=(None,) * len(rows)) def redundant_with_rare(roles=("fit", "select")): spec = [] for role in roles: spec += [("positive", "positive", role, axis(d0=1.5), 20, 1.0), ("control:main", "control", role, axis(d0=-1.5), 12, 1.0), ("control:rare", "control", role, axis(d0=-1.5, d1=0.3), 3, 0.2)] if role == "fit": spec.append(("control:main", "control", role, axis(d0=-1.5, d1=4.0), 40, 0.01)) return make_labeled(spec) def text_features(labeled): return lexical_features([{"path": p, "record_id": r, "text": f"{s.split(':')[-1]} item {r}"} for p, r, s in zip(labeled.paths, labeled.record_ids, labeled.sets)]) FAST = {"budgets": [6, 12, 24], "seeds": 3} @pytest.fixture(scope="module") def rare_report(): labeled = redundant_with_rare() return labeled, run_selection_study(labeled, "s", lexical=text_features(labeled), config=FAST, timings=False) def test_rare_cases_are_measured_per_arm_and_violating_arms_are_flagged(rare_report): labeled, report = rare_report assert report["pool"]["rare_sets"] == ["control:rare"] assert report["evaluation"]["source"] == "role:select" assert set(report["arms"]) == set(ARMS) for name, arm in report["arms"].items(): rows = arm["frontier"] assert [r["budget"] for r in rows] == [6, 12, 24] for row in rows: assert row["heldout_auc"]["n"] == 3 and "interval" in row["heldout_auc"] assert row["cost"]["forwards"] == (75 if arm["family"] == "representation" else row["budget"]) flagged = any(r["coverage"]["violations"] for r in rows) assert arm["coverage_violation"] is flagged for row in rows: if "rare_recall" in row["coverage"]["violations"]: assert row["coverage"]["rare_recall_min"] < 1.0 # Stratified sampling keeps at least one row per stratum whenever the budget allows. assert not report["arms"]["uniform_stratified"]["coverage_violation"] flagged = [n for n, a in report["arms"].items() if a["coverage_violation"]] assert flagged and all(ARMS[n][0] == "representation" for n in flagged) recommendation = report["recommendation"] assert recommendation["verdict"] == "no improvement" and recommendation["rule"] == RULE for name in flagged: assert f"{name}: coverage limit violated; cannot be recommended" in \ recommendation["reasons"] def test_lineage_keeps_record_ids_probabilities_and_reference_sample(rare_report): labeled, report = rare_report stratified = report["arms"]["uniform_stratified"] assert stratified["sampling"].startswith("probability sample") for row in stratified["frontier"]: for entry in row["lineage"]: assert len(entry["inclusion_probabilities"]) == row["budget"] assert all(0 < p <= 1 for p in entry["inclusion_probabilities"]) assert entry["record_ids"] == [labeled.record_ids[i] for i in entry["labeled_rows"]] assert {labeled.roles[i] for i in entry["labeled_rows"]} == {"fit"} kcenter = report["arms"]["kcenter_coreset"] assert kcenter["sampling"] == NOT_PROBABILITY assert kcenter["frontier"][0]["lineage"][0]["inclusion_probabilities"] == NOT_PROBABILITY boundary = report["arms"]["boundary_hard_negatives"]["frontier"][0]["lineage"][0] assert boundary["labels_consulted"] > 0 reference = report["reference_sample"] assert reference["size"] == 24 and reference["inclusion_probability"] == 24 / 75 assert len(set(reference["record_ids"])) == 24 text = json.dumps(report) assert "item" not in text and "main item" not in text # no row text is ever stored def test_fixed_seed_reproduces_the_report_and_timings_stay_outside_the_digest(rare_report): labeled, report = rare_report lexical = text_features(labeled) again = run_selection_study(labeled, "s", lexical=lexical, config=FAST, timings=False) assert json.dumps(again, sort_keys=True) == json.dumps(report, sort_keys=True) timed = run_selection_study(labeled, "s", lexical=lexical, config=FAST, capture_cost={"wall_seconds": 1.5, "rows": 150}, lexical_seconds=0.01) assert timed["report_sha256"] == report["report_sha256"] host = timed["host_measurements"] assert host["recorded"] and "not used in the recommendation" in host["label"] assert host["capture_seconds_per_row"] == 0.01 assert host["estimated_capture_seconds"]["kcenter_coreset"]["6"] == pytest.approx(0.75) assert set(host["selection"]["uniform_stratified"]) == {"6", "12", "24"} changed = run_selection_study(labeled, "s", lexical=lexical, config={**FAST, "seeds": 4}, timings=False) assert changed["report_sha256"] != report["report_sha256"] def test_biased_sample_with_optimistic_in_sample_auc_is_not_an_improvement(): spec = [] for role in ("fit", "select"): spec += [("positive", "positive", role, axis(d0=0.6), 40, 1.0), ("control:main", "control", role, axis(d0=-0.6), 40, 1.0)] labeled = make_labeled(spec, seed=3) def biased(pool, allocation, gen, seed=0): # Positives with the largest, controls with the smallest pure-noise coordinate. order = pool.values[:, 3].argsort(descending=True).tolist() pos = [i for i in order if bool(pool.positive[i])][:allocation[True]] neg = [i for i in order[::-1] if not bool(pool.positive[i])][:allocation[False]] return pos + neg, None arms = {name: ARMS[name] for name in ("uniform_stratified", "lexical_maxmin")} arms["biased_contrast"] = ("representation", biased) config = {"budgets": [8, 16], "seeds": 5, "embedding_cost": "amortized", "coverage": {"rare_share": 0.0}} report = run_selection_study(labeled, "s", lexical=text_features(labeled), config=config, arms=arms, timings=False) rows = report["arms"]["biased_contrast"]["frontier"] baseline_heldout = max(r["heldout_auc"]["max"] for name in ("uniform_stratified", "lexical_maxmin") for r in report["arms"][name]["frontier"]) for row in rows: assert row["in_sample_auc"]["mean"] > row["heldout_auc"]["mean"] + 0.1 assert row["in_sample_auc"]["mean"] > baseline_heldout assert "not used" in row["in_sample_auc"]["label"] assert not report["arms"]["biased_contrast"]["coverage_violation"] assert report["recommendation"]["verdict"] == "no improvement" assert report["recommendation"]["winners"] == [] def test_selection_never_touches_locked_test_rows(): labeled = make_labeled([("positive", "positive", "fit", axis(d0=1), 10, 1), ("control:a", "control", "fit", axis(d0=-1), 10, 1), ("positive", "positive", "test", axis(d0=1), 4, 1), ("control:a", "control", "test", axis(d0=-1), 4, 1)]) with pytest.raises(ValueError, match="locked test"): run_selection_study(labeled, "s", config={"seeds": 1}) fit_only = labeled.select(roles="fit") for bad in ({"pool_roles": ["fit", "test"]}, {"eval_role": "test"}): with pytest.raises(ValueError, match="locked test"): run_selection_study(fit_only, "s", config=bad) seen = [] def spy(pool, allocation, gen, seed=0): seen.append(len(pool)) return ARMS["kcenter_coreset"][1](pool, allocation, gen) report = run_selection_study(fit_only, "s", config={"seeds": 3, "budgets": [4]}, arms={"spy": ("representation", spy)}, timings=False) # Without a select partition, a group-disjoint holdout fold is evaluated and never selected. evaluation = report["evaluation"] assert evaluation["source"].startswith("group-disjoint holdout") assert evaluation["locked_test_rows_used"] == 0 assert seen == [report["pool"]["rows"]] * 3 assert report["pool"]["rows"] + evaluation["rows"] == 20 lineage = {i for e in report["arms"]["spy"]["frontier"][0]["lineage"] for i in e["labeled_rows"]} assert {fit_only.roles[i] for i in lineage} == {"fit"} assert report["recommendation"]["reasons"] == ["baseline unavailable: baseline-uniform, " "baseline-lexical"] def test_stratified_inclusion_probabilities_match_empirical_frequencies(): groups = ("g1", "g1", "g2", "g3", "h1", "h2") pool = Pool(torch.zeros(6, 2), torch.tensor([False] * 6), ("control:a",) * 4 + ("control:b",) * 2, groups) hits, stated = Counter(), {} for draw in range(3000): picked, probability = uniform_stratified(pool, {False: 3}, generator("p", draw)) hits.update(picked) stated.update(dict(zip(picked, probability))) # allocation 2 + 1; a g1 row: 2/3 groups x 1/2 rows = 1/3. assert stated == {0: 1 / 3, 1: 1 / 3, 2: 2 / 3, 3: 2 / 3, 4: 0.5, 5: 0.5} for row, probability in stated.items(): assert hits[row] / 3000 == pytest.approx(probability, abs=0.04) picked, probability = uniform_stratified(pool, {False: 6}, generator("p", 0)) assert sorted(picked) == list(range(6)) and probability == [1.0] * 6 def test_allocation_lexical_features_and_refusals(): assert allocate({"a": 50, "b": 3, "c": 7}, 6) == {"a": 4, "b": 1, "c": 1} assert allocate({"a": 5, "b": 1}, 1) == {"a": 1, "b": 0} assert allocate({"a": 2, "b": 9}, 10) == {"a": 2, "b": 8} with pytest.raises(ArmUnavailable, match="exceeds"): allocate({"a": 2}, 3) features = lexical_features([{"path": "p", "record_id": "1", "text": "Alpha beta"}, {"path": "p", "record_id": "2", "text": "alpha beta gamma"}]) assert all(isinstance(h, int) for h in features[("p", "1")]) and len(features[("p", "1")]) == 3 distances = jaccard_distances(feature_matrix(list(features.values()))) assert distances[0, 1] == pytest.approx(1 - 3 / 5) and distances[0, 0] == 0 labeled = redundant_with_rare() no_lexical = run_selection_study(labeled, "s", config={"seeds": 1, "budgets": [6]}, timings=False) lexical_row = no_lexical["arms"]["lexical_maxmin"]["frontier"][0] assert lexical_row["status"] == "unavailable" and "lexical" in lexical_row["reason"] assert no_lexical["recommendation"]["reasons"][0].startswith("1 seed(s) < 3") with pytest.raises(ValueError, match="Unknown"): resolve_config({"budget": [3]}) with pytest.raises(ValueError, match="Unknown"): resolve_config({"coverage": {"min_rare": 1}}) for bad, match in (({"embedding_cost": "free"}, "embedding_cost"), ({"seeds": 0}, "seeds")): with pytest.raises(ValueError, match=match): resolve_config(bad) with pytest.raises(ValueError, match="Budgets"): run_selection_study(labeled, "s", config={"budgets": [1]}) with pytest.raises(ValueError, match="no site"): run_selection_study(labeled, "missing") big = run_selection_study(labeled, "s", config={"seeds": 1, "budgets": [70]}, timings=False) assert big["arms"]["boundary_hard_negatives"]["frontier"][0]["status"] == "unavailable" def _row(budget, low, high, forwards, *, ok=True, assessable=True): return {"budget": budget, "status": "ok" if ok else "unavailable", "heldout_auc": {"min": low, "max": high, "mean": (low + high) / 2}, "coverage": {"limits_assessable": assessable}, "cost": {"forwards": forwards}} def _arms(rep_rows, violation=False): return {"u": {"family": "baseline-uniform", "frontier": [_row(10, 0.70, 0.75, 10), _row(20, 0.74, 0.78, 20)]}, "l": {"family": "baseline-lexical", "frontier": [_row(10, 0.71, 0.76, 10), _row(20, 0.75, 0.79, 20)]}, "r": {"family": "representation", "coverage_violation": violation, "frontier": rep_rows}} def test_recommendation_rule_needs_both_baselines_coverage_and_cost_frontier(): full = {"heldout_auc": 0.80, "cost": {"forwards": 100}} win = recommend(_arms([_row(10, 0.85, 0.9, 10)]), full, seeds=5) assert win["verdict"] == "improvement" and win["winners"] == [ {"arm": "r", "budget": 10, "heldout_auc_min": 0.85}] overlap = recommend(_arms([_row(10, 0.75, 0.9, 10)]), full, seeds=5) assert overlap["verdict"] == "no improvement" assert recommend(_arms([_row(10, 0.85, 0.9, 10)], violation=True), full, seeds=5)["verdict"] == "no improvement" # Charged embedding: the representation point costs the whole pool, so the full pool wins. charged = recommend(_arms([_row(10, 0.79, 0.9, 100)]), full, seeds=5) assert charged["verdict"] == "no improvement" and "dominated" in charged["reasons"][0] # A cheaper baseline point reaching the same quality also dominates. arms = _arms([_row(20, 0.80, 0.85, 30)]) assert recommend(arms, {**full, "heldout_auc": 0.5}, seeds=5)["verdict"] == "improvement" arms["l"]["frontier"].append(_row(5, 0.84, 0.86, 5)) cheap = recommend(arms, {**full, "heldout_auc": 0.5}, seeds=5) assert cheap["verdict"] == "no improvement" and "dominated" in cheap["reasons"][0] assert recommend(_arms([_row(10, 0.85, 0.9, 10, assessable=False)]), full, seeds=5)["verdict"] == "no improvement" assert recommend(_arms([_row(30, 0.85, 0.9, 30)]), full, seeds=5)["verdict"] == \ "no improvement" # no baseline at that budget assert recommend(_arms([_row(10, 0.85, 0.9, 10)]), full, seeds=2)["reasons"] == [ "2 seed(s) < 3: seed uncertainty not assessable"] stats = seed_stats([0.5, 0.7]) assert stats["mean"] == pytest.approx(0.6) and stats["min"] == 0.5 and stats["max"] == 0.7 assert seed_stats([0.4])["sd"] == 0.0