Files
OBLITERATUS/tests/test_candidate_discovery.py
Joseph MaglyandClaude Opus 5.5 477eb753c8 feat(analysis): dataset-level candidate discovery with confound and null baselines
Extend candidate scanning from two prompts to distributions over a
labeled activation set (F07).

- Candidates with explicit coordinate identities: individual channels
  at a site and residual directions (a difference-of-means refit and
  the pack's stored basis rows). Binary (positive vs controls) or
  per-set contrasts.
- Measures reported separately, never combined: held-out AUC and
  balanced accuracy over group-disjoint folds, standardized mean
  difference, group-bootstrap intervals and fold stability. Everything
  fitted is refitted per training fold; recipe_top1 reports the honest
  held-out score of each fold's own top channel. Independent validation
  uses select rows; in-sample and basis-overlap scores are labelled.
- Nulls: a group-level shuffled-label null rerunning the full recipe
  (max over channels for channels) and random-direction/channel nulls,
  each with the observed quantile.
- Confounds: token-count and hashed-token lexical baselines plus length
  correlation, flagged beside each candidate; features are built in
  memory and no text is stored.
- Locked test is refused for discovery and excluded from acknowledged
  sets. Caps bound channels, bootstraps and permutations.
- `obliteratus experiments discover` and a Python API write versioned
  candidate tables; the Explorer lists them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 11:05:30 -05:00

320 lines
18 KiB
Python

"""Dataset-level candidate discovery: planted signal, shuffled-label calibration, confounds.
Fixtures reuse the planted model of ``tests/test_labeled_activations.py``:
the activation of a record is ``planted[set] + noise[number]`` at layer 0, so
the channel or direction that carries the class is known in advance.
"""
from __future__ import annotations
import json
import re
from dataclasses import replace
import pytest
import torch
from obliteratus import ui_evidence_explorer as explorer
from obliteratus.analysis import candidate_stats as cs
from obliteratus.analysis.candidate_confounds import confound_features, confound_flags
from obliteratus.analysis.candidate_discovery import discover_candidates
from obliteratus.analysis.candidate_report import TABLE_COLUMNS, read_reports, table_rows
from obliteratus.cli import main
from obliteratus.concepts.direction import ConceptDirection
from obliteratus.concepts.pack import ConceptPack
from obliteratus.experiments import SpecError
from obliteratus.experiments.discover import resolve_and_run, run_discover
from tests.test_labeled_activations import (
LEGACY, TETRAHEDRON, PlantedModel, WordTokenizer, build_pack, capture, load,
)
pytestmark = pytest.mark.cpu
FAST = {"n_permutations": 40, "n_bootstrap": 100, "n_random": 50}
TINY = {"n_permutations": 5, "n_bootstrap": 5, "n_random": 5}
HEX = re.compile(r"[0-9a-f]{64}")
def channel_two(value=4.0):
planted = torch.zeros(4, 3)
planted[0, 2] = value # only the positive set carries channel 2
return planted
def pack_direction(pack, receipt=True):
basis = torch.eye(10)[[2, 5]]
provenance = {"id": pack.manifest["id"], "sha256": pack.sha256}
if receipt:
provenance["dataset_access"] = {"revision": pack.dataset().revision, "role": "fit",
"locked_test_acknowledged": False}
return ConceptDirection(directions={0: basis[0]}, subspaces={0: basis},
extraction_layers=[0], intervention_layers=[0],
pack_provenance=provenance, concept_id="user/planted",
model_id="local/planted")
@pytest.fixture(scope="module")
def planted(tmp_path_factory):
root = tmp_path_factory.mktemp("discover")
pack = build_pack(root / "pack")
out = {"root": root, "pack": pack}
for name, model in (("one", PlantedModel(planted=channel_two())),
("tetra", PlantedModel(planted=TETRAHEDRON))):
capture(model, pack, root / name, roles=["fit", "select"])
out[name] = load(model, pack, root / name)
out["confounds"] = confound_features(pack, WordTokenizer(), out["one"])
return out
def test_planted_channel_and_direction_rank_first_with_tight_intervals(planted):
result = discover_candidates(planted["one"], "layer.0", confounds=planted["confounds"],
direction=pack_direction(planted["pack"]), **FAST)
json.dumps(result, allow_nan=False)
[contrast] = result["contrasts"]
assert contrast["class"] == "positive" and contrast["discovery"]["rows"] == 60
assert contrast["rest_sets"] == ["control:neutral", "control:other"]
assert contrast["validation"] == {"rows": 150, "target_rows": 50, "groups": 75}
top = contrast["tables"]["channel"][0]
assert top["id"] == "channel:layer.0:2" and top["coordinate"]["channel"] == 2
held = top["heldout"]
assert held["auc"] > 0.97 and held["auc_ci"][0] > 0.95 and held["fit_on_evaluation_data"] is False
assert held["auc_ci"][1] - held["auc_ci"][0] < 0.06 and held["smd"] > 2.5
assert held["balanced_accuracy_ci"][0] > 0.85 and len(held["fold_auc"]) == 5
assert top["validation"]["auc"] > 0.97 and top["validation"]["fit_on_evaluation_data"] is False
assert top["in_sample"]["fit_on_evaluation_data"] is True
assert top["nulls"]["shuffled_label"]["exceeds_q95"] is True
assert top["nulls"]["random_channel_quantile"] > 0.9
assert top["stability"] == {"sign_consistency": 1.0, "top_n_fraction": 1.0, "mean_rank": 1.0}
second = contrast["tables"]["channel"][1]["heldout"]
assert second["auc"] < held["auc_ci"][0]
assert contrast["recipe_top1"]["selected_channels"] == [2] * 5
# The planted class is a class word, so the lexical baseline matches it and is flagged;
# every row has three tokens, so the length baseline is uninformative.
assert contrast["confound_baselines"]["length"]["auc"] == 0.5
assert contrast["confound_baselines"]["lexical"]["auc"] > 0.95
assert top["confounds"]["flags"] == ["lexical_baseline_matches"]
directions = {e["id"]: e for e in contrast["tables"]["residual_direction"]}
assert set(directions) == {"direction:layer.0:dom", "direction:layer.0:pack0",
"direction:layer.0:pack1"}
assert [e["rank"] for e in contrast["tables"]["residual_direction"]] == [1, 2, 3]
dom, pack0, pack1 = (directions[f"direction:layer.0:{k}"] for k in ("dom", "pack0", "pack1"))
assert dom["heldout"]["auc"] > 0.97 and dom["stability"]["fold_direction_cosine"] > 0.95
assert dom["heldout"]["fit_on_evaluation_data"] is False
# The pack direction was fitted on role fit: scoring it on fit rows is labelled.
assert pack0["heldout"]["fit_on_evaluation_data"] is True
assert "heldout_fit_on_evaluation_data" in pack0["confounds"]["flags"]
assert pack0["validation"]["fit_on_evaluation_data"] is False
assert pack0["heldout"]["auc"] > 0.97 and pack1["heldout"]["auc"] < 0.8
assert result["pack_direction"]["fit_on_evaluation_data"]["heldout"]["value"] is True
examples = top["top_examples"]
assert len(examples["target"]) == 5 and all(e["set"] == "positive" for e in examples["target"])
assert all(e["role"] == "select" and e["input_sha256"] is None for e in examples["rest"])
dist = top["distributions"]
assert dist["scores"] == "validation" and sum(dist["target"]["histogram"]) == 50
assert len(dist["edges"]) == 13 and dist["target"]["median"] > dist["rest"]["median"]
assert "alpha sample" not in json.dumps(result) and "text" not in json.dumps(result)
def _shuffle_sets(labeled, seed):
"""Fit rows with set labels permuted across groups (one group per fit record)."""
fit = labeled.select(roles="fit", sets=["positive", "control:neutral", "control:other"])
generator = torch.Generator().manual_seed(1000 + seed)
order = torch.randperm(len(fit), generator=generator).tolist()
return replace(fit, sets=tuple(fit.sets[i] for i in order))
def test_shuffled_labels_do_not_systematically_yield_confident_discoveries(planted):
seeds = range(16)
hits = {"channel": 0, "dom": 0}
quantiles = []
for seed in seeds:
shuffled = _shuffle_sets(planted["tetra"], seed)
result = discover_candidates(shuffled, "layer.0", validation_roles=(), seed=seed,
n_permutations=40, n_bootstrap=1, n_random=0, top_n=1)
[contrast] = result["contrasts"]
assert contrast["validation"] is None
assert contrast["validation_unavailable_reason"] == "no validation roles requested"
top = contrast["tables"]["channel"][0]["nulls"]["shuffled_label"]
dom = contrast["tables"]["residual_direction"][0]["nulls"]["shuffled_label"]
hits["channel"] += top["exceeds_q95"]
hits["dom"] += dom["exceeds_q95"]
quantiles.append(top["quantile"])
# Nominal rate 0.05: 16 seeds expect 0.8; four or more has probability below 0.01.
assert hits["channel"] <= 3 and hits["dom"] <= 3
assert 0.25 < sum(quantiles) / len(quantiles) < 0.75
def test_paired_groups_permute_inside_the_group_and_still_find_the_planted_channel(planted):
from obliteratus.analysis.candidate_discovery import _permute
fit = planted["one"].select(roles="fit", sets=["positive", "control:neutral"])
# Pair positive k with control k in one independence unit.
groups = tuple("pair-" + re.sub(r"\D", "", record) for record in fit.record_ids)
paired = replace(fit, groups=groups)
y = torch.tensor([s == "positive" for s in paired.sets])
shuffled = _permute(y, list(groups), torch.Generator().manual_seed(0))
for g in set(groups):
rows = [i for i, h in enumerate(groups) if h == g]
assert int(shuffled[rows].sum()) == int(y[rows].sum()) == 1
assert not torch.equal(shuffled, y)
result = discover_candidates(paired, "layer.0", validation_roles=(), **TINY)
contrast = result["contrasts"][0]
assert contrast["discovery"]["groups"] == 20
assert contrast["tables"]["channel"][0]["id"] == "channel:layer.0:2"
def test_length_confound_matches_the_candidate_and_is_flagged(tmp_path):
def lengthen(layout):
for row in layout["positive.jsonl"]:
row["text"] += " present present"
pack = build_pack(tmp_path / "pack", lengthen)
model = PlantedModel(planted=torch.zeros(4, 3))
summary = run_discover(pack, model, WordTokenizer(), 0, tmp_path / "out",
model_id="local/planted", options=FAST)
[(path, result)] = read_reports(tmp_path / "out")
assert summary["report"] == str(path) and path.name == "layer.0-binary-fit.json"
contrast = result["contrasts"][0]
top = contrast["tables"]["channel"][0]
length = contrast["confound_baselines"]["length"]
assert top["id"] == "channel:layer.0:0" and top["heldout"]["auc"] > 0.97
assert length["auc"] >= top["heldout"]["auc"] - 0.05
assert "length_baseline_matches" in top["confounds"]["flags"]
assert "length_correlated" in top["confounds"]["flags"]
assert top["confounds"]["length_spearman"] > 0.5
assert all(HEX.fullmatch(e["input_sha256"]) for e in top["top_examples"]["target"])
again = run_discover(pack, model, WordTokenizer(), 0, tmp_path / "out",
model_id="local/planted", options=TINY)
assert again["reused_labeled_set"] and again["top"]["positive"]["channel"] == top["id"]
def test_locked_test_rows_are_never_used_even_when_acknowledged(planted, tmp_path):
pack, model = planted["pack"], PlantedModel(planted=channel_two())
capture(model, pack, tmp_path / "all", roles=["fit", "select", "test"],
allow_locked_test=True)
labeled = load(model, pack, tmp_path / "all", allow_locked_test=True)
result = discover_candidates(labeled, "layer.0", **TINY)
assert result["excluded_rows"] == {"locked_test": 24, "other_roles": 0}
text = json.dumps(result)
assert '"role": "test"' not in text
contrast = result["contrasts"][0]
assert contrast["discovery"]["rows"] == 60 and contrast["validation"]["rows"] == 150
for roles in ({"discovery_roles": ["test"]}, {"validation_roles": ["test"]}):
with pytest.raises(ValueError, match="never used"):
discover_candidates(labeled, "layer.0", **roles, **TINY)
with pytest.raises(ValueError, match="never used"):
run_discover(pack, model, WordTokenizer(), 0, tmp_path / "x", model_id="local/planted",
validation_roles="test")
only_fit = discover_candidates(labeled, "layer.0", validation_roles=(), **TINY)
assert only_fit["excluded_rows"] == {"locked_test": 24, "other_roles": 200}
def test_sets_mode_screening_and_option_refusals(planted):
result = discover_candidates(planted["tetra"], "layer.0", mode="sets", top_n=2,
max_channels=4, **TINY)
classes = [c["class"] for c in result["contrasts"]]
assert classes == ["control:neutral", "control:other", "neighbour:near", "positive"]
assert result["channels_scanned"] == 4 and result["channel_screen"].startswith("label-free")
assert all(len(c["tables"]["channel"]) == 2 for c in result["contrasts"])
assert all(c["tables"]["channel"][0]["coordinate"]["channel"] in range(3)
for c in result["contrasts"])
none = discover_candidates(planted["tetra"], "layer.0", n_permutations=0, n_random=0,
n_bootstrap=1, validation_roles="select")
entry = none["contrasts"][0]["tables"]["channel"][0]
assert entry["nulls"]["shuffled_label"]["quantile"] is None
assert none["contrasts"][0]["nulls"]["random_direction"] is None
labeled = planted["tetra"]
for options, match in (({"mode": "pairs"}, "mode must"), ({"folds": 1}, "folds must"),
({"top_n": True}, "top_n must"), ({"colour": 1}, "Unknown"),
({"discovery_roles": ["fit"], "validation_roles": ["fit"]},
"disjoint")):
with pytest.raises(ValueError, match=match):
discover_candidates(labeled, "layer.0", **options)
few = labeled.take(labeled.rows(roles="fit", sets="positive")[:6]
+ labeled.rows(roles="fit", sets="control:neutral")[:6])
with pytest.raises(ValueError, match="12 groups; need at least 20"):
discover_candidates(few, "layer.0", folds=20, validation_roles=())
with pytest.raises(ValueError, match="no site"):
discover_candidates(labeled, "layer.9")
with pytest.raises(ValueError, match="positive and control"):
discover_candidates(labeled.select(sets=["positive", "neighbour:near"]), "layer.0")
with pytest.raises(ValueError, match="two non-battery"):
discover_candidates(labeled.select(sets="positive"), "layer.0", mode="sets")
def test_measures_and_flags_on_known_values():
y = torch.tensor([True, True, False, False])
scores = torch.tensor([[2.0, 1.0], [1.0, 1.0], [0.0, 1.0], [-1.0, 1.0]])
assert cs.auc(scores, y).tolist() == [1.0, 0.5]
assert cs.balanced_accuracy(scores - 0.5, y).tolist() == [1.0, 0.5]
assert cs.average_ranks(torch.tensor([3.0, 1.0, 3.0])).flatten().tolist() == [2.5, 1.0, 2.5]
with pytest.raises(ValueError, match="both classes"):
cs.auc(scores, torch.ones(4, dtype=torch.bool))
with pytest.raises(ValueError, match="both classes"):
cs.fold_scores(scores, y, [(torch.tensor([0, 1]), torch.tensor([2, 3]))])
interval, used = cs.group_bootstrap(scores, torch.ones(4, dtype=torch.bool), list("abcd"), 3,
torch.Generator().manual_seed(0))
assert interval is None and used == 0
baselines = {"length": {"auc": 0.9}, "lexical": {"auc": 0.52}}
assert confound_flags(0.92, 0.1, baselines, 0.05, 0.5) == ["length_baseline_matches"]
assert confound_flags(0.99, -0.7, baselines, 0.05, 0.5) == ["length_correlated"]
def test_cli_run_directory_explorer_table_and_legacy_pack(tmp_path, capsys, monkeypatch):
from tests.test_observation_runs import make_spec
pack = build_pack(tmp_path / "pack")
pack_direction(pack, receipt=False).save(tmp_path / "direction.safetensors")
def factory(section):
return type("Handle", (), {"model": PlantedModel(planted=channel_two()),
"tokenizer": WordTokenizer(),
"cleanup": lambda self: None})()
monkeypatch.setattr("obliteratus.experiments.inputs.default_model_factory", factory)
small = ["--n-permutations", "5", "--n-bootstrap", "5", "--n-random", "5"]
main(["experiments", "discover", "--pack", str(tmp_path / "pack"), "--model", "local/planted",
"--layer", "0", "--out", str(tmp_path / "cli"), "--direction",
str(tmp_path / "direction.safetensors"), *small])
printed = json.loads(capsys.readouterr().out)
assert printed["top"]["positive"] == {"channel": "channel:layer.0:2",
"residual_direction": printed["top"]["positive"][
"residual_direction"]}
[(_, stored)] = read_reports(tmp_path / "cli")
packs = [e for e in stored["contrasts"][0]["tables"]["residual_direction"]
if e["coordinate"]["source"] == "pack_direction"]
assert all(e["heldout"]["fit_on_evaluation_data"] is None for e in packs)
assert all("heldout_fit_overlap_unknown" in e["confounds"]["flags"] for e in packs)
with pytest.raises(SystemExit) as locked:
main(["experiments", "discover", "--pack", str(tmp_path / "pack"), "--model", "m",
"--layer", "0", "--validation-role", "test", "--out", str(tmp_path / "locked")])
assert locked.value.code == 4 and "never used" in capsys.readouterr().err
run = tmp_path / "run"
run.mkdir()
(run / "spec.json").write_text(json.dumps(make_spec(tmp_path / "pack", capture=None)))
assert explorer.candidate_rows({"run_dir": run}) == []
summary = resolve_and_run(layer=0, run=run, model_factory=factory,
pack_resolver=ConceptPack.load, mode="sets", options=TINY)
assert summary["report"].endswith("candidates/layer.0-sets-fit.json")
(run / "candidates" / "notes.json").write_text("{}")
(run / "candidates" / "broken.json").write_text("{")
rows = explorer.candidate_rows({"run_dir": run})
assert len(rows) == 4 * (3 + 1) and all(len(r) == len(TABLE_COLUMNS) for r in rows)
assert rows[0][1] == "sets" and rows[0][3] == "channel" and rows[0][-1].endswith(".json")
assert table_rows(tmp_path / "absent") == []
with pytest.raises(SpecError, match="not both"):
resolve_and_run(layer=0, run=run, pack="p")
with pytest.raises(SpecError, match="are required"):
resolve_and_run(layer=0, pack="p")
other = build_pack(tmp_path / "other", lambda layout: layout["positive.jsonl"][30].update(
text="alpha sample 398"))
with pytest.raises(SpecError, match="revision differs"):
resolve_and_run(layer=0, run=run, model_factory=factory,
pack_resolver=lambda _ref: other)
legacy = run_discover(ConceptPack.load(LEGACY), PlantedModel(), WordTokenizer(), 0,
tmp_path / "legacy", model_id="local/planted", validation_roles=(),
options={**TINY, "folds": 2})
[(_, value)] = read_reports(tmp_path / "legacy")
assert legacy["top"]["positive"]["channel"] == "channel:layer.0:0"
assert value["groups_source"].startswith("legacy")
assert value["contrasts"][0]["validation"] is None