import numpy as np
import matplotlib.pyplot as plt


########
# Plot #
########

def create_plots(list_filenames, out_name, list_legend_names=[], loc='lower right'):
    size = 42
    size_legend = 25
    fig = plt.figure(figsize=(12, 12))

    for i in range(len(list_filenames)):
        arr = np.loadtxt(list_filenames[i], delimiter=', ')  # load data

        # print(arr.shape)

        means = np.mean(arr, axis=0)  # y-axis values
        x_axis = np.arange(means.shape[0])  #  x-axis values
        se = np.std(arr, ddof=1, axis=0) / np.sqrt(arr.shape[0])  # standard error (ddof=1 for sample)

        plt.plot(x_axis, means, label=str(list_legend_names[i]), linewidth=3.0)
        plt.fill_between(x_axis, means - se, means + se, alpha=0.2)

    # x-axis
    plt.xlim(0, arr.shape[1])
    plt.xlabel('Time Step', fontsize=size, weight='bold')
    plt.xticks(fontsize=size)
    plt.xticks(np.arange(0.0, arr.shape[1] + 100, 1000), fontsize=size)

    # y-axis
    plt.ylabel('Accuracy', fontsize=size, weight='bold')
    plt.yticks(np.arange(0.0, 1.000001, 0.2), fontsize=size)
    plt.ylim(0.0, 1.0)

    # legend
    if 1:
        leg = plt.legend(ncol=1, loc=loc, fontsize=size_legend)
        leg.get_frame().set_alpha(0.9)

    # grid
    plt.grid(linestyle='dotted')

    # plot
    # plt.show()

    # save
    fig.savefig(out_name + '.png', bbox_inches='tight')

###########
# Results #
###########

ex1a = create_plots(['exps_0.002_0.0_tanh_8.txt', 'exps_0.05_0.0_tanh_8.txt', 'exps_0.02_0.0_tanh_8.txt',
                      'exps_0.2_0.0_tanh_8.txt', 'exps_0.5_0.0_tanh_8.txt'],
                    list_legend_names=['0.002', '0.05', '0.02', '0.2', '0.5'], out_name='ex1a')

ex1b = create_plots(['exps_0.02_0.0_tanh_8.txt', 'exps_0.02_0.5_tanh_8.txt', 'exps_0.02_0.8_tanh_8.txt',
                      'exps_0.02_0.9_tanh_8.txt', 'exps_0.02_0.99_tanh_8.txt'],
                    list_legend_names=['0.0', '0.5', '0.8', '0.9', '0.99'], out_name='ex1b')

ex1c = create_plots(['exps_0.02_0.9_tanh_8.txt', 'exps_0.02_0.9_tanh_16.txt', 'exps_0.02_0.9_tanh_32.txt',
                      'exps_0.02_0.9_tanh_64.txt'],
                    list_legend_names=['8', '16', '32', '64'], out_name='ex1c')

ex1d = create_plots(['exps_0.02_0.9_tanh_8.txt', 'exps_0.02_0.9_sigmoid_8.txt', 'exps_0.02_0.9_relu_8.txt'],
                    list_legend_names=['tanh', 'sigmoid', 'relu'], out_name='ex1d')

ex2b = create_plots(['exps_0.02_0.9_tanh_8.txt', 'exps_sliding20_0.02_0.9_tanh_8.txt', 'exps_sliding150_0.02_0.9_tanh_8.txt'],
                    list_legend_names=['no sliding', 'sliding 20', 'sliding 150'], out_name='ex2')
