Skip to content

Commit

Permalink
Refactor tests and imports to throw import error on environment creation
Browse files Browse the repository at this point in the history
  • Loading branch information
jysdoran committed Aug 16, 2023
1 parent f1bb1e3 commit 9f8e238
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 26 deletions.
21 changes: 6 additions & 15 deletions minigrid/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from minigrid import minigrid_env, wrappers
from minigrid.core import roomgrid
from minigrid.core.world_object import Wall
from minigrid.envs.wfc.config import WFC_PRESETS

__version__ = "2.3.0"

Expand Down Expand Up @@ -567,21 +568,11 @@ def register_minigrid_envs():

# WaveFunctionCollapse
# ----------------------------------------
try:
import imageio
import networkx

from minigrid.envs.wfc.config import WFC_PRESETS

for name in WFC_PRESETS.keys():
register(
id=f"MiniGrid-WFC-{name}-v0",
entry_point="minigrid.envs:WFCEnv",
kwargs={"wfc_config": name},
)
except ImportError:
print(
"networkx and/or imageio not found, skipping registering WFC environments"
for name in WFC_PRESETS.keys():
register(
id=f"MiniGrid-WFC-{name}-v0",
entry_point="minigrid.envs.wfc:WFCEnv",
kwargs={"wfc_config": name},
)

# BabyAI - Language based levels - GoTo
Expand Down
1 change: 0 additions & 1 deletion minigrid/envs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,3 @@
from minigrid.envs.redbluedoors import RedBlueDoorEnv
from minigrid.envs.unlock import UnlockEnv
from minigrid.envs.unlockpickup import UnlockPickupEnv
from minigrid.envs.wfc import WFCEnv
17 changes: 16 additions & 1 deletion minigrid/envs/wfc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,19 @@
WFC_PRESETS_SLOW,
WFCConfig,
)
from minigrid.envs.wfc.wfcenv import WFCEnv

# This is wrapped in a try-except block so the presets can be accessed for registration
# Otherwise, importing here will fail when networkx is not installed
try:
from minigrid.envs.wfc.wfcenv import WFCEnv
except ImportError:

class WFCEnv:
"""Dummy class to give a helpful error message when dependencies are missing"""

def __init__(self, *args, **kwargs):
from gymnasium.error import DependencyNotInstalled

raise DependencyNotInstalled(
'WFC dependencies are missing, please run `pip install "minigrid[wfc]"`'
)
18 changes: 9 additions & 9 deletions minigrid/envs/wfc/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,8 @@
from dataclasses import asdict, dataclass
from pathlib import Path

from gymnasium.error import DependencyNotInstalled
from typing_extensions import Literal

try:
import imageio
except ImportError as e:
raise DependencyNotInstalled(
'imageio is missing, please run `pip install "minigrid[wfc]"`'
) from e

PATTERN_PATH = Path(__file__).parent / "patterns"


Expand Down Expand Up @@ -48,8 +40,16 @@ class WFCConfig:

@property
def wfc_kwargs(self):
try:
from imageio.v2 import imread
except ImportError as e:
from gymnasium.error import DependencyNotInstalled

raise DependencyNotInstalled(
'imageio is missing, please run `pip install "minigrid[wfc]"`'
) from e
kwargs = asdict(self)
kwargs["image"] = imageio.v2.imread(kwargs.pop("pattern_path"))[:, :, :3]
kwargs["image"] = imread(kwargs.pop("pattern_path"))[:, :, :3]
return kwargs


Expand Down
10 changes: 10 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""Finds all the specs that we can test with"""
from __future__ import annotations

from importlib.util import find_spec

import gymnasium as gym
import numpy as np

Expand All @@ -13,6 +15,14 @@
)
]

if find_spec("imageio") is None or find_spec("networkx") is None:
# Do not test WFC environments if dependencies are not installed
all_testing_env_specs = [
env_spec
for env_spec in all_testing_env_specs
if not env_spec.entry_point.startswith("minigrid.envs.wfc")
]

minigrid_testing_env_specs = [
env_spec
for env_spec in all_testing_env_specs
Expand Down

0 comments on commit 9f8e238

Please sign in to comment.