import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib as mpl
import matplotlib.pyplot as plt

sns.set_context("paper")
sns.set_style("white")
mpl.rcParams["figure.figsize"] = (13, 3)
mpl.rcParams["lines.linewidth"] = 0.5
mpl.rc('font', **{'family': 'sans-serif', 'sans-serif': ['Helvetica']})
## for Palatino and other serif fonts use:
# rc('font',**{'family':'serif','serif':['Palatino']})
mpl.rc('text', usetex=True)

# iter10 _part20 as constant, var interval
interval30_amp2 = [275.954, 280.177, 274.320, 274.743, 274.112, 364.728, 276.615, 274.091, 273.943, 604.349, 277.105,
                   274.040, 454.166, 277.019, 634.124, 276.679, 454.129, 276.563, 276.512, 514.058]
interval30_amp4 = [753.046, 395.139, 275.336, 333.899, 244.697, 272.489, 305.703, 724.983, 274.867, 272.489, 244.458,
                   272.264, 782.984, 545.207, 275.018, 602.861, 244.903, 242.275, 242.250, 242.554]
interval30_amp6 = [274.227, 245.370, 247.268, 576.829, 276.689, 334.630, 878.155, 308.197, 936.804, 637.392, 396.147,
                   667.525, 277.071, 454.492, 246.794, 304.515, 246.589, 274.221, 394.033, 246.293]
interval60_amp2 = [248.125, 243.553, 243.970, 244.187, 366.437, 248.305, 484.267, 366.576, 247.099, 244.225, 303.381,
                   243.680, 242.977, 243.444, 303.270, 303.174, 245.902, 243.084, 243.073, 304.008]
interval60_amp4 = [545.600, 427.342, 307.065, 304.720, 244.604, 244.308, 665.223, 607.450, 246.906, 424.974, 426.164,
                   306.752, 305.664, 304.131, 304.441, 785.453, 546.719, 246.498, 364.603, 426.376]
interval60_amp6 = [424.566, 307.057, 904.409, 306.635, 1084.677, 305.442, 244.700, 253.171, 364.849, 1027.450, 246.784,
                   244.335, 244.243, 244.818, 243.903, 244.228, 425.359, 306.190, 247.000, 244.252]

# iter10 _part10 as constant
iter10_part10_amp2 = [363.034, 453.569, 725.110, 364.962, 362.738, 452.759, 425.189, 486.064, 602.718, 364.927, 362.567,
                      362.965, 392.682, 790.790, 395.218, 574.775, 575.250, 634.965, 574.736, 487.021]
iter10_part10_amp4 = [393.818, 368.127, 366.250, 395.262, 364.339, 903.560, 366.226, 1055.606, 366.253, 363.802,
                      395.990, 603.701, 907.099, 1147.173, 365.873, 933.898, 367.646, 875.401, 636.088]
iter10_part10_amp6 = [1058.825, 494.107, 429.667, 653.132, 367.888, 697.416, 365.107, 368.537, 372.947, 850.616,
                      1043.743, 367.873, 395.362, 815.313, 367.576, 365.072, 364.758, 396.724, 367.339, 424.457]

# iter20 _part10 as constant
# iter20_part10_amp2 = [100, 200, 100, 200, 350, 199, 200]
# iter20_part10_amp4 = [1084.637, 604.134, 1775.890, 605.360, 1353.516, 696.253, 1234.321, 1114.661, 784.178, 1086.745, 604.087, 964.222, 786.007, 1084.233, 604.666, 1115.648, 1534.269, 1085.292, 633.834, 604.117]
# iter20_part10_amp6 = [100, 200, 100, 200, 350, 199, 200]

# iter10 _part20 as constant
iter10_part20_amp2 = [275.954, 280.177, 274.320, 274.743, 274.112, 364.728, 276.615, 274.091, 273.943, 604.349, 277.105,
                      274.040, 454.166, 277.019, 634.124, 276.679, 454.129, 276.563, 276.512, 514.058]
iter10_part20_amp4 = [753.046, 395.139, 275.336, 333.899, 244.697, 272.489, 305.703, 724.983, 274.867, 272.489, 244.458,
                      272.264, 782.984, 545.207, 275.018, 602.861, 244.903, 242.275, 242.250, 242.554]
iter10_part20_amp6 = [274.227, 245.370, 247.268, 576.829, 276.689, 334.630, 878.155, 308.197, 936.804, 637.392, 396.147,
                      667.525, 277.071, 454.492, 246.794, 304.515, 246.589, 274.221, 394.033, 246.293, 276.040, 350.617,
                      245.184, 247.868, 289.893, 816.280, 279.209, 965.133, 1058.186, 397.964]

# iter20 _part20
iter20_part20_amp2 = [996.507, 727.154, 577.260, 427.369, 431.658, 934.310, 426.683, 844.425, 396.849, 904.318, 966.987,
                      847.059, 786.707, 576.871, 426.017, 813.947, 936.268, 992.810, 395.078, 902.994]
iter20_part20_amp4 = [996.114, 426.214, 605.763, 1146.145, 995.298, 1235.016, 605.947, 395.353, 1143.801, 1176.241,
                      1174.518, 1267.214, 396.527, 395.063, 1174.503, 396.837, 1114.501, 396.649, 394.126, 394.754]
iter20_part20_amp6 = [664.679, 427.193, 814.606, 457.204, 396.702, 424.584, 427.767, 1354.478, 425.032, 546.192,
                      934.660, 606.753, 396.703, 394.381, 394.188, 1025.015, 394.468, 604.779, 398.245, 1054.312]

# interval30_vs_interval60
runtime_1 = [interval30_amp2, interval60_amp2, interval30_amp4, interval60_amp4, interval30_amp6, interval60_amp6]
# part10_vs_part20
runtime_2 = [iter10_part10_amp2, iter10_part20_amp2, iter10_part10_amp4, iter10_part20_amp4, iter10_part10_amp6,
             iter10_part20_amp6]
# iter10_vs_iter20
runtime_3 = [iter10_part20_amp2, iter20_part20_amp2, iter10_part20_amp4, iter20_part20_amp4, iter10_part20_amp6,
             iter20_part20_amp6]


def flattened_df(dataset, var1, var2, cols):
    """
    :param dataset: in this case, runtime
    :param var1: constant, extend to same length with each sample runtime
    :param var2: ditto
    :param cols: title of dataframe
    :return: pandas dataframe
    """
    df = pd.DataFrame()
    for i, line in enumerate(dataset):
        zipped = list(zip(line, [var1[i]] * len(line), [var2[i]] * len(line)))
        sec = pd.DataFrame(zipped, columns=cols)
        df = pd.concat([df, sec], ignore_index=True)
    return df


df_runtime_1 = flattened_df(runtime_1,
                            [30, 60, 30, 60, 30, 60],
                            [2, 2, 4, 4, 6, 6],
                            ["runtime", "interval", "amplitude"])

df_runtime_2 = flattened_df(runtime_2,
                            [10, 20, 10, 20, 10, 20],
                            [2, 2, 4, 4, 6, 6],
                            ["runtime", "part", "amplitude"])

df_runtime_3 = flattened_df(runtime_3,
                            [10, 20, 10, 20, 10, 20],
                            [2, 2, 4, 4, 6, 6],
                            ["runtime", "iter", "amplitude"])

data = [df_runtime_1, df_runtime_2, df_runtime_3]
vars = ["interval", "part", "iter"]
titles = ["Running Time Comparison of" + i + " PageRank Application" for i in
          ["\n30- and 60-second-MTBA", "\n10- and 20-partition", "\n10- and 20-iteration"]]

fig, axes = plt.subplots(nrows=1, ncols=3, sharey=True)
for i, (ax, datum, var, title) in enumerate(zip(axes, data, vars, titles)):
    if i != 0:
        ax.yaxis.label.set_visible(False)
    ax.set_title(title)
    sns.boxplot(x="amplitude", y="runtime", hue=var, width=0.5, data=datum, palette="Paired", ax=ax)

# plt.show()
plt.savefig("box.pdf", format="pdf", dpi=300, bbox_inches="tight")
