Skip to content

Commit

Permalink
Merge pull request #309 from openego/feature/log_grid_ids
Browse files Browse the repository at this point in the history
Feature/log grid ids
  • Loading branch information
birgits committed Jan 11, 2023
2 parents a01d0e5 + 4ee1352 commit b109630
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 22 deletions.
30 changes: 30 additions & 0 deletions edisgo/edisgo.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,36 @@ def __init__(self, **kwargs):
generator_scenario=kwargs.pop("generator_scenario"), **kwargs
)

# add MVGrid id to logging messages of logger "edisgo"
log_grid_id = kwargs.get("log_grid_id", True)
if log_grid_id:

def add_grid_id_filter(record):
record.grid_id = self.topology.id
return True

file_formatter = logging.Formatter(
"%(asctime)s - %(name)s - %(levelname)s - "
"MVGrid(%(grid_id)s): %(message)s"
)
stream_formatter = logging.Formatter(
"%(name)s - %(levelname)s - MVGrid(%(grid_id)s): %(message)s"
)

logger_edisgo = logging.getLogger("edisgo")
for handler in logger_edisgo.handlers:
if isinstance(logger_edisgo.handlers[0], logging.StreamHandler):
handler.setFormatter(stream_formatter)
elif isinstance(logger_edisgo.handlers[0], logging.FileHandler):
handler.setFormatter(file_formatter)
else:
raise ValueError(
"Disable the log_grid_id function when using other"
" handlers than StreamHandler or FileHandler"
)
handler.filters.clear()
handler.addFilter(add_grid_id_filter)

@property
def config(self):
"""
Expand Down
17 changes: 8 additions & 9 deletions edisgo/network/topology.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import logging
import os
import random
import warnings

from zipfile import ZipFile

Expand Down Expand Up @@ -948,7 +947,7 @@ def _check_bus_for_removal(self, bus_name):
"""
# check if bus is part of topology
if bus_name not in self.buses_df.index:
warnings.warn(
logger.warning(
"Bus of name {} not in Topology. Cannot be removed.".format(bus_name)
)
return False
Expand Down Expand Up @@ -990,7 +989,7 @@ def _check_line_for_removal(self, line_name):
"""
# check if line is part of topology
if line_name not in self.lines_df.index:
warnings.warn(
logger.warning(
"Line of name {} not in Topology. Cannot be "
"removed.".format(line_name)
)
Expand Down Expand Up @@ -1360,7 +1359,7 @@ def _get_line_data():
# if type of line is specified calculate x, r and s_nom
if type_info is not None:
if x is not None or r is not None or b is not None or s_nom is not None:
warnings.warn(
logger.warning(
"When line 'type_info' is provided when creating a new "
"line, x, r, b and s_nom are calculated and provided "
"parameters are overwritten."
Expand Down Expand Up @@ -1393,7 +1392,7 @@ def _get_line_data():
"Newly added line has no line resistance and/or reactance."
)
if s_nom is None:
warnings.warn("Newly added line has no nominal power.")
logger.warning("Newly added line has no nominal power.")

new_line_df = pd.DataFrame(
data={
Expand Down Expand Up @@ -1568,7 +1567,7 @@ def remove_line(self, name):
"""
if not self._check_line_for_removal(name):
warnings.warn(
logger.warning(
f"Removal of line {name} would create isolated node. Remove all "
"connected elements first to remove bus."
)
Expand Down Expand Up @@ -1612,7 +1611,7 @@ def remove_bus(self, name):
conn_comp = self.get_connected_components_from_bus(name)
conn_comp_types = [k for k, v in conn_comp.items() if not v.empty]
if len(conn_comp_types) > 0:
warnings.warn(
logger.warning(
f"Bus {name} is not isolated and therefore not removed. Remove all "
f"connected elements ({conn_comp_types}) first to remove bus."
)
Expand Down Expand Up @@ -2122,7 +2121,7 @@ def _choose_random_substation_id():
else:
# ToDo
# lv_grid = _choose_random_substation_id()
# warnings.warn(
# logger.warning(
# "Given mvlv_subst_id does not exist, wherefore a random "
# "LV Grid ({}) to connect in is chosen.".format(
# lv_grid.id
Expand All @@ -2134,7 +2133,7 @@ def _choose_random_substation_id():
# if no MV/LV substation ID is given, choose random LV grid
else:
lv_grid = _choose_random_substation_id()
warnings.warn(
logger.warning(
"Component has no mvlv_subst_id. It is therefore allocated "
f"to a random LV Grid ({lv_grid.id})."
)
Expand Down
10 changes: 9 additions & 1 deletion edisgo/tools/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ def setup_logger(
reset_loggers : bool
If True the handlers of all loggers are cleared before configuring the loggers.
Only use if you know what you do, it could be dangerous.
Examples
--------
Expand Down Expand Up @@ -151,7 +152,11 @@ def create_home_dir():
]

for logger in existing_loggers:
logger.handlers.clear()
for handler in logger.handlers:
if not isinstance(handler, logging.NullHandler):
if debug_message:
print(f"Removed {handler} of Logger: {logger}")
logger.removeHandler(handler)

loglevel_dict = {
"debug": logging.DEBUG,
Expand Down Expand Up @@ -184,6 +189,9 @@ def create_home_dir():
logger = logging.getLogger(logger_name)
logger.propagate = False

# clear existing handlers for the logger
logger.handlers.clear()

if logger_file_level < logger_stream_level:
logger.setLevel(logger_file_level)
else:
Expand Down
2 changes: 1 addition & 1 deletion examples/plot_example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@
"outputs": [],
"source": [
"plot_dash(edisgo_objects={\"edisgo_analyzed\": edisgo_analyzed, \"edisgo_reinforced\": edisgo_reinforced})\n",
"time.sleep(1)"
"time.sleep(2)"
]
},
{
Expand Down
27 changes: 16 additions & 11 deletions tests/network/test_topology.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ def test_add_storage_unit(self):
with pytest.raises(ValueError, match=msg):
self.topology.add_storage_unit(bus="Unknown_bus", p_nom=1, control="PQ")

def test_add_line(self):
def test_add_line(self, caplog):
"""Test add_line method"""

len_df_before = len(self.topology.lines_df)
Expand All @@ -422,7 +422,7 @@ def test_add_line(self):
"line, x, r, b and s_nom are calculated and provided "
"parameters are overwritten."
)
with pytest.warns(UserWarning, match=msg):
with caplog.at_level(logging.WARNING):
name = self.topology.add_line(
bus0=bus0,
bus1=bus1,
Expand All @@ -431,6 +431,7 @@ def test_add_line(self):
type_info="NA2XS2Y 3x1x185 RM/25",
x=2,
)
assert msg in caplog.text
assert len_df_before + 2 == len(self.topology.lines_df)
assert name == "Line_Bus_BranchTee_MVGrid_1_8_Bus_BranchTee_LVGrid_1_10"
assert np.isclose(self.topology.lines_df.at[name, "s_nom"], 6.18342)
Expand Down Expand Up @@ -502,11 +503,12 @@ def test_add_bus(self):
assert self.topology.buses_df.at[name, "lv_grid_id"] == 1
assert self.topology.buses_df.at[name, "mv_grid_id"] == 1

def test_check_bus_for_removal(self):
def test_check_bus_for_removal(self, caplog):
# test warning if line does not exist
msg = "Bus of name TestBus not in Topology. Cannot be removed."
with pytest.warns(UserWarning, match=msg):
with caplog.at_level(logging.WARNING):
self.topology._check_bus_for_removal("TestBus")
assert msg in caplog.text
return_value = self.topology._check_bus_for_removal("TestBus")
assert not return_value

Expand All @@ -532,12 +534,13 @@ def test_check_bus_for_removal(self):
)
assert return_value

def test_check_line_for_removal(self):
def test_check_line_for_removal(self, caplog):

# test warning if line does not exist
msg = "Line of name TestLine not in Topology. Cannot be removed."
with pytest.warns(UserWarning, match=msg):
with caplog.at_level(logging.WARNING):
self.topology._check_line_for_removal("TestLine")
assert msg in caplog.text
return_value = self.topology._check_line_for_removal("TestLine")
assert not return_value

Expand Down Expand Up @@ -673,13 +676,14 @@ def test_remove_storage_unit(self):
assert bus in self.topology.buses_df.index
assert (connected_lines.index.isin(self.topology.lines_df.index)).all()

def test_remove_line(self):
def test_remove_line(self, caplog):
"""Test remove_line method"""

# test try removing line that cannot be removed
msg = "Removal of line Line_30000010 would create isolated node."
with pytest.warns(UserWarning, match=msg):
with caplog.at_level(logging.WARNING):
self.topology.remove_line("Line_30000010")
assert msg in caplog.text

# test remove line in cycle (no bus is removed)
# add line to create ring
Expand Down Expand Up @@ -715,16 +719,17 @@ def test_remove_line(self):
assert "Bus_BranchTee_LVGrid_3_6" not in self.topology.buses_df.index
assert "Bus_BranchTee_LVGrid_3_5" in self.topology.buses_df.index

def test_remove_bus(self):
def test_remove_bus(self, caplog):
"""Test remove_bus method"""

# test bus cannot be removed
msg = (
"Bus Bus_BranchTee_LVGrid_4_2 is not isolated and "
"therefore not removed. Remove all connected elements *"
"therefore not removed. Remove all connected elements "
)
with pytest.warns(UserWarning, match=msg):
with caplog.at_level(logging.WARNING):
self.topology.remove_bus("Bus_BranchTee_LVGrid_4_2")
assert msg in caplog.text

# test bus can be removed
# create isolated bus
Expand Down

0 comments on commit b109630

Please sign in to comment.