#!/usr/bin/env python

# **Packages**

# Some import
import numpy as np
# from sklearn.tree import export_text
from tqdm import tqdm
import time
import sys

import my_tree as mt
import pandas as pd
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split, LeaveOneGroupOut
import os
import functools
import operator
from random import shuffle
import copy
import my_forest as mf
import json



def main():
    dir = "../"
    os.chdir(dir + "datasets")
    database = {}

    fichiers = [arg + ".csv" for arg in sys.argv[1:]]  # , "dorothea.csv", "dexter.csv", "mnist38.csv", "adult.csv"]

    for f in fichiers:
        database[f.split(".")[0]] = pd.read_csv(f)

    # fichiers = os.listdir()
    # fichiers = fichiers[76::]

    os.chdir(dir + "scripts")

    info = "info_data_RF.json"
    with open(info) as json_data:
        info_nb_tree = json.load(json_data)

    for dataset in database.keys():

        print(f"Work on {dataset}")

        nb_tree = info_nb_tree[dataset]
        if nb_tree % 2 == 0:
            nb_tree += 1
        print("nb forest = ", nb_tree)
        data = database[dataset].copy()
        col = list(data.columns)
        data_name = list(data.columns)

        size = data.shape[1]

        y = data[col[size - 1]]
        X = data.drop(col[size - 1], axis=1)

        X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

        my_forest = []

        rf = RandomForestClassifier(n_estimators=11)  # number tree as paramétres
        rf.fit(X_train, y_train)
        y_predict = rf.predict(X_test)
        accuracy = (np.sum(y_predict == y_test) / len(y_test)) * 100
        print("accuracy=", accuracy)

        for dt in rf.estimators_:
            my_tree = mt.decision_tree()
            my_tree.from_DecisionTreeClassifier(dt)
            my_forest.append(my_tree)
        my_forest = mf.decision_forest(my_forest)
        my_forest.labels = rf.classes_

        instance = X_test.values[1]
        print("Search a direct")
        direct = my_forest.find_direct_reason(instance=instance)
        print("A direct : ", len(direct), direct)
        print()

        print("Search a min")
        min, t  = my_forest.find_prefered_majoritary_reason(instance=instance, solver="openwbo", rf=rf, method=None, nb=1)
        print("A min    : ", len(min), min, t )

        print()
        print("Search a shap prefered")
        shap, t = my_forest.find_prefered_majoritary_reason(instance=instance, rf=rf, solver="openwbo", method="shapely", nb=1)
        print("A shap   : ", len(shap), shap, t)

        print()
        print("Search a feature importance prefered" )
        fi, t = my_forest.find_prefered_majoritary_reason(instance=instance, rf=rf, solver="openwbo", method="f_importance", nb=1)
        print("A fi     : ", len(fi), fi, t )

        print()
        print("Search a wordfreq prefered" )
        wf, t = my_forest.find_prefered_majoritary_reason(instance=instance, rf=rf, col=data_name, solver="openwbo", method="wordfreq", nb=1)
        print("A wordfreq: ", len(wf), wf, t )


if __name__ == '__main__':
    main()
