dice-group / Ontolearn

Ontolearn is an open-source software library for explainable structured machine learning in Python. It learns OWL class expressions from positive and negative examples.
https://ontolearn-docs-dice-group.netlify.app/index.html
MIT License
43 stars 9 forks source link

Error encountered running CELOE while loading knowledge graph using triple store function. #496

Closed Quannz closed 16 hours ago

Quannz commented 1 day ago

The error:

Warning: could not find ontolearn/logging.conf
Target concept:  QALD9_plus_dbpedia
Traceback (most recent call last):
  File "/local/upb/users/q/quannian/profiles/unix/cs/Drill/Ontolearn-0.7.3/Ontolearn/examples/CELOE/concept_learning_with_celoe_heuristic.py", line 66, in <module>
    op = ModifiedCELOERefinement(knowledge_base=target_kb, use_negation=False, use_all_constructor=False)
  File "/upb/users/q/quannian/profiles/unix/cs/.conda/envs/ontolearn0.8.0/lib/python3.10/site-packages/ontolearn/refinement_operators.py", line 357, in __init__
    self._setup()
  File "/upb/users/q/quannian/profiles/unix/cs/.conda/envs/ontolearn0.8.0/lib/python3.10/site-packages/ontolearn/refinement_operators.py", line 371, in _setup
    num = sum(1 for _ in zip(self.kb.get_object_property_values(ind, prop), range(self.card_limit)))
AttributeError: 'TripleStore' object has no attribute 'get_object_property_values'. Did you mean: 'get_object_properties'?

The code I used when I encountered this problem:

import json
import os
import random

from ontolearn.knowledge_base import KnowledgeBase
from ontolearn.concept_learner import CELOE
from ontolearn.heuristics import CELOEHeuristic
from ontolearn.learning_problem import PosNegLPStandard
from ontolearn.metrics import Accuracy
from owlapy.owl_individual import OWLNamedIndividual, IRI
from owlapy.class_expression import OWLClass
from ontolearn.refinement_operators import ModifiedCELOERefinement
from ontolearn.utils import setup_logging
from ontolearn.triple_store import TripleStore
from owlready2 import get_ontology
setup_logging()

try:
    os.chdir("examples")
except FileNotFoundError:
    pass

with open('/upb/users/q/quannian/profiles/unix/cs/Drill/Ontolearn-0.7.3/Ontolearn/LPs/QALD9DB/TandF_MST5_reverse.json') as json_file:
    settings = json.load(json_file)

kb = TripleStore(url="http://eml4u.cs.uni-paderborn.de:9060/sparql")
random.seed(0)

# noinspection DuplicatedCode
for str_target_concept, examples in settings['problems'].items():
    p = set(examples['positive_examples'])
    n = set(examples['negative_examples'])
    print('Target concept: ', str_target_concept)
    target_kb = kb

    typed_pos = set(map(OWLNamedIndividual, map(IRI.create, p)))
    typed_neg = set(map(OWLNamedIndividual, map(IRI.create, n)))
    lp = PosNegLPStandard(pos=typed_pos, neg=typed_neg)

    qual = Accuracy()
    heur = CELOEHeuristic(expansionPenaltyFactor=0.05, startNodeBonus=1.0, nodeRefinementPenalty=0.01)
    op = ModifiedCELOERefinement(knowledge_base=target_kb, use_negation=False, use_all_constructor=False)

    model = CELOE(knowledge_base=target_kb,
                  max_runtime=600,
                  refinement_operator=op,
                  quality_func=qual,
                  heuristic_func=heur,
                  max_num_of_concepts_tested=10_000_000_000,
                  iter_bound=10_000_000_000)
    model.fit(lp)

    model.save_best_hypothesis(n=3, path='Predictions_{0}'.format(str_target_concept))
    # Get Top n hypotheses
    hypotheses = list(model.best_hypotheses(n=3))
    # Use hypotheses as binary function to label individuals.
    predictions = model.predict(individuals=list(typed_pos | typed_neg),
                                hypotheses=hypotheses)
    # print(predictions)
    [print(_) for _ in hypotheses]
    # exit(1)
Demirrr commented 1 day ago

Thank you for the issue! Currently, CELOE does not support TripleStore