-
Notifications
You must be signed in to change notification settings - Fork 246
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix PosixPath being passed to HuggingFace save_to_disk
(which does not accept it)
#855
Open
iwishiwasaneagle
wants to merge
1
commit into
HumanCompatibleAI:master
Choose a base branch
from
iwishiwasaneagle:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import gymnasium as gym | ||
import numpy as np | ||
import pytest | ||
|
||
from imitation.data import types | ||
|
||
SPACES = [ | ||
gym.spaces.Discrete(3), | ||
gym.spaces.MultiDiscrete([3, 4]), | ||
gym.spaces.Box(-1, 1, shape=(1,)), | ||
gym.spaces.Box(-1, 1, shape=(2,)), | ||
gym.spaces.Box(-np.inf, np.inf, shape=(2,)), | ||
] | ||
DICT_SPACE = gym.spaces.Dict( | ||
{"a": gym.spaces.Discrete(3), "b": gym.spaces.Box(-1, 1, shape=(2,))}, | ||
) | ||
LENGTHS = [0, 1, 2, 10] | ||
|
||
|
||
@pytest.fixture(params=SPACES) | ||
def act_space(request): | ||
return request.param | ||
|
||
|
||
@pytest.fixture(params=SPACES + [DICT_SPACE]) | ||
def obs_space(request): | ||
return request.param | ||
|
||
|
||
@pytest.fixture(params=LENGTHS) | ||
def length(request): | ||
return request.param | ||
|
||
|
||
@pytest.fixture | ||
def trajectory( | ||
obs_space: gym.Space, | ||
act_space: gym.Space, | ||
length: int, | ||
) -> types.Trajectory: | ||
"""Fixture to generate trajectory of length `length` iid sampled from spaces.""" | ||
if length == 0: | ||
pytest.skip() | ||
|
||
raw_obs = [obs_space.sample() for _ in range(length + 1)] | ||
if isinstance(obs_space, gym.spaces.Dict): | ||
obs: types.Observation = types.DictObs.from_obs_list(raw_obs) | ||
else: | ||
obs = np.array(raw_obs) | ||
acts = np.array([act_space.sample() for _ in range(length)]) | ||
infos = np.array([{f"key{i}": i} for i in range(length)]) | ||
return types.Trajectory(obs=obs, acts=acts, infos=infos, terminal=True) | ||
|
||
|
||
@pytest.fixture | ||
def trajectory_rew(trajectory: types.Trajectory) -> types.TrajectoryWithRew: | ||
"""Like `trajectory` but with reward randomly sampled from a Gaussian.""" | ||
rews = np.random.randn(len(trajectory)) | ||
return types.TrajectoryWithRew( | ||
**types.dataclass_quick_asdict(trajectory), | ||
rews=rews, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
"""Tests for `imitation.data.serialize`.""" | ||
|
||
import pathlib | ||
|
||
import gymnasium as gym | ||
import numpy as np | ||
import pytest | ||
|
||
from imitation.data import serialize, types | ||
from imitation.data.types import DictObs | ||
|
||
|
||
@pytest.fixture | ||
def data_path(tmp_path): | ||
return tmp_path / "data" | ||
|
||
|
||
@pytest.mark.parametrize("path_type", [str, pathlib.Path]) | ||
def test_save_trajectory(data_path, trajectory, path_type): | ||
if isinstance(trajectory.obs, DictObs): | ||
pytest.skip("serialize.save does not yet support DictObs") | ||
|
||
serialize.save(path_type(data_path), [trajectory]) | ||
assert data_path.exists() | ||
|
||
|
||
@pytest.mark.parametrize("path_type", [str, pathlib.Path]) | ||
def test_save_trajectory_rew(data_path, trajectory_rew, path_type): | ||
if isinstance(trajectory_rew.obs, DictObs): | ||
pytest.skip("serialize.save does not yet support DictObs") | ||
serialize.save(path_type(data_path), [trajectory_rew]) | ||
assert data_path.exists() | ||
|
||
|
||
def test_save_load_trajectory(data_path, trajectory): | ||
if isinstance(trajectory.obs, DictObs): | ||
pytest.skip("serialize.save does not yet support DictObs") | ||
serialize.save(data_path, [trajectory]) | ||
|
||
reconstructed = list(serialize.load(data_path)) | ||
reconstructedi = reconstructed[0] | ||
|
||
assert len(reconstructed) == 1 | ||
assert np.allclose(reconstructedi.obs, trajectory.obs) | ||
assert np.allclose(reconstructedi.acts, trajectory.acts) | ||
assert np.allclose(reconstructedi.terminal, trajectory.terminal) | ||
assert not hasattr(reconstructedi, "rews") | ||
|
||
|
||
@pytest.mark.parametrize("load_fn", [serialize.load, serialize.load_with_rewards]) | ||
def test_save_load_trajectory_rew(data_path, trajectory_rew, load_fn): | ||
if isinstance(trajectory_rew.obs, DictObs): | ||
pytest.skip("serialize.save does not yet support DictObs") | ||
serialize.save(data_path, [trajectory_rew]) | ||
|
||
reconstructed = list(load_fn(data_path)) | ||
reconstructedi = reconstructed[0] | ||
|
||
assert len(reconstructed) == 1 | ||
assert np.allclose(reconstructedi.obs, trajectory_rew.obs) | ||
assert np.allclose(reconstructedi.acts, trajectory_rew.acts) | ||
assert np.allclose(reconstructedi.terminal, trajectory_rew.terminal) | ||
assert np.allclose(reconstructedi.rews, trajectory_rew.rews) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tested with this solution. It works.