File size: 2,914 Bytes
0338a7c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Strict offline scoring of versioned answer manifests and saved choices."""

import argparse
import json
from pathlib import Path


DOMAINS = {"mc4": {"A", "B", "C", "D"}, "binary": {"是", "否"}}


def normalize(value):
    if not isinstance(value, str):
        raise ValueError("Answers must be strings")
    return value.strip().upper()


def read_rows(path):
    with Path(path).open(encoding="utf-8") as stream:
        rows = [json.loads(line) for line in stream if line.strip()]
    if not rows:
        raise ValueError("Input must not be empty")
    indexed = {}
    for row in rows:
        item_id = row["item_id"]
        if not isinstance(item_id, str) or not item_id:
            raise ValueError("item_id must be a nonempty string")
        if item_id in indexed:
            raise ValueError("Duplicate item_id: " + item_id)
        indexed[item_id] = row
    return indexed


def score(manifest_path, predictions_path):
    manifest = read_rows(manifest_path)
    predictions = read_rows(predictions_path)
    if manifest.keys() != predictions.keys():
        raise ValueError(
            "ID coverage mismatch: missing=%d unexpected=%d"
            % (len(manifest.keys() - predictions.keys()), len(predictions.keys() - manifest.keys()))
        )
    by_task = {}
    invalid = 0
    for item_id, expected in manifest.items():
        prediction = predictions[item_id]
        if expected["task_name"] != prediction["task_name"]:
            raise ValueError("Task mismatch: " + item_id)
        domain = DOMAINS[expected["answer_type"]]
        truth = normalize(expected["ground_truth"])
        answer = normalize(prediction["model_answer"])
        if truth not in domain:
            raise ValueError("Ground truth outside answer domain: " + item_id)
        if answer and answer not in domain:
            raise ValueError("Prediction outside answer domain: " + item_id)
        invalid += not answer
        task = by_task.setdefault(expected["task_name"], {"correct": 0, "total": 0})
        task["total"] += 1
        task["correct"] += int(answer == truth)
    for counts in by_task.values():
        counts["accuracy_percent"] = 100 * counts["correct"] / counts["total"]
    correct = sum(counts["correct"] for counts in by_task.values())
    return {
        "correct": correct,
        "total": len(manifest),
        "accuracy_percent": 100 * correct / len(manifest),
        "empty_incorrect_predictions": invalid,
        "by_task": dict(sorted(by_task.items())),
    }


def main():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--manifest", required=True, type=Path)
    parser.add_argument("--predictions", required=True, type=Path)
    arguments = parser.parse_args()
    print(json.dumps(score(arguments.manifest, arguments.predictions), ensure_ascii=False, indent=2))


if __name__ == "__main__":
    main()