diff --git a/ontopy/excelparser.py b/ontopy/excelparser.py index 6cc8e99f3..5e7288844 100755 --- a/ontopy/excelparser.py +++ b/ontopy/excelparser.py @@ -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. @@ -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: @@ -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}". ' @@ -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 diff --git a/tests/test_excelparser/fromexcelonto.ttl b/tests/test_excelparser/fromexcelonto.ttl index 304994288..5ded0b43f 100644 --- a/tests/test_excelparser/fromexcelonto.ttl +++ b/tests/test_excelparser/fromexcelonto.ttl @@ -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 . @@ -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 ; diff --git a/tests/test_excelparser/onto.xlsx b/tests/test_excelparser/onto.xlsx index 10bbfc527..3ff88b994 100755 Binary files a/tests/test_excelparser/onto.xlsx and b/tests/test_excelparser/onto.xlsx differ diff --git a/tests/test_excelparser/test_excelparser.py b/tests/test_excelparser/test_excelparser.py index 00e526a2a..34c069d4b 100644 --- a/tests/test_excelparser/test_excelparser.py +++ b/tests/test_excelparser/test_excelparser.py @@ -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"} @@ -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