import matplotlib.pyplot as plt
import os

x_labels_locations = [1, 2, 4, 5, 8, 13, 16, 18]
indicies = x_labels_locations
x_labels = [str(x_label) for x_label in x_labels_locations]


if __name__ == "__main__":
    files = os.listdir()
    files = [file_ for file_ in files if file_.endswith(".txt")]
    for filename in files:
        with open(filename, 'r') as file_:
            duration = []
            for line in file_.readlines():
                if "time elapsed" in line:
                    duration.append(float(line.split()[0]))
            plt.plot(indicies[:len(duration)], duration, linestyle='--', marker='o', lw=1, label=filename.split(".")[0].split("_", 3)[3])
            plt.yscale("log")
            plt.xlabel("Recursion depth")
            plt.ylabel("Runtime [s]")
            plt.legend(loc='upper center')
            plt.title("Runtimes of the different versions".format(filename.split(".")[0]))
            plt.savefig("./graphics/collected.png".format(filename), dpi=200)
            #plt.clf()
            """
            for nodes, res_ in results.items():
                if res_ == {}:
                    continue
                baseline = res_["MPI_Scatter-0"]
                for function, res__ in res_.items():
                    if function == "MPI_Scatter-0":
                        continue
                    else:
                        plt.subplot(211)
                        plt.plot(indicies, list(baseline.values()), linestyle='--', marker='o', color='orange', lw=1, label="MPI_Scatter")
                        plt.xlabel("message size [Bytes]")
                        plt.ylabel("latency [microseconds]")
                        plt.xscale("log")
                        plt.yscale("log")
                        plt.legend(loc='upper center')
                        plt.xticks(x_labels_locations, x_labels)
                        plt.subplot(212)
                        relative = {k: res__[k]/base for k, base in baseline.items()}
                        plt.ylim(0, max(1.1, max(relative.values()) * 1.1))
                        plt.bar(list(range(len(indicies))), list(relative.values()), color='blue')
                        plt.xlabel("message size [Bytes]")
                        plt.ylabel("relative latency")
                        plt.xticks(list(range(len(indicies))), x_labels)
                        plt.tight_layout()
                        plt.savefig("../graphics/{}-{}.png".format(function, nodes))
                        plt.clf()
                """
