import numpy as np
import scipy.stats as st
import matplotlib as mpl
import matplotlib.pyplot as plt


# From: qa_N_16_norm2, just printing the predictions


mpl.font_manager._rebuild()
plt.rc('font', family='Raleway')
plt.rcParams["axes.prop_cycle"] = plt.cycler("color", plt.cm.Set2.colors)


x = [-0.0004439432439765955, -0.04665297960575527, -0.053067694364402264, -0.05027815178278548, -0.044331649883939464, 0.0009492034872972925, -0.04912438269394953, -0.0525273508468691, -0.05107100636639747, -0.05083425020253851, 0.00013265945058437648, -0.04997131850446657, -0.052792533808458324, -0.04983633261109326, -0.04753791687828784, -0.00011525390673930896]
y = [-0.001129966953761341, 0.05041431867626101, 0.046611014668819103, 0.05236414737161405, 0.0491922490280236, -0.000750562578152953, 0.047192996571450346, 0.04228708480945534, 0.04529701533929545, 0.048164739563084084, 0.0005382042843907181, 0.048367614263833375, 0.05150744848625881, 0.04509966069418716, 0.04934518913213934, 0.0007762266677309802]

x.extend(y)
idx = np.arange(len(x))
width = np.min(np.diff(idx)) / 3

fig = plt.figure(figsize=(4, 2))
plt.bar(idx, x, width)
plt.xlim(-1, 33)
plt.ylabel("Residual")
plt.xlabel("QA Parameter")
plt.tight_layout()
fig.savefig('qa_avg_residuals1.png')
fig.savefig('qa_avg_residuals1.pdf')
plt.show()

x = [-0.00026291125737480105, 0.06283541760322549, 0.056583982616349005, 0.05990645871810003, 0.06566065800524133, 0.0011827519931234519, 0.060328906981773116, 0.05687798011281109, 0.05910749014820109, 0.058371098504864556, 0.00019698595001545804, 0.058875905805067436, 0.057604160354562735, 0.0607710223084855, 0.062207975815574926, -0.00014076988173895548]
y = [-0.0010697128360520553, -0.05857342211010817, -0.06380855908453618, -0.057716028057238355, -0.059573516356976566, -0.0009381881154215948, -0.060140725371328106, -0.06630618565962385, -0.06507121327968957, -0.0597855011162479, 0.00023650402501939434, -0.05917215326693487, -0.0583900690632925, -0.06381378416390338, -0.05747490023292383, 0.0009433311048745752]

x.extend(y)
idx = np.arange(len(x))
width = np.min(np.diff(idx)) / 3

fig = plt.figure(figsize=(4, 2))
plt.bar(idx, x, width)
plt.xlim(-1, 33)
plt.ylabel("Residual")
plt.xlabel("QA Parameter")
plt.tight_layout()
fig.savefig('qa_avg_residuals2.png')
fig.savefig('qa_avg_residuals2.pdf')
plt.show()

# Total percentage of samples with this shape
arr = np.array([
    [0.8061], [0.79705], [0.82595], [0.82245], [0.8134], [0.8061], [0.8228],
    [0.8175], [0.81365], [0.82835]
])
mean = np.mean(arr, axis=0)
x = np.arange(mean.shape[0])
ci = st.t.interval(
    0.95,
    len(arr) - 1,
    loc=np.mean(arr, axis=0),
    scale=st.sem(arr, axis=0)
)
print(mean, "+-", mean - ci[0])
