cognite-neat 0.112.1__py3-none-any.whl → 0.113.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.
@@ -202,6 +202,7 @@ class NeatSession:
202
202
  neat.infer()
203
203
  ```
204
204
  """
205
+ self._state._raise_exception_if_condition_not_met("Data model inference", instances_required=True)
205
206
  return self._infer_subclasses(model_id)
206
207
 
207
208
  def _previous_inference(
@@ -87,12 +87,6 @@ class CDFReadAPI(BaseReadAPI):
87
87
  super().__init__(state, verbose)
88
88
  self.classic = CDFClassicAPI(state, verbose)
89
89
 
90
- @property
91
- def _get_client(self) -> NeatClient:
92
- if self._state.client is None:
93
- raise NeatValueError("No client provided. Please provide a client to read a data model.")
94
- return self._state.client
95
-
96
90
  def data_model(self, data_model_id: DataModelIdentifier) -> IssueList:
97
91
  """Reads a Data Model from CDF to the knowledge graph.
98
92
 
@@ -111,7 +105,13 @@ class CDFReadAPI(BaseReadAPI):
111
105
  if not data_model_id.version:
112
106
  raise NeatSessionError("Data model version is required to read a data model.")
113
107
 
114
- importer = importers.DMSImporter.from_data_model_id(self._get_client, data_model_id)
108
+ self._state._raise_exception_if_condition_not_met(
109
+ "Read data model from CDF",
110
+ empty_rules_store_required=True,
111
+ client_required=True,
112
+ )
113
+
114
+ importer = importers.DMSImporter.from_data_model_id(cast(NeatClient, self._state.client), data_model_id)
115
115
  return self._state.rule_import(importer)
116
116
 
117
117
  def graph(
@@ -132,6 +132,12 @@ class CDFReadAPI(BaseReadAPI):
132
132
  IssueList: A list of issues that occurred during the extraction.
133
133
 
134
134
  """
135
+ self._state._raise_exception_if_condition_not_met(
136
+ "Read DMS Graph",
137
+ empty_rules_store_required=True,
138
+ empty_instances_store_required=True,
139
+ client_required=True,
140
+ )
135
141
  return self._graph(data_model_id, instance_space, skip_cognite_views, unpack_json=False)
136
142
 
137
143
  def _graph(
@@ -145,7 +151,7 @@ class CDFReadAPI(BaseReadAPI):
145
151
  extractor = extractors.DMSGraphExtractor.from_data_model_id(
146
152
  # We are skipping the Cognite Views
147
153
  data_model_id,
148
- self._get_client,
154
+ cast(NeatClient, self._state.client),
149
155
  instance_space=instance_space,
150
156
  skip_cognite_views=skip_cognite_views,
151
157
  unpack_json=unpack_json,
@@ -181,8 +187,13 @@ class CDFReadAPI(BaseReadAPI):
181
187
  ```
182
188
 
183
189
  """
190
+ self._state._raise_exception_if_condition_not_met(
191
+ "Read RAW",
192
+ client_required=True,
193
+ )
194
+
184
195
  extractor = extractors.RAWExtractor(
185
- self._get_client,
196
+ cast(NeatClient, self._state.client),
186
197
  db_name=db_name,
187
198
  table_name=table_name,
188
199
  table_type=type,
@@ -200,12 +211,6 @@ class CDFClassicAPI(BaseReadAPI):
200
211
 
201
212
  """
202
213
 
203
- @property
204
- def _get_client(self) -> NeatClient:
205
- if self._state.client is None:
206
- raise ValueError("No client provided. Please provide a client to read a data model.")
207
- return self._state.client
208
-
209
214
  def graph(
210
215
  self,
211
216
  root_asset_external_id: str,
@@ -257,6 +262,13 @@ class CDFClassicAPI(BaseReadAPI):
257
262
  neat.read.cdf.graph("root_asset_external_id")
258
263
  ```
259
264
  """
265
+ self._state._raise_exception_if_condition_not_met(
266
+ "Read classic graph",
267
+ empty_rules_store_required=True,
268
+ empty_instances_store_required=True,
269
+ client_required=True,
270
+ )
271
+
260
272
  return self._graph(
261
273
  root_asset_external_id, limit_per_type, identifier, reference_timeseries=False, reference_files=False
262
274
  )
@@ -273,7 +285,7 @@ class CDFClassicAPI(BaseReadAPI):
273
285
  ) -> IssueList:
274
286
  namespace = CLASSIC_CDF_NAMESPACE
275
287
  extractor = extractors.ClassicGraphExtractor(
276
- self._get_client,
288
+ cast(NeatClient, self._state.client),
277
289
  root_asset_external_id=root_asset_external_id,
278
290
  limit_per_type=limit_per_type,
279
291
  namespace=namespace,
@@ -467,6 +479,12 @@ class XMLReadAPI(BaseReadAPI):
467
479
  warnings.filterwarnings("default")
468
480
  AlphaFlags.dexpi_read.warn()
469
481
 
482
+ self._state._raise_exception_if_condition_not_met(
483
+ "Read DEXPI file",
484
+ empty_rules_store_required=True,
485
+ empty_instances_store_required=True,
486
+ )
487
+
470
488
  path = NeatReader.create(io).materialize_path()
471
489
  engine = import_engine()
472
490
  engine.set.format = "dexpi"
@@ -521,6 +539,12 @@ class XMLReadAPI(BaseReadAPI):
521
539
  warnings.filterwarnings("default")
522
540
  AlphaFlags.aml_read.warn()
523
541
 
542
+ self._state._raise_exception_if_condition_not_met(
543
+ "Read AML file",
544
+ empty_rules_store_required=True,
545
+ empty_instances_store_required=True,
546
+ )
547
+
524
548
  path = NeatReader.create(io).materialize_path()
525
549
  engine = import_engine()
526
550
  engine.set.format = "aml"
@@ -575,6 +599,11 @@ class RDFReadAPI(BaseReadAPI):
575
599
  warnings.filterwarnings("default")
576
600
  AlphaFlags.ontology_read.warn()
577
601
 
602
+ self._state._raise_exception_if_condition_not_met(
603
+ "Read Ontology file",
604
+ empty_rules_store_required=True,
605
+ )
606
+
578
607
  reader = NeatReader.create(io)
579
608
  importer = importers.OWLImporter.from_file(reader.materialize_path(), source_name=f"file {reader!s}")
580
609
  return self._state.rule_import(importer)
@@ -593,6 +622,11 @@ class RDFReadAPI(BaseReadAPI):
593
622
  warnings.filterwarnings("default")
594
623
  AlphaFlags.imf_read.warn()
595
624
 
625
+ self._state._raise_exception_if_condition_not_met(
626
+ "Read IMF file",
627
+ empty_rules_store_required=True,
628
+ )
629
+
596
630
  reader = NeatReader.create(io)
597
631
  importer = importers.IMFImporter.from_file(reader.materialize_path(), source_name=f"file {reader!s}")
598
632
  return self._state.rule_import(importer)
@@ -646,17 +680,35 @@ class Examples:
646
680
 
647
681
  def nordic44(self) -> IssueList:
648
682
  """Reads the Nordic 44 knowledge graph into the NeatSession graph store."""
683
+
684
+ self._state._raise_exception_if_condition_not_met(
685
+ "Read Nordic44 graph example",
686
+ empty_instances_store_required=True,
687
+ empty_rules_store_required=True,
688
+ )
689
+
649
690
  self._state.instances.store.write(extractors.RdfFileExtractor(instances_examples.nordic44_knowledge_graph))
650
691
  return IssueList()
651
692
 
652
693
  def pump_example(self) -> IssueList:
653
694
  """Reads the Hello World pump example into the NeatSession."""
695
+
696
+ self._state._raise_exception_if_condition_not_met(
697
+ "Read Pump Data Model example",
698
+ empty_rules_store_required=True,
699
+ )
700
+
654
701
  importer: importers.ExcelImporter = importers.ExcelImporter(catalog.hello_world_pump)
655
702
  return self._state.rule_import(importer)
656
703
 
657
704
  def core_data_model(self) -> IssueList:
658
705
  """Reads the core data model example into the NeatSession."""
659
706
 
707
+ self._state._raise_exception_if_condition_not_met(
708
+ "Read Core Data Model example",
709
+ empty_rules_store_required=True,
710
+ )
711
+
660
712
  cdm_v1 = DataModelId.load(("cdf_cdm", "CogniteCore", "v1"))
661
713
  importer: importers.DMSImporter = importers.DMSImporter.from_data_model_id(self._get_client, cdm_v1)
662
714
  return self._state.rule_import(importer)
@@ -65,6 +65,34 @@ class SessionState:
65
65
  issues.extend(extract_issues)
66
66
  return issues
67
67
 
68
+ def _raise_exception_if_condition_not_met(
69
+ self,
70
+ activity: str,
71
+ empty_rules_store_required: bool = False,
72
+ empty_instances_store_required: bool = False,
73
+ instances_required: bool = False,
74
+ client_required: bool = False,
75
+ ) -> None:
76
+ """Set conditions for raising an error in the session that are used by various methods in the session."""
77
+ condition = set()
78
+ suggestion = set()
79
+
80
+ if client_required and not self.client:
81
+ condition.add(f"{activity} expects a client in NEAT session")
82
+ suggestion.add("Please provide a client")
83
+ if empty_rules_store_required and not self.rule_store.empty:
84
+ condition.add(f"{activity} expects no data model in NEAT session")
85
+ suggestion.add("Start new session")
86
+ if empty_instances_store_required and not self.instances.empty:
87
+ condition.add(f"{activity} expects no instances in NEAT session")
88
+ suggestion.add("Start new session")
89
+ if instances_required and self.instances.empty:
90
+ condition.add(f"{activity} expects instances in NEAT session")
91
+ suggestion.add("Read in instances to neat session")
92
+
93
+ if condition:
94
+ raise NeatSessionError(". ".join(condition) + ". " + ". ".join(suggestion) + ". And try again.")
95
+
68
96
 
69
97
  class InstancesState:
70
98
  def __init__(
@@ -2,12 +2,13 @@ import warnings
2
2
  import zipfile
3
3
  from collections.abc import Collection
4
4
  from pathlib import Path
5
- from typing import Any, Literal, overload
5
+ from typing import Any, Literal, cast, overload
6
6
 
7
7
  from cognite.client import data_modeling as dm
8
8
  from cognite.client.data_classes.data_modeling import DataModelIdentifier
9
9
 
10
10
  from cognite.neat._alpha import AlphaFlags
11
+ from cognite.neat._client._api_client import NeatClient
11
12
  from cognite.neat._constants import COGNITE_MODELS
12
13
  from cognite.neat._graph import loaders
13
14
  from cognite.neat._issues import IssueList, NeatIssue, catch_issues
@@ -305,9 +306,12 @@ class CDFToAPI:
305
306
  space_from_property: str | None = None,
306
307
  use_source_space: bool = False,
307
308
  ) -> UploadResultList:
308
- if not self._state.client:
309
- raise NeatSessionError("No CDF client provided!")
310
- client = self._state.client
309
+ self._state._raise_exception_if_condition_not_met(
310
+ "Export DMS instances to CDF",
311
+ client_required=True,
312
+ )
313
+
314
+ client = cast(NeatClient, self._state.client)
311
315
  dms_rules = self._state.rule_store.last_verified_dms_rules
312
316
  instance_space = instance_space or f"{dms_rules.metadata.space}_instances"
313
317
 
@@ -366,12 +370,14 @@ class CDFToAPI:
366
370
 
367
371
  """
368
372
 
369
- exporter = exporters.DMSExporter(existing=existing, export_components=components, drop_data=drop_data)
373
+ self._state._raise_exception_if_condition_not_met(
374
+ "Export DMS data model to CDF",
375
+ client_required=True,
376
+ )
370
377
 
371
- if not self._state.client:
372
- raise NeatSessionError("No client provided!")
378
+ exporter = exporters.DMSExporter(existing=existing, export_components=components, drop_data=drop_data)
373
379
 
374
- result = self._state.rule_store.export_to_cdf(exporter, self._state.client, dry_run)
380
+ result = self._state.rule_store.export_to_cdf(exporter, cast(NeatClient, self._state.client), dry_run)
375
381
  print("You can inspect the details with the .inspect.outcome.data_model(...) method.")
376
382
  return result
377
383
 
cognite/neat/_version.py CHANGED
@@ -1,2 +1,2 @@
1
- __version__ = "0.112.1"
1
+ __version__ = "0.113.0"
2
2
  __engine__ = "^2.0.4"
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: cognite-neat
3
- Version: 0.112.1
3
+ Version: 0.113.0
4
4
  Summary: Knowledge graph transformation
5
5
  License: Apache-2.0
6
6
  Author: Nikola Vasiljevic
@@ -135,7 +135,7 @@ cognite/neat/_rules/transformers/_converters.py,sha256=-Tp7CTqNWVDENil-y5QVnh5EU
135
135
  cognite/neat/_rules/transformers/_mapping.py,sha256=lf-RKN__5Bg3-tZjEOCa1Sf_JtM_ScQ_TYcnciEnaYQ,18189
136
136
  cognite/neat/_rules/transformers/_verification.py,sha256=coZjoLqdfS8yrPP62T_xEKrDZc9ETbPWrLuwEVBSLZQ,4370
137
137
  cognite/neat/_session/__init__.py,sha256=fxQ5URVlUnmEGYyB8Baw7IDq-uYacqkigbc4b-Pr9Fw,58
138
- cognite/neat/_session/_base.py,sha256=qU3twqQrps6H9HQVlS6mUlnHcHcSYFyXWhn6d2N9nho,12050
138
+ cognite/neat/_session/_base.py,sha256=h-zJdzvDGz8aOFwgyMmzolX5MFp4qf1VrF8XwiI_dAg,12157
139
139
  cognite/neat/_session/_collector.py,sha256=RcOGY0DjTCCKJt9j_p0gnQXn4omhsIX2G8Aq3ZFHIt4,4218
140
140
  cognite/neat/_session/_create.py,sha256=doDCbDIWMbHCYe3cyk1obQaFdYJjvARg3X4lRUVicCk,7214
141
141
  cognite/neat/_session/_drop.py,sha256=gOkDAnddASpFxYxkPjlTyhkpNfnmDEj94GRI8tnHFR0,4167
@@ -144,12 +144,12 @@ cognite/neat/_session/_fix.py,sha256=wYXIIHKmWTNmOLr9RvDSkBJllKoomP2mCnMdB9x2ojw
144
144
  cognite/neat/_session/_inspect.py,sha256=qoBAfCQnzC40ef91gxJmhonWo1Kr_VEjBb2KhbCOO_s,10084
145
145
  cognite/neat/_session/_mapping.py,sha256=AkQwmqYH-0EgqoXHqCFwJY92hNSGzfojOelhVFlqH4c,2655
146
146
  cognite/neat/_session/_prepare.py,sha256=BZ1NurenrsZDU4tg629wnt1Iuw_zVLRA58FNTQMYS9I,12636
147
- cognite/neat/_session/_read.py,sha256=kULWbdP9T5jR9bW8S09Ix0aGRxN6PtcMrDy_IFJTycM,24928
147
+ cognite/neat/_session/_read.py,sha256=PMgWcnIEi88pwrMK6jIIotNNKD6pd9gaFs9OjJOV_qU,26534
148
148
  cognite/neat/_session/_set.py,sha256=dCZ5zEmNAw8tiqOGT7-EigSXOIGlfVP2ldA7nmC8LJ8,4451
149
149
  cognite/neat/_session/_show.py,sha256=2lnkud996ouwf6-aKGvU0cU0ttfMeQ3vcb__g_7Yko4,10539
150
- cognite/neat/_session/_state.py,sha256=CPyjYbgUe6uUnCG6h-UqDtdIaWKVLjY4lAz4ar2_75A,4222
150
+ cognite/neat/_session/_state.py,sha256=fwFICVL5RxXU7f7DjvTP9yfQel2UobgavNJRyVxK8bY,5572
151
151
  cognite/neat/_session/_subset.py,sha256=vKtBiEnOruqe_749Nd8vzRS5HIZMR-sXSxyEH9Fa6Gk,2673
152
- cognite/neat/_session/_to.py,sha256=3bZGaXAXFvgzmNPmOtjzLiJLSlRXXaD6vYp4Cotx9Ks,18692
152
+ cognite/neat/_session/_to.py,sha256=urHgHZVt2Fg7983bZItgcNHr5f-OeKM3JwWRn6lm45I,18894
153
153
  cognite/neat/_session/_wizard.py,sha256=9idlzhZy54h2Iwupe9iXKX3RDb5jJQuBZFEouni50L0,1476
154
154
  cognite/neat/_session/engine/__init__.py,sha256=D3MxUorEs6-NtgoICqtZ8PISQrjrr4dvca6n48bu_bI,120
155
155
  cognite/neat/_session/engine/_import.py,sha256=1QxA2_EK613lXYAHKQbZyw2yjo5P9XuiX4Z6_6-WMNQ,169
@@ -176,10 +176,10 @@ cognite/neat/_utils/text.py,sha256=BFJoEOQBFgpelysL92FdF0OVRVFl0q9tRNoz-oRanNc,7
176
176
  cognite/neat/_utils/time_.py,sha256=O30LUiDH9TdOYz8_a9pFqTtJdg8vEjC3qHCk8xZblG8,345
177
177
  cognite/neat/_utils/upload.py,sha256=xWtM6mFuD2QYQHaZ7zCAuGptbEpPIxcH-raWQu93-Ug,5845
178
178
  cognite/neat/_utils/xml_.py,sha256=FQkq84u35MUsnKcL6nTMJ9ajtG9D5i1u4VBnhGqP2DQ,1710
179
- cognite/neat/_version.py,sha256=dV0WP4PUBNhM4CxpxuS75yhwtgI4lO4c1ylB509VGUw,46
179
+ cognite/neat/_version.py,sha256=erluRLA_eMSZKY7XA0ZIiY0c9aX_Kg6zsW0zhUCZUWw,46
180
180
  cognite/neat/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
181
- cognite_neat-0.112.1.dist-info/LICENSE,sha256=W8VmvFia4WHa3Gqxq1Ygrq85McUNqIGDVgtdvzT-XqA,11351
182
- cognite_neat-0.112.1.dist-info/METADATA,sha256=nDs3X4uChc1zyEmkO9dC39PkEcMnDwfnF_b6jbfjWJk,5361
183
- cognite_neat-0.112.1.dist-info/WHEEL,sha256=XbeZDeTWKc1w7CSIyre5aMDU_-PohRwTQceYnisIYYY,88
184
- cognite_neat-0.112.1.dist-info/entry_points.txt,sha256=SsQlnl8SNMSSjE3acBI835JYFtsIinLSbVmHmMEXv6E,51
185
- cognite_neat-0.112.1.dist-info/RECORD,,
181
+ cognite_neat-0.113.0.dist-info/LICENSE,sha256=W8VmvFia4WHa3Gqxq1Ygrq85McUNqIGDVgtdvzT-XqA,11351
182
+ cognite_neat-0.113.0.dist-info/METADATA,sha256=hpnC1K_GVRF0qMZD3O8xwKHQH9qQZCFj-9n3ZrhDHf0,5361
183
+ cognite_neat-0.113.0.dist-info/WHEEL,sha256=XbeZDeTWKc1w7CSIyre5aMDU_-PohRwTQceYnisIYYY,88
184
+ cognite_neat-0.113.0.dist-info/entry_points.txt,sha256=SsQlnl8SNMSSjE3acBI835JYFtsIinLSbVmHmMEXv6E,51
185
+ cognite_neat-0.113.0.dist-info/RECORD,,