cognite-neat 0.102.0__py3-none-any.whl → 0.103.0__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.

Files changed (64) hide show
  1. cognite/neat/__init__.py +1 -1
  2. cognite/neat/_app/api/routers/crud.py +1 -1
  3. cognite/neat/_client/__init__.py +1 -1
  4. cognite/neat/_client/_api/data_modeling_loaders.py +1 -1
  5. cognite/neat/_client/_api/schema.py +1 -1
  6. cognite/neat/_graph/_tracking/__init__.py +1 -1
  7. cognite/neat/_graph/extractors/__init__.py +8 -8
  8. cognite/neat/_graph/extractors/_mock_graph_generator.py +2 -3
  9. cognite/neat/_graph/loaders/_base.py +1 -1
  10. cognite/neat/_graph/loaders/_rdf2dms.py +165 -47
  11. cognite/neat/_graph/transformers/__init__.py +13 -9
  12. cognite/neat/_graph/transformers/_value_type.py +196 -2
  13. cognite/neat/_issues/__init__.py +6 -6
  14. cognite/neat/_issues/_base.py +4 -4
  15. cognite/neat/_issues/errors/__init__.py +22 -22
  16. cognite/neat/_issues/formatters.py +1 -1
  17. cognite/neat/_issues/warnings/__init__.py +20 -18
  18. cognite/neat/_issues/warnings/_properties.py +7 -0
  19. cognite/neat/_issues/warnings/user_modeling.py +2 -2
  20. cognite/neat/_rules/analysis/__init__.py +1 -1
  21. cognite/neat/_rules/catalog/__init__.py +1 -0
  22. cognite/neat/_rules/catalog/hello_world_pump.xlsx +0 -0
  23. cognite/neat/_rules/exporters/__init__.py +5 -5
  24. cognite/neat/_rules/exporters/_rules2excel.py +5 -4
  25. cognite/neat/_rules/importers/__init__.py +4 -4
  26. cognite/neat/_rules/importers/_base.py +7 -3
  27. cognite/neat/_rules/importers/_rdf/__init__.py +1 -1
  28. cognite/neat/_rules/models/__init__.py +5 -5
  29. cognite/neat/_rules/models/_base_rules.py +1 -1
  30. cognite/neat/_rules/models/dms/__init__.py +11 -11
  31. cognite/neat/_rules/models/dms/_validation.py +16 -10
  32. cognite/neat/_rules/models/entities/__init__.py +26 -26
  33. cognite/neat/_rules/models/information/__init__.py +5 -5
  34. cognite/neat/_rules/models/mapping/_classic2core.yaml +54 -8
  35. cognite/neat/_rules/transformers/__init__.py +12 -12
  36. cognite/neat/_rules/transformers/_pipelines.py +10 -5
  37. cognite/neat/_session/_base.py +71 -0
  38. cognite/neat/_session/_collector.py +3 -1
  39. cognite/neat/_session/_drop.py +10 -0
  40. cognite/neat/_session/_inspect.py +35 -1
  41. cognite/neat/_session/_mapping.py +5 -0
  42. cognite/neat/_session/_prepare.py +121 -15
  43. cognite/neat/_session/_read.py +180 -20
  44. cognite/neat/_session/_set.py +11 -1
  45. cognite/neat/_session/_show.py +41 -2
  46. cognite/neat/_session/_to.py +58 -10
  47. cognite/neat/_session/engine/__init__.py +1 -1
  48. cognite/neat/_store/__init__.py +3 -2
  49. cognite/neat/_store/{_base.py → _graph_store.py} +33 -0
  50. cognite/neat/_store/_provenance.py +11 -1
  51. cognite/neat/_store/_rules_store.py +20 -0
  52. cognite/neat/_utils/auth.py +1 -1
  53. cognite/neat/_utils/reader/__init__.py +1 -1
  54. cognite/neat/_version.py +2 -2
  55. cognite/neat/_workflows/__init__.py +3 -3
  56. cognite/neat/_workflows/steps/lib/current/graph_extractor.py +1 -1
  57. cognite/neat/_workflows/steps/lib/current/rules_exporter.py +1 -1
  58. cognite/neat/_workflows/steps/lib/current/rules_importer.py +2 -2
  59. cognite/neat/_workflows/steps/lib/io/io_steps.py +3 -3
  60. {cognite_neat-0.102.0.dist-info → cognite_neat-0.103.0.dist-info}/METADATA +1 -1
  61. {cognite_neat-0.102.0.dist-info → cognite_neat-0.103.0.dist-info}/RECORD +64 -62
  62. {cognite_neat-0.102.0.dist-info → cognite_neat-0.103.0.dist-info}/LICENSE +0 -0
  63. {cognite_neat-0.102.0.dist-info → cognite_neat-0.103.0.dist-info}/WHEEL +0 -0
  64. {cognite_neat-0.102.0.dist-info → cognite_neat-0.103.0.dist-info}/entry_points.txt +0 -0
@@ -21,6 +21,30 @@ from .exceptions import session_class_wrapper
21
21
 
22
22
  @session_class_wrapper
23
23
  class ShowAPI:
24
+ """Visualise a verified data model or instances contained in the graph store.
25
+ See, for example, `.data_model()` or `.instances()` for more.
26
+
27
+ Example:
28
+ Show instances
29
+ ```python
30
+ from cognite.neat import NeatSession
31
+ from cognite.neat import get_cognite_client
32
+
33
+ client = get_cognite_client(env_file_path=".env")
34
+ neat = NeatSession(client, storage="oxigraph") # Storage optimised for storage visualisation
35
+
36
+ # .... intermediate steps of reading, infering verifying and converting a data model and instances
37
+
38
+ neat.show.instances()
39
+ ```
40
+
41
+ Example:
42
+ Show data model
43
+ ```python
44
+ neat.show.data_model()
45
+ ```
46
+ """
47
+
24
48
  def __init__(self, state: SessionState) -> None:
25
49
  self._state = state
26
50
  self.data_model = ShowDataModelAPI(self._state)
@@ -65,6 +89,8 @@ class ShowBaseAPI:
65
89
 
66
90
  @session_class_wrapper
67
91
  class ShowDataModelAPI(ShowBaseAPI):
92
+ """Visualises the verified data model."""
93
+
68
94
  def __init__(self, state: SessionState) -> None:
69
95
  super().__init__(state)
70
96
  self._state = state
@@ -97,9 +123,18 @@ class ShowDataModelAPI(ShowBaseAPI):
97
123
  """Generate a DiGraph from the last verified DMS rules."""
98
124
  di_graph = nx.DiGraph()
99
125
 
126
+ # Views with properties or used as ValueType
127
+ # If a view is not used in properties or as ValueType, it is not added to the graph
128
+ # as we typically do not have the properties for it.
129
+ used_views = {prop_.view for prop_ in rules.properties} | {
130
+ prop_.value_type for prop_ in rules.properties if isinstance(prop_.value_type, ViewEntity)
131
+ }
132
+
100
133
  # Add nodes and edges from Views sheet
101
134
  for view in rules.views:
102
- # if possible use human readable label coming from the view name
135
+ if view.view not in used_views:
136
+ continue
137
+ # if possible use humanreadable label coming from the view name
103
138
  if not di_graph.has_node(view.view.suffix):
104
139
  di_graph.add_node(view.view.suffix, label=view.view.suffix)
105
140
 
@@ -123,7 +158,7 @@ class ShowDataModelAPI(ShowBaseAPI):
123
158
 
124
159
  # Add nodes and edges from Views sheet
125
160
  for class_ in rules.classes:
126
- # if possible use human readable label coming from the view name
161
+ # if possible use humanreadable label coming from the view name
127
162
  if not di_graph.has_node(class_.class_.suffix):
128
163
  di_graph.add_node(
129
164
  class_.class_.suffix,
@@ -228,6 +263,8 @@ class ShowDataModelImplementsAPI(ShowBaseAPI):
228
263
 
229
264
  @session_class_wrapper
230
265
  class ShowDataModelProvenanceAPI(ShowBaseAPI):
266
+ """Visualises the provenance or steps that have been executed in the NeatSession."""
267
+
231
268
  def __init__(self, state: SessionState) -> None:
232
269
  super().__init__(state)
233
270
  self._state = state
@@ -288,6 +325,8 @@ class ShowDataModelProvenanceAPI(ShowBaseAPI):
288
325
 
289
326
  @session_class_wrapper
290
327
  class ShowInstanceAPI(ShowBaseAPI):
328
+ """Visualise the instances contained in the graph store."""
329
+
291
330
  def __init__(self, state: SessionState) -> None:
292
331
  super().__init__(state)
293
332
  self._state = state
@@ -19,6 +19,11 @@ from .exceptions import NeatSessionError, session_class_wrapper
19
19
 
20
20
  @session_class_wrapper
21
21
  class ToAPI:
22
+ """API used to write the contents of a NeatSession to a specified destination. For instance writing information
23
+ rules or DMS rules to a NEAT rules Excel spreadsheet, or writing a verified data model to CDF.
24
+
25
+ """
26
+
22
27
  def __init__(self, state: SessionState, client: NeatClient | None, verbose: bool) -> None:
23
28
  self._state = state
24
29
  self._verbose = verbose
@@ -34,8 +39,22 @@ class ToAPI:
34
39
  Args:
35
40
  io: The file path or file-like object to write the Excel file to.
36
41
  model: The format of the data model to export. Defaults to None.
42
+
43
+ Example:
44
+ Export information model to excel rules sheet
45
+ ```python
46
+ information_rules_file_name = "information_rules.xlsx"
47
+ neat.to.excel(information_rules_file_name, model="information")
48
+ ```
49
+
50
+ Example:
51
+ Export data model to excel rules sheet
52
+ ```python
53
+ dms_rules_file_name = "dms_rules.xlsx"
54
+ neat.to.excel(information_rules_file_name, model="dms")
55
+ ```
37
56
  """
38
- exporter = exporters.ExcelExporter()
57
+ exporter = exporters.ExcelExporter(styling="maximal")
39
58
  rules: VerifiedRules
40
59
  if model == "information" or model == "logical":
41
60
  rules = self._state.data_model.last_verified_information_rules[1]
@@ -63,13 +82,33 @@ class ToAPI:
63
82
  format: The format of the YAML file. Defaults to "neat".
64
83
  skip_system_spaces: If True, system spaces will be skipped. Defaults to True.
65
84
 
66
- ... note::
67
-
85
+ !!! note "YAML formats"
68
86
  - "neat": This is the format Neat uses to store the data model.
69
87
  - "toolkit": This is the format used by Cognite Toolkit, that matches the CDF API.
70
88
 
71
89
  Returns:
72
90
  str | None: If io is None, the YAML string will be returned. Otherwise, None will be returned.
91
+
92
+ Example:
93
+ Export to yaml file in the case of "neat" format
94
+ ```python
95
+ your_yaml_file_name = "neat_rules.yaml"
96
+ neat.to.yaml(your_yaml_file_name, format="neat")
97
+ ```
98
+
99
+ Example:
100
+ Export yaml files as a zip folder in the case of "toolkit" format
101
+ ```python
102
+ your_zip_folder_name = "toolkit_data_model_files.zip"
103
+ neat.to.yaml(your_zip_folder_name, format="toolkit")
104
+ ```
105
+
106
+ Example:
107
+ Export yaml files to a folder in the case of "toolkit" format
108
+ ```python
109
+ your_folder_name = "my_project/data_model_files"
110
+ neat.to.yaml(your_folder_name, format="toolkit")
111
+ ```
73
112
  """
74
113
  if format == "neat":
75
114
  exporter = exporters.YAMLExporter()
@@ -97,12 +136,21 @@ class ToAPI:
97
136
 
98
137
  @session_class_wrapper
99
138
  class CDFToAPI:
139
+ """Write a verified Data Model and Instances to CDF."""
140
+
100
141
  def __init__(self, state: SessionState, client: NeatClient | None, verbose: bool) -> None:
101
142
  self._client = client
102
143
  self._state = state
103
144
  self._verbose = verbose
104
145
 
105
146
  def instances(self, space: str | None = None) -> UploadResultList:
147
+ """Export the verified DMS instances to CDF.
148
+
149
+ Args:
150
+ space: Name of instance space to use. Default is to suffix the schema space with '_instances'.
151
+ Note this space is required to be different than the space with the data model.
152
+
153
+ """
106
154
  if not self._client:
107
155
  raise NeatSessionError("No CDF client provided!")
108
156
 
@@ -120,6 +168,7 @@ class CDFToAPI:
120
168
  self._state.data_model.last_verified_dms_rules[1],
121
169
  self._state.instances.store,
122
170
  instance_space=space,
171
+ client=self._client,
123
172
  )
124
173
  result = loader.load_into_cdf(self._client)
125
174
  self._state.instances.outcome.append(result)
@@ -144,13 +193,12 @@ class CDFToAPI:
144
193
  Note this only applies to spaces and containers if they contain data.
145
194
  components: The components to export. If None, all components will be exported. Defaults to None.
146
195
 
147
- ... note::
148
-
149
- - "fail": If any component already exists, the export will fail.
150
- - "skip": If any component already exists, it will be skipped.
151
- - "update": If any component already exists, it will be updated.
152
- - "force": If any component already exists, and the update fails, it will be deleted and recreated.
153
- - "recreate": All components will be deleted and recreated. The exception is spaces, which will be updated.
196
+ !!! note "Data Model creation modes"
197
+ - "fail": If any component already exists, the export will fail.
198
+ - "skip": If any component already exists, it will be skipped.
199
+ - "update": If any component already exists, it will be updated.
200
+ - "force": If any component already exists, and the update fails, it will be deleted and recreated.
201
+ - "recreate": All components will be deleted and recreated. The exception is spaces, which will be updated.
154
202
 
155
203
  """
156
204
 
@@ -1,4 +1,4 @@
1
1
  from ._import import import_engine
2
2
  from ._load import load_neat_engine
3
3
 
4
- __all__ = ["load_neat_engine", "import_engine"]
4
+ __all__ = ["import_engine", "load_neat_engine"]
@@ -1,3 +1,4 @@
1
- from ._base import NeatGraphStore
1
+ from ._graph_store import NeatGraphStore
2
+ from ._rules_store import NeatRulesStore
2
3
 
3
- __all__ = ["NeatGraphStore"]
4
+ __all__ = ["NeatGraphStore", "NeatRulesStore"]
@@ -315,6 +315,39 @@ class NeatGraphStore:
315
315
 
316
316
  yield from self._read_via_class_entity(class_entity)
317
317
 
318
+ def count_of_id(self, neat_id: URIRef) -> int:
319
+ """Count the number of instances of a given type
320
+
321
+ Args:
322
+ neat_id: Type for which instances are to be counted
323
+
324
+ Returns:
325
+ Number of instances
326
+ """
327
+ if not self.rules:
328
+ warnings.warn("Rules not found in graph store!", stacklevel=2)
329
+ return 0
330
+
331
+ class_entity = next(
332
+ (definition.class_ for definition in self.rules.classes if definition.neatId == neat_id), None
333
+ )
334
+ if not class_entity:
335
+ warnings.warn("Desired type not found in graph!", stacklevel=2)
336
+ return 0
337
+
338
+ if not (class_uri := InformationAnalysis(self.rules).class_uri(class_entity)):
339
+ warnings.warn(
340
+ f"Class {class_entity.suffix} does not have namespace defined for prefix {class_entity.prefix} Rules!",
341
+ stacklevel=2,
342
+ )
343
+ return 0
344
+
345
+ return self.count_of_type(class_uri)
346
+
347
+ def count_of_type(self, class_uri: URIRef) -> int:
348
+ query = f"SELECT (COUNT(?instance) AS ?instanceCount) WHERE {{ ?instance a <{class_uri}> }}"
349
+ return int(next(iter(self.graph.query(query)))[0]) # type: ignore[arg-type, index]
350
+
318
351
  def _parse_file(
319
352
  self,
320
353
  filepath: Path,
@@ -20,12 +20,13 @@ import uuid
20
20
  from collections.abc import Iterable, Sequence
21
21
  from dataclasses import dataclass, field
22
22
  from datetime import datetime
23
- from typing import Optional, TypeVar
23
+ from typing import Generic, Optional, TypeVar
24
24
 
25
25
  from cognite.client.data_classes.data_modeling import DataModelId, DataModelIdentifier
26
26
  from rdflib import PROV, RDF, Literal, URIRef
27
27
 
28
28
  from cognite.neat._constants import CDF_NAMESPACE, DEFAULT_NAMESPACE
29
+ from cognite.neat._issues import IssueList
29
30
  from cognite.neat._rules._shared import JustRules, ReadRules, VerifiedRules
30
31
  from cognite.neat._shared import FrozenNeatObject, NeatList, Triple
31
32
 
@@ -50,6 +51,7 @@ UNKNOWN_AGENT = Agent(acted_on_behalf_of="UNKNOWN", id_=DEFAULT_NAMESPACE["unkno
50
51
  @dataclass(frozen=True)
51
52
  class Entity:
52
53
  was_attributed_to: Agent
54
+ issues: IssueList = field(default_factory=IssueList)
53
55
  was_generated_by: Optional["Activity"] = field(default=None, repr=False)
54
56
  id_: URIRef = DEFAULT_NAMESPACE["graph-store"]
55
57
 
@@ -117,6 +119,14 @@ class Entity:
117
119
  )
118
120
 
119
121
 
122
+ T_Result = TypeVar("T_Result")
123
+
124
+
125
+ @dataclass(frozen=True)
126
+ class ModelEntity(Entity, Generic[T_Result]):
127
+ result: T_Result | None = None
128
+
129
+
120
130
  INSTANCES_ENTITY = Entity(was_attributed_to=NEAT_AGENT, id_=CDF_NAMESPACE["instances"])
121
131
 
122
132
 
@@ -0,0 +1,20 @@
1
+ from cognite.neat._issues import IssueList
2
+ from cognite.neat._rules.exporters import BaseExporter
3
+ from cognite.neat._rules.importers import BaseImporter
4
+ from cognite.neat._rules.transformers import RulesTransformer
5
+
6
+ from ._provenance import Provenance
7
+
8
+
9
+ class NeatRulesStore:
10
+ def __init__(self):
11
+ self._provenance = Provenance()
12
+
13
+ def write(self, importer: BaseImporter) -> IssueList:
14
+ raise NotImplementedError()
15
+
16
+ def transform(self, transformer: RulesTransformer) -> IssueList:
17
+ raise NotImplementedError()
18
+
19
+ def read(self, exporter: BaseExporter) -> IssueList:
20
+ raise NotImplementedError()
@@ -11,7 +11,7 @@ from cognite.client.credentials import CredentialProvider, OAuthClientCredential
11
11
  from cognite.neat import _version
12
12
  from cognite.neat._utils.auxiliary import local_import
13
13
 
14
- __all__ = ["get_cognite_client", "EnvironmentVariables"]
14
+ __all__ = ["EnvironmentVariables", "get_cognite_client"]
15
15
 
16
16
  _LOGIN_FLOW: TypeAlias = Literal["infer", "client_credentials", "interactive", "token"]
17
17
  _VALID_LOGIN_FLOWS = get_args(_LOGIN_FLOW)
@@ -1,3 +1,3 @@
1
1
  from ._base import GitHubReader, HttpFileReader, NeatReader, PathReader
2
2
 
3
- __all__ = ["NeatReader", "PathReader", "GitHubReader", "HttpFileReader"]
3
+ __all__ = ["GitHubReader", "HttpFileReader", "NeatReader", "PathReader"]
cognite/neat/_version.py CHANGED
@@ -1,2 +1,2 @@
1
- __version__ = "0.102.0"
2
- __engine__ = "^2.0.2"
1
+ __version__ = "0.103.0"
2
+ __engine__ = "^2.0.3"
@@ -9,9 +9,9 @@ from cognite.neat._workflows.model import (
9
9
 
10
10
  __all__ = [
11
11
  "BaseWorkflow",
12
- "WorkflowStepDefinition",
12
+ "FlowMessage",
13
+ "WorkflowFullStateReport",
13
14
  "WorkflowManager",
15
+ "WorkflowStepDefinition",
14
16
  "WorkflowStepEvent",
15
- "WorkflowFullStateReport",
16
- "FlowMessage",
17
17
  ]
@@ -10,7 +10,7 @@ from cognite.neat._workflows.model import FlowMessage, StepExecutionStatus
10
10
  from cognite.neat._workflows.steps.data_contracts import MultiRuleData, NeatGraph
11
11
  from cognite.neat._workflows.steps.step_model import Configurable, Step
12
12
 
13
- __all__ = ["GraphFromRdfFile", "GraphFromMockData"]
13
+ __all__ = ["GraphFromMockData", "GraphFromRdfFile"]
14
14
 
15
15
  CATEGORY = __name__.split(".")[-1].replace("_", " ").title()
16
16
 
@@ -16,12 +16,12 @@ from cognite.neat._workflows.steps.data_contracts import CogniteClient, MultiRul
16
16
  from cognite.neat._workflows.steps.step_model import Configurable, Step
17
17
 
18
18
  __all__ = [
19
+ "DeleteDataModelFromCDF",
19
20
  "RulesToDMS",
20
21
  "RulesToExcel",
21
22
  "RulesToOntology",
22
23
  "RulesToSHACL",
23
24
  "RulesToSemanticDataModel",
24
- "DeleteDataModelFromCDF",
25
25
  ]
26
26
 
27
27
 
@@ -19,10 +19,10 @@ from cognite.neat._workflows.steps.step_model import Configurable, Step
19
19
  CATEGORY = __name__.split(".")[-1].replace("_", " ").title()
20
20
 
21
21
  __all__ = [
22
+ "DMSToRules",
22
23
  "ExcelToRules",
23
- "OntologyToRules",
24
24
  "IMFToRules",
25
- "DMSToRules",
25
+ "OntologyToRules",
26
26
  "RulesInferenceFromRdfFile",
27
27
  ]
28
28
 
@@ -14,11 +14,11 @@ CATEGORY = "IO Steps"
14
14
 
15
15
 
16
16
  __all__ = [
17
- "DownloadFileFromGitHub",
18
- "UploadFileToGitHub",
17
+ "DownloadDataFromRestApiToFile",
19
18
  "DownloadFileFromCDF",
19
+ "DownloadFileFromGitHub",
20
20
  "UploadFileToCDF",
21
- "DownloadDataFromRestApiToFile",
21
+ "UploadFileToGitHub",
22
22
  ]
23
23
 
24
24
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: cognite-neat
3
- Version: 0.102.0
3
+ Version: 0.103.0
4
4
  Summary: Knowledge graph transformation
5
5
  Home-page: https://cognite-neat.readthedocs-hosted.com/
6
6
  License: Apache-2.0