Show reference counts per case and per set in the eval report
CI / checks (pull_request) Successful in 1m4s
CI / checks (pull_request) Successful in 1m4s
Each reference check prints precision and recall with counts, missing and surplus entries; sum_matches adds the counts per label after the summary. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
9a7dc6ceb3
commit
857a1818d5
2 files changed
+152
-3
No files matched your search
@@ -1,7 +1,14 @@
|
||||
"""Run an eval set and format the report."""
|
||||
|
||||
from chemenu.evals.graders import score_case
|
||||
from chemenu.evals.model import CaseResult, EvalSet, ResponseError, ResponseSource, SetResult
|
||||
from chemenu.evals.model import (
|
||||
CaseResult,
|
||||
EvalSet,
|
||||
ResponseError,
|
||||
ResponseSource,
|
||||
SetMatch,
|
||||
SetResult,
|
||||
)
|
||||
|
||||
|
||||
def run_eval_set(eval_set: EvalSet, responses: ResponseSource) -> SetResult:
|
||||
@@ -26,8 +33,55 @@ def _reason(case: CaseResult) -> str:
|
||||
return " ".join(reason.split())
|
||||
|
||||
|
||||
def sum_matches(result: SetResult) -> tuple[tuple[str, SetMatch], ...]:
|
||||
"""Add the counts of reference checks per label, in order of first appearance."""
|
||||
totals: dict[str, SetMatch] = {}
|
||||
for case in result.cases:
|
||||
for check in case.checks:
|
||||
match = check.match
|
||||
if match is None:
|
||||
continue
|
||||
prev = totals.get(check.label)
|
||||
if prev is not None:
|
||||
match = SetMatch(
|
||||
prev.matched + match.matched,
|
||||
prev.actual + match.actual,
|
||||
prev.expected + match.expected,
|
||||
)
|
||||
else:
|
||||
match = SetMatch(match.matched, match.actual, match.expected)
|
||||
totals[check.label] = match
|
||||
return tuple(totals.items())
|
||||
|
||||
|
||||
def _counted_cases(result: SetResult) -> dict[str, int]:
|
||||
"""Number of cases with counts per label; a case without counts makes a sum incomplete."""
|
||||
counted: dict[str, int] = {}
|
||||
for case in result.cases:
|
||||
for label in {c.label for c in case.checks if c.match is not None}:
|
||||
counted[label] = counted.get(label, 0) + 1
|
||||
return counted
|
||||
|
||||
|
||||
def _ratio(value: float | None) -> str:
|
||||
return "n/a" if value is None else f"{value:.2f}"
|
||||
|
||||
|
||||
def _match_line(label: str, match: SetMatch, *, details: bool) -> str:
|
||||
line = (
|
||||
f" {label} precision {_ratio(match.precision)} ({match.matched}/{match.actual})"
|
||||
f" recall {_ratio(match.recall)} ({match.matched}/{match.expected})"
|
||||
)
|
||||
if details:
|
||||
if match.missing:
|
||||
line += f" missing: {list(match.missing)!r}"
|
||||
if match.surplus:
|
||||
line += f" surplus: {list(match.surplus)!r}"
|
||||
return line
|
||||
|
||||
|
||||
def format_report(result: SetResult) -> str:
|
||||
"""One line per case, then a summary line."""
|
||||
"""One line per case, then a summary line; reference counts indented below each."""
|
||||
lines: list[str] = []
|
||||
for case in result.cases:
|
||||
line = f"{'PASS' if case.passed else 'FAIL'} {case.case_id} {case.score:.2f}"
|
||||
@@ -35,9 +89,18 @@ def format_report(result: SetResult) -> str:
|
||||
if reason:
|
||||
line += f" {reason}"
|
||||
lines.append(line)
|
||||
lines.extend(
|
||||
_match_line(c.label, c.match, details=True) for c in case.checks if c.match is not None
|
||||
)
|
||||
passed = sum(case.passed for case in result.cases)
|
||||
lines.append(
|
||||
f"{result.name}: {passed}/{len(result.cases)} passed, "
|
||||
f"mean {result.mean_score:.2f}, threshold {result.threshold}"
|
||||
)
|
||||
counted = _counted_cases(result)
|
||||
for label, total in sum_matches(result):
|
||||
line = _match_line(label, total, details=False)
|
||||
if counted[label] < len(result.cases):
|
||||
line += f" counted in {counted[label]}/{len(result.cases)} cases"
|
||||
lines.append(line)
|
||||
return "\n".join(lines)
|
||||
Reference in new issue
Block a user