Skip to content

Commit d979158

Browse files
authored
Merge pull request #1039 from PowerGridModel/fix/asym-val-dense-batch
Buffer Handling: update obtaining raw data view, add tests
2 parents 3aeb5ea + d02cd89 commit d979158

File tree

2 files changed

+165
-4
lines changed

2 files changed

+165
-4
lines changed

src/power_grid_model/_core/buffer_handling.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,15 @@ def _get_raw_attribute_data_view(data: np.ndarray, schema: ComponentMetaData, at
117117
Returns:
118118
a raw view on the data set.
119119
"""
120-
if schema.dtype[attribute].shape == (3,) and data.shape[-1] != 3:
121-
raise ValueError("Given data has a different schema than supported.")
122-
return _get_raw_data_view(data, dtype=schema.dtype[attribute].base)
120+
dense_batch_ndim = 2
121+
122+
attr_schema = schema.dtype[attribute]
123+
attr_shape_start = data.ndim - attr_schema.ndim
124+
dataset_shape = data.shape[:attr_shape_start]
125+
attr_shape = data.shape[attr_shape_start:]
126+
if len(dataset_shape) <= dense_batch_ndim and attr_shape == attr_schema.shape:
127+
return _get_raw_data_view(data, dtype=schema.dtype[attribute].base)
128+
raise ValueError("Given data has a different schema than supported.")
123129

124130

125131
def _get_indptr_view(indptr: np.ndarray) -> IdxPtr: # type: ignore[valid-type]

tests/unit/test_buffer_handling.py

Lines changed: 156 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@
99
import numpy as np
1010
import pytest
1111

12-
from power_grid_model._core.buffer_handling import _get_dense_buffer_properties, _get_sparse_buffer_properties
12+
from power_grid_model._core.buffer_handling import (
13+
_get_dense_buffer_properties,
14+
_get_raw_attribute_data_view,
15+
_get_sparse_buffer_properties,
16+
get_buffer_view,
17+
)
1318
from power_grid_model._core.dataset_definitions import ComponentType, DatasetType
1419
from power_grid_model._core.power_grid_meta import initialize_array, power_grid_meta_data
1520

@@ -83,3 +88,153 @@ def test__get_sparse_buffer_properties(component_type, is_columnar):
8388
assert properties.columns == list(data["data"].keys())
8489
else:
8590
assert properties.columns is None
91+
92+
93+
@pytest.mark.parametrize(
94+
"component, is_batch, is_columnar, is_sparse",
95+
[
96+
pytest.param(ComponentType.sym_load, True, True, False, id="sym_load_columnar"),
97+
pytest.param(ComponentType.asym_load, True, True, False, id="asym_load-columnar"),
98+
],
99+
)
100+
def test__get_raw_attribute_data_view(component, is_batch, is_columnar, is_sparse):
101+
schema = power_grid_meta_data[DatasetType.update][component]
102+
103+
data = load_data(
104+
component_type=component,
105+
is_batch=is_batch,
106+
is_columnar=is_columnar,
107+
is_sparse=is_sparse,
108+
)
109+
if component == ComponentType.asym_load:
110+
assert data["p_specified"].shape[-1] == 3
111+
112+
buffer_view = get_buffer_view(
113+
data,
114+
schema=schema,
115+
is_batch=is_batch,
116+
)
117+
assert buffer_view is not None
118+
assert buffer_view.batch_size == 2
119+
assert buffer_view.n_elements_per_scenario == 4
120+
assert buffer_view.total_elements == 8
121+
122+
123+
@pytest.mark.parametrize(
124+
"component, attribute",
125+
[
126+
pytest.param(ComponentType.asym_load, "p_specified", id="asym_load-shape_missmatch"),
127+
],
128+
)
129+
def test__get_raw_attribute_data_view_fail(component, attribute):
130+
schema = power_grid_meta_data[DatasetType.update][component]
131+
132+
data = load_data(
133+
component_type=component,
134+
is_batch=True,
135+
is_columnar=True,
136+
is_sparse=False,
137+
)
138+
139+
old_shape = data[attribute].shape
140+
new_shape = list(old_shape)
141+
# because in _get_raw_attribute_data_view we check if the dimension of the last element is not 3 to raise an error
142+
new_shape[-1] = 2
143+
data[attribute] = np.zeros(new_shape, dtype=data[attribute].dtype)
144+
145+
updated_shape = data[attribute].shape
146+
assert old_shape != updated_shape
147+
assert old_shape[-1] == 3
148+
assert updated_shape[-1] == 2
149+
150+
with pytest.raises(ValueError, match="Given data has a different schema than supported."):
151+
get_buffer_view(data, schema=schema, is_batch=True)
152+
153+
154+
@pytest.mark.parametrize(
155+
"component, is_batch, is_columnar, is_sparse",
156+
[
157+
pytest.param(ComponentType.sym_load, True, True, False, id="sym_load"),
158+
pytest.param(ComponentType.asym_load, True, True, False, id="asym_load-columnar"),
159+
],
160+
)
161+
def test__get_raw_attribute_data_view_direct(component, is_batch, is_columnar, is_sparse):
162+
attribute = "p_specified"
163+
schema = power_grid_meta_data[DatasetType.update][component]
164+
165+
data = load_data(
166+
component_type=component,
167+
is_batch=is_batch,
168+
is_columnar=is_columnar,
169+
is_sparse=is_sparse,
170+
)
171+
172+
attribute_data = data[attribute]
173+
174+
_get_raw_attribute_data_view(attribute_data, schema, "p_specified")
175+
176+
177+
@pytest.mark.parametrize(
178+
"component, attr_data_shape, attribute",
179+
[
180+
pytest.param(ComponentType.asym_load, (2, 4, 3), "p_specified", id="asym_load-columnar"),
181+
pytest.param(ComponentType.asym_load, (1, 4, 3), "p_specified", id="asym_load-columnar"),
182+
pytest.param(ComponentType.asym_load, (2, 5, 3), "p_specified", id="asym_load-columnar"),
183+
pytest.param(ComponentType.asym_load, (2, 6, 3), "p_specified", id="asym_load-columnar"),
184+
pytest.param(ComponentType.asym_load, 3, "p_specified", id="asym_load-columnar"),
185+
pytest.param(ComponentType.asym_load, (3, 3), "p_specified", id="asym_load-columnar"),
186+
pytest.param(ComponentType.asym_load, (2, 4, 3), "q_specified", id="asym_load-columnar"),
187+
pytest.param(ComponentType.asym_load, (1, 4, 3), "q_specified", id="asym_load-columnar"),
188+
pytest.param(ComponentType.asym_load, (2, 5, 3), "q_specified", id="asym_load-columnar"),
189+
pytest.param(ComponentType.asym_load, (2, 6, 3), "q_specified", id="asym_load-columnar"),
190+
pytest.param(ComponentType.asym_load, 3, "q_specified", id="asym_load-columnar"),
191+
pytest.param(ComponentType.asym_load, (3, 3), "q_specified", id="asym_load-columnar"),
192+
# sym component
193+
pytest.param(ComponentType.sym_load, 3, "p_specified", id="sym_load-columnar"),
194+
pytest.param(ComponentType.sym_load, (3, 3), "p_specified", id="sym_load-columnar"),
195+
pytest.param(ComponentType.sym_load, (4, 3), "p_specified", id="sym_load-columnar"),
196+
pytest.param(ComponentType.sym_load, (4, 0), "p_specified", id="sym_load-columnar"),
197+
pytest.param(ComponentType.sym_load, 0, "p_specified", id="sym_load-columnar"),
198+
pytest.param(ComponentType.sym_load, 3, "q_specified", id="sym_load-columnar"),
199+
pytest.param(ComponentType.sym_load, (3, 3), "q_specified", id="sym_load-columnar"),
200+
pytest.param(ComponentType.sym_load, (4, 3), "q_specified", id="sym_load-columnar"),
201+
pytest.param(ComponentType.sym_load, (4, 0), "q_specified", id="sym_load-columnar"),
202+
pytest.param(ComponentType.sym_load, (0, 4), "q_specified", id="sym_load-columnar"),
203+
pytest.param(ComponentType.sym_load, 0, "q_specified", id="sym_load-columnar"),
204+
],
205+
)
206+
def test__get_raw_attribute_data_view_directly(component, attr_data_shape, attribute):
207+
arr = np.zeros(attr_data_shape)
208+
schema = power_grid_meta_data[DatasetType.update][component]
209+
210+
_get_raw_attribute_data_view(arr, schema, attribute)
211+
212+
213+
@pytest.mark.parametrize(
214+
"component, attr_data_shape, attribute",
215+
[
216+
pytest.param(ComponentType.asym_load, (2, 4, 4), "p_specified", id="asym_load-columnar"),
217+
pytest.param(ComponentType.asym_load, (2, 6, 4), "p_specified", id="asym_load-columnar"),
218+
pytest.param(ComponentType.asym_load, 0, "p_specified", id="asym_load-columnar"),
219+
pytest.param(ComponentType.asym_load, (2, 0, 0), "p_specified", id="asym_load-columnar"),
220+
pytest.param(ComponentType.asym_load, (2, 4, 0), "p_specified", id="asym_load-columnar"),
221+
pytest.param(ComponentType.asym_load, (2, 4, 4), "q_specified", id="asym_load-columnar"),
222+
pytest.param(ComponentType.asym_load, (2, 6, 4), "q_specified", id="asym_load-columnar"),
223+
pytest.param(ComponentType.asym_load, 0, "q_specified", id="asym_load-columnar"),
224+
pytest.param(ComponentType.asym_load, (2, 0, 0), "q_specified", id="asym_load-columnar"),
225+
pytest.param(ComponentType.asym_load, (2, 4, 0), "q_specified", id="asym_load-columnar"),
226+
# sym component
227+
pytest.param(ComponentType.sym_load, (2, 4, 4), "p_specified", id="sym_load-columnar"),
228+
pytest.param(ComponentType.sym_load, (2, 4, 3), "p_specified", id="sym_load-columnar"),
229+
pytest.param(ComponentType.sym_load, (0, 0, 1), "p_specified", id="sym_load-columnar"),
230+
pytest.param(ComponentType.sym_load, (2, 4, 4), "q_specified", id="sym_load-columnar"),
231+
pytest.param(ComponentType.sym_load, (2, 4, 3), "q_specified", id="sym_load-columnar"),
232+
pytest.param(ComponentType.sym_load, (0, 0, 1), "q_specified", id="sym_load-columnar"),
233+
],
234+
)
235+
def test__get_raw_attribute_data_view_directly_fail(component, attr_data_shape, attribute):
236+
arr = np.zeros(attr_data_shape)
237+
schema = power_grid_meta_data[DatasetType.update][component]
238+
239+
with pytest.raises(ValueError, match="Given data has a different schema than supported."):
240+
_get_raw_attribute_data_view(arr, schema, attribute)

0 commit comments

Comments
 (0)