Datasets:
Formats:
parquet
Languages:
English
Size:
< 1K
Tags:
video-language-model
egocentric-video
laboratory
wet-lab
procedural-monitoring
error-detection
License:
| """Markdown report generation for standardized benchmark outputs.""" | |
| from __future__ import annotations | |
| import csv | |
| from pathlib import Path | |
| from typing import Any | |
| from .io import OutputLayout, load_prediction_rows, read_json, write_json | |
| from .plots import TASK_LABELS, TASK_METRICS, write_standard_plots | |
| from .tasks import TASK_REGISTRY | |
| def _model_key(output_dir: Path) -> str: | |
| config_path = output_dir / "run_config.json" | |
| if config_path.exists(): | |
| config = read_json(config_path) | |
| return str(config.get("model") or config.get("model_path") or output_dir.name) | |
| return output_dir.name | |
| def _model_label(output_dir: Path) -> str: | |
| key = _model_key(output_dir) | |
| return output_dir.name if output_dir.name not in {"output", "outputs"} else key | |
| def score_output_dir(output_dir: Path) -> dict[str, Any]: | |
| layout = OutputLayout(output_dir) | |
| tasks: dict[str, Any] = {} | |
| for task_name, task in TASK_REGISTRY.items(): | |
| task_dir = layout.task_dir(task_name) | |
| rows = load_prediction_rows(task_dir, sort_key=task.sort_key) | |
| if not rows: | |
| continue | |
| parsed_rows = task.parse_rows(rows) | |
| # Materialize normalized predictions for easier auditing. | |
| from .io import write_jsonl | |
| write_jsonl(task_dir / "predictions_scored.jsonl", parsed_rows) | |
| tasks[task_name] = task.score(parsed_rows) | |
| return { | |
| "model_key": _model_key(output_dir), | |
| "model_label": _model_label(output_dir), | |
| "output_dir": str(output_dir), | |
| "tasks": tasks, | |
| } | |
| def flatten_metrics(model_results: list[dict[str, Any]]) -> list[dict[str, Any]]: | |
| rows: list[dict[str, Any]] = [] | |
| for model in model_results: | |
| for task_name, metric_map in TASK_METRICS.items(): | |
| source_task = metric_map["source_task"] | |
| summary = model["tasks"].get(source_task) | |
| if not summary: | |
| continue | |
| rows.append( | |
| { | |
| "model": model["model_key"], | |
| "model_label": model["model_label"], | |
| "task": task_name, | |
| "task_label": metric_map["label"], | |
| "rows": summary.get("rows"), | |
| "scored": summary.get("scored"), | |
| "errors": summary.get("errors"), | |
| "parse_errors": summary.get("parse_errors"), | |
| "parse_success_rate": summary.get("parse_success_rate"), | |
| "accuracy": summary.get(metric_map.get("accuracy", "")), | |
| "balanced_accuracy": summary.get(metric_map.get("balanced_accuracy", "")), | |
| "f1": summary.get(metric_map.get("f1", "")), | |
| "precision": summary.get(metric_map.get("precision", "")), | |
| "recall": summary.get(metric_map.get("recall", "")), | |
| } | |
| ) | |
| return rows | |
| def write_metrics_csv(path: Path, rows: list[dict[str, Any]]) -> None: | |
| path.parent.mkdir(parents=True, exist_ok=True) | |
| if not rows: | |
| path.write_text("", encoding="utf-8") | |
| return | |
| with path.open("w", encoding="utf-8", newline="") as fh: | |
| writer = csv.DictWriter(fh, fieldnames=list(rows[0])) | |
| writer.writeheader() | |
| writer.writerows(rows) | |
| def _pct(value: Any) -> str: | |
| return f"{float(value) * 100:.1f}%" if isinstance(value, (int, float)) else "n/a" | |
| def write_report_md(report_dir: Path, model_results: list[dict[str, Any]], metric_rows: list[dict[str, Any]], plot_paths: list[Path]) -> None: | |
| lines = [ | |
| "# Benchmark Report", | |
| "", | |
| "This report is generated from raw model outputs. Responses are parsed and scored at report time.", | |
| "", | |
| "## Summary Metrics", | |
| "", | |
| "| Model | Task | Balanced Accuracy | F1 | Precision | Recall | Parse Success |", | |
| "|---|---|---:|---:|---:|---:|---:|", | |
| ] | |
| for row in metric_rows: | |
| lines.append( | |
| "| {model_label} | {task_label} | {balanced_accuracy} | {f1} | {precision} | {recall} | {parse_success_rate} |".format( | |
| model_label=row["model_label"], | |
| task_label=row["task_label"], | |
| balanced_accuracy=_pct(row["balanced_accuracy"]), | |
| f1=_pct(row["f1"]), | |
| precision=_pct(row["precision"]), | |
| recall=_pct(row["recall"]), | |
| parse_success_rate=_pct(row["parse_success_rate"]), | |
| ) | |
| ) | |
| lines.extend(["", "## Plots", ""]) | |
| for path in plot_paths: | |
| rel = path.relative_to(report_dir) | |
| title = path.stem.replace("_", " ").title() | |
| lines.extend([f"### {title}", "", f"})", ""]) | |
| lines.extend(["## Task Details", ""]) | |
| for model in model_results: | |
| lines.extend([f"### {model['model_label']}", ""]) | |
| for task_name, summary in model["tasks"].items(): | |
| lines.extend( | |
| [ | |
| f"#### {TASK_LABELS.get(task_name, task_name)}", | |
| "", | |
| f"- Rows: `{summary.get('rows')}`", | |
| f"- Scored: `{summary.get('scored')}`", | |
| f"- Errors: `{summary.get('errors')}`", | |
| f"- Parse errors: `{summary.get('parse_errors')}`", | |
| f"- Parse success: `{_pct(summary.get('parse_success_rate'))}`", | |
| "", | |
| ] | |
| ) | |
| (report_dir / "report.md").write_text("\n".join(lines), encoding="utf-8") | |
| def generate_report(output_dirs: list[Path], report_dir: Path | None = None) -> dict[str, Any]: | |
| if not output_dirs: | |
| raise ValueError("Provide at least one output directory.") | |
| report_dir = report_dir or output_dirs[0] | |
| report_dir.mkdir(parents=True, exist_ok=True) | |
| model_results = [score_output_dir(path) for path in output_dirs] | |
| metric_rows = flatten_metrics(model_results) | |
| write_json(report_dir / "metrics.json", {"models": model_results, "rows": metric_rows}) | |
| write_metrics_csv(report_dir / "metrics.csv", metric_rows) | |
| plot_paths = write_standard_plots(model_results, report_dir) | |
| write_report_md(report_dir, model_results, metric_rows, plot_paths) | |
| return {"report_dir": str(report_dir), "models": [row["model_key"] for row in model_results], "plots": [str(path) for path in plot_paths]} | |