"""Plot the per-condition eMCR results (docs/condition_matrix.json, produced by build_condition_matrix.py) as: 1. fig_condition_dotplot - the 4 representative models from Table 4 (BM25 / Q3E8 / Q3R8 / VLR8) across all 15 atomic conditions, grouped by dimension (A/B/C), with Wilson 95% CI error bars. Main-text candidate. 2. fig_condition_heatmap - the full 16-model x 15-condition matrix, paradigm-grouped rows, dimension-grouped columns. Appendix candidate. Usage: python scripts/plot_condition_matrix.py """ from __future__ import annotations import json from pathlib import Path import matplotlib.pyplot as plt import numpy as np ROOT = Path(__file__).resolve().parents[1] MATRIX_PATH = ROOT / "docs" / "condition_matrix.json" OUT_DIR = ROOT / "docs" / "figures" OUT_DIR.mkdir(parents=True, exist_ok=True) # --------------------------------------------------------------------------- # Fixed presentation order (mirrors Table 2 / Table 4 in the paper). # --------------------------------------------------------------------------- ATOM_GROUPS = [ ("A. Expression variation", ["paraphrase", "expand", "restructure", "correction"]), ("B. Intent type", ["content_intent", "sku_intent", "knowledge", "general_sem.", "attribute_scene", "implicit_intent"]), ("C. Constraint signal", ["brand", "style", "negative_intent", "price_query", "image_clue"]), ] ATOMS = [a for _, atoms in ATOM_GROUPS for a in atoms] MODEL_GROUPS = [ ("Sparse", ["BM25"]), ("Text dense", ["BGE-M3", "GritLM-7B", "E5-Mistral-7B", "Qwen3-Emb-4B", "Qwen3-Emb-8B"]), ("Text rerank", ["BGE-Reranker-v2-m3", "Qwen3-Reranker-4B", "Qwen3-Reranker-8B"]), ("MM dense", ["MM-Embed", "VLM2Vec-V2", "Qwen3-VL-Emb-2B", "Qwen3-VL-Emb-8B"]), ("MM rerank", ["Jina-Reranker-m0", "Qwen3-VL-Reranker-2B", "Qwen3-VL-Reranker-8B"]), ] MODELS = [m for _, models in MODEL_GROUPS for m in models] REPRESENTATIVE = [ ("BM25", "BM25", "#7f7f7f", "o"), ("Qwen3-Emb-8B", "Q3E8 (text dense)", "#1f77b4", "s"), ("Qwen3-Reranker-8B", "Q3R8 (text rerank)", "#2ca02c", "^"), ("Qwen3-VL-Reranker-8B", "VLR8 (mm rerank)", "#d62728", "D"), ] def load_matrix(): with open(MATRIX_PATH, encoding="utf-8") as f: return json.load(f)["models"] def plot_dotplot(data): fig, ax = plt.subplots(figsize=(11, 4.2)) x = np.arange(len(ATOMS)) for model_key, label, color, marker in REPRESENTATIVE: ys, lo, hi = [], [], [] for atom in ATOMS: cell = data[model_key]["atoms"][atom] ys.append(cell["p1"]) lo.append(cell["p1"] - cell["ci95"][0]) hi.append(cell["ci95"][1] - cell["p1"]) ax.errorbar( x, ys, yerr=[lo, hi], label=label, color=color, marker=marker, markersize=5, linewidth=1.4, capsize=2, elinewidth=0.8, alpha=0.9, ) # dimension-group shading + separators offset = 0 band_colors = ["#f7f7f7", "#ffffff", "#f0f0f0"] for i, (gname, atoms) in enumerate(ATOM_GROUPS): n = len(atoms) ax.axvspan(offset - 0.5, offset + n - 0.5, color=band_colors[i % 3], zorder=0) ax.text(offset + n / 2 - 0.5, 103, gname, ha="center", va="bottom", fontsize=9, fontweight="bold", color="#444444") if offset > 0: ax.axvline(offset - 0.5, color="#bbbbbb", linewidth=0.8, zorder=0) offset += n ax.set_xticks(x) ax.set_xticklabels([a.replace("_", "\n") for a in ATOMS], fontsize=8, rotation=0) ax.set_ylabel("Precision@1 (%)", fontsize=10) ax.set_ylim(0, 108) ax.set_yticks(range(0, 101, 20)) ax.legend(loc="lower center", bbox_to_anchor=(0.5, 1.08), ncol=4, frameon=False, fontsize=9) ax.spines["top"].set_visible(False) ax.spines["right"].set_visible(False) ax.grid(axis="y", linestyle=":", linewidth=0.5, color="#cccccc", zorder=0) fig.tight_layout() for ext in ("pdf", "png"): fig.savefig(OUT_DIR / f"fig_condition_dotplot.{ext}", dpi=200, bbox_inches="tight") plt.close(fig) print(f"wrote {OUT_DIR / 'fig_condition_dotplot.pdf'} (+ .png)") def plot_heatmap(data): matrix = np.full((len(MODELS), len(ATOMS)), np.nan) for i, model in enumerate(MODELS): for j, atom in enumerate(ATOMS): cell = data[model]["atoms"].get(atom) if cell is not None: matrix[i, j] = cell["p1"] fig, ax = plt.subplots(figsize=(10.5, 8.5)) im = ax.imshow(matrix, cmap="YlGnBu", vmin=0, vmax=100, aspect="auto") ax.set_xticks(np.arange(len(ATOMS))) ax.set_xticklabels(ATOMS, rotation=45, ha="right", fontsize=8) ax.set_yticks(np.arange(len(MODELS))) ax.set_yticklabels(MODELS, fontsize=8) for i in range(len(MODELS)): for j in range(len(ATOMS)): v = matrix[i, j] if not np.isnan(v): txt_color = "white" if v > 60 else "black" ax.text(j, i, f"{v:.0f}", ha="center", va="center", fontsize=6.2, color=txt_color) # paradigm-group separators (rows) + labels row_off = 0 for gname, models in MODEL_GROUPS: n = len(models) if row_off > 0: ax.axhline(row_off - 0.5, color="black", linewidth=1.0) ax.text(-0.7, row_off + n / 2 - 0.5, gname, ha="right", va="center", fontsize=8, fontweight="bold", rotation=0, transform=ax.transData) row_off += n # dimension-group separators (cols) col_off = 0 for gname, atoms in ATOM_GROUPS: n = len(atoms) if col_off > 0: ax.axvline(col_off - 0.5, color="black", linewidth=1.0) col_off += n ax.set_xticks(np.arange(-0.5, len(ATOMS), 1), minor=True) ax.set_yticks(np.arange(-0.5, len(MODELS), 1), minor=True) ax.grid(which="minor", color="white", linewidth=0.6) ax.tick_params(which="minor", length=0) cbar = fig.colorbar(im, ax=ax, fraction=0.03, pad=0.02) cbar.set_label("Precision@1 (%)", fontsize=9) ax.set_title("P@1 by model x atomic condition (16 models x 15 conditions)", fontsize=11, pad=14) fig.tight_layout() for ext in ("pdf", "png"): fig.savefig(OUT_DIR / f"fig_condition_heatmap.{ext}", dpi=200, bbox_inches="tight") plt.close(fig) print(f"wrote {OUT_DIR / 'fig_condition_heatmap.pdf'} (+ .png)") if __name__ == "__main__": data = load_matrix() plot_dotplot(data) plot_heatmap(data)