Files
OBLITERATUS/tests/test_report.py
Joseph Magly 2a0abae1f8 fix(viewer,evaluator,presets): close #661/#663 review follow-ups
The results viewer formats -0 and non-finite values like Python, survives
engines without toFixed(100), and shows an error when readAsText throws. CSV
export writes an empty metric name as `metric:` and orders columns by name
across all rows. StudyConfig rejects `metrics: []` at parse. The evaluator logs
the position_ids fallback and matches more rejection wordings, runs in eval
mode (restoring module modes afterwards), and handles left padding in
classification. Preset getters return deep copies.

Fixes #666
2026-11-25 09:59:06 -05:00

430 lines
18 KiB
Python

"""Tests for the reporting module."""
from __future__ import annotations
import json
from obliteratus.reporting.report import (
REPORT_SCHEMA_VERSION,
AblationReport,
AblationResult,
)
def _make_report() -> AblationReport:
report = AblationReport(model_name="test-model")
report.add_baseline({"perplexity": 25.0, "accuracy": 0.85})
report.add_result(
AblationResult(
strategy="layer_removal",
component="layer_0",
description="Remove layer 0",
metrics={"perplexity": 30.0, "accuracy": 0.80},
)
)
report.add_result(
AblationResult(
strategy="layer_removal",
component="layer_1",
description="Remove layer 1",
metrics={"perplexity": 50.0, "accuracy": 0.60},
)
)
return report
class TestAblationReport:
def test_to_dataframe(self):
report = _make_report()
df = report.to_dataframe()
assert len(df) == 2
assert "perplexity" in df.columns
assert "delta:perplexity" in df.columns
assert "pct_change:perplexity" in df.columns
def test_save_json(self, tmp_path):
report = _make_report()
out = tmp_path / "results.json"
report.save_json(out)
data = json.loads(out.read_text())
assert data["model_name"] == "test-model"
assert len(data["results"]) == 2
assert data["baseline_metrics"]["perplexity"] == 25.0
assert data["schema_version"] == REPORT_SCHEMA_VERSION
def test_save_csv(self, tmp_path):
report = _make_report()
out = tmp_path / "results.csv"
report.save_csv(out)
text = out.read_text()
assert "layer_0" in text
assert "perplexity" in text
def test_delta_calculation(self):
report = _make_report()
df = report.to_dataframe()
row0 = df[df["component"] == "layer_0"].iloc[0]
assert row0["delta:perplexity"] == 5.0 # 30 - 25
assert abs(row0["pct_change:perplexity"] - 20.0) < 0.01
def test_plot_impact(self, tmp_path):
report = _make_report()
out = tmp_path / "impact.png"
report.plot_impact(metric="perplexity", output_path=out)
assert out.exists()
assert out.stat().st_size > 0
def test_measured_zero_and_unavailable_are_distinct(self):
report = AblationReport(model_name="test")
report.add_baseline({"refusal_rate": 0.0, "perplexity": None})
report.add_result(AblationResult(
strategy="advanced",
component="all",
description="partial evaluation",
metrics={"refusal_rate": 0.0, "perplexity": None},
))
data = report.to_dict()
assert data["baseline_metric_status"] == {
"perplexity": "unavailable", "refusal_rate": "measured",
}
assert data["results"][0]["metric_status"] == {
"perplexity": "unavailable", "refusal_rate": "measured",
}
assert data["results"][0]["metrics"]["refusal_rate"] == 0.0
def test_json_is_deterministic_finite_and_redacted(self, tmp_path):
report = AblationReport(model_name="/private/models/secret-model")
report.add_baseline({"score": float("nan")})
report.add_result(AblationResult(
strategy="/private/run/advanced",
component="layer_0",
description="artifact /private/run/output hf_abcdefghijkl",
metrics={"score": float("inf")},
metadata={
"token": "hf_abcdefghijkl",
"artifact": "/private/run/checkpoint.bin",
},
))
first = tmp_path / "first.json"
second = tmp_path / "second.json"
report.save_json(first)
report.save_json(second)
assert first.read_bytes() == second.read_bytes()
text = first.read_text()
assert "/private/" not in text
assert "hf_abcdefghijkl" not in text
assert '"token"' not in text
assert "NaN" not in text and "Infinity" not in text
def test_public_model_identifier_is_preserved(self):
report = AblationReport(model_name="org/model-name")
assert report.to_dict()["model_name"] == "org/model-name"
def _classifier_report(baseline, measured, primary_metric=None) -> AblationReport:
report = AblationReport(model_name="classifier", primary_metric=primary_metric)
report.add_baseline(baseline)
report.add_result(AblationResult("layer_removal", "layer_0", "Remove layer 0", measured))
return report
class TestPlotOutputs:
def test_primary_metric_is_saved_only_when_chosen(self, tmp_path):
"""#606: the study's impact metric survives the key-sorted JSON."""
chosen = _classifier_report(
{"f1": 1.0, "accuracy": 1.0}, {"f1": 0.73, "accuracy": 0.75}, primary_metric="f1",
)
out = tmp_path / "results.json"
chosen.save_json(out)
data = json.loads(out.read_text())
assert list(data["baseline_metrics"]) == ["accuracy", "f1"]
assert data["primary_metric"] == "f1"
assert "primary_metric" not in _make_report().to_dict()
def test_save_plots_uses_primary_metric(self, tmp_path):
report = _classifier_report(
{"accuracy": 1.0, "f1": 1.0}, {"accuracy": 0.75, "f1": 0.73}, primary_metric="f1",
)
outcome = report.save_plots(tmp_path)
assert outcome.impact_metric == "f1"
assert outcome.written == ["impact.png", "heatmap.png"]
def test_unavailable_impact_metric_still_writes_heatmap(self, tmp_path):
"""#607: one unmeasured metric must not suppress every plot."""
report = _classifier_report(
{"accuracy": 0.75, "f1": 0.8}, {"accuracy": None, "f1": 0.6}, primary_metric="accuracy",
)
outcome = report.save_plots(tmp_path)
assert (tmp_path / "heatmap.png").stat().st_size > 0
assert (tmp_path / "impact.png").stat().st_size > 0
assert outcome.impact_metric == "f1"
assert outcome.written == ["impact.png", "heatmap.png"]
assert any("accuracy" in note and "f1" in note for note in outcome.notes)
def test_no_measured_delta_writes_no_plots_without_raising(self, tmp_path):
report = _classifier_report({"score": None}, {"score": 1.0})
outcome = report.save_plots(tmp_path)
assert outcome.written == []
assert outcome.impact_metric is None
assert list(tmp_path.iterdir()) == []
def test_skipped_heatmap_is_not_reported_and_stale_file_is_flagged(self, tmp_path):
"""#604: only files written by this call are reported; old ones are flagged."""
stale = tmp_path / "heatmap.png"
stale.write_bytes(b"previous run")
report = _classifier_report({"accuracy": 0.0}, {"accuracy": 0.5})
outcome = report.save_plots(tmp_path)
assert outcome.written == ["impact.png"]
assert outcome.stale == ["heatmap.png"]
assert stale.read_bytes() == b"previous run"
text = "\n".join(outcome.lines())
assert "impact.png" in text
assert "percent change is undefined" in text
assert "heatmap.png" in text and "earlier run" in text
def test_plot_heatmap_reports_whether_it_wrote(self, tmp_path):
assert _make_report().plot_heatmap(tmp_path / "a.png") is True
zero = _classifier_report({"accuracy": 0.0}, {"accuracy": 0.5})
assert zero.plot_heatmap(tmp_path / "b.png") is False
assert not (tmp_path / "b.png").exists()
def _figures_open() -> set[int]:
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
return set(plt.get_fignums())
def _tmp_leftovers(directory) -> list[str]:
return sorted(p.name for p in directory.iterdir() if p.name not in {"impact.png", "heatmap.png"})
class TestPlotFailures:
"""#618 review: failed plots must not leak figures or corrupt earlier plots."""
def test_drawing_failure_closes_the_figure(self, tmp_path, monkeypatch):
import seaborn
def boom(*args, **kwargs):
raise RuntimeError("draw failed")
monkeypatch.setattr(seaborn, "barplot", boom)
monkeypatch.setattr(seaborn, "heatmap", boom)
before = _figures_open()
outcome = _make_report().save_plots(tmp_path)
assert _figures_open() == before
assert outcome.written == []
assert "impact.png not written: draw failed" in outcome.notes
assert "heatmap.png not written: draw failed" in outcome.notes
assert list(tmp_path.iterdir()) == []
def test_partial_write_keeps_the_earlier_plot_intact(self, tmp_path, monkeypatch):
from matplotlib.figure import Figure
(tmp_path / "impact.png").write_bytes(b"impact from earlier run")
(tmp_path / "heatmap.png").write_bytes(b"heatmap from earlier run")
def truncating_savefig(self, fname, *args, **kwargs):
with open(fname, "wb") as handle:
handle.write(b"\x89PNG partial")
raise OSError("disk full")
monkeypatch.setattr(Figure, "savefig", truncating_savefig)
before = _figures_open()
outcome = _make_report().save_plots(tmp_path)
assert _figures_open() == before
assert (tmp_path / "impact.png").read_bytes() == b"impact from earlier run"
assert (tmp_path / "heatmap.png").read_bytes() == b"heatmap from earlier run"
assert _tmp_leftovers(tmp_path) == []
assert outcome.written == []
assert outcome.stale == ["impact.png", "heatmap.png"]
text = "\n".join(outcome.lines())
assert "impact.png in" in text and "earlier run and was not updated" in text
assert "disk full" in text
def test_successful_plot_replaces_an_existing_file(self, tmp_path):
(tmp_path / "impact.png").write_bytes(b"old")
(tmp_path / "heatmap.png").write_bytes(b"old")
outcome = _make_report().save_plots(tmp_path)
assert outcome.written == ["impact.png", "heatmap.png"]
assert outcome.stale == []
for name in ("impact.png", "heatmap.png"):
assert (tmp_path / name).read_bytes().startswith(b"\x89PNG\r\n\x1a\n")
assert _tmp_leftovers(tmp_path) == []
def test_direct_plot_call_writes_png_and_closes(self, tmp_path):
before = _figures_open()
_make_report().plot_impact(metric="accuracy", output_path=tmp_path / "chart.png")
assert (tmp_path / "chart.png").read_bytes().startswith(b"\x89PNG")
assert _figures_open() == before
assert sorted(p.name for p in tmp_path.iterdir()) == ["chart.png"]
def test_fallback_metric_is_labelled_on_the_chart(self, tmp_path, monkeypatch):
from matplotlib.figure import Figure
titles: list[str] = []
real_savefig = Figure.savefig
def recording_savefig(self, *args, **kwargs):
titles.append(self.axes[0].get_title())
return real_savefig(self, *args, **kwargs)
monkeypatch.setattr(Figure, "savefig", recording_savefig)
report = _classifier_report(
{"accuracy": 0.75, "f1": 0.8}, {"accuracy": None, "f1": 0.6}, primary_metric="accuracy",
)
report.save_plots(tmp_path)
impact_title = titles[0]
assert "f1" in impact_title
assert "accuracy" in impact_title and "no measured change" in impact_title
class TestGeneratedColumnCollisions:
"""#618 r3: metric names like ``a_delta`` must not be read as generated columns."""
@staticmethod
def _capture(monkeypatch, name):
import seaborn
calls = []
real = getattr(seaborn, name)
def spy(*args, **kwargs):
calls.append((args, kwargs))
return real(*args, **kwargs)
monkeypatch.setattr(seaborn, name, spy)
return calls
def test_delta_suffix_metric_is_not_another_metrics_change(self, tmp_path, monkeypatch):
calls = self._capture(monkeypatch, "barplot")
report = _classifier_report({"a": 1.0, "a_delta": 1.0}, {"a": None, "a_delta": 3.0},
primary_metric="a")
outcome = report.save_plots(tmp_path)
assert outcome.impact_metric == "a_delta"
(_, kwargs), = calls
assert kwargs["data"][kwargs["x"]].tolist() == [2.0]
def test_metric_named_like_a_delta_column_keeps_its_own_change(self, tmp_path, monkeypatch):
calls = self._capture(monkeypatch, "barplot")
report = _classifier_report({"x": 1.0, "x_delta": 5.0}, {"x": 2.0, "x_delta": None})
outcome = report.save_plots(tmp_path)
assert outcome.impact_metric == "x"
(_, kwargs), = calls
assert kwargs["data"][kwargs["x"]].tolist() == [1.0]
def test_heatmap_uses_real_percent_changes(self, tmp_path, monkeypatch):
calls = self._capture(monkeypatch, "heatmap")
report = _classifier_report({"a": 1.0, "a_pct_change": 10.0}, {"a": None, "a_pct_change": 20.0})
assert report.plot_heatmap(tmp_path / "h.png") is True
(args, _), = calls
pivot = args[0]
assert list(pivot.columns) == ["a_pct_change"]
assert pivot.to_numpy().tolist() == [[100.0]]
def test_summary_shows_real_deltas(self, capsys, monkeypatch):
monkeypatch.setenv("COLUMNS", "200")
_classifier_report({"a": 1.0, "a_delta": 1.0}, {"a": None, "a_delta": 3.0}).print_summary()
out = capsys.readouterr().out
assert "+2.0000" in out
assert "+3.0000" not in out
class TestCsvColumnLayout:
"""#648: generated columns are namespaced, so no metric name can overwrite one."""
COLLIDING = {
"a": (1.0, 3.0),
"a_delta": (1.0, 5.0), # the pre-#648 generated name for "a"
"a_pct_change": (2.0, 2.0),
"delta:a": (7.0, 8.0), # the namespaced generated name for "a"
"pct_change:a": (1.0, 1.0),
"metric:delta:a": (4.0, 6.0), # the escaped name of the metric "delta:a"
"strategy": (2.0, 4.0), # a fixed column name
"description": (None, 9.0),
}
def _read(self, tmp_path):
import csv
report = _classifier_report(
{name: base for name, (base, _) in self.COLLIDING.items()},
{name: value for name, (_, value) in self.COLLIDING.items()},
)
out = tmp_path / "results.csv"
report.save_csv(out)
with out.open(newline="", encoding="utf-8") as stream:
header = next(csv.reader(stream))
stream.seek(0)
(row,) = list(csv.DictReader(stream))
return header, row
def test_colliding_metric_names_keep_their_own_values(self, tmp_path):
header, row = self._read(tmp_path)
assert len(header) == len(set(header)), header
# Fixed columns keep their labels.
assert (row["strategy"], row["component"], row["description"]) == (
"layer_removal", "layer_0", "Remove layer 0")
# Raw metrics: bare names, unless reserved (fixed column or namespace prefix).
assert row["a"] == "3.0"
assert row["a_delta"] == "5.0"
assert row["a_pct_change"] == "2.0"
assert row["metric:delta:a"] == "8.0"
assert row["metric:pct_change:a"] == "1.0"
assert row["metric:metric:delta:a"] == "6.0"
assert row["metric:strategy"] == "4.0"
assert row["metric:description"] == "9.0"
# Generated changes for each metric, keyed by its real name.
assert row["delta:a"] == "2.0" and row["pct_change:a"] == "200.0"
assert row["delta:a_delta"] == "4.0" and row["pct_change:a_delta"] == "400.0"
assert row["delta:delta:a"] == "1.0"
assert row["delta:metric:delta:a"] == "2.0"
assert row["delta:strategy"] == "2.0" and row["pct_change:strategy"] == "100.0"
assert "delta:description" not in row # no measured baseline
def test_every_value_has_its_own_column(self, tmp_path):
header, _ = self._read(tmp_path)
measured = [name for name, (base, value) in self.COLLIDING.items()
if base is not None and value is not None]
nonzero = [name for name in measured if self.COLLIDING[name][0] != 0]
assert len(header) == 3 + len(self.COLLIDING) + len(measured) + len(nonzero)
def test_column_order_is_fixed_then_per_metric_by_name(self):
report = _classifier_report({"b": 1.0, "a": 2.0}, {"b": 2.0, "a": 4.0})
assert list(report.to_dataframe().columns) == [
"strategy", "component", "description",
"a", "delta:a", "pct_change:a", "b", "delta:b", "pct_change:b",
]
def test_column_order_is_by_name_across_rows(self):
"""#666: a metric first seen in a later row is still placed by name."""
report = AblationReport(model_name="m")
report.add_baseline({"a": 1.0, "b": 1.0, "c": 0.0})
report.add_result(AblationResult("s", "r0", "d", {"c": 1.0}))
report.add_result(AblationResult("s", "r1", "d", {"b": 2.0, "a": None}))
assert list(report.to_dataframe().columns) == [
"strategy", "component", "description",
"a", "b", "delta:b", "pct_change:b", "c", "delta:c",
]
def test_empty_metric_name_is_written_as_reserved_column(self, tmp_path):
"""#666: a metric named "" gets the column ``metric:``, never a blank header."""
report = _classifier_report({"": 1.0, "metric:": 2.0}, {"": 3.0, "metric:": 5.0})
columns = list(report.to_dataframe().columns)
assert "" not in columns
assert columns[3:] == [
"metric:", "delta:", "pct_change:",
"metric:metric:", "delta:metric:", "pct_change:metric:",
]
out = tmp_path / "results.csv"
report.save_csv(out)
import pandas as pd
read_back = pd.read_csv(out)
assert read_back.loc[0, "metric:"] == 3.0
assert read_back.loc[0, "metric:metric:"] == 5.0
assert not any(str(c).startswith("Unnamed") for c in read_back.columns)