Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix which repairs set_line_geodata if the net doesn't have geodata fo… #2123

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
a6f42a9
fix which repairs set_line_geodata if the net doesn't have geodata fo…
vogt31337 Sep 4, 2023
ff3fe5b
Merge remote-tracking branch 'pp/develop' into feature/repaired_set_l…
KS-HTK Oct 11, 2023
f13d9bf
added line to CHANGELOG.rst
KS-HTK Oct 11, 2023
75501cc
fixed issue in coords_from_node_geodata.
KS-HTK Oct 12, 2023
a419bf9
Merge pull request #1 from KS-HTK/feature/repaired_set_line_geodata
vogt31337 Oct 12, 2023
d640605
Merge branch 'develop' into feature/repaired_set_line_geodata
SteffenMeinecke Nov 29, 2023
71ce6bf
Merge branch 'develop' into feature/repaired_set_line_geodata
SteffenMeinecke Nov 29, 2023
23d1f86
Merge branch 'develop' into feature/repaired_set_line_geodata
rbolgaryn Mar 26, 2024
3120565
Merge branch 'develop' of https://github.com/e2nIEE/pandapower into f…
SteffenMeinecke Jun 3, 2024
0514ac9
Merge branch 'develop' into feature/repaired_set_line_geodata
KS-HTK Jun 4, 2024
4c86a13
tiny fix
SteffenMeinecke Jun 5, 2024
4bc0358
Merge branch 'develop' of https://github.com/e2nIEE/pandapower into f…
SteffenMeinecke Jun 5, 2024
14b6063
Merge branch 'feature/repaired_set_line_geodata' of https://github.co…
SteffenMeinecke Jun 5, 2024
c2ee5fa
Merge branch 'develop' into feature/repaired_set_line_geodata
SteffenMeinecke Jun 6, 2024
3aafb13
Merge branch 'develop' into feature/repaired_set_line_geodata
SteffenMeinecke Jun 11, 2024
eaacef8
fix missing l in isnull()
SteffenMeinecke Jun 11, 2024
9d3429c
avoid pandas FutureWarnings
SteffenMeinecke Jun 11, 2024
5b7ce75
Merge branch 'develop' into feature/repaired_set_line_geodata
KS-HTK Jun 12, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ Change Log
- [FIXED] powerfactory2pandapower-converter error if a line has two identical coordinates
- [ADDED] logger messages about the probabilistic load flow calculation (simultaneities) in the powerfactory2pandapower-converter for low voltage loads
- [ADDED] matplotlib v3.8.0 support (fixed :code:`plotting_colormaps.ipynb`)
- [FIXED] bug in plotting_toolbox.py (fixed :code:`coords_from_node_geodata` and :code:`set_line_geodata_from_bus_geodata`)
- [CHANGED] PowerFactory converter - name :code:`for_name` as :code:`equipment` for all elements; also add to line
- [ADDED] option to use a second tap changer for the trafo element
- [CHANGED] parameters of function merge_internal_net_and_equivalent_external_net()
Expand Down
21 changes: 12 additions & 9 deletions pandapower/plotting/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,8 @@ def _get_coords_from_geojson(gj_str):
return ast.literal_eval(m)
return None

if use_bus_geodata is False and line_geodata is None and ("geo" not in net.line.columns or net.line.geo.empty):
if use_bus_geodata is False and line_geodata is None and (
"geo" not in net.line.columns or net.line.geo.isnull().all()):
# if bus geodata is available, but no line geodata
logger.warning("use_bus_geodata is automatically set to True, since net.line.geo is empty.")
use_bus_geodata = True
Expand All @@ -507,18 +508,20 @@ def _get_coords_from_geojson(gj_str):
if len(lines) == 0:
return None

line_geodata: Series[str] = line_geodata.loc[lines] if line_geodata is not None else net.line.geo.loc[lines]
line_geodata: Series[str] = line_geodata.loc[lines] if line_geodata is not None else \
net.line.geo.loc[lines]
lines_without_geo = line_geodata.index[line_geodata.isna()]

if use_bus_geodata or not lines_without_geo.empty:
elem_indices = lines if use_bus_geodata else lines_without_geo
geos, line_index_successful = coords_from_node_geodata(element_indices=elem_indices,
from_nodes=net.line.loc[elem_indices, 'from_bus'].values,
to_nodes=net.line.loc[elem_indices, 'to_bus'].values,
node_geodata=net.bus.geo,
table_name="line",
node_name="bus",
ignore_zero_length=True)
geos, line_index_successful = coords_from_node_geodata(
element_indices=elem_indices,
from_nodes=net.line.loc[elem_indices, 'from_bus'].values,
to_nodes=net.line.loc[elem_indices, 'to_bus'].values,
node_geodata=net.bus.geo,
table_name="line",
node_name="bus",
ignore_zero_length=True)

line_geodata = line_geodata.combine_first(pd.Series(geos, index=line_index_successful))

Expand Down
71 changes: 42 additions & 29 deletions pandapower/plotting/plotting_toolbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ def _rotate_dim2(arr, ang):


def _get_coords_from_geojson(gj_str):
if pd.isnull(gj_str):
return gj_str
pattern = r'"coordinates"\s*:\s*((?:\[(?:\[[^]]+],?\s*)+\])|\[[^]]+\])'
matches = re.findall(pattern, gj_str)

Expand Down Expand Up @@ -168,21 +170,32 @@ def coords_from_node_geodata(element_indices, from_nodes, to_nodes, node_geodata
- elements_with_geo (set) - the indices of branch elements for which coordinates wer found \
in the node geodata table
"""
have_geo = np.isin(from_nodes, node_geodata.index.values) \
# reduction of from_nodes, to_nodes, node_geodata to intersection
in_geo = np.isin(from_nodes, node_geodata.index.values) \
& np.isin(to_nodes, node_geodata.index.values)
elements_with_geo = np.array(element_indices)[have_geo]
fb_with_geo, tb_with_geo = from_nodes[have_geo], to_nodes[have_geo]
fb_with_geo, tb_with_geo = from_nodes[in_geo], to_nodes[in_geo]
if sum(~in_geo):
logger.warning(
f"No coords found for {table_name}s {np.array(element_indices)[~in_geo]}. {node_name} "
f"geodata is missing for those {table_name}s!"
)

# reduction to not nans
fb_geo_is_nan = node_geodata.loc[fb_with_geo].isnull().values
tb_geo_is_nan = node_geodata.loc[tb_with_geo].isnull().values
not_nan = ~(fb_geo_is_nan | tb_geo_is_nan)
if n_nans := len(not_nan) - sum(not_nan):
logger.warning(
f"NaN coords are returned {n_nans} times for {table_name}s applying {node_name} geodata."
)

node_geodata = node_geodata.apply(_get_coords_from_geojson)
coords = [f'{{"coordinates": [[{x_from}, {y_from}], [{x_to}, {y_to}]], "type": "LineString"}}'
for [x_from, y_from], [x_to, y_to]
in zip(node_geodata.loc[fb_with_geo], node_geodata.loc[tb_with_geo])
in zip(node_geodata.loc[fb_with_geo[not_nan]],
node_geodata.loc[tb_with_geo[not_nan]])
if not ignore_zero_length or (ignore_zero_length and not (x_from == x_to and y_from == y_to))]
elements_without_geo = set(element_indices) - set(elements_with_geo)
if len(elements_without_geo) > 0:
logger.warning(
f"No coords found for {table_name}s {elements_without_geo}. {node_name} geodata is missing for those {table_name}s!"
)
return coords, elements_with_geo
return coords, np.array(element_indices)[in_geo & not_nan]


def set_line_geodata_from_bus_geodata(net, line_index=None, overwrite=False, ignore_zero_length=True):
Expand All @@ -195,26 +208,26 @@ def set_line_geodata_from_bus_geodata(net, line_index=None, overwrite=False, ign
:return: None
"""
if 'geo' not in net.bus.columns or net.bus.geo.isnull().all():
logger.warning("The function set_line_geodata_from_bus_geodata requires geodata to be present in net.bus.geo")
logger.warning("The function set_line_geodata_from_bus_geodata requires geodata to be "
"present in net.bus.geo")
return
line_index = line_index if line_index is not None else net.line.index
if not overwrite:
# line_index = np.setdiff1d(line_index, net.line_geodata.index)
try:
line_index = net.line.geo.index[net.line.geo.isnull()]
except AttributeError:
# If line.geo is not found, just net.line.index
pass

geos, line_index_successful = coords_from_node_geodata(element_indices=line_index,
from_nodes=net.line.loc[line_index, 'from_bus'].values,
to_nodes=net.line.loc[line_index, 'to_bus'].values,
node_geodata=net.bus.geo,
table_name="line",
node_name="bus",
ignore_zero_length=ignore_zero_length)

net.line.loc[line_index_successful, 'geo'] = pd.Series(geos)
if overwrite and line_index is None:
line_index = net.line.index
elif not overwrite and line_index is None:
line_index = net.line.index[net.line.geo.isnull()]
elif not overwrite and line_index is not None:
line_index = pd.Index(line_index).intersection(net.line.index[net.line.geo.isnull()])

geos, line_index_successful = coords_from_node_geodata(
element_indices=line_index,
from_nodes=net.line.loc[line_index, 'from_bus'].values,
to_nodes=net.line.loc[line_index, 'to_bus'].values,
node_geodata=net.bus.geo,
table_name="line",
node_name="bus",
ignore_zero_length=ignore_zero_length)

net.line.loc[line_index_successful, 'geo'] = geos

num_failed = len(line_index) - len(line_index_successful)
if num_failed > 0:
Expand Down
6 changes: 4 additions & 2 deletions pandapower/plotting/simple_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
from pandapower.plotting.plotting_toolbox import get_collection_sizes
from pandapower.plotting.collections import create_bus_collection, create_line_collection, \
create_trafo_collection, create_trafo3w_collection, \
create_line_switch_collection, draw_collections, create_bus_bus_switch_collection, create_ext_grid_collection, create_sgen_collection, \
create_line_switch_collection, draw_collections, create_bus_bus_switch_collection, \
create_ext_grid_collection, create_sgen_collection, \
create_gen_collection, create_load_collection, create_dcline_collection
from pandapower.plotting.generic_geodata import create_generic_coordinates

Expand All @@ -26,7 +27,8 @@


def simple_plot(net, respect_switches=False, line_width=1.0, bus_size=1.0, ext_grid_size=1.0,
trafo_size=1.0, plot_loads=False, plot_gens=False, plot_sgens=False, load_size=1.0, gen_size=1.0, sgen_size=1.0,
trafo_size=1.0, plot_loads=False, plot_gens=False, plot_sgens=False, load_size=1.0,
gen_size=1.0, sgen_size=1.0,
switch_size=2.0, switch_distance=1.0, plot_line_switches=False, scale_size=True,
bus_color='b', line_color='grey', dcline_color='c', trafo_color='k',
ext_grid_color='y', switch_color='k', library='igraph', show_plot=True, ax=None):
Expand Down
80 changes: 80 additions & 0 deletions pandapower/test/plotting/test_plotting_toolbox.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# -*- coding: utf-8 -*-

# Copyright (c) 2016-2024 by University of Kassel and Fraunhofer Institute for Energy Economics
# and Energy System Technology (IEE), Kassel. All rights reserved.

from copy import deepcopy
import pytest
import pandas as pd

import pandapower as pp


def test_set_line_geodata_from_bus_geodata():
net = pp.networks.case9()
bus_geo_data = deepcopy(net.bus.geo)

empty_line_geo = pd.Series(None, index=net.line.index, dtype=object) # ensure that line geo data
assert not net.bus.geo.isnull().any() # ensure that bus geo data is available

pp.plotting.simple_plot(net) # test that plotting works with case9 file

# --- create line geo data from complete net.bus.geo to empty net.line.geo
net.line.geo = deepcopy(empty_line_geo) # ensure that line geo data is missing
pp.plotting.set_line_geodata_from_bus_geodata(net)
assert not net.line.geo.isnull().any()
pp.plotting.simple_plot(net) # test that plotting works with case9 file

# --- create line geo data from complete net.bus.geo to incomplete net.line.geo
net.line.at[2, "geo"] = None
net.line.at[4, "geo"] = None
pp.plotting.set_line_geodata_from_bus_geodata(net)
assert not net.line.geo.isnull().any()
pp.plotting.simple_plot(net) # test that plotting works with case9 file

# --- create line geo data from complete net.bus.geo to incomplete net.line.geo using overwrite
net.line.at[2, "geo"] = None
net.line.at[4, "geo"] = None
pp.plotting.set_line_geodata_from_bus_geodata(net, overwrite=True)
assert not net.line.geo.isnull().any()
pp.plotting.simple_plot(net) # test that plotting works with case9 file

# --- create line geo data from incomplete net.bus.geo to incomplete net.line.geo
# (-> no warning expected since all missing data can be filled by available bus data)
net.bus.at[2, "geo"] = None
net.bus.at[4, "geo"] = None
net.line.at[0, "geo"] = None
net.line.at[5, "geo"] = None
pp.plotting.set_line_geodata_from_bus_geodata(net)
assert not net.line.geo.isnull().any()
net.bus.at[2, "geo"] = bus_geo_data.at[2]
net.bus.at[4, "geo"] = bus_geo_data.at[4]
pp.plotting.simple_plot(net) # test that plotting works with case9 file

# --- create line geo data from incomplete net.bus.geo to incomplete net.line.geo
# using line_index (-> no warning expected since all missing data can be filled by available bus
# data)
net.bus.at[2, "geo"] = None
net.bus.at[4, "geo"] = None
net.line.at[6, "geo"] = None
net.line.at[7, "geo"] = None
pp.plotting.set_line_geodata_from_bus_geodata(net, line_index=[6, 7])
assert not net.line.geo.isnull().any()
net.bus.at[2, "geo"] = bus_geo_data.at[2]
net.bus.at[4, "geo"] = bus_geo_data.at[4]
pp.plotting.simple_plot(net) # test that plotting works with case9 file

# --- create line geo data from incomplete net.bus.geo to empty net.line.geo
# (-> warning expected)
net.bus.at[2, "geo"] = None
net.bus.at[4, "geo"] = None
net.line.geo = deepcopy(empty_line_geo) # ensure that line geo data is missing
pp.plotting.set_line_geodata_from_bus_geodata(net)


if __name__ == "__main__":
if 0:
pytest.main([__file__])
else:
test_set_line_geodata_from_bus_geodata()
pass
8 changes: 4 additions & 4 deletions pandapower/test/shortcircuit/test_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Copyright (c) 2016-2024 by University of Kassel and Fraunhofer Institute for Energy Economics
# and Energy System Technology (IEE), Kassel. All rights reserved.


import copy
import pandas as pd
import pytest

Expand Down Expand Up @@ -85,15 +85,15 @@ def test_gen_ext_grid_same_bus():
net = pp.create_empty_network()
b = pp.create_bus(net, 110)

net1 = net.deepcopy()
net1 = copy.deepcopy(net)
pp.create_ext_grid(net1, b, s_sc_max_mva=1000, rx_max=0.4)
sc.calc_sc(net1)

net2 = net.deepcopy()
net2 = copy.deepcopy(net)
pp.create_gen(net2, b, 0, sn_mva=50, vn_kv=115, xdss_pu=0.2, rdss_ohm=20, cos_phi=0.8, pg_percent=0)
sc.calc_sc(net2)

net3 = net1.deepcopy()
net3 = copy.deepcopy(net1)
pp.create_gen(net3, b, 0, sn_mva=50, vn_kv=115, xdss_pu=0.2, rdss_ohm=20, cos_phi=0.8, pg_percent=0)
sc.calc_sc(net3)

Expand Down
12 changes: 6 additions & 6 deletions pandapower/test/shortcircuit/test_iec60909_4.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,10 +327,10 @@ def test_iec_60909_4_3ph_small_with_gen():
def test_iec_60909_4_3ph_small_with_gen_xward():
net = iec_60909_4_small(with_xward=True)
sc.calc_sc(net, fault="3ph", case="max", ip=True, tk_s=0.1, kappa_method="C")

ikss_pf = [40.6422, 31.6394, 16.7409, 33.2808]
assert np.allclose(net.res_bus_sc.ikss_ka.values[:4], np.array(ikss_pf), atol=1e-3)


def test_iec_60909_4_3ph_small_gen_only():
net = iec_60909_4_small_gen_only()
Expand Down Expand Up @@ -364,7 +364,7 @@ def test_iec_60909_4_3ph_2gen_no_ps_detection():
net.gen.at[0, "in_service"] = False
net.gen = net.gen.query("in_service")
sc.calc_sc(net, fault="3ph", case="max", ip=True, tk_s=0.1, kappa_method="C")

ikss_pf = [1.8460, 1.6715, 6.8953, 39.5042]
assert np.allclose(net.res_bus_sc.ikss_ka[:4].values, np.array(ikss_pf), atol=1e-3)

Expand Down Expand Up @@ -463,11 +463,11 @@ def test_iec_60909_4_1ph():

def test_detect_power_station_units():
net = iec_60909_4()
net.gen.power_station_trafo[:] = None
net.gen.power_station_trafo.loc[:] = None

detect_power_station_unit(net)
assert np.all(net.gen.power_station_trafo.values[[0, 1]] == np.array([0, 1]))
net.gen.power_station_trafo[:] = None
net.gen.power_station_trafo.loc[:] = None

detect_power_station_unit(net, mode="trafo")
assert np.all(net.gen.power_station_trafo.values[[0, 1]] == np.array([0, 1]))
Expand All @@ -483,7 +483,7 @@ def test_vde_232():
net = vde_232()
sc.calc_sc(net, fault="3ph", case="max", ip=True, tk_s=0.1, kappa_method="C")



if __name__ == '__main__':
pytest.main([__file__, "-xs"])
2 changes: 1 addition & 1 deletion pandapower/test/shortcircuit/test_meshing_detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def meshed_grid():
net = pp.from_json(os.path.join(pp.pp_dir, "test", "shortcircuit", "sc_test_meshed_grid.json"))
bid = pp.create_bus(net, vn_kv=10.)
pp.create_switch(net, net.ext_grid.bus.iloc[0], bid, et="b")
net.ext_grid.bus.iloc[0] = bid
net.ext_grid.loc[net.ext_grid.index[0], "bus"] = bid
pp.create_bus(net, vn_kv=0.4, in_service=False)
return net

Expand Down
8 changes: 4 additions & 4 deletions pandapower/test/toolbox/test_comparison.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def test_nets_equal():
net = copy.deepcopy(original)

# detecting alternated value
net["load"]["p_mw"][net["load"].index[0]] += 0.1
net["load"].loc[net["load"].index[0], "p_mw"] += 0.1
assert not pandapower.toolbox.nets_equal(original, net)
assert not pandapower.toolbox.nets_equal(net, original)
net = copy.deepcopy(original)
Expand All @@ -46,14 +46,14 @@ def test_nets_equal():
net = copy.deepcopy(original)

# not detecting alternated value if difference is beyond tolerance
net["load"]["p_mw"][net["load"].index[0]] += 0.0001
net["load"].loc[net["load"].index[0], "p_mw"] += 0.0001
assert pandapower.toolbox.nets_equal(original, net, atol=0.1)
assert pandapower.toolbox.nets_equal(net, original, atol=0.1)

# check controllers
original.trafo.tap_side = original.trafo.tap_side.fillna("hv")
net1 = original.deepcopy()
net2 = original.deepcopy()
net1 = copy.deepcopy(original)
net2 = copy.deepcopy(original)
pp.control.ContinuousTapControl(net1, 0, 1.0)
pp.control.ContinuousTapControl(net2, 0, 1.0)
c1 = net1.controller.at[0, "object"]
Expand Down
16 changes: 8 additions & 8 deletions pandapower/test/toolbox/test_grid_modification.py
Original file line number Diff line number Diff line change
Expand Up @@ -847,8 +847,8 @@ def test_repl_to_line_with_switch():
pp.create_switch(net, bus=bus, element=REPL, closed=False, et="l", type="LBS")

# calculate runpp with REPL
net.line.in_service[testindex] = False
net.line.in_service[REPL] = True
net.line.in_service.loc[testindex] = False
net.line.in_service.loc[REPL] = True
pp.runpp(net)

fbus_repl = net.res_bus.loc[fbus]
Expand All @@ -860,9 +860,9 @@ def test_repl_to_line_with_switch():
# get ne line impedances
new_idx = pp.repl_to_line(net, testindex, std, in_service=True)
# activate new idx line
net.line.in_service[REPL] = False
net.line.in_service[testindex] = True
net.line.in_service[new_idx] = True
net.line.in_service.loc[REPL] = False
net.line.in_service.loc[testindex] = True
net.line.in_service.loc[new_idx] = True
pp.runpp(net)
# compare lf results
fbus_ne = net.res_bus.loc[fbus]
Expand All @@ -880,9 +880,9 @@ def test_repl_to_line_with_switch():
assert np.isclose(qloss_repl, qloss_ne)

# and reset to unreinforced state again
net.line.in_service[testindex] = True
net.line.in_service[new_idx] = False
net.line.in_service[REPL] = False
net.line.in_service.loc[testindex] = True
net.line.in_service.loc[new_idx] = False
net.line.in_service.loc[REPL] = False


def test_merge_parallel_line():
Expand Down
Loading