File size: 1,677 Bytes
d37698e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""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)