| """Verify materialized parquet rows match construct() from gold.""" |
|
|
| from __future__ import annotations |
|
|
| from typing import Any, Mapping, Sequence |
|
|
| from localization.construct import ( |
| DEFAULT_SEED, |
| label_specs_from_segments, |
| localization_prompt, |
| ) |
| from localization.schema import GoldSegment, LabelSpec |
|
|
|
|
| def _as_gold_segments(raw: Sequence[Mapping[str, Any]]) -> list[GoldSegment]: |
| return [GoldSegment.from_dict(dict(item)) for item in raw] |
|
|
|
|
| def _as_specs(raw: Sequence[Mapping[str, Any]]) -> list[LabelSpec]: |
| return [LabelSpec.from_dict(dict(item)) for item in raw] |
|
|
|
|
| def verify_row(row: Mapping[str, Any], *, seed: int = DEFAULT_SEED) -> None: |
| episode_id = str(row["id"]) |
| instruction = str(row.get("instruction") or "") |
| gold = _as_gold_segments(row["gold_segments"]) |
| stored_specs = _as_specs(row["label_specs"]) |
| recomputed = label_specs_from_segments(episode_id, gold, seed=seed) |
| if [(s.label, s.multiplicity) for s in recomputed] != [ |
| (s.label, s.multiplicity) for s in stored_specs |
| ]: |
| raise AssertionError( |
| f"{episode_id}: label_specs mismatch " |
| f"stored={[(s.label, s.multiplicity) for s in stored_specs]} " |
| f"recomputed={[(s.label, s.multiplicity) for s in recomputed]}" |
| ) |
| expected_prompt = localization_prompt(instruction, recomputed) |
| stored_prompt = str(row["prompt_text"]) |
| if stored_prompt != expected_prompt: |
| raise AssertionError(f"{episode_id}: prompt_text mismatch") |
|
|
|
|
| def verify_rows(rows: Sequence[Mapping[str, Any]], *, seed: int = DEFAULT_SEED) -> int: |
| for row in rows: |
| verify_row(row, seed=seed) |
| return len(rows) |
|
|