forked from ChelseaKR/sprout
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreport.py
More file actions
294 lines (263 loc) · 11.4 KB
/
Copy pathreport.py
File metadata and controls
294 lines (263 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
"""Render the eval run as committed artifacts — and self-check the HTML for a11y.
Every artifact is a pure function of the :class:`RunResult` (no wall-clock), so identical
inputs produce byte-identical files. The Markdown report is the human-facing scoreboard; the
HTML report is structurally accessibility-checked before it is written (we never emit an
inaccessible accessibility tool); JUnit and SARIF let any CI annotate failures; the model
card states limits plainly. Failures are shown, never hidden.
"""
from __future__ import annotations
import html
import json
import xml.sax.saxutils as sax
from pathlib import Path
from ..a11y import assert_accessible
from .runner import RunResult
from .suite import SuiteResult, Verdict
_DISCLAIMER = (
"This is a build artifact from a reference implementation over a synthetic, CC0 corpus. "
"A passing evaluation is NOT a blanket safety guarantee. This is not veterinary advice."
)
def _verdict_label(v: Verdict) -> str:
return "✅ PASS" if v is Verdict.PASS else "❌ FAIL"
# --- JSON (canonical) ------------------------------------------------------------
def render_json(result: RunResult) -> str:
return result.model_dump_json(indent=2)
# --- Markdown --------------------------------------------------------------------
def _suite_md(s: SuiteResult) -> str:
direction = "higher is better" if s.metric.higher_is_better else "lower is better"
lines = [
f"### `{s.suite}` — {_verdict_label(s.verdict)}",
"",
f"- **Metric:** {s.metric.name}",
f"- **Definition:** {s.metric.definition}",
f"- **Score:** {s.score:.3f} (threshold {s.metric.threshold:.3f}, {direction})",
f"- **95% CI (gated rate):** [{s.ci_low:.3f}, {s.ci_high:.3f}]"
+ (" ⚠️ under-powered (n<30)" if s.underpowered else ""),
f"- **Items evaluated:** {s.n_items}",
f"- **Judge:** {s.judge_method} (config `{s.judge_config_hash[:12]}`)",
]
if s.notes:
lines.append(f"- **Notes:** {s.notes}")
if s.segments:
lines += ["", "| Segment | Score | n | Verdict |", "|---|---|---|---|"]
lines += [
f"| {seg.label} | {seg.score:.3f} | {seg.n} | {_verdict_label(seg.verdict)} |"
for seg in s.segments
]
if s.failing_examples:
lines += ["", "<details><summary>Failing examples</summary>", ""]
lines += [f"- `{o.item_id}` (score {o.score:.2f}): {o.detail}" for o in s.failing_examples]
lines += ["", "</details>"]
return "\n".join(lines)
def render_markdown(result: RunResult) -> str:
fp = result.fingerprint
board = ["| Suite | Verdict | Score | Threshold | n |", "|---|---|---|---|---|"]
board += [
f"| `{s.suite}` | {_verdict_label(s.verdict)} | {s.score:.3f} "
f"| {s.metric.threshold:.3f} | {s.n_items} |"
for s in result.suite_results
]
suites = "\n\n".join(_suite_md(s) for s in result.suite_results)
return "\n".join(
[
"# Sprout Evaluation Report",
"",
f"**Overall verdict:** {_verdict_label(result.overall_verdict)}",
"",
"| | |",
"|---|---|",
f"| Run fingerprint | `{fp.digest[:16]}` |",
f"| Harness version | {fp.harness_version} |",
f"| Seed | {fp.seed} |",
f"| Dataset hash | `{fp.dataset_hash[:16]}` |",
f"| Judge config hash | `{fp.judge_config_hash[:12]}` |",
f"| Target (answer model) | {fp.target} |",
f"| Suites | {', '.join(fp.suite_names)} |",
"",
f"> {_DISCLAIMER}",
"",
"## Scoreboard",
"",
*board,
"",
"## Suites",
"",
suites,
"",
]
)
# --- HTML (self-a11y-checked) ----------------------------------------------------
def _suite_html(s: SuiteResult) -> str:
rows = "".join(
f"<tr><td>{html.escape(o.item_id)}</td><td>{o.score:.2f}</td>"
f"<td>{html.escape(o.detail)}</td></tr>"
for o in s.failing_examples
)
failing = (
f"<table><caption>Failing examples for {html.escape(s.suite)}</caption>"
f'<thead><tr><th scope="col">Item</th><th scope="col">Score</th>'
f'<th scope="col">Detail</th></tr></thead><tbody>{rows}</tbody></table>'
if s.failing_examples
else ""
)
return (
f'<section aria-labelledby="s-{html.escape(s.suite)}">'
f'<h3 id="s-{html.escape(s.suite)}">{html.escape(s.suite)}: '
f"{'PASS' if s.verdict is Verdict.PASS else 'FAIL'}</h3>"
f"<p>{html.escape(s.metric.definition)}</p>"
f"<p>Score {s.score:.3f} (threshold {s.metric.threshold:.3f}); "
f"95% CI [{s.ci_low:.3f}, {s.ci_high:.3f}]; n={s.n_items}.</p>{failing}</section>"
)
def render_html(result: RunResult) -> str:
fp = result.fingerprint
board = "".join(
f"<tr><td><code>{html.escape(s.suite)}</code></td>"
f"<td>{'PASS' if s.verdict is Verdict.PASS else 'FAIL'}</td>"
f"<td>{s.score:.3f}</td><td>{s.metric.threshold:.3f}</td><td>{s.n_items}</td></tr>"
for s in result.suite_results
)
suites = "".join(_suite_html(s) for s in result.suite_results)
overall = "PASS" if result.overall_verdict is Verdict.PASS else "FAIL"
doc = (
'<!doctype html><html lang="en"><head><meta charset="utf-8">'
'<meta name="viewport" content="width=device-width, initial-scale=1">'
"<title>Sprout Evaluation Report</title></head><body><main>"
"<h1>Sprout Evaluation Report</h1>"
f"<p><strong>Overall verdict: {overall}</strong></p>"
f"<p>Run <code>{fp.digest[:16]}</code> · harness {html.escape(fp.harness_version)} · "
f"seed {fp.seed} · judge <code>{html.escape(fp.judge_config_hash[:12])}</code> · "
f"target {html.escape(fp.target)}.</p>"
f"<p>{html.escape(_DISCLAIMER)}</p>"
"<h2>Scoreboard</h2>"
"<table><caption>Suite results</caption><thead><tr>"
'<th scope="col">Suite</th><th scope="col">Verdict</th><th scope="col">Score</th>'
'<th scope="col">Threshold</th><th scope="col">Items</th></tr></thead>'
f"<tbody>{board}</tbody></table>"
f"<h2>Suites</h2>{suites}"
"</main></body></html>"
)
assert_accessible(doc) # fail closed: never emit an inaccessible report
return doc
# --- JUnit -----------------------------------------------------------------------
def render_junit(result: RunResult) -> str:
cases = []
failures = sum(1 for s in result.suite_results if not s.passed)
for s in result.suite_results:
body = ""
if not s.passed:
msg = sax.quoteattr(f"score {s.score:.3f} vs threshold {s.metric.threshold:.3f}")
body = f"<failure message={msg}>{sax.escape(s.notes or s.metric.name)}</failure>"
cases.append(
f'<testcase classname="sprout.eval" name="{sax.escape(s.suite)}">{body}</testcase>'
)
return (
'<?xml version="1.0" encoding="UTF-8"?>\n'
f'<testsuites tests="{len(result.suite_results)}" failures="{failures}">'
f'<testsuite name="sprout-eval" tests="{len(result.suite_results)}" '
f'failures="{failures}">{"".join(cases)}</testsuite></testsuites>'
)
# --- SARIF -----------------------------------------------------------------------
def render_sarif(result: RunResult, dataset_path: str = "eval/suites") -> str:
rules = [
{
"id": f"sprout-eval/{s.suite}",
"name": s.metric.name,
"shortDescription": {"text": s.metric.definition},
}
for s in result.suite_results
]
results = [
{
"ruleId": f"sprout-eval/{s.suite}",
"level": "error",
"message": {
"text": f"{s.suite} scored {s.score:.3f} (threshold {s.metric.threshold:.3f})"
},
"locations": [{"physicalLocation": {"artifactLocation": {"uri": dataset_path}}}],
}
for s in result.suite_results
if not s.passed
]
sarif = {
"$schema": "https://json.schemastore.org/sarif-2.1.0.json",
"version": "2.1.0",
"runs": [
{
"tool": {
"driver": {
"name": "sprout-eval",
"version": result.fingerprint.harness_version,
"rules": rules,
}
},
"results": results,
}
],
}
return json.dumps(sarif, indent=2, sort_keys=True)
# --- model card scoreboard -------------------------------------------------------
def render_model_card_scoreboard(result: RunResult) -> str:
rows = "\n".join(
f"| `{s.suite}` | {_verdict_label(s.verdict)} | {s.score:.3f} "
f"| {s.metric.threshold:.3f} | {s.n_items} | {s.notes} |"
for s in result.suite_results
)
return "\n".join(
[
"| Suite | Verdict | Score | Threshold | n | Notes |",
"|---|---|---|---|---|---|",
rows,
]
)
# --- baseline diff ---------------------------------------------------------------
def diff_against_baseline(
current: RunResult, baseline: RunResult, *, tolerance: float = 0.05
) -> list[str]:
"""Flag fingerprint mismatches, verdict flips, score erosion, and dropped suites."""
issues: list[str] = []
cf, bf = current.fingerprint, baseline.fingerprint
for field in ("dataset_hash", "judge_config_hash", "seed", "target"):
if getattr(cf, field) != getattr(bf, field):
issues.append(f"comparability: {field} differs from baseline")
base = {s.suite: s for s in baseline.suite_results}
for s in current.suite_results:
b = base.get(s.suite)
if b is None:
continue
if b.passed and not s.passed:
issues.append(f"regression: suite `{s.suite}` flipped PASS->FAIL")
elif s.metric.higher_is_better and (b.score - s.score) > tolerance:
issues.append(
f"erosion: suite `{s.suite}` dropped {b.score:.3f}->{s.score:.3f} (> {tolerance})"
)
dropped = set(base) - {s.suite for s in current.suite_results}
issues += [f"dropped suite `{name}`" for name in sorted(dropped)]
return issues
def load_run_result(path: str | Path) -> RunResult:
return RunResult.model_validate_json(Path(path).read_text(encoding="utf-8"))
# --- writer ----------------------------------------------------------------------
_RENDERERS = {
"json": (render_json, "eval-report.json"),
"md": (render_markdown, "eval-report.md"),
"html": (render_html, "eval-report.html"),
"junit": (render_junit, "eval-report.junit.xml"),
"sarif": (render_sarif, "eval-report.sarif"),
}
def write_reports(
result: RunResult,
out_dir: str | Path,
formats: tuple[str, ...] = ("json", "md", "html", "junit", "sarif"),
) -> list[Path]:
out = Path(out_dir)
out.mkdir(parents=True, exist_ok=True)
# Always run the HTML self-a11y check, even if HTML is not among the requested
# formats, so "we never emit an inaccessible accessibility tool" is an unconditional
# invariant rather than a property of the default argument (render_html raises on fail).
render_html(result)
written: list[Path] = []
for fmt in formats:
renderer, filename = _RENDERERS[fmt]
path = out / filename
path.write_text(renderer(result), encoding="utf-8")
written.append(path)
return written