Datasets:
Fix evaluate_from_hf.py: un-pivot columnar annotations, load parquet directly
Browse filesdatasets stores a Sequence(struct) column as a dict of parallel lists rather than a list of per-annotation dicts, so row["annotations"] needed un-pivoting before iterating. Also load the split parquet file(s) directly via snapshot_download + Dataset.from_parquet instead of load_dataset(), since some dataset repos declared dataset_info.features lag behind the actual parquet columns (e.g. this repo is missing reading_order/parent_id/rotation), which made load_dataset raise a cast error.
evaluation_kit/evaluate_from_hf.py
CHANGED
|
@@ -158,11 +158,19 @@ def annotation_boxes_from_hf_row(row: dict[str, Any]) -> list[AnnotationBox]:
|
|
| 158 |
post-processing has to run here or this entry point would score against a
|
| 159 |
different ground truth than the local-JSON scripts do.
|
| 160 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
# Rule-labeled regions are decorative separator lines, not text — the
|
| 162 |
# raw-JSON loader (load_annotation_boxes) drops them the same way.
|
| 163 |
boxes = [
|
| 164 |
annotation_box_from_hf_item(item)
|
| 165 |
-
for item in
|
| 166 |
if item["label"] != "Rule"
|
| 167 |
]
|
| 168 |
|
|
@@ -181,13 +189,28 @@ def main() -> None:
|
|
| 181 |
args = parse_args()
|
| 182 |
|
| 183 |
try:
|
| 184 |
-
from datasets import
|
|
|
|
| 185 |
except ImportError as exc:
|
| 186 |
raise SystemExit(
|
| 187 |
"This script needs the `datasets` package: pip install -r requirements.txt"
|
| 188 |
) from exc
|
| 189 |
|
| 190 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 191 |
if args.dataset_split_column:
|
| 192 |
dataset = dataset.filter(
|
| 193 |
lambda row: row["split"] == args.dataset_split_column
|
|
|
|
| 158 |
post-processing has to run here or this entry point would score against a
|
| 159 |
different ground truth than the local-JSON scripts do.
|
| 160 |
"""
|
| 161 |
+
# `datasets` returns a Sequence(feature=struct) column columnar — a dict
|
| 162 |
+
# of parallel lists, e.g. {"id": [...], "bbox": [...], ...} — rather than
|
| 163 |
+
# a list of per-annotation dicts, so it has to be un-pivoted first.
|
| 164 |
+
columnar = row["annotations"]
|
| 165 |
+
keys = list(columnar.keys())
|
| 166 |
+
count = len(columnar[keys[0]]) if keys else 0
|
| 167 |
+
items = [{key: columnar[key][i] for key in keys} for i in range(count)]
|
| 168 |
+
|
| 169 |
# Rule-labeled regions are decorative separator lines, not text — the
|
| 170 |
# raw-JSON loader (load_annotation_boxes) drops them the same way.
|
| 171 |
boxes = [
|
| 172 |
annotation_box_from_hf_item(item)
|
| 173 |
+
for item in items
|
| 174 |
if item["label"] != "Rule"
|
| 175 |
]
|
| 176 |
|
|
|
|
| 189 |
args = parse_args()
|
| 190 |
|
| 191 |
try:
|
| 192 |
+
from datasets import Dataset
|
| 193 |
+
from huggingface_hub import snapshot_download
|
| 194 |
except ImportError as exc:
|
| 195 |
raise SystemExit(
|
| 196 |
"This script needs the `datasets` package: pip install -r requirements.txt"
|
| 197 |
) from exc
|
| 198 |
|
| 199 |
+
# Load the parquet file(s) directly instead of `datasets.load_dataset(args.dataset)`:
|
| 200 |
+
# some dataset repos' README-declared `dataset_info.features` lag behind columns
|
| 201 |
+
# actually present in the parquet data (e.g. RLALT/ACoPDoc's `annotations` struct
|
| 202 |
+
# is missing `reading_order`/`parent_id`/`rotation` in its declared schema), which
|
| 203 |
+
# makes `load_dataset` raise a cast error while forcing the data into that stale
|
| 204 |
+
# schema. `Dataset.from_parquet` infers features from the file itself instead.
|
| 205 |
+
snapshot_dir = Path(
|
| 206 |
+
snapshot_download(
|
| 207 |
+
repo_id=args.dataset, repo_type="dataset", allow_patterns=[f"data/{args.split}-*.parquet"]
|
| 208 |
+
)
|
| 209 |
+
)
|
| 210 |
+
parquet_files = sorted(str(p) for p in snapshot_dir.glob(f"data/{args.split}-*.parquet"))
|
| 211 |
+
if not parquet_files:
|
| 212 |
+
raise ValueError(f"{args.dataset}: no data/{args.split}-*.parquet files found")
|
| 213 |
+
dataset = Dataset.from_parquet(parquet_files)
|
| 214 |
if args.dataset_split_column:
|
| 215 |
dataset = dataset.filter(
|
| 216 |
lambda row: row["split"] == args.dataset_split_column
|