Skip to content
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

Support heterogeneous dicts in infos #250

Open
wants to merge 16 commits into
base: main
Choose a base branch
from

Conversation

jamartinh
Copy link
Contributor

@jamartinh jamartinh commented Oct 13, 2024

Infos is now a List[Dict]

Description

Allow for infos to be a list of heterogeneous dicts. Currently many environments use different keys for reset() and step(). Also wrappers are free to add arbitrary keys at arbitrary steps, for instance, RecordEspisodeStatistics add keys in the final step.

Hence, so save infos the users has to use almost always a custom step callback to regularize info data.

Fixes #246

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • This change requires a documentation update (perhaps)

Checklist:

  • I have run the pre-commit checks with pre-commit run --all-files (see CONTRIBUTING.md instructions to set it up)
  • I have run pytest -v and no errors are present.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have made corresponding changes to the documentation.
  • I solved any possible warnings that pytest -v has generated that are related to my code to the best of my knowledge.
  • I have added tests that prove my fix is effective or that my feature works.
  • New and existing unit tests pass locally with my changes.
  • Backwards compatibility checks.
  • New feature tests for backends: hdf5 and arrow.
  • New parameter infos_format= None|"dict", "list" to add the optional functionality
  • New parameter infos_format= None|"dict", "list" to add the optional functionality

Infos is now a List[Dict]
…s the same for both backend [arrow,hdf5]

Corrected test_step_data_callback.py to not given an error with varying info structure.
All tests in data_collector path now pass OK
Copy link
Member

@younik younik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR @jamartinh!

I didn't review it completely, just added a couple of comments. However, I do not agree on changing the infos to be a list of dict, as this approach is generally inefficient and inconsistent with what we do with Dict spaces.

To support heterogenous info, we should fill missing values with None/np.nans.
It should be possible to do it in EpisodeBuffer, I believe. This allows minimal changes in the rest of the codebase.

minari/data_collector/episode_buffer.py Outdated Show resolved Hide resolved
minari/dataset/_storages/arrow_storage.py Show resolved Hide resolved
…s the same for both backend [arrow,hdf5]

Corrected test_step_data_callback.py to not given an error with varying info structure.
All tests now pass OK
Optional Using orjson for very fast serialization
…s the same for both backend [arrow,hdf5]

Corrected test_step_data_callback.py to not given an error with varying info structure.
All tests now pass OK
Optional Using orjson for very fast serialization
…s the same for both backend [arrow,hdf5]

Corrected test_step_data_callback.py to not given an error with varying info structure.
All tests now pass OK
Optional Using orjson for very fast serialization
…s the same for both backend [arrow,hdf5]

Corrected test_step_data_callback.py to not given an error with varying info structure.
All tests now pass OK
Optional Using orjson for very fast serialization
…s the same for both backend [arrow,hdf5]

Corrected test_step_data_callback.py to not given an error with varying info structure.
All tests now pass OK
Optional Using orjson for very fast serialization
…s the same for both backend [arrow,hdf5]

Corrected test_step_data_callback.py to not given an error with varying info structure.
All tests now pass OK
Optional Using orjson for very fast serialization
…s the same for both backend [arrow,hdf5]

Corrected test_step_data_callback.py to not given an error with varying info structure.
All tests now pass OK
Optional Using orjson for very fast serialization
…s the same for both backend [arrow,hdf5]

Corrected test_step_data_callback.py to not given an error with varying info structure.
All tests now pass OK
Optional Using orjson for very fast serialization
@jamartinh
Copy link
Contributor Author

The feature is now optional, by specifying an optional parameter in data collector creation infos_format="list" otherwise when ìnfos_format=None (default) or infos_format="dict" all the functionality is exactly the same as default current version.

…s the same for both backend [arrow,hdf5]

Corrected test_step_data_callback.py to not given an error with varying info structure.
All tests now pass OK
Optional Using orjson for very fast serialization
@jamartinh
Copy link
Contributor Author

Infos can be saved as a dictionary of np.arrays or as a list of arbitrary dictionaries by
setting the infos_format parameter. Default is dictionary format infos_format = None or "dict":

from minari import DataCollector
import gymnasium as gym

env = gym.make('CartPole-v1')
env = DataCollector(env, record_infos=True, infos_format="list")

@younik
Copy link
Member

younik commented Oct 16, 2024

Infos can be saved as a dictionary of np.arrays or as a list of arbitrary dictionaries by setting the infos_format parameter. Default is dictionary format infos_format = None or "dict":

from minari import DataCollector
import gymnasium as gym

env = gym.make('CartPole-v1')
env = DataCollector(env, record_infos=True, infos_format="list")

I don't like this for a couple reasons:

  • Now the data format the user can expect when dealing with a dataset is uncertain. Even if it is just for the info, this weak the purpose of Minari which is, among others, to have a standardized format for offline RL data.
  • Now every time we manipulate info, we basically need an if-else. Every branch is a different implementation to do the same thing, making the code less maintainable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug Report] Error when attempting to create a dataset for MuJoco Hopper-v4 and v5
2 participants