"""Generate the standalone SVG from the JSON next to this script: python -B lag_224_figure.py figure.svg"""
from pathlib import Path
import json,sys
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from matplotlib.ticker import PercentFormatter
from matplotlib import rcParams
from math import ceil
D=Path(__file__).resolve().parent;r=json.loads((D/'lag_224_period.json').read_text())
rcParams.update({'svg.fonttype':'none','svg.hashsalt':'cicada224','font.family':'DejaVu Sans','font.size':11,'axes.spines.top':False,'axes.spines.right':False})
fig,ax=plt.subplots(figsize=(7.1,3.9));fig.patch.set_facecolor('#ffffff');ax.set_facecolor('#ffffff')
k=list(range(1,129));y=[c/r['n'] for c in r['counts']];target=r['reference'][0]/r['reference'][1]
ax.grid(axis='y',c='#d9dfe1',lw=.65);ax.set_axisbelow(True)
ax.plot(k,y,color='#23616b',lw=1,marker='.',ms=3)
ax.axhline(target,color='#a34421',ls='--',lw=1.5);ax.text(2,target+.003,'Solved LP1 pooled: 12.48%',color='#913d1e',fontsize=10)
z=r['thresholds']['pooled']['passing'];ax.scatter(z,[y[i-1] for i in z],s=29,color='#a34421',zorder=5)
ax.scatter([4],[y[3]],s=38,color='#132a2e',zorder=5)
ax.annotate('Period 4: 4.70%',xy=(4,y[3]),xytext=(9,.031),color='#132a2e',fontsize=10,arrowprops=dict(arrowstyle='-',color='#526467'))
ax.set_xlim(0,130);ax.set_ylim(.02,.145);ax.yaxis.set_major_formatter(PercentFormatter(1,decimals=0))
ax.set_xticks([1,16,32,48,64,80,96,112,128]);ax.set_xlabel('Repeating key length (runes)');ax.set_ylabel('Maximum share of one rune');fig.tight_layout(pad=.8)
fig.savefig(sys.argv[1],metadata={'Date':None,'Description':'Section 8: the highest possible frequency of a single decoded rune at every nominal period 1 through 128. Only 116, 120, 125 and 127 reach the pooled solved frequency.'})
