Skip to content

Commit e3993c8

Browse files
authored
Feature/create tests refactoring (e2nIEE#2960)
* refactored create tests * fixed issue in bus_create * added test placeholders added some tests * added missing tests for wards * added test for create_line * added tests for create_measurement updated schema removed checks in create_measurement when covered by typing removed forced element_type to bus, there is a raise UserWarning below for this case. * added test_create_gen * added tests for create_motor updated schema to be consisten with create function and other tests * removed check_existing column and added pd.NA to allowed check * added trafo create tests * added create switch test removed incorrect measurement test * added create storage test * added tests for create impedance fixed issue in create_impedances * fixed issue in test_create_pwl_costs * added test_create_poly_costs * added tests for ext_grid_create * fixed issue in test_gen_create.py * added tests for create_group * added tests for create load * added tests for create source dc * fixed issues in vsc_stacked schema fixed issue in schema validation where a forain bus check would run for custom columns * fixed schema validation issues in test_line_create.py * added tests for shunt_create * fixed call for _bus_index_validation * added missing tests for sgen create functions * fixed issues resulting from schema modification * fixed measurement.side dtype * cleaned up storage test to reduce code duplication * cleaned up switch test to reduce code duplication * fixed types in test_line_create.py * sonar maintainability improvements. * fixed all failing tests * updated pandera version for py3.14 support * fixed typing version of pandera * removed float comparison * removed float comparison * removed more float comparisons * removed more float comparisons * fixed missing import * fixed more float comparisons * fixed more float comparisons * fixed remaining sonar warnings
1 parent 376b3b2 commit e3993c8

41 files changed

Lines changed: 5883 additions & 2204 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

pandapower/create/bus_create.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ def create_buses_dc(
320320
geo = _geodata_to_geo_series([geodata], coords, nr_buses_dc)
321321
else:
322322
assert hasattr(geodata, "__iter__"), "geodata must be an iterable"
323-
geo = _geodata_to_geo_series(geodata, coords, nr_buses) # type: ignore
323+
geo = _geodata_to_geo_series(geodata, coords, nr_buses_dc)
324324
else:
325325
geo = _geodata_to_geo_series(geodata, coords, nr_buses_dc)
326326

pandapower/create/cost_create.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ def create_pwl_costs(
104104
- Storage
105105
106106
Parameters:
107+
net: the pandapower network
107108
elements: IDs of the elements in the respective element table
108109
et: element type, one of "gen", "sgen", "ext_grid", "load", "dcline", "storage"
109110
points: [[p1, p2, c1], [p2, p3, c2], …] for each element where c(n) defines the costs between p(n) and p(n+1)

pandapower/create/ext_grid_create.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import logging
99

10-
from numpy import nan, bool_
10+
from numpy import nan
1111

1212
from pandapower.auxiliary import pandapowerNet
1313
from pandapower.network_structure import get_default_value

pandapower/create/impedance_create.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from pandapower.network_structure import get_default_value
1616
from pandapower.pp_types import Int
1717
from pandapower.create._utils import (
18+
_add_to_entries_if_not_nan,
1819
_check_branch_element,
1920
_check_multiple_branch_elements,
2021
_get_index_with_check,
@@ -214,7 +215,7 @@ def create_impedances(
214215
rtf_pu: float | Iterable[float] | None = None,
215216
xtf_pu: float | Iterable[float] | None = None,
216217
name: Iterable[str] | None = None,
217-
in_service: bool | Iterable[str] = True,
218+
in_service: bool | Iterable[bool] = True,
218219
index: Int | Iterable[Int] | None = None,
219220
rft0_pu: float | Iterable[float] | None = None,
220221
xft0_pu: float | Iterable[float] | None = None,
@@ -363,19 +364,20 @@ def create_impedances(
363364
"in_service": in_service,
364365
**kwargs,
365366
}
366-
_set_multiple_entries(net, "impedance", index, entries=entries)
367367

368368
if rft0_pu is not None:
369-
_set_value_if_not_nan(net, index, rft0_pu, "rft0_pu", "impedance")
370-
_set_value_if_not_nan(net, index, xft0_pu, "xft0_pu", "impedance")
371-
_set_value_if_not_nan(net, index, rtf0_pu, "rtf0_pu", "impedance")
372-
_set_value_if_not_nan(net, index, xtf0_pu, "xtf0_pu", "impedance")
369+
_add_to_entries_if_not_nan(net, "impedance", entries, index, "rft0_pu", rft0_pu)
370+
_add_to_entries_if_not_nan(net, "impedance", entries, index, "xft0_pu", xft0_pu)
371+
_add_to_entries_if_not_nan(net, "impedance", entries, index, "rtf0_pu", rtf0_pu)
372+
_add_to_entries_if_not_nan(net, "impedance", entries, index, "xtf0_pu", xtf0_pu)
373373

374374
if gf0_pu is not None:
375-
_set_value_if_not_nan(net, index, gf0_pu, "gf0_pu", "impedance")
376-
_set_value_if_not_nan(net, index, bf0_pu, "bf0_pu", "impedance")
377-
_set_value_if_not_nan(net, index, gt0_pu, "gt0_pu", "impedance")
378-
_set_value_if_not_nan(net, index, bt0_pu, "bt0_pu", "impedance")
375+
_add_to_entries_if_not_nan(net, "impedance", entries, index, "gf0_pu", gf0_pu)
376+
_add_to_entries_if_not_nan(net, "impedance", entries, index, "bf0_pu", bf0_pu)
377+
_add_to_entries_if_not_nan(net, "impedance", entries, index, "gt0_pu", gt0_pu)
378+
_add_to_entries_if_not_nan(net, "impedance", entries, index, "bt0_pu", bt0_pu)
379+
380+
_set_multiple_entries(net, "impedance", index, entries=entries)
379381

380382
return index
381383

pandapower/create/line_create.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,7 @@ def create_lines_from_parameters(
814814
name: Iterable[str] | None = None,
815815
index: Int | Iterable[Int] | None = None,
816816
type: LineType | Iterable[str] | None = None,
817-
geodata: Iterable[Iterable[tuple[float, float]]] | None = None,
817+
geodata: Iterable[Iterable[tuple[float, float]]] | Iterable[tuple[float, float]] | None = None,
818818
in_service: bool | Iterable[bool] = get_default_value("line", "in_service"),
819819
df: float | Iterable[float] = get_default_value("line", "df"),
820820
parallel: int | Iterable[int] = get_default_value("line", "parallel"),

pandapower/create/measurement_create.py

Lines changed: 6 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def create_measurement(
2626
std_dev: float,
2727
element: int,
2828
side: int | Literal["from", "to"] | Literal["hv", "mv", "lv"] | None = None,
29-
check_existing: bool = get_default_value("measurement", "check_existing"),
29+
check_existing: bool = False,
3030
index: Int | None = None,
3131
name: str | None = None,
3232
**kwargs,
@@ -36,6 +36,7 @@ def create_measurement(
3636
are: v, p, q, i, va, ia
3737
3838
Parameters:
39+
net: the network to which the measurement will be added
3940
meas_type: Type of measurement. "v", "p", "q", "i", "va" and "ia" are possible
4041
element_type: Clarifies which element is measured. "bus", "line", "trafo", "trafo3w", "load", "gen", "sgen",
4142
"shunt", "ward", "xward" and "ext_grid" are possible
@@ -63,52 +64,20 @@ def create_measurement(
6364
6465
>>> create_measurement(net, "q", "line", 4.5, 0.1, 2, "to")
6566
"""
66-
if meas_type not in ("v", "p", "q", "i", "va", "ia"):
67-
raise UserWarning(f"Invalid measurement type: {meas_type}")
68-
6967
if side is None and element_type in ("line", "trafo", "trafo3w"):
7068
raise UserWarning(f"The element type '{element_type}' requires parameter 'side' to be set")
7169

72-
if meas_type in ("v", "va"):
73-
element_type = "bus"
74-
75-
if element_type not in (
76-
"bus",
77-
"line",
78-
"trafo",
79-
"trafo3w",
80-
"load",
81-
"gen",
82-
"sgen",
83-
"shunt",
84-
"ward",
85-
"xward",
86-
"ext_grid",
87-
):
88-
raise UserWarning(f"Invalid element type: {element_type}")
89-
90-
if element is not None and element not in net[element_type].index.values:
70+
if element is not None and element not in net[element_type].index.to_numpy():
9171
raise UserWarning(f"{element_type} with index={element} does not exist")
9272

93-
index = _get_index_with_check(net, "measurement", index)
94-
9573
if meas_type in ("i", "ia") and element_type == "bus":
9674
raise UserWarning("Line current measurements cannot be placed at buses")
9775

98-
if meas_type in ("v", "va") and element_type in (
99-
"line",
100-
"trafo",
101-
"trafo3w",
102-
"load",
103-
"gen",
104-
"sgen",
105-
"shunt",
106-
"ward",
107-
"xward",
108-
"ext_grid",
109-
):
76+
if meas_type in ("v", "va") and element_type != "bus":
11077
raise UserWarning(f"Voltage measurements can only be placed at a bus, not at {element_type}")
11178

79+
index = _get_index_with_check(net, "measurement", index)
80+
11281
if check_existing:
11382
if side is None:
11483
existing = net.measurement[

pandapower/create/switch_create.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ def create_switches(
139139
net: pandapowerNet,
140140
buses: Sequence,
141141
elements: Sequence,
142-
et: SwitchElementType | Sequence[str],
142+
et: SwitchElementType | Sequence[SwitchElementType],
143143
closed: bool | Iterable[bool] = get_default_value("switch", "closed"),
144144
type: SwitchType | None = None,
145145
name: Iterable[str] | None = None,

pandapower/create/trafo_create.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,13 @@ def create_transformer(
9999
Example:
100100
>>> create_transformer(net, hv_bus=0, lv_bus=1, std_type="0.4 MVA 10/0.4 kV", name="trafo1")
101101
"""
102-
103102
from pandapower.convert_format import convert_trafo_pst_logic
104103

104+
index = _get_index_with_check(net, "trafo", index, name="transformer")
105+
105106
# Check if bus exist to attach the trafo to
106107
_check_branch_element(net, "Trafo", index, hv_bus, lv_bus)
107108

108-
index = _get_index_with_check(net, "trafo", index, name="transformer")
109-
110109
if df <= 0:
111110
raise ValueError(f"derating factor 'df' must be positive: df = {df:.3f}")
112111

pandapower/network_schema/asymmetric_sgen.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
"in_service": pa.Column(
7979
bool, description="specifies if the generator is in service.", metadata={"default": True}
8080
),
81-
"current_source": pa.Column(bool, description=""), # TODO: missing in docu
81+
#"current_source": pa.Column(bool, description=""), # TODO: missing in docu and create function
8282
},
8383
strict=False,
8484
)

pandapower/network_schema/measurement.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66

77
measurement_schema = pa.DataFrameSchema(
88
{
9-
"name": pa.Column(pd.StringDtype, description="Name of measurement"),
9+
"name": pa.Column(
10+
pd.StringDtype,
11+
nullable=True,
12+
description="Name of measurement"
13+
),
1014
"measurement_type": pa.Column(
1115
str, pa.Check.isin(["p", "q", "i", "v"]), description="Defines what physical quantity is measured"
1216
),
@@ -30,15 +34,11 @@
3034
int,
3135
description="If the element_type is “line”, “trafo”, “trafo3w”, “load”, “gen”, “sgen”, “shunt”, “ward”, “xward” or “ext_grid”, element is the index of the relevant element. For “bus” measurements, it is None (default)",
3236
),
33-
"check_existing": pa.Column(
34-
bool,
35-
description="Checks if a measurement of the type already exists and overwrites it. If set to False, the measurement may be added twice (unsafe behaviour), but the performance increases",
36-
metadata={"default": False},
37-
), # TODO: shouldn't this be called overwrite?
3837
"side": pa.Column(
39-
str,
38+
pd.StringDtype,
39+
nullable=True,
4040
description="Only used for measured lines or transformers. Side defines at which end of the branch the measurement is gathered. For lines this may be “from“, “to“ to denote the side with the from_bus or to_bus. It can also be the index of the from_bus or to_bus. For transformers, it can be “hv“, “mv“ or “lv“ or the corresponding bus index, respectively.",
41-
), # TODO: check nur wenn element_type trafo oder line
41+
), # TODO: check nur wenn element_type trafo(3w) oder line
4242
"origin_id": pa.Column(
4343
pd.StringDtype, nullable=True, required=False, description="element rdfId from CIM", metadata={"cim": True}
4444
),

0 commit comments

Comments
 (0)