Skip to content

Commit 3f09476

Browse files
committed
down merge
Signed-off-by: Tony Xiang <[email protected]>
2 parents 7b699db + 3c07cbe commit 3f09476

File tree

4 files changed

+46
-4
lines changed

4 files changed

+46
-4
lines changed

.pre-commit-config.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ repos:
1515
rev: 22.6.0
1616
hooks:
1717
- id: black
18-
language_version: python3.8
1918
- repo: local
2019
hooks:
2120
- id: pylint

src/power_grid_model/validation/utils.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,8 @@ def update_input_data(input_data: Dict[str, np.ndarray], update_data: Dict[str,
215215
mask = ~np.isnan(array[field])
216216
else:
217217
mask = np.not_equal(array[field], nan)
218+
if mask.ndim == 2:
219+
mask = np.any(mask, axis=1)
218220
data = array[["id", field]][mask]
219221
idx = np.where(merged_data[component]["id"] == np.reshape(data["id"], (-1, 1)))
220222
if isinstance(idx, tuple):

tests/unit/validation/test_batch_validation.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import numpy as np
88
import pytest
99

10-
from power_grid_model import initialize_array
10+
from power_grid_model import LoadGenType, initialize_array
1111
from power_grid_model.validation import validate_batch_data
1212
from power_grid_model.validation.errors import (
1313
MultiComponentNotUniqueError,
@@ -32,15 +32,34 @@ def input_data() -> Dict[str, np.ndarray]:
3232
line["c1"] = 3.0
3333
line["tan1"] = 4.0
3434
line["i_n"] = 5.0
35-
return {"node": node, "line": line}
35+
36+
asym_load = initialize_array("input", "asym_load", 2)
37+
asym_load["id"] = [9, 10]
38+
asym_load["node"] = [1, 2]
39+
asym_load["status"] = [1, 1]
40+
asym_load["type"] = [LoadGenType.const_power, LoadGenType.const_power]
41+
asym_load["p_specified"] = [[11e6, 12e6, 13e6], [21e6, 22e6, 23e6]]
42+
asym_load["q_specified"] = [[11e5, 12e5, 13e5], [21e5, 22e5, 23e5]]
43+
44+
return {"node": node, "line": line, "asym_load": asym_load}
3645

3746

3847
@pytest.fixture
3948
def batch_data() -> Dict[str, np.ndarray]:
4049
line = initialize_array("update", "line", (3, 2))
4150
line["id"] = [[5, 6], [6, 7], [7, 5]]
4251
line["from_status"] = [[1, 1], [1, 1], [1, 1]]
43-
return {"line": line}
52+
53+
# Add batch for asym_load, which has 2-D array for p_specified
54+
asym_load = initialize_array("update", "asym_load", (3, 2))
55+
asym_load["id"] = [[9, 10], [9, 10], [9, 10]]
56+
57+
return {"line": line, "asym_load": asym_load}
58+
59+
60+
def test_validate_batch_data(input_data, batch_data):
61+
errors = validate_batch_data(input_data, batch_data)
62+
assert not errors
4463

4564

4665
def test_validate_batch_data_input_error(input_data, batch_data):

tests/unit/validation/test_utils.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,28 @@ def test_update_input_data_int_nan():
388388
np.testing.assert_array_equal(merged["line"]["from_status"], [0, -128, 1])
389389

390390

391+
def test_update_input_data_asym_nans():
392+
input_load = initialize_array("input", "asym_load", 3)
393+
input_load["id"] = [1, 2, 3]
394+
input_load["p_specified"] = [[1.1, 1.2, 1.3], [2.1, np.nan, np.nan], [np.nan, np.nan, np.nan]]
395+
396+
update_load = initialize_array("update", "asym_load", 3)
397+
update_load["id"] = [1, 2, 3]
398+
update_load["p_specified"] = [[np.nan, np.nan, np.nan], [np.nan, np.nan, 5.3], [6.1, 6.2, 6.3]]
399+
400+
merged = update_input_data(input_data={"asym_load": input_load}, update_data={"asym_load": update_load})
401+
402+
# The desired result would be to update all non-NaN values individually:
403+
# np.testing.assert_array_equal(
404+
# merged["asym_load"]["p_specified"], [[1.1, 1.2, 1.3], [2.1, np.nan, 5.3], [6.1, 6.2, 6.3]]
405+
# )
406+
407+
# The current C++ implementation updates the entire 3-phase value is one of the elements is non-NaN:
408+
np.testing.assert_array_equal(
409+
merged["asym_load"]["p_specified"], [[1.1, 1.2, 1.3], [np.nan, np.nan, 5.3], [6.1, 6.2, 6.3]]
410+
)
411+
412+
391413
def test_errors_to_string_no_errors():
392414
assert errors_to_string(errors=None) == "the data: OK"
393415
assert errors_to_string(errors=[]) == "the data: OK"

0 commit comments

Comments
 (0)