121 lines
4.6 KiB
Python
121 lines
4.6 KiB
Python
#!/usr/bin/env python3
|
||
"""Validate whether running a package correlates with a better record.
|
||
|
||
All decks are 7-win trophies, so record in [0.78, 1.0]. We compare, per
|
||
package, the mean record and 7-0 rate across "dose" buckets (how many of the
|
||
package's cards the deck runs).
|
||
|
||
Outputs: value/<SNAPSHOT>/reports/package_impact.md
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import argparse
|
||
import json
|
||
import statistics
|
||
from collections import Counter, defaultdict
|
||
from pathlib import Path
|
||
|
||
|
||
def load_jsonl(path: Path):
|
||
with path.open(encoding="utf-8") as fh:
|
||
for line in fh:
|
||
line = line.strip()
|
||
if line:
|
||
yield json.loads(line)
|
||
|
||
|
||
def primary_pair(colors):
|
||
if not colors:
|
||
return None
|
||
order = "WUBRG"
|
||
cs = [c for c in order if c in colors.upper()]
|
||
return "".join(cs[:2]) if len(cs) >= 2 else None
|
||
|
||
|
||
def build(snapshot: Path, out_dir: Path) -> None:
|
||
cards = {c["name"]: c for c in
|
||
json.loads((snapshot / "stats/card_data.json").read_text(encoding="utf-8"))["data"]}
|
||
id2name, meta = {}, {}
|
||
for rec in load_jsonl(snapshot / "drafts/draft_picks.jsonl"):
|
||
for cid, m in (rec.get("cards") or {}).items():
|
||
cid = int(cid)
|
||
id2name[cid] = m.get("name")
|
||
meta.setdefault(m.get("name"), dict(m))
|
||
|
||
def is_land(nm):
|
||
return any("Land" in t for t in (meta.get(nm, {}).get("types") or []))
|
||
|
||
decks = []
|
||
for d in load_jsonl(snapshot / "trophies/trophies_decks.jsonl"):
|
||
names = set()
|
||
for e in d["maindeck"]:
|
||
nm = id2name.get(e["mtga_id"])
|
||
if nm in cards and not is_land(nm):
|
||
names.add(nm)
|
||
wins, losses = d["wins"], d["losses"]
|
||
decks.append({"names": names, "wins": wins, "losses": losses,
|
||
"record": wins / (wins + losses) if wins + losses else None,
|
||
"seven0": losses == 0, "arch": primary_pair(d.get("colors"))})
|
||
|
||
pkg = json.loads((out_dir / "packages.json").read_text(encoding="utf-8"))["packages"]
|
||
baseline = statistics.mean(d["record"] for d in decks)
|
||
base70 = sum(1 for d in decks if d["seven0"]) / len(decks)
|
||
|
||
rep = ["# FRA PremierDraft · 卡牌包战绩贡献\n",
|
||
f"基线(全部 {len(decks)} 副奖杯):平均记录 **{baseline:.3f}**,7-0 率 **{base70*100:.1f}%**。\n",
|
||
"> 「剂量」= 该副牌运行了包里几张牌。记录越高、7-0 率越高越好。\n"]
|
||
|
||
for p in pkg:
|
||
pc = set(p["cards"])
|
||
buckets = defaultdict(list)
|
||
for d in decks:
|
||
k = len(d["names"] & pc)
|
||
buckets[k].append(d)
|
||
# only show buckets with >=8 decks, merge tail into "n+"
|
||
rows = sorted(buckets.items())
|
||
rep.append(f"\n## 包:{p['core']} (size={p['size']}, avg lift {p['avg_lift']})\n")
|
||
rep.append("| 运行张数 | n | 平均记录 | 7-0 率 | 平均 losses |")
|
||
rep.append("|---|---|---|---|---|")
|
||
for k, ds in rows:
|
||
if len(ds) < 5:
|
||
continue
|
||
rec = statistics.mean(d["record"] for d in ds if d["record"] is not None)
|
||
s70 = sum(1 for d in ds if d["seven0"]) / len(ds)
|
||
ml = statistics.mean(d["losses"] for d in ds)
|
||
rep.append(f"| {k} | {len(ds)} | {rec:.3f} | {s70*100:.1f}% | {ml:.2f} |")
|
||
rep.append("")
|
||
|
||
# control: blue staples (high inclusion, low lift) as a contrast
|
||
blue_staples = ["Surveillance Phantasm", "Mindseeker Oculus", "Icy Reception", "Unsummon"]
|
||
buckets = defaultdict(list)
|
||
for d in decks:
|
||
buckets[len(d["names"] & set(blue_staples))].append(d)
|
||
rep.append("\n## 对照:蓝色通用件(高采纳、低 lift)\n")
|
||
rep.append("| 运行张数 | n | 平均记录 | 7-0 率 |")
|
||
rep.append("|---|---|---|---|")
|
||
for k, ds in sorted(buckets.items()):
|
||
if len(ds) < 5:
|
||
continue
|
||
rep.append(f"| {k} | {len(ds)} | "
|
||
f"{statistics.mean(d['record'] for d in ds if d['record'] is not None):.3f} | "
|
||
f"{sum(1 for d in ds if d['seven0'])/len(ds)*100:.1f}% |")
|
||
|
||
(out_dir / "reports").mkdir(parents=True, exist_ok=True)
|
||
(out_dir / "reports/package_impact.md").write_text("\n".join(rep), encoding="utf-8")
|
||
print("wrote reports/package_impact.md")
|
||
print(f"baseline record={baseline:.3f} 7-0={base70*100:.1f}%")
|
||
|
||
|
||
def main() -> int:
|
||
ap = argparse.ArgumentParser()
|
||
ap.add_argument("--snapshot", required=True)
|
||
ap.add_argument("--out", required=True)
|
||
args = ap.parse_args()
|
||
build(Path(args.snapshot), Path(args.out))
|
||
return 0
|
||
|
||
|
||
if __name__ == "__main__":
|
||
raise SystemExit(main())
|