Repository navigation
Expand file tree
/
Copy pathevaluate.py
More file actions
156 lines (137 loc) · 5.62 KB
/
Copy pathevaluate.py
File metadata and controls
156 lines (137 loc) · 5.62 KB
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
"""Compare answers with the ground truth in ``samples/questions.json``.
python scripts/evaluate.py
python scripts/evaluate.py --out eval.json
A value counts as correct when it matches the truth. Strings are compared with
the truth as a whole word, case-insensitively. Numbers within 0.05. Dates by
their ``YYYY-MM-DD`` prefix. "Prose" counts when the sentence also states that
value. "Chart" counts when a chart was drawn for a comparison question, and
when a single-number question correctly drew none.
The script always exits 0 after printing the table. A wrong answer is a result,
not a crash.
"""
from __future__ import annotations
import argparse
import json
import re
import sys
import time
from pathlib import Path
from table_qa.config import Settings
from table_qa.pipeline import run_question
ROOT = Path(__file__).resolve().parent.parent
def same_text(pred, truth: str) -> bool:
def norm(value) -> str:
return re.sub(r"[^a-z0-9]+", " ", str(value).lower()).strip()
got, want = norm(pred), norm(truth)
if not want:
return got == want
return got == want or f" {want} " in f" {got} "
def same(pred, truth, kind: str) -> bool:
if kind == "number":
try:
return abs(float(pred) - float(truth)) <= 0.05
except (TypeError, ValueError):
return False
if kind == "date":
return str(pred)[:10] == str(truth)[:10]
return same_text(pred, str(truth))
def prose_mentions(prose: str, truth, kind: str) -> bool:
if kind == "number":
try:
number = float(truth)
except (TypeError, ValueError):
return False
compact = str(int(number)) if number.is_integer() else str(number)
return compact in str(prose).replace(",", "")
if kind == "date":
return str(truth)[:10] in str(prose)
return same_text(prose, str(truth))
def main() -> int:
parser = argparse.ArgumentParser()
parser.add_argument("--samples", default=str(ROOT / "samples"))
parser.add_argument("--out")
parser.add_argument("--only", help="run one question id")
args = parser.parse_args()
settings = Settings.from_env()
settings.validate()
spec = json.loads((Path(args.samples) / "questions.json").read_text())
questions = spec["questions"]
if args.only:
questions = [item for item in questions if item["id"] == args.only]
if not questions:
print(f"no question with id {args.only}", file=sys.stderr)
return 2
rows, raw = [], []
totals = {"value": 0, "prose": 0, "chart": 0, "n": 0}
for item in questions:
path = Path(args.samples) / item["file"]
started = time.time()
try:
result = run_question(path, item["question"], settings)
except Exception as exc: # report and keep going
rows.append((item["id"], "error", "no", "no", time.time() - started, str(exc)))
totals["n"] += 1
raw.append({"id": item["id"], "error": str(exc)})
print(f"{item['id']}: error: {exc}", file=sys.stderr)
continue
value_ok = bool(result.ok) and same(result.value, item["answer"], item["type"])
prose_ok = bool(result.ok) and prose_mentions(result.answer, item["answer"], item["type"])
chart_ok = bool(result.chart_png) == bool(item["expect_chart"])
totals["value"] += int(value_ok)
totals["prose"] += int(prose_ok)
totals["chart"] += int(chart_ok)
totals["n"] += 1
detail = "" if value_ok else f"got {result.value!r}, want {item['answer']!r}"
if result.error and not result.ok:
detail = result.error.splitlines()[-1][:180]
rows.append(
(
item["id"],
"yes" if value_ok else "no",
"yes" if prose_ok else "no",
"yes" if chart_ok else "no",
time.time() - started,
detail,
)
)
raw.append(
{
"id": item["id"],
"file": item["file"],
"question": item["question"],
"expected": item["answer"],
"value": result.value,
"answer": result.answer,
"value_ok": value_ok,
"prose_ok": prose_ok,
"chart": bool(result.chart_png),
"chart_ok": chart_ok,
"steps": result.steps,
"code": result.code,
"error": result.error,
"elapsed": round(result.elapsed, 3),
"model": result.model,
"backend": result.backend,
"answer_source": result.answer_source,
}
)
print(
f"{item['id']}: value={'yes' if value_ok else 'no'} prose={'yes' if prose_ok else 'no'} "
f"chart={'yes' if chart_ok else 'no'} {time.time() - started:.1f}s",
file=sys.stderr,
)
print(f"\nBackend: {settings.backend}, model: {settings.model_name}\n")
print("| Question | Value | Prose | Chart | Time (s) | Detail |")
print("|---|---|---|---|---|---|")
for qid, value, prose, chart, elapsed, detail in rows:
print(f"| {qid} | {value} | {prose} | {chart} | {elapsed:.1f} | {detail or '-'} |")
n = max(totals["n"], 1)
print(
f"| **All** | **{totals['value']}/{totals['n']}** | {totals['prose']}/{totals['n']} | "
f"{totals['chart']}/{totals['n']} | | |"
)
if args.out:
Path(args.out).write_text(json.dumps({"model": settings.model_name, "backend": settings.backend, "results": raw}, indent=2) + "\n")
return 0
if __name__ == "__main__":
sys.exit(main())