ape_tabular_experiments.py
def ape_center(ape, instance, growing_method='GF', nb_features_employed=None):
    closest_counterfactual, growing_sphere, training_instances_in_sphere, train_labels_in_sphere, test_instances_in_sphere, \
                            test_labels_in_sphere, instances_in_sphere_libfolding, farthest_distance, \
                            position_training_instances_in_sphere, nb_training_instance_in_sphere = \
                                                    find_closest_counterfactual(ape, instance, growing_method)
    """ Generates or store instances in the area of the hyperfield and their corresponding labels """
    
    if instances_in_sphere_libfolding != []:
        # In case of categorical data, we transform categorical values into probability distribution (continuous values for libfolding)
        index_counterfactual_instances_in_sphere = ape.store_counterfactual_instances_in_sphere(training_instances_in_sphere, libfolding=True)
        counterfactual_instances_in_sphere = training_instances_in_sphere[index_counterfactual_instances_in_sphere]
        counterfactual_libfolding = instances_in_sphere_libfolding[index_counterfactual_instances_in_sphere]
        #print("start of unimodality test")
        unimodal_test = ape.check_test_unimodal_data(np.array(counterfactual_instances_in_sphere), training_instances_in_sphere, growing_sphere.radius,
                                                        counterfactual_libfolding=counterfactual_libfolding)
    else:
        counterfactual_instances_in_sphere = ape.store_counterfactual_instances_in_sphere(training_instances_in_sphere)
        unimodal_test = ape.check_test_unimodal_data(np.array(counterfactual_instances_in_sphere), training_instances_in_sphere, growing_sphere.radius)
    nb = 0
    min_instance_per_class = ape.nb_min_instance_per_class_in_sphere
    while not unimodal_test:
        # While the libfolding test is not able to declare that data are multimodal or unimodal we extend the number of instances that are generated
        min_instance_per_class *= 1.5
        training_instances_in_sphere, train_labels_in_sphere, percentage_distribution, instances_in_sphere_libfolding = \
                                    ape.generate_instances_inside_sphere(growing_sphere.radius, instance, ape.train_data, farthest_distance, 
                                                                                                min_instance_per_class, position_training_instances_in_sphere, 
                                                                                                nb_training_instance_in_sphere, libfolding=True,
                                                                                                growing_method=growing_method)
        
        if instances_in_sphere_libfolding != []:
            index_counterfactual_instances_in_sphere = ape.store_counterfactual_instances_in_sphere(training_instances_in_sphere, libfolding=True)
            counterfactual_instances_in_sphere = training_instances_in_sphere[index_counterfactual_instances_in_sphere]
            counterfactual_libfolding = instances_in_sphere_libfolding[index_counterfactual_instances_in_sphere]
            unimodal_test = ape.check_test_unimodal_data(np.array(counterfactual_instances_in_sphere), 
                                                            training_instances_in_sphere, growing_sphere.radius,
                                                            counterfactual_libfolding=counterfactual_libfolding)
        else:
            counterfactual_instances_in_sphere = ape.store_counterfactual_instances_in_sphere(training_instances_in_sphere)
            unimodal_test = ape.check_test_unimodal_data(np.array(counterfactual_instances_in_sphere), training_instances_in_sphere, growing_sphere.radius)
        if ape.verbose:
            print("nb times libfolding is not able to determine wheter datas are unimodal or multimodal:", nb)
            print("There are ", len(counterfactual_instances_in_sphere), " instances in the datas given to libfolding.")
            print()
        nb += 1
    """ Computes the labels for instances from the dataset to compute precision for explanation method """
    labels_instance_test_data = ape.black_box_predict(ape.test_data)
    nb_instance_test_data_label_as_target = sum(x == ape.target_class for x in labels_instance_test_data)
    
    
    anchor_precision, anchor_coverage, anchor_f2, anchor_explanation = ape.compute_anchor_precision_coverage(instance, 
                                    labels_instance_test_data, len(test_instances_in_sphere), 
                                    farthest_distance, percentage_distribution, nb_instance_test_data_label_as_target,
                                    growing_method=growing_method)
    ls_precision, ls_coverage, ls_f2, ls_explanation, ape.extended_radius = ape.compute_lime_extending_precision_coverage(training_instances_in_sphere, 
                                    instance, train_labels_in_sphere, growing_sphere, nb_features_employed,
                                    farthest_distance, growing_method)
    if ape.multimodal_results:
        # In case of multimodal data, we generate a rule based explanation and compute precision and coverage of this explanation model
        ape_precision, ape_coverage, ape_f2, ape_explanation = anchor_precision, anchor_coverage, anchor_f2, anchor_explanation

    else:
        # In case of unimodal data, we generate linear explanation and compute precision and coverage of this explanation model
        ape_precision, ape_coverage, ape_f2, ape_explanation = ls_precision, ls_coverage, ls_f2, ls_explanation
    return [ls_precision, anchor_precision, ape_precision], [ls_coverage, anchor_coverage, ape_coverage], [ls_f2, anchor_f2, ape_f2], \
                ape.multimodal_results, ape.extended_radius, ape.separability_index, ape.pvalue
 Generates or store instances in the area of the hyperfield and their corresponding labels 
Generates or store instances in the area of the hyperfield and their corresponding labels
Generates or store instances in the area of the hyperfield and their corresponding labels
def ape_center(ape, instance, growing_method='GF', nb_features_employed=None):
    closest_counterfactual, growing_sphere, training_instances_in_sphere, train_labels_in_sphere, test_instances_in_sphere, \
                            test_labels_in_sphere, instances_in_sphere_libfolding, farthest_distance, \
                            position_training_instances_in_sphere, nb_training_instance_in_sphere = \
                                                    find_closest_counterfactual(ape, instance, growing_method)
    if instances_in_sphere_libfolding != []:
        index_counterfactual_instances_in_sphere = ape.store_counterfactual_instances_in_sphere(training_instances_in_sphere, libfolding=True)
        counterfactual_instances_in_sphere = training_instances_in_sphere[index_counterfactual_instances_in_sphere]
        counterfactual_libfolding = instances_in_sphere_libfolding[index_counterfactual_instances_in_sphere]
        unimodal_test = ape.check_test_unimodal_data(np.array(counterfactual_instances_in_sphere), training_instances_in_sphere, growing_sphere.radius,
                                                        counterfactual_libfolding=counterfactual_libfolding)
    else:
        counterfactual_instances_in_sphere = ape.store_counterfactual_instances_in_sphere(training_instances_in_sphere)
        unimodal_test = ape.check_test_unimodal_data(np.array(counterfactual_instances_in_sphere), training_instances_in_sphere, growing_sphere.radius)
    nb = 0
    min_instance_per_class = ape.nb_min_instance_per_class_in_sphere
    while not unimodal_test:
        min_instance_per_class *= 1.5
        training_instances_in_sphere, train_labels_in_sphere, percentage_distribution, instances_in_sphere_libfolding = \
                                    ape.generate_instances_inside_sphere(growing_sphere.radius, instance, ape.train_data, farthest_distance, 
                                                                                                min_instance_per_class, position_training_instances_in_sphere, 
                                                                                                nb_training_instance_in_sphere, libfolding=True,
                                                                                                growing_method=growing_method)
        if instances_in_sphere_libfolding != []:
            index_counterfactual_instances_in_sphere = ape.store_counterfactual_instances_in_sphere(training_instances_in_sphere, libfolding=True)
            counterfactual_instances_in_sphere = training_instances_in_sphere[index_counterfactual_instances_in_sphere]
            counterfactual_libfolding = instances_in_sphere_libfolding[index_counterfactual_instances_in_sphere]
            unimodal_test = ape.check_test_unimodal_data(np.array(counterfactual_instances_in_sphere), 
                                                            training_instances_in_sphere, growing_sphere.radius,
                                                            counterfactual_libfolding=counterfactual_libfolding)
        else:
            counterfactual_instances_in_sphere = ape.store_counterfactual_instances_in_sphere(training_instances_in_sphere)
            unimodal_test = ape.check_test_unimodal_data(np.array(counterfactual_instances_in_sphere), training_instances_in_sphere, growing_sphere.radius)
        if ape.verbose:
            print("nb times libfolding is not able to determine wheter datas are unimodal or multimodal:", nb)
            print("There are ", len(counterfactual_instances_in_sphere), " instances in the datas given to libfolding.")
            print()
        nb += 1
    labels_instance_test_data = ape.black_box_predict(ape.test_data)
    nb_instance_test_data_label_as_target = sum(x == ape.target_class for x in labels_instance_test_data)
    anchor_precision, anchor_coverage, anchor_f2, anchor_explanation = ape.compute_anchor_precision_coverage(instance, 
                                    labels_instance_test_data, len(test_instances_in_sphere), 
                                    farthest_distance, percentage_distribution, nb_instance_test_data_label_as_target,
                                    growing_method=growing_method)
    ls_precision, ls_coverage, ls_f2, ls_explanation, ape.extended_radius = ape.compute_lime_extending_precision_coverage(training_instances_in_sphere, 
                                    instance, train_labels_in_sphere, growing_sphere, nb_features_employed,
                                    farthest_distance, growing_method)
    if ape.multimodal_results:
        ape_precision, ape_coverage, ape_f2, ape_explanation = anchor_precision, anchor_coverage, anchor_f2, anchor_explanation
    else:
        ape_precision, ape_coverage, ape_f2, ape_explanation = ls_precision, ls_coverage, ls_f2, ls_explanation
    return [ls_precision, anchor_precision, ape_precision], [ls_coverage, anchor_coverage, ape_coverage], [ls_f2, anchor_f2, ape_f2], \
                ape.multimodal_results, ape.extended_radius, ape.separability_index, ape.pvalue
def ape_center ( ape , instance , growing_method = 'GF' , nb_features_employed = None ) : closest_counterfactual , growing_sphere , training_instances_in_sphere , train_labels_in_sphere , test_instances_in_sphere , test_labels_in_sphere , instances_in_sphere_libfolding , farthest_distance , position_training_instances_in_sphere , nb_training_instance_in_sphere = find_closest_counterfactual ( ape , instance , growing_method ) if instances_in_sphere_libfolding != [ ] : index_counterfactual_instances_in_sphere = ape . store_counterfactual_instances_in_sphere ( training_instances_in_sphere , libfolding = True ) counterfactual_instances_in_sphere = training_instances_in_sphere [ index_counterfactual_instances_in_sphere ] counterfactual_libfolding = instances_in_sphere_libfolding [ index_counterfactual_instances_in_sphere ] unimodal_test = ape . check_test_unimodal_data ( np . array ( counterfactual_instances_in_sphere ) , training_instances_in_sphere , growing_sphere . radius , counterfactual_libfolding = counterfactual_libfolding ) else : counterfactual_instances_in_sphere = ape . store_counterfactual_instances_in_sphere ( training_instances_in_sphere ) unimodal_test = ape . check_test_unimodal_data ( np . array ( counterfactual_instances_in_sphere ) , training_instances_in_sphere , growing_sphere . radius ) nb = 0 min_instance_per_class = ape . nb_min_instance_per_class_in_sphere while not unimodal_test : min_instance_per_class *= 1.5 training_instances_in_sphere , train_labels_in_sphere , percentage_distribution , instances_in_sphere_libfolding = ape . generate_instances_inside_sphere ( growing_sphere . radius , instance , ape . train_data , farthest_distance , min_instance_per_class , position_training_instances_in_sphere , nb_training_instance_in_sphere , libfolding = True , growing_method = growing_method ) if instances_in_sphere_libfolding != [ ] : index_counterfactual_instances_in_sphere = ape . store_counterfactual_instances_in_sphere ( training_instances_in_sphere , libfolding = True ) counterfactual_instances_in_sphere = training_instances_in_sphere [ index_counterfactual_instances_in_sphere ] counterfactual_libfolding = instances_in_sphere_libfolding [ index_counterfactual_instances_in_sphere ] unimodal_test = ape . check_test_unimodal_data ( np . array ( counterfactual_instances_in_sphere ) , training_instances_in_sphere , growing_sphere . radius , counterfactual_libfolding = counterfactual_libfolding ) else : counterfactual_instances_in_sphere = ape . store_counterfactual_instances_in_sphere ( training_instances_in_sphere ) unimodal_test = ape . check_test_unimodal_data ( np . array ( counterfactual_instances_in_sphere ) , training_instances_in_sphere , growing_sphere . radius ) if ape . verbose : print ( "nb times libfolding is not able to determine wheter datas are unimodal or multimodal:" , nb ) print ( "There are " , len ( counterfactual_instances_in_sphere ) , " instances in the datas given to libfolding." ) print ( ) nb += 1 """ Computes the labels for instances from the dataset to compute precision for explanation method """ labels_instance_test_data = ape . black_box_predict ( ape . test_data ) nb_instance_test_data_label_as_target = sum ( x == ape . target_class for x in labels_instance_test_data ) anchor_precision , anchor_coverage , anchor_f2 , anchor_explanation = ape . compute_anchor_precision_coverage ( instance , labels_instance_test_data , len ( test_instances_in_sphere ) , farthest_distance , percentage_distribution , nb_instance_test_data_label_as_target , growing_method = growing_method ) ls_precision , ls_coverage , ls_f2 , ls_explanation , ape . extended_radius = ape . compute_lime_extending_precision_coverage ( training_instances_in_sphere , instance , train_labels_in_sphere , growing_sphere , nb_features_employed , farthest_distance , growing_method ) if ape . multimodal_results : ape_precision , ape_coverage , ape_f2 , ape_explanation = anchor_precision , anchor_coverage , anchor_f2 , anchor_explanation else : ape_precision , ape_coverage , ape_f2 , ape_explanation = ls_precision , ls_coverage , ls_f2 , ls_explanation return [ ls_precision , anchor_precision , ape_precision ] , [ ls_coverage , anchor_coverage , ape_coverage ] , [ ls_f2 , anchor_f2 , ape_f2 ] , ape . multimodal_results , ape . extended_radius , ape . separability_index , ape . pvalue
Generates or store instances in the area of the hyperfield and their corresponding labels
Generates or store instances in the area of the hyperfield and their corresponding labels
""" Generates or store instances in the area of the hyperfield and their corresponding labels """ # In case of categorical data, we transform categorical values into probability distribution (continuous values for libfolding) #print("start of unimodality test") # While the libfolding test is not able to declare that data are multimodal or unimodal we extend the number of instances that are generated """ Computes the labels for instances from the dataset to compute precision for explanation method """ # In case of multimodal data, we generate a rule based explanation and compute precision and coverage of this explanation model # In case of unimodal data, we generate linear explanation and compute precision and coverage of this explanation model
ape
instance
growing_method
nb_features_employed