1515
1616"""Wraps the AndroidEnv environment to make its interface flat."""
1717
18- from typing import Any
18+ from typing import Any , cast
1919
20+ from android_env import env_interface
2021from android_env .wrappers import base_wrapper
2122import dm_env
2223from dm_env import specs
2526RGB_CHANNELS = (0 , 1 , 2 )
2627
2728
28- def _extract_screen_pixels (obs : np .ndarray ):
29+ def _extract_screen_pixels (obs : np .ndarray ) -> np . ndarray :
2930 """Get only screen pixels by removing previous action layer."""
3031 is_grayscale_image = obs .shape [- 1 ] == 2
3132 if is_grayscale_image :
3233 return np .expand_dims (obs [..., 0 ], - 1 )
3334 return obs [..., RGB_CHANNELS ]
3435
3536
36- def _get_no_action_observation_spec (obs_spec : specs .BoundedArray ):
37+ def _get_no_action_observation_spec (
38+ obs_spec : specs .BoundedArray ,
39+ ) -> specs .BoundedArray :
3740 """Create an observation spec without the action layer."""
3841 shape = np .array (obs_spec .shape )
3942 shape [2 ] -= 1
@@ -56,25 +59,29 @@ class FlatInterfaceWrapper(base_wrapper.BaseWrapper):
5659 space.
5760 """
5861
59- def __init__ (self ,
60- env : dm_env .Environment ,
61- flat_actions : bool = True ,
62- flat_observations : bool = True ,
63- keep_action_layer : bool = True ):
62+ def __init__ (
63+ self ,
64+ env : env_interface .AndroidEnvInterface ,
65+ flat_actions : bool = True ,
66+ flat_observations : bool = True ,
67+ keep_action_layer : bool = True ,
68+ ) -> None :
6469 super ().__init__ (env )
6570 self ._flat_actions = flat_actions
6671 self ._flat_observations = flat_observations
6772 self ._keep_action_layer = keep_action_layer
6873 self ._action_name = list (self ._env .action_spec ())[0 ]
6974 self ._assert_base_env ()
7075
71- def _assert_base_env (self ):
76+ def _assert_base_env (self ) -> None :
7277 base_action_spec = self ._env .action_spec ()
7378 assert len (base_action_spec ) == 1 , self ._env .action_spec ()
7479 assert isinstance (base_action_spec , dict )
7580 assert isinstance (base_action_spec [self ._action_name ], specs .BoundedArray )
7681
77- def _process_action (self , action : int | np .ndarray | dict [str , Any ]):
82+ def _process_action (
83+ self , action : int | np .ndarray | dict [str , Any ]
84+ ) -> int | np .ndarray | dict [str , Any ]:
7885 if self ._flat_actions :
7986 return {self ._action_name : action }
8087 else :
@@ -85,13 +92,15 @@ def _process_timestep(self, timestep: dm_env.TimeStep) -> dm_env.TimeStep:
8592 step_type , reward , discount , observation = timestep
8693 # Keep only the pixels.
8794 pixels = observation ['pixels' ]
88- pixels = pixels if self ._keep_action_layer else _extract_screen_pixels (
89- pixels )
95+ pixels = (
96+ pixels if self ._keep_action_layer else _extract_screen_pixels (pixels )
97+ )
9098 return dm_env .TimeStep (
9199 step_type = step_type ,
92100 reward = reward ,
93101 discount = discount ,
94- observation = pixels )
102+ observation = pixels ,
103+ )
95104 else :
96105 return timestep
97106
@@ -105,7 +114,9 @@ def step(self, action: int) -> dm_env.TimeStep:
105114
106115 def observation_spec (self ) -> specs .Array | dict [str , specs .Array ]: # pytype: disable=signature-mismatch # overriding-return-type-checks
107116 if self ._flat_observations :
108- pixels_spec = self ._env .observation_spec ()['pixels' ]
117+ pixels_spec = cast (
118+ specs .BoundedArray , self ._env .observation_spec ()['pixels' ]
119+ )
109120 if not self ._keep_action_layer :
110121 return _get_no_action_observation_spec (pixels_spec )
111122 return pixels_spec
@@ -114,6 +125,6 @@ def observation_spec(self) -> specs.Array | dict[str, specs.Array]: # pytype: d
114125
115126 def action_spec (self ) -> specs .BoundedArray | dict [str , specs .Array ]: # pytype: disable=signature-mismatch # overriding-return-type-checks
116127 if self ._flat_actions :
117- return self ._env .action_spec ()[self ._action_name ]
128+ return self ._env .action_spec ()[self ._action_name ] # pytype: disable=bad-return-type
118129 else :
119130 return self ._env .action_spec ()
0 commit comments