Skip to content

Commit

Permalink
improving docs following rte-france#509
Browse files Browse the repository at this point in the history
  • Loading branch information
BDonnot committed Aug 24, 2023
1 parent 2a4801a commit c8824f3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
11 changes: 11 additions & 0 deletions grid2op/Observation/baseObservation.py
Original file line number Diff line number Diff line change
Expand Up @@ -3404,6 +3404,17 @@ def to_dict(self):
-------
The returned dictionary is not necessarily json serializable. To have a grid2op observation that you can
serialize in a json fashion, please use the :func:`grid2op.Space.GridObjects.to_json` function.
.. note::
This function is different to the :func:`grid2op.Space.GridObjects.to_dict`.
Indeed the dictionnary resulting from this function will count as keys all the attributes
in :attr:`GridObjects.attr_list_vect` only.
Concretely, if `obs` is an observation (:class:`grid2op.Observation.BaseObservation`)
then `obs.to_dict()` will have the keys `type(obs).attr_list_vect` and the values will
be numpy arrays whereas `obs.to_json()` will have the keys
`type(obs).attr_list_vect` and `type(obs).attr_list_json` and the values will be
lists (serializable).
"""
if self._dictionnarized is None:
Expand Down
33 changes: 27 additions & 6 deletions grid2op/Space/GridObjects.py
Original file line number Diff line number Diff line change
Expand Up @@ -898,8 +898,21 @@ def to_vect(self):
def to_json(self, convert=True):
"""
Convert this instance of GridObjects to a dictionary that can be json serialized.
convert: do you convert the numpy types to standard python list (might take lots of time)
.. note::
This function is different to the :func:`grid2op.Observation.BaseObservation.to_dict`.
Indeed the dictionnary resulting from this function will count as keys all the attributes
in :attr:`GridObjects.attr_list_vect` and :attr:`GridObjects.attr_list_json`.
Concretely, if `obs` is an observation (:class:`grid2op.Observation.BaseObservation`)
then `obs.to_dict()` will have the keys `type(obs).attr_list_vect` and the values will
be numpy arrays whereas `obs.to_json()` will have the keys
`type(obs).attr_list_vect` and `type(obs).attr_list_json` and the values will be
lists (serializable)
.. warning::
convert: do you convert the numpy types to standard python list (might take lots of time)
TODO doc and example
"""

Expand All @@ -909,11 +922,12 @@ def to_json(self, convert=True):
# or even storing the things in [id, value] for these types of attributes (time_before_cooldown_line,
# time_before_cooldown_sub, time_next_maintenance, duration_next_maintenance etc.)

cls = type(self)
res = {}
for attr_nm in self.attr_list_vect + self.attr_list_json:
for attr_nm in cls.attr_list_vect + cls.attr_list_json:
res[attr_nm] = self._get_array_from_attr_name(attr_nm)
if convert:
self._convert_to_json(res) # TODO !
cls._convert_to_json(res)
return res

def from_json(self, dict_):
Expand Down Expand Up @@ -945,8 +959,9 @@ def from_json(self, dict_):
type_ = type(my_attr)
setattr(self, key, type_(array_[0]))

def _convert_to_json(self, dict_):
for attr_nm in self.attr_list_vect + self.attr_list_json:
@classmethod
def _convert_to_json(cls, dict_):
for attr_nm in cls.attr_list_vect + cls.attr_list_json:
tmp = dict_[attr_nm]
dtype = tmp.dtype
if dtype == dt_float:
Expand All @@ -955,6 +970,12 @@ def _convert_to_json(self, dict_):
dict_[attr_nm] = [int(el) for el in tmp]
elif dtype == dt_bool:
dict_[attr_nm] = [bool(el) for el in tmp]
elif dtype == float:
dict_[attr_nm] = [float(el) for el in tmp]
elif dtype == int:
dict_[attr_nm] = [int(el) for el in tmp]
elif dtype == bool:
dict_[attr_nm] = [bool(el) for el in tmp]

def shape(self):
"""
Expand Down

0 comments on commit c8824f3

Please sign in to comment.