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

New concepts allowed even if name alrady exists in imported ontologies #504

Merged
Show file tree
Hide file tree
Changes from all commits
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
37 changes: 14 additions & 23 deletions ontopy/excelparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,12 @@ def create_ontology_from_excel( # pylint: disable=too-many-arguments
following keys:

- "already_defined": These are concepts that are already in the
ontology, either because they were already added in a
ontology, because they were already added in a
previous line of the excelfile/pandas dataframe, or because
it is already defined in the imported ontologies.
it is already defined in an imported ontology with the same
base_iri as the newly created ontology.
- "in_imported_ontologies": Concepts that are defined in the
excel, but already exist in the imported ontologies.
This is a subset of the 'already_defined'.
- "wrongly_defined": Concepts that are given an invalid
prefLabel (e.g. with a space in the name).
- "missing_parents": Concepts that are missing parents.
Expand Down Expand Up @@ -170,10 +170,6 @@ def create_ontology_from_pandas( # pylint:disable=too-many-locals,too-many-bran
onto, catalog = get_metadata_from_dataframe(
metadata, base_iri, imports=imports
)
# Get a set of imported concepts
imported_concepts = {
concept.prefLabel.first() for concept in onto.get_entities()
}

# Set given or default base_iri if base_iri_from_metadata is False.
if not base_iri_from_metadata:
Expand Down Expand Up @@ -206,20 +202,18 @@ def create_ontology_from_pandas( # pylint:disable=too-many-locals,too-many-bran
name = row["prefLabel"]
try:
onto.get_by_label(name)
if not force:
raise ExcelError(
f'Concept "{name}" already in ontology'
if onto.world[onto.base_iri + name]:
if not force:
raise ExcelError(
f'Concept "{name}" already in ontology'
)
warnings.warn(
f'Ignoring concept "{name}" since it is already in '
"the ontology."
)
warnings.warn(
f'Ignoring concept "{name}" since it is already in '
"the ontology."
)
concepts_with_errors["already_defined"].append(name)
# What to do if we want to add info to this concept?
# Should that be not allowed?
# If it should be allowed the index has to be added to
# added_rows
continue
concepts_with_errors["already_defined"].append(name)
continue
concepts_with_errors["in_imported_ontologies"].append(name)
except (ValueError, TypeError) as err:
warnings.warn(
f'Ignoring concept "{name}". '
Expand Down Expand Up @@ -389,9 +383,6 @@ def create_ontology_from_pandas( # pylint:disable=too-many-locals,too-many-bran
concepts_with_errors = {
key: set(value) for key, value in concepts_with_errors.items()
}
concepts_with_errors["in_imported_ontologies"] = concepts_with_errors[
"already_defined"
].intersection(imported_concepts)

return onto, catalog, concepts_with_errors

Expand Down
6 changes: 5 additions & 1 deletion tests/test_excelparser/fromexcelonto.ttl
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
rdfs:comment "Used for our own special purpose"@en ;
rdfs:subClassOf [ a owl:Restriction ;
owl:onProperty emmo:EMMO_17e27c22_37e1_468c_9dd7_95e137f73e7f ;
owl:someValuesFrom emmo:EMMO_eb77076b_a104_42ac_a065_798b2d2809ad ],
owl:someValuesFrom :EMMO_8b758694-7dd3-547a-8589-a835c15a0fb2 ],
emmo:EMMO_3397f270_dfc1_4500_8f6f_4d0d85ac5f71 ;
core:prefLabel "SpecialMolecule"@en .

Expand All @@ -104,6 +104,10 @@
rdfs:subClassOf :EMMO_9fa9ca88-2891-538a-a8dd-ccb8a08b9890 ;
core:prefLabel "SpatialPattern"@en .

:EMMO_8b758694-7dd3-547a-8589-a835c15a0fb2 a owl:Class ;
rdfs:subClassOf emmo:EMMO_eb77076b_a104_42ac_a065_798b2d2809ad ;
core:prefLabel "Atom"@en .

:EMMO_1b2bfe71-5da9-5c46-b137-be45c3e3f9c3 a owl:Class ;
emmo:EMMO_967080e5_2f42_4eb2_a3a9_c58143e835f9 "NEED elucidation"@en ;
rdfs:subClassOf emmo:EMMO_649bf97b_4397_4005_90d9_219755d92e34 ;
Expand Down
Binary file modified tests/test_excelparser/onto.xlsx
Binary file not shown.
6 changes: 4 additions & 2 deletions tests/test_excelparser/test_excelparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ def test_excelparser(repo_dir: "Path") -> None:
onto = get_ontology(str(ontopath)).load()
xlspath = repo_dir / "tests" / "test_excelparser" / "onto.xlsx"
ontology, catalog, errors = create_ontology_from_excel(xlspath, force=True)

assert onto == ontology

assert errors["already_defined"] == {"Atom", "Pattern"}
assert errors["already_defined"] == {"Pattern"}
assert errors["in_imported_ontologies"] == {"Atom"}
assert errors["wrongly_defined"] == {"Temporal Boundary"}
assert errors["missing_parents"] == {"SpatioTemporalBoundary"}
Expand All @@ -27,7 +28,8 @@ def test_excelparser(repo_dir: "Path") -> None:
"SubgrainBoundary",
}
assert errors["nonadded_concepts"] == {
"Atom",
"Pattern",
"Temporal Boundary",
}

assert len(ontology.get_by_label_all("Atom")) == 2