From 09f47e2ab502e838e20572358ae03dc513ad5b3f Mon Sep 17 00:00:00 2001 From: Caglar Demir Date: Thu, 7 Nov 2024 20:12:49 +0100 Subject: [PATCH] A test for the running example of celoe added --- .../concept_learning_with_celoe_heuristic.py | 3 +- tests/test_owl_neural_retrieval.py | 221 +----------------- 2 files changed, 13 insertions(+), 211 deletions(-) diff --git a/examples/concept_learning_with_celoe_heuristic.py b/examples/concept_learning_with_celoe_heuristic.py index 2dc165ed..348f412a 100644 --- a/examples/concept_learning_with_celoe_heuristic.py +++ b/examples/concept_learning_with_celoe_heuristic.py @@ -1,9 +1,8 @@ import json import os import random - from ontolearn.knowledge_base import KnowledgeBase -from ontolearn.concept_learner import CELOE +from ontolearn.learners import CELOE from ontolearn.heuristics import CELOEHeuristic from ontolearn.learning_problem import PosNegLPStandard from ontolearn.metrics import Accuracy diff --git a/tests/test_owl_neural_retrieval.py b/tests/test_owl_neural_retrieval.py index cd8b9357..390fee0c 100644 --- a/tests/test_owl_neural_retrieval.py +++ b/tests/test_owl_neural_retrieval.py @@ -40,11 +40,12 @@ from itertools import chain -class Test_Neural_Retrieval: +class TestNeuralRetrieval: + def __init__(self): + self.neural_owl_reasoner = TripleStoreNeuralReasoner(path_of_kb="KGs/Family/father.owl", gamma=0.8) def test_retrieval_single_individual_father_owl(self): - neural_owl_reasoner = TripleStoreNeuralReasoner(path_of_kb="KGs/Family/father.owl", gamma=0.8) - triples_about_anna = {(s, p, o) for s, p, o in neural_owl_reasoner.abox("http://example.com/father#anna")} + triples_about_anna = {(s, p, o) for s, p, o in self.neural_owl_reasoner.abox("http://example.com/father#anna")} sanity_checking = { (OWLNamedIndividual("http://example.com/father#anna"), @@ -77,8 +78,6 @@ def test_retrieval_named_concepts_family(self): assert avg_jaccard_index / len(benchmark_dataset) == 1.0 def test_de_morgan_male_and_father_father(self): - neural_owl_reasoner = TripleStoreNeuralReasoner(path_of_kb="KGs/Family/father.owl", gamma=0.8) - male_and_father = OWLObjectIntersectionOf( [ OWLClass("http://example.com/father#male"), @@ -88,7 +87,7 @@ def test_de_morgan_male_and_father_father(self): filler=OWLObjectOneOf( OWLNamedIndividual("http://example.com/father#anna") ))]) - individuals = set(neural_owl_reasoner.instances(male_and_father)) + individuals = set(self.neural_owl_reasoner.instances(male_and_father)) not_female_or_not_mother = OWLObjectComplementOf( OWLObjectUnionOf( [ @@ -108,13 +107,11 @@ def test_de_morgan_male_and_father_father(self): ) ) print(individuals) - individuals_2 = set(neural_owl_reasoner.instances(not_female_or_not_mother)) + individuals_2 = set(self.neural_owl_reasoner.instances(not_female_or_not_mother)) assert individuals == individuals_2 def test_de_morgan_male_and_has_daughter_family(self): - neural_owl_reasoner = TripleStoreNeuralReasoner( - path_of_kb="KGs/Family/family-benchmark_rich_background.owl", gamma=0.8 - ) + prefix = "http://www.benchmark.org/family#" male_and_has_daughter = OWLObjectIntersectionOf( [ @@ -126,7 +123,7 @@ def test_de_morgan_male_and_has_daughter_family(self): ), ] ) - individuals = set(neural_owl_reasoner.instances(male_and_has_daughter)) + individuals = set(self.neural_owl_reasoner.instances(male_and_has_daughter)) not_male_or_not_has_daughter = OWLObjectComplementOf( OWLObjectUnionOf( [ @@ -141,13 +138,10 @@ def test_de_morgan_male_and_has_daughter_family(self): ] ) ) - individuals_2 = set(neural_owl_reasoner.instances(not_male_or_not_has_daughter)) + individuals_2 = set(self.neural_owl_reasoner.instances(not_male_or_not_has_daughter)) assert individuals == individuals_2 def test_de_morgan_not_father_or_brother_family(self): - neural_owl_reasoner = TripleStoreNeuralReasoner( - path_of_kb="KGs/Family/family-benchmark_rich_background.owl", gamma=0.8 - ) prefix = "http://www.benchmark.org/family#" not_father_or_brother = OWLObjectComplementOf( OWLObjectUnionOf( @@ -157,14 +151,14 @@ def test_de_morgan_not_father_or_brother_family(self): ] ) ) - individuals = set(neural_owl_reasoner.instances(not_father_or_brother)) + individuals = set(self.neural_owl_reasoner.instances(not_father_or_brother)) father_and_brother = OWLObjectIntersectionOf( [ OWLObjectComplementOf(OWLClass(prefix + "Father")), OWLObjectComplementOf(OWLClass(prefix + "Brother")), ] ) - individuals_2 = set(neural_owl_reasoner.instances(father_and_brother)) + individuals_2 = set(self.neural_owl_reasoner.instances(father_and_brother)) assert individuals == individuals_2 def test_complement_for_all_named_concepts_family(self): @@ -371,195 +365,4 @@ def concept_retrieval(retriever_func, c) -> Tuple[Set[str], float]: }) df = pd.DataFrame(data) - assert df["Jaccard Similarity"].mean() == 1.0 - - def old_regression_concept_combinations_family(self): - # Helper functions - - def concept_reducer(concepts, opt): - result = set() - for i in concepts: - for j in concepts: - result.add(opt((i, j))) - return result - - def concept_reducer_properties(concepts, opt, cardinality=2): - result = set() - for i in concepts: - for j in object_properties_and_inverse: - if opt == OWLObjectMinCardinality or opt == OWLObjectMaxCardinality: - result.add(opt(cardinality, j, i)) - continue - result.add(opt(j, i)) - return result - - def concept_to_retrieval(concepts, retriever) -> List[Tuple[float, Set[str]]]: - results = [] - for c in concepts: - start_time_ = time.time() - retrieval = {i.str for i in retriever.individuals(c)} - results.append((time.time() - start_time_, retrieval)) - return results - - def retrieval_eval(expressions, y, yhat, verbose=1): - assert len(y) == len(yhat) - similarities = [] - runtime_diff = [] - number_of_concepts = len(expressions) - for expressions, y_report_i, yhat_report_i in zip(expressions, y, yhat): - runtime_y_i, y_i = y_report_i - runtime_yhat_i, yhat_i = yhat_report_i - - jaccard_sim = jaccard_similarity(y_i, yhat_i) - runtime_benefits = runtime_y_i - runtime_yhat_i - if verbose > 0: - print( - f"Concept:{expressions}\tTrue Size:{len(y_i)}\tPredicted Size:{len(yhat_i)}\tRetrieval Similarity:{jaccard_sim}\tRuntime Benefit:{runtime_benefits:.3f}" - ) - similarities.append(jaccard_sim) - runtime_diff.append(runtime_benefits) - avg_jaccard_sim = sum(similarities) / len(similarities) - avg_runtime_benefits = sum(runtime_diff) / len(runtime_diff) - return number_of_concepts, avg_jaccard_sim, avg_runtime_benefits - - symbolic_kb = KnowledgeBase( - path="KGs/Family/family-benchmark_rich_background.owl" - ) - # symbolic_kb = TripleStore(url="http://localhost:3030/family") - neural_owl_reasoner = TripleStoreNeuralReasoner( - path_of_kb="KGs/Family/family-benchmark_rich_background.owl", gamma=0.8 - ) - object_properties = {i for i in symbolic_kb.get_object_properties()} - object_properties_inverse = { - i.get_inverse_property() for i in object_properties - } - object_properties_and_inverse = object_properties.union( - object_properties_inverse - ) - # named concepts - nc = {i for i in symbolic_kb.get_concepts()} - # negated named concepts - nnc = {i.get_object_complement_of() for i in nc} - # union of named and negated named concepts - unnc = nc.union(nnc) - - unions = concept_reducer(nc, opt=OWLObjectUnionOf) - intersections = concept_reducer(nc, opt=OWLObjectIntersectionOf) - unions_unnc = concept_reducer(unnc, opt=OWLObjectUnionOf) - intersections_unnc = concept_reducer(unnc, opt=OWLObjectIntersectionOf) - exist_unnc = concept_reducer_properties(unnc, opt=OWLObjectSomeValuesFrom) - for_all_unnc = concept_reducer_properties(unnc, opt=OWLObjectAllValuesFrom) - - min_cardinality_unnc_1, min_cardinality_unnc_2, min_cardinality_unnc_3 = ( - concept_reducer_properties(unnc, opt=OWLObjectMinCardinality, cardinality=i) - for i in [1, 2, 3] - ) - max_cardinality_unnc_1, max_cardinality_unnc_2, max_cardinality_unnc_3 = ( - concept_reducer_properties(unnc, opt=OWLObjectMaxCardinality, cardinality=i) - for i in [1, 2, 3] - ) - - nc_retrieval_results = retrieval_eval( - expressions=nc, - y=concept_to_retrieval(nc, symbolic_kb), - yhat=concept_to_retrieval(nc, neural_owl_reasoner), - ) - unions_nc_retrieval_results = retrieval_eval( - expressions=unions, - y=concept_to_retrieval(unions, symbolic_kb), - yhat=concept_to_retrieval(unions, neural_owl_reasoner), - ) - intersections_nc_retrieval_results = retrieval_eval( - expressions=intersections, - y=concept_to_retrieval(intersections, symbolic_kb), - yhat=concept_to_retrieval(intersections, neural_owl_reasoner), - ) - nnc_retrieval_results = retrieval_eval( - expressions=nnc, - y=concept_to_retrieval(nnc, symbolic_kb), - yhat=concept_to_retrieval(nnc, neural_owl_reasoner), - ) - unnc_retrieval_results = retrieval_eval( - expressions=unnc, - y=concept_to_retrieval(unnc, symbolic_kb), - yhat=concept_to_retrieval(unnc, neural_owl_reasoner), - ) - unions_unnc_retrieval_results = retrieval_eval( - expressions=unions_unnc, - y=concept_to_retrieval(unions_unnc, symbolic_kb), - yhat=concept_to_retrieval(unions_unnc, neural_owl_reasoner), - ) - intersections_unnc_retrieval_results = retrieval_eval( - expressions=intersections_unnc, - y=concept_to_retrieval(intersections_unnc, symbolic_kb), - yhat=concept_to_retrieval(intersections_unnc, neural_owl_reasoner), - ) - exist_unnc_retrieval_results = retrieval_eval( - expressions=exist_unnc, - y=concept_to_retrieval(exist_unnc, symbolic_kb), - yhat=concept_to_retrieval(exist_unnc, neural_owl_reasoner), - ) - for_all_unnc_retrieval_results = retrieval_eval( - expressions=for_all_unnc, - y=concept_to_retrieval(for_all_unnc, symbolic_kb), - yhat=concept_to_retrieval(for_all_unnc, neural_owl_reasoner), - ) - - ( - min_cardinality_unnc_1_retrieval_results, - min_cardinality_unnc_2_retrieval_results, - min_cardinality_unnc_3_retrieval_results, - ) = ( - retrieval_eval( - expressions=expressions, - y=concept_to_retrieval(expressions, symbolic_kb), - yhat=concept_to_retrieval(expressions, neural_owl_reasoner), - ) - for expressions in [ - min_cardinality_unnc_1, - min_cardinality_unnc_2, - min_cardinality_unnc_3, - ] - ) - - ( - max_cardinality_unnc_1_retrieval_results, - max_cardinality_unnc_2_retrieval_results, - max_cardinality_unnc_3_retrieval_results, - ) = ( - retrieval_eval( - expressions=expressions, - y=concept_to_retrieval(expressions, symbolic_kb), - yhat=concept_to_retrieval(expressions, neural_owl_reasoner), - ) - for expressions in [ - max_cardinality_unnc_1, - max_cardinality_unnc_2, - max_cardinality_unnc_3, - ] - ) - - results = { - "nc_retrieval_results": nc_retrieval_results, - "unions_nc_retrieval_results": unions_nc_retrieval_results, - "intersections_nc_retrieval_results": intersections_nc_retrieval_results, - "nnc_retrieval_results": nnc_retrieval_results, - "unnc_retrieval_results": unnc_retrieval_results, - "unions_unnc_retrieval_results": unions_unnc_retrieval_results, - "intersections_unnc_retrieval_results": intersections_unnc_retrieval_results, - "exist_unnc_retrieval_results": exist_unnc_retrieval_results, - "for_all_unnc_retrieval_results": for_all_unnc_retrieval_results, - "min_cardinality_unnc_1_retrieval_results": min_cardinality_unnc_1_retrieval_results, - "min_cardinality_unnc_2_retrieval_results": min_cardinality_unnc_2_retrieval_results, - "min_cardinality_unnc_3_retrieval_results": min_cardinality_unnc_3_retrieval_results, - "max_cardinality_unnc_1_retrieval_results": max_cardinality_unnc_1_retrieval_results, - "max_cardinality_unnc_2_retrieval_results": max_cardinality_unnc_2_retrieval_results, - "max_cardinality_unnc_3_retrieval_results": max_cardinality_unnc_3_retrieval_results, - } - - for key, value in results.items(): - number_of_concepts, avg_jaccard_sim, avg_runtime_benefits = value - print( - f"Concepts:{number_of_concepts}\tAverage Jaccard Similarity:{avg_jaccard_sim}\tAverage Runtime Benefits:{avg_runtime_benefits}" - ) - assert avg_jaccard_sim == 1.0 + assert df["Jaccard Similarity"].mean() == 1.0 \ No newline at end of file