Skip to content

Commit

Permalink
A test for the running example of celoe added
Browse files Browse the repository at this point in the history
  • Loading branch information
Demirrr committed Nov 7, 2024
1 parent 5a31338 commit 09f47e2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 211 deletions.
3 changes: 1 addition & 2 deletions examples/concept_learning_with_celoe_heuristic.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
221 changes: 12 additions & 209 deletions tests/test_owl_neural_retrieval.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down Expand Up @@ -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"),
Expand All @@ -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(
[
Expand All @@ -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(
[
Expand All @@ -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(
[
Expand All @@ -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(
Expand All @@ -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):
Expand Down Expand Up @@ -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

0 comments on commit 09f47e2

Please sign in to comment.