cognite-neat 0.85.6__py3-none-any.whl → 0.85.8__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of cognite-neat might be problematic. Click here for more details.
- cognite/neat/_version.py +1 -1
- cognite/neat/app/api/routers/data_exploration.py +2 -2
- cognite/neat/graph/extractors/_classic_cdf/_assets.py +136 -27
- cognite/neat/graph/extractors/_classic_cdf/_events.py +56 -26
- cognite/neat/graph/extractors/_classic_cdf/_files.py +73 -29
- cognite/neat/graph/extractors/_classic_cdf/_labels.py +20 -11
- cognite/neat/graph/extractors/_classic_cdf/_relationships.py +35 -20
- cognite/neat/graph/extractors/_classic_cdf/_sequences.py +60 -22
- cognite/neat/graph/extractors/_classic_cdf/_timeseries.py +78 -30
- cognite/neat/graph/extractors/_mock_graph_generator.py +6 -2
- cognite/neat/graph/loaders/_base.py +3 -3
- cognite/neat/graph/loaders/_rdf2dms.py +13 -11
- cognite/neat/graph/queries/_base.py +16 -6
- cognite/neat/graph/queries/_shared.py +2 -2
- cognite/neat/legacy/graph/extractors/_mock_graph_generator.py +4 -2
- cognite/neat/legacy/graph/loaders/_asset_loader.py +5 -5
- cognite/neat/legacy/graph/loaders/core/rdf_to_assets.py +4 -4
- cognite/neat/legacy/graph/loaders/core/rdf_to_relationships.py +2 -2
- cognite/neat/legacy/graph/transformations/query_generator/sparql.py +2 -2
- cognite/neat/legacy/graph/transformations/transformer.py +2 -2
- cognite/neat/legacy/rules/exporters/_rules2ontology.py +6 -6
- cognite/neat/legacy/rules/importers/_graph2rules.py +4 -4
- cognite/neat/legacy/rules/importers/_owl2rules/_owl2classes.py +3 -3
- cognite/neat/legacy/rules/importers/_owl2rules/_owl2properties.py +5 -5
- cognite/neat/rules/exporters/_rules2ontology.py +6 -6
- cognite/neat/rules/importers/_inference2rules.py +35 -25
- cognite/neat/rules/importers/_owl2rules/_owl2classes.py +3 -3
- cognite/neat/rules/importers/_owl2rules/_owl2properties.py +5 -5
- cognite/neat/utils/__init__.py +2 -2
- cognite/neat/utils/utils.py +6 -6
- {cognite_neat-0.85.6.dist-info → cognite_neat-0.85.8.dist-info}/METADATA +1 -1
- {cognite_neat-0.85.6.dist-info → cognite_neat-0.85.8.dist-info}/RECORD +35 -35
- {cognite_neat-0.85.6.dist-info → cognite_neat-0.85.8.dist-info}/LICENSE +0 -0
- {cognite_neat-0.85.6.dist-info → cognite_neat-0.85.8.dist-info}/WHEEL +0 -0
- {cognite_neat-0.85.6.dist-info → cognite_neat-0.85.8.dist-info}/entry_points.txt +0 -0
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import warnings
|
|
2
|
-
from typing import cast
|
|
2
|
+
from typing import Literal, cast, overload
|
|
3
3
|
|
|
4
4
|
from rdflib import RDF, Graph, URIRef
|
|
5
5
|
from rdflib.query import ResultRow
|
|
6
6
|
|
|
7
7
|
from cognite.neat.rules.models.entities import ClassEntity
|
|
8
8
|
from cognite.neat.rules.models.information import InformationRules
|
|
9
|
-
from cognite.neat.utils.utils import
|
|
9
|
+
from cognite.neat.utils.utils import remove_namespace_from_uri
|
|
10
10
|
|
|
11
11
|
from ._construct import build_construct_query
|
|
12
12
|
|
|
@@ -66,7 +66,7 @@ class Queries:
|
|
|
66
66
|
result = self.graph.query(query)
|
|
67
67
|
|
|
68
68
|
# We cannot include the RDF.type in case there is a neat:type property
|
|
69
|
-
return [
|
|
69
|
+
return [remove_namespace_from_uri(*triple) for triple in result if triple[1] != RDF.type] # type: ignore[misc, index]
|
|
70
70
|
else:
|
|
71
71
|
warnings.warn("No rules found for the graph store, returning empty list.", stacklevel=2)
|
|
72
72
|
return []
|
|
@@ -93,7 +93,7 @@ class Queries:
|
|
|
93
93
|
result = self.graph.query(query)
|
|
94
94
|
|
|
95
95
|
# We cannot include the RDF.type in case there is a neat:type property
|
|
96
|
-
return [
|
|
96
|
+
return [remove_namespace_from_uri(*triple) for triple in result if triple[1] != RDF.type] # type: ignore[misc, index]
|
|
97
97
|
else:
|
|
98
98
|
warnings.warn("No rules found for the graph store, returning empty list.", stacklevel=2)
|
|
99
99
|
return []
|
|
@@ -110,14 +110,24 @@ class Queries:
|
|
|
110
110
|
query = f"SELECT ?subject ?predicate ?object WHERE {{ ?subject ?predicate ?object }} LIMIT {limit}"
|
|
111
111
|
return cast(list[ResultRow], list(self.graph.query(query)))
|
|
112
112
|
|
|
113
|
-
|
|
113
|
+
@overload
|
|
114
|
+
def list_types(self, remove_namespace: Literal[False] = False, limit: int = 25) -> list[ResultRow]: ...
|
|
115
|
+
|
|
116
|
+
@overload
|
|
117
|
+
def list_types(self, remove_namespace: Literal[True], limit: int = 25) -> list[str]: ...
|
|
118
|
+
|
|
119
|
+
def list_types(self, remove_namespace: bool = False, limit: int = 25) -> list[ResultRow] | list[str]:
|
|
114
120
|
"""List types in the graph store
|
|
115
121
|
|
|
116
122
|
Args:
|
|
117
123
|
limit: Max number of types to return, by default 25
|
|
124
|
+
remove_namespace: Whether to remove the namespace from the type, by default False
|
|
118
125
|
|
|
119
126
|
Returns:
|
|
120
127
|
List of types
|
|
121
128
|
"""
|
|
122
129
|
query = f"SELECT DISTINCT ?type WHERE {{ ?subject a ?type }} LIMIT {limit}"
|
|
123
|
-
|
|
130
|
+
result = cast(list[ResultRow], list(self.graph.query(query)))
|
|
131
|
+
if remove_namespace:
|
|
132
|
+
return [remove_namespace_from_uri(res[0]) for res in result]
|
|
133
|
+
return result
|
|
@@ -11,7 +11,7 @@ from cognite.neat.rules.models._rdfpath import (
|
|
|
11
11
|
Hop,
|
|
12
12
|
Step,
|
|
13
13
|
)
|
|
14
|
-
from cognite.neat.utils.utils import
|
|
14
|
+
from cognite.neat.utils.utils import remove_namespace_from_uri, uri_to_short_form
|
|
15
15
|
|
|
16
16
|
if sys.version_info >= (3, 11):
|
|
17
17
|
from typing import Self
|
|
@@ -146,7 +146,7 @@ def triples2dictionary(triples: Iterable[tuple[URIRef, URIRef, str | URIRef]]) -
|
|
|
146
146
|
value: str
|
|
147
147
|
uri: URIRef
|
|
148
148
|
|
|
149
|
-
id_, property_, value =
|
|
149
|
+
id_, property_, value = remove_namespace_from_uri(*triple) # type: ignore[misc]
|
|
150
150
|
uri = triple[0]
|
|
151
151
|
|
|
152
152
|
if uri not in dictionary:
|
|
@@ -20,7 +20,7 @@ from cognite.neat.legacy.rules.analysis import (
|
|
|
20
20
|
from cognite.neat.legacy.rules.exporters._rules2rules import subset_rules
|
|
21
21
|
from cognite.neat.legacy.rules.models import Rules
|
|
22
22
|
from cognite.neat.legacy.rules.models.value_types import XSD_VALUE_TYPE_MAPPINGS
|
|
23
|
-
from cognite.neat.utils.utils import
|
|
23
|
+
from cognite.neat.utils.utils import remove_namespace_from_uri
|
|
24
24
|
|
|
25
25
|
from ._base import BaseExtractor
|
|
26
26
|
|
|
@@ -247,7 +247,9 @@ def _generate_mock_data_property_triples(
|
|
|
247
247
|
triples.append((id_, URIRef(namespace[property_]), Literal(numpy.float32(random.uniform(1, 1983)))))
|
|
248
248
|
# generate string
|
|
249
249
|
else:
|
|
250
|
-
triples.append(
|
|
250
|
+
triples.append(
|
|
251
|
+
(id_, URIRef(namespace[property_]), Literal(remove_namespace_from_uri(id_).replace("-", " ")))
|
|
252
|
+
)
|
|
251
253
|
return triples
|
|
252
254
|
|
|
253
255
|
|
|
@@ -15,7 +15,7 @@ from rdflib.query import ResultRow
|
|
|
15
15
|
from cognite.neat.legacy.graph.stores import NeatGraphStoreBase
|
|
16
16
|
from cognite.neat.legacy.rules.models import Rules
|
|
17
17
|
from cognite.neat.legacy.rules.models.rules import Property
|
|
18
|
-
from cognite.neat.utils import
|
|
18
|
+
from cognite.neat.utils import remove_namespace_from_uri
|
|
19
19
|
from cognite.neat.utils.utils import epoch_now_ms
|
|
20
20
|
|
|
21
21
|
from ._base import CogniteLoader
|
|
@@ -166,7 +166,7 @@ class AssetLoader(CogniteLoader[AssetResource]):
|
|
|
166
166
|
logging.debug(f"Class <{class_name}> has {len(result)} instances")
|
|
167
167
|
|
|
168
168
|
for instance_uri, properties_values in groupby(result, lambda x: x[0]):
|
|
169
|
-
instance_id =
|
|
169
|
+
instance_id = remove_namespace_from_uri(instance_uri)
|
|
170
170
|
values_by_property = self._prepare_instance_data(instance_id, properties_values)
|
|
171
171
|
try:
|
|
172
172
|
asset = self._load_asset(
|
|
@@ -195,7 +195,7 @@ class AssetLoader(CogniteLoader[AssetResource]):
|
|
|
195
195
|
relationships = self._load_relationships(
|
|
196
196
|
properties_by_class_name[class_name],
|
|
197
197
|
values_by_property,
|
|
198
|
-
|
|
198
|
+
remove_namespace_from_uri(instance_id),
|
|
199
199
|
class_name,
|
|
200
200
|
)
|
|
201
201
|
except Exception as e:
|
|
@@ -342,11 +342,11 @@ class AssetLoader(CogniteLoader[AssetResource]):
|
|
|
342
342
|
A dictionary with property type as key and a list of values as value.
|
|
343
343
|
"""
|
|
344
344
|
properties_value_tuples: list[tuple[str, str]] = [
|
|
345
|
-
|
|
345
|
+
remove_namespace_from_uri(prop, value) # type: ignore[misc]
|
|
346
346
|
for _, prop, value in properties_values
|
|
347
347
|
]
|
|
348
348
|
# We add an identifier which will be used as fallback for external_id
|
|
349
|
-
properties_value_tuples.append((self._identifier,
|
|
349
|
+
properties_value_tuples.append((self._identifier, remove_namespace_from_uri(instance_id)))
|
|
350
350
|
values_by_property: dict[str, str | list[str]] = {}
|
|
351
351
|
for prop, values in groupby(sorted(properties_value_tuples), lambda x: x[0]):
|
|
352
352
|
values_list: list[str] = [value for _, value in values] # type: ignore[misc, has-type]
|
|
@@ -18,7 +18,7 @@ from rdflib.term import URIRef
|
|
|
18
18
|
from cognite.neat.legacy.graph.loaders.core.models import AssetTemplate
|
|
19
19
|
from cognite.neat.legacy.graph.stores import NeatGraphStoreBase
|
|
20
20
|
from cognite.neat.legacy.rules.models.rules import Property, Rules
|
|
21
|
-
from cognite.neat.utils.utils import chunker, datetime_utc_now,
|
|
21
|
+
from cognite.neat.utils.utils import chunker, datetime_utc_now, remove_namespace_from_uri, retry_decorator
|
|
22
22
|
|
|
23
23
|
if sys.version_info >= (3, 11):
|
|
24
24
|
from datetime import UTC
|
|
@@ -320,13 +320,13 @@ def _list2dict(class_instance: list) -> dict[str, Any]:
|
|
|
320
320
|
|
|
321
321
|
class_instance_dict: dict[str, Any] = {}
|
|
322
322
|
for property_value_pair in class_instance:
|
|
323
|
-
property_ =
|
|
323
|
+
property_ = remove_namespace_from_uri(property_value_pair[0])
|
|
324
324
|
|
|
325
325
|
# Remove namespace from URIRef values, otherwise convert Literal to string
|
|
326
326
|
# ideally this should react upon property type provided in sheet
|
|
327
327
|
# however Assets only support string values
|
|
328
328
|
value = (
|
|
329
|
-
|
|
329
|
+
remove_namespace_from_uri(property_value_pair[1])
|
|
330
330
|
if isinstance(property_value_pair[1], URIRef)
|
|
331
331
|
else str(property_value_pair[1])
|
|
332
332
|
)
|
|
@@ -440,7 +440,7 @@ def rdf2assets(
|
|
|
440
440
|
progress_counter += 1
|
|
441
441
|
except Exception as ValidationError:
|
|
442
442
|
logging.error(
|
|
443
|
-
f"Skipping class <{class_}> instance <{
|
|
443
|
+
f"Skipping class <{class_}> instance <{remove_namespace_from_uri(str(instance_id))}>, "
|
|
444
444
|
f"reason:\n{ValidationError}\n"
|
|
445
445
|
)
|
|
446
446
|
if stop_on_exception:
|
|
@@ -14,7 +14,7 @@ from cognite.neat.legacy.graph.loaders.core.models import RelationshipDefinition
|
|
|
14
14
|
from cognite.neat.legacy.graph.loaders.core.rdf_to_assets import _categorize_cdf_assets
|
|
15
15
|
from cognite.neat.legacy.graph.stores import NeatGraphStoreBase
|
|
16
16
|
from cognite.neat.legacy.rules.models.rules import Rules
|
|
17
|
-
from cognite.neat.utils.utils import chunker, datetime_utc_now, epoch_now_ms,
|
|
17
|
+
from cognite.neat.utils.utils import chunker, datetime_utc_now, epoch_now_ms, remove_namespace_from_uri, retry_decorator
|
|
18
18
|
|
|
19
19
|
|
|
20
20
|
def define_relationships(rules: Rules, data_set_id: int, stop_on_exception: bool = False) -> RelationshipDefinitions:
|
|
@@ -150,7 +150,7 @@ def rdf2relationships(
|
|
|
150
150
|
relationship_data_frame.rename(columns={0: "source_external_id", 1: "target_external_id"}, inplace=True)
|
|
151
151
|
|
|
152
152
|
# removes namespace
|
|
153
|
-
relationship_data_frame = relationship_data_frame.map(
|
|
153
|
+
relationship_data_frame = relationship_data_frame.map(remove_namespace_from_uri) # type: ignore[operator]
|
|
154
154
|
|
|
155
155
|
# adding prefix
|
|
156
156
|
if relationship_external_id_prefix:
|
|
@@ -21,7 +21,7 @@ from cognite.neat.legacy.rules.models.rdfpath import (
|
|
|
21
21
|
parse_traversal,
|
|
22
22
|
)
|
|
23
23
|
from cognite.neat.legacy.rules.models.rules import Rules
|
|
24
|
-
from cognite.neat.utils.utils import
|
|
24
|
+
from cognite.neat.utils.utils import remove_namespace_from_uri
|
|
25
25
|
|
|
26
26
|
|
|
27
27
|
def _generate_prefix_header(prefixes: dict[str, Namespace] = PREFIXES) -> str:
|
|
@@ -529,7 +529,7 @@ def triples2dictionary(triples: Iterable[tuple[URIRef, URIRef, str | URIRef]]) -
|
|
|
529
529
|
property_: str
|
|
530
530
|
value: str
|
|
531
531
|
uri: URIRef
|
|
532
|
-
id_, property_, value =
|
|
532
|
+
id_, property_, value = remove_namespace_from_uri(*triple) # type: ignore[misc]
|
|
533
533
|
uri = triple[0]
|
|
534
534
|
|
|
535
535
|
if uri not in dictionary:
|
|
@@ -24,7 +24,7 @@ from cognite.neat.legacy.rules.models.rdfpath import (
|
|
|
24
24
|
parse_rule,
|
|
25
25
|
)
|
|
26
26
|
from cognite.neat.legacy.rules.models.rules import Rules
|
|
27
|
-
from cognite.neat.utils.utils import
|
|
27
|
+
from cognite.neat.utils.utils import remove_namespace_from_uri
|
|
28
28
|
|
|
29
29
|
prom_total_proc_rules_g = Gauge("neat_total_processed_rules", "Number of processed rules", ["state"])
|
|
30
30
|
rules_processing_timing_metric = Gauge(
|
|
@@ -228,7 +228,7 @@ def domain2app_knowledge_graph(
|
|
|
228
228
|
# in the target graph then we should remove namespace from the object URI and store it as literal
|
|
229
229
|
if isinstance(rule.traversal, AllReferences) and rule_definition.property_type == "DatatypeProperty":
|
|
230
230
|
instance_df[EntityTypes.object] = instance_df[EntityTypes.object].apply(
|
|
231
|
-
lambda x: Literal(
|
|
231
|
+
lambda x: Literal(remove_namespace_from_uri(x))
|
|
232
232
|
)
|
|
233
233
|
|
|
234
234
|
if isinstance(rule, RawLookup):
|
|
@@ -14,7 +14,7 @@ from cognite.neat.legacy.rules.exporters._base import BaseExporter
|
|
|
14
14
|
from cognite.neat.legacy.rules.exporters._validation import are_properties_redefined
|
|
15
15
|
from cognite.neat.legacy.rules.models.rules import Class, Metadata, Property, Rules
|
|
16
16
|
from cognite.neat.legacy.rules.models.value_types import XSD_VALUE_TYPE_MAPPINGS
|
|
17
|
-
from cognite.neat.utils.utils import generate_exception_report,
|
|
17
|
+
from cognite.neat.utils.utils import generate_exception_report, remove_namespace_from_uri
|
|
18
18
|
|
|
19
19
|
if sys.version_info >= (3, 11):
|
|
20
20
|
from typing import Self
|
|
@@ -322,7 +322,7 @@ class OWLProperty(OntologyModel):
|
|
|
322
322
|
if len(v) > 1:
|
|
323
323
|
warnings.warn(
|
|
324
324
|
exceptions.OntologyMultiTypeProperty(
|
|
325
|
-
|
|
325
|
+
remove_namespace_from_uri(info.data["id_"]), [remove_namespace_from_uri(t) for t in v]
|
|
326
326
|
).message,
|
|
327
327
|
category=exceptions.OntologyMultiTypeProperty,
|
|
328
328
|
stacklevel=2,
|
|
@@ -334,7 +334,7 @@ class OWLProperty(OntologyModel):
|
|
|
334
334
|
if len(v) > 1:
|
|
335
335
|
warnings.warn(
|
|
336
336
|
exceptions.OntologyMultiRangeProperty(
|
|
337
|
-
|
|
337
|
+
remove_namespace_from_uri(info.data["id_"]), [remove_namespace_from_uri(t) for t in v]
|
|
338
338
|
).message,
|
|
339
339
|
category=exceptions.OntologyMultiRangeProperty,
|
|
340
340
|
stacklevel=2,
|
|
@@ -346,7 +346,7 @@ class OWLProperty(OntologyModel):
|
|
|
346
346
|
if len(v) > 1:
|
|
347
347
|
warnings.warn(
|
|
348
348
|
exceptions.OntologyMultiDomainProperty(
|
|
349
|
-
|
|
349
|
+
remove_namespace_from_uri(info.data["id_"]), [remove_namespace_from_uri(t) for t in v]
|
|
350
350
|
).message,
|
|
351
351
|
category=exceptions.OntologyMultiDomainProperty,
|
|
352
352
|
stacklevel=2,
|
|
@@ -357,7 +357,7 @@ class OWLProperty(OntologyModel):
|
|
|
357
357
|
def has_multi_name(cls, v, info: ValidationInfo):
|
|
358
358
|
if len(v) > 1:
|
|
359
359
|
warnings.warn(
|
|
360
|
-
exceptions.OntologyMultiLabeledProperty(
|
|
360
|
+
exceptions.OntologyMultiLabeledProperty(remove_namespace_from_uri(info.data["id_"]), v).message,
|
|
361
361
|
category=exceptions.OntologyMultiLabeledProperty,
|
|
362
362
|
stacklevel=2,
|
|
363
363
|
)
|
|
@@ -367,7 +367,7 @@ class OWLProperty(OntologyModel):
|
|
|
367
367
|
def has_multi_comment(cls, v, info: ValidationInfo):
|
|
368
368
|
if len(v) > 1:
|
|
369
369
|
warnings.warn(
|
|
370
|
-
exceptions.OntologyMultiDefinitionProperty(
|
|
370
|
+
exceptions.OntologyMultiDefinitionProperty(remove_namespace_from_uri(info.data["id_"])).message,
|
|
371
371
|
category=exceptions.OntologyMultiDefinitionProperty,
|
|
372
372
|
stacklevel=2,
|
|
373
373
|
)
|
|
@@ -14,7 +14,7 @@ from cognite.neat.constants import PREFIXES
|
|
|
14
14
|
from cognite.neat.legacy.rules import exceptions
|
|
15
15
|
from cognite.neat.legacy.rules.exporters._rules2rules import to_dms_name
|
|
16
16
|
from cognite.neat.legacy.rules.models.tables import Tables
|
|
17
|
-
from cognite.neat.utils.utils import get_namespace,
|
|
17
|
+
from cognite.neat.utils.utils import get_namespace, remove_namespace_from_uri, uri_to_short_form
|
|
18
18
|
|
|
19
19
|
from ._base import BaseImporter
|
|
20
20
|
|
|
@@ -173,7 +173,7 @@ def _graph_to_data_model_dict(graph: Graph, max_number_of_instance: int = -1) ->
|
|
|
173
173
|
|
|
174
174
|
for class_ in _get_class_ids(graph):
|
|
175
175
|
_add_uri_namespace_to_prefixes(class_, prefixes)
|
|
176
|
-
class_name =
|
|
176
|
+
class_name = remove_namespace_from_uri(class_)
|
|
177
177
|
|
|
178
178
|
if class_name in data_model:
|
|
179
179
|
warnings.warn(
|
|
@@ -187,7 +187,7 @@ def _graph_to_data_model_dict(graph: Graph, max_number_of_instance: int = -1) ->
|
|
|
187
187
|
|
|
188
188
|
for instance in _get_class_instance_ids(graph, class_, max_number_of_instance):
|
|
189
189
|
for property_, occurrence, data_type, object_type in _define_instance_properties(graph, instance):
|
|
190
|
-
property_name =
|
|
190
|
+
property_name = remove_namespace_from_uri(property_)
|
|
191
191
|
_add_uri_namespace_to_prefixes(property_, prefixes)
|
|
192
192
|
|
|
193
193
|
type_ = data_type if data_type else object_type
|
|
@@ -196,7 +196,7 @@ def _graph_to_data_model_dict(graph: Graph, max_number_of_instance: int = -1) ->
|
|
|
196
196
|
if not type_:
|
|
197
197
|
continue
|
|
198
198
|
|
|
199
|
-
type_name =
|
|
199
|
+
type_name = remove_namespace_from_uri(type_)
|
|
200
200
|
_add_uri_namespace_to_prefixes(type_, prefixes)
|
|
201
201
|
|
|
202
202
|
if property_name not in data_model[class_name]["properties"]:
|
|
@@ -4,7 +4,7 @@ import numpy as np
|
|
|
4
4
|
import pandas as pd
|
|
5
5
|
from rdflib import OWL, Graph
|
|
6
6
|
|
|
7
|
-
from cognite.neat.utils.utils import
|
|
7
|
+
from cognite.neat.utils.utils import remove_namespace_from_uri
|
|
8
8
|
|
|
9
9
|
|
|
10
10
|
def parse_owl_classes(graph: Graph, make_compliant: bool = False, language: str = "en") -> pd.DataFrame:
|
|
@@ -84,10 +84,10 @@ def _parse_raw_dataframe(query_results: list[tuple]) -> pd.DataFrame:
|
|
|
84
84
|
df.replace(np.nan, "", regex=True, inplace=True)
|
|
85
85
|
|
|
86
86
|
df.Source = df.Class
|
|
87
|
-
df.Class = df.Class.apply(lambda x:
|
|
87
|
+
df.Class = df.Class.apply(lambda x: remove_namespace_from_uri(x))
|
|
88
88
|
df["Source Entity Name"] = df.Class
|
|
89
89
|
df["Match"] = len(df) * ["exact"]
|
|
90
|
-
df["Parent Class"] = df["Parent Class"].apply(lambda x:
|
|
90
|
+
df["Parent Class"] = df["Parent Class"].apply(lambda x: remove_namespace_from_uri(x))
|
|
91
91
|
|
|
92
92
|
return df
|
|
93
93
|
|
|
@@ -4,7 +4,7 @@ import numpy as np
|
|
|
4
4
|
import pandas as pd
|
|
5
5
|
from rdflib import Graph
|
|
6
6
|
|
|
7
|
-
from cognite.neat.utils.utils import
|
|
7
|
+
from cognite.neat.utils.utils import remove_namespace_from_uri
|
|
8
8
|
|
|
9
9
|
from ._owl2classes import _data_type_property_class, _object_property_class, _thing_class
|
|
10
10
|
|
|
@@ -92,12 +92,12 @@ def _parse_raw_dataframe(query_results: list[tuple]) -> pd.DataFrame:
|
|
|
92
92
|
df.replace(np.nan, "", regex=True, inplace=True)
|
|
93
93
|
|
|
94
94
|
df.Source = df.Property
|
|
95
|
-
df.Class = df.Class.apply(lambda x:
|
|
96
|
-
df.Property = df.Property.apply(lambda x:
|
|
97
|
-
df.Type = df.Type.apply(lambda x:
|
|
95
|
+
df.Class = df.Class.apply(lambda x: remove_namespace_from_uri(x))
|
|
96
|
+
df.Property = df.Property.apply(lambda x: remove_namespace_from_uri(x))
|
|
97
|
+
df.Type = df.Type.apply(lambda x: remove_namespace_from_uri(x))
|
|
98
98
|
df["Source Entity Name"] = df.Property
|
|
99
99
|
df["Match Type"] = len(df) * ["exact"]
|
|
100
|
-
df["_property_type"] = df["_property_type"].apply(lambda x:
|
|
100
|
+
df["_property_type"] = df["_property_type"].apply(lambda x: remove_namespace_from_uri(x))
|
|
101
101
|
|
|
102
102
|
return df
|
|
103
103
|
|
|
@@ -20,7 +20,7 @@ from cognite.neat.rules.models.information import (
|
|
|
20
20
|
InformationProperty,
|
|
21
21
|
InformationRules,
|
|
22
22
|
)
|
|
23
|
-
from cognite.neat.utils.utils import generate_exception_report,
|
|
23
|
+
from cognite.neat.utils.utils import generate_exception_report, remove_namespace_from_uri
|
|
24
24
|
|
|
25
25
|
from ._base import BaseExporter
|
|
26
26
|
from ._validation import are_properties_redefined
|
|
@@ -350,7 +350,7 @@ class OWLProperty(OntologyModel):
|
|
|
350
350
|
if len(v) > 1:
|
|
351
351
|
warnings.warn(
|
|
352
352
|
exceptions.OntologyMultiTypeProperty(
|
|
353
|
-
|
|
353
|
+
remove_namespace_from_uri(info.data["id_"]), [remove_namespace_from_uri(t) for t in v]
|
|
354
354
|
).message,
|
|
355
355
|
category=exceptions.OntologyMultiTypeProperty,
|
|
356
356
|
stacklevel=2,
|
|
@@ -362,7 +362,7 @@ class OWLProperty(OntologyModel):
|
|
|
362
362
|
if len(v) > 1:
|
|
363
363
|
warnings.warn(
|
|
364
364
|
exceptions.OntologyMultiRangeProperty(
|
|
365
|
-
|
|
365
|
+
remove_namespace_from_uri(info.data["id_"]), [remove_namespace_from_uri(t) for t in v]
|
|
366
366
|
).message,
|
|
367
367
|
category=exceptions.OntologyMultiRangeProperty,
|
|
368
368
|
stacklevel=2,
|
|
@@ -374,7 +374,7 @@ class OWLProperty(OntologyModel):
|
|
|
374
374
|
if len(v) > 1:
|
|
375
375
|
warnings.warn(
|
|
376
376
|
exceptions.OntologyMultiDomainProperty(
|
|
377
|
-
|
|
377
|
+
remove_namespace_from_uri(info.data["id_"]), [remove_namespace_from_uri(t) for t in v]
|
|
378
378
|
).message,
|
|
379
379
|
category=exceptions.OntologyMultiDomainProperty,
|
|
380
380
|
stacklevel=2,
|
|
@@ -385,7 +385,7 @@ class OWLProperty(OntologyModel):
|
|
|
385
385
|
def has_multi_name(cls, v, info: ValidationInfo):
|
|
386
386
|
if len(v) > 1:
|
|
387
387
|
warnings.warn(
|
|
388
|
-
exceptions.OntologyMultiLabeledProperty(
|
|
388
|
+
exceptions.OntologyMultiLabeledProperty(remove_namespace_from_uri(info.data["id_"]), v).message,
|
|
389
389
|
category=exceptions.OntologyMultiLabeledProperty,
|
|
390
390
|
stacklevel=2,
|
|
391
391
|
)
|
|
@@ -395,7 +395,7 @@ class OWLProperty(OntologyModel):
|
|
|
395
395
|
def has_multi_comment(cls, v, info: ValidationInfo):
|
|
396
396
|
if len(v) > 1:
|
|
397
397
|
warnings.warn(
|
|
398
|
-
exceptions.OntologyMultiDefinitionProperty(
|
|
398
|
+
exceptions.OntologyMultiDefinitionProperty(remove_namespace_from_uri(info.data["id_"])).message,
|
|
399
399
|
category=exceptions.OntologyMultiDefinitionProperty,
|
|
400
400
|
stacklevel=2,
|
|
401
401
|
)
|
|
@@ -17,7 +17,7 @@ from cognite.neat.rules.models.information import (
|
|
|
17
17
|
InformationMetadata,
|
|
18
18
|
InformationRulesInput,
|
|
19
19
|
)
|
|
20
|
-
from cognite.neat.utils.utils import get_namespace,
|
|
20
|
+
from cognite.neat.utils.utils import get_namespace, remove_namespace_from_uri, uri_to_short_form
|
|
21
21
|
|
|
22
22
|
ORDERED_CLASSES_QUERY = """SELECT ?class (count(?s) as ?instances )
|
|
23
23
|
WHERE { ?s a ?class . }
|
|
@@ -36,28 +36,36 @@ class InferenceImporter(BaseImporter):
|
|
|
36
36
|
"""Infers rules from a triple store.
|
|
37
37
|
|
|
38
38
|
Rules inference through analysis of knowledge graph provided in various formats.
|
|
39
|
-
Use the factory methods to create
|
|
39
|
+
Use the factory methods to create a triple store from sources such as
|
|
40
40
|
RDF files, JSON files, YAML files, XML files, or directly from a graph store.
|
|
41
41
|
|
|
42
42
|
Args:
|
|
43
43
|
issue_list: Issue list to store issues
|
|
44
44
|
graph: Knowledge graph
|
|
45
45
|
max_number_of_instance: Maximum number of instances to be used in inference
|
|
46
|
+
prefix: Prefix to be used for the inferred model
|
|
46
47
|
"""
|
|
47
48
|
|
|
48
|
-
def __init__(
|
|
49
|
+
def __init__(
|
|
50
|
+
self, issue_list: IssueList, graph: Graph, max_number_of_instance: int = -1, prefix: str = "inferred"
|
|
51
|
+
) -> None:
|
|
49
52
|
self.issue_list = issue_list
|
|
50
53
|
self.graph = graph
|
|
51
54
|
self.max_number_of_instance = max_number_of_instance
|
|
55
|
+
self.prefix = prefix
|
|
52
56
|
|
|
53
57
|
@classmethod
|
|
54
|
-
def from_graph_store(
|
|
58
|
+
def from_graph_store(
|
|
59
|
+
cls, store: NeatGraphStore, max_number_of_instance: int = -1, prefix: str = "inferred"
|
|
60
|
+
) -> "InferenceImporter":
|
|
55
61
|
issue_list = IssueList(title="Inferred from graph store")
|
|
56
62
|
|
|
57
|
-
return cls(issue_list, store.graph, max_number_of_instance=max_number_of_instance)
|
|
63
|
+
return cls(issue_list, store.graph, max_number_of_instance=max_number_of_instance, prefix=prefix)
|
|
58
64
|
|
|
59
65
|
@classmethod
|
|
60
|
-
def from_rdf_file(
|
|
66
|
+
def from_rdf_file(
|
|
67
|
+
cls, filepath: Path, max_number_of_instance: int = -1, prefix: str = "inferred"
|
|
68
|
+
) -> "InferenceImporter":
|
|
61
69
|
issue_list = IssueList(title=f"'{filepath.name}'")
|
|
62
70
|
|
|
63
71
|
graph = Graph()
|
|
@@ -66,18 +74,24 @@ class InferenceImporter(BaseImporter):
|
|
|
66
74
|
except Exception:
|
|
67
75
|
issue_list.append(issues.fileread.FileReadError(filepath))
|
|
68
76
|
|
|
69
|
-
return cls(issue_list, graph, max_number_of_instance=max_number_of_instance)
|
|
77
|
+
return cls(issue_list, graph, max_number_of_instance=max_number_of_instance, prefix=prefix)
|
|
70
78
|
|
|
71
79
|
@classmethod
|
|
72
|
-
def from_json_file(
|
|
80
|
+
def from_json_file(
|
|
81
|
+
cls, filepath: Path, max_number_of_instance: int = -1, prefix: str = "inferred"
|
|
82
|
+
) -> "InferenceImporter":
|
|
73
83
|
raise NotImplementedError("JSON file format is not supported yet.")
|
|
74
84
|
|
|
75
85
|
@classmethod
|
|
76
|
-
def from_yaml_file(
|
|
86
|
+
def from_yaml_file(
|
|
87
|
+
cls, filepath: Path, max_number_of_instance: int = -1, prefix: str = "inferred"
|
|
88
|
+
) -> "InferenceImporter":
|
|
77
89
|
raise NotImplementedError("YAML file format is not supported yet.")
|
|
78
90
|
|
|
79
91
|
@classmethod
|
|
80
|
-
def from_xml_file(
|
|
92
|
+
def from_xml_file(
|
|
93
|
+
cls, filepath: Path, max_number_of_instance: int = -1, prefix: str = "inferred"
|
|
94
|
+
) -> "InferenceImporter":
|
|
81
95
|
raise NotImplementedError("JSON file format is not supported yet.")
|
|
82
96
|
|
|
83
97
|
@overload
|
|
@@ -142,7 +156,7 @@ class InferenceImporter(BaseImporter):
|
|
|
142
156
|
for class_uri, no_instances in self.graph.query(ORDERED_CLASSES_QUERY): # type: ignore[misc]
|
|
143
157
|
self._add_uri_namespace_to_prefixes(cast(URIRef, class_uri), prefixes)
|
|
144
158
|
|
|
145
|
-
if (class_id :=
|
|
159
|
+
if (class_id := remove_namespace_from_uri(class_uri)) in classes:
|
|
146
160
|
# handles cases when class id is already present in classes
|
|
147
161
|
class_id = f"{class_id}_{len(classes)+1}"
|
|
148
162
|
|
|
@@ -164,7 +178,7 @@ class InferenceImporter(BaseImporter):
|
|
|
164
178
|
for property_uri, occurrence, data_type_uri, object_type_uri in self.graph.query( # type: ignore[misc]
|
|
165
179
|
INSTANCE_PROPERTIES_DEFINITION.replace("instance_id", instance)
|
|
166
180
|
): # type: ignore[misc]
|
|
167
|
-
property_id =
|
|
181
|
+
property_id = remove_namespace_from_uri(property_uri)
|
|
168
182
|
self._add_uri_namespace_to_prefixes(cast(URIRef, property_uri), prefixes)
|
|
169
183
|
value_type_uri = data_type_uri if data_type_uri else object_type_uri
|
|
170
184
|
|
|
@@ -173,7 +187,7 @@ class InferenceImporter(BaseImporter):
|
|
|
173
187
|
continue
|
|
174
188
|
|
|
175
189
|
self._add_uri_namespace_to_prefixes(cast(URIRef, value_type_uri), prefixes)
|
|
176
|
-
value_type_id =
|
|
190
|
+
value_type_id = remove_namespace_from_uri(value_type_uri)
|
|
177
191
|
id_ = f"{class_id}:{property_id}"
|
|
178
192
|
|
|
179
193
|
definition = {
|
|
@@ -239,8 +253,7 @@ class InferenceImporter(BaseImporter):
|
|
|
239
253
|
if Namespace(get_namespace(URI)) not in prefixes.values():
|
|
240
254
|
prefixes[f"prefix-{len(prefixes)+1}"] = Namespace(get_namespace(URI))
|
|
241
255
|
|
|
242
|
-
|
|
243
|
-
def _default_metadata(cls):
|
|
256
|
+
def _default_metadata(self):
|
|
244
257
|
return InformationMetadata(
|
|
245
258
|
name="Inferred Model",
|
|
246
259
|
creator="NEAT",
|
|
@@ -248,7 +261,7 @@ class InferenceImporter(BaseImporter):
|
|
|
248
261
|
created=datetime.now(),
|
|
249
262
|
updated=datetime.now(),
|
|
250
263
|
description="Inferred model from knowledge graph",
|
|
251
|
-
prefix=
|
|
264
|
+
prefix=self.prefix,
|
|
252
265
|
namespace=DEFAULT_NAMESPACE,
|
|
253
266
|
)
|
|
254
267
|
|
|
@@ -262,12 +275,9 @@ class InferenceImporter(BaseImporter):
|
|
|
262
275
|
|
|
263
276
|
@classmethod
|
|
264
277
|
def _read_value_type_occurrence_from_comment(cls, value_type: str, comment: str) -> int:
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
),
|
|
272
|
-
).group(1)
|
|
273
|
-
)
|
|
278
|
+
if result := re.search(
|
|
279
|
+
rf"with value type <{value_type}> which occurs <(\d+)> times in the graph",
|
|
280
|
+
comment,
|
|
281
|
+
):
|
|
282
|
+
return int(result.group(1))
|
|
283
|
+
return 0
|
|
@@ -5,7 +5,7 @@ import pandas as pd
|
|
|
5
5
|
from rdflib import OWL, Graph
|
|
6
6
|
|
|
7
7
|
from cognite.neat.rules.models._base import MatchType
|
|
8
|
-
from cognite.neat.utils.utils import
|
|
8
|
+
from cognite.neat.utils.utils import remove_namespace_from_uri
|
|
9
9
|
|
|
10
10
|
|
|
11
11
|
def parse_owl_classes(graph: Graph, language: str = "en") -> list[dict]:
|
|
@@ -68,10 +68,10 @@ def _parse_raw_dataframe(query_results: list[tuple]) -> pd.DataFrame:
|
|
|
68
68
|
df.replace(np.nan, "", regex=True, inplace=True)
|
|
69
69
|
|
|
70
70
|
df.Reference = df.Class
|
|
71
|
-
df.Class = df.Class.apply(lambda x:
|
|
71
|
+
df.Class = df.Class.apply(lambda x: remove_namespace_from_uri(x))
|
|
72
72
|
df["Match Type"] = len(df) * [MatchType.exact]
|
|
73
73
|
df["Comment"] = len(df) * [None]
|
|
74
|
-
df["Parent Class"] = df["Parent Class"].apply(lambda x:
|
|
74
|
+
df["Parent Class"] = df["Parent Class"].apply(lambda x: remove_namespace_from_uri(x))
|
|
75
75
|
|
|
76
76
|
return df
|
|
77
77
|
|
|
@@ -5,7 +5,7 @@ import pandas as pd
|
|
|
5
5
|
from rdflib import Graph
|
|
6
6
|
|
|
7
7
|
from cognite.neat.rules.models._base import MatchType
|
|
8
|
-
from cognite.neat.utils.utils import
|
|
8
|
+
from cognite.neat.utils.utils import remove_namespace_from_uri
|
|
9
9
|
|
|
10
10
|
from ._owl2classes import _data_type_property_class, _object_property_class, _thing_class
|
|
11
11
|
|
|
@@ -82,12 +82,12 @@ def _parse_raw_dataframe(query_results: list[tuple]) -> pd.DataFrame:
|
|
|
82
82
|
df.replace(np.nan, "", regex=True, inplace=True)
|
|
83
83
|
|
|
84
84
|
df.Reference = df.Property
|
|
85
|
-
df.Class = df.Class.apply(lambda x:
|
|
86
|
-
df.Property = df.Property.apply(lambda x:
|
|
87
|
-
df["Value Type"] = df["Value Type"].apply(lambda x:
|
|
85
|
+
df.Class = df.Class.apply(lambda x: remove_namespace_from_uri(x))
|
|
86
|
+
df.Property = df.Property.apply(lambda x: remove_namespace_from_uri(x))
|
|
87
|
+
df["Value Type"] = df["Value Type"].apply(lambda x: remove_namespace_from_uri(x))
|
|
88
88
|
df["Match Type"] = len(df) * [MatchType.exact]
|
|
89
89
|
df["Comment"] = len(df) * [None]
|
|
90
|
-
df["_property_type"] = df["_property_type"].apply(lambda x:
|
|
90
|
+
df["_property_type"] = df["_property_type"].apply(lambda x: remove_namespace_from_uri(x))
|
|
91
91
|
|
|
92
92
|
return df
|
|
93
93
|
|
cognite/neat/utils/__init__.py
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
from .utils import
|
|
1
|
+
from .utils import remove_namespace_from_uri
|
|
2
2
|
|
|
3
|
-
__all__ = ["
|
|
3
|
+
__all__ = ["remove_namespace_from_uri"]
|