Skip to content

Commit 4d340b6

Browse files
author
yassine abdou
committed
fix : bug logur
1 parent 7fbbadf commit 4d340b6

File tree

2 files changed

+39
-14
lines changed

2 files changed

+39
-14
lines changed

grid2op/Backend/protectionScheme.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import copy
2-
from loguru import logger
2+
import logging
33
from typing import Tuple, Union, Any, List, Optional
44
import numpy as np
55

@@ -20,6 +20,7 @@ def __init__(
2020
parameters: Parameters,
2121
thermal_limits: ThermalLimits,
2222
is_dc: bool = False,
23+
logger: Optional[logging.Logger] = None,
2324
):
2425
"""
2526
Initialise l'état du réseau avec des protections personnalisables.
@@ -42,6 +43,12 @@ def __init__(
4243
self._timestep_overflow = np.zeros(self.thermal_limits.n_line, dtype=dt_int)
4344
self.infos: List[str] = []
4445

46+
if logger is None:
47+
self.logger = logging.getLogger(__name__)
48+
self.logger.disabled = True
49+
else:
50+
self.logger: logging.Logger = logger.getChild("grid2op_BaseEnv")
51+
4552
def _validate_input(self, backend: Backend, parameters: Optional[Parameters]) -> None:
4653
if not isinstance(backend, Backend):
4754
raise Grid2OpException(f"Argument 'backend' doit être de type 'Backend', reçu : {type(backend)}")
@@ -55,12 +62,18 @@ def _run_power_flow(self) -> Optional[Exception]:
5562
try:
5663
return self.backend._runpf_with_diverging_exception(self.is_dc)
5764
except Exception as e:
58-
logger.error(f"Erreur flux de puissance : {e}")
65+
if self.logger is not None:
66+
self.logger.error(
67+
f"Erreur flux de puissance : {e}"
68+
)
5969
return e
6070

6171
def _update_overflows(self, lines_flows: np.ndarray) -> np.ndarray:
6272
if self._thermal_limit_a is None:
63-
logger.error("Thermal limits must be provided for overflow calculations.")
73+
if self.logger is not None:
74+
self.logger.error(
75+
"Thermal limits must be provided for overflow calculations."
76+
)
6477
raise ValueError("Thermal limits must be provided for overflow calculations.")
6578

6679
lines_status = self.backend.get_line_status() # self._thermal_limit_a reste fixe. self._soft_overflow_threshold = 1
@@ -77,7 +90,9 @@ def _disconnect_lines(self, lines_to_disconnect: np.ndarray, timestep: int) -> N
7790
for line_idx in np.where(lines_to_disconnect)[0]:
7891
self.backend._disconnect_line(line_idx)
7992
self.disconnected_during_cf[line_idx] = timestep
80-
logger.warning(f"Ligne {line_idx} déconnectée au pas de temps {timestep}.")
93+
if self.logger is not None:
94+
self.logger.warning(f"Ligne {line_idx} déconnectée au pas de temps {timestep}.")
95+
8196

8297
def next_grid_state(self) -> Tuple[np.ndarray, List[Any], Union[None, Exception]]:
8398
try:
@@ -99,7 +114,8 @@ def next_grid_state(self) -> Tuple[np.ndarray, List[Any], Union[None, Exception]
99114
return self.disconnected_during_cf, self.infos, None
100115

101116
except Exception as e:
102-
logger.exception("Erreur inattendue dans le calcul de l'état du réseau.")
117+
if self.logger is not None:
118+
self.exception("Erreur inattendue dans le calcul de l'état du réseau.")
103119
return self.disconnected_during_cf, self.infos, e
104120

105121
class NoProtection:

grid2op/Backend/thermalLimits.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
except ImportError:
55
from typing_extensions import Self
66

7-
from loguru import logger
7+
import logging
88
import numpy as np
99
import copy
1010

@@ -21,7 +21,8 @@ def __init__(
2121
self,
2222
_thermal_limit_a: Optional[np.ndarray] = None,
2323
line_names: Optional[List[str]] = None,
24-
n_line: Optional[int] = None
24+
n_line: Optional[int] = None,
25+
logger: Optional[logging.Logger] = None,
2526
):
2627
"""
2728
Initializes the thermal limits manager.
@@ -43,8 +44,12 @@ def __init__(
4344
self._n_line = n_line
4445
self._name_line = line_names
4546

46-
logger.info(f"ThermalLimits initialized with {self.n_line} limits.")
47-
47+
if logger is None:
48+
self.logger = logging.getLogger(__name__)
49+
self.logger.disabled = True
50+
else:
51+
self.logger: logging.Logger = logger.getChild("grid2op_BaseEnv")
52+
4853
@property
4954
def n_line(self) -> int:
5055
return self._n_line
@@ -54,8 +59,9 @@ def n_line(self, new_n_line: int) -> None:
5459
if new_n_line <= 0:
5560
raise ValueError("Number of lines must be a positive integer.")
5661
self._n_line = new_n_line
57-
logger.info(f"Number of lines updated to {self._n_line}.")
58-
62+
if self.logger is not None:
63+
self.logger.info(f"Number of lines updated to {self._n_line}.")
64+
5965
@property
6066
def name_line(self) -> Union[List[str], np.ndarray]:
6167
return self._name_line
@@ -75,7 +81,8 @@ def name_line(self, new_name_line: Union[List[str], np.ndarray]) -> None:
7581
raise ValueError("Length of name list must match the number of lines.")
7682

7783
self._name_line = new_name_line
78-
logger.info(f"Power line names updated")
84+
if self.logger is not None:
85+
self.logger.info("Power line names updated")
7986

8087
@property
8188
def limits(self) -> np.ndarray:
@@ -185,7 +192,8 @@ def env_limits(self, thermal_limit):
185192
)
186193

187194
self._thermal_limit_a = tmp
188-
logger.info("Env thermal limits successfully set.")
195+
if self.logger is not None:
196+
self.logger.info("Env thermal limits successfully set.")
189197

190198
def update_limits_from_vector(self, thermal_limit_a: np.ndarray) -> None:
191199
"""
@@ -196,7 +204,8 @@ def update_limits_from_vector(self, thermal_limit_a: np.ndarray) -> None:
196204
"""
197205
thermal_limit_a = np.array(thermal_limit_a).astype(dt_float)
198206
self._thermal_limit_a = thermal_limit_a
199-
logger.info("Thermal limits updated from vector.")
207+
if self.logger is not None:
208+
self.logger.info("Thermal limits updated from vector.")
200209

201210
def update_limits(self, env : "grid2op.Environment.BaseEnv") -> None:
202211
pass

0 commit comments

Comments
 (0)