import json import sys from pathlib import Path import matplotlib matplotlib.use("Agg") import matplotlib.pyplot as plt import numpy as np sys.path.insert(0, "D:/GraphEvolve/figures_workspace/_utils") import plot_utils as pu ROOT = Path(__file__).resolve().parents[1] OUT = Path("D:/GraphEvolve/docs/paper_v3/figures") MODELS = [("qwen3vl", "Qwen3-VL-8B"), ("qwen25vl", "Qwen2.5-VL-7B"), ("deepseekvl2s", "DeepSeek-VL2-Small")] FAMILIES = [("left_right", "left/right"), ("above_below", "above/below"), ("front_behind", "front/behind"), ("on_not_on", "on/not-on")] pu.setup_style(palette="olive-apricot", language="en", font_family=["Arial", "DejaVu Sans"]) PRIMARY = pu.COLORS["primary"] SECONDARY = pu.COLORS["secondary"] ACCENT = pu.COLORS["accent"] MODEL_COLORS = {"qwen3vl": PRIMARY, "qwen25vl": SECONDARY, "deepseekvl2s": ACCENT} MODEL_MARKERS = {"qwen3vl": "o", "qwen25vl": "s", "deepseekvl2s": "^"} COL_W = 2.55 FULL_W = 5.5 def load(path): path = Path(path) return json.loads(path.read_text(encoding="utf-8")) if path.exists() else None def fig_matrix2(): fig, axes = plt.subplots(1, 3, figsize=(FULL_W, 2.45), constrained_layout=True) for ax, (key, label) in zip(axes, MODELS): table = load(ROOT / f"analysis/matrix_{key}.json") cells = table["cells"] grid = np.array([[cells["y01"], cells["y11"]], [cells["y00"], cells["y10"]]]) image = ax.imshow(grid, cmap="YlGnBu", vmin=0, vmax=1) ax.set_xticks([0, 1], ["T− (negates)", "T+ (asserts)"]) ax.set_yticks([0, 1], ["I− (relation\nfalse)", "I+ (relation\ntrue)"]) for row in range(2): for col in range(2): value = grid[row, col] ax.text(col, row, f"{value * 100:.1f}", ha="center", va="center", color="white" if value > 0.6 else "black", fontsize=9) ax.set_title(label, fontsize=9) ax.tick_params(labelsize=7.5) fig.colorbar(image, ax=axes, shrink=0.85, label="Pr(answer = yes), %", pad=0.02) for suffix in ("pdf", "png"): fig.savefig(OUT / f"fig_matrix2.{suffix}", dpi=300 if suffix == "png" else None) plt.close(fig) def fig_vt(): fig, ax = plt.subplots(figsize=(COL_W, 2.4), constrained_layout=True) markers = MODEL_MARKERS for key, label in MODELS: table = load(ROOT / f"analysis/matrix_{key}.json") for family, family_label in FAMILIES: cell = table["by_family"][family]["matrix"] v = cell["v"]["value"] if isinstance(cell["v"], dict) else cell["v"] t = cell["t"]["value"] if isinstance(cell["t"], dict) else cell["t"] ax.scatter(v * 100, t * 100, marker=markers[key], s=34, color=MODEL_COLORS[key], edgecolor="black", linewidth=0.4, label=label if family == FAMILIES[0][0] else None) ax.annotate(family_label, (v * 100, t * 100), fontsize=5.5, xytext=(2, 3), textcoords="offset points") ax.plot([0, 100], [0, 100], linestyle=":", color="grey", linewidth=0.8) ax.set_xlabel("Visual response $V$ (%)", fontsize=8) ax.set_ylabel("Textual response $T$ (%)", fontsize=8) ax.tick_params(labelsize=7) ax.set_xlim(0, 80) ax.set_ylim(0, 105) ax.legend(fontsize=6, frameon=False, loc="lower right") ax.text(52, 4, "text dominates", fontsize=6, color="grey") ax.text(4, 62, "vision dominates", fontsize=6, color="grey", rotation=90) for suffix in ("pdf", "png"): fig.savefig(OUT / f"fig_vt.{suffix}", dpi=300 if suffix == "png" else None) plt.close(fig) def fig_risk(): fig, ax = plt.subplots(figsize=(COL_W, 2.4), constrained_layout=True) table = load(ROOT / "analysis/vcg_test_qwen3vl.json") for name, style, colour in (("vcg", "-", PRIMARY), ("vcg_support_only", "--", SECONDARY)): points = [p for p in table["risk_coverage"][name] if p["risk"] is not None] ax.plot([p["coverage"] * 100 for p in points], [p["risk"] * 100 for p in points], style, color=colour, marker="o", markersize=2.5, label="gated" if name == "vcg" else "gated-support") direct = {m["method"]: m for m in table["methods"]}["direct_only"] ax.axhline((1 - direct["accuracy"]) * 100, color="black", linestyle=":", linewidth=0.9, label="direct-only risk") ax.set_xlabel("coverage (%)", fontsize=8) ax.set_ylabel("risk on committed items (%)", fontsize=8) ax.tick_params(labelsize=7) ax.legend(fontsize=6, frameon=False) for suffix in ("pdf", "png"): fig.savefig(OUT / f"fig_risk.{suffix}", dpi=300 if suffix == "png" else None) plt.close(fig) def fig_family2(): fig, axes = plt.subplots(1, 2, figsize=(FULL_W, 2.4), constrained_layout=True, sharey=True) width = 0.26 index = np.arange(len(FAMILIES)) for ax, metric, name in ((axes[0], "v", "visual response $V$"), (axes[1], "k", "interaction $K$")): for offset, (key, label) in zip((-width, 0.0, width), MODELS): table = load(ROOT / f"analysis/matrix_{key}.json") values = [] for family, _ in FAMILIES: cell = table["by_family"][family]["matrix"][metric] values.append((cell["value"] if isinstance(cell, dict) else cell) * 100) ax.bar(index + offset, values, width, label=label, color=MODEL_COLORS[key]) ax.set_xticks(index, [label for _, label in FAMILIES], fontsize=7, rotation=15) ax.set_title(name, fontsize=8.5) ax.tick_params(labelsize=7) ax.axhline(0, color="black", linewidth=0.6) axes[0].legend(fontsize=6, frameon=False) for suffix in ("pdf", "png"): fig.savefig(OUT / f"fig_family2.{suffix}", dpi=300 if suffix == "png" else None) plt.close(fig) if __name__ == "__main__": OUT.mkdir(parents=True, exist_ok=True) fig_matrix2() fig_vt() fig_risk() fig_family2() print("figures written")