|
| 1 | +# SPDX-FileCopyrightText: Contributors to the Power Grid Model project <powergridmodel@lfenergy.org> |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MPL-2.0 |
| 4 | + |
| 5 | +""" |
| 6 | +Data types involving PGM datasets. |
| 7 | +
|
| 8 | +Data types for library-internal use. In an attempt to clarify type hints, some types |
| 9 | +have been defined and explained in this file. |
| 10 | +""" |
| 11 | + |
| 12 | +from typing import TypeAlias, TypedDict, TypeVar |
| 13 | + |
| 14 | +import numpy as np |
| 15 | + |
| 16 | +from power_grid_model._core.dataset_definitions import ComponentTypeVar |
| 17 | + |
| 18 | +SingleArray: TypeAlias = np.ndarray |
| 19 | + |
| 20 | +AttributeType: TypeAlias = str |
| 21 | + |
| 22 | +SingleColumn: TypeAlias = np.ndarray |
| 23 | + |
| 24 | +DenseBatchArray: TypeAlias = np.ndarray |
| 25 | + |
| 26 | +SingleColumnarData = dict[AttributeType, SingleColumn] |
| 27 | + |
| 28 | +_SingleComponentData = TypeVar("_SingleComponentData", SingleArray, SingleColumnarData) # deduction helper |
| 29 | +SingleComponentData = SingleArray | SingleColumnarData |
| 30 | + |
| 31 | + |
| 32 | +SingleDataset = dict[ComponentTypeVar, _SingleComponentData] |
| 33 | + |
| 34 | +BatchList = list[SingleDataset] |
| 35 | + |
| 36 | +BatchColumn: TypeAlias = np.ndarray |
| 37 | + |
| 38 | +DenseBatchColumnarData = dict[AttributeType, BatchColumn] |
| 39 | + |
| 40 | +IndexPointer: TypeAlias = np.ndarray |
| 41 | + |
| 42 | + |
| 43 | +class SparseBatchColumnarData(TypedDict): |
| 44 | + """ |
| 45 | + Sparse batch columnar data is a dictionary containing the keys `indptr` and `data`. |
| 46 | +
|
| 47 | + - data: a :class:`SingleColumnarData`. The exact supported attribute columns depend on the component type. |
| 48 | + - indptr: an :class:`IndexPointer` representing the start and end indices for each batch scenario. |
| 49 | +
|
| 50 | + - Examples: |
| 51 | +
|
| 52 | + - structure: {"indptr": :class:`IndexPointer`, "data": :class:`SingleColumnarData`} |
| 53 | + - concrete example: {"indptr": [0, 2, 2, 3], "data": {"id": [0, 1, 0], "status": [1, 1, 0]}} |
| 54 | +
|
| 55 | + - the scenario 0 sets the status of components with ids 0 and 1 to 1 |
| 56 | + (and keeps defaults for other components) |
| 57 | + - scenario 1 keeps the default values for all components |
| 58 | + - scenario 2 sets the status of component with id 0 to 0 (and keeps defaults for other components) |
| 59 | + """ |
| 60 | + |
| 61 | + indptr: IndexPointer |
| 62 | + data: SingleColumnarData |
| 63 | + |
| 64 | + |
| 65 | +class SparseBatchArray(TypedDict): |
| 66 | + """ |
| 67 | + A sparse batch array is a dictionary containing the keys `indptr` and `data`. |
| 68 | +
|
| 69 | + - data: a :class:`SingleArray`. The exact dtype depends on the type of component. |
| 70 | + - indptr: an :class:`IndexPointer` representing the start and end indices for each batch scenario. |
| 71 | +
|
| 72 | + - Examples: |
| 73 | +
|
| 74 | + - structure: {"indptr": :class:`IndexPointer`, "data": :class:`SingleArray`} |
| 75 | + - concrete example: {"indptr": [0, 2, 2, 3], "data": [(0, 1, 1), (1, 1, 1), (0, 0, 0)]} |
| 76 | +
|
| 77 | + - the scenario 0 sets the statuses of components with ids 0 and 1 to 1 |
| 78 | + (and keeps defaults for other components) |
| 79 | + - scenario 1 keeps the default values for all components |
| 80 | + - scenario 2 sets the statuses of component with id 0 to 0 (and keeps defaults for other components) |
| 81 | + """ |
| 82 | + |
| 83 | + indptr: IndexPointer |
| 84 | + data: SingleArray |
| 85 | + |
| 86 | + |
| 87 | +SparseBatchData = SparseBatchArray | SparseBatchColumnarData |
| 88 | + |
| 89 | +SparseDataComponentType: TypeAlias = str |
| 90 | + |
| 91 | +BatchColumnarData = DenseBatchColumnarData | SparseBatchColumnarData |
| 92 | + |
| 93 | +ColumnarData = SingleColumnarData | BatchColumnarData |
| 94 | +BatchArray = DenseBatchArray | SparseBatchArray |
| 95 | + |
| 96 | + |
| 97 | +BatchComponentData = BatchArray | BatchColumnarData |
| 98 | + |
| 99 | +_BatchComponentData = TypeVar("_BatchComponentData", BatchArray, BatchColumnarData) # deduction helper |
| 100 | + |
| 101 | + |
| 102 | +BatchDataset = dict[ComponentTypeVar, _BatchComponentData] |
| 103 | + |
| 104 | + |
| 105 | +DataArray = SingleArray | BatchArray |
| 106 | + |
| 107 | + |
| 108 | +_ComponentData = TypeVar("_ComponentData", SingleComponentData, BatchComponentData) # deduction helper |
| 109 | +ComponentData = DataArray | ColumnarData |
| 110 | + |
| 111 | +Dataset = dict[ComponentTypeVar, _ComponentData] |
| 112 | + |
| 113 | + |
| 114 | +DenseBatchData = DenseBatchArray | DenseBatchColumnarData |
| 115 | + |
| 116 | +NominalValue = int |
| 117 | + |
| 118 | +RealValue = float |
| 119 | + |
| 120 | +AsymValue = tuple[RealValue, RealValue, RealValue] |
| 121 | + |
| 122 | +AttributeValue = RealValue | NominalValue | AsymValue |
| 123 | + |
| 124 | +Component = dict[AttributeType, AttributeValue | str] |
| 125 | + |
| 126 | +ComponentList = list[Component] |
| 127 | + |
| 128 | +SinglePythonDataset = dict[ComponentTypeVar, ComponentList] |
| 129 | + |
| 130 | +BatchPythonDataset = list[SinglePythonDataset] |
| 131 | + |
| 132 | +PythonDataset = SinglePythonDataset | BatchPythonDataset |
0 commit comments