-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy patheval.py
More file actions
256 lines (235 loc) ยท 10.5 KB
/
Copy patheval.py
File metadata and controls
256 lines (235 loc) ยท 10.5 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
cat > /gz-data/eval.py << 'PYEOF'
#!/usr/bin/env python3
"""
eval.py
python eval.py # ๅ
จ้
python eval.py --n 5 # ๅ 5 ๆก
python eval.py --resume # ๆญ็น็ปญ่ท
python eval.py --plot-only # ๅช้็ปๅพ
python eval.py --shard 0 --total-shards 4 # ๅนถๅๅ็
"""
import argparse, json, re, time
from pathlib import Path
import numpy as np
DATA_DIR = Path("/gz-data")
TESTSET = DATA_DIR / "testset.json"
RESULT_F = DATA_DIR / "eval_results.jsonl"
SUMMARY_F = DATA_DIR / "eval_summary.json"
REPORT_DIR = DATA_DIR / "eval_report"
REPORT_DIR.mkdir(exist_ok=True)
FOV_RANGE = (4.0, 59.0)
FNUM_RANGE = (1.6, 6.0)
APER_RANGE = (2.0, 12.0)
RMS_MAX = 0.5
TOL_FOV, TOL_FNUM, TOL_APER = 5.0, 0.5, 3.0
from agent import run_agent
def parse_prediction(text: str) -> dict:
fa = re.search(r"Final Answer[::](.*)", text, re.DOTALL | re.IGNORECASE)
t = fa.group(1) if fa else text
pred = {}
fov_all = re.findall(r"FOV[=:][^0-9]*([0-9]+\.?[0-9]*)", t, re.IGNORECASE)
if fov_all:
pred["fov"] = float(fov_all[-1])
fn_all = re.findall(r"F/([0-9]+\.?[0-9]*)", t, re.IGNORECASE)
if fn_all:
pred["fnum"] = float(fn_all[-1])
rm = re.search(r"RMS[^0-9]*([0-9]+\.[0-9]+(?:[eE][+-]?[0-9]+)?)\s*mm", t, re.IGNORECASE)
if rm:
pred["rms"] = float(rm.group(1))
return pred
def norm_err(val, pred, lo, hi):
if val is None or pred is None:
return None
return abs(val - pred) / (hi - lo)
def score_item(item: dict, pred: dict) -> dict:
c = item["constraints"]
s = {}
if "fov" in c and "fov" in pred:
s["fov_err"] = norm_err(c["fov"], pred["fov"], *FOV_RANGE)
elif "fov_min" in c and "fov" in pred:
s["fov_err"] = norm_err((c["fov_min"]+c["fov_max"])/2, pred["fov"], *FOV_RANGE)
if "fnum" in c and "fnum" in pred:
s["fnum_err"] = norm_err(c["fnum"], pred["fnum"], *FNUM_RANGE)
elif "fnum_min" in c and "fnum" in pred:
s["fnum_err"] = norm_err((c["fnum_min"]+c["fnum_max"])/2, pred["fnum"], *FNUM_RANGE)
if c.get("aper", 0) > 0 and "aper" in pred:
s["aper_err"] = norm_err(c["aper"], pred["aper"], *APER_RANGE)
if "rms" in pred:
s["rms_pred"] = pred["rms"]
s["rms_norm"] = min(pred["rms"] / RMS_MAX, 1.0)
errs = [v for k, v in s.items() if k.endswith("_err") and v is not None]
s["norm_sum"] = sum(errs) / len(errs) if errs else None
if not pred:
return {**s, "hit": 0}
hit = True
if "fov" in c and "fov" in pred: hit &= abs(c["fov"] - pred["fov"]) <= TOL_FOV
if "fnum" in c and "fnum" in pred: hit &= abs(c["fnum"] - pred["fnum"]) <= TOL_FNUM
if c.get("aper", 0) > 0 and "aper" in pred:
hit &= abs(c["aper"] - pred["aper"]) <= TOL_APER
if "rms_target" in c and "rms" in pred:
hit &= pred["rms"] <= c["rms_target"]
if "fov_min" in c and "fov" in pred:
hit &= c["fov_min"] <= pred["fov"] <= c["fov_max"]
if "fnum_min" in c and "fnum" in pred:
hit &= c["fnum_min"] <= pred["fnum"] <= c["fnum_max"]
s["hit"] = int(hit)
return s
def plot_results(results: list):
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
TYPES = ["in_domain", "out_of_domain", "partial", "range", "specific"]
COLORS = ["#4C72B0", "#DD8452", "#55A868", "#C44E52", "#8172B2"]
def avg(key):
out = {}
for t in TYPES:
vals = [r["scores"][key] for r in results
if r["type"] == t and r["scores"].get(key) is not None]
out[t] = float(np.mean(vals)) if vals else 0.0
return out
metrics = [
("fov_err", "FOV Norm Error"),
("fnum_err", "Fnum Norm Error"),
("norm_sum", "Overall Norm Error"),
("hit", "Hit Rate"),
]
fig, axes = plt.subplots(2, 2, figsize=(14, 10))
fig.suptitle("LensAgent Baseline Evaluation", fontsize=14, fontweight="bold")
for ax, (key, title) in zip(axes.flat, metrics):
d = avg(key)
bars = ax.bar(d.keys(), d.values(), color=COLORS, width=0.5)
ax.set_title(title)
ax.set_ylim(0, 1.15)
ax.tick_params(axis="x", rotation=20)
for b, v in zip(bars, d.values()):
ax.text(b.get_x() + b.get_width()/2, b.get_height() + 0.02,
f"{v:.3f}", ha="center", fontsize=9)
plt.tight_layout()
out = REPORT_DIR / "baseline_metrics.png"
plt.savefig(out, dpi=150, bbox_inches="tight")
print(f"๐ ๅพ่กจๅทฒไฟๅญ: {out}")
def summarize(results):
TYPES = ["in_domain", "out_of_domain", "partial", "range", "specific"]
summary = {"total": len(results), "by_type": {}}
for t in TYPES:
sub = [r for r in results if r["type"] == t]
if not sub:
continue
hits = [r["scores"].get("hit", 0) for r in sub]
norms = [r["scores"]["norm_sum"] for r in sub if r["scores"].get("norm_sum") is not None]
rmss = [r["scores"]["rms_pred"] for r in sub if r["scores"].get("rms_pred") is not None]
summary["by_type"][t] = {
"n": len(sub),
"hit_rate": round(float(np.mean(hits)), 3),
"avg_norm_sum": round(float(np.mean(norms)), 4) if norms else None,
"avg_rms": round(float(np.mean(rmss)), 4) if rmss else None,
}
overall = float(np.mean([r["scores"].get("hit", 0) for r in results]))
summary["overall_hit_rate"] = round(overall, 3)
return summary, overall
def main():
p = argparse.ArgumentParser()
p.add_argument("--n", type=int, default=None)
p.add_argument("--resume", action="store_true")
p.add_argument("--plot-only", action="store_true")
p.add_argument("--shard", type=int, default=None, help="ๅ็็ดขๅผ 0-based")
p.add_argument("--total-shards", type=int, default=4, help="ๆปๅ็ๆฐ")
p.add_argument("--merge", action="store_true", help="ๅๅนถๆๆๅ็็ปๆ")
args = p.parse_args()
# โโ merge ๆจกๅผ๏ผๅๅนถๅ็็ปๆๅนถ็ๆๆฑๆป โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
if args.merge:
results = []
for i in range(args.total_shards):
f = DATA_DIR / f"eval_results_shard{i}.jsonl"
if f.exists():
for line in f.read_text().splitlines():
if line.strip():
results.append(json.loads(line))
print(f"ๅๅนถ {len(results)} ๆก็ปๆ")
RESULT_F.write_text(
"\n".join(json.dumps(r, ensure_ascii=False) for r in results),
encoding="utf-8"
)
summary, overall = summarize(results)
SUMMARY_F.write_text(json.dumps(summary, ensure_ascii=False, indent=2), encoding="utf-8")
print(f"ๆปๅฝไธญ็: {overall:.1%} ({len(results)} ๆก)")
for t, s in summary["by_type"].items():
print(f" {t:15s}: hit={s['hit_rate']:.1%} norm_sum={s['avg_norm_sum']} rms={s['avg_rms']}")
plot_results(results)
return
# โโ plot-only ๆจกๅผ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
if args.plot_only:
results = [json.loads(l) for l in RESULT_F.read_text().splitlines() if l.strip()]
plot_results(results)
return
# โโ ็กฎๅฎ่พๅบๆไปถๅๆฐๆฎ่ๅด โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
items = json.loads(TESTSET.read_text(encoding="utf-8"))
if args.n:
items = items[:args.n]
if args.shard is not None:
# ๅ็๏ผๆฏไธช shard ๅ่ชๅทฑ็้ฃๆฎต
items = [x for i, x in enumerate(items) if i % args.total_shards == args.shard]
out_file = DATA_DIR / f"eval_results_shard{args.shard}.jsonl"
print(f"[Shard {args.shard}/{args.total_shards}] {len(items)} ๆก")
else:
out_file = RESULT_F
# ๆญ็น็ปญ่ท
done_ids = set()
if (args.resume or args.shard is not None) and out_file.exists():
for line in out_file.read_text().splitlines():
if line.strip():
done_ids.add(json.loads(line)["id"])
print(f"ๅทฒๅฎๆ {len(done_ids)} ๆก๏ผ่ทณ่ฟ")
results = []
mode = "a" if (args.resume or args.shard is not None) else "w"
with open(out_file, mode, encoding="utf-8") as fout:
for i, item in enumerate(items):
iid = item.get("id", i)
if iid in done_ids:
continue
q = item["query"]
print(f"[{i+1}/{len(items)}] [{item['type']:15s}] {q[:55]}")
t0 = time.time()
try:
response = run_agent(q)
status = "ok"
except Exception as e:
response = f"ERROR: {e}"
status = "error"
print(f" โ {e}")
elapsed = round(time.time() - t0, 1)
pred = parse_prediction(response)
scores = score_item(item, pred)
row = {
"id": iid,
"type": item["type"],
"query": q,
"response": response[:800],
"pred": pred,
"scores": scores,
"status": status,
"elapsed": elapsed,
}
results.append(row)
fout.write(json.dumps(row, ensure_ascii=False) + "\n")
fout.flush()
ns = scores.get("norm_sum")
ns_str = f"{ns:.3f}" if ns is not None else "-"
hit_icon = "โ
" if scores.get("hit") else "โ"
print(f" {hit_icon} pred={pred} norm_sum={ns_str} [{elapsed}s]")
# ้ๅ็ๆจกๅผๆ่ชๅจๆฑๆป
if args.shard is None:
if not results:
results = [json.loads(l) for l in out_file.read_text().splitlines() if l.strip()]
summary, overall = summarize(results)
SUMMARY_F.write_text(json.dumps(summary, ensure_ascii=False, indent=2), encoding="utf-8")
print(f"\nๆปๅฝไธญ็: {overall:.1%} ({len(results)} ๆก)")
for t, s in summary["by_type"].items():
print(f" {t:15s}: hit={s['hit_rate']:.1%} norm_sum={s['avg_norm_sum']} rms={s['avg_rms']}")
plot_results(results)
else:
print(f"[Shard {args.shard}] ๅฎๆ {len(results)} ๆก โ {out_file}")
if __name__ == "__main__":
main()
PYEOF
echo "โ
eval.py ๅๅ
ฅๅฎๆ"