pptx-brand-linter / eval /run_eval.py
killbizz's picture
Initial deploy
5e05280 verified
Raw History Blame Contribute Delete
7.21 kB
"""Run the linter on Annotated_IDEXX and grade it against speaker-notes ground truth.
Output:
- Per-issue verdict (caught / missed)
- Tool flags not in ground truth (false positives or bonus catches)
- Precision and recall, both overall and broken down by category
- Coverage on the *catchable* subset (issues our architecture can in principle reach)
Run from project root:
python -m eval.run_eval
"""
from __future__ import annotations
import argparse
import json
import sys
from collections import defaultdict
from dataclasses import dataclass
from dotenv import load_dotenv
from checker.aggregate import Violation, aggregate
from checker.checks_cross_slide import run_cross_slide_checks
from checker.checks_per_slide import run_per_slide_checks
from checker.extract import extract_deck
from checker.profile import build_profile
from .ground_truth import GROUND_TRUTH, GroundTruthIssue, by_slide, catchable_issues
@dataclass
class IssueVerdict:
issue: GroundTruthIssue
caught: bool
matched_violation: Violation | None
def _run_pipeline(path: str, *, with_llm: bool) -> list[Violation]:
deck = extract_deck(path)
profile = build_profile(deck)
violations: list[Violation] = []
violations.extend(run_per_slide_checks(deck))
violations.extend(run_cross_slide_checks(deck, profile))
if with_llm:
from checker.agents_llm import run_llm_checks
violations.extend(run_llm_checks(deck))
return violations
def _match(
issues: list[GroundTruthIssue], violations: list[Violation]
) -> tuple[list[IssueVerdict], set[int]]:
"""For each ground-truth issue, find the first matching tool violation.
Returns (verdicts, used_violation_indices). A tool violation matches a
ground-truth issue when the slide and rule id agree.
"""
used: set[int] = set()
verdicts: list[IssueVerdict] = []
for issue in issues:
match: Violation | None = None
if issue.expected_rule:
for i, v in enumerate(violations):
if i in used:
continue
if v.slide == issue.slide and v.rule == issue.expected_rule:
match = v
used.add(i)
break
verdicts.append(IssueVerdict(issue=issue, caught=match is not None, matched_violation=match))
return verdicts, used
def _bonus_catches(violations: list[Violation], used: set[int]) -> list[Violation]:
return [v for i, v in enumerate(violations) if i not in used]
def _percent(n: int, d: int) -> str:
if d == 0:
return "n/a"
return f"{(100.0 * n / d):.0f}%"
def _print_section(title: str) -> None:
print(f"\n{'=' * 78}\n{title}\n{'=' * 78}")
def _format_issue(v: IssueVerdict) -> str:
icon = "βœ“" if v.caught else "βœ—"
rule = v.issue.expected_rule or "β€”"
catchable = " (uncatchable)" if not v.issue.catchable else ""
return f" {icon} slide {v.issue.slide:>2} [{v.issue.category:<11}] {v.issue.description} β†’ {rule}{catchable}"
def _format_violation(v: Violation) -> str:
src = {"deterministic": "det", "cross_slide": "cross", "llm": "llm"}.get(v.source, v.source)
return f" β€’ slide {v.slide:>2} [{src}] {v.rule} β€” {v.description[:80]}"
def main() -> int:
load_dotenv()
parser = argparse.ArgumentParser(description="Grade the linter on Annotated_IDEXX.")
parser.add_argument(
"--no-llm",
action="store_true",
help="Skip the LLM judgment pass (faster; lower recall on subjective rules).",
)
parser.add_argument(
"--deck",
default="data/IDEXX_annotated.pptx",
help="Path to the annotated deck. Defaults to data/IDEXX_annotated.pptx.",
)
parser.add_argument(
"--json",
action="store_true",
help="Emit a machine-readable JSON summary at the end.",
)
args = parser.parse_args()
print(f"Running pipeline on {args.deck} (LLM: {'off' if args.no_llm else 'on'})")
violations = _run_pipeline(args.deck, with_llm=not args.no_llm)
print(f"Tool produced {len(violations)} violation(s).")
verdicts, used = _match(GROUND_TRUTH, violations)
bonus = _bonus_catches(violations, used)
# Aggregate stats
total = len(GROUND_TRUTH)
caught = sum(1 for v in verdicts if v.caught)
catchable_total = sum(1 for v in verdicts if v.issue.catchable)
catchable_caught = sum(1 for v in verdicts if v.issue.catchable and v.caught)
bonus_count = len(bonus)
# Per-category breakdown (catchable only)
cat_total: dict[str, int] = defaultdict(int)
cat_caught: dict[str, int] = defaultdict(int)
for v in verdicts:
if not v.issue.catchable:
continue
cat_total[v.issue.category] += 1
if v.caught:
cat_caught[v.issue.category] += 1
# Per-issue listing, grouped by slide
_print_section("Per-issue verdict (grouped by slide)")
for slide in sorted({v.issue.slide for v in verdicts}):
slide_verdicts = [v for v in verdicts if v.issue.slide == slide]
for v in slide_verdicts:
print(_format_issue(v))
# Bonus catches
_print_section(f"Tool violations not in ground truth ({bonus_count})")
if not bonus:
print(" (none)")
else:
for v in bonus:
print(_format_violation(v))
print()
print(
" Note: many of these are real but unflagged-by-reviewer issues "
"(e.g. yellow 'To be updated' callouts on slides 12-14, off-palette "
"red callouts on slide 1/4, the #006CBD cell fill on slide 8). "
"The reviewer's notes are not exhaustive."
)
# Headline metrics
_print_section("Summary")
print(
f"Recall (raw): {caught}/{total} ground-truth issues caught "
f"({_percent(caught, total)})"
)
print(
f"Recall (catchable subset): {catchable_caught}/{catchable_total} "
f"({_percent(catchable_caught, catchable_total)})"
)
print(f"Bonus / unflagged catches: {bonus_count}")
print()
print("Recall by category (catchable issues only):")
for cat in sorted(cat_total.keys()):
print(
f" {cat:<13} {cat_caught[cat]}/{cat_total[cat]} "
f"({_percent(cat_caught[cat], cat_total[cat])})"
)
# Honest miss list
misses = [v for v in verdicts if not v.caught and v.issue.catchable]
if misses:
_print_section(f"Catchable misses to discuss in the Loom ({len(misses)})")
for v in misses:
print(_format_issue(v))
if args.json:
summary = {
"total_ground_truth": total,
"caught": caught,
"catchable_total": catchable_total,
"catchable_caught": catchable_caught,
"bonus_unflagged_catches": bonus_count,
"by_category": {
cat: {"caught": cat_caught[cat], "total": cat_total[cat]}
for cat in cat_total
},
"tool_violation_count": len(violations),
}
print()
print(json.dumps(summary, indent=2))
return 0
if __name__ == "__main__":
sys.exit(main())