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

Don't import pygame when not necessary #753

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
14 changes: 7 additions & 7 deletions documentation/source/action.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
],
"source": [
"from metadrive.component.vehicle.base_vehicle import BaseVehicle\n",
"from metadrive.utils import print_source\n",
"from metadrive.utils.doc_utils import print_source\n",
"print_source(BaseVehicle._set_action)\n",
"print_source(BaseVehicle._apply_throttle_brake)"
]
Expand Down Expand Up @@ -96,7 +96,7 @@
"source": [
"from metadrive.envs.metadrive_env import MetaDriveEnv\n",
"from metadrive.component.vehicle.vehicle_type import DefaultVehicle\n",
"from metadrive.utils import generate_gif\n",
"from metadrive.utils.doc_utils import generate_gif\n",
"\n",
"env=MetaDriveEnv(dict(map=\"S\", traffic_density=0))\n",
"frames = []\n",
Expand Down Expand Up @@ -199,7 +199,7 @@
],
"source": [
"from metadrive.policy.env_input_policy import EnvInputPolicy\n",
"from metadrive.utils import print_source\n",
"from metadrive.utils.doc_utils import print_source\n",
"print_source(EnvInputPolicy.get_input_space)"
]
},
Expand Down Expand Up @@ -363,7 +363,7 @@
"outputs": [],
"source": [
"from metadrive.envs.metadrive_env import MetaDriveEnv\n",
"from metadrive.utils import generate_gif\n",
"from metadrive.utils.doc_utils import generate_gif\n",
"\n",
"env=MetaDriveEnv(dict(map=\"S\",\n",
" log_level=50,\n",
Expand Down Expand Up @@ -435,7 +435,7 @@
"source": [
"from metadrive.envs.metadrive_env import MetaDriveEnv\n",
"from metadrive.policy.lange_change_policy import LaneChangePolicy\n",
"from metadrive.utils import generate_gif\n",
"from metadrive.utils.doc_utils import generate_gif\n",
"\n",
"env=MetaDriveEnv(dict(map=\"C\",\n",
" discrete_action=True,\n",
Expand Down Expand Up @@ -545,7 +545,7 @@
],
"source": [
"from metadrive.policy.env_input_policy import ExtraEnvInputPolicy\n",
"from metadrive.utils import print_source\n",
"from metadrive.utils.doc_utils import print_source\n",
"print_source(ExtraEnvInputPolicy.get_input_space)"
]
},
Expand Down Expand Up @@ -882,7 +882,7 @@
"from metadrive.engine.asset_loader import AssetLoader\n",
"from metadrive.policy.replay_policy import ReplayEgoCarPolicy\n",
"from metadrive.envs.scenario_env import ScenarioEnv\n",
"from metadrive.utils import generate_gif\n",
"from metadrive.utils.doc_utils import generate_gif\n",
"import cv2\n",
"from IPython.display import Image\n",
"\n",
Expand Down
4 changes: 2 additions & 2 deletions documentation/source/config_system.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@
"metadata": {},
"outputs": [],
"source": [
"from metadrive.utils import print_source\n",
"from metadrive.utils.doc_utils import print_source\n",
"print_source(ScenarioEnv.default_config)"
]
},
Expand Down Expand Up @@ -241,7 +241,7 @@
"outputs": [],
"source": [
"import metadrive.envs.base_env as base_env\n",
"from metadrive.utils import print_source, CONFIG\n",
"from metadrive.utils.doc_utils import print_source, CONFIG\n",
"module_source = print_source(base_env, [\"BASE_DEFAULT_CONFIG\", \")\\n\\n\"], colorscheme=CONFIG)"
]
},
Expand Down
12 changes: 6 additions & 6 deletions documentation/source/obs.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
"import cv2\n",
"from metadrive.policy.idm_policy import IDMPolicy\n",
"from IPython.display import Image\n",
"from metadrive.utils import generate_gif\n",
"from metadrive.utils.doc_utils import generate_gif\n",
"import numpy as np\n",
"import os\n",
"sensor_size = (84, 60) if os.getenv('TEST_DOC') else (200, 100)\n",
Expand Down Expand Up @@ -340,7 +340,7 @@
},
"outputs": [],
"source": [
"from metadrive import TopDownMetaDrive\n",
"from metadrive.envs.top_down_env import TopDownMetaDrive\n",
"\n",
"env = TopDownMetaDrive()\n",
"try:\n",
Expand Down Expand Up @@ -373,7 +373,7 @@
},
"outputs": [],
"source": [
"from metadrive.utils import print_source\n",
"from metadrive.utils.doc_utils import print_source\n",
"from metadrive.envs.metadrive_env import MetaDriveEnv\n",
"print_source(MetaDriveEnv.get_single_observation)"
]
Expand All @@ -395,7 +395,7 @@
},
"outputs": [],
"source": [
"from metadrive.utils import print_source\n",
"from metadrive.utils.doc_utils import print_source\n",
"from metadrive.envs.top_down_env import TopDownMetaDrive, TopDownSingleFrameMetaDriveEnv\n",
"print_source(TopDownSingleFrameMetaDriveEnv)\n",
"print_source(TopDownMetaDrive)"
Expand Down Expand Up @@ -511,7 +511,7 @@
}
],
"source": [
"from metadrive.utils import generate_gif\n",
"from metadrive.utils.doc_utils import generate_gif\n",
"from IPython.display import Image\n",
"\n",
"frames = []\n",
Expand Down Expand Up @@ -605,7 +605,7 @@
"from metadrive.obs.observation_base import BaseObservation\n",
"from metadrive.obs.image_obs import ImageObservation\n",
"import os\n",
"from metadrive.utils import generate_gif\n",
"from metadrive.utils.doc_utils import generate_gif\n",
"from IPython.display import Image\n",
"sensor_size = (1, 1) if os.getenv('TEST_DOC') else (200, 200)"
]
Expand Down
4 changes: 2 additions & 2 deletions documentation/source/reward_cost_done.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
"source": [
"from metadrive.envs.metadrive_env import MetaDriveEnv\n",
"from metadrive.envs.scenario_env import ScenarioEnv\n",
"from metadrive.utils import print_source\n",
"from metadrive.utils.doc_utils import print_source\n",
"print_source(MetaDriveEnv.reward_function)"
]
},
Expand Down Expand Up @@ -290,7 +290,7 @@
}
],
"source": [
"from metadrive.utils import print_source\n",
"from metadrive.utils.doc_utils import print_source\n",
"from metadrive.envs import MetaDriveEnv\n",
"print_source(MetaDriveEnv.cost_function)"
]
Expand Down
8 changes: 4 additions & 4 deletions documentation/source/rl_environments.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -816,7 +816,7 @@
"from metadrive.envs.varying_dynamics_env import VaryingDynamicsEnv\n",
"import pygame\n",
"import matplotlib.pyplot as plt\n",
"from metadrive.utils import generate_gif\n",
"from metadrive.utils.doc_utils import generate_gif\n",
"import cv2\n",
"from IPython.display import Image\n",
"\n",
Expand Down Expand Up @@ -978,7 +978,7 @@
}
],
"source": [
"from metadrive.utils import print_source\n",
"from metadrive.utils.doc_utils import print_source\n",
"from metadrive import MetaDriveEnv\n",
"print_source(MetaDriveEnv.done_function)"
]
Expand Down Expand Up @@ -1299,7 +1299,7 @@
"from metadrive.engine.asset_loader import AssetLoader\n",
"from metadrive.policy.replay_policy import ReplayEgoCarPolicy\n",
"from metadrive.envs.scenario_env import ScenarioEnv\n",
"from metadrive.utils import generate_gif\n",
"from metadrive.utils.doc_utils import generate_gif\n",
"from IPython.display import Image, clear_output\n",
"import cv2\n",
"\n",
Expand Down Expand Up @@ -1384,7 +1384,7 @@
"from metadrive.engine.asset_loader import AssetLoader\n",
"from metadrive.policy.replay_policy import ReplayEgoCarPolicy\n",
"from metadrive.envs.scenario_env import ScenarioEnv\n",
"from metadrive.utils import generate_gif\n",
"from metadrive.utils.doc_utils import generate_gif\n",
"import cv2\n",
"from IPython.display import Image\n",
"\n",
Expand Down
2 changes: 1 addition & 1 deletion documentation/source/system_design.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"metadata": {},
"outputs": [],
"source": [
"from metadrive.utils import print_source\n",
"from metadrive.utils.doc_utils import print_source\n",
"from metadrive.envs.base_env import BaseEnv\n",
"print_source(BaseEnv.step)"
]
Expand Down
4 changes: 2 additions & 2 deletions documentation/source/top_down_render.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"source": [
"from metadrive.envs import MetaDriveEnv\n",
"from IPython.display import Image\n",
"from metadrive.utils import print_source, get_source\n",
"from metadrive.utils.doc_utils import print_source, get_source\n",
"import cv2"
]
},
Expand Down Expand Up @@ -93,7 +93,7 @@
"outputs": [],
"source": [
"from metadrive.engine.top_down_renderer import TopDownRenderer\n",
"from metadrive.utils import CONFIG, FUNC_2\n",
"from metadrive.utils.doc_utils import CONFIG, FUNC_2\n",
"print_source(TopDownRenderer.__init__, [\"def\", \"# doc-end\"], colorscheme=FUNC_2)"
]
},
Expand Down
2 changes: 1 addition & 1 deletion documentation/source/training.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"import matplotlib.pyplot as plt\n",
"from stable_baselines3.common.monitor import Monitor\n",
"from metadrive.component.map.base_map import BaseMap\n",
"from metadrive.utils import generate_gif\n",
"from metadrive.utils.doc_utils import generate_gif\n",
"from IPython.display import Image\n",
"\n",
"def create_env(need_monitor=False):\n",
Expand Down
10 changes: 2 additions & 8 deletions metadrive/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
from os import environ

environ['PYGAME_HIDE_SUPPORT_PROMPT'] = '1'
import pygame # it is important to import pygame after that
import os

from metadrive.envs import MetaDriveEnv, TopDownMetaDrive, TopDownSingleFrameMetaDriveEnv, TopDownMetaDriveEnvV2, \
SafeMetaDriveEnv, MultiAgentRoundaboutEnv, MultiAgentIntersectionEnv, MultiAgentParkingLotEnv, \
MultiAgentTollgateEnv, MultiAgentBottleneckEnv, MultiAgentMetaDrive, ScenarioEnv
from metadrive.envs import MetaDriveEnv, SafeMetaDriveEnv, ScenarioEnv
from metadrive.utils.registry import get_metadrive_class
import os

MetaDrive_PACKAGE_DIR = os.path.dirname(os.path.abspath(__file__))
10 changes: 8 additions & 2 deletions metadrive/engine/core/manual_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@

from metadrive.utils import import_pygame

pygame, gfxdraw = import_pygame()

pygame = None
def get_pygame():
global pygame
if not pygame:
pygame = import_pygame()

class Controller:
def process_input(self, vehicle):
Expand All @@ -39,6 +42,7 @@ class KeyboardController(Controller):
def __init__(self, pygame_control):
self.pygame_control = pygame_control
if self.pygame_control:
get_pygame()
pygame.init()
else:
self.inputs = InputState()
Expand Down Expand Up @@ -141,6 +145,7 @@ def __init__(self):
"Fail to load evdev, which is required for steering wheel control. "
"Install evdev via pip install evdev"
)
get_pygame()
pygame.display.init()
pygame.joystick.init()
assert not is_win(), "Steering Wheel is supported in linux and mac only"
Expand Down Expand Up @@ -223,6 +228,7 @@ def __init__(self):
"Fail to load evdev, which is required for steering wheel control. "
"Install evdev via pip install evdev"
)
get_pygame()
pygame.display.init()
pygame.joystick.init()
assert not is_win(), "Joystick is supported in linux and mac only"
Expand Down
4 changes: 2 additions & 2 deletions metadrive/engine/top_down_renderer.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import copy
from metadrive.engine.logger import get_logger

from metadrive.utils import generate_gif
from metadrive.utils.doc_utils import generate_gif
import math
from collections import deque
from typing import Optional, Union, Iterable
Expand All @@ -16,7 +16,7 @@
from metadrive.utils.utils import import_pygame
from metadrive.utils.utils import is_map_related_instance

pygame, gfxdraw = import_pygame()
pygame = import_pygame()

color_white = (255, 255, 255)

Expand Down
3 changes: 0 additions & 3 deletions metadrive/envs/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
from metadrive.envs.marl_envs import MultiAgentMetaDrive, MultiAgentBottleneckEnv, MultiAgentTollgateEnv, \
MultiAgentParkingLotEnv, MultiAgentIntersectionEnv, MultiAgentRoundaboutEnv
from metadrive.envs.metadrive_env import MetaDriveEnv
from metadrive.envs.safe_metadrive_env import SafeMetaDriveEnv
from metadrive.envs.top_down_env import TopDownSingleFrameMetaDriveEnv, TopDownMetaDrive, TopDownMetaDriveEnvV2
from metadrive.envs.varying_dynamics_env import VaryingDynamicsEnv
from metadrive.envs.scenario_env import ScenarioEnv
from metadrive.envs.base_env import BaseEnv
2 changes: 1 addition & 1 deletion metadrive/examples/Basic_MetaDrive_Usages.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@
"os.environ['SDL_VIDEODRIVER']='dummy'\n",
"# Note: this step is only for cheating Colab\n",
"\n",
"from metadrive import MultiAgentRoundaboutEnv, MultiAgentBottleneckEnv, MultiAgentIntersectionEnv, MultiAgentParkingLotEnv, MultiAgentTollgateEnv\n",
"from metadrive.envs.marl_envs import MultiAgentRoundaboutEnv, MultiAgentBottleneckEnv, MultiAgentIntersectionEnv, MultiAgentParkingLotEnv, MultiAgentTollgateEnv\n",
"from metadrive.examples import expert\n",
"\n",
"env_classes = [MultiAgentRoundaboutEnv, \n",
Expand Down
2 changes: 1 addition & 1 deletion metadrive/examples/profile_metadrive_marl.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import numpy as np

from metadrive import MultiAgentRoundaboutEnv
from metadrive.envs.marl_envs import MultiAgentRoundaboutEnv
from metadrive.utils import setup_logger

if __name__ == '__main__':
Expand Down
2 changes: 1 addition & 1 deletion metadrive/examples/top_down_metadrive.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import matplotlib.pyplot as plt

from metadrive import TopDownMetaDrive
from metadrive.envs.top_down_env import TopDownMetaDrive
from metadrive.constants import HELP_MESSAGE
from metadrive.examples.ppo_expert.numpy_expert import expert

Expand Down
2 changes: 1 addition & 1 deletion metadrive/obs/top_down_obs.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
ObjectGraphics, LaneGraphics
from metadrive.utils import import_pygame

pygame, gfxdraw = import_pygame()
pygame = import_pygame()


class TopDownObservation(BaseObservation):
Expand Down
2 changes: 1 addition & 1 deletion metadrive/obs/top_down_obs_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from collections import namedtuple

PositionType = Union[Tuple[float, float], np.ndarray]
pygame, gfxdraw = import_pygame()
pygame = import_pygame()
COLOR_BLACK = pygame.Color("black")
history_object = namedtuple("history_object", "name position heading_theta WIDTH LENGTH color done type")

Expand Down
2 changes: 1 addition & 1 deletion metadrive/obs/top_down_obs_multi_channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from metadrive.component.navigation_module.edge_network_navigation import EdgeNetworkNavigation
from metadrive.component.navigation_module.trajectory_navigation import TrajectoryNavigation

pygame, gfxdraw = import_pygame()
pygame = import_pygame()
COLOR_WHITE = pygame.Color("white")
DEFAULT_TRAJECTORY_LANE_WIDTH = 3

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import time

from metadrive import TopDownMetaDriveEnvV2
from metadrive.envs.top_down_env import TopDownMetaDriveEnvV2

if __name__ == '__main__':
env = TopDownMetaDriveEnvV2(dict(num_scenarios=10, frame_stack=10, frame_skip=3))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from metadrive.utils.math import wrap_to_pi
import numpy as np

from metadrive import MultiAgentRoundaboutEnv
from metadrive.envs.marl_envs import MultiAgentRoundaboutEnv
from metadrive.component.map.base_map import BaseMap
from metadrive.component.map.pg_map import MapGenerateMethod
from metadrive.envs.safe_metadrive_env import SafeMetaDriveEnv
Expand Down
1 change: 0 additions & 1 deletion metadrive/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,3 @@
from metadrive.utils.registry import get_metadrive_class
from metadrive.utils.utils import is_mac, import_pygame, recursive_equal, setup_logger, merge_dicts, \
concat_step_infos, is_win, time_me
from metadrive.utils.doc_utils import print_source, list_files, get_source, generate_gif, CONFIG, FUNC, FUNC_2
2 changes: 1 addition & 1 deletion metadrive/utils/draw_top_down_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from metadrive.engine.top_down_renderer import draw_top_down_map_native as native_draw
from metadrive.utils.utils import import_pygame

pygame, gfxdraw = import_pygame()
pygame = import_pygame()


def draw_top_down_map(map,
Expand Down
3 changes: 1 addition & 2 deletions metadrive/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ def is_port_occupied(port, host='127.0.0.1'):
def import_pygame():
os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = "hide"
import pygame
from pygame import gfxdraw
return pygame, gfxdraw
return pygame


def get_time_str():
Expand Down
Loading