Skip to content

Commit 196ac08

Browse files
committed
fixed most mypy issues
1 parent ea0e125 commit 196ac08

File tree

4 files changed

+75
-68
lines changed

4 files changed

+75
-68
lines changed

pandapower/analysis/LODF.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import pandas as pd
1111
import numpy as np
12+
import numpy.typing as npt
1213

1314
from pandapower import pandapowerNet
1415
from pandapower.analysis.PTDF import _makePTDF_ppci, _get_PTDF_perturb
@@ -29,7 +30,7 @@ def _get_LODF_direct(
2930
outage_branch_ix=None,
3031
using_sparse_solver=True,
3132
random_verify=True,
32-
branch_dict: dict[str, Union[list[int], None]] | None = None,
33+
branch_dict: dict[str, list[int] | None] | None = None,
3334
reduced=True,
3435
) -> dict:
3536
"""
@@ -41,8 +42,8 @@ def _get_LODF_direct(
4142
using_sparse_solver = True
4243

4344
# If branch_dict not None compute list of ppci branch indices and its branch type intervals as lookup
44-
branch_ppci_lookup = None
45-
branch_id = None
45+
branch_ppci_lookup: dict | None = None
46+
branch_id: list | None = None
4647
if branch_dict is not None:
4748
branch_id, branch_ppci_lookup = branch_dict_to_ppci_branch_list(net=net, branch_dict=branch_dict)
4849
else:
@@ -68,6 +69,7 @@ def _get_LODF_direct(
6869
# Set results to default value of bridge branch
6970
lodf_ppci[:, bridge_branch_mask] = np.nan
7071
if branch_id is not None and not reduced:
72+
assert branch_ppci_lookup is not None # force mypy type narrowing
7173
branch_id_complement = [x for x in range(list(branch_ppci_lookup.values())[-1][1]) if x not in branch_id]
7274
lodf_ppci[:, branch_id_complement] = np.nan
7375
lodf_ppci[branch_id_complement, :] = np.nan
@@ -123,7 +125,7 @@ def _init_LODF_pp_np(
123125
)
124126

125127
for data in lodf_pp.values():
126-
data[:] = np.NaN
128+
data[:] = np.nan
127129
return lodf_pp
128130

129131

@@ -134,12 +136,12 @@ def _LODF_ppci_to_pp(
134136
):
135137
# convert the branch sensitivity of the ppci layer to pandapower net layer
136138
if branch_ppci_lookup is not None:
137-
pp_ppci_branch_lookups = {
139+
pp_ppci_branch_lookups: dict[str, npt.NDArray | range | None] = {
138140
br_type: range(branch_ppci_lookup[br_type][0], branch_ppci_lookup[br_type][1])
139141
for br_type in ("line", "trafo", "impedance")
140142
if br_type in branch_ppci_lookup.keys()
141143
}
142-
pp_ppci_trafo3w_lookups = {
144+
pp_ppci_trafo3w_lookups: dict[str, npt.NDArray | range] | None = {
143145
type: range(branch_ppci_lookup[type][0], branch_ppci_lookup[type][1])
144146
for type in ("trafo3w_hv", "trafo3w_mv", "trafo3w_lv")
145147
if type in branch_ppci_lookup.keys()
@@ -237,7 +239,7 @@ def _get_LODF_perturb(
237239
)
238240

239241
# number of out of service buses
240-
num_out_of_service_bus = np.sum(np.isnan(net_mod.res_bus.va_degree.to_numpy()))
242+
num_out_of_service_bus: int = np.sum(np.isnan(net_mod.res_bus.va_degree.to_numpy()))
241243
# list with the indices of out of service buses
242244
# list_out_of_service_bus = list(net_mod.res_bus.va_degree[np.isnan(net_mod.res_bus.va_degree.to_numpy())].index)
243245

@@ -556,7 +558,7 @@ def _get_dc_n1_perturb(
556558
outage_branch_ix = _get_outage_branch_ix(net_mod, outage_branch_type, outage_branch_ix)
557559

558560
rundcpp(net_mod, distributed_slack=distributed_slack)
559-
num_out_of_service_bus = np.sum(np.isnan(net_mod.res_bus.va_degree.to_numpy()))
561+
num_out_of_service_bus: int = np.sum(np.isnan(net_mod.res_bus.va_degree.to_numpy()))
560562
outage_br_p0_series = net_mod["res_" + outage_branch_type][
561563
"p_" + THIS_RES_BR_SIDE_MAPPING[outage_branch_type] + "_mw"
562564
].copy()

pandapower/analysis/PSDF.py

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,56 +4,54 @@
44
# and Energy System Technology (IEE), Kassel. All rights reserved.
55

66
# Builds the DC PSDF matrix based on the DC PTDF
7-
import scipy as sp
7+
import logging
88
from math import pi
9+
10+
import scipy as sp
911
from scipy.sparse import csr_matrix, csc_matrix
12+
import pandas as pd
13+
import numpy as np
14+
import numpy.typing as npt
1015

1116
from pandapower.analysis.LODF import _LODF_ppci_to_pp, _LODF_pp_np_to_df
1217
from pandapower.analysis.PTDF import _makePTDF_ppci
1318
from pandapower.pypower.idx_brch import F_BUS, T_BUS
1419
from pandapower.pypower.idx_bus import BUS_TYPE, REF
1520
from pandapower.pypower.makeBdc import calc_b_from_branch
1621
from numpy import ones, r_, real, int64, arange, flatnonzero as find, isscalar
17-
18-
from typing import Union, Tuple
19-
20-
import pandas as pd
21-
import numpy as np
22-
2322
from pandapower import pandapowerNet
2423
from pandapower.analysis.utils import branch_dict_to_ppci_branch_list, _get_outage_branch_ix, ELE_IX_TYPE
2524

26-
import logging
2725
logger = logging.getLogger(__name__)
2826

29-
3027
def makePSDF(
3128
baseMVA: float,
32-
PTDF: np.ndarray,
33-
bus: np.ndarray,
34-
branch: np.ndarray,
29+
PTDF: npt.NDArray,
30+
bus: npt.NDArray,
31+
branch: npt.NDArray,
3532
using_sparse_solver: bool = False,
3633
branch_id: int | None = None,
3734
reduced: bool = False,
38-
slack: Union[int, np.ndarray] | None = None
35+
slack: int | npt.NDArray | None = None
3936
):
40-
"""Builds the DC PSDF matrix based on the DC PTDF
37+
"""
38+
Builds the DC PSDF matrix based on the DC PTDF
39+
4140
Returns the DC PSDF matrix . The matrix is
4241
C{nbr x nbr}, where C{nbr} is the number of branches. The DC PSDF is independent from the selected slack.
4342
To restrict the PSDF computation to a subset of branches, supply a list of ppci branch indices in C{branch_id}.
4443
If C{reduced==True}, the output is reduced to the branches given in C{branch_id}, otherwise the complement rows are set to NaN.
4544
@see: L{makeLODF}
4645
"""
47-
if reduced and not branch_id:
46+
if reduced and branch_id is None:
4847
raise ValueError("'reduced=True' is only valid if branch_id is not None")
4948

5049
## Select csc/csr B matrix
5150
sparse = csr_matrix if using_sparse_solver else csc_matrix
5251

5352
## use reference bus for slack by default
5453
if slack is None:
55-
slack = find(bus[:, BUS_TYPE] == REF)
56-
slack = slack[0]
54+
slack = find(bus[:, BUS_TYPE] == REF)[0]
5755

5856
## set the slack bus to be used to compute initial PTDF
5957
if isscalar(slack):
@@ -90,11 +88,11 @@ def makePSDF(
9088

9189
def _get_PSDF_direct(
9290
net: pandapowerNet,
93-
phase_shift_branch_type: str,
91+
phase_shift_branch_type: str | None,
9492
phase_shift_branch_ix: ELE_IX_TYPE | None = None,
9593
using_sparse_solver: bool = True,
9694
random_verify=False,
97-
branch_dict: dict[str, Union[list[int], None]] | None = None,
95+
branch_dict: dict[str, list[int] | None] | None = None,
9896
reduced=True,
9997
):
10098
"""
@@ -105,8 +103,8 @@ def _get_PSDF_direct(
105103
logger.warning("Calculating lodf for large network, switched to sparse solver!")
106104

107105
# If branch_dict not None compute list of ppci branch indices and its branch type intervals as lookup
108-
branch_ppci_lookup = None
109-
branch_id = None
106+
branch_ppci_lookup: dict | None = None
107+
branch_id: int | None = None
110108
if branch_dict is not None:
111109
branch_id, branch_ppci_lookup = branch_dict_to_ppci_branch_list(net=net, branch_dict=branch_dict)
112110
else:
@@ -159,11 +157,11 @@ def _get_PSDF_direct(
159157

160158
def _get_PSDF_perturb(
161159
net: pandapowerNet,
162-
phase_shift_branch_type: str,
160+
phase_shift_branch_type: str | None,
163161
phase_shift_branch_ix: ELE_IX_TYPE | None = None,
164162
distributed_slack=True,
165-
recycle="lodf",
166-
) -> dict[Tuple[str, str], pd.DataFrame]:
163+
recycle: str | None = "lodf",
164+
) -> dict[tuple[str, str], pd.DataFrame]:
167165
"""
168166
this function calculates PSDF (ratio without unit) of branch to
169167
a pp branch with perturb method (brute-force)
@@ -173,15 +171,15 @@ def _get_PSDF_perturb(
173171

174172
def run_PSDF(
175173
net: pandapowerNet,
176-
phase_shift_branch_type: Union[None, str],
174+
phase_shift_branch_type: str | None,
177175
phase_shift_branch_ix: ELE_IX_TYPE | None = None,
178176
distributed_slack: bool = True,
179177
perturb: bool = False,
180-
recycle: Union[str, None] = None,
178+
recycle: str | None = None,
181179
using_sparse_solver: bool = True,
182-
branch_dict: dict[str, Union[list[int], None]] | None = None,
180+
branch_dict: dict[str, list[int] | None] | None = None,
183181
reduced: bool = True,
184-
) -> dict[Tuple[str, str], pd.DataFrame]:
182+
) -> dict[tuple[str, str], pd.DataFrame]:
185183
"""
186184
this function is a wrapper of calculating PSDF of a pp branch from the phase shift through a pp branch
187185
with pypower matrix function or perturb function.
@@ -207,10 +205,10 @@ def run_PSDF(
207205
DataFrame(data=psdf, index=goal_branch_pp_index, columns=phase_shift_branch_ix)}
208206
"""
209207
# ToDo: check if distributed slack makes any difference
210-
if perturb and phase_shift_branch_type is None:
211-
logger.info("If a lot of branch required in psdf, please set perturb to False!")
212208

213209
if perturb:
210+
if phase_shift_branch_type is None:
211+
logger.info("If a lot of branch required in psdf, please set perturb to False!")
214212
if recycle == "lodf" and distributed_slack == True:
215213
logger.warning("distributed_slack deactivated! recycling does not allow distributed slack")
216214
psdf = _get_PSDF_perturb(

pandapower/analysis/PTDF.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@
22

33
# Copyright (c) 2016-2025 by University of Kassel and Fraunhofer Institute for Energy Economics
44
# and Energy System Technology (IEE), Kassel. All rights reserved.
5-
6-
from typing import Union, List, Dict
75
from copy import deepcopy
86

97
import pandas as pd
108
import numpy as np
9+
import numpy.typing as npt
1110

12-
from pandapower import pandapowerNet
11+
from pandapower.auxiliary import pandapowerNet
1312
from pandapower.analysis.utils import _get_bus_lookup, _get_branch_lookup, _get_trafo3w_lookup, \
1413
branch_dict_to_ppci_branch_list, _get_source_bus_ix, DISCONNECTED_PADDING_VALUE, BR_SIDE_MAPPING, BR_SIDE_MAPPING_1, \
1514
ELE_IX_TYPE
@@ -22,19 +21,18 @@
2221
# replace pandapower makePTDF with custom function
2322
from pandapower.pypower.makePTDF import makePTDF
2423
from pandapower.analysis.utils import get_dist_slack, get_ppci_dist_slack, LOAD_REFRENCE
25-
from pandapower.auxiliary import pandapowerNet
2624

2725
import logging
2826
logger = logging.getLogger(__name__)
2927

3028

3129
def _get_PTDF_direct(
3230
net: pandapowerNet,
33-
source_bus: Union[int, np.ndarray] | None = None,
31+
source_bus: int | npt.NDArray | None = None,
3432
result_side=0,
3533
using_sparse_solver: bool = True,
3634
random_verify: bool = True,
37-
branch_dict: dict[str, Union[list[int], None]] | None = None,
35+
branch_dict: dict[str, list[int] | None] | None = None,
3836
reduced: bool = True
3937
):
4038
"""
@@ -87,7 +85,7 @@ def _get_PTDF_direct(
8785

8886
def _get_PTDF_perturb(
8987
net: pandapowerNet,
90-
source_bus: Union[int, np.ndarray] | None = None,
88+
source_bus: int | npt.NDArray | None = None,
9189
result_side: int = 0,
9290
distributed_slack: bool=True
9391
):
@@ -332,13 +330,13 @@ def _PTDF_ppci_to_pp(net, ptdf_ppci, result_side, branch_ppci_lookup=None):
332330

333331
def run_PTDF(
334332
net: pandapowerNet,
335-
source_bus: Union[int, np.ndarray] | None = None,
333+
source_bus: int | npt.NDArray | None = None,
336334
distributed_slack: bool = True,
337335
result_side: int = 0,
338336
perturb: bool = False,
339337
using_sparse_solver: bool = True,
340338
random_verify: bool = False,
341-
branch_dict: dict[str, Union[list[int], None]] | None = None,
339+
branch_dict: dict[str, list[int] | None] | None = None,
342340
reduced: bool = True,
343341
):
344342
"""

0 commit comments

Comments
 (0)