From 0bb4403849192ef289cd7c22ce6137c7a6c86aa0 Mon Sep 17 00:00:00 2001 From: Thijs Baaijen <13253091+Thijss@users.noreply.github.com> Date: Wed, 29 Jan 2025 13:50:15 +0100 Subject: [PATCH] Fix: solve import issues for performance tests (#22) * Update test_get_downstream_nodes Co-authored-by: jaapschoutenalliander Signed-off-by: Thijs Baaijen <13253091+Thijss@users.noreply.github.com> * Update performance tests Co-authored-by: jaapschoutenalliander Signed-off-by: Thijs Baaijen <13253091+Thijss@users.noreply.github.com> --------- Signed-off-by: Thijs Baaijen <13253091+Thijss@users.noreply.github.com> Co-authored-by: jaapschoutenalliander --- tests/performance/_constants.py | 18 ++-- tests/performance/_helpers.py | 101 +++++------------- tests/performance/array_performance_tests.py | 54 +++++----- tests/performance/filter_performance_tests.py | 14 +-- tests/performance/graph_performance_tests.py | 19 ++-- tests/performance/grid_performance_tests.py | 30 ++++++ tests/unit/model/grids/test_grid_search.py | 14 ++- 7 files changed, 127 insertions(+), 123 deletions(-) create mode 100644 tests/performance/grid_performance_tests.py diff --git a/tests/performance/_constants.py b/tests/performance/_constants.py index f114754..e531976 100644 --- a/tests/performance/_constants.py +++ b/tests/performance/_constants.py @@ -6,18 +6,18 @@ "dtype = [('id', ' +# +# SPDX-License-Identifier: MPL-2.0 + +from tests.performance._helpers import do_performance_test + +# pylint: disable=missing-function-docstring + + +def test_get_downstream_nodes_performance(): + setup_code = { + "grid": "import numpy as np;" + + "from power_grid_model_ds.enums import NodeType;" + + "from power_grid_model_ds import Grid;" + + "from power_grid_model_ds.generators import RadialGridGenerator;" + + "from power_grid_model_ds.graph_models import RustworkxGraphModel;" + + "grid=RadialGridGenerator(nr_nodes={size}, grid_class=Grid, graph_model=RustworkxGraphModel).run();" + + "non_substation_node = grid.node.filter(node_type=NodeType.UNSPECIFIED).id;" + + "node_id = np.random.choice(non_substation_node)" + } + + code_to_test = [ + "grid.get_downstream_nodes(node_id)", + ] + + do_performance_test(code_to_test, [10, 1000, 5000], 100, setup_code) + + +if __name__ == "__main__": + test_get_downstream_nodes_performance() diff --git a/tests/unit/model/grids/test_grid_search.py b/tests/unit/model/grids/test_grid_search.py index f3ac243..034f1cd 100644 --- a/tests/unit/model/grids/test_grid_search.py +++ b/tests/unit/model/grids/test_grid_search.py @@ -5,6 +5,7 @@ import numpy as np import pytest +from power_grid_model_ds import Grid from power_grid_model_ds._core.model.arrays.base.errors import RecordDoesNotExist from power_grid_model_ds._core.model.enums.nodes import NodeType @@ -25,10 +26,19 @@ def test_grid_get_nearest_substation_node_no_substation(basic_grid): basic_grid.get_nearest_substation_node(node_id=103) -def test_get_downstream_nodes(basic_grid): +def test_get_downstream_nodes(basic_grid: Grid): """Test that get_downstream_nodes returns the expected nodes.""" + # Move the open line to be able to test sorting of nodes by distance correctly + basic_grid.make_active(basic_grid.line.get(203)) + basic_grid.make_inactive(basic_grid.link.get(601)) downstream_nodes = basic_grid.get_downstream_nodes(node_id=102) - assert {103, 106} == set(downstream_nodes) + assert downstream_nodes[-1] == 104 # Furthest away + assert {103, 104, 106} == set(downstream_nodes) + + downstream_nodes = basic_grid.get_downstream_nodes(node_id=102, inclusive=True) + assert downstream_nodes[0] == 102 + assert downstream_nodes[-1] == 104 + assert {102, 103, 104, 106} == set(downstream_nodes) def test_get_downstream_nodes_from_substation_node(basic_grid):