File size: 2,615 Bytes
e1ced61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""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,
    )