mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 10:34:23 +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>
267 lines
12 KiB
Python
267 lines
12 KiB
Python
"""Geometry laboratory metrics: analytic oracles and perturbation tests on known benchmarks."""
|
|
|
|
import math
|
|
|
|
import pytest
|
|
import torch
|
|
|
|
from obliteratus.analysis import geometry_lid as lid
|
|
from obliteratus.analysis import geometry_local as gl
|
|
from obliteratus.analysis.geometry_benchmarks import make_benchmark, random_rotation
|
|
from obliteratus.analysis.geometry_views import pca_view, projection_distortion
|
|
|
|
pytestmark = pytest.mark.cpu
|
|
CURVED_OR_FLAT = ["plane", "sphere", "circle", "torus", "swiss_roll"]
|
|
|
|
|
|
def points(name, n=1000, **kw):
|
|
return make_benchmark(name, n, **kw)["points"]
|
|
|
|
|
|
def neighbours(x, k=10):
|
|
return gl.knn(gl.pairwise(x), k)
|
|
|
|
|
|
def rigid(x, seed=5):
|
|
g = torch.Generator().manual_seed(seed)
|
|
return x @ random_rotation(x.shape[1], g).T + torch.randn(x.shape[1], generator=g,
|
|
dtype=torch.float64)
|
|
|
|
|
|
# Benchmarks -----------------------------------------------------------------
|
|
|
|
@pytest.mark.parametrize("name, dim, rank", [("plane", 2, 2), ("sphere", 2, 3), ("circle", 1, 2),
|
|
("torus", 2, 3), ("swiss_roll", 2, 3)])
|
|
def test_benchmarks_have_declared_truth_and_affine_rank(name, dim, rank):
|
|
bench = make_benchmark(name, 300, ambient_dim=12, seed=3)
|
|
x = bench["points"]
|
|
assert x.shape == (300, 12) and bench["truth"]["intrinsic_dim"] == dim
|
|
singular = torch.linalg.svdvals(x - x.mean(0))
|
|
assert int((singular > 1e-9 * singular[0]).sum()) == rank == bench["truth"]["linear_rank"]
|
|
assert torch.equal(x, make_benchmark(name, 300, ambient_dim=12, seed=3)["points"])
|
|
|
|
|
|
def test_embedding_is_an_isometry_and_noise_is_declared():
|
|
sphere = points("sphere", 200, ambient_dim=7)
|
|
centre = sphere.mean(0)
|
|
assert torch.allclose(sphere.norm(dim=1), torch.ones(200, dtype=torch.float64), atol=1e-12)
|
|
assert centre.norm() < 0.2
|
|
noisy = make_benchmark("sphere", 200, ambient_dim=7, noise=0.1)
|
|
assert noisy["parameters"]["noise"] == 0.1 and not torch.allclose(noisy["points"], sphere)
|
|
|
|
|
|
def test_union_truth_and_refusals():
|
|
joined = make_benchmark("union_of_subspaces", 100)
|
|
assert joined["truth"] == {"intrinsic_dim": None, "component_dims": [3, 1], "linear_rank": 4,
|
|
"connected": True, "single_dimension": False}
|
|
apart = make_benchmark("union_of_subspaces", 100, dims=(2, 2), offset=5.0)
|
|
assert apart["truth"]["connected"] is False and apart["truth"]["single_dimension"] is True
|
|
for bad in ({"name": "cube"}, {"n": 2}, {"noise": -1.0}, {"ambient_dim": 1}):
|
|
with pytest.raises(ValueError):
|
|
make_benchmark(**{"name": "plane", **bad})
|
|
|
|
|
|
# kNN graph ------------------------------------------------------------------
|
|
|
|
def test_knn_breaks_ties_by_row_index_and_components_split_far_clusters():
|
|
square = torch.tensor([[0., 0.], [1., 0.], [0., 1.], [1., 1.]], dtype=torch.float64)
|
|
_, index = gl.knn(gl.pairwise(square), 2)
|
|
assert index.tolist() == [[1, 2], [0, 3], [0, 3], [1, 2]]
|
|
with pytest.raises(ValueError):
|
|
gl.knn(gl.pairwise(square), 4)
|
|
far = torch.cat([square, square + 100])
|
|
assert gl.components(gl.knn(gl.pairwise(far), 2)[1]) == [4, 4]
|
|
for bad in (torch.zeros(5), torch.zeros(2, 3), torch.tensor([[math.nan, 0.]] * 4)):
|
|
with pytest.raises(ValueError):
|
|
gl.as_points(bad)
|
|
|
|
|
|
# Neighbourhood stability ----------------------------------------------------
|
|
|
|
def test_noise_stability_is_one_at_zero_noise_and_rigid_motion_invariant():
|
|
x = points("sphere", 400)
|
|
_, index = neighbours(x)
|
|
out = gl.noise_stability(x, index, 10, [0.0], 2, torch.Generator().manual_seed(0))
|
|
assert out["levels"][0]["mean_overlap"] == 1.0
|
|
assert torch.equal(neighbours(rigid(x))[1], index)
|
|
assert out["chance_overlap"] == pytest.approx(10 / 399)
|
|
|
|
|
|
def test_noise_stability_decreases_with_noise_towards_chance():
|
|
x = points("plane", 400)
|
|
_, index = neighbours(x)
|
|
out = gl.noise_stability(x, index, 10, [0.05, 0.5, 5.0, 100.0], 2,
|
|
torch.Generator().manual_seed(0))
|
|
means = [lv["mean_overlap"] for lv in out["levels"]]
|
|
assert means == sorted(means, reverse=True) and means[0] > 0.95
|
|
assert means[-1] < 5 * out["chance_overlap"]
|
|
|
|
|
|
# Local rank -----------------------------------------------------------------
|
|
|
|
@pytest.mark.parametrize("name, expected", [("plane", 2), ("sphere", 2), ("circle", 1),
|
|
("torus", 2)])
|
|
def test_local_rank_recovers_the_tangent_dimension(name, expected):
|
|
x = points(name, 1500)
|
|
report, ranks = gl.local_rank(x, neighbours(x)[1], 0.9)
|
|
assert report["modal"] == expected and report["modal_fraction"] > 0.95
|
|
assert gl.global_pca(x, 0.99)["pca_rank"] >= expected
|
|
|
|
|
|
def test_local_rank_is_rigid_invariant_and_noise_raises_it():
|
|
x = points("plane", 600)
|
|
_, ranks = gl.local_rank(x, neighbours(x)[1], 0.9)
|
|
_, moved = gl.local_rank(rigid(x), neighbours(rigid(x))[1], 0.9)
|
|
assert torch.equal(ranks, moved)
|
|
noisy = points("plane", 600, noise=0.2)
|
|
report, _ = gl.local_rank(noisy, neighbours(noisy)[1], 0.9)
|
|
assert report["median"] > 4
|
|
constant = torch.zeros(12, 3, dtype=torch.float64)
|
|
assert gl.local_rank(constant, neighbours(constant, 6)[1], 0.9)[0]["histogram"] == {"0": 12}
|
|
|
|
|
|
# Tangent variation ----------------------------------------------------------
|
|
|
|
def test_plane_tangents_do_not_vary_and_match_the_linear_baseline():
|
|
x = points("plane", 500)
|
|
out, base = gl.tangent_variation(x, neighbours(x)[1], 2, gl.global_basis(x, 2))
|
|
assert out["max_edge_angle_degrees"] < 1e-5 and out["discontinuity_fraction"] == 0.0
|
|
assert base["global_subspace_angle_degrees"]["p95"] < 1e-5
|
|
|
|
|
|
def test_even_circle_neighbour_tangents_differ_by_the_angular_step():
|
|
n = 360
|
|
x = make_benchmark("circle", n, spacing="even", ambient_dim=5)["points"]
|
|
out, base = gl.tangent_variation(x, neighbours(x, 2)[1], 1, gl.global_basis(x, 1))
|
|
assert out["n_edges"] == n
|
|
for value in out["edge_angle_degrees"].values():
|
|
assert value == pytest.approx(360 / n, abs=1e-6)
|
|
assert base["global_subspace_angle_degrees"]["p50"] > 30
|
|
|
|
|
|
def test_union_tangents_jump_across_the_intersection():
|
|
x = points("union_of_subspaces", 1000, dims=(2, 2))
|
|
out, _ = gl.tangent_variation(x, neighbours(x)[1], 2, gl.global_basis(x, 2))
|
|
assert out["max_edge_angle_degrees"] > 80
|
|
|
|
|
|
# LID ------------------------------------------------------------------------
|
|
|
|
def test_mle_and_twonn_match_hand_computed_values():
|
|
dist = torch.tensor([[1.0, 2.0, 4.0]] * 3, dtype=torch.float64)
|
|
assert lid.mle_lid(dist, 3)[0] == pytest.approx(2 / (3 * math.log(2)))
|
|
two = torch.tensor([[1.0, math.e]] * 4, dtype=torch.float64)
|
|
assert lid.twonn_lid(two)[0] == pytest.approx(1.0)
|
|
with pytest.raises(ValueError):
|
|
lid.mle_sums(dist, 2)
|
|
|
|
|
|
@pytest.mark.parametrize("name, dim", [("plane", 2), ("sphere", 2), ("circle", 1), ("torus", 2),
|
|
("swiss_roll", 2)])
|
|
def test_lid_recovers_dimension_at_adequate_density(name, dim):
|
|
dist, _ = neighbours(points(name, 2000), 10)
|
|
mle, bad = lid.mle_lid(dist, 10)
|
|
assert bad == 0
|
|
assert mle == pytest.approx(dim, abs=0.15 * dim + 0.1)
|
|
assert lid.twonn_lid(dist)[0] == pytest.approx(dim, abs=0.25 * dim + 0.1)
|
|
|
|
|
|
def test_lid_is_similarity_invariant_and_noise_inflates_it():
|
|
x = points("sphere", 800)
|
|
base = lid.mle_lid(neighbours(x)[0], 10)[0]
|
|
assert lid.mle_lid(neighbours(7.0 * rigid(x))[0], 10)[0] == pytest.approx(base, rel=1e-9)
|
|
noisy = points("sphere", 800, noise=0.1)
|
|
assert lid.mle_lid(neighbours(noisy)[0], 10)[0] > 4
|
|
|
|
|
|
def test_duplicate_rows_are_excluded_not_measured():
|
|
x = torch.cat([points("plane", 200), points("plane", 200)[:5]])
|
|
dist, _ = neighbours(x)
|
|
estimate, bad = lid.mle_lid(dist, 10)
|
|
assert bad == 10 and estimate is not None
|
|
assert lid.twonn_lid(dist)[1] == 10
|
|
|
|
|
|
# Local regularity -----------------------------------------------------------
|
|
|
|
@pytest.mark.parametrize("seed", [0, 1, 2])
|
|
def test_regularity_rejects_a_variable_dimension_union(seed):
|
|
x = points("union_of_subspaces", 1000, seed=seed)
|
|
dist, _ = neighbours(x)
|
|
out = lid.regularity_test(dist, 10, seed=seed)
|
|
assert out["verdict"] == "rejected" and out["dispersion"]["rejects"]
|
|
report = lid.lid_report(dist, 10, out)
|
|
assert report["dimension"] is None and "rejected" in report["dimension_withheld_reason"]
|
|
assert report["estimates"]["mle"] is not None
|
|
|
|
|
|
@pytest.mark.parametrize("name", CURVED_OR_FLAT)
|
|
@pytest.mark.parametrize("seed", [0, 1])
|
|
def test_regularity_does_not_reject_homogeneous_benchmarks(name, seed):
|
|
dist, _ = neighbours(points(name, 1000, seed=seed))
|
|
out = lid.regularity_test(dist, 10, seed=seed)
|
|
assert out["verdict"] == "not_rejected", out
|
|
assert lid.lid_report(dist, 10, out)["dimension"] is not None
|
|
|
|
|
|
def test_regularity_scale_check_rejects_scale_dependent_dimension():
|
|
"""A noisy line: small neighbourhoods see the noise, larger ones the line."""
|
|
x = points("circle", 2000, noise=0.004, ambient_dim=10)
|
|
dist, _ = neighbours(x, 20)
|
|
out = lid.regularity_test(dist, 20)
|
|
assert out["scale"]["rejects"] and out["verdict"] == "rejected"
|
|
|
|
|
|
def test_regularity_unavailable_inputs():
|
|
dist, _ = neighbours(points("plane", 100), 5)
|
|
assert lid.regularity_test(dist, 5)["verdict"] == "unavailable"
|
|
few, _ = neighbours(points("plane", 15), 10)
|
|
assert lid.regularity_test(few, 10)["verdict"] == "unavailable"
|
|
report = lid.lid_report(few, 10, {"verdict": "unavailable"})
|
|
assert report["dimension"] is None and "unavailable" in report["dimension_withheld_reason"]
|
|
|
|
|
|
# Projection distortion and views ---------------------------------------------
|
|
|
|
def test_exact_projection_of_a_plane_has_no_distortion():
|
|
x = points("plane", 300)
|
|
view, info = pca_view(x, 2)
|
|
out = projection_distortion(x, view, 10)
|
|
assert out["stress"] < 1e-9 and out["knn_preservation"] == 1.0
|
|
assert out["false_neighbour_fraction"] == 0.0 and out["artifacts"] is False
|
|
assert out["distance_ratio"]["p05"] == pytest.approx(1.0) == out["distance_ratio"]["p95"]
|
|
assert out["distance_spearman"] == pytest.approx(1.0)
|
|
assert info["fit_rows"] == "all" and info["n_fit"] == 300 and info["seed"] == 0
|
|
|
|
|
|
@pytest.mark.parametrize("name", ["sphere", "swiss_roll"])
|
|
def test_collapsing_views_are_flagged_as_artifacts(name):
|
|
x = points(name, 600)
|
|
out = projection_distortion(x, pca_view(x, 2)[0], 10)
|
|
assert out["artifacts"] is True and out["false_neighbour_fraction"] > 0.1
|
|
|
|
|
|
def test_distortion_is_rigid_invariant_and_samples_declared_pairs():
|
|
x = points("sphere", 300)
|
|
a = projection_distortion(x, pca_view(x, 2)[0], 10)
|
|
b = projection_distortion(rigid(x), pca_view(rigid(x), 2)[0], 10)
|
|
for key in ("knn_preservation", "false_neighbour_fraction"):
|
|
assert a[key] == pytest.approx(b[key])
|
|
assert a["stress"] == pytest.approx(b["stress"], rel=1e-6)
|
|
sampled = projection_distortion(x, pca_view(x, 2)[0], 10, max_pairs=500)
|
|
assert sampled["pairs_sampled"] and sampled["n_pairs"] <= 500
|
|
|
|
|
|
def test_view_records_fit_rows_seed_and_fixed_signs():
|
|
x = points("torus", 200)
|
|
view, info = pca_view(x, 2, fit_fraction=0.25, seed=4)
|
|
again, same = pca_view(x, 2, fit_fraction=0.25, seed=4)
|
|
assert torch.equal(view, again) and info == same
|
|
assert info["n_fit"] == 50 == len(info["fit_rows"]) and info["seed"] == 4
|
|
assert pca_view(x, 2, fit_fraction=0.25, seed=5)[1]["fit_rows"] != info["fit_rows"]
|
|
flipped, _ = pca_view(-x, 2)
|
|
assert torch.allclose(flipped.abs(), pca_view(x, 2)[0].abs())
|
|
for bad in ({"n_components": 0}, {"n_components": 11}, {"fit_fraction": 0.0}):
|
|
with pytest.raises(ValueError):
|
|
pca_view(x, **{"n_components": 2, **bad})
|