utils.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. """Finds all the specs that we can test with"""
  2. from __future__ import annotations
  3. from importlib.util import find_spec
  4. import gymnasium as gym
  5. import numpy as np
  6. all_testing_env_specs = [
  7. env_spec
  8. for env_spec in gym.envs.registry.values()
  9. if (
  10. isinstance(env_spec.entry_point, str)
  11. and env_spec.entry_point.startswith("minigrid.envs")
  12. )
  13. ]
  14. if find_spec("imageio") is None or find_spec("networkx") is None:
  15. # Do not test WFC environments if dependencies are not installed
  16. all_testing_env_specs = [
  17. env_spec
  18. for env_spec in all_testing_env_specs
  19. if not env_spec.entry_point.startswith("minigrid.envs.wfc")
  20. ]
  21. minigrid_testing_env_specs = [
  22. env_spec
  23. for env_spec in all_testing_env_specs
  24. if not env_spec.entry_point.startswith("minigrid.envs.babyai")
  25. ]
  26. def assert_equals(a, b, prefix=None):
  27. """Assert equality of data structures `a` and `b`.
  28. Args:
  29. a: first data structure
  30. b: second data structure
  31. prefix: prefix for failed assertion message for types and dicts
  32. """
  33. assert type(a) is type(b), f"{prefix}Differing types: {a} and {b}"
  34. if isinstance(a, dict):
  35. assert list(a.keys()) == list(b.keys()), f"{prefix}Key sets differ: {a} and {b}"
  36. for k in a.keys():
  37. v_a = a[k]
  38. v_b = b[k]
  39. assert_equals(v_a, v_b)
  40. elif isinstance(a, np.ndarray):
  41. np.testing.assert_array_equal(a, b)
  42. elif isinstance(a, tuple):
  43. for elem_from_a, elem_from_b in zip(a, b):
  44. assert_equals(elem_from_a, elem_from_b)
  45. else:
  46. assert a == b