#!/usr/bin/env python3 """Merge row-level metrics with judge annotations into compiled benchmark files.""" from __future__ import annotations import argparse from pathlib import Path from typing import Dict, Iterable, Iterator, Tuple from apm_metrics import flatten_judge_fields, load_json_or_jsonl, write_json TEXT_FIELDS = ( "clean_text", "noisy_prompt", "model_response", "response", "assisted_prompt", "mediated_prompt", ) def parse_args() -> argparse.Namespace: parser = argparse.ArgumentParser(description=__doc__) parser.add_argument( "--metrics-root", type=Path, required=True, help="Root containing //metrics.json files.", ) parser.add_argument( "--judged-root", type=Path, required=True, help="Root containing //results.jsonl files with judge annotations.", ) parser.add_argument( "--output-root", type=Path, default=Path("compiled"), help="Directory where compiled JSON files will be written.", ) parser.add_argument( "--include-text-fields", action="store_true", help="Include source prompts and model responses when present.", ) return parser.parse_args() def judged_file(noise_dir: Path) -> Path | None: for preferred in ("results.jsonl", "results.json"): path = noise_dir / preferred if path.exists(): return path candidates = sorted( path for path in noise_dir.iterdir() if path.is_file() and path.suffix in {".json", ".jsonl"} ) return candidates[0] if candidates else None def discover_judged(judged_root: Path) -> Iterator[Tuple[str, str, Path]]: for model_dir in sorted(path for path in judged_root.iterdir() if path.is_dir()): for noise_dir in sorted(path for path in model_dir.iterdir() if path.is_dir()): path = judged_file(noise_dir) if path is not None: yield model_dir.name, noise_dir.name, path def compile_file( *, model: str, noise: str, metrics_path: Path, judged_path: Path, output_path: Path, include_text_fields: bool, ) -> Tuple[int, int]: metrics = load_json_or_jsonl(metrics_path) judged = load_json_or_jsonl(judged_path) metrics_by_id: Dict[str, Dict] = { row["example_id"]: row for row in metrics if row.get("example_id") is not None } compiled = [] missing = 0 for judged_row in judged: ex_id = judged_row.get("example_id") metric_row = metrics_by_id.get(ex_id) if metric_row is None: missing += 1 continue row = { "example_id": ex_id, "model": metric_row.get("model") or judged_row.get("model") or model, "noise": metric_row.get("noise") or judged_row.get("noise") or noise, } for key, value in metric_row.items(): if key not in {"example_id", "model", "noise"}: row[key] = value row.update(flatten_judge_fields(judged_row)) if include_text_fields: for key in TEXT_FIELDS: if key in judged_row: row[key] = judged_row[key] compiled.append(row) write_json(compiled, output_path) return len(compiled), missing def main() -> None: args = parse_args() total_rows = 0 total_missing = 0 total_files = 0 for model, noise, judged_path in discover_judged(args.judged_root): metrics_path = args.metrics_root / model / noise / "metrics.json" if not metrics_path.exists(): print(f"Skipping {model}/{noise}: missing {metrics_path}") continue output_path = args.output_root / model / noise / "compiled.json" rows, missing = compile_file( model=model, noise=noise, metrics_path=metrics_path, judged_path=judged_path, output_path=output_path, include_text_fields=args.include_text_fields, ) total_rows += rows total_missing += missing total_files += 1 print(f"{model}/{noise}: {rows} compiled rows, {missing} missing metrics -> {output_path}") print( f"Compiled {total_rows} rows from {total_files} files; " f"{total_missing} judged rows had no matching metric row" ) if __name__ == "__main__": main()