| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | from __future__ import annotations |
| 10 | |
| 11 | import argparse |
| 12 | import json |
| 13 | import os |
| 14 | import sys |
| 15 | import time |
| 16 | |
| 17 | |
| 18 | def _tablo(points, esikler): |
| 19 | print(f"\n{'esik':>6}{'yanlis cekimser':>18}{'yanlis cevap':>16}") |
| 20 | for p in points: |
| 21 | if round(p.threshold, 4) not in esikler: |
| 22 | continue |
| 23 | d = p.to_dict() |
| 24 | fa, fc = d["false_abstention_rate"], d["false_answer_rate"] |
| 25 | f = lambda v: " -" if v is None else f"{v:6.3f}" |
| 26 | print(f"{p.threshold:6.2f}{f(fa):>18}{f(fc):>16}" |
| 27 | f" ({p.false_abstentions}/{p.n_answerable}, " |
| 28 | f"{p.false_answers}/{p.n_unanswerable})") |
| 29 | |
| 30 | |
| 31 | def main(argv=None) -> int: |
| 32 | ap = argparse.ArgumentParser(description="Cekimserlik esigi kalibrasyonu") |
| 33 | ap.add_argument("--golden", default="tests/golden/golden_10biy_v2.json") |
| 34 | ap.add_argument("--book", default=None) |
| 35 | ap.add_argument("--out-dir", default="tests/evaluation/results") |
| 36 | ap.add_argument("--max-false-abstention", type=float, default=0.05) |
| 37 | a = ap.parse_args(argv) |
| 38 | |
| 39 | from .abstain_calibration import candidates, collect_scores, report |
| 40 | from .golden_schema import split_dev_frozen |
| 41 | from .runner import BOOK_PATH, build_pipeline |
| 42 | |
| 43 | if not os.path.isfile(a.golden): |
| 44 | print(f"[hata] golden set yok: {a.golden}", file=sys.stderr) |
| 45 | return 2 |
| 46 | items = json.load(open(a.golden, encoding="utf-8"))["items"] |
| 47 | dev, frozen = split_dev_frozen(items) |
| 48 | print(f"[kalibrasyon] dev={len(dev)} frozen={len(frozen)}", flush=True) |
| 49 | |
| 50 | pipeline = build_pipeline(a.book or BOOK_PATH) |
| 51 | gen = pipeline["generator"] |
| 52 | |
| 53 | t0 = time.time() |
| 54 | sonuc = {} |
| 55 | for ad, kume in (("dev", dev), ("frozen", frozen)): |
| 56 | print(f"\n[kalibrasyon] {ad} kumesi ({len(kume)} item) olculuyor...", |
| 57 | flush=True) |
| 58 | |
| 59 | def ilerle(i, n, s, _ad=ad): |
| 60 | if i % 20 == 0 or i == n: |
| 61 | print(f" {_ad} {i}/{n}", flush=True) |
| 62 | |
| 63 | skorlar = collect_scores(gen, kume, on_progress=ilerle) |
| 64 | r = report(skorlar) |
| 65 | sonuc[ad] = r |
| 66 | |
| 67 | dev_rapor, frozen_rapor = sonuc["dev"], sonuc["frozen"] |
| 68 | adaylar = candidates(dev_rapor.points, |
| 69 | max_false_abstention=a.max_false_abstention) |
| 70 | |
| 71 | print("\n=== DEV (ayar burada yapilir) ===") |
| 72 | _tablo(dev_rapor.points, {0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8}) |
| 73 | print("\nsenaryo bazinda tepe skor dagilimi (dev):") |
| 74 | for k, v in dev_rapor.by_scenario.items(): |
| 75 | print(f" {k:<16}n={v['n']:<4}min={v['min']:<8}p10={v['p10']:<8}" |
| 76 | f"medyan={v['median']:<8}max={v['max']}") |
| 77 | |
| 78 | print("\n=== ADAY ISLETME NOKTALARI (dev uzerinde) ===") |
| 79 | for ad, d in adaylar.items(): |
| 80 | if d is None: |
| 81 | print(f" {ad:<34} YOK (kisiti saglayan esik bulunamadi)") |
| 82 | continue |
| 83 | print(f" {ad:<34} esik={d['threshold']:.2f} " |
| 84 | f"yanlis_cekimser={d['false_abstention_rate']} " |
| 85 | f"yanlis_cevap={d['false_answer_rate']}") |
| 86 | |
| 87 | print("\n=== FROZEN (yalniz RAPOR -- burada ayar YAPILMAZ) ===") |
| 88 | ilgi = {round(d["threshold"], 4) for d in adaylar.values() if d} |
| 89 | ilgi.add(0.30) |
| 90 | _tablo(frozen_rapor.points, ilgi) |
| 91 | |
| 92 | os.makedirs(a.out_dir, exist_ok=True) |
| 93 | damga = time.strftime("%Y%m%dT%H%M%SZ", time.gmtime()) |
| 94 | yol = os.path.join(a.out_dir, f"abstain_calibration_{damga}.json") |
| 95 | with open(yol, "w", encoding="utf-8") as f: |
| 96 | json.dump({"meta": {"golden": a.golden, "seconds": round(time.time() - t0, 1), |
| 97 | "current_default": 0.30, |
| 98 | "max_false_abstention": a.max_false_abstention}, |
| 99 | "dev": dev_rapor.to_dict(), "frozen": frozen_rapor.to_dict(), |
| 100 | "candidates": adaylar}, f, ensure_ascii=False, indent=2) |
| 101 | print(f"\n[kalibrasyon] {time.time() - t0:.1f}s · JSON: {yol}") |
| 102 | print("[kalibrasyon] ESIK SECILMEDI -- odunlesim olculdu, karar insana ait.") |
| 103 | return 0 |
| 104 | |
| 105 | |
| 106 | if __name__ == "__main__": |
| 107 | raise SystemExit(main()) |