-
Notifications
You must be signed in to change notification settings - Fork 1
docs(bench): score pdfgrab against the field, and fix the ICDAR metric #26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
hallelx2
merged 1 commit into
main
from
halleluyaholudele/hal-1361-pdfgrab-benchmark-against-the-whole-field-and-fix-a-metric
Sep 17, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| """Score every available table-extraction system on ICDAR 2013. | ||
|
|
||
| python bench/icdar2013/compare.py <corpus-root> <pdfgrab-extractor> [--limit N] | ||
|
|
||
| Where score.py answers "is pdfgrab as good as pdfplumber", this answers | ||
| the broader question: how does it stand against the field. Same corpus, | ||
| same metric, same process — the only thing that varies is the extractor. | ||
|
|
||
| Reports accuracy AND wall-clock. A benchmark that reports only F1 hides | ||
| the trade a pipeline actually has to make. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import argparse | ||
| import json | ||
| import os | ||
| import sys | ||
| from collections import Counter | ||
|
|
||
| HERE = os.path.dirname(os.path.abspath(__file__)) | ||
| sys.path.insert(0, HERE) | ||
|
|
||
| from score import gt_relations, norm, prf, relations_from_grid, score # noqa: E402 | ||
| from systems import Timing, build_adapters, timed # noqa: E402 | ||
|
|
||
|
|
||
| def relations(tables) -> Counter: | ||
| rels: Counter = Counter() | ||
| for grid in tables: | ||
| rels += relations_from_grid([[norm(c) for c in row] for row in grid]) | ||
| return rels | ||
|
|
||
|
|
||
| def find_pairs(root: str, limit: int = 0) -> list[tuple[str, str]]: | ||
| pairs = [] | ||
| for dirpath, _, files in os.walk(root): | ||
| for f in sorted(files): | ||
| if not f.endswith("-str.xml"): | ||
| continue | ||
| pdf = os.path.join(dirpath, f.replace("-str.xml", ".pdf")) | ||
| if os.path.exists(pdf): | ||
| pairs.append((pdf, os.path.join(dirpath, f))) | ||
| pairs.sort() | ||
| return pairs[:limit] if limit else pairs | ||
|
|
||
|
|
||
| def main() -> int: | ||
| ap = argparse.ArgumentParser() | ||
| ap.add_argument("root", help="corpus root") | ||
| ap.add_argument("exe", help="built pdfgrab extractor") | ||
| ap.add_argument("--limit", type=int, default=0) | ||
| ap.add_argument("--gxpdf", default="", help="built coregx/gxpdf extractor") | ||
| ap.add_argument("--json", default="", help="also write results here") | ||
| args = ap.parse_args() | ||
|
|
||
| pairs = find_pairs(args.root, args.limit) | ||
| adapters = build_adapters(args.exe, args.gxpdf) | ||
|
|
||
| active, skipped = [], [] | ||
| for a in adapters: | ||
| ok, why = a.available() | ||
| (active if ok else skipped).append((a, why)) | ||
|
|
||
| print(f"corpus : {args.root}") | ||
| print(f"documents : {len(pairs)}") | ||
| print(f"systems : {len(active)} active, {len(skipped)} skipped\n") | ||
|
|
||
| if skipped: | ||
| print("skipped (library not importable):") | ||
| for a, why in skipped: | ||
| print(f" {a.name:<26} pip install {a.install}") | ||
| print(f" {'':<26} {why}") | ||
| print() | ||
|
|
||
| # Two aggregations, because they are different numbers and only one of | ||
| # them is the competition's. | ||
| # | ||
| # micro — pool every relation across the corpus, then score once. | ||
| # Weights a document by how many relations it has. | ||
| # macro — score each document, then average the per-document F1s. | ||
| # A one-table document counts as much as a five-table one. | ||
| # | ||
| # ICDAR 2013 specifies MACRO ("per-document averages"), so that is the | ||
| # number comparable to published results. Micro is reported alongside | ||
| # because it is the more natural read of "how many relations did we get | ||
| # right", and quoting one while the reader assumes the other is exactly | ||
| # how benchmark numbers get misused. | ||
| totals = {a.name: [0, 0, 0] for a, _ in active} | ||
| per_doc = {a.name: [] for a, _ in active} | ||
| timings = {a.name: Timing() for a, _ in active} | ||
|
|
||
| for i, (pdf, xml) in enumerate(pairs, 1): | ||
| gt = gt_relations(xml) | ||
| for a, _ in active: | ||
| got = relations(timed(a.extract, pdf, timings[a.name])) | ||
| c, nd, ng = score(gt, got) | ||
| totals[a.name][0] += c | ||
| totals[a.name][1] += nd | ||
| totals[a.name][2] += ng | ||
| per_doc[a.name].append(prf(c, nd, ng)) | ||
| if i % 10 == 0: | ||
| print(f" ...{i}/{len(pairs)}", flush=True) | ||
|
|
||
| rows = [] | ||
| for a, _ in active: | ||
| mp, mr, mf = prf(*totals[a.name]) | ||
| docs = per_doc[a.name] | ||
| n = len(docs) or 1 | ||
| Mp = sum(d[0] for d in docs) / n | ||
| Mr = sum(d[1] for d in docs) / n | ||
| Mf = sum(d[2] for d in docs) / n | ||
| t = timings[a.name] | ||
| rows.append({ | ||
| "system": a.name, | ||
| "version": a.version(), | ||
| "macro_precision": round(Mp, 3), | ||
| "macro_recall": round(Mr, 3), | ||
| "macro_f1": round(Mf, 3), | ||
| "micro_precision": round(mp, 3), | ||
| "micro_recall": round(mr, 3), | ||
| "micro_f1": round(mf, 3), | ||
| "mean_ms": round(t.mean_ms(), 1), | ||
| "p95_ms": round(t.p95_ms(), 1), | ||
| "failures": t.failures, | ||
| "note": a.note, | ||
| }) | ||
| rows.sort(key=lambda d: d["macro_f1"], reverse=True) | ||
|
|
||
| w = max(len(r["system"]) for r in rows) + 2 | ||
| print(f"\n{'':<{w}} {'--- per-document (ICDAR) ---':^25} {'-- pooled --':^17}") | ||
| print(f"{'system':<{w}} {'prec':>7} {'recall':>7} {'F1':>8} " | ||
| f"{'prec':>7} {'F1':>8} {'ms/doc':>9} {'p95 ms':>9} {'fails':>6}") | ||
| print("-" * (w + 60)) | ||
| for r in rows: | ||
| print(f"{r['system']:<{w}} {r['macro_precision']:>7.3f} " | ||
| f"{r['macro_recall']:>7.3f} {r['macro_f1']:>8.3f} " | ||
| f"{r['micro_precision']:>7.3f} {r['micro_f1']:>8.3f} " | ||
| f"{r['mean_ms']:>9.1f} {r['p95_ms']:>9.1f} {r['failures']:>6d}") | ||
|
|
||
| gtn = next(iter(totals.values()))[2] if totals else 0 | ||
| print(f"\nground-truth relations: {gtn}") | ||
| print("\nMetric: adjacency relations (Goebel et al.), END-TO-END — find the") | ||
| print("table AND grid it. NOT comparable to published structure-only scores,") | ||
| print("which are handed the table region.") | ||
| print() | ||
| print("Ranked on PER-DOCUMENT F1, which is the ICDAR 2013 protocol and the") | ||
| print("column to cite. Pooled F1 is shown too because it answers a different") | ||
| print("question (how many relations were right overall) and the two diverge") | ||
| print("whenever documents differ in size.") | ||
|
|
||
| if args.json: | ||
| with open(args.json, "w") as fh: | ||
| json.dump({"documents": len(pairs), "results": rows}, fh, indent=2) | ||
| print(f"\nwrote {args.json}") | ||
| return 0 | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| sys.exit(main()) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: hallelx2/pdfgrab
Length of output: 4837
Correct the pdfgrab mode and rank statement.
The results table ranks
pdfgrab (auto)fifth with a per-document F1 of 0.443. It rankspdfgrab (lines)sixth with 0.442. Update the README to identify the mode and rank correctly.🤖 Prompt for AI Agents