File size: 5,310 Bytes
b68f9f3
 
 
 
5ee4f7e
 
 
b68f9f3
 
 
 
5ee4f7e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b68f9f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5ee4f7e
 
 
 
 
 
 
 
 
 
 
 
 
 
b68f9f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5ee4f7e
b68f9f3
 
 
5ee4f7e
 
 
 
 
b68f9f3
 
 
 
 
 
 
 
5ee4f7e
 
b68f9f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
from __future__ import annotations

import io
import json
import os
import subprocess
import sys

from deberta_ime.evaluation_cli import run


def test_evaluation_cli_import_does_not_require_model_runtime() -> None:
    script = """
import builtins
real_import = builtins.__import__
def guarded_import(name, *args, **kwargs):
    if name == "torch" or name.startswith("transformers"):
        raise AssertionError(f"unexpected model runtime import: {name}")
    return real_import(name, *args, **kwargs)
builtins.__import__ = guarded_import
from deberta_ime.evaluation_cli import run
assert callable(run)
"""
    environment = {**os.environ, "PYTHONPATH": "src"}

    completed = subprocess.run(
        [sys.executable, "-c", script],
        cwd=os.getcwd(),
        env=environment,
        capture_output=True,
        text=True,
        check=False,
    )

    assert completed.returncode == 0, completed.stderr


def test_evaluation_cli_scores_finite_predictions_and_writes_receipts(tmp_path) -> None:
    items_path = tmp_path / "items.json"
    predictions_path = tmp_path / "predictions.json"
    items_path.write_text(
        json.dumps(
            [
                {
                    "id": "clean",
                    "input": "猫です",
                    "references": ["猫です"],
                    "label": "clean",
                },
                {
                    "id": "typo",
                    "input": "犬でし",
                    "references": ["犬です"],
                    "label": "typo",
                },
            ],
            ensure_ascii=False,
        ),
        encoding="utf-8",
    )
    predictions_path.write_text(
        json.dumps(
            [
                {
                    "id": "clean",
                    "candidates": [],
                    "provenance": "provider",
                    "reason": "baseline_best",
                    "margin": 0.0,
                },
                {
                    "id": "typo",
                    "candidates": ["犬です"],
                    "provenance": "deberta",
                    "reason": "accepted",
                    "margin": 1.25,
                },
            ],
            ensure_ascii=False,
        ),
        encoding="utf-8",
    )
    stdout = io.StringIO()

    exit_code = run(
        [
            "--items",
            str(items_path),
            "--predictions",
            str(predictions_path),
            "--dataset-name",
            "fixture-clean-typo",
            "--dataset-revision",
            "fixture-v1",
            "--dataset-license",
            "test-only",
            "--output-dir",
            str(tmp_path / "outputs"),
            "--stem",
            "fixture",
        ],
        stdout=stdout,
    )

    summary = json.loads(stdout.getvalue())
    report = json.loads((tmp_path / "outputs" / "fixture.json").read_text("utf-8"))
    assert exit_code == 0
    assert summary["ok"] is True
    assert report["schema_version"] == 2
    assert report["status"] == "LOCAL_FINITE_CANDIDATE_EVALUATION"
    assert report["evaluation"]["metrics"]["effective_acc_at_1"] == 1.0
    assert report["evaluation"]["metrics"]["overcorrection_rate"] == 0.0
    assert report["evaluation"]["metrics"]["declared_provenance_counts"] == {
        "deberta": 1,
        "provider": 1,
    }
    assert report["evaluation"]["metrics"]["mean_reported_margin"] == 0.625
    assert report["dataset"] == {
        "name": "fixture-clean-typo",
        "revision": "fixture-v1",
        "license": "test-only",
    }
    assert len(report["artifacts"]["items"]["sha256"]) == 64
    markdown = (tmp_path / "outputs" / "fixture.md").read_text("utf-8")
    assert "fixture-clean-typo" in markdown
    assert "Accepted candidate misses: 0" in markdown
    assert "Selection errors: 0" in markdown


def test_evaluation_cli_adapts_ajimee_without_inventing_clean_labels(tmp_path) -> None:
    items_path = tmp_path / "ajimee.json"
    predictions_path = tmp_path / "predictions.json"
    items_path.write_text(
        json.dumps(
            [
                {
                    "index": "7",
                    "input": "セイネンシ",
                    "expected_output": ["青年誌"],
                }
            ],
            ensure_ascii=False,
        ),
        encoding="utf-8",
    )
    predictions_path.write_text(
        json.dumps([{"index": "7", "candidates": ["青年誌"]}], ensure_ascii=False),
        encoding="utf-8",
    )

    run(
        [
            "--items",
            str(items_path),
            "--predictions",
            str(predictions_path),
            "--format",
            "ajimee",
            "--output-dir",
            str(tmp_path / "outputs"),
        ],
        stdout=io.StringIO(),
    )

    report = json.loads(
        (tmp_path / "outputs" / "finite_candidate_evaluation.json").read_text("utf-8")
    )
    assert report["evaluation"]["metrics"]["effective_acc_at_1"] == 1.0
    assert report["evaluation"]["metrics"]["overcorrection_rate"] is None
    assert report["evaluation"]["metrics"]["clean_rows"] == 0
    assert report["dataset"] == {
        "name": "AJIMEE-compatible input",
        "revision": "unverified-by-sha256",
        "license": "unspecified",
    }