Skip to content
Snippets Groups Projects
Commit 15696cd7 authored by AjUm-HEIDI's avatar AjUm-HEIDI
Browse files

Update the csv order

parent f6144c50
No related branches found
No related tags found
No related merge requests found
......@@ -21,24 +21,6 @@ from typing import Final
from ontolearn.abstracts import AbstractFitness
from ontolearn.ea_utils import Tree
class LinearPressureFitness(AbstractFitness):
"""Linear parametric parsimony pressure."""
__slots__ = 'gain', 'penalty'
name: Final = 'Linear_Pressure_Fitness'
def __init__(self, gain: float = 2048.0, penalty: float = 1.0):
self.gain = gain
self.penalty = penalty
def apply(self, individual: Tree):
quality = individual.quality.values[0]
fitness = self.gain*quality - self.penalty*len(individual)
# print(self.gain, quality, self.gain*quality, len(individual))
individual.fitness.values = (round(fitness, 5),)
class DiscriminativeExplainer:
""" An abstract class which represent an explainer. An explainer should be able to use a label to generate a
......@@ -86,7 +68,7 @@ class DiscriminativeExplainer:
return OWLObjectMinCardinality(ce.get_cardinality(), ce.get_property(), ce2)
return ce
def __init__(self, gnn, data: Union[HeteroData, Data], namespace = "http://example.org/", owl_graph_path = "./owlGraphs/example.owl", generate_new_owl_file: bool = False, create_nominals: bool = False, add_edge_counts: bool = False, create_data_properties_as_boolean: bool = False, full_edge_name: bool = False, ignore_nodes: bool = False, high_level_concepts: dict = None, create_high_level_concepts_as_boolean: bool = False,) -> None:
def __init__(self, gnn, data: Union[HeteroData, Data], namespace = "http://example.org/", owl_graph_path = "./owlGraphs/example.owl", generate_new_owl_file: bool = False, create_nominals: bool = False, add_edge_counts: bool = False, create_data_properties_as_boolean: bool = False, full_edge_name: bool = False, ignore_nodes: bool = False, high_level_concepts: dict = None, create_high_level_concepts_as_boolean: bool = False, add_false_values=False) -> None:
"""Initializes the explainer based on the given GNN and the Dataset. After the initialization the object should
be able to produce explanations of single labels.
......@@ -115,7 +97,7 @@ class DiscriminativeExplainer:
if generate_new_owl_file and os.path.isfile(self.owl_graph_path):
os.remove(self.owl_graph_path)
if not os.path.isfile(self.owl_graph_path):
self.owlGraph = convert_to_owl(data=self.data, namespace=self.namespace, owlGraphPath=self.owl_graph_path, high_level_concepts=self.high_level_concepts, create_nominals=create_nominals, add_edge_counts=add_edge_counts, create_data_properties_as_boolean = create_data_properties_as_boolean, full_edge_name=full_edge_name, ignore_nodes=self.ignore_nodes, create_high_level_concepts_as_boolean=create_high_level_concepts_as_boolean)
self.owlGraph = convert_to_owl(data=self.data, namespace=self.namespace, owlGraphPath=self.owl_graph_path, high_level_concepts=self.high_level_concepts, create_nominals=create_nominals, add_edge_counts=add_edge_counts, create_data_properties_as_boolean = create_data_properties_as_boolean, full_edge_name=full_edge_name, ignore_nodes=self.ignore_nodes, create_high_level_concepts_as_boolean=create_high_level_concepts_as_boolean, add_false_values=add_false_values)
self.owlGraph.buildGraph()
self.knowledge_base = KnowledgeBase(path=self.owl_graph_path)
......@@ -126,8 +108,7 @@ class DiscriminativeExplainer:
debug: Optional[bool] = False,
max_runtime: Optional[int] = 60,
num_generations: Optional[int] = 600,
quality_func: Optional[AbstractScorer] = None,
length_penalty: Optional[int] = 1.0) -> OWLClassExpression:
quality_func: Optional[AbstractScorer] = None) -> OWLClassExpression:
"""Explains based on the GNN a given label. The explanation is in the form of a Class Expression.
Args:
......@@ -144,7 +125,7 @@ class DiscriminativeExplainer:
if quality_func is None:
quality_func = F1()
self.model = EvoLearner(knowledge_base=self.knowledge_base, use_data_properties=use_data_properties, max_runtime=max_runtime, num_generations=num_generations, quality_func=quality_func, population_size=1000, fitness_func=LinearPressureFitness(penalty=length_penalty))
self.model = EvoLearner(knowledge_base=self.knowledge_base, use_data_properties=use_data_properties, max_runtime=max_runtime, num_generations=num_generations, quality_func=quality_func, population_size=1000)
positive_examples = []
negative_examples = []
......
......@@ -69,8 +69,17 @@ def append_to_csv_file(results, results_dir, dataset_key, num_groups, create_hig
filename = os.path.join(results_dir, f"{dataset_key}_results.csv")
with open(filename, mode='a', newline='', encoding='utf-8') as csvfile:
fieldnames = ['Dataset', 'Number of Groups', 'Create High Level Concepts As Boolean', 'Label Name',
'Hypothesis', 'Accuracy', 'Recall', 'Precision', 'F1 Score', 'Length', 'Explain Time']
fieldnames = ['Dataset',
'Number of Groups',
'F1 Score',
'Length',
'Create High Level Concepts As Boolean',
'Label Name',
'Hypothesis',
'Accuracy',
'Recall',
'Precision',
'Explain Time']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
if write_header:
......@@ -80,14 +89,14 @@ def append_to_csv_file(results, results_dir, dataset_key, num_groups, create_hig
writer.writerow({
'Dataset': dataset_key,
'Number of Groups': num_groups,
'F1 Score': data['evaluation'].get('F1', 'N/A'),
'Length': data['length'],
'Create High Level Concepts As Boolean': create_high_level_concepts_as_boolean,
'Label Name': data['label_name'],
'Hypothesis': data['hypothesis'],
'Accuracy': data['evaluation'].get('Accuracy', 'N/A'),
'Recall': data['evaluation'].get('Recall', 'N/A'),
'Precision': data['evaluation'].get('Precision', 'N/A'),
'F1 Score': data['evaluation'].get('F1', 'N/A'),
'Length': data['length'],
'Explain Time': data['explain_time']
})
......@@ -157,9 +166,16 @@ def summarize_aggregated_results(aggregated_results, results_dir, dataset_name):
with open(summary_filename, mode="w", newline="", encoding="utf-8") as csvfile:
fieldnames = [
"Label Name", "Number of Groups", "Best Hypothesis", "Best F1 Score",
"Average F1 Score", "Best Accuracy", "Average Accuracy", "Length at Best F1",
"Average Length", "Average Explain Time"
"Label Name",
"Number of Groups",
"Best F1 Score",
"Average F1 Score",
"Best Accuracy",
"Average Accuracy",
"Length at Best F1",
"Average Length",
"Best Hypothesis",
"Average Explain Time"
]
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
......@@ -173,13 +189,13 @@ def summarize_aggregated_results(aggregated_results, results_dir, dataset_name):
writer.writerow({
"Label Name": data["label_name"],
"Number of Groups": num_groups,
"Best Hypothesis": data["best_hypothesis"],
"Best F1 Score": data["best_F1"],
"Average F1 Score": avg_f1,
"Best Accuracy": data["best_accuracy"],
"Average Accuracy": avg_accuracy,
"Length at Best F1": data["length_at_best_f1"],
"Average Length": avg_length,
"Best Hypothesis": data["best_hypothesis"],
"Average Explain Time": avg_time
})
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment