Skip to content

Commit

Permalink
trying to adress #78
Browse files Browse the repository at this point in the history
  • Loading branch information
BDonnot committed Mar 25, 2024
1 parent 41c738e commit 0657680
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 21 deletions.
20 changes: 15 additions & 5 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,22 @@ jobs:
run: python setup.py sdist

- name: Install wheel
run: pip3 install wheelhouse/*.whl --user
run: |
pip3 install wheelhouse/*.whl --user
pip freeze
# - name: Install GDB
# run: yum install -y gdb

- name: Check package can be imported
- name: Check package can be imported (bare install)
run: |
python3 -c "import lightsim2grid"
python3 -c "from lightsim2grid import *"
python3 -c "from lightsim2grid.newtonpf import newtonpf"
python3 -c "from lightsim2grid.timeSerie import TimeSeriesCPP"
python3 -c "from lightsim2grid.contingencyAnalysis import ContingencyAnalysisCPP"
python3 -c "from lightsim2grid.securityAnalysis import SecurityAnalysisCPP"
python3 -c "from lightsim2grid.gridmodel import init, GridModel"
- name: Fix urllib3 (python 3.7)
if: matrix.python.name == 'cp37'
Expand All @@ -98,11 +104,15 @@ jobs:
python3 -m pip install grid2op
python3 -m pip freeze
- name: Check LightSimBackend can be imported 1
- name: Check extra can be imported can be imported (with grid2op)
run:
python3 -v -c "from lightsim2grid import LightSimBackend"

- name: Check LightSimBackend can be imported 2
python3 -c "from lightsim2grid.timeSerie import TimeSerie"
python3 -c "from lightsim2grid.contingencyAnalysis import ContingencyAnalysis"
python3 -c "from lightsim2grid.physical_law_checker import PhysicalLawChecker"
python3 -c "from lightsim2grid.securityAnalysis import SecurityAnalysis"

- name: Check LightSimBackend can be used to create env
run:
python3 -v -c "from lightsim2grid import LightSimBackend; import grid2op; env = grid2op.make('l2rpn_case14_sandbox', test=True, backend=LightSimBackend())"

Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ Change Log
time series or contingency analysis.
- [FIXED] another bug in ContingencyAnalysis (cpp side) leading to wrong computation
when a powerline was disconnected
- [FIXED] some broken imports when grid2op was not installed
- [FIXED] missing "typing_extension" as required when installation
- [ADDED] some information of compilation directly in the cpp module
- [ADDED] some information of compilation available in the python `compilation_options`
module python side
Expand Down
10 changes: 9 additions & 1 deletion benchmarks/utils_benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,14 @@ def print_configuration():
print(f"- numpy version: {np.__version__}")
print(f"- pandas version: {pd.__version__}")
print(f"- pandapower version: {pp.__version__}")
print(f"- lightsim2grid version: {lightsim2grid.__version__}")
print(f"- grid2op version: {grid2op.__version__}")
print(f"- lightsim2grid version: {lightsim2grid.__version__}")
if hasattr(lightsim2grid, "compilation_options"):
print(f"- lightsim2grid extra information: ")
print()
print(f"\t- klu_solver_available: {lightsim2grid.compilation_options.klu_solver_available} ")
print(f"\t- nicslu_solver_available: {lightsim2grid.compilation_options.nicslu_solver_available} ")
print(f"\t- cktso_solver_available: {lightsim2grid.compilation_options.cktso_solver_available} ")
print(f"\t- compiled_march_native: {lightsim2grid.compilation_options.compiled_march_native} ")
print(f"\t- compiled_o3_optim: {lightsim2grid.compilation_options.compiled_o3_optim} ")
print()
3 changes: 2 additions & 1 deletion lightsim2grid/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@
print(f"ContingencyAnalysis import error: {exc_}")

try:
from lightsim2grid.rewards import *
__all__.append("rewards")
except ImportError as exc_:
# grid2op is not installed, the SecurtiyAnalysis module will not be available
pass
print(f"ContingencyAnalysis import error: {exc_}")
print(f"rewards import error: {exc_}")
27 changes: 21 additions & 6 deletions lightsim2grid/contingencyAnalysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,26 @@
# SPDX-License-Identifier: MPL-2.0
# This file is part of LightSim2grid, LightSim2grid implements a c++ backend targeting the Grid2Op platform.

__all__ = ["ContingencyAnalysisCPP", "ContingencyAnalysis",
# deprecated
"SecurityAnalysisCPP", "SecurityAnalysis",
]
__all__ = ["ContingencyAnalysisCPP"]

import copy
import numpy as np
from collections.abc import Iterable

from lightsim2grid.lightSimBackend import LightSimBackend
from lightsim2grid.solver import SolverType
from lightsim2grid_cpp import ContingencyAnalysisCPP

try:
from lightsim2grid.lightSimBackend import LightSimBackend
__all__.append("ContingencyAnalysis")
__all__.append("SecurityAnalysis")
GRID2OP_INSTALLED = True
except ImportError as exc_:
# grid2op is not installed
GRID2OP_INSTALLED = False

class ContingencyAnalysis(object):

class __ContingencyAnalysis(object):
"""
This class allows to perform a "security analysis" from a given grid state.
Expand Down Expand Up @@ -76,6 +81,12 @@ class ContingencyAnalysis(object):
STR_TYPES = (str, np.str_) # np.str deprecated in numpy 1.20 and earlier versions not supported anyway

def __init__(self, grid2op_env):
if not GRID2OP_INSTALLED:
raise RuntimeError("Impossible to use the python wrapper `ContingencyAnalysis` "
"when grid2op is not installed. Please fall back to the "
"c++ version (available in python) with:\n"
"\tfrom lightsim2grid.contingencyAnalysis import ContingencyAnalysisCPP\n"
"and refer to the appropriate documentation.")
from grid2op.Environment import Environment
if isinstance(grid2op_env, Environment):
if not isinstance(grid2op_env.backend, LightSimBackend):
Expand Down Expand Up @@ -368,3 +379,7 @@ def close(self):
self._ls_backend.close()
self.clear()
self.computer.close()


if GRID2OP_INSTALLED:
ContingencyAnalysis = __ContingencyAnalysis
24 changes: 21 additions & 3 deletions lightsim2grid/securityAnalysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,26 @@
# SPDX-License-Identifier: MPL-2.0
# This file is part of LightSim2grid, LightSim2grid implements a c++ backend targeting the Grid2Op platform.

from lightsim2grid.contingencyAnalysis import ContingencyAnalysisCPP, ContingencyAnalysis

import warnings
# Deprecated now, will be removed
warnings.warn("You are using old names. Please upgrade to SecurityAnalysisCPP > ContingencyAnalysisCPP"
" and SecurityAnalysis > ContingencyAnalysis instead.",
category=DeprecationWarning)
__all__ = ["SecurityAnalysisCPP"]


from lightsim2grid.contingencyAnalysis import ContingencyAnalysisCPP

try:
from lightsim2grid.contingencyAnalysis import ContingencyAnalysis
GRID2OP_INSTALLED = True
except ImportError as exc_:
GRID2OP_INSTALLED = False


SecurityAnalysisCPP = ContingencyAnalysisCPP
SecurityAnalysis = ContingencyAnalysis


if GRID2OP_INSTALLED:
SecurityAnalysis = ContingencyAnalysis
__all__.append("SecurityAnalysis")
27 changes: 23 additions & 4 deletions lightsim2grid/timeSerie.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,31 @@
# SPDX-License-Identifier: MPL-2.0
# This file is part of LightSim2grid, LightSim2grid implements a c++ backend targeting the Grid2Op platform.

__all__ = ["TimeSerieCPP", "TimeSerie",
__all__ = ["TimeSeriesCPP",
# deprecated
"Computers"]

import numpy as np
import warnings

from grid2op.Chronics import Multifolder, GridStateFromFile

from lightsim2grid.lightSimBackend import LightSimBackend
try:
from grid2op.Chronics import Multifolder, GridStateFromFile
from lightsim2grid.lightSimBackend import LightSimBackend
__all__.append("TimeSerie")
GRID2OP_INSTALLED = True
except ImportError as exc_:
# grid2Op is not installed
GRID2OP_INSTALLED = False

from lightsim2grid.solver import SolverType
from lightsim2grid_cpp import TimeSeriesCPP

# deprecated
Computers = TimeSeriesCPP


class TimeSerie:
class ___TimeSerie:
"""
This helper class, that only works with grid2op when using a LightSimBackend allows to compute
the flows (at the origin side of the powerline / transformers). It is roughly equivalent to the
Expand Down Expand Up @@ -77,6 +84,13 @@ class TimeSerie:
"""
def __init__(self, grid2op_env):
if not GRID2OP_INSTALL:
raise RuntimeError("Impossible to use the python wrapper `TimeSerie` "
"when grid2op is not installed. Please fall back to the "
"c++ version (available in python) with:\n"
"\tfrom lightsim2grid.timeSerie import TimeSerieCPP\n"
"and refer to the appropriate documentation.")

from grid2op.Environment import Environment # otherwise i got issues...
if not isinstance(grid2op_env.backend, LightSimBackend):
raise RuntimeError("This class only works with LightSimBackend")
Expand Down Expand Up @@ -263,3 +277,8 @@ def _extract_inj(self):
self.load_p = 1.0 * data_loader.load_p
self.load_q = 1.0 * data_loader.load_q
return self.prod_p, self.load_p, self.load_q


if GRID2OP_INSTALLED:
TimeSerie = ___TimeSerie

3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,8 @@
"pandapower" if sys.version_info < (3, 10) else "pandapower>=2.8",
"pytest", # for pandapower see https://github.com/e2nIEE/pandapower/issues/1988
]

if sys.version_info < (3, 11):
req_pkgs.append("typing_extensions")
if sys.version_info.major == 3 and sys.version_info.minor <= 7:
# typing "Literal" not available on python 3.7
req_pkgs.append("typing_extensions")
Expand Down

0 comments on commit 0657680

Please sign in to comment.