Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions godot_rl/core/godot_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,11 @@ def _process_obs(self, response_obs: dict):
if "2d" in k:
for sub in response_obs:
sub[k] = self._decode_2d_obs_from_string(sub[k], self.observation_space[k].shape)
elif isinstance(response_obs[0][k], str):
# Sent by the plugin's Sync node when binary observations are enabled:
# the float32 bytes of the observation, hex-encoded.
for sub in response_obs:
sub[k] = self._decode_float_obs_from_string(sub[k], self.observation_space[k].shape)

return response_obs

Expand Down Expand Up @@ -437,6 +442,13 @@ def _decode_2d_obs_from_string(
):
return np.frombuffer(bytes.fromhex(hex_string), dtype=np.uint8).reshape(shape)

@staticmethod
def _decode_float_obs_from_string(
hex_string,
shape,
):
return np.frombuffer(bytes.fromhex(hex_string), dtype="<f4").reshape(shape)

def _send_as_json(self, dictionary):
message_json = json.dumps(dictionary)
self._send_string(message_json)
Expand Down
53 changes: 53 additions & 0 deletions tests/test_obs_encoding.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import numpy as np
from gymnasium import spaces

from godot_rl.core.godot_env import GodotEnv


def _env_with_obs_space(obs_space: spaces.Dict) -> GodotEnv:
# Only _process_obs is exercised, so no Godot process or socket is needed.
env = GodotEnv.__new__(GodotEnv)
env.observation_spaces = [obs_space]
env.observation_space = obs_space
return env


def test_float_obs_hex_string_is_decoded_exactly():
values = np.random.default_rng(0).uniform(-1.0, 1.0, size=(3, 69)).astype(np.float32)
env = _env_with_obs_space(spaces.Dict({"obs": spaces.Box(low=-1.0, high=1.0, shape=(69,))}))

response_obs = [{"obs": row.astype("<f4").tobytes().hex()} for row in values]
processed = env._process_obs(response_obs)

assert len(processed) == 3
for row, sub in zip(values, processed):
assert sub["obs"].dtype == np.float32
assert sub["obs"].shape == (69,)
np.testing.assert_array_equal(sub["obs"], row)


def test_list_obs_is_left_untouched():
env = _env_with_obs_space(spaces.Dict({"obs": spaces.Box(low=-1.0, high=1.0, shape=(3,))}))
response_obs = [{"obs": [0.1, 0.2, 0.3]}]

assert env._process_obs(response_obs) == [{"obs": [0.1, 0.2, 0.3]}]


def test_mixed_keys_decode_independently():
image = np.arange(12, dtype=np.uint8).reshape(2, 2, 3)
vector = np.array([0.5, -0.25], dtype=np.float32)
env = _env_with_obs_space(
spaces.Dict(
{
"camera_2d": spaces.Box(low=0, high=255, shape=(2, 2, 3), dtype=np.uint8),
"obs": spaces.Box(low=-1.0, high=1.0, shape=(2,)),
"plain": spaces.Box(low=-1.0, high=1.0, shape=(1,)),
}
)
)
response_obs = [{"camera_2d": image.tobytes().hex(), "obs": vector.astype("<f4").tobytes().hex(), "plain": [0.75]}]
sub = env._process_obs(response_obs)[0]

np.testing.assert_array_equal(sub["camera_2d"], image)
np.testing.assert_array_equal(sub["obs"], vector)
assert sub["plain"] == [0.75]