"""Render Session 25's measured lag profile. Requires matplotlib."""
from pathlib import Path
import json
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt

root=Path(__file__).resolve().parent
r=json.loads((root/'repetition_mechanism_results.json').read_text())
x=list(range(1,13))
y=[100*sum(s['lag'][d-1]['equal'] for s in r['sections'])/
   sum(s['lag'][d-1]['pairs'] for s in r['sections']) for d in x]
plt.rcParams.update({'font.size':12,'axes.spines.top':False,'axes.spines.right':False})
fig,ax=plt.subplots(figsize=(10,5.4),layout='constrained')
ax.plot(x,y,'o-',color='#166b98',linewidth=2,label='Observed in eight unsolved sections')
ax.axhline(100/29,color='#646a73',linestyle='--',linewidth=1.4,
           label='Independent uniform baseline: 1/29 (3.45%)')
ax.set(xlim=(.7,12.3),ylim=(0,4.15),xticks=x,
       xlabel='Distance in rune positions (1 = immediate neighbor)',
       ylabel='Identical-rune pairs (%)')
ax.set_title('The repeat shortage is concentrated at immediate neighbors',loc='left',pad=18,fontweight='bold')
ax.annotate('86 / 12,939 pairs\n0.665%',xy=(1,y[0]),xytext=(2.2,1.25),
            arrowprops={'arrowstyle':'->','color':'#166b98'},color='#166b98')
ax.grid(axis='y',alpha=.18)
ax.legend(loc='lower right',frameon=False,fontsize=10)
fig.supxlabel('Descriptive measurements from the retained LP2 transcription. Section boundaries are not crossed.\nThe baseline is a comparison model, not the probability of a cipher hypothesis.',fontsize=10)
fig.savefig(root/'repetition_distance_profile.png',dpi=180)
