Skip to content

Commit

Permalink
Colab test (#495)
Browse files Browse the repository at this point in the history
* add colab test to CI

* update workflow

* fix main.yaml

* fix bug

* fix bug

* fix bug

* add real-world example to colab

* fix bug
  • Loading branch information
QuanyiLi committed Sep 12, 2023
1 parent e2f1dc5 commit 7d6f8ef
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 3 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,29 @@ jobs:
cd metadrive/
pytest --cov=./ --cov-config=.coveragerc --cov-report=xml -sv tests/test_export_record_scenario
test_ipynb:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Python 3.9
uses: actions/setup-python@v2
with:
python-version: 3.9
- name: Blackbox tests
run: |
pip install cython
pip install numpy
pip install -e .
python -m metadrive.pull_asset
pip install pytest
pip install pytest-cov
pip install ray
cd metadrive/
pip install nbmake pytest-xdist
mkdir ./tests/test_ipynb
cp ./examples/Basic_MetaDrive_Usages.ipynb ./tests/test_ipynb/
pytest --nbmake -n=auto ./tests/test_ipynb/
# - name: Upload coverage to Codecov
# uses: codecov/codecov-action@v1
# with:
Expand Down
1 change: 1 addition & 0 deletions metadrive/base_class/base_object.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import copy

import logging
import math
from typing import Dict
Expand Down
3 changes: 2 additions & 1 deletion metadrive/component/traffic_participants/pedestrian.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ def reset(self, position, heading_theta: float = 0., random_seed=None, name=None
self.current_speed_model = self.SPEED_LIST[0]
if self._instance is not None:
self._instance.detachNode()
self._instance = Pedestrian._MODEL[self.current_speed_model].instanceTo(self.origin)
if self.render:
self._instance = Pedestrian._MODEL[self.current_speed_model].instanceTo(self.origin)

@classmethod
def init_pedestrian_model(cls):
Expand Down
2 changes: 2 additions & 0 deletions metadrive/engine/asset_loader.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
from metadrive.constants import RENDER_MODE_NONE
import pathlib
import sys
from metadrive.pull_asset import pull_asset
Expand All @@ -24,6 +25,7 @@ def init_loader(engine):
if engine.win is None:
AssetLoader.logger.debug("Physics world mode")
return
assert engine.mode != RENDER_MODE_NONE
AssetLoader.logger.debug("Onscreen/Offscreen mode, Render/Load Elements")
AssetLoader.loader = engine.loader

Expand Down
2 changes: 1 addition & 1 deletion metadrive/engine/base_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ def warmup(self):
This function automatically initialize models/objects. It can prevent the lagging when creating some objects
for the first time.
"""
if self.global_config["preload_models"]:
if self.global_config["preload_models"] and self.mode != RENDER_MODE_NONE:
from metadrive.component.traffic_participants.pedestrian import Pedestrian
from metadrive.component.traffic_light.base_traffic_light import BaseTrafficLight
from metadrive.component.static_object.traffic_object import TrafficBarrier
Expand Down
99 changes: 98 additions & 1 deletion metadrive/examples/Basic_MetaDrive_Usages.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@
"outputs": [],
"source": [
"# @title Test Installation\n",
"from metadrive.engine.engine_utils import close_engine\n",
"close_engine()\n",
"# NOTE: usually you don't need the above lines. It is only for avoiding a potential bug when running on colab\n",
"\n",
"from metadrive import MetaDriveEnv\n",
"from metadrive.examples import expert\n",
"\n",
Expand Down Expand Up @@ -115,6 +119,9 @@
"outputs": [],
"source": [
"# @title A minimalist example of using MetaDrive with the built-in PPO expert policy\n",
"from metadrive.engine.engine_utils import close_engine\n",
"close_engine()\n",
"# NOTE: usually you don't need the above lines. It is only for avoiding a potential bug when running on colab\n",
"\n",
"import os\n",
"os.environ['SDL_VIDEODRIVER']='dummy'\n",
Expand Down Expand Up @@ -172,6 +179,10 @@
"outputs": [],
"source": [
"# @title PPO expert policy can also drive in the safety-critical SafeMetaDrive environment\n",
"from metadrive.engine.engine_utils import close_engine\n",
"close_engine()\n",
"# NOTE: usually you don't need the above lines. It is only for avoiding a potential bug when running on colab\n",
"\n",
"import os\n",
"os.environ['SDL_VIDEODRIVER']='dummy'\n",
"# Note: this step is only for cheating Colab\n",
Expand Down Expand Up @@ -230,6 +241,10 @@
"outputs": [],
"source": [
"# @title Multi-agent Environment Visualization\n",
"from metadrive.engine.engine_utils import close_engine\n",
"close_engine()\n",
"# NOTE: usually you don't need the above lines. It is only for avoiding a potential bug when running on colab\n",
"\n",
"import os\n",
"os.environ['SDL_VIDEODRIVER']='dummy'\n",
"# Note: this step is only for cheating Colab\n",
Expand Down Expand Up @@ -275,6 +290,80 @@
"Image(open(\"demo.gif\", 'rb').read())"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# @title Real-world Scenario Environment Visualization\n",
"from metadrive.engine.engine_utils import close_engine\n",
"close_engine()\n",
"# NOTE: usually you don't need the above lines. It is only for avoiding a potential bug when running on colab\n",
"\n",
"import argparse\n",
"import random\n",
"from metadrive.policy.replay_policy import ReplayEgoCarPolicy\n",
"from metadrive.constants import HELP_MESSAGE\n",
"from metadrive.engine.asset_loader import AssetLoader\n",
"from metadrive.envs.real_data_envs.waymo_env import WaymoEnv\n",
"\n",
"\n",
"class DemoWaymoEnv(WaymoEnv):\n",
" def reset(self, seed=None):\n",
" if self.engine is not None:\n",
" seeds = [i for i in range(self.config[\"num_scenarios\"])]\n",
" seeds.remove(self.current_seed)\n",
" seed = random.choice(seeds)\n",
" return super(DemoWaymoEnv, self).reset(seed=seed)\n",
"\n",
"\n",
"extra_args = dict(film_size=(1200, 1200))\n",
"asset_path = AssetLoader.asset_path\n",
"\n",
"try:\n",
" env = DemoWaymoEnv(\n",
" {\n",
" \"manual_control\": False,\n",
" \"reactive_traffic\": False,\n",
" \"use_render\": False,\n",
" \"agent_policy\": ReplayEgoCarPolicy,\n",
" \"data_directory\": AssetLoader.file_path(asset_path, \"waymo\", return_raw_style=False),\n",
" \"num_scenarios\": 3\n",
" }\n",
" )\n",
" o, _ = env.reset()\n",
" frames = []\n",
" for i in range(1, 100000):\n",
" o, r, tm, tc, info = env.step([1.0, 0.])\n",
" frame=env.render(\n",
" mode=\"top_down\",\n",
" **extra_args\n",
" )\n",
" frames.append(frame)\n",
" if tm or tc:\n",
" env.reset()\n",
" break\n",
" \n",
"except Exception as e:\n",
" raise e\n",
"finally:\n",
" env.close()\n",
" \n",
"# render image\n",
"print(\"\\nGenerate gif...\")\n",
"import pygame\n",
"import numpy as np\n",
"from PIL import Image\n",
"\n",
"imgs = [pygame.surfarray.array3d(frame) for frame in frames]\n",
"imgs = [Image.fromarray(img) for img in imgs]\n",
"imgs[0].save(\"demo.gif\", save_all=True, append_images=imgs[1:], duration=50, loop=0)\n",
"print(\"\\nOpen gif...\")\n",
"from IPython.display import Image\n",
"Image(open(\"demo.gif\", 'rb').read())"
]
},
{
"attachments": {},
"cell_type": "markdown",
Expand All @@ -300,6 +389,10 @@
"outputs": [],
"source": [
"# @title Draw the generated maps in top-down view\n",
"from metadrive.engine.engine_utils import close_engine\n",
"close_engine()\n",
"# NOTE: usually you don't need the above lines. It is only for avoiding a potential bug when running on colab\n",
"\n",
"\n",
"import random\n",
"\n",
Expand Down Expand Up @@ -342,6 +435,10 @@
"# @title Draw the generated maps in top-down view with fixed block sequence\n",
"# @markdown You can also specify the road block sequence then randomize the block parameters.\n",
"# @markdown Please refer to [documentation](https://metadrive-simulator.readthedocs.io/en/latest/env_config.html#map-config) for the meaning of the map string.\n",
"from metadrive.engine.engine_utils import close_engine\n",
"close_engine()\n",
"# NOTE: usually you don't need the above lines. It is only for avoiding a potential bug when running on colab\n",
"\n",
"\n",
"import random\n",
"\n",
Expand Down Expand Up @@ -392,7 +489,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.17"
"version": "3.9.18"
}
},
"nbformat": 4,
Expand Down

0 comments on commit 7d6f8ef

Please sign in to comment.