File size: 11,518 Bytes
ca66b51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
"""Command-line utilities for validating and scoring DocuBench."""

from __future__ import annotations

import argparse
import csv
import json
import sys
from pathlib import Path
from typing import Any

from scorer import score_standardization


DEFAULT_ENGINE_DISPLAY_NAMES = {
    "claude": "Claude",
    "docupipe_high": "DocuPipe high",
    "docupipe_standard": "DocuPipe standard",
    "extend": "Extend",
    "gemini": "Gemini",
    "gpt": "GPT",
}


def load_json(path: Path) -> Any:
    with open(path, encoding="utf-8") as f:
        return json.load(f)


def write_json(path: Path, data: Any) -> None:
    path.parent.mkdir(parents=True, exist_ok=True)
    with open(path, "w", encoding="utf-8") as f:
        json.dump(data, f, ensure_ascii=False, indent=2)
        f.write("\n")


def benchmark_ids(root: Path) -> list[str]:
    return sorted(p.stem for p in (root / "labels").glob("*.json"))


def discover_engines(root: Path) -> list[str]:
    results_dir = root / "results"
    if not results_dir.exists():
        return []
    return sorted(p.name for p in results_dir.iterdir() if p.is_dir())


def document_id_map(root: Path) -> dict[str, Path]:
    docs: dict[str, Path] = {}
    for path in (root / "documents").iterdir():
        if path.is_file():
            docs[path.stem] = path
    return docs


def validate_benchmark(root: Path) -> tuple[list[str], list[str], dict[str, Any]]:
    errors: list[str] = []
    warnings: list[str] = []

    required_dirs = ["documents", "labels", "schemas", "results"]
    for dirname in required_dirs:
        if not (root / dirname).is_dir():
            errors.append(f"missing required directory: {dirname}/")

    sources_path = root / "sources.json"
    if not sources_path.is_file():
        errors.append("missing sources.json")
        sources = []
    else:
        try:
            sources = load_json(sources_path)
        except json.JSONDecodeError as exc:
            errors.append(f"sources.json is invalid JSON: {exc}")
            sources = []

    if errors:
        return errors, warnings, {}

    doc_paths = document_id_map(root)
    doc_ids = set(doc_paths)
    label_ids = {p.stem for p in (root / "labels").glob("*.json")}
    schema_ids = {p.stem for p in (root / "schemas").glob("*.json")}
    source_ids = {row.get("doc_id") for row in sources if isinstance(row, dict)}

    expected_ids = label_ids | schema_ids | source_ids
    for collection_name, ids in [
        ("documents", doc_ids),
        ("labels", label_ids),
        ("schemas", schema_ids),
        ("sources", source_ids),
    ]:
        missing = sorted(expected_ids - ids)
        extra = sorted(ids - expected_ids)
        if missing:
            errors.append(f"{collection_name} missing ids: {', '.join(missing)}")
        if extra:
            errors.append(f"{collection_name} has unexpected ids: {', '.join(extra)}")

    for directory in ["labels", "schemas"]:
        for path in sorted((root / directory).glob("*.json")):
            try:
                load_json(path)
            except json.JSONDecodeError as exc:
                errors.append(f"{path.relative_to(root)} is invalid JSON: {exc}")

    engines = discover_engines(root)
    for engine in engines:
        engine_dir = root / "results" / engine
        result_ids = {p.stem for p in engine_dir.glob("*.json")}
        missing = sorted(label_ids - result_ids)
        extra = sorted(result_ids - label_ids)
        if missing:
            warnings.append(f"results/{engine} missing ids: {', '.join(missing)}")
        if extra:
            warnings.append(f"results/{engine} has extra ids: {', '.join(extra)}")
        for path in sorted(engine_dir.glob("*.json")):
            try:
                payload = load_json(path)
            except json.JSONDecodeError as exc:
                errors.append(f"{path.relative_to(root)} is invalid JSON: {exc}")
                continue
            if not isinstance(payload, dict):
                errors.append(f"{path.relative_to(root)} must contain a JSON object")
            elif "data" not in payload:
                warnings.append(f"{path.relative_to(root)} has no top-level data key")

    summary = {
        "documents": len(doc_ids),
        "labels": len(label_ids),
        "schemas": len(schema_ids),
        "sources": len(source_ids),
        "engines": engines,
    }
    return errors, warnings, summary


def score_engines(root: Path, engines: list[str] | None = None) -> dict[str, Any]:
    doc_ids = benchmark_ids(root)
    selected_engines = engines or discover_engines(root)
    per_engine: dict[str, dict[str, float | None]] = {engine: {} for engine in selected_engines}

    for doc_id in doc_ids:
        schema = load_json(root / "schemas" / f"{doc_id}.json")
        label = load_json(root / "labels" / f"{doc_id}.json")
        for engine in selected_engines:
            result_path = root / "results" / engine / f"{doc_id}.json"
            if not result_path.exists():
                per_engine[engine][doc_id] = None
                continue
            result_payload = load_json(result_path)
            result = result_payload.get("data", {}) if isinstance(result_payload, dict) else {}
            score = score_standardization(result=result, schema=schema, label=label)
            per_engine[engine][doc_id] = score["final"]

    per_doc = []
    for doc_id in doc_ids:
        row: dict[str, Any] = {"doc_id": doc_id}
        for engine in selected_engines:
            row[engine] = per_engine[engine][doc_id]
        per_doc.append(row)

    aggregates = {}
    for engine, scores_by_doc in per_engine.items():
        scores = [score for score in scores_by_doc.values() if score is not None]
        aggregates[engine] = sum(scores) / len(scores) if scores else None

    return {
        "benchmark": {
            "name": "DocuBench",
            "version": "0.1.0",
            "doc_count": len(doc_ids),
            "metric": "macro_average_field_accuracy",
        },
        "engine_display_names": {
            engine: DEFAULT_ENGINE_DISPLAY_NAMES.get(engine, engine)
            for engine in selected_engines
        },
        "aggregates": aggregates,
        "breakdowns": build_breakdowns(root, per_doc, selected_engines),
        "per_doc": per_doc,
    }


def build_breakdowns(root: Path, per_doc: list[dict[str, Any]], engines: list[str]) -> dict[str, Any]:
    sources_path = root / "sources.json"
    if not sources_path.exists():
        return {}
    sources = load_json(sources_path)
    metadata = {row["doc_id"]: row for row in sources if isinstance(row, dict) and "doc_id" in row}
    by_doc = {row["doc_id"]: row for row in per_doc}
    breakdowns: dict[str, Any] = {}

    for dimension in ["ftype", "lang"]:
        groups: dict[str, list[str]] = {}
        for doc_id, row in metadata.items():
            value = str(row.get(dimension) or "unknown")
            groups.setdefault(value, []).append(doc_id)
        dimension_rows = []
        for value in sorted(groups):
            doc_ids = sorted(doc_id for doc_id in groups[value] if doc_id in by_doc)
            out: dict[str, Any] = {"value": value, "doc_count": len(doc_ids)}
            for engine in engines:
                scores = [by_doc[doc_id][engine] for doc_id in doc_ids if by_doc[doc_id][engine] is not None]
                out[engine] = sum(scores) / len(scores) if scores else None
            dimension_rows.append(out)
        breakdowns[dimension] = dimension_rows
    return breakdowns


def print_score_table(scores: dict[str, Any]) -> None:
    engines = list(scores["aggregates"].keys())
    header = f"{'doc_id':<12}" + "".join(f"{engine:>20}" for engine in engines)
    print(header)
    print("-" * len(header))
    for row in scores["per_doc"]:
        line = f"{row['doc_id']:<12}"
        for engine in engines:
            score = row[engine]
            line += f"{(f'{score:.4f}' if score is not None else 'n/a'):>20}"
        print(line)
    print("-" * len(header))
    aggregate_line = f"{'AGGREGATE':<12}"
    for engine in engines:
        score = scores["aggregates"][engine]
        aggregate_line += f"{(f'{score:.4f}' if score is not None else 'n/a'):>20}"
    print(aggregate_line)


def write_summary_csv(path: Path, scores: dict[str, Any]) -> None:
    engines = list(scores["aggregates"].keys())
    path.parent.mkdir(parents=True, exist_ok=True)
    with open(path, "w", encoding="utf-8", newline="") as f:
        writer = csv.DictWriter(f, fieldnames=["doc_id", *engines], lineterminator="\n")
        writer.writeheader()
        for row in scores["per_doc"]:
            writer.writerow({key: row.get(key) for key in ["doc_id", *engines]})
        writer.writerow({"doc_id": "AGGREGATE", **scores["aggregates"]})


def cmd_validate(args: argparse.Namespace) -> int:
    errors, warnings, summary = validate_benchmark(args.root)
    for warning in warnings:
        print(f"warning: {warning}", file=sys.stderr)
    if errors:
        for error in errors:
            print(f"error: {error}", file=sys.stderr)
        return 1
    print(
        "validated "
        f"{summary['documents']} documents, "
        f"{summary['labels']} labels, "
        f"{summary['schemas']} schemas, "
        f"{summary['sources']} source records, "
        f"{len(summary['engines'])} result sets"
    )
    return 0


def cmd_score(args: argparse.Namespace) -> int:
    scores = score_engines(args.root, args.engine)
    if args.json:
        print(json.dumps(scores, ensure_ascii=False, indent=2))
    else:
        print_score_table(scores)
    return 0


def cmd_report(args: argparse.Namespace) -> int:
    scores = score_engines(args.root, args.engine)
    write_json(args.summary_json, scores)
    write_summary_csv(args.summary_csv, scores)
    print(f"wrote {args.summary_json}")
    print(f"wrote {args.summary_csv}")
    return 0


def build_parser() -> argparse.ArgumentParser:
    parser = argparse.ArgumentParser(prog="docubench")
    parser.add_argument("--root", type=Path, default=Path.cwd(), help="repository root")
    subparsers = parser.add_subparsers(dest="command", required=True)

    validate = subparsers.add_parser("validate", help="validate benchmark files")
    validate.set_defaults(func=cmd_validate)

    score = subparsers.add_parser("score", help="score committed result sets")
    score.add_argument("--engine", action="append", help="result directory to score; repeatable")
    score.add_argument("--json", action="store_true", help="emit JSON instead of a table")
    score.set_defaults(func=cmd_score)

    report = subparsers.add_parser("report", help="write summary JSON and CSV reports")
    report.add_argument("--engine", action="append", help="result directory to score; repeatable")
    report.add_argument("--summary-json", type=Path, default=Path("results/summary.json"))
    report.add_argument("--summary-csv", type=Path, default=Path("results/summary.csv"))
    report.set_defaults(func=cmd_report)

    return parser


def main(argv: list[str] | None = None) -> int:
    parser = build_parser()
    args = parser.parse_args(argv)
    args.root = args.root.resolve()
    if hasattr(args, "summary_json") and not args.summary_json.is_absolute():
        args.summary_json = args.root / args.summary_json
    if hasattr(args, "summary_csv") and not args.summary_csv.is_absolute():
        args.summary_csv = args.root / args.summary_csv
    return args.func(args)


if __name__ == "__main__":
    raise SystemExit(main())