import seaborn as sns
import pandas as pd
from pandas import DataFrame, Series
import matplotlib.pyplot as plt

import pathlib
import os
plt.rcParams["figure.figsize"] = (40, 5)
sns.set()

## new cell
cols = [
    "TEST", "MAP", '50000', '100000', '150000', '200000', '250000', '300000', '350000', '400000',
    '500000', '600000', '700000', '800000', '900000', '1000000', '2000000', '3000000',
    '4000000', '5000000', '6000000', '7000000', '8000000', '9000000', '10000000', '15000000',
    '20000000', '25000000', '30000000', '35000000', '40000000', '45000000', '50000000'
]
data = pd.read_csv("results.csv", quotechar="'", header=None)
data.columns = cols
# data.head()

## new cell
groups = data.groupby(["TEST", "MAP"])
groups_mean = groups.mean()
groups_std = groups.std()

## new cell
def max_val(hmap, test):
    return groups_mean.loc[test, hmap].max()

def sort_maps(test):
    maps = list(groups_mean.loc[test].index)
    new = [(max_val(i, test), i) for i in maps]
    new.sort()
    new = [i[1] for i in new]
    return new



def plot_test(test, include_error=True, log=False):
    sizes = [50000, 100000, 150000, 200000, 250000, 300000, 350000, 400000, 500000,
        600000, 700000, 800000, 900000, 1000000,
        2000000, 3000000, 4000000, 5000000, 6000000, 7000000, 8000000, 9000000, 10000000,
        15000000, 20000000, 25000000, 30000000, 35000000, 40000000, 45000000, 50000000]
    maps = sort_maps(test)
    # print(maps)
    count = 16
    repeats = [0, 5, 11]
    while count > -1:
        if not count and count not in repeats:
            break
        mp = maps[count]
        if include_error:
            plt.errorbar(groups_mean.columns,groups_mean.loc[test, mp], yerr=groups_std.loc[test, mp], label=mp)
        else:
            plt.plot(groups_mean.columns, groups_mean.loc[test, mp], label=mp)

        if count in repeats:
            if log:
                plt.xscale("log")
            plt.ylabel("{} time (ns)".format(test))
            
            plt.legend()
            plt.title(test)
            plt.savefig("./plots/{}/{}.png".format(test,count))
            plt.clf()
            # plt.show()
            
            repeats.pop(repeats.index(count))
        else:
            count -=1
## new cell

tests = data["TEST"].unique()
p = pathlib.Path("./plots")
if not p.is_dir():
    p.mkdir()
for i in tests:
    path = pathlib.Path("./plots/{}/".format(i))
    if not path.is_dir():
        path.mkdir()
    print(i)
    plot_test(i, False)
    # break


    
        


