File size: 2,381 Bytes
f9bc0c1 | 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 | from __future__ import annotations
import importlib.util
from pathlib import Path
import pytest
ROOT = Path(__file__).resolve().parents[1]
SPEC = importlib.util.spec_from_file_location(
"audit_ctc_label_feasibility",
ROOT / "tools" / "audit_ctc_label_feasibility.py",
)
assert SPEC and SPEC.loader
MODULE = importlib.util.module_from_spec(SPEC)
SPEC.loader.exec_module(MODULE)
def test_ctc_min_timesteps_counts_adjacent_equal_tokens() -> None:
assert MODULE.ctc_min_timesteps("πππ«") == 4
assert MODULE.ctc_min_timesteps("ππ«π") == 3
assert MODULE.ctc_min_timesteps("πππ") == 5
def test_report_fails_impossible_label_and_passes_wider_pool(tmp_path: Path) -> None:
dictionary = tmp_path / "dict.txt"
dictionary.write_text("π\nπ«\n", encoding="utf-8")
labels = tmp_path / "labels.txt"
labels.write_text("a.png\tπππ«\nb.png\tππ«\n", encoding="utf-8")
failed = MODULE.build_report(
dictionary=dictionary,
label_files=[("train", labels)],
pool_width=3,
max_text_length=2,
use_space_char=False,
)
assert failed["status"] == "FAIL"
assert failed["splits"][0]["over_pool_width"] == 1
assert failed["splits"][0]["over_max_text_length"] == 1
passed = MODULE.build_report(
dictionary=dictionary,
label_files=[("train", labels)],
pool_width=4,
max_text_length=3,
use_space_char=False,
)
assert passed["status"] == "PASS"
def test_multicodepoint_dictionary_is_rejected(tmp_path: Path) -> None:
dictionary = tmp_path / "dict.txt"
dictionary.write_text("ππ\n", encoding="utf-8")
with pytest.raises(ValueError, match="exactly one codepoint"):
MODULE.audit_dictionary(dictionary, use_space_char=False)
def test_unknown_label_codepoint_is_a_failure(tmp_path: Path) -> None:
dictionary = tmp_path / "dict.txt"
dictionary.write_text("π\n", encoding="utf-8")
labels = tmp_path / "labels.txt"
labels.write_text("a.png\tπ\u00a0π\n", encoding="utf-8")
report = MODULE.build_report(
dictionary=dictionary,
label_files=[("train", labels)],
pool_width=10,
max_text_length=10,
use_space_char=True,
)
assert report["status"] == "FAIL"
assert report["splits"][0]["unknown_codepoints"] == ["U+00A0"]
|