Datasets:
Download src/explicit_learning/dsl/plotdsl.py from sungguk/visual-answerability: direct link, hf CLI and curl.
- Browser
- Download file 7.37 kB
-
https://huggingface.co/datasets/sungguk/visual-answerability/resolve/main/src/explicit_learning/dsl/plotdsl.py
- Command line
-
hf download hf://datasets/sungguk/visual-answerability/src/explicit_learning/dsl/plotdsl.py
-
curl -L -o plotdsl.py https://huggingface.co/datasets/sungguk/visual-answerability/resolve/main/src/explicit_learning/dsl/plotdsl.py
7.37 kB
| """``plotqa_dsl_v1`` operator table + dataset-level PlotQA program compiler | |
| (``docs/02`` §5.2, ``docs/07`` §4 P2). | |
| The compiler is **dataset-level** (per-template, not per-example): each allowlisted | |
| PlotQA template maps to a fixed typed-AST shape built in code from the QA's slot | |
| metadata. No per-example human labeling, no execution, no model calls — those are | |
| P3/P4. The ``TEMPLATE_DSL_MAPPING`` strings in :mod:`~explicit_learning.sources.plotqa` | |
| are the human-readable reference for the same shapes built here as typed | |
| :class:`~explicit_learning.dsl.ast.AstNode` trees. | |
| Slot extraction reads ``item.provenance["slots"]`` (the QA's ``series`` / ``x`` / | |
| ``x1`` / ``x2`` slots, carried through ``normalize``). The series label is resolved | |
| to its stable world id (``series:0``) so the program carries a real entity ``REF``; | |
| the x value is a literal string (x-axis values are not tracked entities). An | |
| unresolvable slot yields ``compile_status="unsupported"`` with a ``reason_code`` — | |
| never a guessed program (``docs/02`` §5.2, runbook: no faked ``compiled``). | |
| """ | |
| from __future__ import annotations | |
| from dataclasses import replace | |
| from typing import Any | |
| from ..ingest.base import NormalizedItem | |
| from ..sources.base import World | |
| from .ast import AstNode, Dsl, Program, lit, ref | |
| from .canonicalize import canonical_program_sha256 | |
| from .typecheck import OpSignature, typecheck | |
| DSL: Dsl = "plotqa_dsl_v1" | |
| # --- operator signature table (§5.2) --------------------------------------- | |
| # | |
| # ``selector_kind``: "scalar" (exactly one referent), "set" (zero or more, so a | |
| # zero-cardinality referent is NOT invalid — §11.3), or "none" (a value). | |
| # ``LOOKUP``/``SUBTRACT`` are order-significant; ``SUM`` is variadic-commutative. | |
| SIGNATURES: dict[str, OpSignature] = { | |
| "SELECT_SERIES": OpSignature( | |
| arg_types=("series",), return_type="series", selector_kind="scalar" | |
| ), | |
| "SELECT_X": OpSignature(arg_types=("string",), return_type="x", selector_kind="scalar"), | |
| "LOOKUP": OpSignature(arg_types=("series", "x"), return_type="value", selector_kind="none"), | |
| "POINTS": OpSignature(arg_types=("series",), return_type="points", selector_kind="set"), | |
| "ARGMAX": OpSignature( | |
| arg_types=("points", "string"), return_type="point", selector_kind="scalar" | |
| ), | |
| "ARGMIN": OpSignature( | |
| arg_types=("points", "string"), return_type="point", selector_kind="scalar" | |
| ), | |
| "SUBTRACT": OpSignature( | |
| arg_types=("value", "value"), return_type="value", selector_kind="none" | |
| ), | |
| "SUM": OpSignature( | |
| arg_types=("any",), | |
| return_type="value", | |
| commutative=True, | |
| variadic=True, | |
| selector_kind="none", | |
| ), | |
| "COUNT": OpSignature(arg_types=("points",), return_type="integer", selector_kind="set"), | |
| "EXIST": OpSignature(arg_types=("points",), return_type="boolean", selector_kind="set"), | |
| } | |
| # Canonicalization profile (§7.2 steps 3–5). | |
| COMMUTATIVE_OPS: frozenset[str] = frozenset({"SUM"}) | |
| ALIASES: dict[str, str] = {"GET_VALUE": "LOOKUP", "MAX": "ARGMAX", "MIN": "ARGMIN"} | |
| IDENTITY_CASTS: frozenset[str] = frozenset({"TO_VALUE"}) | |
| _UNSUPPORTED_NODE = AstNode(op="UNSUPPORTED", args=(), return_type="") | |
| # --- compile --------------------------------------------------------------- | |
| def compile_plotqa(item: NormalizedItem, world: World) -> Program: | |
| """Build the typed ``plotqa_dsl_v1`` program for one normalized PlotQA item. | |
| Returns a :class:`Program` with ``compile_status="compiled"`` (type-checked, | |
| canonical hash filled) or ``"unsupported"`` (with a ``reason_code``) when a | |
| slot is missing/unresolvable. Never guesses a program. | |
| """ | |
| prov = item.provenance | |
| template = prov.get("template") | |
| slots = prov.get("slots") or {} | |
| series_id = _series_id_for_label(world, slots.get("series")) | |
| if template == "T_LOOKUP": | |
| x = slots.get("x") | |
| if series_id is None or x is None: | |
| return _unsupported(item, "t_lookup_missing_series_or_x") | |
| program = _lookup(series_id, str(x)) | |
| referent = _select_series(series_id) | |
| elif template == "T_DIFFERENCE": | |
| x1, x2 = slots.get("x1"), slots.get("x2") | |
| if series_id is None or x1 is None or x2 is None: | |
| return _unsupported(item, "t_difference_missing_slots") | |
| program = AstNode( | |
| op="SUBTRACT", | |
| args=(_lookup(series_id, str(x1)), _lookup(series_id, str(x2))), | |
| return_type="value", | |
| ) | |
| referent = _select_series(series_id) | |
| elif template in ("T_MAX_YEAR", "T_MIN_YEAR"): | |
| if series_id is None: | |
| return _unsupported(item, f"{template.lower()}_missing_series") | |
| op = "ARGMAX" if template == "T_MAX_YEAR" else "ARGMIN" | |
| program = AstNode(op=op, args=(_points(series_id), "value"), return_type="point") | |
| referent = _select_series(series_id) | |
| elif template == "T_SUM": | |
| if series_id is None: | |
| return _unsupported(item, "t_sum_missing_series") | |
| program = AstNode(op="SUM", args=(_points(series_id),), return_type="value") | |
| referent = _select_series(series_id) | |
| else: | |
| return _unsupported(item, f"template_{template}_not_compilable") | |
| program_node = typecheck( | |
| Program( | |
| dsl=DSL, | |
| program=program, | |
| base_id=item.base_id, | |
| question_sha256=item.question_sha256, | |
| choices_sha256=item.choices_sha256, | |
| compile_status="compiled", | |
| referent_selector=referent, | |
| required_referent_cardinality=1, | |
| ), | |
| world=world, | |
| ) | |
| return replace( | |
| program_node, canonical_program_sha256=canonical_program_sha256(program_node, world=world) | |
| ) | |
| # --- AST builders ---------------------------------------------------------- | |
| def _select_series(series_id: str) -> AstNode: | |
| return AstNode( | |
| op="SELECT_SERIES", args=(ref(series_id, return_type="series"),), return_type="series" | |
| ) | |
| def _select_x(x_label: str) -> AstNode: | |
| return AstNode(op="SELECT_X", args=(lit(x_label, return_type="string"),), return_type="x") | |
| def _lookup(series_id: str, x_label: str) -> AstNode: | |
| return AstNode( | |
| op="LOOKUP", args=(_select_series(series_id), _select_x(x_label)), return_type="value" | |
| ) | |
| def _points(series_id: str) -> AstNode: | |
| return AstNode(op="POINTS", args=(_select_series(series_id),), return_type="points") | |
| def _unsupported(item: NormalizedItem, reason: str) -> Program: | |
| return Program( | |
| dsl=DSL, | |
| program=_UNSUPPORTED_NODE, | |
| base_id=item.base_id, | |
| question_sha256=item.question_sha256, | |
| choices_sha256=item.choices_sha256, | |
| compile_status="unsupported", | |
| reason_code=reason, | |
| ) | |
| # --- world helpers --------------------------------------------------------- | |
| def _series_id_for_label(world: World, label: Any) -> str | None: | |
| """Resolve a series label to its stable world id (``series:0``); None if absent.""" | |
| if not isinstance(label, str) or not label: | |
| return None | |
| for series in world.get("series", []) or []: | |
| if isinstance(series, dict) and series.get("label") == label and series.get("id"): | |
| return str(series["id"]) | |
| return None | |
| __all__ = [ | |
| "ALIASES", | |
| "COMMUTATIVE_OPS", | |
| "DSL", | |
| "IDENTITY_CASTS", | |
| "SIGNATURES", | |
| "compile_plotqa", | |
| ] | |