Files
OBLITERATUS/tests/test_leace_erasure_oracles.py
Joseph MaglyandClaude Opus 5.5 40744bac67 fix(analysis): label geometry measurements and null unsupported values
Extend the measurement-validity treatment from riemannian_manifold.py
to concept_geometry, the LEACE paths, cross_layer and
wasserstein_optimal. Each audited result carries measurement evidence,
every reported field has a catalog entry, and a registry test fails
when either is missing.

- concept_geometry: the cone "solid angle" is a cap around the mean
  axis, not a minimal enclosing cone; relabelled. Placeholder defaults
  (cosine 1.0, specificity 1.0, linear/polyhedral flags, layer indices 0)
  become None when there is too little data. The informed pipeline skips
  unavailable flags; decisions on valid inputs are unchanged.
- cross_layer: persistence, adjacent cosine and angle path length are
  None with fewer than two layers instead of 0.0/1.0; zero or non-finite
  directions are rejected instead of reported as orthogonal.
- leace: the Fisher "erasure loss" is relabelled as variance along the
  direction; exact and multiclass LEACE gain erasure oracles.
- wasserstein_optimal: a layer with no signal reported a cost ratio of 0
  and could be chosen as best_layer; it is now None and excluded. The
  W2 cost is exact for any direction (closed-form oracle), and the
  comment calling it a bound is corrected.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 20:45:45 -05:00

149 lines
7.6 KiB
Python

"""Planted-concept oracles for exact, multiclass and Fisher LEACE paths.
Declared tolerance: float64 erasure residuals at most TOL on unit-scale data.
"""
import pytest
import torch
from obliteratus.analysis.leace import LEACEExtractor
from obliteratus.analysis.leace_exact import ExactLEACEExtractor, fit_leace_eraser
from obliteratus.concepts.extraction import ExtractionRecipe, extract_layer
pytestmark = pytest.mark.cpu
TOL = 1e-9
def planted(n=120, d=4, seed=0):
generator = torch.Generator().manual_seed(seed)
mix = torch.tensor([[3.0, 0.0, 0.0, 0.0], [1.0, 0.5, 0.0, 0.0],
[0.0, 0.2, 2.0, 0.0], [0.5, 0.0, 0.0, 0.1]], dtype=torch.float64)[:d, :d]
labels = torch.arange(n) % 2
shift = torch.tensor([1.0, -2.0, 0.5, 1.5], dtype=torch.float64)[:d]
X = torch.randn(n, d, generator=generator, dtype=torch.float64) @ mix + labels.unsqueeze(1) * shift
return X, labels
def probe_predictions(X, targets, weights=None):
"""Weighted least-squares linear probe with intercept (minimum-norm solution)."""
design = torch.cat([X, torch.ones(len(X), 1, dtype=X.dtype)], dim=1)
w = torch.ones(len(X), dtype=X.dtype) if weights is None else weights
root = w.sqrt().unsqueeze(1)
coefficients = torch.linalg.lstsq(design * root, targets * root, driver="gelsd").solution
return design @ coefficients
def test_exact_leace_erases_planted_binary_concept_and_preserves_the_complement():
X, labels = planted()
eraser = fit_leace_eraser(X, labels)
erased = eraser.apply(X)
delta = X[labels == 1].mean(0) - X[labels == 0].mean(0)
assert delta.norm() > 1 # the concept is present before erasure
gap = erased[labels == 1].mean(0) - erased[labels == 0].mean(0)
assert gap.abs().max() < TOL
z = labels.to(X.dtype)
cross = (erased - erased.mean(0)).T @ (z - z.mean()) / len(X)
assert cross.abs().max() < TOL
before = probe_predictions(X, z.unsqueeze(1))
after = probe_predictions(erased, z.unsqueeze(1))
assert (before - z.mean()).abs().max() > 0.1
assert (after - z.mean()).abs().max() < 1e-8 # probe collapses to the class prior
torch.testing.assert_close(erased.mean(0), X.mean(0), atol=TOL, rtol=0)
# Every projection orthogonal to delta is unchanged, so its variance is preserved.
complement = torch.linalg.svd(delta.unsqueeze(0)).Vh[1:]
torch.testing.assert_close(erased @ complement.T, X @ complement.T, atol=TOL, rtol=0)
# Minimal change: no larger than the orthogonal eraser, which also zeros cross-covariance.
u = delta / delta.norm()
orthogonal = X - torch.outer((X - X.mean(0)) @ u, u)
assert ((orthogonal - orthogonal.mean(0)).T @ (z - z.mean())).abs().max() < 1e-8
leace_loss = (X - erased).square().sum(1).mean()
assert leace_loss <= (X - orthogonal).square().sum(1).mean() + TOL
def test_exact_extractor_labels_are_diagnostic_with_closed_form_separation():
X, labels = planted(seed=3)
result = ExactLEACEExtractor().extract(list(X[labels == 1]), list(X[labels == 0]))
centered = X - X.mean(0)
delta = X[labels == 1].mean(0) - X[labels == 0].mean(0)
mahalanobis = delta @ torch.linalg.pinv(centered.T @ centered / len(X)) @ delta
assert result.generalized_eigenvalue == pytest.approx(mahalanobis.item(), rel=1e-8)
assert set(result.measurement_evidence) == {"direction", "generalized_eigenvalue", "erasure_loss", "eraser"}
assert all(e.status == "diagnostic" for e in result.measurement_evidence.values())
assert "no LEACE guarantee" in result.measurement_evidence["eraser"].scope
@pytest.mark.parametrize("n, d", [(2, 3), (6, 10)])
def test_rank_deficient_and_minimal_samples_still_equalize_class_means(n, d):
X, labels = planted(n=n, d=4, seed=n)
X = torch.cat([X, torch.full((n, d - 4), 7.0, dtype=X.dtype)], 1) if d > 4 else X[:, :d]
erased = fit_leace_eraser(X, labels).apply(X)
gap = erased[labels == 1].mean(0) - erased[labels == 0].mean(0)
assert gap.abs().max() < 1e-8
assert torch.equal(erased[:, 4:], X[:, 4:]) # constant features untouched
def samples():
generator = torch.Generator().manual_seed(11)
base = lambda n: torch.randn(n, 4, generator=generator, dtype=torch.float64) # noqa: E731
positive = base(30) * torch.tensor([2.0, 1.0, 0.5, 1.0], dtype=torch.float64) + torch.tensor([2.0, 0, 0, 0], dtype=torch.float64)
controls = {"a": base(20) + torch.tensor([0, 1.5, 0, 0], dtype=torch.float64),
"b": base(40) * 0.5 + torch.tensor([0, 0, -1.0, 0], dtype=torch.float64)}
return positive, controls
def test_multiclass_leace_has_zero_weighted_cross_covariance_and_preserves_complement():
positive, controls = samples()
result = extract_layer(positive, controls, ExtractionRecipe(method="leace_exact", n_directions=2))
projector = result.projector_left @ result.projector_right.T
groups = [positive, *controls.values()]
weights = torch.cat([g.new_full((len(g),), (0.5 if k == 0 else 0.25) / len(g)) for k, g in enumerate(groups)])
X = torch.cat(groups)
one_hot = torch.block_diag(*[g.new_ones(len(g), 1) for g in groups])
erased = X - (X - result.reference_mean) @ projector.T
mean = weights @ erased
cross = (erased - mean).T @ (weights.unsqueeze(1) * (one_hot - weights @ one_hot))
assert cross.abs().max() < TOL
predictions = probe_predictions(erased, one_hot, weights)
assert (predictions - weights @ one_hot).abs().max() < 1e-8
assert (probe_predictions(X, one_hot, weights) - weights @ one_hot).abs().max() > 0.1
means = torch.stack([g.mean(0) for g in groups])
contrasts = means - torch.tensor([0.5, 0.25, 0.25], dtype=X.dtype) @ means
complement = torch.linalg.svd(contrasts).Vh[2:]
torch.testing.assert_close(erased @ complement.T, X @ complement.T, atol=TOL, rtol=0)
def test_fisher_projection_is_not_linear_erasure_and_loss_is_centered_variance():
X, labels = planted(seed=5)
harmful, harmless = X[labels == 1].float(), X[labels == 0].float()
result = LEACEExtractor().extract(list(harmful), list(harmless))
v = result.direction
delta = harmful.mean(0) - harmless.mean(0)
projected_gap = delta - (delta @ v) * v
assert projected_gap.norm() > 0.1 * delta.norm() # orthogonal FLD projection leaves signal
pooled = torch.cat([harmful, harmless])
centered = pooled - pooled.mean(0)
change = ((centered @ v) ** 2).mean()
n = len(pooled)
assert result.erasure_loss == pytest.approx((change * n / (n - 1)).item(), rel=1e-4)
evidence = result.measurement_evidence
assert "Not a LEACE erasure loss" in evidence["erasure_loss"].definition
assert all(e.status == "diagnostic" for e in evidence.values())
def test_fisher_unavailable_markers_are_labelled(monkeypatch):
X, labels = planted(seed=6)
harmful, harmless = list(X[labels == 1].float()), list(X[labels == 0].float())
result = LEACEExtractor().extract(harmful, harmless)
degenerate = LEACEExtractor.compare_with_diff_of_means(result, torch.zeros(4), torch.zeros(4))
assert degenerate["cosine_similarity"] == 0 and degenerate["cosine_similarity_available"] is False
regular = LEACEExtractor.compare_with_diff_of_means(result, X[labels == 1].mean(0).float(), X[labels == 0].mean(0).float())
assert regular["cosine_similarity_available"] is True
def fail(*_args, **_kwargs):
raise RuntimeError("fixture")
monkeypatch.setattr(torch.linalg, "eigvalsh", fail)
failed = LEACEExtractor().extract(harmful, harmless)
assert failed.within_class_condition == float("inf")
assert failed.measurement_evidence["within_class_condition"].status == "unavailable"
assert failed.measurement_evidence["within_class_condition"].unavailable_reason