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()

csv = []
head = [ "accuracy", "len_bin"]
first = True

for file in fichiers:
#    print(file)
    with open(f'{file}') as json_data:

        data_dict = json.load(json_data)
        score = data_dict["acc"]
        len_bin = 0
        for i, v in enumerate(data_dict["len_bin"]):
            if i % 2 == 0:
                len_bin += v
        len_bin = len_bin/10
        nb = len(data_dict['instance'])
        print(file, round(score, 2), int(len_bin), nb, sep="&", end='')
        for type in ["enum_min_maj_r", "enum_pref_shap_r", "enum_pref_f_import_r", "enum_wordf_import_r"]:
            nb_ok = 0
            nb_approx = 0
            nb_to = 0
            nb_rien = 0
            nb_all = 0
            all = []
            for m in data_dict[type]:
                if m[1] < 59 :
                    nb_all += 1
                    all.append(len(m[0]))
                if m[1] >= 59.3:
                    nb_to += 1
                if len(m[0]) > 1 or m[1] < 59:
                    nb_ok += 1
                else :
                    if len(m[0]) == 0:
                        nb_rien += 1
            if len(all) == 0 :
                mean = "-"
                dev = "-"
            else :
                mean = round(np.mean(all), 1)
                dev = round(np.std(all), 1)
            print("&", nb_ok, nb_all, f"{mean} ({dev})", sep="&", end='')
        print("\\\\")


        '''
        for type in ["enum_min_maj_r", "enum_pref_shap_r", "enum_pref_f_import_r", "enum_wordf_import_r"]:
            nb_min = 0
            sz_min = 0
            time = 0
            nb_to = 0
            nb = 0
            for m in data_dict[type]:
                nb+= 1
                time+= m[1]
                sz_min += sum(m[0]) / len(m[0])
                nb_to += 1 if m[1] > 59 else 0
                nb_min += len(m[0])
            print(nb_min/nb, sz_min/nb, time/nb, nb_to, " ", end='')
        print()
'''
    #csv.append(file)
#print("CSV:", csv)
#csv2 = head
#csv = np.asarray(csv)
#np.savetxt("tableau.csv", csv, header=','.join(head), delimiter=",", fmt="%f")
