Skip to content

Commit 53d6bc1

Browse files
authored
Merge pull request #1042 from PowerGridModel/feature/clean-up-remaining-enums-2
Use of enumerations: Clean remaining enums in other places
2 parents 3366b4f + da14005 commit 53d6bc1

File tree

7 files changed

+203
-154
lines changed

7 files changed

+203
-154
lines changed

src/power_grid_model/data_types.py

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -101,16 +101,20 @@
101101
"""
102102
A batch dataset is a dictionary where the keys are the component types and the values are :class:`BatchComponentData`
103103
104-
- Example: {"node": :class:`DenseBatchArray`, "line": :class:`SparseBatchArray`,
105-
"link": :class:`DenseBatchColumnarData`, "transformer": :class:`SparseBatchColumnarData`}
104+
- Example: {
105+
ComponentType.node: :class:`DenseBatchArray`,
106+
ComponentType.line: :class:`SparseBatchArray`,
107+
ComponentType.link: :class:`DenseBatchColumnarData`,
108+
ComponentType.transformer: :class:`SparseBatchColumnarData`
109+
}
106110
"""
107111

108112
BatchList = _BatchList
109113
"""
110114
A batch list is an alternative representation of a batch. It is a list of single datasets, where each single dataset
111115
is actually a batch. The batch list is intended as an intermediate data type, during conversions.
112116
113-
- Example: [:class:`SingleDataset`, {"node": :class:`SingleDataset`}]
117+
- Example: [:class:`SingleDataset`, {ComponentType.node: :class:`SingleDataset`}]
114118
"""
115119

116120
BatchPythonDataset = _BatchPythonDataset
@@ -121,8 +125,8 @@
121125
122126
- Example:
123127
124-
[{"line": [{"id": 3, "from_status": 0, "to_status": 0, ...}],},
125-
{"line": [{"id": 3, "from_status": 1, "to_status": 1, ...}],}]
128+
[{ComponentType.line: [{"id": 3, "from_status": 0, "to_status": 0, ...}],},
129+
{ComponentType.line: [{"id": 3, "from_status": 1, "to_status": 1, ...}],}]
126130
"""
127131

128132
ColumnarData = _ColumnarData
@@ -162,10 +166,14 @@
162166
163167
- Examples:
164168
165-
- single: {"node": :class:`SingleArray`, "line": :class:`SingleColumnarData`}
169+
- single: {ComponentType.node: :class:`SingleArray`, ComponentType.line: :class:`SingleColumnarData`}
166170
167-
- batch: {"node": :class:`DenseBatchArray`, "line": :class:`SparseBatchArray`,
168-
"link": :class:`DenseBatchColumnarData`, "transformer": :class:`SparseBatchColumnarData`}
171+
- batch: {
172+
ComponentType.node: :class:`DenseBatchArray`,
173+
ComponentType.line: :class:`SparseBatchArray`,
174+
ComponentType.link: :class:`DenseBatchColumnarData`,
175+
ComponentType.transformer: :class:`SparseBatchColumnarData`
176+
}
169177
170178
"""
171179

@@ -216,14 +224,14 @@
216224
- single:
217225
218226
{
219-
"node": [{"id": 1, "u_rated": 10500.0}, {"id": 2, "u_rated": 10500.0}],
220-
"line": [{"id": 3, "from_node": 1, "to_node": 2, ...}],
227+
ComponentType.node: [{"id": 1, "u_rated": 10500.0}, {"id": 2, "u_rated": 10500.0}],
228+
ComponentType.line: [{"id": 3, "from_node": 1, "to_node": 2, ...}],
221229
}
222230
223231
- batch:
224232
225-
[{"line": [{"id": 3, "from_status": 0, "to_status": 0, ...}],},
226-
{"line": [{"id": 3, "from_status": 1, "to_status": 1, ...}],}]
233+
[{ComponentType.line: [{"id": 3, "from_status": 0, "to_status": 0, ...}],},
234+
{ComponentType.line: [{"id": 3, "from_status": 1, "to_status": 1, ...}],}]
227235
"""
228236

229237
RealValue = _RealValue
@@ -240,7 +248,10 @@
240248
- Examples:
241249
242250
- structure: <1d-array>
243-
- concrete: array([(0, 10500.0), (0, 10500.0)], dtype=power_grid_meta_data["input"]["node"].dtype)
251+
- concrete: array(
252+
[(0, 10500.0), (0, 10500.0)],
253+
dtype=power_grid_meta_data[DatasetType.input][ComponentType.node].dtype
254+
)
244255
"""
245256

246257
SingleColumn = _SingleColumn
@@ -253,8 +264,11 @@
253264
- structure: <1d-array>
254265
- concrete:
255266
256-
- array([0, 1], dtype=power_grid_meta_data["input"]["node"].dtype.fields["id"][0])
257-
- array([10500.0, 10500.0], dtype=power_grid_meta_data["input"]["node"].dtype.fields["u_rated"][0])
267+
- array([0, 1], dtype=power_grid_meta_data[DatasetType.input][ComponentType.node].dtype.fields["id"][0])
268+
- array(
269+
[10500.0, 10500.0],
270+
dtype=power_grid_meta_data[DatasetType.input][ComponentType.node].dtype.fields["u_rated"][0]
271+
)
258272
"""
259273

260274

@@ -277,7 +291,7 @@
277291
A single dataset is a dictionary where the keys are the component types and the values are
278292
:class:`ComponentData`
279293
280-
- Example: {"node": :class:`SingleArray`, "line": :class:`SingleColumnarData`}
294+
- Example: {ComponentType.node: :class:`SingleArray`, ComponentType.line: :class:`SingleColumnarData`}
281295
"""
282296

283297
SinglePythonDataset = _SinglePythonDataset
@@ -289,8 +303,8 @@
289303
- Example:
290304
291305
{
292-
"node": [{"id": 1, "u_rated": 10500.0}, {"id": 2, "u_rated": 10500.0}],
293-
"line": [{"id": 3, "from_node": 1, "to_node": 2, ...}],
306+
ComponentType.node: [{"id": 1, "u_rated": 10500.0}, {"id": 2, "u_rated": 10500.0}],
307+
ComponentType.line: [{"id": 3, "from_node": 1, "to_node": 2, ...}],
294308
}
295309
"""
296310

tests/unit/test_0Z_model_validation.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import numpy as np
1010
import pytest
1111

12+
from power_grid_model._core.dataset_definitions import ComponentType
1213
from power_grid_model._core.utils import convert_batch_dataset_to_batch_list
1314
from power_grid_model.enum import TapChangingStrategy
1415

@@ -129,12 +130,12 @@ def test_single_validation(
129130
assert np.all(input_array["id"][indexer_array] == ids_array)
130131

131132
# test calculate with only node and source result
132-
kwargs = dict(base_kwargs, **{"output_component_types": ["node", "source"]})
133+
kwargs = dict(base_kwargs, **{"output_component_types": [ComponentType.node, ComponentType.source]})
133134
result = calculation_function(model, **supported_kwargs(kwargs=kwargs, supported=calculation_args))
134-
assert set(result.keys()) == {"node", "source"}
135-
kwargs = dict(base_kwargs, **{"output_component_types": {"node", "source"}})
135+
assert set(result.keys()) == {ComponentType.node, ComponentType.source}
136+
kwargs = dict(base_kwargs, **{"output_component_types": {ComponentType.node, ComponentType.source}})
136137
result = calculation_function(model, **supported_kwargs(kwargs=kwargs, supported=calculation_args))
137-
assert set(result.keys()) == {"node", "source"}
138+
assert set(result.keys()) == {ComponentType.node, ComponentType.source}
138139

139140

140141
@pytest.mark.parametrize(

tests/unit/test_dataset.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,10 @@ def test_const_dataset__conflicting_data():
5454
with pytest.raises(PowerGridError):
5555
CConstDataset(
5656
data={
57-
ComponentType.node: np.zeros(1, dtype=power_grid_meta_data["input"][ComponentType.node]),
58-
"sym_load": np.zeros(1, dtype=power_grid_meta_data["update"]["sym_load"]),
57+
ComponentType.node: np.zeros(1, dtype=power_grid_meta_data[DatasetType.input][ComponentType.node]),
58+
ComponentType.sym_load: np.zeros(
59+
1, dtype=power_grid_meta_data[DatasetType.update][ComponentType.sym_load]
60+
),
5961
}
6062
)
6163

@@ -112,14 +114,16 @@ def test_const_dataset__sparse_batch_data(dataset_type):
112114
"indptr": np.array([0, 2, 3, 3]),
113115
},
114116
ComponentType.sym_load: {
115-
"data": np.zeros(shape=2, dtype=power_grid_meta_data[dataset_type]["sym_load"]),
117+
"data": np.zeros(shape=2, dtype=power_grid_meta_data[dataset_type][ComponentType.sym_load]),
116118
"indptr": np.array([0, 0, 1, 2]),
117119
},
118120
ComponentType.asym_load: {
119-
"data": np.zeros(shape=4, dtype=power_grid_meta_data[dataset_type]["asym_load"]),
121+
"data": np.zeros(shape=4, dtype=power_grid_meta_data[dataset_type][ComponentType.asym_load]),
120122
"indptr": np.array([0, 2, 3, 4]),
121123
},
122-
ComponentType.link: np.zeros(shape=(batch_size, 4), dtype=power_grid_meta_data[dataset_type]["link"]),
124+
ComponentType.link: np.zeros(
125+
shape=(batch_size, 4), dtype=power_grid_meta_data[dataset_type][ComponentType.link]
126+
),
123127
}
124128

125129
dataset = CConstDataset(data, dataset_type)

0 commit comments

Comments
 (0)