import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as pch
import json


def main():
    with open("res.json", 'r') as json_f:
        times = json.load(json_f)

    # plot means
    t_mean = []
    scat_x = []
    scat_y = []
    for labda in times.keys():
        t_mean.append(float(sum(times[labda])) / len(times[labda]))
        scat_x += [labda] * len(times[labda])
        scat_y += times[labda]

    plt.plot(times.keys(), t_mean)
    plt.scatter(scat_x, scat_y, color="red", s=2)

    plt.legend(["Mean transient times per labda", "Individual transient times"])

    plt.xlabel("Labda")
    ticksM = np.arange(0, 70.1, step=70.1/11)
    plt.xticks(ticksM, [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0])
    plt.ylabel("Transient time in steps")
    plt.yscale("log")
    plt.title("Mean and individual transient times for labda values")

    txt = "Mean and individual transient times for each possible labda with k=4, r=1, width=10 and height=4^10 for 10 repeated calculations per labda using table walkthrough for rule set building. Table walkthrough was used to ensure the correct labda, since this is not ensured when using the random generation"

    plt.figtext(0.5, 0.01, txt, wrap=True, horizontalalignment='center', fontsize=12)
    plt.subplots_adjust(bottom=0.2)
    plt.savefig("plot.png")
    plt.show()


if __name__ == "__main__":
    main()
