Datasets:

Modalities:
Image
Text
Formats:
parquet
Languages:
Armenian
Size:
< 1K
ArXiv:
License:
Yeva commited on
Commit
4bc18f2
·
verified ·
1 Parent(s): b576be9

Replace evaluation kit with evaluation_kit/ folder content

Browse files
evaluation_kit/README.md CHANGED
@@ -401,34 +401,6 @@ python3 evaluate_from_hf.py \
401
  This prints one line per filter variant (`cer=... (N pages) -> path`)
402
  and writes the four standard report JSONs (§6, §7) to `results/`.
403
 
404
- #### Per-document (per-page) breakdown
405
-
406
- Every run also writes `results/pages_summary.json`: a flat table with one row
407
- per (page, variant) — `page_name`, `variant`, `ocr_region_count`,
408
- `ocr_region_gt_char_count`, `ocr_region_char_edit_distance`, `ocr_region_cer` —
409
- sorted worst CER first within each variant, so the pages dragging the score
410
- down are visible without opening the `pages` list inside each full report JSON.
411
- Same data, same file, on `evaluate_from_hf_fast.py`.
412
-
413
- #### Per-source (or per-column) breakdown
414
-
415
- Add `--group-by <column>` to also get one set of report JSONs per distinct
416
- value of a dataset column, on top of the all-pages reports:
417
-
418
- ```bash
419
- python3 evaluate_from_hf.py \
420
- --dataset RLALT/ACoPDoc \
421
- --predictions-dir evaluation_csvs \
422
- --output-dir results \
423
- --unit-level word \
424
- --group-by source
425
- ```
426
-
427
- For ACoPDoc's `source` column this is a per-document breakdown (20 groups of
428
- 10 pages). Output goes to `results/by_source/<value>/<variant>.json` plus a
429
- flat `results/summary_by_source.json` table (`source`, `variant`, `pages`,
430
- `ocr_region_cer`). `--group-by` works the same on `evaluate_from_hf_fast.py`.
431
-
432
  `evaluate_from_hf.py` builds `AnnotationBox` objects directly from each row's
433
  `annotations` list — the HF schema is already flattened compared to what the
434
  local-JSON loader (`load_annotation_boxes`, used by the scripts in §8)
 
401
  This prints one line per filter variant (`cer=... (N pages) -> path`)
402
  and writes the four standard report JSONs (§6, §7) to `results/`.
403
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
404
  `evaluate_from_hf.py` builds `AnnotationBox` objects directly from each row's
405
  `annotations` list — the HF schema is already flattened compared to what the
406
  local-JSON loader (`load_annotation_boxes`, used by the scripts in §8)
evaluation_kit/REPORT_JSON_METRICS.md CHANGED
@@ -226,10 +226,4 @@ overall_accuracy_filtered_all_report.json
226
  ```
227
 
228
  The overlay PNGs and matching JSON legends are written under
229
- `region_overlays/<report-name>/` inside the selected results directory.
230
-
231
- It also writes `pages_summary.json`, a flat per-document (per-page) table
232
- derived from the `pages` list of each variant report: one row per
233
- `(page_name, variant)` with `ocr_region_count`, `ocr_region_gt_char_count`,
234
- `ocr_region_char_edit_distance`, and `ocr_region_cer`, sorted worst CER first
235
- within each variant.
 
226
  ```
227
 
228
  The overlay PNGs and matching JSON legends are written under
229
+ `region_overlays/<report-name>/` inside the selected results directory.
 
 
 
 
 
 
evaluation_kit/evaluate_from_hf.py CHANGED
@@ -113,119 +113,9 @@ def parse_args() -> argparse.Namespace:
113
  default=None,
114
  help="Generate only this filter variant. Omit to generate all four.",
115
  )
116
- parser.add_argument(
117
- "--group-by",
118
- default=None,
119
- help=(
120
- "Also write a per-group breakdown, bucketing pages by this dataset "
121
- "column (e.g. 'source' -> one report per newspaper issue / document). "
122
- "Reports go to <output-dir>/by_<column>/<value>/ plus a "
123
- "summary_by_<column>.json table. The unaggregated all-pages reports "
124
- "are still written as usual."
125
- ),
126
- )
127
  return parser.parse_args()
128
 
129
 
130
- def sanitize_group_value(value: str) -> str:
131
- """Make a group value safe to use as a single path segment."""
132
- return "".join("_" if ch in '/\\\0' or ch.isspace() else ch for ch in value) or "__empty__"
133
-
134
-
135
- def write_grouped_reports(
136
- output_dir: Path,
137
- variants: tuple[dict[str, Any], ...],
138
- page_reports_by_variant: dict[str, list[dict[str, Any]]],
139
- group_by: str,
140
- coverage_threshold: float,
141
- failure_example_count: int,
142
- unit_level: str,
143
- ) -> None:
144
- """Write one aggregate report per (group value, variant), plus a summary table.
145
-
146
- Each page report dict must carry a "group" key holding the row's value for
147
- the grouped column; pages that somehow lack one are bucketed under
148
- "__ungrouped__" so nothing is silently dropped.
149
- """
150
- group_root = output_dir / f"by_{group_by}"
151
- group_root.mkdir(parents=True, exist_ok=True)
152
- summary_rows: list[dict[str, Any]] = []
153
-
154
- for variant in variants:
155
- by_group: dict[str, list[dict[str, Any]]] = {}
156
- for page_report in page_reports_by_variant[variant["name"]]:
157
- key = page_report.get("group") or "__ungrouped__"
158
- by_group.setdefault(key, []).append(page_report)
159
-
160
- for group_value in sorted(by_group):
161
- group_page_reports = by_group[group_value]
162
- aggregate_report = aggregate_reports(
163
- page_reports=group_page_reports,
164
- coverage_threshold=coverage_threshold,
165
- failure_example_count=failure_example_count,
166
- unit_level=unit_level,
167
- )
168
- group_dir = group_root / sanitize_group_value(group_value)
169
- group_dir.mkdir(parents=True, exist_ok=True)
170
- (group_dir / variant["filename"]).write_text(
171
- json.dumps(aggregate_report, ensure_ascii=False, indent=2),
172
- encoding="utf-8",
173
- )
174
- summary = aggregate_report["summary"]
175
- summary_rows.append(
176
- {
177
- group_by: group_value,
178
- "variant": variant["name"],
179
- "pages": summary["pair_count"],
180
- "ocr_region_cer": summary["ocr_region_cer"],
181
- }
182
- )
183
- print(
184
- f" [{group_by}={group_value}] {variant['name']}: "
185
- f"cer={summary['ocr_region_cer']:.4f} ({summary['pair_count']} page(s))",
186
- flush=True,
187
- )
188
-
189
- (output_dir / f"summary_by_{group_by}.json").write_text(
190
- json.dumps(summary_rows, ensure_ascii=False, indent=2),
191
- encoding="utf-8",
192
- )
193
- print(f"Wrote per-{group_by} reports to {group_root}", flush=True)
194
-
195
-
196
- def write_page_summary(
197
- output_dir: Path,
198
- variants: tuple[dict[str, Any], ...],
199
- aggregate_report_by_variant: dict[str, dict[str, Any]],
200
- ) -> None:
201
- """Write a flat per-document (per-page) CER table to `pages_summary.json`.
202
-
203
- One row per (page, variant), sorted worst CER first within each variant, so
204
- the pages dragging a run's score down are visible without digging through
205
- the `pages` list inside each full report JSON.
206
- """
207
- rows: list[dict[str, Any]] = []
208
- for variant in variants:
209
- for page in aggregate_report_by_variant[variant["name"]]["pages"]:
210
- summary = page["summary"]
211
- rows.append(
212
- {
213
- "page_name": page["page_name"],
214
- "variant": variant["name"],
215
- "ocr_region_count": summary["ocr_region_count"],
216
- "ocr_region_gt_char_count": summary["ocr_region_gt_char_count"],
217
- "ocr_region_char_edit_distance": summary["ocr_region_char_edit_distance"],
218
- "ocr_region_cer": summary["ocr_region_cer"],
219
- }
220
- )
221
- rows.sort(key=lambda r: (r["variant"], -r["ocr_region_cer"], r["page_name"]))
222
- (output_dir / "pages_summary.json").write_text(
223
- json.dumps(rows, ensure_ascii=False, indent=2),
224
- encoding="utf-8",
225
- )
226
- print(f"Wrote per-page summary to {output_dir / 'pages_summary.json'}", flush=True)
227
-
228
-
229
  def annotation_box_from_hf_item(item: dict[str, Any]) -> AnnotationBox:
230
  """Build an AnnotationBox from one entry of the dataset's `annotations` column.
231
 
@@ -335,12 +225,6 @@ def main() -> None:
335
  )
336
  print(f"Loaded {len(dataset)} page(s) from {args.dataset}[{args.split}]")
337
 
338
- if args.group_by and args.group_by not in dataset.column_names:
339
- raise SystemExit(
340
- f"--group-by {args.group_by!r}: column not in dataset "
341
- f"({', '.join(dataset.column_names)})"
342
- )
343
-
344
  variants = REPORT_VARIANTS
345
  if args.variant:
346
  variants = tuple(v for v in REPORT_VARIANTS if v["name"] == args.variant)
@@ -371,15 +255,14 @@ def main() -> None:
371
  filters=filters,
372
  unit_level=args.unit_level,
373
  )
374
- page_report = {
375
- "page_name": page_id,
376
- "predictions_csv": str(predictions_csv),
377
- "annotations_json": f"hf://{args.dataset}/{args.split}#{page_id}",
378
- "report": report,
379
- }
380
- if args.group_by:
381
- page_report["group"] = str(row[args.group_by])
382
- page_reports_by_variant[variant["name"]].append(page_report)
383
 
384
  if missing_predictions:
385
  print(
@@ -390,7 +273,6 @@ def main() -> None:
390
  )
391
 
392
  args.output_dir.mkdir(parents=True, exist_ok=True)
393
- aggregate_report_by_variant: dict[str, dict[str, Any]] = {}
394
  for variant in variants:
395
  aggregate_report = aggregate_reports(
396
  page_reports=page_reports_by_variant[variant["name"]],
@@ -398,7 +280,6 @@ def main() -> None:
398
  failure_example_count=args.failure_example_count,
399
  unit_level=args.unit_level,
400
  )
401
- aggregate_report_by_variant[variant["name"]] = aggregate_report
402
  output_path = args.output_dir / variant["filename"]
403
  output_path.write_text(
404
  json.dumps(aggregate_report, ensure_ascii=False, indent=2),
@@ -411,19 +292,6 @@ def main() -> None:
411
  flush=True,
412
  )
413
 
414
- write_page_summary(args.output_dir, variants, aggregate_report_by_variant)
415
-
416
- if args.group_by:
417
- write_grouped_reports(
418
- output_dir=args.output_dir,
419
- variants=variants,
420
- page_reports_by_variant=page_reports_by_variant,
421
- group_by=args.group_by,
422
- coverage_threshold=args.coverage_threshold,
423
- failure_example_count=args.failure_example_count,
424
- unit_level=args.unit_level,
425
- )
426
-
427
 
428
  if __name__ == "__main__":
429
  main()
 
113
  default=None,
114
  help="Generate only this filter variant. Omit to generate all four.",
115
  )
 
 
 
 
 
 
 
 
 
 
 
116
  return parser.parse_args()
117
 
118
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
119
  def annotation_box_from_hf_item(item: dict[str, Any]) -> AnnotationBox:
120
  """Build an AnnotationBox from one entry of the dataset's `annotations` column.
121
 
 
225
  )
226
  print(f"Loaded {len(dataset)} page(s) from {args.dataset}[{args.split}]")
227
 
 
 
 
 
 
 
228
  variants = REPORT_VARIANTS
229
  if args.variant:
230
  variants = tuple(v for v in REPORT_VARIANTS if v["name"] == args.variant)
 
255
  filters=filters,
256
  unit_level=args.unit_level,
257
  )
258
+ page_reports_by_variant[variant["name"]].append(
259
+ {
260
+ "page_name": page_id,
261
+ "predictions_csv": str(predictions_csv),
262
+ "annotations_json": f"hf://{args.dataset}/{args.split}#{page_id}",
263
+ "report": report,
264
+ }
265
+ )
 
266
 
267
  if missing_predictions:
268
  print(
 
273
  )
274
 
275
  args.output_dir.mkdir(parents=True, exist_ok=True)
 
276
  for variant in variants:
277
  aggregate_report = aggregate_reports(
278
  page_reports=page_reports_by_variant[variant["name"]],
 
280
  failure_example_count=args.failure_example_count,
281
  unit_level=args.unit_level,
282
  )
 
283
  output_path = args.output_dir / variant["filename"]
284
  output_path.write_text(
285
  json.dumps(aggregate_report, ensure_ascii=False, indent=2),
 
292
  flush=True,
293
  )
294
 
 
 
 
 
 
 
 
 
 
 
 
 
 
295
 
296
  if __name__ == "__main__":
297
  main()
evaluation_kit/evaluate_from_hf_fast.py CHANGED
@@ -49,11 +49,7 @@ from generate_accuracy_report_variants import REPORT_VARIANTS # noqa: E402
49
  from loading import load_predicted_rows # noqa: E402
50
 
51
  sys.path.insert(0, str(_ROOT))
52
- from evaluate_from_hf import ( # noqa: E402
53
- annotation_boxes_from_hf_row,
54
- write_grouped_reports,
55
- write_page_summary,
56
- )
57
 
58
 
59
  def parse_args() -> argparse.Namespace:
@@ -72,21 +68,10 @@ def parse_args() -> argparse.Namespace:
72
  default=None,
73
  help="Score only these page_ids (fast subset check). Omit to score all pages.",
74
  )
75
- parser.add_argument(
76
- "--group-by",
77
- default=None,
78
- help=(
79
- "Also write a per-group breakdown, bucketing pages by this dataset "
80
- "column (e.g. 'source'). Reports go to <output-dir>/by_<column>/<value>/ "
81
- "plus a summary_by_<column>.json table."
82
- ),
83
- )
84
  return parser.parse_args()
85
 
86
 
87
- def load_annotations_only(
88
- dataset: str, split: str, extra_columns: list[str] | None = None
89
- ) -> list[dict[str, Any]]:
90
  import pyarrow.parquet as pq
91
  from huggingface_hub import snapshot_download
92
 
@@ -96,25 +81,14 @@ def load_annotations_only(
96
  parquet_files = sorted(str(p) for p in snapshot_dir.glob(f"data/{split}-*.parquet"))
97
  if not parquet_files:
98
  raise ValueError(f"{dataset}: no data/{split}-*.parquet files found")
99
- columns = ["page_id", "annotations"]
100
- for column in extra_columns or []:
101
- if column not in columns:
102
- available = pq.read_schema(parquet_files[0]).names
103
- if column not in available:
104
- raise SystemExit(
105
- f"--group-by {column!r}: column not in dataset ({', '.join(available)})"
106
- )
107
- columns.append(column)
108
- table = pq.read_table(parquet_files, columns=columns)
109
  return table.to_pylist()
110
 
111
 
112
  def main() -> None:
113
  args = parse_args()
114
 
115
- rows = load_annotations_only(
116
- args.dataset, args.split, extra_columns=[args.group_by] if args.group_by else None
117
- )
118
  if args.pages:
119
  wanted = set(args.pages)
120
  rows = [r for r in rows if r["page_id"] in wanted]
@@ -151,15 +125,14 @@ def main() -> None:
151
  filters=filters,
152
  unit_level=args.unit_level,
153
  )
154
- page_report = {
155
- "page_name": page_id,
156
- "predictions_csv": str(predictions_csv),
157
- "annotations_json": f"hf://{args.dataset}/{args.split}#{page_id}",
158
- "report": report,
159
- }
160
- if args.group_by:
161
- page_report["group"] = str(row[args.group_by])
162
- page_reports_by_variant[variant["name"]].append(page_report)
163
 
164
  if missing_predictions:
165
  print(
@@ -169,7 +142,6 @@ def main() -> None:
169
  )
170
 
171
  args.output_dir.mkdir(parents=True, exist_ok=True)
172
- aggregate_report_by_variant: dict[str, dict[str, Any]] = {}
173
  for variant in variants:
174
  aggregate_report = aggregate_reports(
175
  page_reports=page_reports_by_variant[variant["name"]],
@@ -177,7 +149,6 @@ def main() -> None:
177
  failure_example_count=args.failure_example_count,
178
  unit_level=args.unit_level,
179
  )
180
- aggregate_report_by_variant[variant["name"]] = aggregate_report
181
  output_path = args.output_dir / variant["filename"]
182
  output_path.write_text(json.dumps(aggregate_report, ensure_ascii=False, indent=2), encoding="utf-8")
183
  summary = aggregate_report["summary"]
@@ -187,19 +158,6 @@ def main() -> None:
187
  flush=True,
188
  )
189
 
190
- write_page_summary(args.output_dir, variants, aggregate_report_by_variant)
191
-
192
- if args.group_by:
193
- write_grouped_reports(
194
- output_dir=args.output_dir,
195
- variants=variants,
196
- page_reports_by_variant=page_reports_by_variant,
197
- group_by=args.group_by,
198
- coverage_threshold=args.coverage_threshold,
199
- failure_example_count=args.failure_example_count,
200
- unit_level=args.unit_level,
201
- )
202
-
203
 
204
  if __name__ == "__main__":
205
  main()
 
49
  from loading import load_predicted_rows # noqa: E402
50
 
51
  sys.path.insert(0, str(_ROOT))
52
+ from evaluate_from_hf import annotation_boxes_from_hf_row # noqa: E402
 
 
 
 
53
 
54
 
55
  def parse_args() -> argparse.Namespace:
 
68
  default=None,
69
  help="Score only these page_ids (fast subset check). Omit to score all pages.",
70
  )
 
 
 
 
 
 
 
 
 
71
  return parser.parse_args()
72
 
73
 
74
+ def load_annotations_only(dataset: str, split: str) -> list[dict[str, Any]]:
 
 
75
  import pyarrow.parquet as pq
76
  from huggingface_hub import snapshot_download
77
 
 
81
  parquet_files = sorted(str(p) for p in snapshot_dir.glob(f"data/{split}-*.parquet"))
82
  if not parquet_files:
83
  raise ValueError(f"{dataset}: no data/{split}-*.parquet files found")
84
+ table = pq.read_table(parquet_files, columns=["page_id", "annotations"])
 
 
 
 
 
 
 
 
 
85
  return table.to_pylist()
86
 
87
 
88
  def main() -> None:
89
  args = parse_args()
90
 
91
+ rows = load_annotations_only(args.dataset, args.split)
 
 
92
  if args.pages:
93
  wanted = set(args.pages)
94
  rows = [r for r in rows if r["page_id"] in wanted]
 
125
  filters=filters,
126
  unit_level=args.unit_level,
127
  )
128
+ page_reports_by_variant[variant["name"]].append(
129
+ {
130
+ "page_name": page_id,
131
+ "predictions_csv": str(predictions_csv),
132
+ "annotations_json": f"hf://{args.dataset}/{args.split}#{page_id}",
133
+ "report": report,
134
+ }
135
+ )
 
136
 
137
  if missing_predictions:
138
  print(
 
142
  )
143
 
144
  args.output_dir.mkdir(parents=True, exist_ok=True)
 
145
  for variant in variants:
146
  aggregate_report = aggregate_reports(
147
  page_reports=page_reports_by_variant[variant["name"]],
 
149
  failure_example_count=args.failure_example_count,
150
  unit_level=args.unit_level,
151
  )
 
152
  output_path = args.output_dir / variant["filename"]
153
  output_path.write_text(json.dumps(aggregate_report, ensure_ascii=False, indent=2), encoding="utf-8")
154
  summary = aggregate_report["summary"]
 
158
  flush=True,
159
  )
160
 
 
 
 
 
 
 
 
 
 
 
 
 
 
161
 
162
  if __name__ == "__main__":
163
  main()