import json
import os
import numpy as np
from tqdm import tqdm

import matplotlib.pyplot as plt

os.chdir("../result_RF_IJCAI")

fichiers = os.listdir()
#plot time computation/length

global_result = {}

def flatten(t):
    return [item for sublist in t for item in sublist]

for f in tqdm(fichiers) :
    
    os.chdir("../result_RF_IJCAI")
    with open(f) as json_data:
        data_dict = json.load(json_data)
    os.chdir("../plot_rf")
    
    dataset = f.split(".")[0]
    
    #For each tree
    keys = ["dir_r", "min_maj_r", "pref_f_import_r", "pref_shap_r", "pref_strate_wordf_r", "pref_wordf_r"]
    dico_values = {}
    
    for k in data_dict.keys() :
        
        
        if k in keys :
        
            len_value = []
            time_value = []
            
            for l in data_dict[k] :
                len_value.append(l[0])
                time_value.append(l[1])
            
            dico_values[k+"_len"] = np.mean(len_value)
            dico_values[k+"_time"] = np.mean(time_value)
            
        elif k[:3] == "enu" and  k != "enum_strate_wordf_import_r" :
            
            len_values = []
            nb_value = []
            mean_len_value = []
            mean_std_value = []
            time_value = []
            to_value = []
            
            for l in data_dict[k] :
                
                len_values.append(l[0])
                nb_value.append(len(l[0]))
                mean_len_value.append(np.mean(l[0]))
                mean_std_value.append(np.std(l[0]))
                time_value.append(l[1])
                to_value.append(l[2])
                
            dico_values[k+"_len"] = len_values
            dico_values[k+"_nb"] = nb_value
            dico_values[k+"_mnb"] = np.mean(nb_value)
            dico_values[k+"_mlen"] = np.mean(mean_len_value)
            dico_values[k+"_std"] = np.mean(mean_std_value)
            dico_values[k+"_time"] = time_value
            dico_values[k+"_mtime"] = np.mean(time_value)
            dico_values[k+"_to"] = np.mean(to_value)
                
    '''h = [dico_values[k] for k in keys]
    plt.figure()
    x = np.arange(len(keys))
    plt.bar(x, height=h)
    plt.xticks(x, keys);
    plt.ylabel('Size of reasons')
    plt.title(dataset)
    plt.savefig(f'{dataset}_len.pdf')'''
    
    
    names = ['min_maj', 'shap', 'f_import', 'wordf']
    
    
    '''#histo---------------------------------------------------------------------------------------------------------------
    #Number
    h = [dico_values[k+"_mnb"] for k in data_dict.keys() if k[:3] == "enu" and k != "enum_strate_wordf_import_r" ]
    plt.figure()
    x = np.arange(len(h))
    plt.bar(x, height=h, color="blue")
    plt.xticks(x, names);
    plt.ylabel('Number of reasons')
    plt.title(dataset)
    plt.savefig(f'{dataset}_nb.pdf')
    
    #Length
    h = [dico_values[k+"_mlen"] for k in data_dict.keys() if k[:3] == "enu" and k != "enum_strate_wordf_import_r"]
    plt.figure()
    x = np.arange(len(h))
    plt.bar(x, height=h, color="red")
    plt.xticks(x, names);
    plt.ylabel('Mean length of reasons')
    plt.title(dataset)
    plt.savefig(f'{dataset}_mlen.pdf')
    
    #std
    h = [dico_values[k+"_std"] for k in data_dict.keys() if k[:3] == "enu" and k != "enum_strate_wordf_import_r"]
    plt.figure()
    x = np.arange(len(h))
    plt.bar(x, height=h, color="yellow")
    plt.xticks(x, names);
    plt.ylabel('Mean std on length of reasons')
    plt.title(dataset)
    plt.savefig(f'{dataset}_std.pdf')
    
    #time
    h = [dico_values[k+"_mtime"] for k in data_dict.keys() if k[:3] == "enu" and k != "enum_strate_wordf_import_r"]
    plt.figure()
    x = np.arange(len(h))
    plt.bar(x, height=h, color="green")
    plt.xticks(x, names);
    plt.ylabel('Computation time to enumerate (s)')
    plt.title(dataset)
    plt.savefig(f'{dataset}_time.pdf')
    
    #boxplot---------------------------------------------------------------------------------------------------------------
    #boxplot
    fig = plt.figure()
    #plt.title(dataset + " : Size of reason boxplot ")
    h = [flatten(dico_values[k+"_len"]) for k in data_dict.keys() if k[:3] == "enu" and k != "enum_strate_wordf_import_r"]
    plt.boxplot(h, labels = names)
    plt.grid()
    plt.ylabel("Size of reason")
    plt.savefig(f'{dataset}_boxplot_len.pdf')
    '''
    #boxplot
    fig = plt.figure()
    #plt.title(dataset + " : Size of reason boxplot ")
    h = [dico_values[k+"_nb"] for k in data_dict.keys() if k[:3] == "enu" and k != "enum_strate_wordf_import_r"]
    plt.boxplot(h, labels = names)
    plt.grid()
    plt.ylabel("Number of reasons")
    plt.savefig(f'{dataset}_boxplot_nb.pdf')
    
    #boxplot
    fig = plt.figure()
    #plt.title(dataset + " : Size of reason boxplot ")
    h = [dico_values[k+"_time"] for k in data_dict.keys() if k[:3] == "enu" and k != "enum_strate_wordf_import_r"]
    plt.boxplot(h, labels = names)
    plt.grid()
    plt.ylabel("Computation time (s)")
    plt.savefig(f'{dataset}_boxplot_time.pdf')
    
    '''
    #scatter---------------------------------------------------------------------------------------------------------------
    fig = plt.figure()
    plt.plot(dico_values["enum_min_maj_r_time"], dico_values["enum_min_maj_r_nb"], "r+")
    plt.grid()
    plt.xlabel("time (s)")
    plt.ylabel("number of reason")
    plt.savefig(f"{dataset}_scat_min.pdf")
    
    fig = plt.figure()
    plt.plot(dico_values["enum_pref_shap_r_time"], dico_values["enum_pref_shap_r_nb"], "g+")
    plt.grid()
    plt.xlabel("time (s)")
    plt.ylabel("number of reason")
    plt.savefig(f"{dataset}_scat_shap.pdf")
    
    fig = plt.figure()
    plt.plot(dico_values["enum_pref_f_import_r_time"], dico_values["enum_pref_f_import_r_nb"], "b+")
    plt.grid()
    plt.xlabel("time (s)")
    plt.ylabel("number of reason")
    plt.savefig(f"{dataset}_scat_f_import.pdf")
    
    fig = plt.figure()
    plt.plot(dico_values["enum_wordf_import_r_time"], dico_values["enum_wordf_import_r_nb"], "y+")
    plt.grid()
    plt.xlabel("time (s)")
    plt.ylabel("number of reason")
    plt.savefig(f"{dataset}_scat_wordf.pdf")
    '''
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
