import argparse import glob import json from collections import defaultdict from pathlib import Path MODELS = ["qwen3vl", "qwen25vl", "idefics3", "internvl3", "molmo"] CONDITIONS = ["T0", "Tpos", "Tneg", "Tabs", "Tunk"] ROOT = Path("/mnt/f70f6709-366e-49a0-861f-497645d98975/lyz/GraphEvolve") def load_jsonl(path): with open(path, encoding="utf-8") as fh: for line in fh: line = line.strip() if line: yield json.loads(line) def natural_truth(): truth = {} for key, path in ( ("spatialsense", ROOT / "runs/n_evidence_v1/gate0/source_manifest.jsonl"), ("vsr", ROOT / "runs/n_evidence_v1/gate3/manifest_vsr.jsonl"), ): for row in load_jsonl(path): truth[(key, row["annotation_id"])] = row["gold"] return truth def natural_table(truth, model): stats = defaultdict(lambda: {"n": 0, "correct": 0, "yes": 0}) for path in sorted(glob.glob(str(ROOT / f"n_full_v1/predictions/natural/*_{model}_shard*.jsonl"))): collection = Path(path).name.split("_")[0] for row in load_jsonl(path): gold = truth.get((collection, row["annotation_id"])) if gold is None: continue cell = stats[(collection, row["condition"])] cell["n"] += 1 cell["yes"] += int(row["answer"] == "yes") cell["correct"] += int(row["answer"] == gold) out = {} for (collection, condition), cell in stats.items(): if condition not in CONDITIONS: continue out.setdefault(collection, {})[condition] = { "n": cell["n"], "acc": cell["correct"] / cell["n"] if cell["n"] else None, "yes_rate": cell["yes"] / cell["n"] if cell["n"] else None, } return out def synth_table(model): path = ROOT / f"n_full_v1/analysis/synth_{model}.json" if not path.exists(): return None return json.loads(path.read_text(encoding="utf-8")) def gate_table(model, split="test"): path = ROOT / f"n_full_v1/analysis/vcg_{split}_{model}.json" if not path.exists(): return None return json.loads(path.read_text(encoding="utf-8")) def fmt(value, digits=3): if value is None: return "—" return f"{value:.{digits}f}" def main(): parser = argparse.ArgumentParser() parser.add_argument("--out", default=str(ROOT / "n_full_v1/analysis/tables.md")) args = parser.parse_args() truth = natural_truth() lines = [] synth = {} for model in MODELS: table = synth_table(model) if table: synth[model] = table lines.append("# 合成域条件层与 V/T/K/R\n") header = "| model | condition | n | acc | yes |" lines.append(header) lines.append("| --- | --- | ---: | ---: | ---: |") for model, table in synth.items(): for condition in CONDITIONS: cell = table["overall"]["conditions"].get(condition) if not cell: continue lines.append(f"| {model} | {condition} | {cell['n']} | {fmt(cell['acc'])} | {fmt(cell['yes_rate'])} |") lines.append("\n# 合成域 V/T/K/R 与端点\n") lines.append("| model | V | T | K | R | FCA(T+) | FCA(T-) | TEU(T+) | TEU(T-) |") lines.append("| --- | ---: | ---: | ---: | ---: | ---: | ---: | ---: | ---: |") for model, table in synth.items(): vtk = table["overall"]["vtk"] lines.append( "| {} | {} | {} | {} | {} | {} | {} | {} | {} |".format( model, fmt(vtk["v"]), fmt(vtk["t"]), fmt(vtk["k"]), fmt(vtk["r"]), fmt(table["overall"].get("fca_Tpos")), fmt(table["overall"].get("fca_Tneg")), fmt(table["overall"].get("teu_Tpos")), fmt(table["overall"].get("teu_Tneg")), ) ) lines.append("\n# 分族 V/T/K\n") lines.append("| model | family | V | T | K | R |") lines.append("| --- | --- | ---: | ---: | ---: | ---: |") for model, table in synth.items(): for family, cell in table["by_family"].items(): vtk = cell["vtk"] lines.append( f"| {model} | {family} | {fmt(vtk['v'])} | {fmt(vtk['t'])} | {fmt(vtk['k'])} | {fmt(vtk['r'])} |" ) lines.append("\n# 门控四方法(test)\n") lines.append("| model | method | acc | coverage | induced | harm | repair |") lines.append("| --- | --- | ---: | ---: | ---: | ---: | ---: |") for model in MODELS: gate = gate_table(model) if not gate: continue for method in gate["methods"]: lines.append( "| {} | {} | {} | {} | {} | {} | {} |".format( model, method["method"], fmt(method["accuracy"]), fmt(method["coverage"]), fmt(method["induced_error"]), fmt(method["harm_rate"]), fmt(method["repair_rate"]), ) ) lines.append("\n# 自然域条件层\n") lines.append("| model | collection | condition | n | acc | yes |") lines.append("| --- | --- | --- | ---: | ---: | ---: |") for model in MODELS: table = natural_table(truth, model) for collection in sorted(table): for condition in CONDITIONS: cell = table[collection].get(condition) if not cell: continue lines.append( f"| {model} | {collection} | {condition} | {cell['n']} | {fmt(cell['acc'])} | {fmt(cell['yes_rate'])} |" ) out = Path(args.out) out.parent.mkdir(parents=True, exist_ok=True) out.write_text("\n".join(lines) + "\n", encoding="utf-8") print(f"WROTE {out} models={list(synth)}") if __name__ == "__main__": main()