ontolearn.base_concept_learner

Base classes of concept learners.

Module Contents

Classes

BaseConceptLearner

@TODO: CD: Why should this class inherit from AbstractConceptNode ?

RefinementBasedConceptLearner

Base class for refinement based Concept Learning approaches.

Attributes

Factory

logger

ontolearn.base_concept_learner.Factory
ontolearn.base_concept_learner.logger
class ontolearn.base_concept_learner.BaseConceptLearner(knowledge_base: KnowledgeBase, reasoner: owlapy.owl_reasoner.OWLReasoner | None = None, quality_func: AbstractScorer | None = None, max_num_of_concepts_tested: int | None = None, max_runtime: int | None = None, terminate_on_goal: bool | None = None)[source]

Bases: Generic[_N]

@TODO: CD: Why should this class inherit from AbstractConceptNode ? @TODO: CD: This class should be redefined. An owl class expression learner does not need to be a search based model.

Base class for Concept Learning approaches.

Learning problem definition, Let
  • K = (TBOX, ABOX) be a knowledge base.

  • ALCConcepts be a set of all ALC concepts.

  • hypotheses be a set of ALC concepts : hypotheses subseteq ALCConcepts.

  • K_N be a set of all instances.

  • K_C be a set of concepts defined in TBOX: K_C subseteq ALCConcepts

  • K_R be a set of properties/relations.

  • E^+, E^- be a set of positive and negative instances and the followings hold

    ** E^+ cup E^- subseteq K_N ** E^+ cap E^- = emptyset

The goal is to learn a set of concepts $hypotheses subseteq ALCConcepts$ such that

∀ H in hypotheses: { (K wedge H models E^+) wedge neg( K wedge H models E^-) }.

kb

The knowledge base that the concept learner is using.

Type:

KnowledgeBase

quality_func
Type:

AbstractScorer

max_num_of_concepts_tested
Type:

int

terminate_on_goal

Whether to stop the algorithm if a perfect solution is found.

Type:

bool

max_runtime

Limit to stop the algorithm after n seconds.

Type:

int

_number_of_tested_concepts

Yes, you got it. This stores the number of tested concepts.

Type:

int

reasoner

The reasoner that this model is using.

Type:

OWLReasoner

start_time

The time when fit() starts the execution. Used to calculate the total time fit() takes to execute.

Type:

float

property number_of_tested_concepts
__slots__ = ('kb', 'reasoner', 'quality_func', 'max_num_of_concepts_tested', 'terminate_on_goal',...
name: ClassVar[str]
kb: KnowledgeBase
quality_func: AbstractScorer | None
max_num_of_concepts_tested: int | None
terminate_on_goal: bool | None
max_runtime: int | None
start_time: float | None
abstract clean()[source]

Clear all states of the concept learner.

train(*args, **kwargs)[source]

Train RL agent on learning problems.

Returns:

self.

terminate()[source]

This method is called when the search algorithm terminates.

If INFO log level is enabled, it prints out some statistics like runtime and concept tests to the logger.

Returns:

The concept learner object itself.

construct_learning_problem(type_: Type[_X], xargs: Tuple, xkwargs: Dict) _X[source]

Construct learning problem of given type based on args and kwargs. If a learning problem is contained in args or the learning_problem kwarg, it is used. otherwise, a new learning problem of type type_ is created with args and kwargs as parameters.

Parameters:
  • type – Type of the learning problem.

  • xargs – The positional arguments.

  • xkwargs – The keyword arguments.

Returns:

The learning problem.

abstract fit(*args, **kwargs)[source]

Run the concept learning algorithm according to its configuration.

Once finished, the results can be queried with the best_hypotheses function.

abstract best_hypotheses(n=10) Iterable[owlapy.class_expression.OWLClassExpression][source]

Get the current best found hypotheses according to the quality.

Parameters:

n – Maximum number of results.

Returns:

Iterable with hypotheses in form of search tree nodes.

predict(individuals: List[owlapy.owl_individual.OWLNamedIndividual], hypotheses: owlapy.class_expression.OWLClassExpression | List[_N | owlapy.class_expression.OWLClassExpression] | None = None, axioms: List[owlapy.owl_axiom.OWLAxiom] | None = None, n: int = 10) pandas.DataFrame[source]

@TODO: CD: Predicting an individual can be done by a retrieval function not a concept learner @TODO: A concept learner learns an owl class expression. @TODO: This learned expression can be used as a binary predictor.

Creates a binary data frame showing for each individual whether it is entailed in the given hypotheses (class expressions). The individuals do not have to be in the ontology/knowledge base yet. In that case, axioms describing these individuals must be provided.

The state of the knowledge base/ontology is not changed, any provided axioms will be removed again.

Parameters:
  • individuals – A list of individuals/instances.

  • hypotheses – (Optional) A list of search tree nodes or class expressions. If not provided, the current BaseConceptLearner.best_hypothesis() of the concept learner are used.

  • axioms – (Optional) A list of axioms that are not in the current knowledge base/ontology. If the individual list contains individuals that are not in the ontology yet, axioms describing these individuals must be provided. The argument can also be used to add arbitrary axioms to the ontology for the prediction.

  • n – Integer denoting number of ALC concepts to extract from search tree if hypotheses=None.

Returns:

Pandas data frame with dimensions |individuals|*|hypotheses| indicating for each individual and each hypothesis whether the individual is entailed in the hypothesis.

save_best_hypothesis(n: int = 10, path: str = 'Predictions', rdf_format: str = 'rdfxml') None[source]

Serialise the best hypotheses to a file. @TODO: CD: This function should be deprecated. @TODO: CD: Saving owl class expressions into disk should be disentangled from a concept earner

Parameters:
  • n – Maximum number of hypotheses to save.

  • path – Filename base (extension will be added automatically).

  • rdf_format – Serialisation format. currently supported: “rdfxml”.

load_hypotheses(path: str) Iterable[owlapy.class_expression.OWLClassExpression][source]

@TODO: CD: This function should be deprecated. @TODO: CD: Loading owl class expressions from disk should be disentangled from a concept earner

Loads hypotheses (class expressions) from a file saved by BaseConceptLearner.save_best_hypothesis().

Parameters:

path – Path to the file containing hypotheses.

static verbalize(predictions_file_path: str)[source]

@TODO:CD: this function should be removed from this class. This should be defined at best as a static func.

class ontolearn.base_concept_learner.RefinementBasedConceptLearner(knowledge_base: KnowledgeBase, reasoner: owlapy.owl_reasoner.OWLReasoner | None = None, refinement_operator: BaseRefinement | None = None, heuristic_func: AbstractHeuristic | None = None, quality_func: AbstractScorer | None = None, max_num_of_concepts_tested: int | None = None, max_runtime: int | None = None, terminate_on_goal: bool | None = None, iter_bound: int | None = None, max_child_length: int | None = None, root_concept: owlapy.class_expression.OWLClassExpression | None = None)[source]

Bases: BaseConceptLearner[_N]

Base class for refinement based Concept Learning approaches.

kb

The knowledge base that the concept learner is using.

Type:

KnowledgeBase

quality_func
Type:

AbstractScorer

max_num_of_concepts_tested
Type:

int

terminate_on_goal

Whether to stop the algorithm if a perfect solution is found.

Type:

bool

max_runtime

Limit to stop the algorithm after n seconds.

Type:

int

_number_of_tested_concepts

Yes, you got it. This stores the number of tested concepts.

Type:

int

reasoner

The reasoner that this model is using.

Type:

OWLReasoner

start_time

The time when fit() starts the execution. Used to calculate the total time fit() takes to execute.

Type:

float

iter_bound

Limit to stop the algorithm after n refinement steps are done.

Type:

int

heuristic_func

Function to guide the search heuristic.

Type:

AbstractHeuristic

operator

Operator used to generate refinements.

Type:

BaseRefinement

start_class

The starting class expression for the refinement operation.

Type:

OWLClassExpression

max_child_length

Limit the length of concepts generated by the refinement operator.

Type:

int

__slots__ = ('operator', 'heuristic_func', 'max_child_length', 'start_class', 'iter_bound')
operator: BaseRefinement | None
heuristic_func: AbstractHeuristic | None
max_child_length: int | None
start_class: owlapy.class_expression.OWLClassExpression | None
iter_bound: int | None
terminate()[source]

This method is called when the search algorithm terminates.

If INFO log level is enabled, it prints out some statistics like runtime and concept tests to the logger.

Returns:

The concept learner object itself.

abstract next_node_to_expand(*args, **kwargs)[source]

Return from the search tree the most promising search tree node to use for the next refinement step.

Returns:

Next search tree node to refine.

Return type:

_N

abstract downward_refinement(*args, **kwargs)[source]

Execute one refinement step of a refinement based learning algorithm.

Parameters:

node (_N) – the search tree node on which to refine.

Returns:

Refinement results as new search tree nodes (they still need to be added to the tree).

Return type:

Iterable[_N]

abstract show_search_tree(heading_step: str, top_n: int = 10) None[source]

A debugging function to print out the current search tree and the current n best found hypotheses to standard output.

Parameters:
  • heading_step – A message to display at the beginning of the output.

  • top_n – The number of current best hypotheses to print out.