power-grid-model 1.11.23__py3-none-win_amd64.whl → 1.12.70__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.
Files changed (67) hide show
  1. power_grid_model/__init__.py +54 -52
  2. power_grid_model/_core/__init__.py +3 -3
  3. power_grid_model/_core/buffer_handling.py +493 -478
  4. power_grid_model/_core/data_handling.py +195 -141
  5. power_grid_model/_core/data_types.py +143 -132
  6. power_grid_model/_core/dataset_definitions.py +109 -108
  7. power_grid_model/_core/enum.py +226 -226
  8. power_grid_model/_core/error_handling.py +206 -205
  9. power_grid_model/_core/errors.py +130 -126
  10. power_grid_model/_core/index_integer.py +17 -17
  11. power_grid_model/_core/options.py +71 -70
  12. power_grid_model/_core/power_grid_core.py +563 -581
  13. power_grid_model/_core/power_grid_dataset.py +535 -534
  14. power_grid_model/_core/power_grid_meta.py +257 -243
  15. power_grid_model/_core/power_grid_model.py +969 -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 +255 -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 +316 -0
  22. power_grid_model/_core/power_grid_model_c/include/power_grid_model_c/dataset_definitions.h +1052 -0
  23. power_grid_model/_core/power_grid_model_c/include/power_grid_model_c/handle.h +99 -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 +125 -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 +220 -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 +317 -317
  47. power_grid_model/_core/typing.py +20 -20
  48. power_grid_model/_core/utils.py +798 -789
  49. power_grid_model/data_types.py +321 -307
  50. power_grid_model/enum.py +27 -27
  51. power_grid_model/errors.py +37 -37
  52. power_grid_model/typing.py +43 -43
  53. power_grid_model/utils.py +473 -465
  54. power_grid_model/validation/__init__.py +25 -25
  55. power_grid_model/validation/_rules.py +1171 -1175
  56. power_grid_model/validation/_validation.py +1172 -1158
  57. power_grid_model/validation/assertions.py +93 -93
  58. power_grid_model/validation/errors.py +602 -588
  59. power_grid_model/validation/utils.py +313 -321
  60. {power_grid_model-1.11.23.dist-info → power_grid_model-1.12.70.dist-info}/METADATA +178 -180
  61. power_grid_model-1.12.70.dist-info/RECORD +65 -0
  62. {power_grid_model-1.11.23.dist-info → power_grid_model-1.12.70.dist-info}/WHEEL +1 -1
  63. power_grid_model-1.12.70.dist-info/entry_points.txt +3 -0
  64. power_grid_model/_core/_power_grid_core.dll +0 -0
  65. power_grid_model-1.11.23.dist-info/RECORD +0 -36
  66. power_grid_model-1.11.23.dist-info/top_level.txt +0 -1
  67. {power_grid_model-1.11.23.dist-info → power_grid_model-1.12.70.dist-info}/licenses/LICENSE +0 -0
@@ -1,534 +1,535 @@
1
- # SPDX-FileCopyrightText: Contributors to the Power Grid Model project <powergridmodel@lfenergy.org>
2
- #
3
- # SPDX-License-Identifier: MPL-2.0
4
-
5
- """
6
- Power grid model raw dataset handler
7
- """
8
-
9
- from typing import Any, Mapping, cast
10
-
11
- from power_grid_model._core.buffer_handling import (
12
- BufferProperties,
13
- CAttributeBuffer,
14
- CBuffer,
15
- create_buffer,
16
- get_buffer_properties,
17
- get_buffer_view,
18
- )
19
- from power_grid_model._core.data_types import (
20
- AttributeType,
21
- ColumnarData,
22
- ComponentData,
23
- Dataset,
24
- DenseBatchColumnarData,
25
- SingleColumnarData,
26
- SparseBatchColumnarData,
27
- )
28
- from power_grid_model._core.dataset_definitions import ComponentType, DatasetType, _str_to_component_type
29
- from power_grid_model._core.enum import ComponentAttributeFilterOptions
30
- from power_grid_model._core.error_handling import VALIDATOR_MSG, assert_no_error
31
- from power_grid_model._core.power_grid_core import (
32
- ConstDatasetPtr,
33
- DatasetInfoPtr,
34
- MutableDatasetPtr,
35
- WritableDatasetPtr,
36
- power_grid_core as pgc,
37
- )
38
- from power_grid_model._core.power_grid_meta import ComponentMetaData, DatasetMetaData, power_grid_meta_data
39
- from power_grid_model._core.typing import ComponentAttributeMapping, _ComponentAttributeMappingDict
40
- from power_grid_model._core.utils import (
41
- get_dataset_type,
42
- is_columnar,
43
- is_nan_or_equivalent,
44
- is_sparse,
45
- process_data_filter,
46
- )
47
-
48
-
49
- class CDatasetInfo:
50
- """
51
- Raw Power Grid Model dataset info.
52
- """
53
-
54
- def __init__(self, info: DatasetInfoPtr):
55
- self._info: DatasetInfoPtr = info
56
-
57
- def name(self) -> str:
58
- """
59
- The name of the dataset type.
60
-
61
- Returns:
62
- The name of the dataset type
63
- """
64
- return pgc.dataset_info_name(self._info)
65
-
66
- def dataset_type(self):
67
- """
68
- The name of the dataset type.
69
-
70
- Returns:
71
- The name of the dataset type
72
- """
73
- return self.name()
74
-
75
- def is_batch(self) -> bool:
76
- """
77
- Whether the dataset is a batch dataset.
78
-
79
- Returns:
80
- Whether the dataset is a batch dataset
81
- """
82
- return bool(pgc.dataset_info_is_batch(self._info))
83
-
84
- def batch_size(self) -> int:
85
- """
86
- The size of the dataset.
87
-
88
- Returns:
89
- The size of the dataset
90
- """
91
- return pgc.dataset_info_batch_size(self._info)
92
-
93
- def n_components(self) -> int:
94
- """
95
- The amount of components in the dataset.
96
-
97
- Returns:
98
- The amount of components in the dataset
99
- """
100
- return pgc.dataset_info_n_components(self._info)
101
-
102
- def components(self) -> list[ComponentType]:
103
- """
104
- The components in the dataset.
105
-
106
- Returns:
107
- A list of the component names in the dataset
108
- """
109
- return [
110
- _str_to_component_type(pgc.dataset_info_component_name(self._info, idx))
111
- for idx in range(self.n_components())
112
- ]
113
-
114
- def elements_per_scenario(self) -> Mapping[ComponentType, int]:
115
- """
116
- The number of elements per scenario in the dataset.
117
-
118
- Returns:
119
- The number of elements per senario for each component in the dataset;
120
- or -1 if the scenario is not uniform (different amount per scenario)
121
- """
122
- return {
123
- component_name: pgc.dataset_info_elements_per_scenario(self._info, idx)
124
- for idx, component_name in enumerate(self.components())
125
- }
126
-
127
- def total_elements(self) -> Mapping[ComponentType, int]:
128
- """
129
- The total number of elements in the dataset.
130
-
131
- Returns:
132
- The total number of elements for each component.
133
- For each component, if the number of elements per scenario is uniform, its value shall be equal to
134
- the product of the batch size and the amount of elements per scenario for that component.
135
- """
136
- return {
137
- component_name: pgc.dataset_info_total_elements(self._info, idx)
138
- for idx, component_name in enumerate(self.components())
139
- }
140
-
141
- def attribute_indications(self) -> Mapping[ComponentType, None | list[AttributeType]]:
142
- """
143
- The attribute indications in the dataset.
144
-
145
- Returns:
146
- A map of component to its attribute indications.
147
- None means no attribute indications
148
- """
149
- result_dict: dict[ComponentType, None | list[AttributeType]] = {}
150
- components = self.components()
151
- for component_idx, component_name in enumerate(components):
152
- has_indications = pgc.dataset_info_has_attribute_indications(self._info, component_idx)
153
- if has_indications == 0:
154
- result_dict[component_name] = None
155
- else:
156
- n_indications = pgc.dataset_info_n_attribute_indications(self._info, component_idx)
157
- result_dict[component_name] = [
158
- pgc.dataset_info_attribute_name(self._info, component_idx, attribute_idx)
159
- for attribute_idx in range(n_indications)
160
- ]
161
- return result_dict
162
-
163
-
164
- class CMutableDataset:
165
- """
166
- A view of a user-owned dataset.
167
-
168
- This may be used to provide a user dataset to the Power Grid Model.
169
-
170
- The dataset will create mutable buffers that the Power Grid Model can use to load data.
171
- """
172
-
173
- _dataset_type: DatasetType
174
- _schema: DatasetMetaData
175
- _is_batch: bool
176
- _batch_size: int
177
- _mutable_dataset: MutableDatasetPtr
178
- _buffer_views: list[CBuffer]
179
-
180
- def __new__(cls, data: Dataset, dataset_type: Any = None):
181
- instance = super().__new__(cls)
182
- instance._mutable_dataset = MutableDatasetPtr()
183
- instance._buffer_views = []
184
-
185
- instance._dataset_type = dataset_type if dataset_type in DatasetType else get_dataset_type(data)
186
- instance._schema = power_grid_meta_data[instance._dataset_type]
187
-
188
- if data:
189
- first_component, first_component_data = next(iter(data.items()))
190
- first_sub_info = get_buffer_properties(data=first_component_data, schema=instance._schema[first_component])
191
- instance._is_batch = first_sub_info.is_batch
192
- instance._batch_size = first_sub_info.batch_size
193
- else:
194
- instance._is_batch = False
195
- instance._batch_size = 1
196
-
197
- instance._mutable_dataset = pgc.create_dataset_mutable(
198
- instance._dataset_type.value, instance._is_batch, instance._batch_size
199
- )
200
- assert_no_error()
201
-
202
- instance._add_data(data)
203
- assert_no_error()
204
-
205
- return instance
206
-
207
- def get_dataset_ptr(self) -> MutableDatasetPtr:
208
- """
209
- Get the raw underlying mutable dataset pointer.
210
-
211
- Returns:
212
- MutableDatasetPtr: the raw underlying mutable dataset pointer.
213
- """
214
- return self._mutable_dataset
215
-
216
- def get_info(self) -> CDatasetInfo:
217
- """
218
- Get the info for this dataset.
219
-
220
- Returns:
221
- The dataset info for this dataset.
222
- """
223
- return CDatasetInfo(pgc.dataset_mutable_get_info(self._mutable_dataset))
224
-
225
- def get_buffer_views(self) -> list[CBuffer]:
226
- """
227
- Get list of buffer views
228
-
229
- Returns:
230
- list of buffer view
231
- """
232
- return self._buffer_views
233
-
234
- def _add_data(self, data: Dataset):
235
- """
236
- Add Power Grid Model data to the mutable dataset view.
237
-
238
- Args:
239
- data: the data.
240
-
241
- Raises:
242
- ValueError: if the component is unknown and allow_unknown is False.
243
- ValueError: if the data is inconsistent with the rest of the dataset.
244
- PowerGridError: if there was an internal error.
245
- """
246
- for component, component_data in data.items():
247
- self._add_component_data(component, component_data, allow_unknown=False)
248
-
249
- def _add_component_data(self, component: ComponentType, data: ComponentData, allow_unknown: bool = False):
250
- """
251
- Add Power Grid Model data for a single component to the mutable dataset view.
252
-
253
- Args:
254
- component: the name of the component
255
- data: the data of the component
256
- allow_unknown (optional): ignore any unknown components. Defaults to False.
257
-
258
- Raises:
259
- ValueError: if the component is unknown and allow_unknown is False.
260
- ValueError: if the data is inconsistent with the rest of the dataset.
261
- PowerGridError: if there was an internal error.
262
- """
263
- if component not in self._schema:
264
- if not allow_unknown:
265
- raise ValueError(f"Unknown component {component} in schema. {VALIDATOR_MSG}")
266
- return
267
-
268
- self._validate_properties(data, self._schema[component])
269
- c_buffer = get_buffer_view(data, self._schema[component], self._is_batch, self._batch_size)
270
- self._buffer_views.append(c_buffer)
271
- self._register_buffer(component, c_buffer)
272
-
273
- def _register_buffer(self, component: ComponentType, buffer: CBuffer):
274
- pgc.dataset_mutable_add_buffer(
275
- dataset=self._mutable_dataset,
276
- component=component.value,
277
- elements_per_scenario=buffer.n_elements_per_scenario,
278
- total_elements=buffer.total_elements,
279
- indptr=buffer.indptr,
280
- data=buffer.data,
281
- )
282
- assert_no_error()
283
- for attr, attr_data in buffer.attribute_data.items():
284
- self._register_attribute_buffer(component, attr, attr_data)
285
-
286
- def _register_attribute_buffer(self, component, attr, attr_data):
287
- pgc.dataset_mutable_add_attribute_buffer(
288
- dataset=self._mutable_dataset,
289
- component=component.value,
290
- attribute=attr,
291
- data=attr_data.data,
292
- )
293
- assert_no_error()
294
-
295
- def _validate_properties(self, data: ComponentData, schema: ComponentMetaData):
296
- properties = get_buffer_properties(data, schema=schema, is_batch=self._is_batch, batch_size=self._batch_size)
297
- if properties.is_batch != self._is_batch:
298
- raise ValueError(
299
- f"Dataset type (single or batch) must be consistent across all components. {VALIDATOR_MSG}"
300
- )
301
- if properties.batch_size != self._batch_size:
302
- raise ValueError(f"Dataset must have a consistent batch size across all components. {VALIDATOR_MSG}")
303
-
304
- def __del__(self):
305
- pgc.destroy_dataset_mutable(self._mutable_dataset)
306
-
307
-
308
- class CConstDataset:
309
- """
310
- A view of a user-owned dataset.
311
-
312
- This may be used to provide a user dataset to the Power Grid Model.
313
-
314
- The dataset will create const buffers that the Power Grid Model can use to load data.
315
-
316
- It is created from mutable dataset.
317
- """
318
-
319
- _const_dataset: ConstDatasetPtr
320
- _buffer_views: list[CBuffer]
321
-
322
- def __new__(cls, data: Dataset, dataset_type: DatasetType | None = None):
323
- instance = super().__new__(cls)
324
- instance._const_dataset = ConstDatasetPtr()
325
-
326
- # create from mutable dataset
327
- mutable_dataset = CMutableDataset(data=data, dataset_type=dataset_type)
328
- instance._const_dataset = pgc.create_dataset_const_from_mutable(mutable_dataset.get_dataset_ptr())
329
- assert_no_error()
330
- instance._buffer_views = mutable_dataset.get_buffer_views()
331
-
332
- return instance
333
-
334
- def get_dataset_ptr(self) -> ConstDatasetPtr:
335
- """
336
- Get the raw underlying const dataset pointer.
337
-
338
- Returns:
339
- ConstDatasetPtr: the raw underlying const dataset pointer.
340
- """
341
- return self._const_dataset
342
-
343
- def get_info(self) -> CDatasetInfo:
344
- """
345
- Get the info for this dataset.
346
-
347
- Returns:
348
- The dataset info for this dataset.
349
- """
350
- return CDatasetInfo(pgc.dataset_const_get_info(self._const_dataset))
351
-
352
- def __del__(self):
353
- pgc.destroy_dataset_const(self._const_dataset)
354
-
355
-
356
- class CWritableDataset:
357
- """
358
- A view of a Power Grid Model-owned dataset.
359
-
360
- This may be used to retrieve data from the Power Grid Model.
361
-
362
- This class provides buffers to which the Power Grid Model can write data in an external call.
363
- After writing to the buffers, the data contents can be retrieved.
364
- """
365
-
366
- def __init__(self, dataset_ptr: WritableDatasetPtr, data_filter: ComponentAttributeMapping):
367
- self._writable_dataset = dataset_ptr
368
-
369
- info = self.get_info()
370
- self._dataset_type = info.dataset_type()
371
- self._schema = power_grid_meta_data[self._dataset_type]
372
-
373
- self._data_filter = process_data_filter(
374
- dataset_type=info.dataset_type(),
375
- data_filter=data_filter,
376
- available_components=info.components(),
377
- )
378
-
379
- self._data: Dataset = {}
380
- self._buffers: Mapping[str, CBuffer] = {}
381
- self._component_buffer_properties = self._get_buffer_properties(info)
382
-
383
- self._add_buffers()
384
- assert_no_error()
385
-
386
- def get_dataset_ptr(self) -> WritableDatasetPtr:
387
- """
388
- Get the raw underlying writable dataset pointer.
389
-
390
- Returns:
391
- WritableDatasetPtr: the raw underlying writable dataset pointer.
392
- """
393
- return self._writable_dataset
394
-
395
- def get_info(self) -> CDatasetInfo:
396
- """
397
- Get the info for this dataset.
398
-
399
- Returns:
400
- The dataset info for this dataset.
401
- """
402
- return CDatasetInfo(pgc.dataset_writable_get_info(self._writable_dataset))
403
-
404
- def get_data(self) -> Dataset:
405
- """
406
- Retrieve data from the Power Grid Model dataset.
407
-
408
- The Power Grid Model may write to these buffers at a later point in time.
409
-
410
- Returns:
411
- The full dataset with filters applied.
412
- """
413
- self._post_filtering()
414
- return self._data
415
-
416
- def get_component_data(self, component: ComponentType) -> ComponentData:
417
- """
418
- Retrieve Power Grid Model data from the dataset for a specific component.
419
-
420
- Args:
421
- component: the component.
422
-
423
- Returns:
424
- The dataset for the specified component.
425
- """
426
- return self._data[component]
427
-
428
- def get_data_filter(self) -> _ComponentAttributeMappingDict:
429
- """Gets the data filter requested
430
-
431
- Returns:
432
- _ComponentAttributeMappingDict: data filter
433
- """
434
- return self._data_filter
435
-
436
- def _add_buffers(self):
437
- for component, buffer_properties in self._component_buffer_properties.items():
438
- self._add_buffer(component, buffer_properties)
439
-
440
- def _add_buffer(self, component: ComponentType, buffer_properties: BufferProperties):
441
- schema = self._schema[component]
442
-
443
- self._data[component] = create_buffer(buffer_properties, schema)
444
- self._register_buffer(component, get_buffer_view(self._data[component], schema))
445
-
446
- def _register_buffer(self, component: ComponentType, buffer: CBuffer):
447
- pgc.dataset_writable_set_buffer(
448
- dataset=self._writable_dataset,
449
- component=component,
450
- indptr=buffer.indptr,
451
- data=buffer.data,
452
- )
453
- assert_no_error()
454
- for attribute, attribute_data in buffer.attribute_data.items():
455
- self._register_attribute_buffer(component, attribute, attribute_data)
456
-
457
- def _register_attribute_buffer(
458
- self,
459
- component: ComponentType,
460
- attribute: AttributeType,
461
- buffer: CAttributeBuffer,
462
- ):
463
- pgc.dataset_writable_set_attribute_buffer(
464
- dataset=self._writable_dataset,
465
- component=component,
466
- attribute=attribute,
467
- data=buffer.data,
468
- )
469
- assert_no_error()
470
-
471
- def _get_buffer_properties(self, info: CDatasetInfo) -> Mapping[ComponentType, BufferProperties]:
472
- is_batch = info.is_batch()
473
- batch_size = info.batch_size()
474
- components = info.components()
475
- n_elements_per_scenario = info.elements_per_scenario()
476
- n_total_elements = info.total_elements()
477
- attribute_indications = info.attribute_indications()
478
-
479
- return {
480
- component: BufferProperties(
481
- is_sparse=n_elements_per_scenario[component] == -1,
482
- is_batch=is_batch,
483
- batch_size=batch_size,
484
- n_elements_per_scenario=n_elements_per_scenario[component],
485
- n_total_elements=n_total_elements[component],
486
- columns=_get_filtered_attributes(
487
- schema=self._schema[component],
488
- component_data_filter=self._data_filter[component],
489
- attribute_indication=attribute_indications[component],
490
- ),
491
- )
492
- for component in components
493
- if component in self._data_filter
494
- }
495
-
496
- def _filter_attributes(self, buffer: ColumnarData):
497
- if is_sparse(buffer):
498
- attributes = cast(SparseBatchColumnarData, buffer)["data"]
499
- else:
500
- attributes = cast(SingleColumnarData | DenseBatchColumnarData, buffer)
501
-
502
- keys_to_remove = []
503
- for attr, array in attributes.items():
504
- if is_nan_or_equivalent(array):
505
- keys_to_remove.append(attr)
506
- for key in keys_to_remove:
507
- del attributes[key]
508
-
509
- def _filter_with_mapping(self):
510
- for component_type, component_buffer in self._data.items():
511
- if component_type in self._data_filter:
512
- filter_option = self._data_filter[component_type]
513
- if filter_option is ComponentAttributeFilterOptions.relevant and is_columnar(component_buffer):
514
- self._filter_attributes(component_buffer)
515
-
516
- def _post_filtering(self):
517
- if isinstance(self._data_filter, dict):
518
- self._filter_with_mapping()
519
-
520
-
521
- def _get_filtered_attributes(
522
- schema: ComponentMetaData,
523
- component_data_filter: set[str] | list[str] | None | ComponentAttributeFilterOptions,
524
- attribute_indication: None | list[AttributeType],
525
- ) -> list[AttributeType] | None:
526
- if component_data_filter is None:
527
- return None
528
-
529
- if isinstance(component_data_filter, ComponentAttributeFilterOptions):
530
- if component_data_filter == ComponentAttributeFilterOptions.relevant and attribute_indication is not None:
531
- return attribute_indication
532
- return [] if schema.dtype.names is None else list(schema.dtype.names)
533
-
534
- return list(component_data_filter)
1
+ # SPDX-FileCopyrightText: Contributors to the Power Grid Model project <powergridmodel@lfenergy.org>
2
+ #
3
+ # SPDX-License-Identifier: MPL-2.0
4
+
5
+ """
6
+ Power grid model raw dataset handler
7
+ """
8
+
9
+ from collections.abc import Mapping
10
+ from typing import Any, cast
11
+
12
+ from power_grid_model._core.buffer_handling import (
13
+ BufferProperties,
14
+ CAttributeBuffer,
15
+ CBuffer,
16
+ create_buffer,
17
+ get_buffer_properties,
18
+ get_buffer_view,
19
+ )
20
+ from power_grid_model._core.data_types import (
21
+ AttributeType,
22
+ ColumnarData,
23
+ ComponentData,
24
+ Dataset,
25
+ DenseBatchColumnarData,
26
+ SingleColumnarData,
27
+ SparseBatchColumnarData,
28
+ )
29
+ from power_grid_model._core.dataset_definitions import ComponentType, DatasetType, _str_to_component_type
30
+ from power_grid_model._core.enum import ComponentAttributeFilterOptions
31
+ from power_grid_model._core.error_handling import VALIDATOR_MSG, assert_no_error
32
+ from power_grid_model._core.power_grid_core import (
33
+ ConstDatasetPtr,
34
+ DatasetInfoPtr,
35
+ MutableDatasetPtr,
36
+ WritableDatasetPtr,
37
+ power_grid_core as pgc,
38
+ )
39
+ from power_grid_model._core.power_grid_meta import ComponentMetaData, DatasetMetaData, power_grid_meta_data
40
+ from power_grid_model._core.typing import ComponentAttributeMapping, ComponentAttributeMappingDict
41
+ from power_grid_model._core.utils import (
42
+ get_dataset_type,
43
+ is_columnar,
44
+ is_nan_or_equivalent,
45
+ is_sparse,
46
+ process_data_filter,
47
+ )
48
+
49
+
50
+ class CDatasetInfo:
51
+ """
52
+ Raw Power Grid Model dataset info.
53
+ """
54
+
55
+ def __init__(self, info: DatasetInfoPtr):
56
+ self._info: DatasetInfoPtr = info
57
+
58
+ def name(self) -> str:
59
+ """
60
+ The name of the dataset type.
61
+
62
+ Returns:
63
+ The name of the dataset type
64
+ """
65
+ return pgc.dataset_info_name(self._info)
66
+
67
+ def dataset_type(self):
68
+ """
69
+ The name of the dataset type.
70
+
71
+ Returns:
72
+ The name of the dataset type
73
+ """
74
+ return self.name()
75
+
76
+ def is_batch(self) -> bool:
77
+ """
78
+ Whether the dataset is a batch dataset.
79
+
80
+ Returns:
81
+ Whether the dataset is a batch dataset
82
+ """
83
+ return bool(pgc.dataset_info_is_batch(self._info))
84
+
85
+ def batch_size(self) -> int:
86
+ """
87
+ The size of the dataset.
88
+
89
+ Returns:
90
+ The size of the dataset
91
+ """
92
+ return pgc.dataset_info_batch_size(self._info)
93
+
94
+ def n_components(self) -> int:
95
+ """
96
+ The amount of components in the dataset.
97
+
98
+ Returns:
99
+ The amount of components in the dataset
100
+ """
101
+ return pgc.dataset_info_n_components(self._info)
102
+
103
+ def components(self) -> list[ComponentType]:
104
+ """
105
+ The components in the dataset.
106
+
107
+ Returns:
108
+ A list of the component names in the dataset
109
+ """
110
+ return [
111
+ _str_to_component_type(pgc.dataset_info_component_name(self._info, idx))
112
+ for idx in range(self.n_components())
113
+ ]
114
+
115
+ def elements_per_scenario(self) -> Mapping[ComponentType, int]:
116
+ """
117
+ The number of elements per scenario in the dataset.
118
+
119
+ Returns:
120
+ The number of elements per senario for each component in the dataset;
121
+ or -1 if the scenario is not uniform (different amount per scenario)
122
+ """
123
+ return {
124
+ component_name: pgc.dataset_info_elements_per_scenario(self._info, idx)
125
+ for idx, component_name in enumerate(self.components())
126
+ }
127
+
128
+ def total_elements(self) -> Mapping[ComponentType, int]:
129
+ """
130
+ The total number of elements in the dataset.
131
+
132
+ Returns:
133
+ The total number of elements for each component.
134
+ For each component, if the number of elements per scenario is uniform, its value shall be equal to
135
+ the product of the batch size and the amount of elements per scenario for that component.
136
+ """
137
+ return {
138
+ component_name: pgc.dataset_info_total_elements(self._info, idx)
139
+ for idx, component_name in enumerate(self.components())
140
+ }
141
+
142
+ def attribute_indications(self) -> Mapping[ComponentType, None | list[AttributeType]]:
143
+ """
144
+ The attribute indications in the dataset.
145
+
146
+ Returns:
147
+ A map of component to its attribute indications.
148
+ None means no attribute indications
149
+ """
150
+ result_dict: dict[ComponentType, None | list[AttributeType]] = {}
151
+ components = self.components()
152
+ for component_idx, component_name in enumerate(components):
153
+ has_indications = pgc.dataset_info_has_attribute_indications(self._info, component_idx)
154
+ if has_indications == 0:
155
+ result_dict[component_name] = None
156
+ else:
157
+ n_indications = pgc.dataset_info_n_attribute_indications(self._info, component_idx)
158
+ result_dict[component_name] = [
159
+ pgc.dataset_info_attribute_name(self._info, component_idx, attribute_idx)
160
+ for attribute_idx in range(n_indications)
161
+ ]
162
+ return result_dict
163
+
164
+
165
+ class CMutableDataset:
166
+ """
167
+ A view of a user-owned dataset.
168
+
169
+ This may be used to provide a user dataset to the Power Grid Model.
170
+
171
+ The dataset will create mutable buffers that the Power Grid Model can use to load data.
172
+ """
173
+
174
+ _dataset_type: DatasetType
175
+ _schema: DatasetMetaData
176
+ _is_batch: bool
177
+ _batch_size: int
178
+ _mutable_dataset: MutableDatasetPtr
179
+ _buffer_views: list[CBuffer]
180
+
181
+ def __new__(cls, data: Dataset, dataset_type: Any = None):
182
+ instance = super().__new__(cls)
183
+ instance._mutable_dataset = MutableDatasetPtr()
184
+ instance._buffer_views = []
185
+
186
+ instance._dataset_type = dataset_type if dataset_type in DatasetType else get_dataset_type(data)
187
+ instance._schema = power_grid_meta_data[instance._dataset_type]
188
+
189
+ if data:
190
+ first_component, first_component_data = next(iter(data.items()))
191
+ first_sub_info = get_buffer_properties(data=first_component_data, schema=instance._schema[first_component])
192
+ instance._is_batch = first_sub_info.is_batch
193
+ instance._batch_size = first_sub_info.batch_size
194
+ else:
195
+ instance._is_batch = False
196
+ instance._batch_size = 1
197
+
198
+ instance._mutable_dataset = pgc.create_dataset_mutable(
199
+ instance._dataset_type.value, instance._is_batch, instance._batch_size
200
+ )
201
+ assert_no_error()
202
+
203
+ instance._add_data(data)
204
+ assert_no_error()
205
+
206
+ return instance
207
+
208
+ def get_dataset_ptr(self) -> MutableDatasetPtr:
209
+ """
210
+ Get the raw underlying mutable dataset pointer.
211
+
212
+ Returns:
213
+ MutableDatasetPtr: the raw underlying mutable dataset pointer.
214
+ """
215
+ return self._mutable_dataset
216
+
217
+ def get_info(self) -> CDatasetInfo:
218
+ """
219
+ Get the info for this dataset.
220
+
221
+ Returns:
222
+ The dataset info for this dataset.
223
+ """
224
+ return CDatasetInfo(pgc.dataset_mutable_get_info(self._mutable_dataset))
225
+
226
+ def get_buffer_views(self) -> list[CBuffer]:
227
+ """
228
+ Get list of buffer views
229
+
230
+ Returns:
231
+ list of buffer view
232
+ """
233
+ return self._buffer_views
234
+
235
+ def _add_data(self, data: Dataset):
236
+ """
237
+ Add Power Grid Model data to the mutable dataset view.
238
+
239
+ Args:
240
+ data: the data.
241
+
242
+ Raises:
243
+ ValueError: if the component is unknown.
244
+ ValueError: if the data is inconsistent with the rest of the dataset.
245
+ PowerGridError: if there was an internal error.
246
+ """
247
+ for component, component_data in data.items():
248
+ self._add_component_data(component, component_data, allow_unknown=False)
249
+
250
+ def _add_component_data(self, component: ComponentType, data: ComponentData, allow_unknown: bool = False):
251
+ """
252
+ Add Power Grid Model data for a single component to the mutable dataset view.
253
+
254
+ Args:
255
+ component: the name of the component
256
+ data: the data of the component
257
+ allow_unknown (optional): ignore any unknown components. Defaults to False.
258
+
259
+ Raises:
260
+ ValueError: if the component is unknown and allow_unknown is False.
261
+ ValueError: if the data is inconsistent with the rest of the dataset.
262
+ PowerGridError: if there was an internal error.
263
+ """
264
+ if component not in self._schema:
265
+ if not allow_unknown:
266
+ raise ValueError(f"Unknown component {component} in schema. {VALIDATOR_MSG}")
267
+ return
268
+
269
+ self._validate_properties(data, self._schema[component])
270
+ c_buffer = get_buffer_view(data, self._schema[component], self._is_batch, self._batch_size)
271
+ self._buffer_views.append(c_buffer)
272
+ self._register_buffer(component, c_buffer)
273
+
274
+ def _register_buffer(self, component: ComponentType, buffer: CBuffer):
275
+ pgc.dataset_mutable_add_buffer(
276
+ dataset=self._mutable_dataset,
277
+ component=component.value,
278
+ elements_per_scenario=buffer.n_elements_per_scenario,
279
+ total_elements=buffer.total_elements,
280
+ indptr=buffer.indptr,
281
+ data=buffer.data,
282
+ )
283
+ assert_no_error()
284
+ for attr, attr_data in buffer.attribute_data.items():
285
+ self._register_attribute_buffer(component, attr, attr_data)
286
+
287
+ def _register_attribute_buffer(self, component, attr, attr_data):
288
+ pgc.dataset_mutable_add_attribute_buffer(
289
+ dataset=self._mutable_dataset,
290
+ component=component.value,
291
+ attribute=attr,
292
+ data=attr_data.data,
293
+ )
294
+ assert_no_error()
295
+
296
+ def _validate_properties(self, data: ComponentData, schema: ComponentMetaData):
297
+ properties = get_buffer_properties(data, schema=schema, is_batch=None, batch_size=None)
298
+ if properties.is_batch != self._is_batch:
299
+ raise ValueError(
300
+ f"Dataset type (single or batch) must be consistent across all components. {VALIDATOR_MSG}"
301
+ )
302
+ if properties.batch_size != self._batch_size:
303
+ raise ValueError(f"Dataset must have a consistent batch size across all components. {VALIDATOR_MSG}")
304
+
305
+ def __del__(self):
306
+ pgc.destroy_dataset_mutable(self._mutable_dataset)
307
+
308
+
309
+ class CConstDataset:
310
+ """
311
+ A view of a user-owned dataset.
312
+
313
+ This may be used to provide a user dataset to the Power Grid Model.
314
+
315
+ The dataset will create const buffers that the Power Grid Model can use to load data.
316
+
317
+ It is created from mutable dataset.
318
+ """
319
+
320
+ _const_dataset: ConstDatasetPtr
321
+ _buffer_views: list[CBuffer]
322
+
323
+ def __new__(cls, data: Dataset, dataset_type: DatasetType | None = None):
324
+ instance = super().__new__(cls)
325
+ instance._const_dataset = ConstDatasetPtr()
326
+
327
+ # create from mutable dataset
328
+ mutable_dataset = CMutableDataset(data=data, dataset_type=dataset_type)
329
+ instance._const_dataset = pgc.create_dataset_const_from_mutable(mutable_dataset.get_dataset_ptr())
330
+ assert_no_error()
331
+ instance._buffer_views = mutable_dataset.get_buffer_views()
332
+
333
+ return instance
334
+
335
+ def get_dataset_ptr(self) -> ConstDatasetPtr:
336
+ """
337
+ Get the raw underlying const dataset pointer.
338
+
339
+ Returns:
340
+ ConstDatasetPtr: the raw underlying const dataset pointer.
341
+ """
342
+ return self._const_dataset
343
+
344
+ def get_info(self) -> CDatasetInfo:
345
+ """
346
+ Get the info for this dataset.
347
+
348
+ Returns:
349
+ The dataset info for this dataset.
350
+ """
351
+ return CDatasetInfo(pgc.dataset_const_get_info(self._const_dataset))
352
+
353
+ def __del__(self):
354
+ pgc.destroy_dataset_const(self._const_dataset)
355
+
356
+
357
+ class CWritableDataset:
358
+ """
359
+ A view of a Power Grid Model-owned dataset.
360
+
361
+ This may be used to retrieve data from the Power Grid Model.
362
+
363
+ This class provides buffers to which the Power Grid Model can write data in an external call.
364
+ After writing to the buffers, the data contents can be retrieved.
365
+ """
366
+
367
+ def __init__(self, dataset_ptr: WritableDatasetPtr, data_filter: ComponentAttributeMapping):
368
+ self._writable_dataset = dataset_ptr
369
+
370
+ info = self.get_info()
371
+ self._dataset_type = info.dataset_type()
372
+ self._schema = power_grid_meta_data[self._dataset_type]
373
+
374
+ self._data_filter = process_data_filter(
375
+ dataset_type=info.dataset_type(),
376
+ data_filter=data_filter,
377
+ available_components=info.components(),
378
+ )
379
+
380
+ self._data: Dataset = {}
381
+ self._buffers: Mapping[str, CBuffer] = {}
382
+ self._component_buffer_properties = self._get_buffer_properties(info)
383
+
384
+ self._add_buffers()
385
+ assert_no_error()
386
+
387
+ def get_dataset_ptr(self) -> WritableDatasetPtr:
388
+ """
389
+ Get the raw underlying writable dataset pointer.
390
+
391
+ Returns:
392
+ WritableDatasetPtr: the raw underlying writable dataset pointer.
393
+ """
394
+ return self._writable_dataset
395
+
396
+ def get_info(self) -> CDatasetInfo:
397
+ """
398
+ Get the info for this dataset.
399
+
400
+ Returns:
401
+ The dataset info for this dataset.
402
+ """
403
+ return CDatasetInfo(pgc.dataset_writable_get_info(self._writable_dataset))
404
+
405
+ def get_data(self) -> Dataset:
406
+ """
407
+ Retrieve data from the Power Grid Model dataset.
408
+
409
+ The Power Grid Model may write to these buffers at a later point in time.
410
+
411
+ Returns:
412
+ The full dataset with filters applied.
413
+ """
414
+ self._post_filtering()
415
+ return self._data
416
+
417
+ def get_component_data(self, component: ComponentType) -> ComponentData:
418
+ """
419
+ Retrieve Power Grid Model data from the dataset for a specific component.
420
+
421
+ Args:
422
+ component: the component.
423
+
424
+ Returns:
425
+ The dataset for the specified component.
426
+ """
427
+ return self._data[component]
428
+
429
+ def get_data_filter(self) -> ComponentAttributeMappingDict:
430
+ """Gets the data filter requested
431
+
432
+ Returns:
433
+ ComponentAttributeMappingDict: data filter
434
+ """
435
+ return self._data_filter
436
+
437
+ def _add_buffers(self):
438
+ for component, buffer_properties in self._component_buffer_properties.items():
439
+ self._add_buffer(component, buffer_properties)
440
+
441
+ def _add_buffer(self, component: ComponentType, buffer_properties: BufferProperties):
442
+ schema = self._schema[component]
443
+
444
+ self._data[component] = create_buffer(buffer_properties, schema)
445
+ self._register_buffer(component, get_buffer_view(self._data[component], schema))
446
+
447
+ def _register_buffer(self, component: ComponentType, buffer: CBuffer):
448
+ pgc.dataset_writable_set_buffer(
449
+ dataset=self._writable_dataset,
450
+ component=component,
451
+ indptr=buffer.indptr,
452
+ data=buffer.data,
453
+ )
454
+ assert_no_error()
455
+ for attribute, attribute_data in buffer.attribute_data.items():
456
+ self._register_attribute_buffer(component, attribute, attribute_data)
457
+
458
+ def _register_attribute_buffer(
459
+ self,
460
+ component: ComponentType,
461
+ attribute: AttributeType,
462
+ buffer: CAttributeBuffer,
463
+ ):
464
+ pgc.dataset_writable_set_attribute_buffer(
465
+ dataset=self._writable_dataset,
466
+ component=component,
467
+ attribute=attribute,
468
+ data=buffer.data,
469
+ )
470
+ assert_no_error()
471
+
472
+ def _get_buffer_properties(self, info: CDatasetInfo) -> Mapping[ComponentType, BufferProperties]:
473
+ is_batch = info.is_batch()
474
+ batch_size = info.batch_size()
475
+ components = info.components()
476
+ n_elements_per_scenario = info.elements_per_scenario()
477
+ n_total_elements = info.total_elements()
478
+ attribute_indications = info.attribute_indications()
479
+
480
+ return {
481
+ component: BufferProperties(
482
+ is_sparse=n_elements_per_scenario[component] == -1,
483
+ is_batch=is_batch,
484
+ batch_size=batch_size,
485
+ n_elements_per_scenario=n_elements_per_scenario[component],
486
+ n_total_elements=n_total_elements[component],
487
+ columns=_get_filtered_attributes(
488
+ schema=self._schema[component],
489
+ component_data_filter=self._data_filter[component],
490
+ attribute_indication=attribute_indications[component],
491
+ ),
492
+ )
493
+ for component in components
494
+ if component in self._data_filter
495
+ }
496
+
497
+ def _filter_attributes(self, buffer: ColumnarData):
498
+ if is_sparse(buffer):
499
+ attributes = cast(SparseBatchColumnarData, buffer)["data"]
500
+ else:
501
+ attributes = cast(SingleColumnarData | DenseBatchColumnarData, buffer)
502
+
503
+ keys_to_remove = []
504
+ for attr, array in attributes.items():
505
+ if is_nan_or_equivalent(array):
506
+ keys_to_remove.append(attr)
507
+ for key in keys_to_remove:
508
+ del attributes[key]
509
+
510
+ def _filter_with_mapping(self):
511
+ for component_type, component_buffer in self._data.items():
512
+ if component_type in self._data_filter:
513
+ filter_option = self._data_filter[component_type]
514
+ if filter_option is ComponentAttributeFilterOptions.relevant and is_columnar(component_buffer):
515
+ self._filter_attributes(component_buffer)
516
+
517
+ def _post_filtering(self):
518
+ if isinstance(self._data_filter, dict):
519
+ self._filter_with_mapping()
520
+
521
+
522
+ def _get_filtered_attributes(
523
+ schema: ComponentMetaData,
524
+ component_data_filter: set[str] | list[str] | None | ComponentAttributeFilterOptions,
525
+ attribute_indication: None | list[AttributeType],
526
+ ) -> list[AttributeType] | None:
527
+ if component_data_filter is None:
528
+ return None
529
+
530
+ if isinstance(component_data_filter, ComponentAttributeFilterOptions):
531
+ if component_data_filter == ComponentAttributeFilterOptions.relevant and attribute_indication is not None:
532
+ return attribute_indication
533
+ return [] if schema.dtype.names is None else list(schema.dtype.names)
534
+
535
+ return list(component_data_filter)