Files
OBLITERATUS/obliteratus/experiments/geometry_lab_cli.py
Joseph MaglyandClaude Opus 5.5 ac9dfc1222 feat(analysis): geometry laboratory with synthetic benchmarks
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>
2026-11-16 09:25:48 -05:00

104 lines
5.1 KiB
Python

"""``obliteratus experiments geometry-lab benchmark|sweep|points`` (geometry laboratory)."""
from pathlib import Path
from obliteratus.analysis.geometry_benchmarks import GENERATORS
def _lab_options(parser):
parser.add_argument("--k", type=int, default=10, help="Neighbours per row (>= 6)")
parser.add_argument("--rank-threshold", type=float, default=0.9,
help="Explained-variance threshold for PCA and local ranks")
parser.add_argument("--tangent-dim", type=int,
help="Tangent dimension (default: modal local rank)")
parser.add_argument("--noise-levels", type=float, nargs="+", default=[0.1, 0.5],
help="Perturbation norms relative to the median kNN distance")
parser.add_argument("--view-components", type=int, default=2)
parser.add_argument("--view-fit-fraction", type=float, default=1.0,
help="Fraction of rows the PCA view is fitted on (seeded)")
parser.add_argument("--max-points", type=int, default=4000)
parser.add_argument("--seed", type=int, default=0)
parser.add_argument("--out", help="Write the full JSON report here; print a summary")
def _benchmark_options(parser):
parser.add_argument("--name", required=True, choices=sorted(GENERATORS))
parser.add_argument("--n", type=int, default=1000, help="Rows")
parser.add_argument("--noise", type=float, default=0.0, help="Per-coordinate noise sigma")
parser.add_argument("--ambient-dim", type=int, default=10)
parser.add_argument("--benchmark-seed", type=int, default=0)
def add_geometry_lab(commands):
parser = commands.add_parser(
"geometry-lab", help="Geometry laboratory: local-structure measurements with linear "
"baselines on synthetic benchmarks or stored activations")
sub = parser.add_subparsers(dest="geometry_command", required=True)
bench = sub.add_parser("benchmark", help="Lab report on one synthetic benchmark with its "
"known answer")
_benchmark_options(bench)
_lab_options(bench)
sweep = sub.add_parser("sweep", help="Density or noise sensitivity of compact estimates")
_benchmark_options(sweep)
sweep.add_argument("--kind", required=True, choices=("density", "noise"))
sweep.add_argument("--values", type=float, nargs="+", required=True,
help="Row counts (density) or noise sigmas (noise)")
sweep.add_argument("--k", type=int, default=10)
sweep.add_argument("--rank-threshold", type=float, default=0.9)
sweep.add_argument("--seed", type=int, default=0)
sweep.add_argument("--out", help="Write the full JSON report here; print a summary")
points = sub.add_parser("points", help="Lab report on a stored (n, D) tensor "
"(.safetensors or .npy)")
points.add_argument("file", help="Tensor file")
points.add_argument("--key", help="Tensor name inside a .safetensors file")
_lab_options(points)
def _load_points(path, key):
path = Path(path)
if path.suffix == ".npy":
import numpy as np
import torch
return torch.from_numpy(np.load(path, allow_pickle=False))
if path.suffix != ".safetensors":
raise ValueError("points file must be .safetensors or .npy")
from safetensors.torch import load_file
tensors = load_file(str(path))
if key is None:
if len(tensors) != 1:
raise ValueError(f"--key is required; file holds {sorted(tensors)}")
key = next(iter(tensors))
if key not in tensors:
raise ValueError(f"Tensor {key!r} is not in {path.name}")
return tensors[key]
def _options(args):
return {"k": args.k, "rank_threshold": args.rank_threshold, "tangent_dim": args.tangent_dim,
"noise_levels": args.noise_levels, "view_components": args.view_components,
"view_fit_fraction": args.view_fit_fraction, "max_points": args.max_points,
"seed": args.seed}
def run_geometry_lab_command(args):
from obliteratus.analysis import geometry_lab as lab
if args.geometry_command == "benchmark":
report = lab.run_benchmark(args.name, args.n, noise=args.noise,
ambient_dim=args.ambient_dim,
benchmark_seed=args.benchmark_seed, **_options(args))
elif args.geometry_command == "sweep":
values = [int(v) for v in args.values] if args.kind == "density" else args.values
report = lab.run_sweep(args.name, args.kind, values, n=args.n, noise=args.noise,
ambient_dim=args.ambient_dim, benchmark_seed=args.benchmark_seed,
k=args.k, rank_threshold=args.rank_threshold, seed=args.seed)
else:
points = _load_points(args.file, args.key)
source = {"kind": "file", "name": Path(args.file).name, "key": args.key}
report = lab.run_geometry_lab(points, source=source, **_options(args))
if args.out is None:
return report
return {"report": str(lab.write_report(report, args.out)), **lab.summary(report)}
__all__ = ["add_geometry_lab", "run_geometry_lab_command"]