Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Corrected get_by_label to use only labels in the ontology #643

Merged
merged 17 commits into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions ontopy/ontology.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
)

if TYPE_CHECKING:
from typing import List, Sequence

Check warning on line 52 in ontopy/ontology.py

View check run for this annotation

Codecov / codecov/patch

ontopy/ontology.py#L52

Added line #L52 was not covered by tests


# Default annotations to look up
Expand Down Expand Up @@ -100,8 +100,9 @@
this Ontology subclass. Defaults to `ontopy.Ontology`.
label_annotations: Sequence of label IRIs used for accessing
entities in the ontology given that they are in the ontology.
Label IRIs not in the ontology will simply be ignored.
Detault to DEFAULT_LABEL_ANNOTATIONS module variable.
Label IRIs not in the ontology will need to be added to
ontologies in order to be accessible.
Default is None.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

None (default), is equivalent to set label_annotations to the DEFAULT_LABEL_ANNOTATIONS module variable.

francescalb marked this conversation as resolved.
Show resolved Hide resolved
"""
base_iri = base_iri.as_uri() if isinstance(base_iri, Path) else base_iri

Expand Down Expand Up @@ -526,24 +527,24 @@
stacklevel=2,
)
if hasattr(iri, "iri"):
iri = iri.iri

Check warning on line 530 in ontopy/ontology.py

View check run for this annotation

Codecov / codecov/patch

ontopy/ontology.py#L530

Added line #L530 was not covered by tests
if iri not in self.label_annotations:
self.label_annotations.append(iri)

Check warning on line 532 in ontopy/ontology.py

View check run for this annotation

Codecov / codecov/patch

ontopy/ontology.py#L532

Added line #L532 was not covered by tests

def remove_label_annotation(self, iri):
"""Removes label annotation used by get_by_label()."""
warnings.warn(
"Ontology.add_label_annotations() is deprecated. "
"Ontology.remove_label_annotations() is deprecated. "
"Direct modify the `label_annotations` attribute instead.",
DeprecationWarning,
stacklevel=2,
)
if hasattr(iri, "iri"):
iri = iri.iri

Check warning on line 543 in ontopy/ontology.py

View check run for this annotation

Codecov / codecov/patch

ontopy/ontology.py#L543

Added line #L543 was not covered by tests
try:
self.label_annotations.remove(iri)
except ValueError:
pass

Check warning on line 547 in ontopy/ontology.py

View check run for this annotation

Codecov / codecov/patch

ontopy/ontology.py#L546-L547

Added lines #L546 - L547 were not covered by tests

def set_common_prefix(
self,
Expand Down
23 changes: 23 additions & 0 deletions tests/ontopy_tests/test_deprecation_warnings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from typing import TYPE_CHECKING
import pytest
from owlready2.entity import ThingClass
from owlready2.prop import ObjectPropertyClass, DataPropertyClass
from owlready2 import AnnotationPropertyClass

if TYPE_CHECKING:
from pathlib import Path


@pytest.mark.filterwarnings(
"ignore:Ontology.add_label_annotations() is deprecated. Direct modify the `label_annotations` attribute instead."
)
def test_deprecation_warnings() -> None:
"""Test functionalities will be removed and currently have depracation warnings"""
from ontopy import get_ontology
from ontopy.ontology import DEFAULT_LABEL_ANNOTATIONS

testonto = get_ontology("http://domain_ontology/new_ontology")

testonto.add_label_annotation(DEFAULT_LABEL_ANNOTATIONS[0])

testonto.remove_label_annotation(DEFAULT_LABEL_ANNOTATIONS[0])
19 changes: 19 additions & 0 deletions tests/ontopy_tests/test_new_entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ def test_new_entity(testonto: "Ontology") -> None:
testonto.new_entity(
"AnotherClass", testonto.TestClass, entitytype=ThingClass
)

testonto.new_entity(
"YetAnotherClass",
testonto.TestClass,
entitytype=ThingClass,
preflabel="YetAnotherClass",
)

testonto.new_entity(
"hasSubObjectProperty",
testonto.hasObjectProperty,
Expand Down Expand Up @@ -100,3 +108,14 @@ def test_new_entity(testonto: "Ontology") -> None:
testonto.new_annotation_property(
"hasSubAnnotationProperty3", testonto.hasAnnotationProperty
)

from ontopy import get_ontology
from ontopy.ontology import DEFAULT_LABEL_ANNOTATIONS

testonto2 = get_ontology("http://domain_ontology/new_ontology")
testonto2.new_entity(
"NewClass",
testonto.TestClass,
entitytype=ThingClass,
preflabel="NewClass",
)
18 changes: 18 additions & 0 deletions tests/ontopy_tests/test_ontology_helper_functions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from typing import TYPE_CHECKING
import pytest
from owlready2.entity import ThingClass
from owlready2.prop import ObjectPropertyClass, DataPropertyClass
from owlready2 import AnnotationPropertyClass

if TYPE_CHECKING:
from pathlib import Path


def test_ontology_to_storids(testonto: "Ontology") -> None:
"""Test adding helper functions in ontopy.ontology"""
from ontopy.ontology import DEFAULT_LABEL_ANNOTATIONS

label_annotations = DEFAULT_LABEL_ANNOTATIONS
assert len(testonto._to_storids(label_annotations)) == 3
assert testonto._to_storids(None) == []
assert testonto._to_storids([testonto.TestClass])
17 changes: 17 additions & 0 deletions tests/test_get_by_label.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,23 @@ def test_get_by_label_onto(repo_dir) -> None:
testonto.Class,
}

# Test with label annotations given directly when creating the ontology
testonto2 = get_ontology(
"http://domain_ontology/new_ontology",
label_annotations=[
"http://www.w3.org/2004/02/skos/core#prefLabel",
"http://www.w3.org/2004/02/skos/core#altLabel",
],
)

testonto2.new_annotation_property(
"prefLabel",
parent=[owlready2.AnnotationProperty],
)
testonto2.new_entity("Class", owlready2.Thing, preflabel="Klasse")

assert testonto2.get_by_label("Klasse") == testonto2.Class


def test_get_by_label_emmo(emmo: "Ontology") -> None:
# Loading emmo-inferred where everything is sqashed into one ontology
Expand Down
Loading