|
1 | | -# Copyright (c) 2019-2020, RTE (https://www.rte-france.com) |
| 1 | +# Copyright (c) 2019-2025, RTE (https://www.rte-france.com) |
2 | 2 | # See AUTHORS.txt |
3 | 3 | # This Source Code Form is subject to the terms of the Mozilla Public License, version 2.0. |
4 | 4 | # If a copy of the Mozilla Public License, version 2.0 was not distributed with this file, |
|
22 | 22 | import os |
23 | 23 | import numpy as np |
24 | 24 | import sys |
| 25 | + |
| 26 | +import importlib.metadata |
25 | 27 | from packaging import version |
26 | 28 | from typing import Dict, Type, Union, Literal, Any, List, Optional, ClassVar, Tuple |
27 | 29 |
|
|
30 | 32 | from grid2op.typing_variables import CLS_AS_DICT_TYPING, N_BUSBAR_PER_SUB_TYPING |
31 | 33 | from grid2op.Exceptions import * |
32 | 34 | from grid2op.Space.space_utils import extract_from_dict, save_to_dict, ElTypeInfo |
| 35 | +from grid2op.Space.default_var import (DEFAULT_ALLOW_DETACHMENT, |
| 36 | + DEFAULT_N_BUSBAR_PER_SUB, |
| 37 | + GRID2OP_CLASSES_ENV_FOLDER, |
| 38 | + GRID2OP_CURRENT_VERSION_STR, |
| 39 | + GRID2OP_CURRENT_VERSION) |
33 | 40 |
|
34 | 41 | # TODO tests of these methods and this class in general |
35 | | -DEFAULT_N_BUSBAR_PER_SUB = 2 |
36 | | -DEFAULT_ALLOW_DETACHMENT = False |
37 | | -GRID2OP_CLASSES_ENV_FOLDER = "_grid2op_classes" |
38 | 42 |
|
39 | 43 |
|
40 | 44 | class GridObjects: |
@@ -480,7 +484,7 @@ class GridObjects: |
480 | 484 |
|
481 | 485 | BEFORE_COMPAT_VERSION : ClassVar[str] = "neurips_2020_compat" |
482 | 486 | MIN_VERSION_DETACH : ClassVar[str] = version.parse("1.11.0.dev2") |
483 | | - glop_version : ClassVar[str] = grid2op.__version__ |
| 487 | + glop_version : ClassVar[str] = GRID2OP_CURRENT_VERSION_STR |
484 | 488 |
|
485 | 489 | _INIT_GRID_CLS = None # do not modify that, this is handled by grid2op automatically |
486 | 490 | _PATH_GRID_CLASSES : ClassVar[Optional[str]] = None # especially do not modify that |
@@ -746,7 +750,7 @@ def _clear_grid_dependant_class_attributes(cls) -> None: |
746 | 750 | cls.n_busbar_per_sub = DEFAULT_N_BUSBAR_PER_SUB |
747 | 751 | cls.detachment_is_allowed = DEFAULT_ALLOW_DETACHMENT |
748 | 752 |
|
749 | | - cls.glop_version = grid2op.__version__ |
| 753 | + cls.glop_version = GRID2OP_CURRENT_VERSION_STR |
750 | 754 |
|
751 | 755 | cls.attr_list_vect = None |
752 | 756 | cls.attr_list_set = {} |
@@ -2967,7 +2971,7 @@ def init_grid(cls, gridobj, force=False, extra_name=None, force_module=None, _lo |
2967 | 2971 | """ |
2968 | 2972 | # nothing to do now that the value are class member |
2969 | 2973 | name_res = "{}_{}".format(cls.__name__, gridobj.env_name) |
2970 | | - if gridobj.glop_version != grid2op.__version__: |
| 2974 | + if gridobj.glop_version != GRID2OP_CURRENT_VERSION_STR: |
2971 | 2975 | name_res += f"_{gridobj.glop_version}" |
2972 | 2976 |
|
2973 | 2977 | if gridobj._PATH_GRID_CLASSES is not None: |
@@ -4409,7 +4413,7 @@ class res(GridObjects): |
4409 | 4413 |
|
4410 | 4414 | cls.process_shunt_static_data() |
4411 | 4415 |
|
4412 | | - if cls.glop_version != grid2op.__version__: |
| 4416 | + if cls.glop_version != GRID2OP_CURRENT_VERSION_STR: |
4413 | 4417 | # change name of the environment, this is done in Environment.py for regular environment |
4414 | 4418 | # see `self.backend.set_env_name(f"{self.name}_{self._compat_glop_version}")` |
4415 | 4419 | # cls.set_env_name(f"{cls.env_name}_{cls.glop_version}") |
@@ -4597,7 +4601,7 @@ def init_grid_from_dict_for_pickle(name_res, orig_cls, cls_attr): |
4597 | 4601 | # if hasattr(res_cls, "n_sub") and res_cls.n_sub > 0: |
4598 | 4602 | # that's a grid2op class iniailized with an environment, I need to initialize it too |
4599 | 4603 | res_cls._compute_pos_big_topo_cls() |
4600 | | - if res_cls.glop_version != grid2op.__version__: |
| 4604 | + if res_cls.glop_version != GRID2OP_CURRENT_VERSION_STR: |
4601 | 4605 | res_cls.process_grid2op_compat() |
4602 | 4606 | res_cls.process_shunt_static_data() |
4603 | 4607 | res_cls.process_detachment() |
@@ -4981,7 +4985,7 @@ def format_el(values): |
4981 | 4985 |
|
4982 | 4986 | class {cls.__name__}({cls._INIT_GRID_CLS.__name__}): |
4983 | 4987 | BEFORE_COMPAT_VERSION = \"{cls.BEFORE_COMPAT_VERSION}\" |
4984 | | - glop_version = grid2op.__version__ # tells it's the installed grid2op version |
| 4988 | + glop_version = \"{GRID2OP_CURRENT_VERSION_STR}\" # tells it's the installed grid2op version |
4985 | 4989 | _PATH_GRID_CLASSES = {_PATH_ENV_str} # especially do not modify that |
4986 | 4990 | _INIT_GRID_CLS = {cls._INIT_GRID_CLS.__name__} |
4987 | 4991 | _CLS_DICT = None # init once to avoid yet another serialization of the class as dict (in make_cls_dict) |
@@ -5387,7 +5391,7 @@ def _aux_check_kirchhoff(cls, |
5387 | 5391 | shunt_info : Optional[ElTypeInfo] = None, |
5388 | 5392 | ) -> Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray]: |
5389 | 5393 | """ |
5390 | | - Analogous to "backend.check_kirchhoff" but from the observation |
| 5394 | + Analogous to "backend.check_kirchhoff" but can be used for both the observation and the backend |
5391 | 5395 |
|
5392 | 5396 | .. versionadded:: 1.11.0 |
5393 | 5397 | |
|
0 commit comments