Datasets:
Download src/explicit_learning/ingest/chartqa.py from sungguk/visual-answerability: direct link, hf CLI and curl.
- Browser
- Download file 2.62 kB
-
https://huggingface.co/datasets/sungguk/visual-answerability/resolve/main/src/explicit_learning/ingest/chartqa.py
- Command line
-
hf download hf://datasets/sungguk/visual-answerability/src/explicit_learning/ingest/chartqa.py
-
curl -L -o chartqa.py https://huggingface.co/datasets/sungguk/visual-answerability/resolve/main/src/explicit_learning/ingest/chartqa.py
2.62 kB
| """ChartQA-H importer — human-authored questions only, table artifact hashed. | |
| Of ChartQA's 28,299 train rows, only the 7,398 ``type=human`` rows are kept | |
| (``docs/01`` §3.4); augmented questions are excluded from the core | |
| human-question-preservation evidence. Non-human rows are *dropped* (the importer | |
| returns ``None``), never silently relabeled, and the driver counts them so the | |
| "zero non-human rows in output" invariant is verifiable. | |
| The underlying table is the ground truth for recomputing an ``A_CHANGED`` | |
| answer, so its canonical-JSON SHA-256 is recorded in | |
| ``provenance.table_sha256`` and linked separately from the raster image. | |
| """ | |
| from __future__ import annotations | |
| from collections.abc import Mapping | |
| from typing import Any | |
| from ..hashing import canonical_json, sha256_bytes | |
| from .base import ( | |
| ImageResolver, | |
| ImageStore, | |
| IngestError, | |
| NormalizedItem, | |
| Policy, | |
| infer_open_answer_type, | |
| make_item, | |
| ) | |
| NAME = "chartqa" | |
| POLICY: Policy = "c2_train_candidate" | |
| ALLOWED_SPLIT = "train" | |
| HUMAN_TYPE = "human" | |
| def is_human(row: Mapping[str, Any]) -> bool: | |
| """True when a ChartQA row is a human-authored question.""" | |
| return str(row.get("type", "")).strip() == HUMAN_TYPE | |
| def normalize( | |
| row: Mapping[str, Any], | |
| images: ImageStore, | |
| resolve: ImageResolver, | |
| *, | |
| revision: str, | |
| split: str, | |
| config: str = "default", | |
| ) -> NormalizedItem | None: | |
| """Normalize one ChartQA row; return ``None`` for non-human (augmented) rows.""" | |
| if split != ALLOWED_SPLIT: | |
| raise IngestError(f"chartqa: only split {ALLOWED_SPLIT!r} may be ingested, got {split!r}") | |
| if not is_human(row): | |
| return None # augmented row — dropped, counted by the driver | |
| question = str(row["query"]) | |
| answer_raw = str(row["label"]) | |
| answer_type = infer_open_answer_type(answer_raw) | |
| image_ref = str(row["img"]) | |
| rel, digest = images.store(resolve(image_ref)) | |
| table = row.get("table") | |
| extra: dict[str, Any] = {} | |
| if table is not None: | |
| extra["table_sha256"] = sha256_bytes(canonical_json(table).encode("utf-8")) | |
| native_id = str(row.get("id", row.get("index"))) | |
| return make_item( | |
| source=NAME, | |
| source_revision=revision, | |
| source_config=config, | |
| source_split=split, | |
| source_native_id=native_id, | |
| question=question, | |
| choices=(), | |
| answer_raw=answer_raw, | |
| answer_canonical=answer_raw, | |
| answer_type=answer_type, | |
| image_paths=(rel,), | |
| image_sha256=(digest,), | |
| policy=POLICY, | |
| native_row=row, | |
| extra_provenance=extra, | |
| ) | |