Files
OBLITERATUS/tests/test_bayesian_projection_boundaries.py

52 lines
2.3 KiB
Python

"""Projection proxy rejects invalid objectives and unsupported geometry."""
from dataclasses import replace
import pytest
import torch
from obliteratus.analysis.bayesian_kernel_projection import BayesianKernelProjection, ProjectionConfig
from tests.test_bayesian_activation_contracts import evidence
pytestmark = pytest.mark.cpu
@pytest.mark.parametrize("refusal,distortion,message", [
(-.1, .4, "finite and nonnegative"),
(.6, float("nan"), "finite and nonnegative"),
(float("inf"), .4, "finite and nonnegative"),
(0., 0., "at least one objective weight"),
])
def test_invalid_objective_weights_are_rejected(refusal, distortion, message):
with pytest.raises(ValueError, match=message):
BayesianKernelProjection(refusal_weight=refusal, distortion_weight=distortion)
@pytest.mark.parametrize("count", [0, -1, True, 1.5])
def test_invalid_direction_budget_is_rejected_before_empty_evidence_shortcut(count):
with pytest.raises(ValueError, match="max_directions must be a positive integer"):
BayesianKernelProjection(n_trials=1).optimize({}, {}, {}, max_directions=count)
@pytest.mark.parametrize("changes,message", [
({"n_directions": 0}, "positive integer"),
({"n_directions": True}, "positive integer"),
({"n_directions": 3}, "fewer supplied directions than requested"),
({"regularization": -.1}, "regularization"),
({"regularization": 1.1}, "regularization"),
({"regularization": float("nan")}, "regularization"),
({"per_layer_weights": {0: -.1}}, "projection weights must be between zero and one"),
({"per_layer_weights": {0: 1.1}}, "projection weights must be between zero and one"),
({"per_layer_weights": {0: float("inf")}}, "projection weights must be between zero and one"),
])
def test_invalid_trial_is_rejected_without_mutating_retained_samples(changes, message):
optimizer = BayesianKernelProjection(n_trials=1)
data = evidence()
stats = optimizer._precompute_stats(*data, [0])
original = {name: value.clone() for name, value in stats[0].items() if isinstance(value, torch.Tensor)}
config = replace(ProjectionConfig((0, 0), {0: .5}, 1, .2, False), **changes)
with pytest.raises(ValueError, match=message):
optimizer._evaluate_trial(config, stats, [0], 0)
for name, value in original.items():
torch.testing.assert_close(stats[0][name], value, rtol=0, atol=0)