"""O02 study: selection arms at matched budgets, scored on independent rows only. Selection sees permitted development rows only (``pool_roles``, default ``fit``). Quality is held-out targeting quality: a difference-of-means direction (controls pooled mean-of-means, as in extraction) is fitted on the selected rows and scored by AUC and balanced accuracy (threshold: midpoint of the fitted class means) on independent rows, either the ``eval_role`` partition (default ``select``) or, when it is absent, a group-disjoint holdout fold of the pool that no arm ever sees. Locked ``test`` rows are refused outright. In-sample AUC on the selected rows is reported, labelled optimistic, and never used for the recommendation. Costs: deterministic forward passes (rows whose activations an arm needs) and activation bytes at the site; representation arms are charged the whole pool unless ``embedding_cost`` is declared ``amortized``. Measured host timings go to ``host_measurements`` (excluded from ``report_sha256`` and the rule). """ from __future__ import annotations import time from collections import Counter from copy import deepcopy import torch from obliteratus.analysis.example_selection import ( ARMS, NOT_PROBABILITY, ArmUnavailable, Pool, allocate, feature_matrix, generator, ) from obliteratus.analysis.example_selection_report import recommend, seal, seed_stats from obliteratus.analysis.labeled_selection import group_folds from obliteratus.concepts.extraction import pooled_control_mean, projection_auc SCHEMA = "obliteratus.example-selection/1" DEFAULT_CONFIG = { "budgets": None, "seeds": 5, "negative_kinds": ["control"], "pool_roles": ["fit"], "eval_role": "select", "holdout_folds": 5, "holdout_seed": 0, "embedding_cost": "charged", "min_seeds_for_recommendation": 3, "coverage": {"min_set_coverage": 1.0, "min_rare_recall": 1.0, "rare_share": 0.1, "min_rare_rows": 1, "rare_sets": []}, } NOTE = ("Results on synthetic fixtures are not evidence for real models. Activation proximity is " "a selection aid; it does not establish semantic equivalence or authorize deleting " "records.") def resolve_config(config=None): """Defaults merged with ``config``; unknown keys and locked roles are refused.""" out, config = deepcopy(DEFAULT_CONFIG), deepcopy(config or {}) coverage = config.pop("coverage", {}) unknown = (set(config) - set(out)) | (set(coverage) - set(out["coverage"])) if unknown: raise ValueError(f"Unknown example-selection config keys: {sorted(unknown)}") out.update(config) out["coverage"].update(coverage) if "test" in out["pool_roles"] or out["eval_role"] == "test": raise ValueError("Example selection never uses locked test rows") if out["embedding_cost"] not in {"charged", "amortized"}: raise ValueError("embedding_cost must be charged or amortized") if isinstance(out["seeds"], bool) or not isinstance(out["seeds"], int) or out["seeds"] < 1: raise ValueError("seeds must be a positive integer") return out def _split(labeled, cfg): usable = [i for i in range(len(labeled)) if labeled.kinds[i] == "positive" or labeled.kinds[i] in cfg["negative_kinds"]] pool = [i for i in usable if labeled.roles[i] in cfg["pool_roles"]] held = [i for i in usable if labeled.roles[i] == cfg["eval_role"]] if held and cfg["eval_role"] not in cfg["pool_roles"]: if {labeled.groups[i] for i in pool} & {labeled.groups[i] for i in held}: raise ValueError("Evaluation rows share independence groups with the pool") source = {"source": f"role:{cfg['eval_role']}", "independent_of_selection": True} else: fold = group_folds([labeled.groups[i] for i in pool], cfg["holdout_folds"], cfg["holdout_seed"], [labeled.sets[i] for i in pool]) held = [i for i, f in zip(pool, fold) if f == 0] pool = [i for i, f in zip(pool, fold) if f != 0] source = {"source": f"group-disjoint holdout fold 0 of {cfg['holdout_folds']} " f"(seed {cfg['holdout_seed']})", "independent_of_selection": True} if any(labeled.roles[i] == "test" for i in pool + held): raise ValueError("Example selection never uses locked test rows") for name, rows in (("pool", pool), ("evaluation", held)): kinds = {labeled.kinds[i] == "positive" for i in rows} if kinds != {True, False}: raise ValueError(f"The {name} rows need both positive and negative rows") return pool, held, source def _fit(values, positive, sets): negatives = {} for i in range(len(values)): if not bool(positive[i]): negatives.setdefault(sets[i], []).append(i) positive_mean = values[positive].mean(0) control_mean = pooled_control_mean({s: values[r] for s, r in negatives.items()}) direction = positive_mean - control_mean return direction, float((positive_mean + control_mean) @ direction) / 2 def _score(direction, threshold, values, positive): scores = values @ direction auc = projection_auc(scores[positive], scores[~positive]) balanced = ((scores[positive] > threshold).double().mean() + (scores[~positive] <= threshold).double().mean()) / 2 return auc, float(balanced) def _coverage(pool, picked, allocation, rare, limits): counts = Counter(pool.sets[i] for i in picked) strata = sorted(set(pool.sets)) feasible = all( k >= sum(1 + (limits["min_rare_rows"] - 1) * (s in rare) for s in {pool.sets[i] for i in pool.members(positive)}) for positive, k in allocation.items()) set_coverage = sum(counts[s] > 0 for s in strata) / len(strata) rare_recall = (sum(counts[s] >= limits["min_rare_rows"] for s in rare) / len(rare) if rare else None) violations = [] if feasible and set_coverage < limits["min_set_coverage"]: violations.append("set_coverage") if feasible and rare_recall is not None and rare_recall < limits["min_rare_recall"]: violations.append("rare_recall") return {"feasible": feasible, "set_coverage": set_coverage, "rare_recall": rare_recall, "distinct_group_fraction": len({pool.groups[i] for i in picked}) / len(picked), "violations": violations} def _default_budgets(n): return sorted({min(n, max(2, round(n * f))) for f in (0.1, 0.2, 0.4)}) def run_selection_study(labeled, site, *, lexical=None, config=None, capture_cost=None, lexical_seconds=None, arms=None, timings=True): """Run every arm x budget x seed and return the sealed, versioned report.""" cfg = resolve_config(config) if "test" in labeled.roles: raise ValueError("Example selection refuses labeled sets holding locked test rows") if site not in labeled.values: raise ValueError(f"Labeled set has no site {site!r}") pool_rows, held, evaluation = _split(labeled, cfg) values = labeled.values[site].double() lex = None if lexical is not None: keys = [(labeled.paths[i], labeled.record_ids[i]) for i in pool_rows] lex = feature_matrix([lexical[k] for k in keys]) if all(k in lexical for k in keys) \ else None pool = Pool(values[pool_rows], torch.tensor([labeled.kinds[i] == "positive" for i in pool_rows]), tuple(labeled.sets[i] for i in pool_rows), tuple(labeled.groups[i] for i in pool_rows), lex) eval_values = values[held] eval_positive = torch.tensor([labeled.kinds[i] == "positive" for i in held]) limits, n = cfg["coverage"], len(pool) shares = Counter(pool.sets) rare = sorted({s for s, c in shares.items() if c / n < limits["rare_share"]} | (set(limits["rare_sets"]) & set(shares))) budgets = cfg["budgets"] or _default_budgets(n) if any(isinstance(b, bool) or not isinstance(b, int) or not 2 <= b <= n for b in budgets): raise ValueError(f"Budgets must be integers in [2, {n}]") bytes_per_row = values.shape[1] * labeled.values[site].element_size() direction, threshold = _fit(pool.values, pool.positive, pool.sets) full_auc, full_ba = _score(direction, threshold, eval_values, eval_positive) full = {"heldout_auc": full_auc, "heldout_balanced_accuracy": full_ba, "cost": {"forwards": n, "activation_bytes": n * bytes_per_row}} ref_size = max(budgets) ref = torch.randperm(n, generator=generator("reference", 0))[:ref_size].tolist() reference = {"design": "simple random sample without replacement", "size": ref_size, "inclusion_probability": ref_size / n, "labeled_rows": [labeled.index[pool_rows[i]] for i in sorted(ref)], "record_ids": [labeled.record_ids[pool_rows[i]] for i in sorted(ref)]} classes = {True: len(pool.members(True)), False: len(pool.members(False))} report_arms, host = {}, {} for name, (family, select) in (arms or ARMS).items(): frontier, host[name] = [], {} for budget in budgets: allocation = allocate(classes, budget) row = {"budget": budget, "allocation": {"positive": allocation[True], "negative": allocation[False]}} results, seconds = [], [] try: for seed in range(cfg["seeds"]): started = time.perf_counter() picked, probability, notes = (*select( pool, allocation, generator(name, budget, seed), seed=seed), {})[:3] seconds.append(time.perf_counter() - started) if len(set(picked)) != budget: raise ArmUnavailable(f"arm returned {len(set(picked))} distinct rows") results.append((seed, picked, probability, notes)) except ArmUnavailable as error: frontier.append({**row, "status": "unavailable", "reason": str(error)}) continue frontier.append(_summarize(row, results, pool, pool_rows, labeled, eval_values, eval_positive, rare, limits, family, cfg, bytes_per_row)) host[name][str(budget)] = {"selection_seconds_mean": sum(seconds) / len(seconds), "selection_seconds_max": max(seconds)} violated = any(r["status"] == "ok" and r["coverage"]["violations"] for r in frontier) report_arms[name] = { "family": family, "coverage_violation": violated, "sampling": "probability sample (exact inclusion probabilities per row)" if family == "baseline-uniform" else NOT_PROBABILITY, "frontier": frontier, "cost_frontier": sorted( ({"forwards": r["cost"]["forwards"], "budget": r["budget"], "heldout_auc_mean": r["heldout_auc"]["mean"], "heldout_auc_min": r["heldout_auc"]["min"], "heldout_auc_max": r["heldout_auc"]["max"]} for r in frontier if r["status"] == "ok"), key=lambda p: (p["forwards"], p["budget"]))} report = { "schema": SCHEMA, "study": "O02 coverage and contrast-based example selection", "site": site, "config": cfg, "budgets": budgets, "notes": [NOTE], "labeled_set": {k: labeled.manifest.get(k) for k in ("pack", "dataset_revision", "capture_identity_sha256", "labels_sha256", "partitioned", "roles")}, "pool": {"rows": n, "positive": classes[True], "negative": classes[False], "set_counts": dict(sorted(shares.items())), "rare_sets": rare, "groups": len(set(pool.groups)), "lexical_features": lex is not None}, "evaluation": {**evaluation, "rows": len(held), "set_counts": dict(sorted(Counter(labeled.sets[i] for i in held).items())), "locked_test_rows_used": 0}, "full_development_pool": full, "reference_sample": reference, "arms": report_arms, "recommendation": recommend(report_arms, full, seeds=cfg["seeds"], min_seeds=cfg["min_seeds_for_recommendation"])} report["host_measurements"] = _host(host, capture_cost, lexical_seconds, report_arms) \ if timings else {"recorded": False} return seal(report) def _summarize(row, results, pool, pool_rows, labeled, eval_values, eval_positive, rare, limits, family, cfg, bytes_per_row): aucs, bas, insample, coverage, lineage = [], [], [], [], [] for seed, picked, probability, notes in results: index = torch.tensor(picked) direction, threshold = _fit(pool.values[index], pool.positive[index], [pool.sets[i] for i in picked]) auc, ba = _score(direction, threshold, eval_values, eval_positive) aucs.append(auc) bas.append(ba) insample.append(_score(direction, threshold, pool.values[index], pool.positive[index])[0]) coverage.append(_coverage(pool, picked, {True: row["allocation"]["positive"], False: row["allocation"]["negative"]}, rare, limits)) lineage.append({"seed": seed, "labeled_rows": [labeled.index[pool_rows[i]] for i in picked], "record_ids": [labeled.record_ids[pool_rows[i]] for i in picked], "inclusion_probabilities": probability if probability is not None else NOT_PROBABILITY, **notes}) charged = family == "representation" and cfg["embedding_cost"] == "charged" forwards = len(pool) if charged else row["budget"] return {**row, "status": "ok", "heldout_auc": seed_stats(aucs), "heldout_balanced_accuracy": seed_stats(bas), "in_sample_auc": {**seed_stats(insample), "label": "optimistic in-sample AUC; not used for the " "recommendation"}, "coverage": {"limits_assessable": coverage[0]["feasible"], "set_coverage_min": min(c["set_coverage"] for c in coverage), "rare_recall_min": None if coverage[0]["rare_recall"] is None else min(c["rare_recall"] for c in coverage), "distinct_group_fraction_mean": sum(c["distinct_group_fraction"] for c in coverage) / len(coverage), "violations": sorted({v for c in coverage for v in c["violations"]})}, "cost": {"forwards": forwards, "activation_bytes": forwards * bytes_per_row, "embedding": "whole pool charged" if charged else ("whole pool declared amortized" if family == "representation" else "selected rows only")}, "lineage": lineage} def _host(selection, capture_cost, lexical_seconds, arms): per_row = None if capture_cost and capture_cost.get("wall_seconds") is not None and capture_cost.get("rows"): per_row = capture_cost["wall_seconds"] / capture_cost["rows"] estimated = {name: {str(r["budget"]): r["cost"]["forwards"] * per_row for r in arm["frontier"] if r["status"] == "ok"} for name, arm in arms.items()} if per_row is not None else None return {"recorded": True, "label": "measured on this host; not used in the recommendation or report_sha256", "capture": capture_cost or {"measured": False}, "capture_seconds_per_row": per_row, "estimated_capture_seconds": estimated, "lexical_feature_seconds": lexical_seconds, "selection": selection}