From a11f6818b7c84d46e3ae5ece56ff22d81108b8f0 Mon Sep 17 00:00:00 2001 From: Caglar Demir Date: Thu, 14 Nov 2024 14:09:14 +0100 Subject: [PATCH 1/3] A CSV file is mapped to an RDF KGs containing only literals --- README.md | 2 +- owlapy/owl_literal.py | 2 +- owlapy/util_owl_static_funcs.py | 26 +++++++++++++------------- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 2cfcbc91..36f8bfc7 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ pip3 install owlapy ```shell # To download RDF knowledge graphs wget https://files.dice-research.org/projects/Ontolearn/KGs.zip -O ./KGs.zip && unzip KGs.zip -pytest -p no:warnings -x # Running 142 tests ~ 30 secs +pytest -p no:warnings -x # Running 147 tests ~ 35 secs ``` ## Examples diff --git a/owlapy/owl_literal.py b/owlapy/owl_literal.py index d6ffb5bb..3154e487 100644 --- a/owlapy/owl_literal.py +++ b/owlapy/owl_literal.py @@ -257,7 +257,7 @@ def parse_integer(self) -> int: Returns: An integer value that is represented by this literal. """ - raise ValueError + return int(self._v) def is_string(self) -> bool: """Whether this literal is typed as string.""" diff --git a/owlapy/util_owl_static_funcs.py b/owlapy/util_owl_static_funcs.py index 1e8027b7..1513c17e 100644 --- a/owlapy/util_owl_static_funcs.py +++ b/owlapy/util_owl_static_funcs.py @@ -62,7 +62,7 @@ def save_owl_class_expressions(expressions: OWLClassExpression | List[OWLClassEx ontology.add_axiom(equivalent_classes_axiom) ontology.save(path=path, inplace=False, rdf_format=rdf_format) -def csv_to_rdf_kg(path_csv:str=None,path_kg:str=None,namespace:str=None,rdf_format:str=None): +def csv_to_rdf_kg(path_csv:str=None,path_kg:str=None,namespace:str=None): """ Transfroms a CSV file to an RDF Knowledge Graph in RDF/XML format. @@ -70,7 +70,6 @@ def csv_to_rdf_kg(path_csv:str=None,path_kg:str=None,namespace:str=None,rdf_form path_csv (str): X path_kg (str): X namespace (str): X - rdf_format(str):X Raises: AssertionError: @@ -94,10 +93,6 @@ def csv_to_rdf_kg(path_csv:str=None,path_kg:str=None,namespace:str=None,rdf_form assert path_kg is not None, f"path_kg cannot be None.Currently {path_kg}" assert namespace is not None, "namespace cannot be None" assert namespace[:7]=="http://", "First characters of namespace must be 'http://'" - if rdf_format is None: - rdf_format="rdfxml" - else: - assert rdf_format in ["ntriples", "turtle"] # Initialize an Ontology Manager. manager = SyncOntologyManager() @@ -106,22 +101,27 @@ def csv_to_rdf_kg(path_csv:str=None,path_kg:str=None,namespace:str=None,rdf_form # Read the CSV file df = pd.read_csv(path_csv) + # () Iterate over rows for index, row in tqdm(df.iterrows()): individual=OWLNamedIndividual(f"{namespace}#{str(index)}".replace(" ","_")) + # column_name is considered as a predicate + # value is considered as a data property for column_name, value in row.to_dict().items(): - if isinstance(value, float): - # Create an IRI for the predicate - str_property_iri=f"{namespace}#{column_name}".replace(" ","_") - str_property_iri=str_property_iri.replace("(","/") - str_property_iri = str_property_iri.replace(")", "") + # Create an IRI for the predicate + str_property_iri = f"{namespace}#{column_name}".replace(" ", "_") + str_property_iri = str_property_iri.replace("(", "/") + str_property_iri = str_property_iri.replace(")", "") + if isinstance(value, float) or isinstance(value, int) or isinstance(value, str): axiom = OWLDataPropertyAssertionAxiom(subject=individual, property_=OWLDataProperty(iri=str_property_iri), object_=OWLLiteral(value=value)) ontology.add_axiom(axiom) - else: - raise NotImplementedError(f"How to represent value={value} has not been decided") + raise NotImplementedError(f"How to represent\n" + f"predicate=**{str_property_iri}**\n" + f"value=**{value}**\n" + f"has not been decided") ontology.save(path=path_kg) From 8e2f1c809424e4563bfa92b80eef0042c9ca3a16 Mon Sep 17 00:00:00 2001 From: Caglar Demir Date: Thu, 14 Nov 2024 14:15:40 +0100 Subject: [PATCH 2/3] Refactoring csv_to_rdf_kg --- owlapy/util_owl_static_funcs.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/owlapy/util_owl_static_funcs.py b/owlapy/util_owl_static_funcs.py index 1513c17e..542ac243 100644 --- a/owlapy/util_owl_static_funcs.py +++ b/owlapy/util_owl_static_funcs.py @@ -103,8 +103,9 @@ def csv_to_rdf_kg(path_csv:str=None,path_kg:str=None,namespace:str=None): df = pd.read_csv(path_csv) # () Iterate over rows - for index, row in tqdm(df.iterrows()): + for index, row in (tqdm_bar := tqdm(df.iterrows()) ): individual=OWLNamedIndividual(f"{namespace}#{str(index)}".replace(" ","_")) + tqdm_bar.set_description_str(f"Creating RDF Graph from Row:{index}") # column_name is considered as a predicate # value is considered as a data property for column_name, value in row.to_dict().items(): @@ -123,5 +124,4 @@ def csv_to_rdf_kg(path_csv:str=None,path_kg:str=None,namespace:str=None): f"predicate=**{str_property_iri}**\n" f"value=**{value}**\n" f"has not been decided") - ontology.save(path=path_kg) From f582927915025f07ff90370ba39b17f21e1e1a5e Mon Sep 17 00:00:00 2001 From: Caglar Demir Date: Thu, 14 Nov 2024 14:23:49 +0100 Subject: [PATCH 3/3] Refactoring csv_to_rdf_kg and adding example --- README.md | 23 +++++++++++++++++++++++ tests/test_owl_static_funcs.py | 3 --- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 36f8bfc7..6dd942c2 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,29 @@ pytest -p no:warnings -x # Running 147 tests ~ 35 secs ## Examples +### Sklearn to OWL Ontology + +
Click me! + +```python +from owlapy.owl_ontology_manager import SyncOntologyManager +from owlapy.util_owl_static_funcs import csv_to_rdf_kg +import pandas as pd +from sklearn.datasets import load_iris +data = load_iris() +df = pd.DataFrame(data.data, columns=data.feature_names) +df.to_csv("iris_dataset.csv", index=False) +path_kg = "iris_kg.owl" +# Construct an RDF Knowledge Graph from a CSV file +csv_to_rdf_kg(path_csv="iris_dataset.csv", path_kg=path_kg, namespace="http://owlapy.com/iris") +onto = SyncOntologyManager().load_ontology(path_kg) +assert len(onto.get_abox_axioms()) == 750 + +``` + +
+ + ### Exploring OWL Ontology
Click me! diff --git a/tests/test_owl_static_funcs.py b/tests/test_owl_static_funcs.py index f1bd7771..668c99bb 100644 --- a/tests/test_owl_static_funcs.py +++ b/tests/test_owl_static_funcs.py @@ -10,8 +10,6 @@ import rdflib from owlapy.owl_ontology_manager import SyncOntologyManager -from sklearn.datasets import load_iris -import pandas as pd class TestRunningExamples: def test_readme(self): @@ -35,7 +33,6 @@ def test_csv_to_kg(self): df = pd.DataFrame(data.data, columns=data.feature_names) df['target'] = data.target df.to_csv("iris_dataset.csv", index=False) - assert len(df) == 150 path_kg = "iris_kg.owl" csv_to_rdf_kg(path_csv="iris_dataset.csv", path_kg=path_kg, namespace="http://example.com/society")