Files
OBLITERATUS/tests/test_benchmark_plots.py
2026-10-17 13:39:48 -04:00

213 lines
7.6 KiB
Python

"""CPU-only contracts for benchmark visualization generation."""
from __future__ import annotations
import matplotlib.pyplot as plt
import numpy as np
import pytest
from obliteratus.evaluation import benchmark_plots as plots
@pytest.fixture
def results():
return [
{
"method": "advanced",
"model": "org/model-a",
"model_short": "model-a",
"perplexity": 10.0,
"refusal_rate": 0.1,
"coherence": 0.9,
"time_s": 2.0,
"strong_layers": 3,
"ega_expert_dirs": 2,
"cot_preserved": 1,
"expert_classified_layers": 4,
},
{
"method": "unknown/hf_secret123456",
"model": "org/model-b",
"perplexity": 12.0,
"refusal_rate": 0.2,
"coherence": 0.8,
"time_s": 3.0,
"strong_layers": 0,
"ega_expert_dirs": 0,
"cot_preserved": 0,
"ega_safety_layers": 0,
},
]
def test_label_sanitization_and_palette_fallback():
label = plots._sanitize_label(
"/private/path/model hf_secret123456 0123456789abcdef0123456789abcdef", max_len=40,
)
assert "/private/path" not in label
assert "hf_secret" not in label
assert label == "model <TOKEN> <REDACTED>"
assert plots._get_color("basic") == plots.PALETTE["basic"]
assert plots._get_color("custom", 9) == plots.MODEL_PALETTE[1]
def test_model_scaling_keeps_unavailable_refusal_as_a_gap(results):
results[0]["refusal_rate"] = None
results[1]["refusal_rate"] = 0.0
figure = plots.plot_model_scaling(results)
try:
refusal = figure.axes[0].lines[0].get_ydata()
assert np.isnan(refusal[0])
assert refusal[1] == 0.0
assert list(figure.axes[1].lines[0].get_ydata()) == [10.0, 12.0]
finally:
plt.close(figure)
@pytest.mark.parametrize(
"factory",
[
plots.plot_pareto_frontier,
plots.plot_method_radar,
plots.plot_metric_bars,
plots.plot_timing_efficiency,
plots.plot_moe_metrics,
plots.plot_model_scaling,
],
)
def test_each_plot_returns_a_figure_for_empty_input(factory):
figure = factory([])
assert isinstance(figure, plt.Figure)
plt.close(figure)
@pytest.mark.parametrize(
"factory",
[
plots.plot_pareto_frontier,
plots.plot_method_radar,
plots.plot_metric_bars,
plots.plot_timing_efficiency,
plots.plot_moe_metrics,
plots.plot_model_scaling,
],
)
def test_each_plot_renders_synthetic_results(factory, results):
figure = factory(results, " — fixture")
assert isinstance(figure, plt.Figure)
assert figure.axes
plt.close(figure)
def test_pareto_handles_single_point_without_frontier_line(results):
figure = plots.plot_pareto_frontier(results[:1])
assert isinstance(figure, plt.Figure)
plt.close(figure)
def test_pareto_and_quality_scores_penalize_invalid_completions(results):
results[0].update(refusal_rate=0.0, invalid_rate=1.0, invalid_count=10)
results[1].update(refusal_rate=0.0, invalid_rate=0.0, invalid_count=0)
figure = plots.plot_pareto_frontier(results)
assert figure.axes[0].collections[0].get_offsets()[0, 0] == 100.0
assert figure.axes[0].collections[1].get_offsets()[0, 0] == 0.0
plt.close(figure)
figure = plots.plot_metric_bars(results)
assert figure.axes[0].patches[0].get_height() == 0.0
assert figure.axes[0].patches[1].get_height() == 100.0
plt.close(figure)
def test_quality_scores_do_not_double_count_invalid_rate(results):
results[0].update(refusal_rate=0.25, invalid_rate=0.5, refusal_objective=0.75)
figure = plots.plot_metric_bars(results[:1])
assert figure.axes[0].patches[0].get_height() == 25.0
plt.close(figure)
def test_pareto_normalizes_historical_and_valid_only_rates(results):
results[0].update(refusal_rate=0.5, invalid_rate=0.5)
results[1].update(refusal_rate=0.8, invalid_rate=0.5, exclude_invalid=True)
figure = plots.plot_pareto_frontier(results)
try:
assert figure.axes[0].collections[0].get_offsets()[0, 0] == 100.0
assert figure.axes[0].collections[1].get_offsets()[0, 0] == 90.0
finally:
plt.close(figure)
@pytest.mark.parametrize("factory", [plots.plot_pareto_frontier, plots.plot_timing_efficiency])
@pytest.mark.parametrize("objective", [0.0, 1.0])
@pytest.mark.parametrize("include_legacy", [False, True])
def test_objective_scatter_keeps_runs_without_refusal_rate(factory, objective, include_legacy, results):
results[0].update(refusal_rate=None, refusal_objective=objective, invalid_rate=objective, coherence=0.0)
results[1].update(refusal_rate=0.0, invalid_rate=0.25, coherence=0.5)
rows = results if include_legacy else results[:1]
figure = factory(rows)
try:
ax = figure.axes[0]
offsets = [collection.get_offsets()[0].tolist() for collection in ax.collections]
if factory is plots.plot_pareto_frontier:
expected = [[objective * 100, 10.0], [25.0, 12.0]]
else:
expected = [[2.0, (1.0 - objective) * 60], [3.0, 65.0]]
np.testing.assert_allclose(offsets, expected if include_legacy else expected[:1])
assert "No valid data points" not in [text.get_text() for text in ax.texts]
finally:
plt.close(figure)
@pytest.mark.parametrize(
"factory, coordinate",
[(plots.plot_pareto_frontier, "perplexity"), (plots.plot_timing_efficiency, "time_s")],
)
@pytest.mark.parametrize("missing", ["coordinate", "refusal"])
def test_objective_scatter_still_requires_both_metrics(factory, coordinate, missing, results):
results[0].update(refusal_rate=None, refusal_objective=1.0)
results[0][coordinate if missing == "coordinate" else "refusal_objective"] = None
figure = factory(results[:1])
try:
assert not figure.axes[0].collections
assert "No valid data points" in [text.get_text() for text in figure.axes[0].texts]
finally:
plt.close(figure)
@pytest.mark.parametrize("include_measured", [False, True])
@pytest.mark.parametrize("omit_perplexity", [False, True])
def test_metric_bars_keep_objective_without_perplexity(include_measured, omit_perplexity, results):
results[0].update(refusal_rate=None, refusal_objective=1.0, invalid_rate=1.0, perplexity=None)
if omit_perplexity:
results[0].pop("perplexity")
rows = results if include_measured else results[:1]
figure = plots.plot_metric_bars(rows)
try:
ax = figure.axes[0]
assert len(ax.containers[0]) == len(rows)
assert ax.containers[0][0].get_height() == 0.0
assert np.isnan(ax.containers[2][0].get_height())
assert [label.get_text() for label in ax.get_xticklabels()][0] == "advanced"
if include_measured:
assert ax.containers[0][1].get_height() == 80.0
assert ax.containers[2][1].get_height() == 0.0
finally:
plt.close(figure)
def test_moe_plot_handles_non_moe_results(results):
figure = plots.plot_moe_metrics(results[1:])
assert "No MoE-specific features" in figure.axes[0].texts[0].get_text()
plt.close(figure)
def test_dashboard_modes_and_optional_moe_panel(results):
assert plots.generate_benchmark_dashboard([]) == []
method_figures = plots.generate_benchmark_dashboard(results, mode="multi_method")
model_figures = plots.generate_benchmark_dashboard(results, mode="multi_model")
assert len(method_figures) == 5
assert len(model_figures) == 5
assert plots.generate_benchmark_dashboard(results, mode="unsupported") == []
for figure in method_figures + model_figures:
plt.close(figure)