Skip to content

Commit 16fa44f

Browse files
authored
Merge pull request #953 from emabre/feature/Cleanup-import-statements2
Cleanup import statements: rearrange modules that _core imports from
2 parents 7625f72 + 1e8599d commit 16fa44f

31 files changed

+803
-590
lines changed

docs/api_reference/python-api-reference.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ are internal implementations.
3232
```{eval-rst}
3333
.. automodule:: power_grid_model.enum
3434
:undoc-members:
35+
:imported-members:
36+
:member-order: bysource
3537
:show-inheritance:
3638
```
3739

@@ -40,6 +42,8 @@ are internal implementations.
4042
```{eval-rst}
4143
.. automodule:: power_grid_model.data_types
4244
:undoc-members:
45+
:imported-members:
46+
:member-order: bysource
4347
:show-inheritance:
4448
:exclude-members: BatchList,NominalValue,RealValue,AsymValue,AttributeValue,Component,ComponentList,SinglePythonDataset,BatchPythonDataset,PythonDataset
4549
```
@@ -48,6 +52,9 @@ are internal implementations.
4852

4953
```{eval-rst}
5054
.. automodule:: power_grid_model.errors
55+
:imported-members:
56+
:member-order: bysource
57+
:show-inheritance:
5158
```
5259

5360
## typing

src/power_grid_model/_core/buffer_handling.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,26 @@
1111

1212
import numpy as np
1313

14+
from power_grid_model._core.data_types import (
15+
AttributeType,
16+
ComponentData,
17+
DenseBatchData,
18+
IndexPointer,
19+
SingleComponentData,
20+
SparseBatchArray,
21+
SparseBatchData,
22+
)
1423
from power_grid_model._core.error_handling import VALIDATOR_MSG
1524
from power_grid_model._core.index_integer import IdxC, IdxNp
1625
from power_grid_model._core.power_grid_core import IdxPtr, VoidPtr
1726
from power_grid_model._core.power_grid_meta import ComponentMetaData
18-
from power_grid_model._utils import (
27+
from power_grid_model._core.utils import (
1928
_extract_data_from_component_data,
2029
_extract_indptr,
2130
check_indptr_consistency,
2231
is_columnar,
2332
is_sparse,
2433
)
25-
from power_grid_model.data_types import (
26-
AttributeType,
27-
ComponentData,
28-
DenseBatchData,
29-
IndexPointer,
30-
SingleComponentData,
31-
SparseBatchArray,
32-
SparseBatchData,
33-
)
3434

3535

3636
@dataclass

src/power_grid_model/_core/data_handling.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88

99
import numpy as np
1010

11+
from power_grid_model._core.data_types import Dataset, SingleDataset
1112
from power_grid_model._core.dataset_definitions import ComponentType, DatasetType
13+
from power_grid_model._core.enum import CalculationType, ComponentAttributeFilterOptions
14+
from power_grid_model._core.errors import PowerGridUnreachableHitError
1215
from power_grid_model._core.power_grid_dataset import CConstDataset, CMutableDataset
1316
from power_grid_model._core.power_grid_meta import initialize_array, power_grid_meta_data
14-
from power_grid_model._utils import process_data_filter
15-
from power_grid_model.data_types import Dataset, SingleDataset
16-
from power_grid_model.enum import CalculationType, ComponentAttributeFilterOptions
17-
from power_grid_model.errors import PowerGridUnreachableHitError
18-
from power_grid_model.typing import ComponentAttributeMapping
17+
from power_grid_model._core.typing import ComponentAttributeMapping
18+
from power_grid_model._core.utils import process_data_filter
1919

2020

2121
def get_output_type(*, calculation_type: CalculationType, symmetric: bool) -> DatasetType:
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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

Comments
 (0)