
from pyxai import Builder, Learning, Explainer, Tools
import os
import json
import numpy
from sklearn.utils import shuffle


class NpEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, numpy.integer):
            return int(obj)
        if isinstance(obj, numpy.floating):
            return float(obj)
        if isinstance(obj, numpy.ndarray):
            return obj.tolist()
        return super(NpEncoder, self).default(obj)


reference_percentages = [5, 10, 20, 40]
#reference_percentages = [8]
model_directory = Tools.Options.model_directory
dataset_directory = Tools.Options.dataset

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

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

for id, model in enumerate(models):

  
    explainer = Explainer.initialize(model)

    instances_anchored = learner.get_instances(
                dataset=dataset,
                indexes=Learning.TRAINING,
                model=model,
                details=True)
    
    instances_anchored = shuffle(instances_anchored, random_state=0)

    for percentage in reference_percentages:
        
        n_anchored = int((percentage*len(instances_anchored))/100)
        instances_anchored_part = instances_anchored[0:n_anchored]

        result_anchored_0 = [tuple(element["instance"]) for element in instances_anchored_part if element["label"] == 0]
        result_anchored_1 = [tuple(element["instance"]) for element in instances_anchored_part if element["label"] == 1]
        
        print("For " + str(percentage)+"%: " + str(len(result_anchored_0)) + " 0-labeled and "  + str(len(result_anchored_1)) + " 1-labeled")
        
        reference_instances = dict()
        reference_instances[0] = result_anchored_0
        reference_instances[1] = result_anchored_1
        
        reference_file = model_directory + os.sep + name+"."+str(id)+"."+str(percentage)+".v2.references.json"
        with open(reference_file, 'w') as file:
            json.dump(reference_instances, file, cls=NpEncoder)

        print("save " + reference_file)
        