-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathtest_envs.py
More file actions
199 lines (175 loc) · 6.43 KB
/
test_envs.py
File metadata and controls
199 lines (175 loc) · 6.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
from typing import Annotated
import gymnasium as gym
import numpy as np
from rcsss.envs.space_utils import RCSpaceType, get_space, get_space_keys
class SimpleSpace(RCSpaceType):
my_int: Annotated[
int,
gym.spaces.Discrete(1),
]
my_float: Annotated[
float,
gym.spaces.Box(low=0, high=1, shape=(1,), dtype=np.float32),
]
class SimpleSpaceWithLambda(RCSpaceType):
image: Annotated[
np.ndarray,
lambda height, width: gym.spaces.Box(low=0, high=255, shape=(height, width, 3), dtype=np.uint8),
"image",
]
class SimpleNestedSpace(RCSpaceType):
robots_joints: dict[
Annotated[str, "robots"],
Annotated[
np.ndarray,
gym.spaces.Box(
low=-np.pi,
high=np.pi,
shape=(7,),
dtype=np.float32,
),
],
]
class AdvancedNestedSpace(RCSpaceType):
frames: dict[
Annotated[str, "cams"],
dict[
Annotated[str, "cam_type"],
Annotated[
np.ndarray,
gym.spaces.Box(
low=0,
high=255,
shape=(480, 640, 3),
dtype=np.uint8,
),
],
],
]
class AdvancedNestedSpaceWithLambda(RCSpaceType):
frames: dict[
Annotated[str, "cams"],
dict[
Annotated[str, "cam_type"],
Annotated[
np.ndarray,
lambda height, width: gym.spaces.Box(low=0, high=255, shape=(height, width, 3), dtype=np.uint8),
"frames",
],
],
]
class Composed(AdvancedNestedSpaceWithLambda, SimpleSpace): ...
class TestGetSpace:
def test_simple_space(self):
assert get_space(SimpleSpace) == gym.spaces.Dict(
{
"my_int": gym.spaces.Discrete(1),
"my_float": gym.spaces.Box(low=0, high=1, shape=(1,), dtype=np.float32),
}
)
def test_simple_space_with_lambda(self):
assert get_space(SimpleSpaceWithLambda, params={"image": {"height": 480, "width": 640}}) == gym.spaces.Dict(
{
"image": gym.spaces.Box(low=0, high=255, shape=(480, 640, 3), dtype=np.uint8),
}
)
def test_simple_nested_space(self):
assert get_space(
SimpleNestedSpace, child_dict_keys_to_unfold={"robots": ["robot1", "robot2"]}
) == gym.spaces.Dict(
{
"robots_joints": gym.spaces.Dict(
{
"robot1": gym.spaces.Box(low=-np.pi, high=np.pi, shape=(7,), dtype=np.float32),
"robot2": gym.spaces.Box(low=-np.pi, high=np.pi, shape=(7,), dtype=np.float32),
}
),
}
)
def test_advanced_nested_space(self):
assert get_space(
AdvancedNestedSpace,
child_dict_keys_to_unfold={
"cams": ["cam1", "cam2"],
"/cam1/cam_type": ["depth", "rgb"],
"/cam2/cam_type": ["rgb"],
},
) == gym.spaces.Dict(
{
"frames": gym.spaces.Dict(
{
"cam1": gym.spaces.Dict(
{
"depth": gym.spaces.Box(low=0, high=255, shape=(480, 640, 3), dtype=np.uint8),
"rgb": gym.spaces.Box(low=0, high=255, shape=(480, 640, 3), dtype=np.uint8),
}
),
"cam2": gym.spaces.Dict(
{
"rgb": gym.spaces.Box(low=0, high=255, shape=(480, 640, 3), dtype=np.uint8),
}
),
}
),
}
)
def test_advanced_nested_space_with_lambda(self):
assert get_space(
AdvancedNestedSpaceWithLambda,
child_dict_keys_to_unfold={
"cams": ["cam1", "cam2"],
"/cam1/cam_type": ["depth", "rgb"],
"/cam2/cam_type": ["rgb"],
},
params={"/cam1/rgb/frames": {"height": 128, "width": 128}, "frames": {"height": 512, "width": 512}},
) == gym.spaces.Dict(
{
"frames": gym.spaces.Dict(
{
"cam1": gym.spaces.Dict(
{
"depth": gym.spaces.Box(low=0, high=255, shape=(512, 512, 3), dtype=np.uint8),
"rgb": gym.spaces.Box(low=0, high=255, shape=(128, 128, 3), dtype=np.uint8),
}
),
"cam2": gym.spaces.Dict(
{
"rgb": gym.spaces.Box(low=0, high=255, shape=(512, 512, 3), dtype=np.uint8),
}
),
}
),
}
)
def test_composed_space(self):
assert get_space(
Composed,
child_dict_keys_to_unfold={
"cams": ["cam1", "cam2"],
"/cam1/cam_type": ["depth", "rgb"],
"/cam2/cam_type": ["rgb"],
},
params={"/cam1/rgb/frames": {"height": 128, "width": 128}, "frames": {"height": 512, "width": 512}},
) == gym.spaces.Dict(
{
"frames": gym.spaces.Dict(
{
"cam1": gym.spaces.Dict(
{
"depth": gym.spaces.Box(low=0, high=255, shape=(512, 512, 3), dtype=np.uint8),
"rgb": gym.spaces.Box(low=0, high=255, shape=(128, 128, 3), dtype=np.uint8),
}
),
"cam2": gym.spaces.Dict(
{
"rgb": gym.spaces.Box(low=0, high=255, shape=(512, 512, 3), dtype=np.uint8),
}
),
}
),
"my_int": gym.spaces.Discrete(1),
"my_float": gym.spaces.Box(low=0, high=1, shape=(1,), dtype=np.float32),
}
)
def test_get_space_keys(self):
assert set(get_space_keys(SimpleSpace)) == {"my_int", "my_float"}