power-grid-model 1.10.17__py3-none-win_amd64.whl → 1.12.119__py3-none-win_amd64.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 power-grid-model might be problematic. Click here for more details.

Files changed (67) hide show
  1. power_grid_model/__init__.py +54 -29
  2. power_grid_model/_core/__init__.py +3 -3
  3. power_grid_model/_core/buffer_handling.py +507 -478
  4. power_grid_model/_core/data_handling.py +195 -141
  5. power_grid_model/_core/data_types.py +142 -0
  6. power_grid_model/_core/dataset_definitions.py +109 -109
  7. power_grid_model/_core/enum.py +226 -0
  8. power_grid_model/_core/error_handling.py +215 -198
  9. power_grid_model/_core/errors.py +134 -0
  10. power_grid_model/_core/index_integer.py +17 -17
  11. power_grid_model/_core/options.py +71 -69
  12. power_grid_model/_core/power_grid_core.py +577 -562
  13. power_grid_model/_core/power_grid_dataset.py +545 -490
  14. power_grid_model/_core/power_grid_meta.py +262 -244
  15. power_grid_model/_core/power_grid_model.py +1025 -687
  16. power_grid_model/_core/power_grid_model_c/__init__.py +3 -0
  17. power_grid_model/_core/power_grid_model_c/bin/power_grid_model_c.dll +0 -0
  18. power_grid_model/_core/power_grid_model_c/get_pgm_dll_path.py +63 -0
  19. power_grid_model/_core/power_grid_model_c/include/power_grid_model_c/basics.h +251 -0
  20. power_grid_model/_core/power_grid_model_c/include/power_grid_model_c/buffer.h +108 -0
  21. power_grid_model/_core/power_grid_model_c/include/power_grid_model_c/dataset.h +332 -0
  22. power_grid_model/_core/power_grid_model_c/include/power_grid_model_c/dataset_definitions.h +1060 -0
  23. power_grid_model/_core/power_grid_model_c/include/power_grid_model_c/handle.h +111 -0
  24. power_grid_model/_core/power_grid_model_c/include/power_grid_model_c/meta_data.h +189 -0
  25. power_grid_model/_core/power_grid_model_c/include/power_grid_model_c/model.h +130 -0
  26. power_grid_model/_core/power_grid_model_c/include/power_grid_model_c/options.h +142 -0
  27. power_grid_model/_core/power_grid_model_c/include/power_grid_model_c/serialization.h +118 -0
  28. power_grid_model/_core/power_grid_model_c/include/power_grid_model_c.h +36 -0
  29. power_grid_model/_core/power_grid_model_c/include/power_grid_model_cpp/basics.hpp +65 -0
  30. power_grid_model/_core/power_grid_model_c/include/power_grid_model_cpp/buffer.hpp +61 -0
  31. power_grid_model/_core/power_grid_model_c/include/power_grid_model_cpp/dataset.hpp +224 -0
  32. power_grid_model/_core/power_grid_model_c/include/power_grid_model_cpp/handle.hpp +108 -0
  33. power_grid_model/_core/power_grid_model_c/include/power_grid_model_cpp/meta_data.hpp +84 -0
  34. power_grid_model/_core/power_grid_model_c/include/power_grid_model_cpp/model.hpp +63 -0
  35. power_grid_model/_core/power_grid_model_c/include/power_grid_model_cpp/options.hpp +52 -0
  36. power_grid_model/_core/power_grid_model_c/include/power_grid_model_cpp/serialization.hpp +124 -0
  37. power_grid_model/_core/power_grid_model_c/include/power_grid_model_cpp/utils.hpp +81 -0
  38. power_grid_model/_core/power_grid_model_c/include/power_grid_model_cpp.hpp +19 -0
  39. power_grid_model/_core/power_grid_model_c/lib/cmake/power_grid_model/power_grid_modelConfig.cmake +37 -0
  40. power_grid_model/_core/power_grid_model_c/lib/cmake/power_grid_model/power_grid_modelConfigVersion.cmake +65 -0
  41. power_grid_model/_core/power_grid_model_c/lib/cmake/power_grid_model/power_grid_modelTargets-release.cmake +19 -0
  42. power_grid_model/_core/power_grid_model_c/lib/cmake/power_grid_model/power_grid_modelTargets.cmake +144 -0
  43. power_grid_model/_core/power_grid_model_c/lib/power_grid_model_c.lib +0 -0
  44. power_grid_model/_core/power_grid_model_c/share/LICENSE +292 -0
  45. power_grid_model/_core/power_grid_model_c/share/README.md +15 -0
  46. power_grid_model/_core/serialization.py +319 -317
  47. power_grid_model/_core/typing.py +20 -0
  48. power_grid_model/{_utils.py → _core/utils.py} +798 -783
  49. power_grid_model/data_types.py +321 -319
  50. power_grid_model/enum.py +27 -214
  51. power_grid_model/errors.py +37 -119
  52. power_grid_model/typing.py +43 -48
  53. power_grid_model/utils.py +529 -400
  54. power_grid_model/validation/__init__.py +25 -10
  55. power_grid_model/validation/{rules.py → _rules.py} +1167 -962
  56. power_grid_model/validation/{validation.py → _validation.py} +1172 -1015
  57. power_grid_model/validation/assertions.py +93 -92
  58. power_grid_model/validation/errors.py +602 -524
  59. power_grid_model/validation/utils.py +313 -318
  60. {power_grid_model-1.10.17.dist-info → power_grid_model-1.12.119.dist-info}/METADATA +162 -165
  61. power_grid_model-1.12.119.dist-info/RECORD +65 -0
  62. {power_grid_model-1.10.17.dist-info → power_grid_model-1.12.119.dist-info}/WHEEL +1 -1
  63. power_grid_model-1.12.119.dist-info/entry_points.txt +3 -0
  64. power_grid_model/_core/_power_grid_core.dll +0 -0
  65. power_grid_model-1.10.17.dist-info/RECORD +0 -32
  66. power_grid_model-1.10.17.dist-info/top_level.txt +0 -1
  67. {power_grid_model-1.10.17.dist-info → power_grid_model-1.12.119.dist-info/licenses}/LICENSE +0 -0
@@ -1,524 +1,602 @@
1
- # SPDX-FileCopyrightText: Contributors to the Power Grid Model project <powergridmodel@lfenergy.org>
2
- #
3
- # SPDX-License-Identifier: MPL-2.0
4
-
5
- """
6
- Error classes
7
- """
8
- import re
9
- from abc import ABC
10
- from enum import Enum
11
- from typing import Any, Iterable, Type
12
-
13
- from power_grid_model import ComponentType
14
-
15
-
16
- class ValidationError(ABC):
17
- """
18
- The Validation Error is an abstract base class which should be extended by all validation errors. It supplies
19
- three public member variables: component, field and ids; storing information about the origin of the validation
20
- error. Error classes can extend the public members. For example:
21
-
22
- NotBetweenError(ValidationError):
23
- component = 'vehicle'
24
- field = 'direction'
25
- id = [3, 14, 15, 92, 65, 35]
26
- ref_value = (-3.1416, 3.1416)
27
-
28
- For convenience, a human readable representation of the error is supplied using the str() function.
29
- I.e. print(str(error)) will print a human readable error message like:
30
-
31
- Field `direction` is not between -3.1416 and 3.1416 for 6 vehicles
32
-
33
- """
34
-
35
- component: ComponentType | list[ComponentType] | None = None
36
- """
37
- The component, or components, to which the error applies.
38
- """
39
-
40
- field: str | list[str] | list[tuple[ComponentType, str]] | None = None
41
- """
42
- The field, or fields, to which the error applies. A field can also be a tuple (component, field) when multiple
43
- components are being addressed.
44
- """
45
-
46
- ids: list[int] | list[tuple[ComponentType, int]] | None = None
47
- """
48
- The object identifiers to which the error applies. A field object identifier can also be a tuple (component, id)
49
- when multiple components are being addressed.
50
- """
51
-
52
- _message: str = "An unknown validation error occurred."
53
-
54
- _delimiter: str = " and "
55
-
56
- @property
57
- def component_str(self) -> str:
58
- """
59
- A string representation of the component to which this error applies
60
- """
61
- if self.component is None:
62
- return str(None)
63
- if isinstance(self.component, list):
64
- return "/".join(component.value for component in self.component)
65
- return self.component.value
66
-
67
- @property
68
- def field_str(self) -> str:
69
- """
70
- A string representation of the field to which this error applies
71
- """
72
-
73
- def _unpack(field: str | tuple[ComponentType, str]) -> str:
74
- if isinstance(field, str):
75
- return f"'{field}'"
76
- return ".".join(field)
77
-
78
- if isinstance(self.field, list):
79
- return self._delimiter.join(_unpack(field) for field in self.field)
80
- return _unpack(self.field) if self.field else str(self.field)
81
-
82
- def get_context(self, id_lookup: list[str] | dict[int, str] | None = None) -> dict[str, Any]:
83
- """
84
- Returns a dictionary that supplies (human readable) information about this error. Each member variable is
85
- included in the dictionary. If a function {field_name}_str() exists, the value is overwritten by that function.
86
-
87
- Args:
88
- id_lookup: A list or dict (int->str) containing textual object ids
89
- """
90
- context = self.__dict__.copy()
91
- if id_lookup:
92
- if isinstance(id_lookup, list):
93
- id_lookup = dict(enumerate(id_lookup))
94
- context["ids"] = (
95
- {i: id_lookup.get(i[1] if isinstance(i, tuple) else i) for i in self.ids} if self.ids else set()
96
- )
97
- for key in context:
98
- if hasattr(self, key + "_str"):
99
- context[key] = str(getattr(self, key + "_str"))
100
- return context
101
-
102
- def __str__(self) -> str:
103
- n_objects = len(self.ids) if self.ids else 0
104
- context = self.get_context()
105
- context["n"] = n_objects
106
- context["objects"] = context.get("component", "object")
107
- if n_objects != 1:
108
- context["objects"] = re.sub(r"([a-z_]+)", r"\1s", context["objects"])
109
- return self._message.format(**context).strip()
110
-
111
- def __repr__(self) -> str:
112
- context = " ".join(f"{key}={value}" for key, value in self.get_context().items())
113
- return f"<{type(self).__name__}: {context}>"
114
-
115
- def __eq__(self, other):
116
- return (
117
- type(self) is type(other)
118
- and self.component == other.component
119
- and self.field == other.field
120
- and self.ids == other.ids
121
- )
122
-
123
-
124
- class SingleFieldValidationError(ValidationError):
125
- """
126
- Base class for an error that applies to a single field in a single component
127
- """
128
-
129
- _message = "Field {field} is not valid for {n} {objects}."
130
- component: ComponentType
131
- field: str
132
- ids: list[int] | None
133
-
134
- def __init__(self, component: ComponentType, field: str, ids: Iterable[int] | None):
135
- """
136
- Args:
137
- component: Component name
138
- field: Field name
139
- ids: List of component IDs (not row indices)
140
- """
141
- self.component = component
142
- self.field = field
143
- self.ids = sorted(ids) if ids is not None else None
144
-
145
-
146
- class MultiFieldValidationError(ValidationError):
147
- """
148
- Base class for an error that applies to multiple fields in a single component
149
- """
150
-
151
- _message = "Combination of fields {field} is not valid for {n} {objects}."
152
- component: ComponentType
153
- field: list[str]
154
- ids: list[int]
155
-
156
- def __init__(self, component: ComponentType, fields: list[str], ids: list[int]):
157
- """
158
- Args:
159
- component: Component name
160
- fields: List of field names
161
- ids: List of component IDs (not row indices)
162
- """
163
- self.component = component
164
- self.field = sorted(fields)
165
- self.ids = sorted(ids)
166
-
167
- if len(self.field) < 2:
168
- raise ValueError(f"{type(self).__name__} expects at least two fields: {self.field}")
169
-
170
-
171
- class MultiComponentValidationError(ValidationError):
172
- """
173
- Base class for an error that applies to multiple components, and, subsequently, multiple fields.
174
- Even if both fields have the same name, they are considered to be different fields and notated as such.
175
- E.g. the two fields `id` fields of the `node` and `line` component: [('node', 'id'), ('line', 'id')].
176
- """
177
-
178
- component: list[ComponentType]
179
- field: list[tuple[ComponentType, str]]
180
- ids: list[tuple[ComponentType, int]]
181
- _message = "Fields {field} are not valid for {n} {objects}."
182
-
183
- def __init__(self, fields: list[tuple[ComponentType, str]], ids: list[tuple[ComponentType, int]]):
184
- """
185
- Args:
186
- fields: List of field names, formatted as tuples (component, field)
187
- ids: List of component IDs (not row indices), formatted as tuples (component, id)
188
- """
189
- self.component = sorted(set(component for component, _ in fields), key=str)
190
- self.field = sorted(fields)
191
- self.ids = sorted(ids)
192
-
193
- if len(self.field) < 2:
194
- raise ValueError(f"{type(self).__name__} expects at least two fields: {self.field}")
195
- if len(self.component) < 2:
196
- raise ValueError(f"{type(self).__name__} expects at least two components: {self.component}")
197
-
198
-
199
- class NotIdenticalError(SingleFieldValidationError):
200
- """
201
- The value is not unique within a single column in a dataset
202
- E.g. When two nodes share the same id.
203
- """
204
-
205
- _message = "Field {field} is not unique for {n} {objects}: {num_unique} different values."
206
- values: list[Any]
207
- unique: set[Any]
208
- num_unique: int
209
-
210
- def __init__(self, component: ComponentType, field: str, ids: Iterable[int], values: list[Any]):
211
- super().__init__(component, field, ids)
212
- self.values = values
213
- self.unique = set(self.values)
214
- self.num_unique = len(self.unique)
215
-
216
-
217
- class NotUniqueError(SingleFieldValidationError):
218
- """
219
- The value is not unique within a single column in a dataset
220
- E.g. When two nodes share the same id.
221
- """
222
-
223
- _message = "Field {field} is not unique for {n} {objects}."
224
-
225
-
226
- class MultiComponentNotUniqueError(MultiComponentValidationError):
227
- """
228
- The value is not unique between multiple columns in multiple components
229
- E.g. When a node and a line share the same id.
230
- """
231
-
232
- _message = "Fields {field} are not unique for {n} {objects}."
233
-
234
-
235
- class InvalidEnumValueError(SingleFieldValidationError):
236
- """
237
- The value is not a valid value in the supplied enumeration type.
238
- E.g. a sym_load has a non existing LoadGenType.
239
- """
240
-
241
- _message = "Field {field} contains invalid {enum} values for {n} {objects}."
242
- enum: Type[Enum] | list[Type[Enum]]
243
-
244
- def __init__(self, component: ComponentType, field: str, ids: list[int], enum: Type[Enum] | list[Type[Enum]]):
245
- super().__init__(component, field, ids)
246
- self.enum = enum
247
-
248
- @property
249
- def enum_str(self) -> str:
250
- """
251
- A string representation of the field to which this error applies.
252
- """
253
- if isinstance(self.enum, list):
254
- return ",".join(e.__name__ for e in self.enum)
255
-
256
- return self.enum.__name__
257
-
258
- def __eq__(self, other):
259
- return super().__eq__(other) and self.enum == other.enum
260
-
261
-
262
- class SameValueError(MultiFieldValidationError):
263
- """
264
- The value of two fields is equal.
265
- E.g. A line has the same from_node as to_node.
266
- """
267
-
268
- _message = "Same value for {field} for {n} {objects}."
269
-
270
-
271
- class NotBooleanError(SingleFieldValidationError):
272
- """
273
- Invalid boolean value. Boolean fields don't really exist in our data structure, they are 1-byte signed integers and
274
- should contain either a 0 (=False) or a 1 (=True).
275
- """
276
-
277
- _message = "Field {field} is not a boolean (0 or 1) for {n} {objects}."
278
-
279
-
280
- class MissingValueError(SingleFieldValidationError):
281
- """
282
- A required value was missing, i.e. NaN.
283
- """
284
-
285
- _message = "Field {field} is missing for {n} {objects}."
286
-
287
-
288
- class IdNotInDatasetError(SingleFieldValidationError):
289
- """
290
- An object identifier does not exist in the original data.
291
- E.g. An update data set contains a record for a line that doesn't exist in the input data set.
292
- """
293
-
294
- _message = "ID does not exist in {ref_dataset} for {n} {objects}."
295
- ref_dataset: str
296
-
297
- def __init__(self, component: ComponentType, ids: list[int], ref_dataset: str):
298
- super().__init__(component=component, field="id", ids=ids)
299
- self.ref_dataset = ref_dataset
300
-
301
- def __eq__(self, other):
302
- return super().__eq__(other) and self.ref_dataset == other.ref_dataset
303
-
304
-
305
- class InvalidIdError(SingleFieldValidationError):
306
- """
307
- An object identifier does not refer to the right type of object.
308
- E.g. An line's from_node refers to a source.
309
-
310
- Filters can have been applied to check a subset of the records. E.g. This error may apply only to power_sensors
311
- that are said to be connected to a source (filter: measured_terminal_type=source). This useful to spot validation
312
- mistakes, due to ambiguity.
313
-
314
- E.g. when a sym_power_sensor is connected to a load, but measured_terminal_type is accidentally set to 'source',
315
- the error is:
316
-
317
- "Field `measured_object` does not contain a valid source id for 1 sym_power_sensor. (measured_terminal_type=source)"
318
-
319
- """
320
-
321
- _message = "Field {field} does not contain a valid {ref_components} id for {n} {objects}. {filters}"
322
- ref_components: list[ComponentType]
323
-
324
- def __init__( # pylint: disable=too-many-arguments
325
- self,
326
- component: ComponentType,
327
- field: str,
328
- ids: list[int] | None = None,
329
- ref_components: ComponentType | list[ComponentType] | None = None,
330
- filters: dict[str, Any] | None = None,
331
- ):
332
- # pylint: disable=too-many-positional-arguments
333
- super().__init__(component=component, field=field, ids=ids)
334
- ref_components = ref_components if ref_components is not None else []
335
- self.ref_components = [ref_components] if isinstance(ref_components, (str, ComponentType)) else ref_components
336
- self.filters = filters if filters else None
337
-
338
- @property
339
- def ref_components_str(self):
340
- """
341
- A string representation of the components to which this error applies
342
- """
343
- return "/".join(self.ref_components)
344
-
345
- @property
346
- def filters_str(self):
347
- """
348
- A string representation of the filters that have been applied to the data to which this error refers
349
- """
350
- if not self.filters:
351
- return ""
352
- filters = ", ".join(f"{k}={v.name if isinstance(v, Enum) else v}" for k, v in self.filters.items())
353
- return f"({filters})"
354
-
355
- def __eq__(self, other):
356
- return super().__eq__(other) and self.ref_components == other.ref_components and self.filters == other.filters
357
-
358
-
359
- class TwoValuesZeroError(MultiFieldValidationError):
360
- """
361
- A record has a 0.0 value in two fields at the same time.
362
- E.g. A line's `r1`, `x1` are both 0.
363
- """
364
-
365
- _message = "Fields {field} are both zero for {n} {objects}."
366
-
367
-
368
- class ComparisonError(SingleFieldValidationError):
369
- """
370
- Base class for comparison errors.
371
- E.g. A transformer's `i0` is not greater or equal to it's `p0` divided by it's `sn`
372
- """
373
-
374
- _message = "Invalid {field}, compared to {ref_value} for {n} {objects}."
375
-
376
- RefType = int | float | str | tuple[int | float | str, ...]
377
-
378
- def __init__(self, component: ComponentType, field: str, ids: list[int], ref_value: "ComparisonError.RefType"):
379
- super().__init__(component, field, ids)
380
- self.ref_value = ref_value
381
-
382
- @property
383
- def ref_value_str(self):
384
- """
385
- A string representation of the reference value. E.g. 'zero', 'one', 'field_a and field_b' or '123'.
386
- """
387
- if isinstance(self.ref_value, tuple):
388
- return self._delimiter.join(map(str, self.ref_value))
389
- if self.ref_value == 0:
390
- return "zero"
391
- if self.ref_value == 1:
392
- return "one"
393
- return str(self.ref_value)
394
-
395
- def __eq__(self, other):
396
- return super().__eq__(other) and self.ref_value == other.ref_value
397
-
398
-
399
- class NotGreaterThanError(ComparisonError):
400
- """
401
- The value of a field is not greater than a reference value or expression.
402
- """
403
-
404
- _message = "Field {field} is not greater than {ref_value} for {n} {objects}."
405
-
406
-
407
- class NotGreaterOrEqualError(ComparisonError):
408
- """
409
- The value of a field is not greater or equal to a reference value or expression.
410
- """
411
-
412
- _message = "Field {field} is not greater than (or equal to) {ref_value} for {n} {objects}."
413
-
414
-
415
- class NotLessThanError(ComparisonError):
416
- """
417
- The value of a field is not less than a reference value or expression.
418
- """
419
-
420
- _message = "Field {field} is not smaller than {ref_value} for {n} {objects}."
421
-
422
-
423
- class NotLessOrEqualError(ComparisonError):
424
- """
425
- The value of a field is not smaller or equal to a reference value or expression.
426
- """
427
-
428
- _message = "Field {field} is not smaller than (or equal to) {ref_value} for {n} {objects}."
429
-
430
-
431
- class NotBetweenError(ComparisonError):
432
- """
433
- The value of a field is not between two a reference values or expressions (exclusive).
434
- """
435
-
436
- _message = "Field {field} is not between {ref_value} for {n} {objects}."
437
-
438
-
439
- class NotBetweenOrAtError(ComparisonError):
440
- """
441
- The value of a field is not between two a reference values or expressions (inclusive).
442
- """
443
-
444
- _message = "Field {field} is not between (or at) {ref_value} for {n} {objects}."
445
-
446
-
447
- class InfinityError(SingleFieldValidationError):
448
- """
449
- The value of a field is infinite.
450
- """
451
-
452
- _message = "Field {field} is infinite for {n} {objects}."
453
-
454
-
455
- class TransformerClockError(MultiFieldValidationError):
456
- """
457
- Invalid clock number.
458
- """
459
-
460
- _message = (
461
- "Invalid clock number for {n} {objects}. "
462
- "If one side has wye winding and the other side has not, the clock number should be odd. "
463
- "If either both or none of the sides have wye winding, the clock number should be even."
464
- )
465
-
466
-
467
- class FaultPhaseError(MultiFieldValidationError):
468
- """
469
- The fault phase does not match the fault type.
470
- """
471
-
472
- _message = "The fault phase is not applicable to the corresponding fault type for {n} {objects}."
473
-
474
-
475
- class InvalidAssociatedEnumValueError(MultiFieldValidationError):
476
- """
477
- The value is not a valid value in combination with the other specified attributes.
478
- E.g. When a transformer tap regulator has a branch3 control side but regulates a transformer.
479
- """
480
-
481
- _message = "The combination of fields {field} results in invalid {enum} values for {n} {objects}."
482
- enum: Type[Enum] | list[Type[Enum]]
483
-
484
- def __init__(
485
- self,
486
- component: ComponentType,
487
- fields: list[str],
488
- ids: list[int],
489
- enum: Type[Enum] | list[Type[Enum]],
490
- ):
491
- """
492
- Args:
493
- component: Component name
494
- fields: List of field names
495
- ids: List of component IDs (not row indices)
496
- enum: The supported enum values
497
- """
498
- super().__init__(component, fields, ids)
499
- self.enum = enum
500
-
501
- @property
502
- def enum_str(self) -> str:
503
- """
504
- A string representation of the field to which this error applies.
505
- """
506
- if isinstance(self.enum, list):
507
- return ",".join(e.__name__ for e in self.enum)
508
-
509
- return self.enum.__name__
510
-
511
- def __eq__(self, other):
512
- return super().__eq__(other) and self.enum == other.enum
513
-
514
-
515
- class UnsupportedTransformerRegulationError(MultiFieldValidationError):
516
- """
517
- The control side of a branch regulator is not supported for the regulated object
518
- """
519
-
520
- _message = (
521
- "Unsupported control side for {n} {objects}. "
522
- "The control side cannot be on the same side as the tap side. "
523
- "The node at the control side must have the same or lower u_rated as the node at the tap side."
524
- )
1
+ # SPDX-FileCopyrightText: Contributors to the Power Grid Model project <powergridmodel@lfenergy.org>
2
+ #
3
+ # SPDX-License-Identifier: MPL-2.0
4
+
5
+ """
6
+ Error classes
7
+ """
8
+
9
+ import re
10
+ from abc import ABC
11
+ from collections.abc import Iterable
12
+ from enum import Enum
13
+ from typing import Any
14
+
15
+ from power_grid_model import ComponentType
16
+ from power_grid_model._core.dataset_definitions import DatasetType
17
+
18
+ _MIN_FIELDS = 2
19
+ _MIN_COMPONENTS = 2
20
+
21
+
22
+ class ValidationError(ABC):
23
+ """
24
+ The Validation Error is an abstract base class which should be extended by all validation errors. It supplies
25
+ three public member variables: component, field and ids; storing information about the origin of the validation
26
+ error. Error classes can extend the public members. For example:
27
+
28
+ NotBetweenError(ValidationError):
29
+ component = 'vehicle'
30
+ field = 'direction'
31
+ id = [3, 14, 15, 92, 65, 35]
32
+ ref_value = (-3.1416, 3.1416)
33
+
34
+ For convenience, a human readable representation of the error is supplied using the str() function.
35
+ I.e. print(str(error)) will print a human readable error message like:
36
+
37
+ Field `direction` is not between -3.1416 and 3.1416 for 6 vehicles
38
+
39
+ """
40
+
41
+ component: ComponentType | list[ComponentType] | None = None
42
+ """
43
+ The component, or components, to which the error applies.
44
+ """
45
+
46
+ field: str | list[str] | list[tuple[ComponentType, str]] | None = None
47
+ """
48
+ The field, or fields, to which the error applies. A field can also be a tuple (component, field) when multiple
49
+ components are being addressed.
50
+ """
51
+
52
+ ids: list[int] | list[tuple[ComponentType, int]] | None = None
53
+ """
54
+ The object identifiers to which the error applies. A field object identifier can also be a tuple (component, id)
55
+ when multiple components are being addressed.
56
+ """
57
+
58
+ _message: str = "An unknown validation error occurred."
59
+
60
+ _delimiter: str = " and "
61
+
62
+ __hash__ = None # type: ignore[assignment]
63
+
64
+ @property
65
+ def component_str(self) -> str:
66
+ """
67
+ A string representation of the component to which this error applies
68
+ """
69
+ if self.component is None:
70
+ return str(None)
71
+ if isinstance(self.component, list):
72
+ return "/".join(component.value for component in self.component)
73
+ return self.component.value
74
+
75
+ @property
76
+ def field_str(self) -> str:
77
+ """
78
+ A string representation of the field to which this error applies
79
+ """
80
+
81
+ def _unpack(field: str | tuple[ComponentType, str]) -> str:
82
+ if isinstance(field, str):
83
+ return f"'{field}'"
84
+ return ".".join(field)
85
+
86
+ if isinstance(self.field, list):
87
+ return self._delimiter.join(_unpack(field) for field in self.field)
88
+ return _unpack(self.field) if self.field else str(self.field)
89
+
90
+ def get_context(self, id_lookup: list[str] | dict[int, str] | None = None) -> dict[str, Any]:
91
+ """
92
+ Returns a dictionary that supplies (human readable) information about this error. Each member variable is
93
+ included in the dictionary. If a function {field_name}_str() exists, the value is overwritten by that function.
94
+
95
+ Args:
96
+ id_lookup: A list or dict (int->str) containing textual object ids
97
+ """
98
+ context = self.__dict__.copy()
99
+ if id_lookup:
100
+ if isinstance(id_lookup, list):
101
+ id_lookup = dict(enumerate(id_lookup))
102
+ context["ids"] = (
103
+ {i: id_lookup.get(i[1] if isinstance(i, tuple) else i) for i in self.ids} if self.ids else set()
104
+ )
105
+ for key in context:
106
+ if hasattr(self, key + "_str"):
107
+ context[key] = str(getattr(self, key + "_str"))
108
+ return context
109
+
110
+ def __str__(self) -> str:
111
+ n_objects = len(self.ids) if self.ids else 0
112
+ context = self.get_context()
113
+ context["n"] = n_objects
114
+ context["objects"] = context.get("component", "object")
115
+ if n_objects != 1:
116
+ context["objects"] = re.sub(r"([a-z_]+)", r"\1s", context["objects"])
117
+ return self._message.format(**context).strip()
118
+
119
+ def __repr__(self) -> str:
120
+ context = " ".join(f"{key}={value}" for key, value in self.get_context().items())
121
+ return f"<{type(self).__name__}: {context}>"
122
+
123
+ def __eq__(self, other):
124
+ return (
125
+ type(self) is type(other)
126
+ and self.component == other.component
127
+ and self.field == other.field
128
+ and self.ids == other.ids
129
+ )
130
+
131
+
132
+ class SingleFieldValidationError(ValidationError):
133
+ """
134
+ Base class for an error that applies to a single field in a single component
135
+ """
136
+
137
+ _message = "Field {field} is not valid for {n} {objects}."
138
+ component: ComponentType
139
+ field: str
140
+ ids: list[int] | None
141
+
142
+ def __init__(self, component: ComponentType, field: str, ids: Iterable[int] | None):
143
+ """
144
+ Args:
145
+ component: Component name
146
+ field: Field name
147
+ ids: List of component IDs (not row indices)
148
+ """
149
+ self.component = component
150
+ self.field = field
151
+ self.ids = sorted(ids) if ids is not None else None
152
+
153
+
154
+ class MultiFieldValidationError(ValidationError):
155
+ """
156
+ Base class for an error that applies to multiple fields in a single component
157
+ """
158
+
159
+ _message = "Combination of fields {field} is not valid for {n} {objects}."
160
+ component: ComponentType
161
+ field: list[str]
162
+ ids: list[int]
163
+
164
+ def __init__(self, component: ComponentType, fields: list[str], ids: list[int]):
165
+ """
166
+ Args:
167
+ component: Component name
168
+ fields: List of field names
169
+ ids: List of component IDs (not row indices)
170
+ """
171
+ self.component = component
172
+ self.field = sorted(fields)
173
+ self.ids = sorted(ids)
174
+
175
+ if len(self.field) < _MIN_FIELDS:
176
+ raise ValueError(f"{type(self).__name__} expects at least 2 fields: {self.field}")
177
+
178
+
179
+ class MultiComponentValidationError(ValidationError):
180
+ """
181
+ Base class for an error that applies to multiple components, and, subsequently, multiple fields.
182
+ Even if both fields have the same name, they are considered to be different fields and notated as such.
183
+ E.g. the two fields `id` fields of the `node` and `line` component: [('node', 'id'), ('line', 'id')].
184
+ """
185
+
186
+ component: list[ComponentType]
187
+ field: list[tuple[ComponentType, str]]
188
+ ids: list[tuple[ComponentType, int]]
189
+ _message = "Fields {field} are not valid for {n} {objects}."
190
+
191
+ def __init__(self, fields: list[tuple[ComponentType, str]], ids: list[tuple[ComponentType, int]]):
192
+ """
193
+ Args:
194
+ fields: List of field names, formatted as tuples (component, field)
195
+ ids: List of component IDs (not row indices), formatted as tuples (component, id)
196
+ """
197
+ self.component = sorted({component for component, _ in fields}, key=str)
198
+ self.field = sorted(fields)
199
+ self.ids = sorted(ids)
200
+
201
+ if len(self.field) < _MIN_FIELDS:
202
+ raise ValueError(f"{type(self).__name__} expects at least {_MIN_FIELDS} fields: {self.field}")
203
+ if len(self.component) < _MIN_COMPONENTS:
204
+ raise ValueError(f"{type(self).__name__} expects at least {_MIN_COMPONENTS} components: {self.component}")
205
+
206
+
207
+ class NotIdenticalError(SingleFieldValidationError):
208
+ """
209
+ The value is not unique within a single column in a dataset
210
+ E.g. When two nodes share the same id.
211
+ """
212
+
213
+ _message = "Field {field} is not unique for {n} {objects}: {num_unique} different values."
214
+ values: list[Any]
215
+ unique: set[Any]
216
+ num_unique: int
217
+
218
+ def __init__(self, component: ComponentType, field: str, ids: Iterable[int], values: list[Any]):
219
+ super().__init__(component, field, ids)
220
+ self.values = values
221
+ self.unique = set(self.values)
222
+ self.num_unique = len(self.unique)
223
+
224
+
225
+ class NotUniqueError(SingleFieldValidationError):
226
+ """
227
+ The value is not unique within a single column in a dataset
228
+ E.g. When two nodes share the same id.
229
+ """
230
+
231
+ _message = "Field {field} is not unique for {n} {objects}."
232
+
233
+
234
+ class MultiComponentNotUniqueError(MultiComponentValidationError):
235
+ """
236
+ The value is not unique between multiple columns in multiple components
237
+ E.g. When a node and a line share the same id.
238
+ """
239
+
240
+ _message = "Fields {field} are not unique for {n} {objects}."
241
+
242
+
243
+ class InvalidValueError(SingleFieldValidationError):
244
+ """
245
+ The value is not a valid value in the supplied list of supported values.
246
+ E.g. an enum value that is not supported for a specific feature.
247
+ """
248
+
249
+ _message = "Field {field} contains invalid values for {n} {objects}."
250
+ values: list
251
+ __hash__ = None # type: ignore[assignment]
252
+
253
+ def __init__(self, component: ComponentType, field: str, ids: list[int], values: list):
254
+ super().__init__(component, field, ids)
255
+ self.values = values
256
+
257
+ @property
258
+ def values_str(self) -> str:
259
+ """
260
+ A string representation of the field to which this error applies.
261
+ """
262
+ return ",".join(v.name if isinstance(v, Enum) else v for v in self.values)
263
+
264
+ def __eq__(self, other):
265
+ return super().__eq__(other) and self.values == other.values
266
+
267
+
268
+ class InvalidEnumValueError(SingleFieldValidationError):
269
+ """
270
+ The value is not a valid value in the supplied enumeration type.
271
+ E.g. a sym_load has a non existing LoadGenType.
272
+ """
273
+
274
+ _message = "Field {field} contains invalid {enum} values for {n} {objects}."
275
+ enum: type[Enum] | list[type[Enum]]
276
+ __hash__ = None # type: ignore[assignment]
277
+
278
+ def __init__(self, component: ComponentType, field: str, ids: list[int], enum: type[Enum] | list[type[Enum]]):
279
+ super().__init__(component, field, ids)
280
+ self.enum = enum
281
+
282
+ @property
283
+ def enum_str(self) -> str:
284
+ """
285
+ A string representation of the field to which this error applies.
286
+ """
287
+ if isinstance(self.enum, list):
288
+ return ",".join(e.__name__ for e in self.enum)
289
+
290
+ return self.enum.__name__
291
+
292
+ def __eq__(self, other):
293
+ return super().__eq__(other) and self.enum == other.enum
294
+
295
+
296
+ class SameValueError(MultiFieldValidationError):
297
+ """
298
+ The value of two fields is equal.
299
+ E.g. A line has the same from_node as to_node.
300
+ """
301
+
302
+ _message = "Same value for {field} for {n} {objects}."
303
+
304
+
305
+ class NotBooleanError(SingleFieldValidationError):
306
+ """
307
+ Invalid boolean value. Boolean fields don't really exist in our data structure, they are 1-byte signed integers and
308
+ should contain either a 0 (=False) or a 1 (=True).
309
+ """
310
+
311
+ _message = "Field {field} is not a boolean (0 or 1) for {n} {objects}."
312
+
313
+
314
+ class MissingValueError(SingleFieldValidationError):
315
+ """
316
+ A required value was missing, i.e. NaN.
317
+ """
318
+
319
+ _message = "Field {field} is missing for {n} {objects}."
320
+
321
+
322
+ class IdNotInDatasetError(SingleFieldValidationError):
323
+ """
324
+ An object identifier does not exist in the original data.
325
+ E.g. An update data set contains a record for a line that doesn't exist in the input data set.
326
+ """
327
+
328
+ _message = "ID does not exist in {ref_dataset} for {n} {objects}."
329
+ ref_dataset: DatasetType
330
+ __hash__ = None # type: ignore[assignment]
331
+
332
+ def __init__(self, component: ComponentType, ids: list[int], ref_dataset: DatasetType):
333
+ super().__init__(component=component, field="id", ids=ids)
334
+ self.ref_dataset = ref_dataset
335
+
336
+ def __eq__(self, other):
337
+ return super().__eq__(other) and self.ref_dataset == other.ref_dataset
338
+
339
+
340
+ class InvalidIdError(SingleFieldValidationError):
341
+ """
342
+ An object identifier does not refer to the right type of object.
343
+ E.g. An line's from_node refers to a source.
344
+
345
+ Filters can have been applied to check a subset of the records. E.g. This error may apply only to power_sensors
346
+ that are said to be connected to a source (filter: measured_terminal_type=source). This useful to spot validation
347
+ mistakes, due to ambiguity.
348
+
349
+ E.g. when a sym_power_sensor is connected to a load, but measured_terminal_type is accidentally set to 'source',
350
+ the error is:
351
+
352
+ "Field `measured_object` does not contain a valid source id for 1 sym_power_sensor. (measured_terminal_type=source)"
353
+
354
+ """
355
+
356
+ _message = "Field {field} does not contain a valid {ref_components} id for {n} {objects}. {filters}"
357
+ ref_components: list[ComponentType]
358
+ __hash__ = None # type: ignore[assignment]
359
+
360
+ def __init__(
361
+ self,
362
+ component: ComponentType,
363
+ field: str,
364
+ ids: list[int] | None = None,
365
+ ref_components: ComponentType | list[ComponentType] | None = None,
366
+ filters: dict[str, Any] | None = None,
367
+ ):
368
+ super().__init__(component=component, field=field, ids=ids)
369
+ ref_components = ref_components if ref_components is not None else []
370
+ self.ref_components = [ref_components] if isinstance(ref_components, (str, ComponentType)) else ref_components
371
+ self.filters = filters if filters else None
372
+
373
+ @property
374
+ def ref_components_str(self):
375
+ """
376
+ A string representation of the components to which this error applies
377
+ """
378
+ return "/".join(self.ref_components)
379
+
380
+ @property
381
+ def filters_str(self):
382
+ """
383
+ A string representation of the filters that have been applied to the data to which this error refers
384
+ """
385
+ if not self.filters:
386
+ return ""
387
+ filters = ", ".join(f"{k}={v.name if isinstance(v, Enum) else v}" for k, v in self.filters.items())
388
+ return f"({filters})"
389
+
390
+ def __eq__(self, other):
391
+ return super().__eq__(other) and self.ref_components == other.ref_components and self.filters == other.filters
392
+
393
+
394
+ class TwoValuesZeroError(MultiFieldValidationError):
395
+ """
396
+ A record has a 0.0 value in two fields at the same time.
397
+ E.g. A line's `r1`, `x1` are both 0.
398
+ """
399
+
400
+ _message = "Fields {field} are both zero for {n} {objects}."
401
+
402
+
403
+ class ComparisonError(SingleFieldValidationError):
404
+ """
405
+ Base class for comparison errors.
406
+ E.g. A transformer's `i0` is not greater or equal to it's `p0` divided by it's `sn`
407
+ """
408
+
409
+ _message = "Invalid {field}, compared to {ref_value} for {n} {objects}."
410
+
411
+ RefType = int | float | str | tuple[int | float | str, ...]
412
+
413
+ __hash__ = None # type: ignore[assignment]
414
+
415
+ def __init__(self, component: ComponentType, field: str, ids: list[int], ref_value: "ComparisonError.RefType"):
416
+ super().__init__(component, field, ids)
417
+ self.ref_value = ref_value
418
+
419
+ @property
420
+ def ref_value_str(self):
421
+ """
422
+ A string representation of the reference value. E.g. 'zero', 'one', 'field_a and field_b' or '123'.
423
+ """
424
+ if isinstance(self.ref_value, tuple):
425
+ return self._delimiter.join(map(str, self.ref_value))
426
+ if self.ref_value == 0:
427
+ return "zero"
428
+ if self.ref_value == 1:
429
+ return "one"
430
+ return str(self.ref_value)
431
+
432
+ def __eq__(self, other):
433
+ return super().__eq__(other) and self.ref_value == other.ref_value
434
+
435
+
436
+ class NotGreaterThanError(ComparisonError):
437
+ """
438
+ The value of a field is not greater than a reference value or expression.
439
+ """
440
+
441
+ _message = "Field {field} is not greater than {ref_value} for {n} {objects}."
442
+
443
+
444
+ class NotGreaterOrEqualError(ComparisonError):
445
+ """
446
+ The value of a field is not greater or equal to a reference value or expression.
447
+ """
448
+
449
+ _message = "Field {field} is not greater than (or equal to) {ref_value} for {n} {objects}."
450
+
451
+
452
+ class NotLessThanError(ComparisonError):
453
+ """
454
+ The value of a field is not less than a reference value or expression.
455
+ """
456
+
457
+ _message = "Field {field} is not smaller than {ref_value} for {n} {objects}."
458
+
459
+
460
+ class NotLessOrEqualError(ComparisonError):
461
+ """
462
+ The value of a field is not smaller or equal to a reference value or expression.
463
+ """
464
+
465
+ _message = "Field {field} is not smaller than (or equal to) {ref_value} for {n} {objects}."
466
+
467
+
468
+ class NotBetweenError(ComparisonError):
469
+ """
470
+ The value of a field is not between two a reference values or expressions (exclusive).
471
+ """
472
+
473
+ _message = "Field {field} is not between {ref_value} for {n} {objects}."
474
+
475
+
476
+ class NotBetweenOrAtError(ComparisonError):
477
+ """
478
+ The value of a field is not between two a reference values or expressions (inclusive).
479
+ """
480
+
481
+ _message = "Field {field} is not between (or at) {ref_value} for {n} {objects}."
482
+
483
+
484
+ class InfinityError(SingleFieldValidationError):
485
+ """
486
+ The value of a field is infinite.
487
+ """
488
+
489
+ _message = "Field {field} is infinite for {n} {objects}."
490
+
491
+
492
+ class TransformerClockError(MultiFieldValidationError):
493
+ """
494
+ Invalid clock number.
495
+ """
496
+
497
+ _message = (
498
+ "Invalid clock number for {n} {objects}. "
499
+ "If one side has wye winding and the other side has not, the clock number should be odd. "
500
+ "If either both or none of the sides have wye winding, the clock number should be even."
501
+ )
502
+
503
+
504
+ class FaultPhaseError(MultiFieldValidationError):
505
+ """
506
+ The fault phase does not match the fault type.
507
+ """
508
+
509
+ _message = "The fault phase is not applicable to the corresponding fault type for {n} {objects}."
510
+
511
+
512
+ class PQSigmaPairError(MultiFieldValidationError):
513
+ """
514
+ The combination of p_sigma and q_sigma is not valid. They should be both present or both absent.
515
+ """
516
+
517
+ _message = "The combination of p_sigma and q_sigma is not valid for {n} {objects}."
518
+
519
+
520
+ class InvalidAssociatedEnumValueError(MultiFieldValidationError):
521
+ """
522
+ The value is not a valid value in combination with the other specified attributes.
523
+ E.g. When a transformer tap regulator has a branch3 control side but regulates a transformer.
524
+ """
525
+
526
+ _message = "The combination of fields {field} results in invalid {enum} values for {n} {objects}."
527
+ enum: type[Enum] | list[type[Enum]]
528
+ __hash__ = None # type: ignore[assignment]
529
+
530
+ def __init__(
531
+ self,
532
+ component: ComponentType,
533
+ fields: list[str],
534
+ ids: list[int],
535
+ enum: type[Enum] | list[type[Enum]],
536
+ ):
537
+ """
538
+ Args:
539
+ component: Component name
540
+ fields: List of field names
541
+ ids: List of component IDs (not row indices)
542
+ enum: The supported enum values
543
+ """
544
+ super().__init__(component, fields, ids)
545
+ self.enum = enum
546
+
547
+ @property
548
+ def enum_str(self) -> str:
549
+ """
550
+ A string representation of the field to which this error applies.
551
+ """
552
+ if isinstance(self.enum, list):
553
+ return ",".join(e.__name__ for e in self.enum)
554
+
555
+ return self.enum.__name__
556
+
557
+ def __eq__(self, other):
558
+ return super().__eq__(other) and self.enum == other.enum
559
+
560
+
561
+ class UnsupportedMeasuredTerminalType(InvalidValueError):
562
+ """
563
+ The measured terminal type is not a supported value.
564
+
565
+ Supported values are in the supplied list of values.
566
+ """
567
+
568
+ _message = "measured_terminal_type contains unsupported values for {n} {objects}."
569
+
570
+
571
+ class MixedCurrentAngleMeasurementTypeError(MultiFieldValidationError):
572
+ """
573
+ Mixed current angle measurement type error.
574
+ """
575
+
576
+ _message = (
577
+ "Mixture of different current angle measurement types on the same terminal for {n} {objects}. "
578
+ "If multiple current sensors measure the same terminal of the same object, all angle measurement types must be "
579
+ "the same. Mixing local_angle and global_angle current measurements on the same terminal is not supported."
580
+ )
581
+
582
+
583
+ class MixedPowerCurrentSensorError(MultiComponentValidationError):
584
+ """
585
+ Mixed power and current sensor error.
586
+ """
587
+
588
+ _message = (
589
+ "Mixture of power and current sensors on the same terminal for {n} {objects}. "
590
+ "If multiple sensors measure the same terminal of the same object, all sensors must measure the same quantity."
591
+ )
592
+
593
+
594
+ class MissingVoltageAngleMeasurementError(MultiComponentValidationError):
595
+ """
596
+ Missing voltage angle measurement error.
597
+ """
598
+
599
+ _message = (
600
+ "Missing voltage angle measurement for {n} {objects}. "
601
+ "If a voltage sensor measures the voltage of a terminal, it must also measure the voltage angle."
602
+ )