Skip to content

Commit

Permalink
-Improve error messages for missing or non-car initial object
Browse files Browse the repository at this point in the history
-Add skip for MetaDrive tests
-Update test_pedestrian_movement to have specific coordinates for Car and Pedestrian
  • Loading branch information
lola831 committed Feb 18, 2025
1 parent edbc4f3 commit 3ef779e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
10 changes: 5 additions & 5 deletions src/scenic/simulators/metadrive/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
metadrive = None
try:
import metadrive
except ImportError:
raise ImportError("Metadrive is required. Please install the 'metadrive' package.")
if metadrive:
from .simulator import MetaDriveSimulator
del metadrive
raise ImportError(
"Metadrive is required. Please install the 'metadrive' package (and sumolib) or use scenic[metadrive]."
)

from .simulator import MetaDriveSimulator
1 change: 0 additions & 1 deletion src/scenic/simulators/metadrive/model.scenic
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ from scenic.simulators.metadrive.simulator import MetaDriveSimulator
from scenic.simulators.metadrive.actions import *
from scenic.simulators.metadrive.behaviors import *
from scenic.simulators.metadrive.utils import scenicToMetaDriveHeading
from metadrive.utils.math import norm


if globalParameters.get("sumo_map") is None:
Expand Down
9 changes: 7 additions & 2 deletions src/scenic/simulators/metadrive/simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,16 @@ def __init__(
sumo_map_boundary,
**kwargs,
):
# NOTE: MetaDrive requires at least one agent to be defined per simulation run
# NOTE: MetaDrive requires at least one agent to be defined per simulation run,
# and the first object must be a car (to serve as the ego vehicle).
try:
if len(scene.objects) == 0:
raise SimulationCreationError(
"The Metadrive interface requires you to define at least one Scenic object."
"Metadrive requires you to define at least one Scenic object."
)
if not scene.objects[0].isCar:
raise SimulationCreationError(
"The first object must be a car to serve as the ego vehicle in Metadrive."
)
except SimulationCreationError as e:
print(e)
Expand Down
12 changes: 9 additions & 3 deletions tests/simulators/metadrive/test_metadrive.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@

import pytest

from scenic.simulators.metadrive import MetaDriveSimulator
try:
import metadrive

from scenic.simulators.metadrive import MetaDriveSimulator
except ModuleNotFoundError:
pytest.skip("MetaDrive package not installed", allow_module_level=True)

from tests.utils import compileScenic, pickle_test, sampleScene, tryPickling


Expand Down Expand Up @@ -110,8 +116,8 @@ def test_pedestrian_movement(getMetadriveSimulator):
do WalkForward() for 2 steps
do StopWalking() for 2 steps
ego = new Car
pedestrian = new Pedestrian with behavior WalkThenStop
ego = new Car at (30, 2)
pedestrian = new Pedestrian at (50, 6), with behavior WalkThenStop
record initial pedestrian.position as InitialPos
record final pedestrian.position as FinalPos
Expand Down

0 comments on commit 3ef779e

Please sign in to comment.