import matplotlib.pyplot as plt
import seaborn as sb

sb.set_style(style="whitegrid")
sb.set_color_codes()
import numpy as np
import scipy.stats as sts
import pandas
import pandas as pd
import eif_old as eif_old_class
import scipy.io as sio
import os
import sklearn.metrics as skm


def draw_roc_from_EIF_result(filename, number_of_trees, subsample_size, extensionLevel, dataset_type):
    path = './EIF_SIF_Result/EIF_Result/' + filename + "_EIF_Result_Data_" + dataset_type + "-" + str(
        number_of_trees) + "-" + str(subsample_size) + "-" + str(extensionLevel) + ".xlsx"
    pd_data = pd.read_excel(path, index_col=0)
    y_test = np.array(pd_data.loc[:, "label"], dtype="int32")
    y_score = np.array(pd_data.loc[:, "score"])
    fpr_e, tpr_e, thresholds_s = skm.roc_curve(y_test, y_score, pos_label=1)

    return fpr_e, tpr_e


def draw_roc_from_SIF_result(filename, number_of_trees, subsample_size, extensionLevel, dataset_type):
    path = './EIF_SIF_Result/SIF_Result/' + filename + "_SIF_Result_Data_" + dataset_type + "-" + str(
        number_of_trees) + "-" + str(subsample_size) + "-" + str(0) + ".xlsx"
    pd_data = pd.read_excel(path, index_col=0)
    y_test = np.array(pd_data.loc[:, "label"], dtype="int32")
    y_score = np.array(pd_data.loc[:, "score"])
    fpr_s, tpr_s, thresholds_s = skm.roc_curve(y_test, y_score, pos_label=1)

    return fpr_s, tpr_s


# "mammography" ,
# dataset_names = ["annthyroid", "cardio", "foresttype", "ionosphere", "mammography", "satellite", "shuttle", "thyroid"]
# dataset_names = ["annthyroid", "cardio",  "ionosphere","mammography" ,"satellite", "shuttle", "thyroid"]
dataset_names = ["smtp","satimage-2","pendigits","speech"]
dataset_types = ["origin", "copula_0.0625", "copula_0.25", "copula_1", "copula_4", "copula_16", "10BIN", "15BIN"]
# parameter for traing the forest
number_of_trees = 500
subsample_size = 256
extensionLevel = "full"

for dataset_name in dataset_names:
    # fig, (ax_e, ax_s) = plt.subplots(ncols=2,nrows=1)
    fig, (ax_e, ax_s) = plt.subplots(ncols=2, nrows=1, figsize = (20,10))
    for dataset_type in dataset_types:
        fpr_e, tpr_e = draw_roc_from_EIF_result(filename=dataset_name, number_of_trees=number_of_trees,
                                                subsample_size=subsample_size,
                                                extensionLevel=extensionLevel,
                                                dataset_type=dataset_type)
        fpr_s, tpr_s = draw_roc_from_SIF_result(filename=dataset_name, number_of_trees=number_of_trees,
                                                subsample_size=subsample_size,
                                                extensionLevel=0,
                                                dataset_type=dataset_type)
        if dataset_type == "10BIN":
            plot_color = "brown"
            plot_linestyle = "--"
        elif dataset_type == "15BIN":
            plot_color = "darkblue"
            plot_linestyle = "--"
        elif dataset_type == "copula_0.0625":
            plot_color = "brown"
            plot_linestyle = "dotted"
        elif dataset_type == "copula_0.25":
            plot_color = "royalblue"
            plot_linestyle = "dotted"
        elif dataset_type == "copula_1":
            plot_color = "yellow"
            plot_linestyle = "dotted"
        elif dataset_type == "copula_4":
            plot_color = "royalblue"
            plot_linestyle = "dashdot"
        elif dataset_type == "copula_16":
            plot_color = "brown"
            plot_linestyle = "dashdot"
        else:
            # origin
            plot_color = "black"
            plot_linestyle = "-"

        ax_e.plot(fpr_e, tpr_e, label=dataset_type, color=plot_color, linestyle=plot_linestyle)
        ax_s.plot(fpr_s, tpr_s, label=dataset_type, color=plot_color, linestyle=plot_linestyle)

    ax_e.set_xlabel('FPR')
    ax_e.set_ylabel('TPR')
    ax_e.set_title("EIF ROC Curve")
    ax_e.legend()

    ax_s.set_xlabel('FPR')
    # ax_s.set_ylabel('TPR')
    ax_s.set_title("SIF ROC Curve")
    ax_s.legend()

    if extensionLevel == "full":
        extensionLevel_str = extensionLevel
    else:
        extensionLevel_str = str(extensionLevel)

    plot_path = "./plots/" + dataset_name + "-EIF-SIF-ROC_Curve-" + str(number_of_trees) + "-" + str(
        subsample_size) + "-" + extensionLevel_str + ".jpg"
    fig.savefig(plot_path)
    plt.tight_layout
    plt.close(fig)

    # y_predict = np.array(pd_data.loc[:, "Prediction"])
    # fpr_p, tpr_p, thresholds_p = skm.roc_curve(y_test, y_predict, pos_label=1)
    # print(filename+dataset_type)
    # print("pred")
    # print(skm.roc_auc_score(y_test,y_predict))
    # print("score")
    # print(skm.roc_auc_score(y_test, y_score))
    # print("fpr tpr pre")
    # print(skm.auc(fpr,tpr))
    # print("fpr tpr sco")
    # print(skm.auc(fpr_s,tpr_s))
    # return fpr_p, tpr_p, subsample_size
