
from pyxai import Builder, Learning, Explainer, Tools
import os
import json
import numpy
from collections import OrderedDict

model_directory = Tools.Options.model_directory
dataset_directory = Tools.Options.dataset
time_limit = int(Tools.Options.time_limit)
type_references = Tools.Options.type_references #normal ou one_side


reference_percentages = [5, 10, 20]
#reference_percentages = [50]

print("directory:", model_directory)
name = model_directory.split(os.sep)[-2]
print("name:", name)
dataset = dataset_directory+os.sep+name+".csv"
types = dataset_directory+os.sep+name+".types"

print("name:", dataset)

learner, models = Learning.load(models_directory=model_directory) 
feature_names = Explainer.initialize(models[0]).get_feature_names()

def load_reference_instances(directory, name, percentage, id, dict_labels, target_prediction):
    #str_prediction = "for_0" if target_prediction == 0 else "for_1"
    #reference_file = directory+os.sep+name+"."+str(id)+"."+str(percentage)+"."+str_prediction+".references.json"
    reference_file = directory+os.sep+name+"."+str(id)+"."+str(percentage)+".v2.references.json"
    
    with open(reference_file) as json_file:
      data = json.load(json_file)
    #print("here:", data.keys())
    #print("dict_labels[key]:", dict_labels)
    new_data = dict()
    #for key in dict_labels:
    #    new_data[dict_labels[key]] = data[key]
    
    new_data[0] = data["0"]
    new_data[1] = data["1"]
    
    return new_data

def compute_stat(array):
    d = dict()
    d["values"] = array
    d["median"] = numpy.median(array) 
    d["mean"] = numpy.mean(array) 
    d["std"] = numpy.std(array) 
    if len(array) != 0:
        d["min"] = min(array) 
    else:
        d["min"] = None
    if len(array) != 0:
        d["max"] = max(array)
    else:
        d["max"] = None 
    return d

t_total = []
t_n_anchoreds = []
t_percentages = []
stats_times = []
stats_n_anchoreds = []
stats_sizes = []
array_TO = []

for percentage in reference_percentages:
    total = 0
    n_anchoreds = dict()
    array_times = []
    array_n_anchoreds = []
    array_size = []
    n_TO = 0
    
    for id, model in enumerate(models):

        instances = learner.get_instances(
            dataset=dataset,
            indexes=model_directory, 
            model=model, 
            instances_id=id)
        explainer = Explainer.initialize(model, features_type=types)
        
        id_instance = 0
        reference_instances = load_reference_instances(model_directory, name, percentage, id, learner.dict_labels, explainer.target_prediction)
        
        explainer.set_reference_instances(reference_instances)
        
        for instance, prediction in instances:
                total += 1
                
                explainer.set_instance(instance)

                most_anchored_reason = explainer.most_anchored_reason(time_limit=time_limit, type_references=type_references, check=True)

                if explainer.last_n_anchors not in n_anchoreds.keys():
                    n_anchoreds[explainer.last_n_anchors] = 1
                else:
                    n_anchoreds[explainer.last_n_anchors] += 1
                if explainer.elapsed_time == -1:
                    n_TO += 1
                array_times.append(explainer.elapsed_time if explainer.elapsed_time != -1 else time_limit)
                array_n_anchoreds.append(explainer.last_n_anchors)
                print("id instance: ", id_instance)
                print("prediction: ", explainer.target_prediction)
                
                print("id model: ", id)
                print("percentage references: ", percentage)
                print("time most_anchored_reason: ", explainer.elapsed_time if explainer.elapsed_time != -1 else time_limit)
                print("n_anchors most_anchored_reason: ", explainer.last_n_anchors)
                size = len(most_anchored_reason) if most_anchored_reason is not None else None
                print("size most_anchored_reason: ", size)
                if size is not None:
                    array_size.append(size)
                
                print("-------------------------------------------------------------")
                id_instance += 1
    n_anchoreds = dict(sorted(n_anchoreds.items()))

    stats_times.append(compute_stat(array_times))
    stats_n_anchoreds.append(compute_stat(array_n_anchoreds))
    stats_sizes.append(compute_stat(array_size))

    t_total.append(total)
    t_percentages.append(percentage)
    t_n_anchoreds.append(n_anchoreds)
    array_TO.append(n_TO)

print("total:", t_total)
print("percentage:", t_percentages)
print("n_anchoreds:", t_n_anchoreds)
print("stats_times:", stats_times)
print("stats_n_anchoreds:", stats_n_anchoreds)
print("stats_sizes:", stats_sizes)
print("array_TO:", array_TO)
