Skip to content

Commit

Permalink
Merge pull request #287 from dice-group/general_adjustments
Browse files Browse the repository at this point in the history
General adjustments
  • Loading branch information
Demirrr authored Sep 16, 2023
2 parents 448da7f + 00e59c8 commit 372f6d4
Show file tree
Hide file tree
Showing 99 changed files with 1,014 additions and 1,745 deletions.
4 changes: 0 additions & 4 deletions MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,5 @@ recursive-include examples *.py
recursive-include examples *.json
recursive-include examples *.conf
recursive-include tests *.py
include sortedcontainers-stubs/py.typed
recursive-include sortedcontainers-stubs *.pyi
include superprop/py.typed
recursive-include superprop *.pyi
prune *.egg-info
prune .tox
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,13 @@ the learning algorithms. Also in the [tests](https://github.com/dice-group/Ontol
For more detailed instructions we suggest to follow the [guides](https://ontolearn-docs-dice-group.netlify.app/usage/03_algorithm.html) in the documentation.

Below we give a simple example on using CELOE to learn class expressions for a small dataset.

```python
from ontolearn.concept_learner import CELOE
from ontolearn.model_adapter import ModelAdapter
from owlapy.model import OWLNamedIndividual, IRI
from owlapy.namespaces import Namespaces
from owlapy.render import DLSyntaxObjectRenderer
from ontolearn.owlapy.model import OWLNamedIndividual, IRI
from ontolearn.owlapy.namespaces import Namespaces
from ontolearn.owlapy.render import DLSyntaxObjectRenderer
from examples.experiments_standard import ClosedWorld_ReasonerFactory

NS = Namespaces('ex', 'http://example.com/father#')
Expand Down
4 changes: 2 additions & 2 deletions deploy_cl.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
from ontolearn.learning_problem import PosNegLPStandard
from ontolearn.refinement_operators import ModifiedCELOERefinement
from ontolearn.value_splitter import EntropyValueSplitter, BinningValueSplitter
from owlapy.model import OWLNamedIndividual, IRI
from owlapy.render import DLSyntaxObjectRenderer
from ontolearn.owlapy.model import OWLNamedIndividual, IRI
from ontolearn.owlapy.render import DLSyntaxObjectRenderer

metrics = {'F1': F1,
'Accuracy': Accuracy,
Expand Down
9 changes: 5 additions & 4 deletions docs/usage/01_knowledge_base.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,14 @@ before fitting a model.
It can be done as follows:

<!--pytest-codeblocks:cont-->

```python
from owlapy.model import OWLClass
from owlapy.model import IRI
from ontolearn.owlapy.model import OWLClass
from ontolearn.owlapy.model import IRI

iri = IRI('http://example.com/father#', 'Father')
iri = IRI('http://example.com/father#', 'Father')
father_concept = OWLClass(iri)
concepts_to_ignore = {father_concept} # you can add more than 1
concepts_to_ignore = {father_concept} # you can add more than 1

new_kb = kb.ignore_and_copy(ignored_classes=concepts_to_ignore)
```
Expand Down
5 changes: 3 additions & 2 deletions docs/usage/02_learning_problem.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ describe) are stefan, markus, and martin. And our negative examples
could write the following Python code:

<!--pytest-codeblocks:cont-->

```python
from owlapy.namespaces import Namespaces
from owlapy.model import OWLNamedIndividual, IRI
from ontolearn.owlapy.namespaces import Namespaces
from ontolearn.owlapy.model import OWLNamedIndividual, IRI

NS = Namespaces('ex', 'http://example.com/father#')

Expand Down
26 changes: 15 additions & 11 deletions docs/usage/03_ontologies.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ To load an ontology as well as to manage it, you will need an [OWLOntologyManage
To load an ontology, use the following Python code:

```python
from owlapy.model import IRI
from owlapy.owlready2 import OWLOntologyManager_Owlready2
from ontolearn.owlapy.model import IRI
from ontolearn.owlapy.owlready2 import OWLOntologyManager_Owlready2

manager = OWLOntologyManager_Owlready2()
onto = manager.load_ontology(IRI.create("file://KGs/father.owl"))
Expand Down Expand Up @@ -70,9 +70,10 @@ Let's suppose you want to add a new class in our example ontology `KGs/father.ow
to see a description of it). It can be done as follows:

<!--pytest-codeblocks:cont-->

```python
from owlapy.model import OWLClass
from owlapy.model import OWLDeclarationAxiom
from ontolearn.owlapy.model import OWLClass
from ontolearn.owlapy.model import OWLDeclarationAxiom

iri = IRI('http://example.com/father#', 'child')
child_class = OWLClass(iri)
Expand All @@ -98,16 +99,17 @@ you can use the class [OWLObjectProperty](owlapy.model.OWLObjectProperty) and fo
properties you can use the class [OWLDataProperty](owlapy.model.OWLDataProperty).

<!--pytest-codeblocks:cont-->

```python
from owlapy.model import OWLObjectProperty
from owlapy.model import OWLDataProperty
from ontolearn.owlapy.model import OWLObjectProperty
from ontolearn.owlapy.model import OWLDataProperty

# adding the object property 'hasParent'
hasParent_op = OWLObjectProperty(IRI('http://example.com/father#', 'hasParent'))
hasParent_op_declaration_axiom = OWLDeclarationAxiom(hasParent_op)
manager.add_axiom(onto, hasParent_op_declaration_axiom)

#adding the data property 'hasAge'
# adding the data property 'hasAge'
hasAge_dp = OWLDataProperty(IRI('http://example.com/father#', 'hasAge'))
hasAge_dp_declaration_axiom = OWLDeclarationAxiom(hasAge_dp)
manager.add_axiom(onto, hasAge_dp_declaration_axiom)
Expand All @@ -120,11 +122,12 @@ See the [API documentation](owlapy.model) for more OWL entities that you can add
To assign a class to a specific individual use the following code:

<!--pytest-codeblocks:cont-->

```python
from owlapy.model import OWLClassAssertionAxiom
from ontolearn.owlapy.model import OWLClassAssertionAxiom

individuals = list(onto.individuals_in_signature())
heinz = individuals[1] # get the 2nd individual in the list which is 'heinz'
heinz = individuals[1] # get the 2nd individual in the list which is 'heinz'

class_assertion_axiom = OWLClassAssertionAxiom(heinz, child_class)

Expand All @@ -142,9 +145,10 @@ Let's show one more example using a `OWLDataPropertyAssertionAxiom` to assign th
heinz.

<!--pytest-codeblocks:cont-->

```python
from owlapy.model import OWLLiteral
from owlapy.model import OWLDataPropertyAssertionAxiom
from ontolearn.owlapy.model import OWLLiteral
from ontolearn.owlapy.model import OWLDataPropertyAssertionAxiom

literal_17 = OWLLiteral(17)
dp_assertion_axiom = OWLDataPropertyAssertionAxiom(heinz, hasAge_dp, literal_17)
Expand Down
25 changes: 15 additions & 10 deletions docs/usage/04_reasoner.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ To load any reasoner, use the following code:
<!--pytest-codeblocks:cont-->

```python
from owlapy.owlready2 import OWLReasoner_Owlready2
from owlapy.owlready2.complex_ce_instances import OWLReasoner_Owlready2_ComplexCEInstances
from owlapy.fast_instance_checker import OWLReasoner_FastInstanceChecker
from ontolearn.owlapy.owlready2 import OWLReasoner_Owlready2
from ontolearn.owlapy.owlready2.complex_ce_instances import OWLReasoner_Owlready2_ComplexCEInstances
from ontolearn.owlapy.fast_instance_checker import OWLReasoner_FastInstanceChecker

structural_reasoner = OWLReasoner_Owlready2(onto)
complex_reasoner = OWLReasoner_Owlready2_ComplexCEInstances(onto)
Expand Down Expand Up @@ -60,11 +60,13 @@ but a reasoner can give you more than that. You can get the subclasses, supercla
equivalent classes of a class in the ontology:

<!--pytest-codeblocks:cont-->

```python
from owlapy.model import OWLClass
from owlapy.model import IRI
from ontolearn.owlapy.model import OWLClass
from ontolearn.owlapy.model import IRI

namespace = "http://example.com/father#"
male = OWLClass(IRI(namespace,"male"))
male = OWLClass(IRI(namespace, "male"))

male_super_classes = fast_instance_checker.super_classes(male)
male_sub_classes = fast_instance_checker.sub_classes(male)
Expand All @@ -87,12 +89,14 @@ upon the class, object property, or data property hierarchy.
You can get all the types of a certain individual using `types` method:

<!--pytest-codeblocks:cont-->

```python
from owlapy.owlready2 import OWLOntologyManager_Owlready2
from ontolearn.owlapy.owlready2 import OWLOntologyManager_Owlready2

manager = OWLOntologyManager_Owlready2()
onto = manager.load_ontology(IRI.create("KGs/father.owl"))
anna = list(onto.individuals_in_signature()).pop() # getting the 1st individual in the list of individuals which is 'anna'
anna = list(
onto.individuals_in_signature()).pop() # getting the 1st individual in the list of individuals which is 'anna'

anna_types = fast_instance_checker.types(anna)
```
Expand Down Expand Up @@ -146,10 +150,11 @@ specified individual.
In the same way as with classes, you can also get the sub object properties or equivalent object properties.

<!--pytest-codeblocks:cont-->

```python
from owlapy.model import OWLObjectProperty
from ontolearn.owlapy.model import OWLObjectProperty

hasChild = OWLObjectProperty(IRI(namespace,"hasChild"))
hasChild = OWLObjectProperty(IRI(namespace, "hasChild"))

equivalent_to_hasChild = fast_instance_checker.equivalent_object_properties(hasChild)
hasChild_sub_properties = fast_instance_checker.sub_object_properties(hasChild)
Expand Down
11 changes: 6 additions & 5 deletions docs/usage/05_concept_learners.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,14 @@ each individual (stored as `string`) from the set `positive_examples `
and `negative_examples` to `OWLNamedIndividual`:

<!--pytest-codeblocks:cont-->

```python
from ontolearn.learning_problem import PosNegLPStandard
from owlapy.model import IRI,OWLNamedIndividual
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)
from ontolearn.owlapy.model import IRI, OWLNamedIndividual

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)
```

To construct an [OWLNamedIndividual](owlapy.model.OWLNamedIndividual) object an [IRI](owlapy.model.IRI) is required as an input.
Expand Down
15 changes: 7 additions & 8 deletions docs/usage/09_model_adapter.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ from ontolearn.concept_learner import CELOE
from ontolearn.heuristics import CELOEHeuristic
from ontolearn.metrics import Accuracy
from ontolearn.model_adapter import ModelAdapter
from owlapy.model import OWLNamedIndividual, IRI
from owlapy.namespaces import Namespaces
from owlapy.owlready2 import OWLOntologyManager_Owlready2
from owlapy.owlready2.complex_ce_instances import OWLReasoner_Owlready2_ComplexCEInstances
from owlapy.render import DLSyntaxObjectRenderer

from ontolearn.owlapy.model import OWLNamedIndividual, IRI
from ontolearn.owlapy.namespaces import Namespaces
from ontolearn.owlapy.owlready2 import OWLOntologyManager_Owlready2
from ontolearn.owlapy.owlready2.complex_ce_instances import OWLReasoner_Owlready2_ComplexCEInstances
from ontolearn.owlapy.render import DLSyntaxObjectRenderer

manager = OWLOntologyManager_Owlready2()
onto = manager.load_ontology(IRI.create("KGs/father.owl"))
Expand All @@ -32,10 +31,10 @@ negative_examples = {OWLNamedIndividual(IRI.create(NS, 'heinz')),

# Only the class of the learning algorithm is specified
model = ModelAdapter(learner_type=CELOE,
reasoner=complex_ce_reasoner, # (*)
reasoner=complex_ce_reasoner, # (*)
path="KGs/father.owl",
quality_type=Accuracy,
heuristic_type=CELOEHeuristic, # (*)
heuristic_type=CELOEHeuristic, # (*)
expansionPenaltyFactor=0.05,
startNodeBonus=1.0,
nodeRefinementPenalty=0.01,
Expand Down
8 changes: 4 additions & 4 deletions examples/concept_learning_drill_train.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@
from ontolearn.concept_learner import Drill
from ontolearn.metrics import F1
from ontolearn.heuristics import Reward
from owlapy.model import OWLOntology, OWLReasoner
from ontolearn.owlapy.model import OWLOntology, OWLReasoner
from ontolearn.utils import setup_logging

setup_logging()


def ClosedWorld_ReasonerFactory(onto: OWLOntology) -> OWLReasoner:
from owlapy.owlready2 import OWLOntology_Owlready2
from owlapy.owlready2.complex_ce_instances import OWLReasoner_Owlready2_ComplexCEInstances
from owlapy.fast_instance_checker import OWLReasoner_FastInstanceChecker
from ontolearn.owlapy.owlready2 import OWLOntology_Owlready2
from ontolearn.owlapy.owlready2.complex_ce_instances import OWLReasoner_Owlready2_ComplexCEInstances
from ontolearn.owlapy.fast_instance_checker import OWLReasoner_FastInstanceChecker
assert isinstance(onto, OWLOntology_Owlready2)
base_reasoner = OWLReasoner_Owlready2_ComplexCEInstances(ontology=onto)
reasoner = OWLReasoner_FastInstanceChecker(ontology=onto,
Expand Down
2 changes: 1 addition & 1 deletion examples/concept_learning_with_celoe_heuristic.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from ontolearn.heuristics import CELOEHeuristic
from ontolearn.learning_problem import PosNegLPStandard
from ontolearn.metrics import Accuracy
from owlapy.model import OWLClass, OWLNamedIndividual, IRI
from ontolearn.owlapy.model import OWLClass, OWLNamedIndividual, IRI
from ontolearn.refinement_operators import ModifiedCELOERefinement
from ontolearn.utils import setup_logging

Expand Down
6 changes: 3 additions & 3 deletions examples/concept_learning_with_celoe_heuristic_ma.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
from ontolearn.concept_learner import CELOE
from ontolearn.knowledge_base import KnowledgeBase
from ontolearn.model_adapter import ModelAdapter
from owlapy.model import OWLClass, OWLNamedIndividual, IRI
from ontolearn.owlapy.model import OWLClass, OWLNamedIndividual, IRI
from ontolearn.utils import setup_logging
from owlapy.owlready2 import BaseReasoner_Owlready2, OWLOntology_Owlready2
from owlapy.owlready2.complex_ce_instances import OWLReasoner_Owlready2_ComplexCEInstances
from ontolearn.owlapy.owlready2 import BaseReasoner_Owlready2, OWLOntology_Owlready2
from ontolearn.owlapy.owlready2.complex_ce_instances import OWLReasoner_Owlready2_ComplexCEInstances
from typing import cast
setup_logging()

Expand Down
2 changes: 1 addition & 1 deletion examples/concept_learning_with_evolearner.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from ontolearn.knowledge_base import KnowledgeBase
from ontolearn.concept_learner import EvoLearner
from ontolearn.learning_problem import PosNegLPStandard
from owlapy.model import OWLClass, OWLNamedIndividual, IRI
from ontolearn.owlapy.model import OWLClass, OWLNamedIndividual, IRI
from ontolearn.utils import setup_logging

setup_logging()
Expand Down
2 changes: 1 addition & 1 deletion examples/concept_learning_with_ocel.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from ontolearn.concept_learner import OCEL
from ontolearn.learning_problem import PosNegLPStandard
from ontolearn.utils import setup_logging
from owlapy.model import OWLClass, IRI, OWLNamedIndividual
from ontolearn.owlapy.model import OWLClass, IRI, OWLNamedIndividual

setup_logging()

Expand Down
15 changes: 8 additions & 7 deletions examples/example_knowledge_base.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from ontolearn.concept_generator import ConceptGenerator
from ontolearn.knowledge_base import KnowledgeBase
import os

Expand All @@ -8,7 +9,7 @@

kb_path = '../KGs/Family/family-benchmark_rich_background.owl'
kb = KnowledgeBase(path=kb_path)

generator = ConceptGenerator()

# All concepts.
for i in kb.get_concepts():
Expand Down Expand Up @@ -42,23 +43,23 @@
print('*' * 100)


for concept in kb.most_general_existential_restrictions(domain=kb.thing):
for concept in kb.most_general_existential_restrictions(domain=generator.thing):
print(concept)

print('*' * 100)
for concept in kb.most_general_universal_restrictions(domain=kb.thing):
for concept in kb.most_general_universal_restrictions(domain=generator.thing):
print(concept)

print('*' * 100)
for concept in kb.most_general_existential_restrictions(domain=kb.nothing):
for concept in kb.most_general_existential_restrictions(domain=generator.nothing):
print(concept)

print('*' * 100)
for concept in kb.most_general_universal_restrictions(domain=kb.nothing):
for concept in kb.most_general_universal_restrictions(domain=generator.nothing):
print(concept)

print('*' * 100)
for c in kb.get_concepts():
neg_c = kb.negation(c)
neg_neg_c = kb.negation(neg_c)
neg_c = generator.negation(c)
neg_neg_c = generator.negation(neg_c)
assert neg_neg_c == c
10 changes: 4 additions & 6 deletions examples/example_reasoner.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
from owlapy.model import OWLSubClassOfAxiom, OWLEquivalentClassesAxiom, \
OWLEquivalentObjectPropertiesAxiom, OWLObjectPropertyDomainAxiom, OWLDataProperty
from owlapy.owlready2._base import OWLReasoner_Owlready2, BaseReasoner_Owlready2
from owlapy.fast_instance_checker import OWLReasoner_FastInstanceChecker
from ontolearn.owlapy.model import OWLSubClassOfAxiom, OWLEquivalentObjectPropertiesAxiom, OWLObjectPropertyDomainAxiom, OWLDataProperty
from ontolearn.owlapy.owlready2 import OWLReasoner_Owlready2, BaseReasoner_Owlready2
from ontolearn.knowledge_base import KnowledgeBase
from owlapy.model import OWLObjectProperty, IRI, OWLObjectSomeValuesFrom, \
from ontolearn.owlapy.model import OWLObjectProperty, IRI, OWLObjectSomeValuesFrom, \
OWLObjectIntersectionOf, OWLClass, OWLNamedIndividual
from owlapy.owlready2.complex_ce_instances import OWLReasoner_Owlready2_ComplexCEInstances
from ontolearn.owlapy.owlready2.complex_ce_instances import OWLReasoner_Owlready2_ComplexCEInstances

data_file = '../KGs/test_ontology.owl'
NS = 'http://www.semanticweb.org/stefan/ontologies/2023/1/untitled-ontology-11#'
Expand Down
8 changes: 4 additions & 4 deletions examples/experiments_standard.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from ontolearn.metrics import F1
from ontolearn.refinement_operators import LengthBasedRefinement
from ontolearn.utils import setup_logging
from owlapy.model import OWLOntology, OWLReasoner
from ontolearn.owlapy.model import OWLOntology, OWLReasoner

setup_logging()
full_computation_time = time.time()
Expand All @@ -44,9 +44,9 @@ def sanity_checking_args(args):


def ClosedWorld_ReasonerFactory(onto: OWLOntology) -> OWLReasoner:
from owlapy.owlready2 import OWLOntology_Owlready2
from owlapy.owlready2.complex_ce_instances import OWLReasoner_Owlready2_ComplexCEInstances
from owlapy.fast_instance_checker import OWLReasoner_FastInstanceChecker
from ontolearn.owlapy.owlready2 import OWLOntology_Owlready2
from ontolearn.owlapy.owlready2.complex_ce_instances import OWLReasoner_Owlready2_ComplexCEInstances
from ontolearn.owlapy.fast_instance_checker import OWLReasoner_FastInstanceChecker
assert isinstance(onto, OWLOntology_Owlready2)
base_reasoner = OWLReasoner_Owlready2_ComplexCEInstances(ontology=onto)
reasoner = OWLReasoner_FastInstanceChecker(ontology=onto,
Expand Down
Loading

0 comments on commit 372f6d4

Please sign in to comment.