From 58c09ad8d81cf315caa240ac9d7d5fe0edc1e443 Mon Sep 17 00:00:00 2001 From: DONNOT Benjamin Date: Wed, 26 Jun 2024 08:52:49 +0200 Subject: [PATCH] fixing legacy lightsim2grid compat --- .circleci/config.yml | 4 ++-- grid2op/Backend/pandaPowerBackend.py | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 5773afcd..01e9dab4 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -178,7 +178,7 @@ jobs: command: | source venv_test/bin/activate python -m pip install -U pip setuptools wheel - python -m pip install -U lightsim2grid==0.5.3 gymnasium "numpy<1.22" "pandapower<2.14.9" + python -m pip install -U lightsim2grid==0.5.3 gymnasium "numpy<1.22" - run: command: | source venv_test/bin/activate @@ -205,7 +205,7 @@ jobs: command: | source venv_test/bin/activate python -m pip install -U pip setuptools wheel - python -m pip install -U lightsim2grid==0.6.0 gymnasium + python -m pip install -U lightsim2grid==0.6.0 gymnasium "numpy<1.22" - run: command: | source venv_test/bin/activate diff --git a/grid2op/Backend/pandaPowerBackend.py b/grid2op/Backend/pandaPowerBackend.py index 27bcdb41..299043b6 100644 --- a/grid2op/Backend/pandaPowerBackend.py +++ b/grid2op/Backend/pandaPowerBackend.py @@ -17,6 +17,8 @@ import pandapower as pp import scipy +# check that pandapower does not introduce some +from packaging import version import grid2op from grid2op.dtypes import dt_int, dt_float, dt_bool @@ -24,6 +26,8 @@ from grid2op.Exceptions import BackendError from grid2op.Backend.backend import Backend +MIN_LS_VERSION_VM_PU = version.parse("0.6.0") + try: import numba NUMBA_ = True @@ -544,6 +548,23 @@ def load_grid(self, self._in_service_storage_cold_id = int((self._grid.storage.columns == "in_service").nonzero()[0][0]) self.comp_time = 0. + # hack for backward compat with oldest lightsim2grid version + try: + import lightsim2grid + if version.parse(lightsim2grid.__version__) < MIN_LS_VERSION_VM_PU: + warnings.warn("You are using a really old version of lightsim2grid. Consider upgrading.") + if "_options" in self._grid and "init_vm_pu" in self._grid["_options"]: + try: + float(self._grid["_options"]["init_vm_pu"]) + except ValueError as exc_: + # we delete it because lightsim2grid uses it + # to init its internal "GridModel" and did not check that + # this is a float until MIN_LS_VERSION_VM_PU + del self._grid["_options"]["init_vm_pu"] + except ImportError: + # lightsim2grid is not installed, so no risk to contaminate it + pass + def _aux_run_pf_init(self): """run a powerflow when the file is being loaded. This is called three times for each call to "load_grid" """ with warnings.catch_warnings():