1515from abc import ABC , abstractmethod
1616import numpy as np
1717import pandas as pd
18- from typing import Tuple , Optional , Any , Dict , Union
18+ from typing import Tuple , Optional , Any , Dict , Type , Union
1919
2020try :
2121 from typing import Self
3636 Grid2OpException ,
3737)
3838from grid2op .Space import GridObjects , ElTypeInfo , DEFAULT_N_BUSBAR_PER_SUB , DEFAULT_ALLOW_DETACHMENT
39+ import grid2op .Observation # for type hints
40+ import grid2op .Action # for type hints
41+ import grid2op .Action ._BackendAction # for type hints
3942
4043
4144# TODO method to get V and theta at each bus, could be in the same shape as check_kirchoff
@@ -116,8 +119,8 @@ class Backend(GridObjects, ABC):
116119 IS_BK_CONVERTER : bool = False
117120
118121 # action to set me
119- my_bk_act_class : "Optional[grid2op.Action._backendAction ._BackendAction]" = None
120- _complete_action_class : "Optional[grid2op.Action.CompleteAction]" = None
122+ my_bk_act_class : "Optional[Type[ grid2op.Action._BackendAction ._BackendAction] ]" = None
123+ _complete_action_class : "Optional[Type[ grid2op.Action.CompleteAction] ]" = None
121124
122125 ERR_INIT_POWERFLOW : str = "Power cannot be computed on the first time step, please check your data."
123126 ERR_DETACHMENT : str = ("One or more {} were isolated from the grid "
@@ -1906,8 +1909,8 @@ def get_action_to_set(self) -> "grid2op.Action.CompleteAction":
19061909 sh_conn = bus_s > 0
19071910 p_s [sh_conn ] *= (self ._sh_vnkv [sh_conn ] / sh_v [sh_conn ]) ** 2
19081911 q_s [sh_conn ] *= (self ._sh_vnkv [sh_conn ] / sh_v [sh_conn ]) ** 2
1909- p_s [bus_s == - 1 ] = np .NaN
1910- q_s [bus_s == - 1 ] = np .NaN
1912+ p_s [bus_s == - 1 ] = np .nan
1913+ q_s [bus_s == - 1 ] = np .nan
19111914 dict_ ["shunt" ]["shunt_p" ] = p_s
19121915 dict_ ["shunt" ]["shunt_q" ] = q_s
19131916
@@ -1920,7 +1923,7 @@ def get_action_to_set(self) -> "grid2op.Action.CompleteAction":
19201923
19211924 def update_from_obs (self ,
19221925 obs : "grid2op.Observation.CompleteObservation" ,
1923- force_update : Optional [bool ]= False ):
1926+ force_update : Optional [bool ]= False ) -> "grid2op.Action._BackendAction._BackendAction" :
19241927 """
19251928 Takes an observation as input and update the internal state of `self` to match the state of the backend
19261929 that produced this observation.
@@ -1954,23 +1957,23 @@ def update_from_obs(self,
19541957 )
19551958
19561959 cls = type (self )
1957- backend_action = cls .my_bk_act_class ()
1960+ backend_action : "grid2op.Action._BackendAction._BackendAction" = cls .my_bk_act_class ()
19581961 act = cls ._complete_action_class ()
19591962 line_status = self ._aux_get_line_status_to_set (obs .line_status )
19601963 # skip the action part and update directly the backend action !
19611964 dict_ = {
19621965 "set_bus" : obs .topo_vect ,
19631966 "set_line_status" : line_status ,
19641967 "injection" : {
1965- "prod_p" : obs .prod_p ,
1966- "prod_v" : obs .prod_v ,
1967- "load_p" : obs .load_p ,
1968- "load_q" : obs .load_q ,
1968+ "prod_p" : obs ._get_gen_p_for_forecasts () ,
1969+ "prod_v" : obs ._get_gen_v_for_forecasts () ,
1970+ "load_p" : obs ._get_load_p_for_forecasts () ,
1971+ "load_q" : obs ._get_load_q_for_forecasts () ,
19691972 },
19701973 }
19711974
19721975 if cls .shunts_data_available and type (obs ).shunts_data_available :
1973- if cls .n_shunt > 0 and "_shunt_bus" not in type (obs ).attr_list_set :
1976+ if cls .n_shunt > 0 and "_shunt_bus" not in ( type (obs ).attr_list_set | set ( type ( obs ). attr_list_json )) :
19741977 raise BackendError (
19751978 "Impossible to set the backend to the state given by the observation: shunts data "
19761979 "are not present in the observation."
@@ -1982,15 +1985,17 @@ def update_from_obs(self,
19821985 mults = (self ._sh_vnkv / obs ._shunt_v ) ** 2
19831986 sh_p = obs ._shunt_p * mults
19841987 sh_q = obs ._shunt_q * mults
1985- sh_p [~ shunt_co ] = np .NaN
1986- sh_q [~ shunt_co ] = np .NaN
1988+ sh_p [~ shunt_co ] = np .nan
1989+ sh_q [~ shunt_co ] = np .nan
19871990 dict_ ["shunt" ]["shunt_p" ] = sh_p
19881991 dict_ ["shunt" ]["shunt_q" ] = sh_q
19891992 elif cls .shunts_data_available and not type (obs ).shunts_data_available :
19901993 warnings .warn ("Backend supports shunt but not the observation. This behaviour is non standard." )
19911994 act .update (dict_ )
19921995 backend_action += act
19931996 self .apply_action (backend_action )
1997+ backend_action .reset () # already processed by the backend
1998+ return backend_action
19941999
19952000 def assert_grid_correct (self , _local_dir_cls = None ) -> None :
19962001 """
0 commit comments