import os
import numpy as np
import matplotlib.pyplot as plt

def path(str):
    return os.path.join(os.path.dirname(__file__), str)

params = {'text.usetex': True,
        'font.size': 9,
        'axes.labelsize': 9,
        'font.family': 'serif',
        'font.serif': ['Computer Modern'],
        'lines.linewidth': 1.0,
        'lines.markersize': 4.0}
plt.rcParams.update(params)
plt.rc('text.latex')
plt.rcParams['text.latex.preamble'] = r'\usepackage{amsmath} \usepackage{amssymb} \newcommand{\Hsquare}{\text{\fboxsep=.0pt\fbox{\rule{0pt}{1ex}\rule{1ex}{0pt}}}}'


fig, ax = plt.subplots()
fig.set_size_inches(2.78, 1.71)
ax.plot()

d32 = np.loadtxt(path('Stability_ti_4w_NS=2_WS=0.19634_vS=0.05_v0=0.01_x=1.0_T=0.025_NW=32.txt'))
d64 = np.loadtxt(path('Stability_ti_4w_NS=2_WS=0.19634_vS=0.05_v0=0.01_x=1.0_T=0.025_NW=64.txt'))
d128 = np.loadtxt(path('Stability_ti_4w_NS=2_WS=0.19634_vS=0.05_v0=0.01_x=1.0_T=0.025_NW=128.txt'))
d256 = np.loadtxt(path('Stability_ti_4w_NS=2_WS=0.19634_vS=0.05_v0=0.01_x=1.0_T=0.025_NW=256.txt'))
d512 = np.loadtxt(path('Stability_ti_4w_NS=2_WS=0.19634_vS=0.05_v0=0.01_x=1.0_T=0.025_NW=512.txt'))
d1024 = np.loadtxt(path('Stability_ti_4w_NS=2_WS=0.19634_vS=0.05_v0=0.01_x=1.0_T=0.025_NW=1024.txt'))


g4    = np.array([0.5011872336272722, 0.251188643150958, 0.1778279410038923, 0.08912509381337454, 0.050118723360, 0.026607250597988])
Ig4m  = np.array([0.0005506472953252, 0.000586596778340, 0.0005976226176908, 0.00061110235090501, 0.000617123828, 0.000620769975126])
sIg4  = np.array([0.0009739116972556, 0.000297330984130, 0.0001326446135625, 6.04437438806278e-5, 2.927084e-05,   1.442193014900e-5])
maIg4 = np.array([0.0018660537751012, 0.000512053769650, 0.0002244661068709, 8.98162868739334e-5, 4.09567574e-05, 2.023768462452e-5])
Ig4   = np.array([0.0005516748327894, 0.000586897147431, 0.0005977567637132, 0.00061114310903046, 0.000617145674, 0.000620780874059])

d4    = np.array([0.19634954084936207, 0.09817477042468103, 0.04908738521234052, 0.0245436926061702, 0.01227184630, 0.00619441075076])
sId4  = np.array([0.03075387217660721, 0.00339722175316666, 0.04343834594736065, 0.0425272297585360, 0.04029526010, 0.03695982861245])
maId4 = np.array([0.05781294788905573, 0.00825310584969745, 0.08460286654538818, 0.0828775465300915, 0.07839494801, 0.07195737414149])

plt.plot(d32[:, 0], d32[:, 12], '-', color="tab:red", label=r"\textbf{$32$}")
plt.plot(g4[0], maIg4[0], 'o',  color="tab:red")
plt.plot(d4[0], maId4[0], 's',  color="tab:red")

plt.plot(d64[:, 0], d64[:, 12], '-', color="tab:orange", label=r"\textbf{$64$}")
plt.plot(g4[1], maIg4[1], 'o',  color="tab:orange")
plt.plot(d4[1], maId4[1], 's',  color="tab:orange")

plt.plot(d128[:, 0], d128[:, 12], '-', color="tab:green", label=r"\textbf{$128$}")
plt.plot(g4[2], maIg4[2], 'o',  color="tab:green")
plt.plot(d4[2], maId4[2], 's',  color="tab:green")

plt.plot(d256[:, 0], d256[:, 12], '-', color="tab:purple", label=r"\textbf{$256$}")
plt.plot(g4[3], maIg4[3], 'o',  color="tab:purple")
plt.plot(d4[3], maId4[3], 's',  color="tab:purple")

plt.plot(d512[:, 0], d512[:, 12], '-', color="tab:blue", label=r"\textbf{$512$}")
plt.plot(g4[4], maIg4[4], 'o',  color="tab:blue")
plt.plot(d4[4], maId4[4], 's',  color="tab:blue")

plt.loglog([1.5], [2e-1], marker='s', linestyle='None', color = 'black')
plt.loglog([1.5], [4e-2], marker='o', linestyle='None', color = 'black')
ax.annotate(r"$\gamma^{\Hsquare}$", (2.2, 1.8e-1), annotation_clip=False, size=8)
ax.annotate(r"$\check{\gamma}$", (2.2, 3.6e-2), annotation_clip=False, size=8)


ax.annotate(r"\textbf{$N_{\mathcal{W}}$}", (0.21, 0.655), xycoords='axes fraction', annotation_clip=False, size=8)

plt.semilogy()
plt.semilogx()
plt.xlim(1e-4, 10)
plt.ylim(1e-5, 1)

ax.xaxis.set_major_locator(plt.LogLocator(base=10,numticks=100))
ax.xaxis.set_minor_locator(plt.LogLocator(base=10,subs=np.arange(2, 10) * .1,numticks=100))
ax.yaxis.set_major_locator(plt.LogLocator(base=10,numticks=100))
ax.yaxis.set_minor_locator(plt.LogLocator(base=10,subs=np.arange(2, 10) * .1,numticks=100))

plt.xlabel(r'\textbf{$\gamma \ (\omega_0)$}', x=0.5)
plt.ylabel(r'\textbf{$\sigma(I_{\diamondsuit})/|\overline{I_{\diamondsuit}}|$}', rotation=90)

legend = plt.legend(ncol=1, labelspacing=0.2, columnspacing=0.5, handlelength=1.3,
                   handletextpad=0.3, loc=(0.1, 0.05), frameon=False)

plt.savefig(path("Fig3.pdf"), bbox_inches='tight')
