Skip to content

Commit c3135c4

Browse files
authored
Split reduce method in 3 by usage (#1129)
Signed-off-by: Hugo KULESZA <[email protected]>
1 parent e1222ae commit c3135c4

File tree

5 files changed

+85
-9
lines changed

5 files changed

+85
-9
lines changed

docs/reference/network.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,9 @@ Miscellaneous network functions
281281
:nosignatures:
282282

283283
Network.reduce
284+
Network.reduce_by_ids
285+
Network.reduce_by_voltage_range
286+
Network.reduce_by_ids_and_depths
284287
Network.merge
285288
Network.get_single_line_diagram
286289
Network.write_single_line_diagram_svg

docs/user_guide/network.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -946,7 +946,7 @@ For this example we will keep only voltage levels with voltage superior or equal
946946
S3VL1 S3 400.0 440.0 390.0
947947
S4VL1 S4 400.0 440.0 390.0
948948

949-
>>> net.reduce(v_min=400)
949+
>>> net.reduce_by_voltage_range(v_min=400)
950950
>>> net.get_voltage_levels()
951951
name substation_id nominal_v high_voltage_limit low_voltage_limit
952952
id
@@ -969,7 +969,7 @@ For the next example we will keep voltage level S1VL1 with a depth of 1.
969969
S2VL1 S2 400.0 440.0 390.0
970970
S3VL1 S3 400.0 440.0 390.0
971971
S4VL1 S4 400.0 440.0 390.0
972-
>>> net.reduce(vl_depths=[['S1VL1', 1]])
972+
>>> net.reduce_by_ids_and_depths(vl_depths=[('S1VL1', 1)])
973973
>>> net.get_voltage_levels()
974974
name substation_id nominal_v high_voltage_limit low_voltage_limit
975975
id
@@ -979,7 +979,7 @@ For the next example we will keep voltage level S1VL1 with a depth of 1.
979979
S1VL1 is connected to S1VL2 by the transformer TWT, so it is kept after the network reduction.
980980
It is the only voltage level connected to S1VL1 by one branch.
981981

982-
the parameter "ids" can be used to specify the exact voltage levels that will be kept
982+
Reduction can also be done by specifying directly the list of voltage levels to keep using the :meth:reduce_by_ids method.
983983

984984
Using operational limits
985985
------------------------

java/pypowsybl/src/main/java/com/powsybl/python/network/NetworkCFunctions.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -458,9 +458,9 @@ public void run() {
458458
}
459459
if (depthsCount != 0) {
460460
final List<Integer> depths = CTypeUtil.toIntegerList(depthsPtr, depthsCount);
461-
final List<String> voltageLeveles = toStringList(vlsPtrPtr, vlsCount);
461+
final List<String> voltageLevels = toStringList(vlsPtrPtr, vlsCount);
462462
for (int i = 0; i < depths.size(); i++) {
463-
predicates.add(new SubNetworkPredicate(network.getVoltageLevel(voltageLeveles.get(i)), depths.get(i)));
463+
predicates.add(new SubNetworkPredicate(network.getVoltageLevel(voltageLevels.get(i)), depths.get(i)));
464464
}
465465
}
466466
final OrNetworkPredicate orNetworkPredicate = new OrNetworkPredicate(predicates);

pypowsybl/network/impl/network.py

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,8 +290,11 @@ def save_to_binary_buffer(self, format: str = 'XIIDM', parameters: ParamsDict =
290290
None if report_node is None else report_node._report_node)) # pylint: disable=protected-access
291291

292292
def reduce(self, v_min: float = 0, v_max: float = sys.float_info.max, ids: Optional[List[str]] = None,
293-
vl_depths: tuple = (), with_dangling_lines: bool = False) -> None:
293+
vl_depths: Optional[List[tuple]] = None, with_dangling_lines: bool = False) -> None:
294294
"""
295+
.. deprecated:: 1.14.0
296+
Use :meth:`reduce_by_voltage_range`, :meth:`reduce_by_ids` or :meth:`reduce_by_ids_and_depths` instead depending on your use case.
297+
295298
Reduce to a smaller network according to the following parameters
296299
297300
:param v_min: minimum voltage of the voltage levels kept after reducing
@@ -300,15 +303,76 @@ def reduce(self, v_min: float = 0, v_max: float = sys.float_info.max, ids: Optio
300303
:param vl_depths: depth around voltage levels which are indicated by their id, that will be kept
301304
:param with_dangling_lines: keeping the dangling lines
302305
"""
306+
warnings.warn("reduce is deprecated, use `reduce_by_voltage_range`, `reduce_by_ids` or `reduce_by_ids_and_depths` instead depending on your use case", DeprecationWarning)
303307
if ids is None:
304308
ids = []
305309
vls = []
306310
depths = []
311+
if vl_depths is None:
312+
vl_depths = []
307313
for v in vl_depths:
308314
vls.append(v[0])
309315
depths.append(v[1])
310316
_pp.reduce_network(self._handle, v_min, v_max, ids, vls, depths, with_dangling_lines)
311317

318+
def reduce_by_voltage_range(self, v_min: float = 0, v_max: float = sys.float_info.max, with_dangling_lines: bool = False) -> None:
319+
"""
320+
Reduce to a smaller network (only keeping all elements whose nominal voltage is in the specified voltage range)
321+
322+
:param v_min: minimum voltage of the voltage levels kept after reducing
323+
:param v_max: voltage maximum of the voltage levels kept after reducing
324+
:param with_dangling_lines: whether dangling lines should be created to replace lines cut at the boundary of reduction
325+
326+
Example:
327+
328+
.. code-block:: python
329+
330+
network.reduce_by_voltage_range(v_min=90, v_max=250, with_dangling_lines=True)
331+
332+
will only keep elements of voltage level between 90 and 250kV, replacing the lines cut at the boundary by dangling lines.
333+
"""
334+
_pp.reduce_network(self._handle, v_min=v_min, v_max=v_max, ids=[], vls=[], depths=[], with_dangling_lines=with_dangling_lines)
335+
336+
def reduce_by_ids(self, ids: List[str], with_dangling_lines: bool = False) -> None:
337+
"""
338+
Reduce to a smaller network (only keeping voltage levels whose id is in the specified list)
339+
340+
:param ids: list of the voltage level ids that should be kept in the reduced network
341+
:param with_dangling_lines: whether dangling lines should be created to replace lines cut at the boundary of reduction
342+
343+
Example:
344+
345+
.. code-block:: python
346+
347+
network.reduce_by_ids(ids=["VL1", "VL2"])
348+
349+
will only keep voltage levels VL1 and VL2 and all network elements between them.
350+
"""
351+
_pp.reduce_network(self._handle, v_min=0, v_max=sys.float_info.max, ids=ids, vls=[], depths=[], with_dangling_lines=with_dangling_lines)
352+
353+
def reduce_by_ids_and_depths(self, vl_depths: List[tuple[str, int]], with_dangling_lines: bool = False) -> None:
354+
"""
355+
Reduce to a smaller network (keeping the specified voltage levels with all respective neighbours at most at the specified depth).
356+
357+
:param vl_depths: list of the voltage level ids that should be kept in the reduced network
358+
:param with_dangling_lines: whether dangling lines should be created to replace lines cut at the boundary of reduction
359+
360+
Example:
361+
362+
.. code-block:: python
363+
364+
network.reduce_by_ids_and_depths(vl_depths=[("VL1", 1), ("VL25", 3)])
365+
366+
will only keep voltage levels VL1 and its neighbours, and VL25 with all elements around it with at most 3 connections between them.
367+
"""
368+
vls = []
369+
depths = []
370+
for v in vl_depths:
371+
vls.append(v[0])
372+
depths.append(v[1])
373+
_pp.reduce_network(self._handle, v_min=0, v_max=sys.float_info.max, ids=[], vls=vls, depths=depths, with_dangling_lines=with_dangling_lines)
374+
375+
312376
def write_single_line_diagram_svg(self, container_id: str, svg_file: PathOrStr, metadata_file: Optional[PathOrStr] = None,
313377
parameters: Optional[SldParameters] = None, sld_profile: Optional[SldProfile] = None) -> None:
314378
"""

tests/test_network_reduction.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
66
#
77
import pytest
8+
import re
89

910
import pypowsybl as pp
1011
import pathlib
@@ -21,21 +22,29 @@ def test_reduce_by_voltage():
2122
n = pp.network.create_eurostag_tutorial_example1_network()
2223
pp.loadflow.run_ac(n)
2324
assert 4 == len(n.get_buses())
24-
n.reduce(v_min=240, v_max=400)
25+
n.reduce_by_voltage_range(v_min=240, v_max=400)
2526
assert 2 == len(n.get_buses())
2627

2728

2829
def test_reduce_by_ids():
2930
n = pp.network.create_eurostag_tutorial_example1_network()
3031
pp.loadflow.run_ac(n)
3132
assert 4 == len(n.get_buses())
32-
n.reduce(ids=['P2'])
33+
n.reduce_by_ids(ids=['P2'])
3334
assert 2 == len(n.get_buses())
3435

3536

3637
def test_reduce_by_subnetwork():
3738
n = pp.network.create_eurostag_tutorial_example1_network()
3839
pp.loadflow.run_ac(n)
3940
assert 4 == len(n.get_buses())
40-
n.reduce(vl_depths=(('VLGEN', 1), ('VLLOAD', 1)))
41+
n.reduce_by_ids_and_depths(vl_depths=[('VLGEN', 1), ('VLLOAD', 1)])
4142
assert 4 == len(n.get_buses())
43+
44+
def test_deprecated_reduce():
45+
n = pp.network.create_eurostag_tutorial_example1_network()
46+
pp.loadflow.run_ac(n)
47+
assert 4 == len(n.get_buses())
48+
with pytest.warns(DeprecationWarning, match=re.escape("reduce is deprecated, use `reduce_by_voltage_range`")):
49+
n.reduce(v_min=240, v_max=400)
50+
assert 2 == len(n.get_buses())

0 commit comments

Comments
 (0)