"""The registered structural forecast for iodine and the independent sampling check."""
import json
from geometry import fold_profile
from make_figures import result
from plate import IODO, MUTED, ROOT, Plate
from si_methods import save_si

p=Plate(66)
p.panel(1,3,'a','Registered iodine forecast')
p.text(3,17,'Fold ≥25° or contact to',fontsize=8)
p.text(3,22,'vanadium-bound sulfur',fontsize=8)
p.text(3,39,'Shorter spin lifetime',fontsize=8,weight='bold')
p.text(3,44,'than parent at 20 cm⁻¹',fontsize=8)
p.panel(55,3,'b','Calculated structure')
ax=p.axes(55,10,43,37)
fold_profile(ax,'R5_pI')
p.text(77,53,'Nearest contact: outer-ring sulfur',ha='center',fontsize=7.25)

p.panel(100,3,'c','Iodine lifetime in two calculations')
with (ROOT/'preregistration'/'judge_receipt_R5_pI_rcpt-f0670a4fa30c.json').open() as f:
    signed=json.load(f)['details']['paired_ratio']['per_sigma']
widths=[5,10,20]
ax=p.axes(121.5,20,52,31)
ax.axvline(1,c=MUTED,ls=(0,(2,2)),lw=.75)
for offset,marker,fill,values,label in [
    (-.13,'o','white',[signed[str(w)] for w in widths],'Signed result'),
    (.13,'D',IODO,[result('R5_pI',w) for w in widths],'Matched grid'),
]:
    ax.scatter(values,[j+offset for j in range(3)],facecolor=fill,edgecolor=IODO,s=25,
               linewidth=.9,marker=marker,label=label,zorder=3)
ax.set(xlim=(.68,1.56),ylim=(2.55,-.58),yticks=range(3),yticklabels=widths,
       xticks=[.8,1,1.2,1.4],xticklabels=['0.8×','1.0×','1.2×','1.4×'],
       xlabel='Spin lifetime relative to parent',ylabel='Linewidth (cm⁻¹)')
ax.spines['left'].set_visible(False)
ax.tick_params(axis='y',length=0)
ax.legend(loc='lower center',bbox_to_anchor=(.45,1.06),ncol=2,handlelength=.9,
          columnspacing=.7,handletextpad=.35,borderaxespad=0)
save_si(p,'S4_iodine_test')
