cognite-neat 0.86.0__py3-none-any.whl → 0.87.3__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 (108) hide show
  1. cognite/neat/_version.py +1 -1
  2. cognite/neat/app/api/configuration.py +1 -10
  3. cognite/neat/app/api/routers/data_exploration.py +1 -1
  4. cognite/neat/config.py +84 -17
  5. cognite/neat/constants.py +11 -9
  6. cognite/neat/graph/extractors/_classic_cdf/_assets.py +1 -1
  7. cognite/neat/graph/extractors/_classic_cdf/_events.py +1 -1
  8. cognite/neat/graph/extractors/_classic_cdf/_files.py +1 -1
  9. cognite/neat/graph/extractors/_classic_cdf/_labels.py +1 -1
  10. cognite/neat/graph/extractors/_classic_cdf/_relationships.py +1 -1
  11. cognite/neat/graph/extractors/_classic_cdf/_sequences.py +1 -1
  12. cognite/neat/graph/extractors/_classic_cdf/_timeseries.py +1 -1
  13. cognite/neat/graph/extractors/_dexpi.py +1 -1
  14. cognite/neat/graph/extractors/_mock_graph_generator.py +8 -9
  15. cognite/neat/graph/loaders/__init__.py +5 -2
  16. cognite/neat/graph/loaders/_base.py +13 -5
  17. cognite/neat/graph/loaders/_rdf2asset.py +185 -55
  18. cognite/neat/graph/loaders/_rdf2dms.py +7 -7
  19. cognite/neat/graph/queries/_base.py +20 -11
  20. cognite/neat/graph/queries/_construct.py +5 -5
  21. cognite/neat/graph/queries/_shared.py +21 -7
  22. cognite/neat/graph/stores/_base.py +16 -4
  23. cognite/neat/graph/transformers/__init__.py +3 -0
  24. cognite/neat/graph/transformers/_rdfpath.py +42 -0
  25. cognite/neat/legacy/graph/extractors/_dexpi.py +0 -5
  26. cognite/neat/legacy/graph/extractors/_mock_graph_generator.py +1 -1
  27. cognite/neat/legacy/graph/loaders/_asset_loader.py +2 -2
  28. cognite/neat/legacy/graph/loaders/core/rdf_to_assets.py +5 -2
  29. cognite/neat/legacy/graph/loaders/core/rdf_to_relationships.py +4 -1
  30. cognite/neat/legacy/graph/loaders/rdf_to_dms.py +3 -1
  31. cognite/neat/legacy/graph/stores/_base.py +24 -8
  32. cognite/neat/legacy/graph/stores/_graphdb_store.py +3 -2
  33. cognite/neat/legacy/graph/stores/_memory_store.py +3 -3
  34. cognite/neat/legacy/graph/stores/_oxigraph_store.py +8 -4
  35. cognite/neat/legacy/graph/stores/_rdf_to_graph.py +5 -3
  36. cognite/neat/legacy/graph/transformations/query_generator/sparql.py +49 -16
  37. cognite/neat/legacy/graph/transformations/transformer.py +1 -1
  38. cognite/neat/legacy/rules/exporters/_rules2dms.py +8 -3
  39. cognite/neat/legacy/rules/exporters/_rules2graphql.py +1 -1
  40. cognite/neat/legacy/rules/exporters/_rules2ontology.py +2 -1
  41. cognite/neat/legacy/rules/exporters/_rules2pydantic_models.py +3 -4
  42. cognite/neat/legacy/rules/importers/_dms2rules.py +4 -1
  43. cognite/neat/legacy/rules/importers/_graph2rules.py +3 -3
  44. cognite/neat/legacy/rules/importers/_owl2rules/_owl2classes.py +1 -1
  45. cognite/neat/legacy/rules/importers/_owl2rules/_owl2metadata.py +2 -1
  46. cognite/neat/legacy/rules/importers/_owl2rules/_owl2properties.py +1 -1
  47. cognite/neat/legacy/rules/models/raw_rules.py +19 -7
  48. cognite/neat/legacy/rules/models/rules.py +32 -12
  49. cognite/neat/rules/_shared.py +6 -1
  50. cognite/neat/rules/analysis/__init__.py +4 -4
  51. cognite/neat/rules/analysis/_asset.py +143 -0
  52. cognite/neat/rules/analysis/_base.py +385 -6
  53. cognite/neat/rules/analysis/_information.py +183 -0
  54. cognite/neat/rules/exporters/_rules2dms.py +1 -1
  55. cognite/neat/rules/exporters/_rules2ontology.py +6 -5
  56. cognite/neat/rules/importers/_dms2rules.py +3 -1
  57. cognite/neat/rules/importers/_dtdl2rules/dtdl_converter.py +2 -8
  58. cognite/neat/rules/importers/_inference2rules.py +3 -7
  59. cognite/neat/rules/importers/_owl2rules/_owl2classes.py +1 -1
  60. cognite/neat/rules/importers/_owl2rules/_owl2metadata.py +2 -1
  61. cognite/neat/rules/importers/_owl2rules/_owl2properties.py +1 -1
  62. cognite/neat/rules/issues/spreadsheet.py +35 -0
  63. cognite/neat/rules/models/_base.py +7 -7
  64. cognite/neat/rules/models/_rdfpath.py +17 -21
  65. cognite/neat/rules/models/asset/_rules.py +4 -5
  66. cognite/neat/rules/models/asset/_validation.py +38 -1
  67. cognite/neat/rules/models/dms/_converter.py +1 -2
  68. cognite/neat/rules/models/dms/_exporter.py +7 -3
  69. cognite/neat/rules/models/dms/_rules.py +3 -0
  70. cognite/neat/rules/models/dms/_schema.py +5 -4
  71. cognite/neat/rules/models/domain.py +5 -2
  72. cognite/neat/rules/models/entities.py +28 -17
  73. cognite/neat/rules/models/information/_rules.py +10 -8
  74. cognite/neat/rules/models/information/_rules_input.py +1 -2
  75. cognite/neat/rules/models/information/_validation.py +2 -2
  76. cognite/neat/utils/__init__.py +0 -3
  77. cognite/neat/utils/auth.py +47 -28
  78. cognite/neat/utils/auxiliary.py +141 -1
  79. cognite/neat/utils/cdf/__init__.py +0 -0
  80. cognite/neat/utils/{cdf_classes.py → cdf/data_classes.py} +122 -2
  81. cognite/neat/utils/{cdf_loaders → cdf/loaders}/_data_modeling.py +37 -0
  82. cognite/neat/utils/{cdf_loaders → cdf/loaders}/_ingestion.py +2 -1
  83. cognite/neat/utils/collection_.py +18 -0
  84. cognite/neat/utils/rdf_.py +165 -0
  85. cognite/neat/utils/text.py +4 -0
  86. cognite/neat/utils/time_.py +17 -0
  87. cognite/neat/utils/upload.py +13 -1
  88. cognite/neat/workflows/_exceptions.py +5 -5
  89. cognite/neat/workflows/base.py +1 -1
  90. cognite/neat/workflows/steps/lib/current/graph_store.py +28 -8
  91. cognite/neat/workflows/steps/lib/current/rules_validator.py +2 -2
  92. cognite/neat/workflows/steps/lib/legacy/graph_extractor.py +130 -28
  93. cognite/neat/workflows/steps/lib/legacy/graph_loader.py +1 -1
  94. cognite/neat/workflows/steps/lib/legacy/graph_store.py +4 -4
  95. cognite/neat/workflows/steps/lib/legacy/rules_exporter.py +1 -1
  96. cognite/neat/workflows/steps/lib/legacy/rules_importer.py +1 -1
  97. {cognite_neat-0.86.0.dist-info → cognite_neat-0.87.3.dist-info}/METADATA +2 -2
  98. {cognite_neat-0.86.0.dist-info → cognite_neat-0.87.3.dist-info}/RECORD +103 -102
  99. cognite/neat/rules/analysis/_information_rules.py +0 -476
  100. cognite/neat/utils/cdf.py +0 -59
  101. cognite/neat/utils/cdf_loaders/data_classes.py +0 -121
  102. cognite/neat/utils/exceptions.py +0 -41
  103. cognite/neat/utils/utils.py +0 -429
  104. /cognite/neat/utils/{cdf_loaders → cdf/loaders}/__init__.py +0 -0
  105. /cognite/neat/utils/{cdf_loaders → cdf/loaders}/_base.py +0 -0
  106. {cognite_neat-0.86.0.dist-info → cognite_neat-0.87.3.dist-info}/LICENSE +0 -0
  107. {cognite_neat-0.86.0.dist-info → cognite_neat-0.87.3.dist-info}/WHEEL +0 -0
  108. {cognite_neat-0.86.0.dist-info → cognite_neat-0.87.3.dist-info}/entry_points.txt +0 -0
@@ -1,476 +0,0 @@
1
- import itertools
2
- import logging
3
- import warnings
4
- from collections import defaultdict
5
- from typing import Any, Generic, TypeVar
6
-
7
- import pandas as pd
8
- from pydantic import ValidationError
9
-
10
- from cognite.neat.rules.models import SchemaCompleteness
11
- from cognite.neat.rules.models._rdfpath import Hop, RDFPath
12
- from cognite.neat.rules.models.asset import AssetClass, AssetProperty, AssetRules
13
- from cognite.neat.rules.models.entities import (
14
- AssetEntity,
15
- ClassEntity,
16
- EntityTypes,
17
- ParentClassEntity,
18
- ReferenceEntity,
19
- RelationshipEntity,
20
- )
21
- from cognite.neat.rules.models.information import (
22
- InformationClass,
23
- InformationProperty,
24
- InformationRules,
25
- )
26
- from cognite.neat.utils.utils import get_inheritance_path
27
-
28
- T_Rules = TypeVar("T_Rules", InformationRules, AssetRules)
29
- T_Property = TypeVar("T_Property", InformationProperty, AssetProperty)
30
- T_Class = TypeVar("T_Class", InformationClass, AssetClass)
31
-
32
-
33
- class _SharedAnalysis(Generic[T_Rules, T_Property, T_Class]):
34
- def __init__(self, rules: T_Rules):
35
- self.rules: T_Rules = rules
36
-
37
- @property
38
- def directly_referred_classes(self) -> set[ClassEntity]:
39
- return {
40
- class_.reference.as_class_entity()
41
- for class_ in self.rules.classes
42
- if self.rules.reference
43
- and class_.reference
44
- and isinstance(class_.reference, ReferenceEntity)
45
- and class_.reference.prefix == self.rules.reference.metadata.prefix
46
- }
47
-
48
- @property
49
- def inherited_referred_classes(self) -> set[ClassEntity]:
50
- dir_referred_classes = self.directly_referred_classes
51
- inherited_referred_classes = []
52
- for class_ in dir_referred_classes:
53
- inherited_referred_classes.extend(self.class_inheritance_path(class_))
54
- return set(inherited_referred_classes)
55
-
56
- def class_parent_pairs(self) -> dict[ClassEntity, list[ParentClassEntity]]:
57
- """This only returns class - parent pairs only if parent is in the same data model"""
58
- class_subclass_pairs: dict[ClassEntity, list[ParentClassEntity]] = {}
59
-
60
- if not self.rules:
61
- return class_subclass_pairs
62
-
63
- for definition in self.rules.classes:
64
- class_subclass_pairs[definition.class_] = []
65
-
66
- if definition.parent is None:
67
- continue
68
-
69
- for parent in definition.parent:
70
- if parent.prefix == definition.class_.prefix:
71
- class_subclass_pairs[definition.class_].append(parent)
72
- else:
73
- warnings.warn(
74
- f"Parent class {parent} of class {definition} is not in the same namespace, skipping !",
75
- stacklevel=2,
76
- )
77
-
78
- return class_subclass_pairs
79
-
80
- def classes_with_properties(self, consider_inheritance: bool = False) -> dict[ClassEntity, list[T_Property]]:
81
- """Returns classes that have been defined in the data model.
82
-
83
- Args:
84
- consider_inheritance: Whether to consider inheritance or not. Defaults False
85
-
86
- Returns:
87
- Dictionary of classes with a list of properties defined for them
88
-
89
- !!! note "consider_inheritance"
90
- If consider_inheritance is True, properties from parent classes will also be considered.
91
- This means if a class has a parent class, and the parent class has properties defined for it,
92
- while we do not have any properties defined for the child class, we will still consider the
93
- properties from the parent class. If consider_inheritance is False, we will only consider
94
- properties defined for the child class, thus if no properties are defined for the child class,
95
- it will not be included in the returned dictionary.
96
- """
97
-
98
- class_property_pairs: dict[ClassEntity, list[T_Property]] = defaultdict(list)
99
-
100
- for property_ in self.rules.properties:
101
- class_property_pairs[property_.class_].append(property_) # type: ignore
102
-
103
- if consider_inheritance:
104
- class_parent_pairs = self.class_parent_pairs()
105
- for class_ in class_parent_pairs:
106
- self._add_inherited_properties(class_, class_property_pairs, class_parent_pairs)
107
-
108
- return class_property_pairs
109
-
110
- def class_inheritance_path(self, class_: ClassEntity | str) -> list[ClassEntity]:
111
- class_ = class_ if isinstance(class_, ClassEntity) else ClassEntity.load(class_)
112
- class_parent_pairs = self.class_parent_pairs()
113
- return get_inheritance_path(class_, class_parent_pairs)
114
-
115
- @classmethod
116
- def _add_inherited_properties(
117
- cls,
118
- class_: ClassEntity,
119
- class_property_pairs: dict[ClassEntity, list[T_Property]],
120
- class_parent_pairs: dict[ClassEntity, list[ParentClassEntity]],
121
- ):
122
- inheritance_path = get_inheritance_path(class_, class_parent_pairs)
123
- for parent in inheritance_path:
124
- # ParentClassEntity -> ClassEntity to match the type of class_property_pairs
125
- if parent.as_class_entity() in class_property_pairs:
126
- for property_ in class_property_pairs[parent.as_class_entity()]:
127
- property_ = property_.model_copy()
128
-
129
- # This corresponds to importing properties from parent class
130
- # making sure that the property is attached to desired child class
131
- property_.class_ = class_
132
- property_.inherited = True
133
-
134
- # need same if we have RDF path to make sure that the starting class is the
135
-
136
- if class_ in class_property_pairs:
137
- class_property_pairs[class_].append(property_)
138
- else:
139
- class_property_pairs[class_] = [property_]
140
-
141
- def class_property_pairs(
142
- self, only_rdfpath: bool = False, consider_inheritance: bool = False
143
- ) -> dict[ClassEntity, dict[str, T_Property]]:
144
- """Returns a dictionary of classes with a dictionary of properties associated with them.
145
-
146
- Args:
147
- only_rdfpath : To consider only properties which have rule `rdfpath` set. Defaults False
148
- consider_inheritance: Whether to consider inheritance or not. Defaults False
149
-
150
- Returns:
151
- Dictionary of classes with a dictionary of properties associated with them.
152
-
153
- !!! note "difference to get_classes_with_properties"
154
- This method returns a dictionary of classes with a dictionary of properties associated with them.
155
- While get_classes_with_properties returns a dictionary of classes with a list of
156
- properties defined for them,
157
- here we filter the properties based on the `only_rdfpath` parameter and only consider
158
- the first definition of a property if it is defined more than once.
159
-
160
- !!! note "only_rdfpath"
161
- If only_rdfpath is True, only properties with RuleType.rdfpath will be returned as
162
- a part of the dictionary of properties related to a class. Otherwise, all properties
163
- will be returned.
164
-
165
- !!! note "consider_inheritance"
166
- If consider_inheritance is True, properties from parent classes will also be considered.
167
- This means if a class has a parent class, and the parent class has properties defined for it,
168
- while we do not have any properties defined for the child class, we will still consider the
169
- properties from the parent class. If consider_inheritance is False, we will only consider
170
- properties defined for the child class, thus if no properties are defined for the child class,
171
- it will not be included in the returned dictionary.
172
- """
173
- # TODO: https://cognitedata.atlassian.net/jira/software/projects/NEAT/boards/893?selectedIssue=NEAT-78
174
-
175
- class_property_pairs = {}
176
-
177
- for class_, properties in self.classes_with_properties(consider_inheritance).items():
178
- processed_properties = {}
179
- for property_ in properties:
180
- if property_.property_ in processed_properties:
181
- # TODO: use appropriate Warning class from _exceptions.py
182
- # if missing make one !
183
- warnings.warn(
184
- f"Property {property_.property_} for {class_} has been defined more than once!"
185
- " Only the first definition will be considered, skipping the rest..",
186
- stacklevel=2,
187
- )
188
- continue
189
-
190
- if (only_rdfpath and isinstance(property_.transformation, RDFPath)) or not only_rdfpath:
191
- processed_properties[property_.property_] = property_
192
- class_property_pairs[class_] = processed_properties
193
-
194
- return class_property_pairs
195
-
196
- def class_linkage(self, consider_inheritance: bool = False) -> pd.DataFrame:
197
- """Returns a dataframe with the class linkage of the data model.
198
-
199
- Args:
200
- consider_inheritance: Whether to consider inheritance or not. Defaults False
201
-
202
- Returns:
203
- Dataframe with the class linkage of the data model
204
- """
205
-
206
- class_linkage = pd.DataFrame(
207
- columns=[
208
- "source_class",
209
- "target_class",
210
- "connecting_property",
211
- "max_occurrence",
212
- ]
213
- )
214
-
215
- class_property_pairs = self.classes_with_properties(consider_inheritance)
216
- properties = list(itertools.chain.from_iterable(class_property_pairs.values()))
217
-
218
- for property_ in properties:
219
- if property_.type_ == EntityTypes.object_property:
220
- new_row = pd.Series(
221
- {
222
- "source_class": property_.class_,
223
- "connecting_property": property_.property_,
224
- "target_class": property_.value_type,
225
- "max_occurrence": property_.max_count,
226
- }
227
- )
228
- class_linkage = pd.concat([class_linkage, new_row.to_frame().T], ignore_index=True)
229
-
230
- class_linkage.drop_duplicates(inplace=True)
231
- class_linkage = class_linkage[["source_class", "connecting_property", "target_class", "max_occurrence"]]
232
-
233
- return class_linkage
234
-
235
- def connected_classes(self, consider_inheritance: bool = False) -> set[ClassEntity]:
236
- """Return a set of classes that are connected to other classes.
237
-
238
- Args:
239
- consider_inheritance: Whether to consider inheritance or not. Defaults False
240
-
241
- Returns:
242
- Set of classes that are connected to other classes
243
- """
244
- class_linkage = self.class_linkage(consider_inheritance)
245
- return set(class_linkage.source_class.values).union(set(class_linkage.target_class.values))
246
-
247
- def defined_classes(self, consider_inheritance: bool = False) -> set[ClassEntity]:
248
- """Returns classes that have properties defined for them in the data model.
249
-
250
- Args:
251
- consider_inheritance: Whether to consider inheritance or not. Defaults False
252
-
253
- Returns:
254
- Set of classes that have been defined in the data model
255
- """
256
- class_property_pairs = self.classes_with_properties(consider_inheritance)
257
- properties = list(itertools.chain.from_iterable(class_property_pairs.values()))
258
-
259
- return {property.class_ for property in properties}
260
-
261
- def disconnected_classes(self, consider_inheritance: bool = False) -> set[ClassEntity]:
262
- """Return a set of classes that are disconnected (i.e. isolated) from other classes.
263
-
264
- Args:
265
- consider_inheritance: Whether to consider inheritance or not. Defaults False
266
-
267
- Returns:
268
- Set of classes that are disconnected from other classes
269
- """
270
- return self.defined_classes(consider_inheritance) - self.connected_classes(consider_inheritance)
271
-
272
- def symmetrically_connected_classes(
273
- self, consider_inheritance: bool = False
274
- ) -> set[tuple[ClassEntity, ClassEntity]]:
275
- """Returns a set of pairs of symmetrically linked classes.
276
-
277
- Args:
278
- consider_inheritance: Whether to consider inheritance or not. Defaults False
279
-
280
- Returns:
281
- Set of pairs of symmetrically linked classes
282
-
283
- !!! note "Symmetrically Connected Classes"
284
- Symmetrically connected classes are classes that are connected to each other
285
- in both directions. For example, if class A is connected to class B, and class B
286
- is connected to class A, then classes A and B are symmetrically connected.
287
- """
288
-
289
- # TODO: Find better name for this method
290
- sym_pairs: set[tuple[ClassEntity, ClassEntity]] = set()
291
-
292
- class_linkage = self.class_linkage(consider_inheritance)
293
- if class_linkage.empty:
294
- return sym_pairs
295
-
296
- for _, row in class_linkage.iterrows():
297
- source = row.source_class
298
- target = row.target_class
299
- target_targets = class_linkage[class_linkage.source_class == target].target_class.values
300
- if source in target_targets and (source, target) not in sym_pairs:
301
- sym_pairs.add((source, target))
302
- return sym_pairs
303
-
304
- def as_property_dict(
305
- self,
306
- ) -> dict[str, list[T_Property]]:
307
- """This is used to capture all definitions of a property in the data model."""
308
- property_dict: dict[str, list[T_Property]] = defaultdict(list)
309
- for definition in self.rules.properties:
310
- property_dict[definition.property_].append(definition) # type: ignore
311
- return property_dict
312
-
313
- def as_class_dict(self) -> dict[str, T_Class]:
314
- """This is to simplify access to classes through dict."""
315
- class_dict: dict[str, T_Class] = {}
316
- for definition in self.rules.classes:
317
- class_dict[str(definition.class_.suffix)] = definition # type: ignore
318
- return class_dict
319
-
320
- def subset_rules(self, desired_classes: set[ClassEntity]) -> T_Rules:
321
- """
322
- Subset rules to only include desired classes and their properties.
323
-
324
- Args:
325
- desired_classes: Desired classes to include in the reduced data model
326
-
327
- Returns:
328
- Instance of InformationRules
329
-
330
- !!! note "Inheritance"
331
- If desired classes contain a class that is a subclass of another class(es), the parent class(es)
332
- will be included in the reduced data model as well even though the parent class(es) are
333
- not in the desired classes set. This is to ensure that the reduced data model is
334
- consistent and complete.
335
-
336
- !!! note "Partial Reduction"
337
- This method does not perform checks if classes that are value types of desired classes
338
- properties are part of desired classes. If a class is not part of desired classes, but it
339
- is a value type of a property of a class that is part of desired classes, derived reduced
340
- rules will be marked as partial.
341
-
342
- !!! note "Validation"
343
- This method will attempt to validate the reduced rules with custom validations.
344
- If it fails, it will return a partial rules with a warning message, validated
345
- only with base Pydantic validators.
346
- """
347
-
348
- if self.rules.metadata.schema_ is not SchemaCompleteness.complete:
349
- raise ValueError("Rules are not complete cannot perform reduction!")
350
- class_as_dict = self.as_class_dict()
351
- class_parents_pairs = self.class_parent_pairs()
352
- defined_classes = self.defined_classes(consider_inheritance=True)
353
-
354
- possible_classes = defined_classes.intersection(desired_classes)
355
- impossible_classes = desired_classes - possible_classes
356
-
357
- # need to add all the parent classes of the desired classes to the possible classes
358
- parents: set[ClassEntity] = set()
359
- for class_ in possible_classes:
360
- parents = parents.union(
361
- {parent.as_class_entity() for parent in get_inheritance_path(class_, class_parents_pairs)}
362
- )
363
- possible_classes = possible_classes.union(parents)
364
-
365
- if not possible_classes:
366
- logging.error("None of the desired classes are defined in the data model!")
367
- raise ValueError("None of the desired classes are defined in the data model!")
368
-
369
- if impossible_classes:
370
- logging.warning(f"Could not find the following classes defined in the data model: {impossible_classes}")
371
- warnings.warn(
372
- f"Could not find the following classes defined in the data model: {impossible_classes}",
373
- stacklevel=2,
374
- )
375
-
376
- reduced_data_model: dict[str, Any] = {
377
- "metadata": self.rules.metadata.model_copy(),
378
- "prefixes": (self.rules.prefixes or {}).copy(),
379
- "classes": [],
380
- "properties": [],
381
- }
382
-
383
- logging.info(f"Reducing data model to only include the following classes: {possible_classes}")
384
- for class_ in possible_classes:
385
- reduced_data_model["classes"].append(class_as_dict[str(class_.suffix)])
386
-
387
- class_property_pairs = self.classes_with_properties(consider_inheritance=False)
388
-
389
- for class_, properties in class_property_pairs.items():
390
- if class_ in possible_classes:
391
- reduced_data_model["properties"].extend(properties)
392
-
393
- try:
394
- return type(self.rules)(**reduced_data_model)
395
- except ValidationError as e:
396
- warnings.warn(f"Reduced data model is not complete: {e}", stacklevel=2)
397
- reduced_data_model["metadata"].schema_ = SchemaCompleteness.partial
398
- return type(self.rules).model_construct(**reduced_data_model)
399
-
400
-
401
- class InformationArchitectRulesAnalysis(_SharedAnalysis[InformationRules, InformationProperty, InformationClass]):
402
- """Assumes analysis over only the complete schema"""
403
-
404
- def has_hop_transformations(self):
405
- return any(
406
- prop_.transformation and isinstance(prop_.transformation.traversal, Hop) for prop_ in self.rules.properties
407
- )
408
-
409
- def define_property_renaming_config(self) -> dict[str, str]:
410
- # placeholder comes in new PR
411
- return {}
412
-
413
-
414
- class AssetArchitectRulesAnalysis(_SharedAnalysis[AssetRules, AssetProperty, AssetClass]):
415
- """Assumes analysis over only the complete schema"""
416
-
417
- def class_property_pairs(
418
- self,
419
- only_rdfpath: bool = False,
420
- consider_inheritance: bool = False,
421
- implementation_type: EntityTypes = EntityTypes.asset,
422
- ) -> dict[ClassEntity, dict[str, AssetProperty]]:
423
- class_property_pairs = {}
424
-
425
- T_implementation = AssetEntity if implementation_type == EntityTypes.asset else RelationshipEntity
426
-
427
- for class_, properties in self.classes_with_properties(consider_inheritance).items():
428
- processed_properties = {}
429
- for property_ in properties:
430
- if property_.property_ in processed_properties:
431
- # TODO: use appropriate Warning class from _exceptions.py
432
- # if missing make one !
433
- warnings.warn(
434
- f"Property {property_.property_} for {class_} has been defined more than once!"
435
- " Only the first definition will be considered, skipping the rest..",
436
- stacklevel=2,
437
- )
438
- continue
439
-
440
- if (
441
- property_.implementation
442
- and any(isinstance(implementation, T_implementation) for implementation in property_.implementation)
443
- and (not only_rdfpath or (only_rdfpath and isinstance(property_.transformation, RDFPath)))
444
- ):
445
- implementation = [
446
- implementation
447
- for implementation in property_.implementation
448
- if isinstance(implementation, T_implementation)
449
- ]
450
-
451
- processed_properties[property_.property_] = property_.model_copy(
452
- deep=True, update={"implementation": implementation}
453
- )
454
-
455
- if processed_properties:
456
- class_property_pairs[class_] = processed_properties
457
-
458
- return class_property_pairs
459
-
460
- def asset_definition(
461
- self, only_rdfpath: bool = False, consider_inheritance: bool = False
462
- ) -> dict[ClassEntity, dict[str, AssetProperty]]:
463
- return self.class_property_pairs(
464
- consider_inheritance=consider_inheritance,
465
- only_rdfpath=only_rdfpath,
466
- implementation_type=EntityTypes.asset,
467
- )
468
-
469
- def relationship_definition(
470
- self, only_rdfpath: bool = False, consider_inheritance: bool = False
471
- ) -> dict[ClassEntity, dict[str, AssetProperty]]:
472
- return self.class_property_pairs(
473
- consider_inheritance=consider_inheritance,
474
- only_rdfpath=only_rdfpath,
475
- implementation_type=EntityTypes.relationship,
476
- )
cognite/neat/utils/cdf.py DELETED
@@ -1,59 +0,0 @@
1
- from cognite.client import CogniteClient
2
- from cognite.client.data_classes import filters
3
- from pydantic import BaseModel, field_validator
4
-
5
-
6
- class CogniteClientConfig(BaseModel):
7
- project: str = "dev"
8
- client_id: str = "neat"
9
- base_url: str = "https://api.cognitedata.com"
10
- scopes: list[str] = ["project:read", "project:write"]
11
- timeout: int = 60
12
- max_workers: int = 3
13
-
14
- @field_validator("scopes", mode="before")
15
- def string_to_list(cls, value):
16
- return [value] if isinstance(value, str) else value
17
-
18
-
19
- class InteractiveCogniteClient(CogniteClientConfig):
20
- authority_url: str
21
- redirect_port: int = 53_000
22
-
23
-
24
- class ServiceCogniteClient(CogniteClientConfig):
25
- token_url: str = "https://login.microsoftonline.com/common/oauth2/token"
26
- client_secret: str = "secret"
27
-
28
-
29
- def clean_space(client: CogniteClient, space: str) -> None:
30
- """Deletes all data in a space.
31
-
32
- This means all nodes, edges, views, containers, and data models located in the given space.
33
-
34
- Args:
35
- client: Connected CogniteClient
36
- space: The space to delete.
37
-
38
- """
39
- edges = client.data_modeling.instances.list("edge", limit=-1, filter=filters.Equals(["edge", "space"], space))
40
- if edges:
41
- instances = client.data_modeling.instances.delete(edges=edges.as_ids())
42
- print(f"Deleted {len(instances.edges)} edges")
43
- nodes = client.data_modeling.instances.list("node", limit=-1, filter=filters.Equals(["node", "space"], space))
44
- if nodes:
45
- instances = client.data_modeling.instances.delete(nodes=nodes.as_ids())
46
- print(f"Deleted {len(instances.nodes)} nodes")
47
- views = client.data_modeling.views.list(limit=-1, space=space)
48
- if views:
49
- deleted_views = client.data_modeling.views.delete(views.as_ids())
50
- print(f"Deleted {len(deleted_views)} views")
51
- containers = client.data_modeling.containers.list(limit=-1, space=space)
52
- if containers:
53
- deleted_containers = client.data_modeling.containers.delete(containers.as_ids())
54
- print(f"Deleted {len(deleted_containers)} containers")
55
- if data_models := client.data_modeling.data_models.list(limit=-1, space=space):
56
- deleted_data_models = client.data_modeling.data_models.delete(data_models.as_ids())
57
- print(f"Deleted {len(deleted_data_models)} data models")
58
- deleted_space = client.data_modeling.spaces.delete(space)
59
- print(f"Deleted space {deleted_space}")
@@ -1,121 +0,0 @@
1
- from abc import ABC
2
- from dataclasses import dataclass
3
- from typing import Any, cast
4
-
5
- from cognite.client import CogniteClient
6
- from cognite.client.data_classes._base import (
7
- CogniteResourceList,
8
- WriteableCogniteResource,
9
- WriteableCogniteResourceList,
10
- )
11
-
12
- # The Table, TableWrite data classes in the Cognite-SDK lacks the database attribute.
13
- # This is a problem when creating the RawTableLoader that needs the data class to be able to create, update, retrieve
14
- # and delete tables.
15
- # This is a reimplemented version of the Table, TableWrite data classes with the database attribute added.
16
-
17
-
18
- @dataclass(frozen=True)
19
- class RawTableID:
20
- table: str
21
- database: str
22
-
23
- def as_tuple(self) -> tuple[str, str]:
24
- return self.database, self.table
25
-
26
-
27
- class RawTableCore(WriteableCogniteResource["RawTableWrite"], ABC):
28
- """A NoSQL database table to store customer data
29
-
30
- Args:
31
- name (str | None): Unique name of the table
32
- """
33
-
34
- def __init__(
35
- self,
36
- name: str | None = None,
37
- database: str | None = None,
38
- ) -> None:
39
- self.name = name
40
- self.database = database
41
-
42
- def as_id(self) -> RawTableID:
43
- if self.name is None or self.database is None:
44
- raise ValueError("name and database are required to create a TableID")
45
- return RawTableID(table=self.name, database=self.database)
46
-
47
-
48
- class RawTable(RawTableCore):
49
- """A NoSQL database table to store customer data.
50
- This is the reading version of the Table class, which is used when retrieving a table.
51
-
52
- Args:
53
- name (str | None): Unique name of the table
54
- created_time (int | None): Time the table was created.
55
- cognite_client (CogniteClient | None): The client to associate with this object.
56
- """
57
-
58
- def __init__(
59
- self,
60
- name: str | None = None,
61
- database: str | None = None,
62
- created_time: int | None = None,
63
- cognite_client: CogniteClient | None = None,
64
- ) -> None:
65
- super().__init__(name, database)
66
- self.created_time = created_time
67
- self._cognite_client = cast("CogniteClient", cognite_client)
68
-
69
- self._db_name: str | None = None
70
-
71
- def as_write(self) -> "RawTableWrite":
72
- """Returns this Table as a TableWrite"""
73
- if self.name is None or self.database is None:
74
- raise ValueError("name and database are required to create a Table")
75
- return RawTableWrite(name=self.name, database=self.database)
76
-
77
-
78
- class RawTableWrite(RawTableCore):
79
- """A NoSQL database table to store customer data
80
- This is the writing version of the Table class, which is used when creating a table.
81
-
82
- Args:
83
- name (str): Unique name of the table
84
- """
85
-
86
- def __init__(
87
- self,
88
- name: str,
89
- database: str,
90
- ) -> None:
91
- super().__init__(name, database)
92
-
93
- @classmethod
94
- def _load(cls, resource: dict[str, Any], cognite_client: CogniteClient | None = None) -> "RawTableWrite":
95
- return cls(resource["name"], resource["database"])
96
-
97
- def as_write(self) -> "RawTableWrite":
98
- """Returns this TableWrite instance."""
99
- return self
100
-
101
-
102
- class RawTableWriteList(CogniteResourceList[RawTableWrite]):
103
- _RESOURCE = RawTableWrite
104
-
105
- def as_ids(self) -> list[RawTableID]:
106
- """Returns this TableWriteList as a list of TableIDs"""
107
- return [table.as_id() for table in self.data]
108
-
109
-
110
- class RawTableList(
111
- WriteableCogniteResourceList[RawTableWrite, RawTable],
112
- ):
113
- _RESOURCE = RawTable
114
-
115
- def as_write(self) -> RawTableWriteList:
116
- """Returns this TableList as a TableWriteList"""
117
- return RawTableWriteList([table.as_write() for table in self.data])
118
-
119
- def as_ids(self) -> list[RawTableID]:
120
- """Returns this TableList as a list of TableIDs"""
121
- return [table.as_id() for table in self.data]