mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 02:24:12 +01:00
Add the geometry laboratory (F10), a research branch of measurement tools that reports when its assumptions fail rather than assuming them. - Neighbourhood stability under noise (with chance level), local PCA rank, tangent variation between neighbouring local bases, LID (Levina-Bickel MLE and TwoNN) with a local-regularity test, resample stability, and raw-space versus PCA-view projection distortion. Every nonlinear measure carries a global PCA-rank baseline. - A rejected regularity test nulls the LID with a reason. Curvature is always null and unsupported. Graph splits, connectivity changes, duplicates, projection artifacts and unstable neighbourhoods are flagged and labelled as not concept structure. - Synthetic benchmarks with known answers (plane, sphere, circle, torus, swiss roll, union of subspaces) and density/noise sweeps; catalog entries checked against the code and every report's evidence. - `obliteratus experiments geometry-lab benchmark|sweep|points` and a Python API including verified labeled activation sets. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
226 lines
11 KiB
Python
226 lines
11 KiB
Python
"""Geometry laboratory report: schema, flags, linear baselines, catalog registry, CLI, labeled sets."""
|
|
|
|
import json
|
|
import re
|
|
from pathlib import Path
|
|
|
|
import numpy as np
|
|
import pytest
|
|
import torch
|
|
from safetensors.torch import save_file
|
|
|
|
from obliteratus.analysis import geometry_lab as lab
|
|
from obliteratus.analysis.geometry_lab_evidence import CATALOG, NONLINEAR
|
|
from obliteratus.analysis.labeled_selection import LabeledSelection
|
|
from obliteratus.cli import main
|
|
|
|
pytestmark = pytest.mark.cpu
|
|
DOC = Path(__file__).resolve().parents[1] / "docs" / "geometry-measurement-validity.md"
|
|
FAST = {"noise_replicates": 1, "n_resamples": 2, "null_replicates": 20}
|
|
STATUSES = {"diagnostic", "unavailable", "unsupported"}
|
|
|
|
|
|
def report(name, n=600, **kw):
|
|
return lab.run_benchmark(name, n, **{**FAST, **kw})
|
|
|
|
|
|
def codes(result):
|
|
return {f["code"] for f in result["flags"]}
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
def reports():
|
|
return {
|
|
"plane": report("plane"),
|
|
"union": report("union_of_subspaces", 1000),
|
|
"apart": report("union_of_subspaces", benchmark_params={"dims": (2, 2), "offset": 5.0}),
|
|
"swiss": report("swiss_roll"),
|
|
}
|
|
|
|
|
|
def test_report_is_versioned_serializable_and_deterministic(reports):
|
|
plane = reports["plane"]
|
|
assert plane["schema"] == lab.SCHEMA and plane["schema_version"] == 1
|
|
text = json.dumps(plane, sort_keys=True, allow_nan=False)
|
|
assert text == json.dumps(report("plane"), sort_keys=True, allow_nan=False)
|
|
assert plane["parameters"]["seed"] == 0 and plane["input"]["sha256"]
|
|
assert plane["view"]["method"] == "pca" and plane["view"]["fit_rows"] == "all"
|
|
assert codes(plane) == set() and plane["lid"]["dimension"] == pytest.approx(2, abs=0.3)
|
|
assert plane["benchmark"]["pca_rank_matches_linear_rank"] is True
|
|
assert plane["benchmark"]["lid_mle_abs_error"] < 0.3
|
|
|
|
|
|
def test_every_nonlinear_metric_has_a_linear_baseline(reports):
|
|
for result in reports.values():
|
|
for name in NONLINEAR:
|
|
section = result[name]
|
|
if section is not None:
|
|
assert section["linear_baseline"]["pca_rank"] == \
|
|
result["linear_baseline"]["pca_rank"] or name == "resample_stability"
|
|
assert isinstance(result["resample_stability"]["linear_baseline"]["pca_rank"], list)
|
|
assert "raw_vs_pca_rank_knn_overlap" in \
|
|
result["neighbourhood_stability"]["linear_baseline"]
|
|
|
|
|
|
def test_union_rejects_regularity_and_withholds_one_dimension(reports):
|
|
union = reports["union"]
|
|
assert union["local_regularity"]["verdict"] == "rejected"
|
|
assert union["lid"]["dimension"] is None and union["lid"]["estimates"]["mle"] is not None
|
|
assert {"local_regularity_rejected", "tangent_discontinuities"} <= codes(union)
|
|
assert union["measurement_evidence"]["lid"]["assumption_check"] == "rejected"
|
|
assert union["benchmark"]["truth"]["single_dimension"] is False
|
|
|
|
|
|
def test_disconnected_graph_is_flagged_not_presented_as_concepts(reports):
|
|
apart = reports["apart"]
|
|
assert apart["connectivity"]["n_components"] == 2
|
|
flag = next(f for f in apart["flags"] if f["code"] == "graph_disconnected")
|
|
assert "not evidence of separate concepts" in flag["message"]
|
|
assert "not evidence of concept structure" in apart["interpretation"]
|
|
assert apart["local_regularity"]["verdict"] == "not_rejected"
|
|
|
|
|
|
def test_projection_artifacts_are_flagged(reports):
|
|
swiss = reports["swiss"]
|
|
assert "projection_artifacts" in codes(swiss)
|
|
assert swiss["projection_distortion"]["knn_preservation"] < 0.5
|
|
assert "not concept structure" in next(
|
|
f["message"] for f in swiss["flags"] if f["code"] == "projection_artifacts")
|
|
assert swiss["view_comparison"]["raw"]["lid_mle"] != swiss["view_comparison"]["view"]["lid_mle"]
|
|
|
|
|
|
def test_curvature_is_unsupported_and_unknown(reports):
|
|
for result in reports.values():
|
|
assert result["curvature"] is None
|
|
assert result["measurement_evidence"]["curvature"]["status"] == "unsupported"
|
|
|
|
|
|
def test_small_k_unstable_neighbourhoods_and_subsampling_are_flagged():
|
|
cloud = torch.randn(200, 20, generator=torch.Generator().manual_seed(0), dtype=torch.float64)
|
|
result = lab.run_geometry_lab(cloud, k=6, rank_threshold=0.999, noise_levels=[5.0],
|
|
max_points=150, **FAST)
|
|
assert {"local_rank_at_cap", "neighbourhoods_unstable", "subsampled_input"} <= codes(result)
|
|
assert len(result["input"]["kept_rows"]) == 150 and result["input"]["n_input"] == 200
|
|
|
|
|
|
def test_unavailable_tangent_section_carries_a_reason():
|
|
x = lab.make_benchmark("plane", 60)["points"]
|
|
result = lab.run_geometry_lab(x, tangent_dim=10, subsample_fraction=0.1, **FAST)
|
|
assert result["tangent_variation"] is None
|
|
evidence = result["measurement_evidence"]["tangent_variation"]
|
|
assert evidence["status"] == "unavailable" and "not in [1, k)" in evidence["unavailable_reason"]
|
|
assert result["resample_stability"]["n_rows"] == 21
|
|
|
|
|
|
def test_labels_and_refusals():
|
|
x = lab.make_benchmark("union_of_subspaces", 400, dims=(2, 2), offset=5.0)["points"]
|
|
labels = ["a"] * 200 + ["b"] * 195 + ["c"] * 5
|
|
result = lab.run_geometry_lab(x, labels=labels, **FAST)
|
|
assert result["by_label"]["a"]["n_components"] == 1
|
|
assert result["by_label"]["c"] == {"n": 5, "unavailable_reason": "fewer than 2k + 1 rows"}
|
|
for bad in ({"k": 5}, {"rank_threshold": 0.0}, {"subsample_fraction": 1.0},
|
|
{"noise_levels": [-1]}, {"bogus": 1}):
|
|
with pytest.raises(ValueError):
|
|
lab.run_geometry_lab(x, **bad)
|
|
with pytest.raises(ValueError):
|
|
lab.run_geometry_lab(x, labels=["a"])
|
|
with pytest.raises(ValueError):
|
|
lab.run_geometry_lab(x[:20])
|
|
|
|
|
|
def test_sweeps_report_density_and_noise_sensitivity():
|
|
density = lab.run_sweep("plane", "density", [100, 1500], null_replicates=20)
|
|
assert density["schema"] == lab.SWEEP_SCHEMA and density["truth"]["intrinsic_dim"] == 2
|
|
assert density["rows"][-1]["lid_mle_abs_error"] < 0.25
|
|
noise = lab.run_sweep("sphere", "noise", [0.0, 0.1], n=600, null_replicates=20)
|
|
errors = [row["lid_mle_abs_error"] for row in noise["rows"]]
|
|
assert errors[0] < 0.25 < 2 < errors[1]
|
|
union = lab.run_sweep("union_of_subspaces", "density", [400], null_replicates=20)
|
|
assert union["rows"][0]["lid_mle_abs_error"] is None
|
|
assert lab.summary(noise)["rows"][1]["regularity"] in {"rejected", "not_rejected"}
|
|
with pytest.raises(ValueError):
|
|
lab.run_sweep("plane", "curvature", [1])
|
|
|
|
|
|
# Catalog registry -------------------------------------------------------------
|
|
|
|
def lab_catalog_rows():
|
|
text = DOC.read_text(encoding="utf-8")
|
|
match = re.search(r"^### Geometry laboratory report\s*$(.*?)(?=^#|\Z)", text, re.M | re.S)
|
|
assert match, "geometry laboratory catalog section missing"
|
|
return set(re.findall(r"^\| `([a-z_]+)` \|", match.group(1), re.M))
|
|
|
|
|
|
def test_every_lab_metric_has_a_catalog_row_and_complete_evidence(reports):
|
|
assert lab_catalog_rows() == set(CATALOG)
|
|
for result in reports.values():
|
|
assert set(result["measurement_evidence"]) == set(CATALOG)
|
|
for name, entry in result["measurement_evidence"].items():
|
|
assert entry["definition"] and entry["units"] and entry["assumptions"], name
|
|
assert entry["scope"] and entry["status"] in STATUSES
|
|
assert (entry["status"] == "diagnostic") == (entry["unavailable_reason"] is None)
|
|
if result[name] is None:
|
|
assert entry["status"] != "diagnostic", name
|
|
assert {CATALOG[name][3] for name in NONLINEAR} == {"synthetic-tested"}
|
|
|
|
|
|
# CLI and labeled activation sets ----------------------------------------------
|
|
|
|
def run_cli(capsys, *argv):
|
|
main(["experiments", "geometry-lab", *argv])
|
|
return json.loads(capsys.readouterr().out)
|
|
|
|
|
|
def test_cli_benchmark_sweep_and_points(tmp_path, capsys):
|
|
out = run_cli(capsys, "benchmark", "--name", "circle", "--n", "300", "--out",
|
|
str(tmp_path / "c.json"))
|
|
assert out["local_regularity"] == "not_rejected" and out["pca_rank"] == 2
|
|
stored = json.loads((tmp_path / "c.json").read_text())
|
|
assert stored["benchmark"]["truth"]["intrinsic_dim"] == 1
|
|
sweep = run_cli(capsys, "sweep", "--name", "plane", "--kind", "density", "--values", "200")
|
|
assert sweep["rows"][0]["value"] == 200 and sweep["kind"] == "density"
|
|
noise = run_cli(capsys, "sweep", "--name", "plane", "--kind", "noise", "--values", "0.01",
|
|
"--n", "200", "--out", str(tmp_path / "s.json"))
|
|
assert noise["schema"] == lab.SWEEP_SCHEMA
|
|
x = lab.make_benchmark("sphere", 200)["points"]
|
|
save_file({"acts": x.float()}, str(tmp_path / "p.safetensors"))
|
|
np.save(tmp_path / "p.npy", x.numpy())
|
|
full = run_cli(capsys, "points", str(tmp_path / "p.safetensors"))
|
|
assert full["schema"] == lab.SCHEMA and full["input"]["source"]["name"] == "p.safetensors"
|
|
assert run_cli(capsys, "points", str(tmp_path / "p.npy"), "--k", "8")["parameters"]["k"] == 8
|
|
save_file({"a": x.float(), "b": x.float()}, str(tmp_path / "two.safetensors"))
|
|
for argv in (["two.safetensors"], ["two.safetensors", "--key", "c"], ["p.txt"]):
|
|
(tmp_path / "p.txt").write_text("x")
|
|
with pytest.raises(SystemExit) as stop:
|
|
main(["experiments", "geometry-lab", "points", str(tmp_path / argv[0]), *argv[1:]])
|
|
assert stop.value.code == 4
|
|
capsys.readouterr()
|
|
|
|
|
|
def test_lab_on_a_verified_labeled_activation_set(tmp_path):
|
|
from tests.test_labeled_activations import PlantedModel, build_pack, capture, load
|
|
pack, model = build_pack(tmp_path / "pack"), PlantedModel()
|
|
capture(model, pack, tmp_path / "set", roles=["select", "calibrate"])
|
|
labeled = load(model, pack, tmp_path / "set")
|
|
result = lab.lab_on_selection(labeled, "layer.0", **FAST)
|
|
source = result["input"]["source"]
|
|
assert source["kind"] == "labeled_activations" and source["n_rows"] == 216
|
|
assert source["capture_identity_sha256"] == labeled.manifest["capture_identity_sha256"]
|
|
assert set(result["by_label"]) == set(labeled.sets)
|
|
assert "linear_baseline" in result["local_rank"]
|
|
narrowed = lab.lab_on_selection(labeled, "layer.0", sets="positive", **FAST)
|
|
assert narrowed["input"]["n"] == 54 and narrowed["input"]["source"]["sets"] == ["positive"]
|
|
with pytest.raises(ValueError):
|
|
lab.lab_on_selection(labeled, "layer.9")
|
|
|
|
|
|
def test_selection_built_from_rows_uses_its_values():
|
|
x = lab.make_benchmark("plane", 60)["points"]
|
|
n = len(x)
|
|
selection = LabeledSelection(
|
|
manifest={}, values={"s": x}, index=tuple(range(n)), record_ids=tuple(map(str, range(n))),
|
|
paths=("p",) * n, sets=("positive",) * n, kinds=("positive",) * n,
|
|
roles=("select",) * n, groups=tuple(map(str, range(n))), families=(None,) * n)
|
|
result = lab.lab_on_selection(selection, "s", **FAST)
|
|
assert result["input"]["sha256"] == lab.run_geometry_lab(x, **FAST)["input"]["sha256"]
|