run.create_edge_spectrum(state, p, overwrite=False)
    
def entanglement_entropy(es):
    return sum(-v * np.log2(v) for v in es if not np.isclose(v, 0))

entropy = []
for p in params:
    spec = run.load_edge_spectrum(state, p, evecs=False)
    es = [spec_pnt[1] for spec_pnt in spec]
    entropy.append(entanglement_entropy(es))
    
entropy = np.real(entropy)
Ls = np.array(Ls)
extrap_Ls = np.array([0, 1]+list(Ls))

fit_pts, params, R = analysis.curve_fit_main(entropy, analysis.linear, Ls, extrap_Ls)
# flip sign of gamma
params = [params[0], -1*params[1]]

print params
print fit_pts[0], fit_pts[1]

plt.plot(Ls, entropy, 'k.', label='Data')
plt.plot(extrap_Ls, fit_pts, 'b-', label='Fit')

text = text = plt.text(1, 5, r'$S = \alpha W-\gamma$')
pl2_txt = lambda params: (r'$\alpha={}$'+'\n'+r'\gamma={}$').format(IO.round_sig(params[0],2),IO.round_sig(params[1], 2))
text2 = plt.text(1, 3.5, pl2_txt(params))

plt.xlim(xmin=-1, xmax=11)
plt.ylim(ymin=-1)
spec_points.plot_toolbox.spec_plot(spec_points.filter([],[lambda L:L>4], L='L'), 'E',
                                                shift_func=lambda L: 0.3*(L-L_max-1)/(L_max - L_min), ax = main_ax)

import PEPS.analysis as a

symdata = symmetric_inverse_corr2.view_props(['L', 'Correlation Bound'])
symdata = np.array([[L, val] for L, val in symdata])
symdata = np.array(sorted(symdata, key = lambda x:x[0]))
plt.sca(axs[1])
fit_data = symdata[:, 1][-4:]
extrapolate_pnts = np.arange(3, 10, 0.1)
fit_func = a.expic
fit_Ls = symdata[:, 0][-4:]
fit_label = 'Fit'

(fit_data, params, R) = a.curve_fit_main(fit_data,fit_func, fit_Ls, extrapolate_pnts)
symmetric_inverse_corr2.plot('L', 'Correlation Bound', ax = axs[1], xlabel = 'L', ylabel='Correlation Length', title = 'Symmetric Correlation Length', label = 'Data', marker = '*', color = 'k')
plt.plot(extrapolate_pnts, fit_data, label = fit_label, color='k')
txt_coords = (5, -0.3)
txt = plt.text(txt_coords[0], txt_coords[1],  fit_label+': \n$a-ce^{-L/b}$'+'\n a ={} \n b={} \nc={}'.format(*[IO.round_sig(p) for p in params]))
plt.xlim(3, 10)
plt.ylim(-0.4, 1.5)                                                


notsymdata = notsymmetric_inverse_corr.view_props(['L', 'Correlation Bound'])
notsymdata = np.array([[L, val] for L, val in notsymdata])
notsymdata = np.array(sorted(notsymdata, key = lambda x:x[0]))
plt.sca(axs[1])
fit_data = notsymdata[:, 1][-4:]
extrapolate_pnts = np.arange(3, 10, 0.1)
fit_func = a.expic