"""Scientific plots from saved tables/probabilities only; no model access.""" import argparse import csv import json from pathlib import Path import numpy as np import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt from matplotlib.patches import Rectangle from .protocol import LAYERS,PAIRS def read_csv(path): with path.open() as f:return list(csv.DictReader(f)) def main(): p=argparse.ArgumentParser();p.add_argument('--root',type=Path,required=True);args=p.parse_args();root=args.root;out=root/'figures';out.mkdir(exist_ok=True) plt.rcParams.update({'font.size':9,'pdf.fonttype':42,'svg.fonttype':'none','savefig.bbox':'tight'}) def save(fig,name): for ext in ('png','pdf','svg'):fig.savefig(out/f'{name}.{ext}',dpi=180) plt.close(fig) status=json.loads((root/'status.json').read_text());summary=json.loads((root/'calibration-summary.json').read_text()) matrices=[] for mode in ('qk_head','identity','matched'): matrix=np.full((6,6),np.nan) for i,j in PAIRS: rows=[r for r in summary if r['i']==i and r['j']==j and ((mode=='qk_head' and r['kind']=='qk_head') or (mode!='qk_head' and r['id']==f'qk_map_{i}_{j}_{mode}'))] value=min(r['worst_mean_js'] for r in rows) a,b=LAYERS.index(i),LAYERS.index(j);matrix[a,b]=matrix[b,a]=value matrices.append(matrix) fig,axes=plt.subplots(1,3,figsize=(13,4.7));vmax=max(np.nanmax(m) for m in matrices) for ax,m,title in zip(axes,matrices,['Best single head pair','All heads: original order','All heads: calibration bijection']): im=ax.imshow(m,cmap='YlOrRd',vmin=0,vmax=vmax) for a in range(6): ax.add_patch(Rectangle((a-.5,a-.5),1,1,facecolor='#eeeeee',edgecolor='#aaaaaa',hatch='///',lw=.3)) for b in range(6): if a!=b:ax.text(b,a,f'{m[a,b]:.3f}',ha='center',va='center',fontsize=7,color='black' if m[a,b]