2019-07-12 14:25:50 -07:00
|
|
|
"""Tests for the filter observation wrapper."""
|
2021-12-08 22:14:15 +01:00
|
|
|
from typing import Optional
|
2019-07-12 14:25:50 -07:00
|
|
|
|
|
|
|
import numpy as np
|
2022-03-31 12:50:38 -07:00
|
|
|
import pytest
|
2019-07-12 14:25:50 -07:00
|
|
|
|
|
|
|
import gym
|
Update the flake8 pre-commit ignores (#2778)
* Remove additional ignores from flake8
* Remove all unused imports
* Remove all unused imports
* Update flake8 and pyupgrade
* F841, removed unused variables
* E731, removed lambda assignment to variables
* Remove E731, F403, F405, F524
* Remove E722, bare exceptions
* Remove E712, compare variable == True or == False to is True or is False
* Remove E402, module level import not at top of file
* Added --pre-file-ignores
* Add --per-file-ignores removing E741, E302 and E704
* Add E741, do not use variables named ‘l’, ‘O’, or ‘I’ to ignore issues in classic control
* Fixed issues for pytest==6.2
* Remove unnecessary # noqa
* Edit comment with the removal of E302
* Added warnings and declared module, attr for pyright type hinting
* Remove unused import
* Removed flake8 E302
* Updated flake8 from 3.9.2 to 4.0.1
* Remove unused variable
2022-04-26 16:18:37 +01:00
|
|
|
from gym.spaces import Box, Dict, Tuple
|
2019-10-19 00:53:24 +02:00
|
|
|
from gym.wrappers import FilterObservation, FlattenObservation
|
2019-07-12 14:25:50 -07:00
|
|
|
|
|
|
|
|
|
|
|
class FakeEnvironment(gym.Env):
|
2022-06-08 00:20:56 +02:00
|
|
|
def __init__(self, observation_space, render_mode=None):
|
2019-07-12 14:25:50 -07:00
|
|
|
self.observation_space = observation_space
|
|
|
|
self.obs_keys = self.observation_space.spaces.keys()
|
2021-07-29 02:26:34 +02:00
|
|
|
self.action_space = Box(shape=(1,), low=-1, high=1, dtype=np.float32)
|
2022-06-08 00:20:56 +02:00
|
|
|
self.render_mode = render_mode
|
2019-07-12 14:25:50 -07:00
|
|
|
|
2022-06-08 00:20:56 +02:00
|
|
|
def render(self, mode="human"):
|
|
|
|
image_shape = (32, 32, 3)
|
2019-07-12 14:25:50 -07:00
|
|
|
return np.zeros(image_shape, dtype=np.uint8)
|
|
|
|
|
2022-01-19 23:28:59 +01:00
|
|
|
def reset(self, *, seed: Optional[int] = None, options: Optional[dict] = None):
|
2021-12-08 22:14:15 +01:00
|
|
|
super().reset(seed=seed)
|
2019-07-12 14:25:50 -07:00
|
|
|
observation = self.observation_space.sample()
|
|
|
|
return observation
|
|
|
|
|
|
|
|
def step(self, action):
|
|
|
|
del action
|
|
|
|
observation = self.observation_space.sample()
|
|
|
|
reward, terminal, info = 0.0, False, {}
|
|
|
|
return observation, reward, terminal, info
|
|
|
|
|
|
|
|
|
|
|
|
NESTED_DICT_TEST_CASES = (
|
2021-07-29 02:26:34 +02:00
|
|
|
(
|
|
|
|
Dict(
|
|
|
|
{
|
|
|
|
"key1": Box(shape=(2,), low=-1, high=1, dtype=np.float32),
|
|
|
|
"key2": Dict(
|
|
|
|
{
|
|
|
|
"subkey1": Box(shape=(2,), low=-1, high=1, dtype=np.float32),
|
|
|
|
"subkey2": Box(shape=(2,), low=-1, high=1, dtype=np.float32),
|
|
|
|
}
|
|
|
|
),
|
|
|
|
}
|
|
|
|
),
|
|
|
|
(6,),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
Dict(
|
|
|
|
{
|
|
|
|
"key1": Box(shape=(2, 3), low=-1, high=1, dtype=np.float32),
|
|
|
|
"key2": Box(shape=(), low=-1, high=1, dtype=np.float32),
|
|
|
|
"key3": Box(shape=(2,), low=-1, high=1, dtype=np.float32),
|
|
|
|
}
|
|
|
|
),
|
|
|
|
(9,),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
Dict(
|
|
|
|
{
|
|
|
|
"key1": Tuple(
|
|
|
|
(
|
|
|
|
Box(shape=(2,), low=-1, high=1, dtype=np.float32),
|
|
|
|
Box(shape=(2,), low=-1, high=1, dtype=np.float32),
|
|
|
|
)
|
|
|
|
),
|
|
|
|
"key2": Box(shape=(), low=-1, high=1, dtype=np.float32),
|
|
|
|
"key3": Box(shape=(2,), low=-1, high=1, dtype=np.float32),
|
|
|
|
}
|
|
|
|
),
|
|
|
|
(7,),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
Dict(
|
|
|
|
{
|
|
|
|
"key1": Tuple((Box(shape=(2,), low=-1, high=1, dtype=np.float32),)),
|
|
|
|
"key2": Box(shape=(), low=-1, high=1, dtype=np.float32),
|
|
|
|
"key3": Box(shape=(2,), low=-1, high=1, dtype=np.float32),
|
|
|
|
}
|
|
|
|
),
|
|
|
|
(5,),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
Dict(
|
|
|
|
{
|
2021-07-29 15:39:42 -04:00
|
|
|
"key1": Tuple(
|
|
|
|
(Dict({"key9": Box(shape=(2,), low=-1, high=1, dtype=np.float32)}),)
|
|
|
|
),
|
2021-07-29 02:26:34 +02:00
|
|
|
"key2": Box(shape=(), low=-1, high=1, dtype=np.float32),
|
|
|
|
"key3": Box(shape=(2,), low=-1, high=1, dtype=np.float32),
|
|
|
|
}
|
|
|
|
),
|
|
|
|
(5,),
|
|
|
|
),
|
2019-07-12 14:25:50 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-01-11 18:12:05 +01:00
|
|
|
class TestNestedDictWrapper:
|
2021-07-29 02:26:34 +02:00
|
|
|
@pytest.mark.parametrize("observation_space, flat_shape", NESTED_DICT_TEST_CASES)
|
2019-07-19 20:22:42 -07:00
|
|
|
def test_nested_dicts_size(self, observation_space, flat_shape):
|
2019-07-12 14:25:50 -07:00
|
|
|
env = FakeEnvironment(observation_space=observation_space)
|
|
|
|
|
|
|
|
# Make sure we are testing the right environment for the test.
|
|
|
|
observation_space = env.observation_space
|
|
|
|
assert isinstance(observation_space, Dict)
|
|
|
|
|
2019-10-19 00:53:24 +02:00
|
|
|
wrapped_env = FlattenObservation(FilterObservation(env, env.obs_keys))
|
2019-07-12 14:25:50 -07:00
|
|
|
assert wrapped_env.observation_space.shape == flat_shape
|
|
|
|
|
2019-10-19 00:53:24 +02:00
|
|
|
assert wrapped_env.observation_space.dtype == np.float32
|
2019-07-19 20:22:42 -07:00
|
|
|
|
|
|
|
@pytest.mark.parametrize("observation_space, flat_shape", NESTED_DICT_TEST_CASES)
|
|
|
|
def test_nested_dicts_ravel(self, observation_space, flat_shape):
|
|
|
|
env = FakeEnvironment(observation_space=observation_space)
|
2019-10-19 00:53:24 +02:00
|
|
|
wrapped_env = FlattenObservation(FilterObservation(env, env.obs_keys))
|
2019-07-19 20:22:42 -07:00
|
|
|
obs = wrapped_env.reset()
|
|
|
|
assert obs.shape == wrapped_env.observation_space.shape
|