Skip to content

Commit

Permalink
Remove seaborn (mirror #750) (#808)
Browse files Browse the repository at this point in the history
* remove seaborn

* format

* Fix

* remove

* fix

* format

* Fix a test

* fix bug

* fix

---------

Co-authored-by: Maxime Desroches <[email protected]>
  • Loading branch information
pengzhenghao and maxime-desroches authored Jan 31, 2025
1 parent 6102378 commit 5e27400
Show file tree
Hide file tree
Showing 13 changed files with 57 additions and 63 deletions.
16 changes: 7 additions & 9 deletions metadrive/base_class/base_object.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
import copy
from abc import ABC
from metadrive.engine.logger import get_logger
from metadrive.type import MetaDriveType

import logging
import math
from abc import ABC
from typing import Dict

import numpy as np
import seaborn as sns
from panda3d.bullet import BulletWorld, BulletBodyNode, BulletVehicle
from panda3d.core import LVector3, NodePath, PandaNode
from metadrive.constants import Semantics, CameraTagStateKey

from metadrive.base_class.base_runnable import BaseRunnable
from metadrive.constants import ObjectState
from metadrive.constants import Semantics, CameraTagStateKey, get_color_palette
from metadrive.engine.asset_loader import AssetLoader
from metadrive.engine.core.physics_world import PhysicsWorld
from metadrive.engine.logger import get_logger
from metadrive.engine.physics_node import BaseRigidBodyNode, BaseGhostBodyNode
from metadrive.type import MetaDriveType
from metadrive.utils import Vector
from metadrive.utils import get_np_random
from metadrive.utils import random_string
Expand Down Expand Up @@ -151,8 +149,8 @@ def __init__(self, name=None, random_seed=None, config=None, escape_random_seed_
self.loader.__init__()

# add color setting for visualization
color = sns.color_palette("colorblind")
color.remove(color[2]) # Remove the green and leave it for special vehicle
color = get_color_palette()
color.pop(2) # Remove the green and leave it for special vehicle
idx = get_np_random().randint(len(color))
rand_c = color[idx]
self._panda_color = rand_c
Expand Down
8 changes: 3 additions & 5 deletions metadrive/component/traffic_participants/cyclist.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from metadrive.component.traffic_participants.base_traffic_participant import BaseTrafficParticipant
from metadrive.constants import CollisionGroup
from metadrive.constants import MetaDriveType, Semantics
from metadrive.constants import MetaDriveType, Semantics, get_color_palette
from metadrive.engine.asset_loader import AssetLoader
from metadrive.engine.physics_node import BaseRigidBodyNode

Expand Down Expand Up @@ -92,7 +92,6 @@ def __init__(self, position, heading_theta, random_seed, name=None, **kwargs):

# Add some color to help debug
from panda3d.core import Material, LVecBase4
import seaborn as sns

show_contour = self.config["show_contour"] if "show_contour" in self.config else False
if show_contour:
Expand Down Expand Up @@ -144,7 +143,7 @@ def __init__(self, position, heading_theta, random_seed, name=None, **kwargs):
line_np.setMaterial(line_material, True)
line_np.reparentTo(self.origin)

color = sns.color_palette("colorblind")
color = get_color_palette()
color.remove(color[2]) # Remove the green and leave it for special vehicle
idx = 0
rand_c = color[idx]
Expand Down Expand Up @@ -179,8 +178,7 @@ def reset(self, position, heading_theta: float = 0., random_seed=None, name=None

# Add some color to help debug
from panda3d.core import Material, LVecBase4
import seaborn as sns
color = sns.color_palette("colorblind")
color = list(COLOR_PALETTE)
color.remove(color[2]) # Remove the green and leave it for special vehicle
idx = 0
rand_c = color[idx]
Expand Down
5 changes: 2 additions & 3 deletions metadrive/component/traffic_participants/pedestrian.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from panda3d.core import LineSegs, NodePath

from metadrive.component.traffic_participants.base_traffic_participant import BaseTrafficParticipant
from metadrive.constants import MetaDriveType, Semantics
from metadrive.constants import MetaDriveType, Semantics, get_color_palette
from metadrive.engine.asset_loader import AssetLoader
from metadrive.engine.physics_node import BaseRigidBodyNode
from metadrive.utils.math import norm
Expand Down Expand Up @@ -171,7 +171,6 @@ def __init__(self, position, heading_theta, width, length, height, random_seed=N

# Add some color to help debug
from panda3d.core import Material
import seaborn as sns

show_contour = self.config["show_contour"] if "show_contour" in self.config else False
if show_contour:
Expand Down Expand Up @@ -223,7 +222,7 @@ def __init__(self, position, heading_theta, width, length, height, random_seed=N
line_np.setMaterial(line_material, True)
line_np.reparentTo(self.origin)

color = sns.color_palette("colorblind")
color = get_color_palette()
color.remove(color[2]) # Remove the green and leave it for special vehicle
idx = 0
rand_c = color[idx]
Expand Down
5 changes: 2 additions & 3 deletions metadrive/component/vehicle/base_vehicle.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from typing import Union, Optional

import numpy as np
import seaborn as sns
from panda3d._rplight import RPSpotLight
from panda3d.bullet import BulletVehicle, BulletBoxShape, ZUp
from panda3d.core import Material, Vec3, TransformState
Expand All @@ -17,7 +16,7 @@
from metadrive.component.lane.straight_lane import StraightLane
from metadrive.component.navigation_module.node_network_navigation import NodeNetworkNavigation
from metadrive.component.pg_space import VehicleParameterSpace, ParameterSpace
from metadrive.constants import CamMask
from metadrive.constants import CamMask, get_color_palette
from metadrive.constants import MetaDriveType, CollisionGroup
from metadrive.constants import Semantics
from metadrive.engine.asset_loader import AssetLoader
Expand Down Expand Up @@ -1021,7 +1020,7 @@ def lane_index(self):
def panda_color(self):
c = super(BaseVehicle, self).panda_color
if self._use_special_color:
color = sns.color_palette("colorblind")
color = get_color_palette()
rand_c = color[2] # A pretty green
c = rand_c
return c
Expand Down
14 changes: 14 additions & 0 deletions metadrive/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -553,3 +553,17 @@ class CameraTagStateKey:

DEFAULT_SENSOR_OFFSET = (0., 0.8, 1.5)
DEFAULT_SENSOR_HPR = (0., -5, 0.0)

COLOR_PALETTE = (
(0.00392156862745098, 0.45098039215686275,
0.6980392156862745), (0.8705882352941177, 0.5607843137254902, 0.0196078431372549),
(0.00784313725490196, 0.6196078431372549, 0.45098039215686275), (0.8352941176470589, 0.3686274509803922, 0.0),
(0.8, 0.47058823529411764, 0.7372549019607844), (0.792156862745098, 0.5686274509803921, 0.3803921568627451),
(0.984313725490196, 0.6862745098039216,
0.8941176470588236), (0.5803921568627451, 0.5803921568627451, 0.5803921568627451),
(0.9254901960784314, 0.8823529411764706, 0.2), (0.33725490196078434, 0.7058823529411765, 0.9137254901960784)
)


def get_color_palette():
return list(COLOR_PALETTE)
8 changes: 2 additions & 6 deletions metadrive/envs/multigoal_intersection.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@

import gymnasium as gym
import numpy as np
import seaborn as sns

from metadrive.component.navigation_module.node_network_navigation import NodeNetworkNavigation
from metadrive.component.pg_space import ParameterSpace, Parameter, DiscreteSpace, BoxSpace
from metadrive.component.pgblock.first_block import FirstPGBlock
from metadrive.component.pgblock.intersection import InterSectionWithUTurn
from metadrive.component.road_network import Road
from metadrive.constants import DEFAULT_AGENT
from metadrive.constants import DEFAULT_AGENT, get_color_palette
from metadrive.engine.logger import get_logger
from metadrive.envs.metadrive_env import MetaDriveEnv
from metadrive.manager.base_manager import BaseManager
Expand Down Expand Up @@ -208,7 +207,7 @@ def __init__(self):
vehicle_config = config["vehicle_config"]
self.navigations = {}
navi = NodeNetworkNavigation
colors = sns.color_palette("colorblind")
colors = [get_color_palette()[c] for c in range(len(self.GOALS))]
for c, (dest_name, road) in enumerate(self.GOALS.items()):
self.navigations[dest_name] = navi(
# self.engine,
Expand Down Expand Up @@ -659,9 +658,6 @@ def reset(self, *args, **kwargs):
print('=======================')

if done:

import numpy as np

# for t in range(i):
# # avg = [v[t] for k, v in obs_recorder.items()]
# v = np.stack([v[0] for k, v in obs_recorder.items()])
Expand Down
5 changes: 2 additions & 3 deletions metadrive/examples/run_scenario_online_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
"""
import pathlib

import seaborn as sns

from metadrive.constants import get_color_palette
from metadrive.engine.asset_loader import AssetLoader
from metadrive.envs.scenario_env import ScenarioOnlineEnv
from metadrive.policy.replay_policy import ReplayEgoCarPolicy
Expand Down Expand Up @@ -41,6 +40,6 @@

if i == 999:
raise ValueError("Can not arrive dest")
assert env.agent.panda_color == sns.color_palette("colorblind")[2]
assert env.agent.panda_color == get_color_palette()[2]
finally:
env.close()
5 changes: 2 additions & 3 deletions metadrive/tests/test_env/test_metadrive_env.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import copy
import seaborn as sns
import os

import numpy as np
import pytest

from metadrive import MetaDriveEnv
from metadrive.constants import TerminationState
from metadrive.constants import TerminationState, get_color_palette

# Key: scenario name, value: environmental config
blackbox_test_configs = dict(
Expand Down Expand Up @@ -60,7 +59,7 @@ def test_pgdrive_env_blackbox(config):
env = MetaDriveEnv(config=cfg)
try:
obs, _ = env.reset()
assert env.agent.panda_color == sns.color_palette("colorblind")[2]
assert env.agent.panda_color == get_color_palette()[2]
assert env.observation_space.contains(obs)
_act(env, env.action_space.sample())
for x in [-1, 0, 1]:
Expand Down
12 changes: 4 additions & 8 deletions metadrive/tests/test_env/test_scenario_online_env.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
import pathlib

import pytest
import seaborn as sns
import numpy as np

from metadrive.constants import get_color_palette
from metadrive.engine.asset_loader import AssetLoader
from metadrive.envs.scenario_env import ScenarioOnlineEnv
from metadrive.policy.idm_policy import TrajectoryIDMPolicy
from metadrive.policy.replay_policy import ReplayEgoCarPolicy
from metadrive.scenario.utils import read_dataset_summary, read_scenario_data
import pickle
import pathlib

from metadrive.policy.replay_policy import ReplayEgoCarPolicy


@pytest.mark.parametrize("data_directory", ["waymo", "nuscenes"])
Expand Down Expand Up @@ -40,7 +36,7 @@ def test_scenario_online_env(data_directory, render=False):

if i == 999:
raise ValueError("Can not arrive dest")
assert env.agent.panda_color == sns.color_palette("colorblind")[2]
assert env.agent.panda_color == get_color_palette()[2]
finally:
env.close()

Expand Down
6 changes: 3 additions & 3 deletions metadrive/tests/test_env/test_waymo_env.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest
import seaborn as sns
import numpy as np
import pytest

from metadrive.constants import get_color_palette
from metadrive.engine.asset_loader import AssetLoader
from metadrive.envs.scenario_env import ScenarioEnv
from metadrive.policy.idm_policy import TrajectoryIDMPolicy
Expand Down Expand Up @@ -38,7 +38,7 @@ def test_waymo_env(policy, render=False, num_scenarios=3):

if i == 999:
raise ValueError("Can not arrive dest")
assert env.agent.panda_color == sns.color_palette("colorblind")[2]
assert env.agent.panda_color == get_color_palette()[2]
finally:
env.close()

Expand Down
34 changes: 15 additions & 19 deletions metadrive/tests/test_functionality/test_nondeterminism.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@
Usage: run this file. or pytest.
"""
import copy
import os

from collections import defaultdict

import numpy as np
import pandas as pd
import pytest
import seaborn as sns
from metadrive import MetaDriveEnv
from metadrive.constants import TerminationState

from metadrive.envs.metadrive_env import MetaDriveEnv
from metadrive.examples.ppo_expert.numpy_expert import expert

Expand Down Expand Up @@ -46,22 +43,21 @@ def assert_dict_almost_equal(dict1, dict2, tol=1e-3):


def are_traces_deterministic(traces) -> bool:
df = pd.DataFrame(traces)

# grouping by repetition to get a list of traces
traces = df.groupby("repetition")

# drop index and repetition ID to compare only step info later
stripped_traces = [trace.reset_index(drop=True).drop("repetition", axis=1) for _, trace in traces]
# Group traces by repetition
grouped_traces = defaultdict(list)
for trace in traces:
repetition_id = trace["repetition"]
grouped_traces[repetition_id].append({k: v for k, v in trace.items() if k != "repetition"})

# iterate over each trace and check if it is equal to the first one
are_equal_to_first_trace = [trace.equals(stripped_traces[0]) for trace in stripped_traces]
# Convert traces to lists of dictionaries
stripped_traces = [sorted(group, key=lambda x: sorted(x.items())) for group in grouped_traces.values()]

first_trace = stripped_traces[0].to_dict()
# Compare each trace list to the first one
first_trace = stripped_traces[0] # This is a list of dictionaries
for trace in stripped_traces:
# Assert
trace = trace.to_dict()
assert_dict_almost_equal(first_trace, trace)
assert len(trace) == len(first_trace)
for i in range(len(first_trace)):
assert_dict_almost_equal(first_trace[i], trace[i])


@pytest.mark.parametrize(
Expand Down
1 change: 1 addition & 0 deletions metadrive/tests/test_functionality/test_obs_noise.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@


def _act(env, action):
action = np.asarray(action, dtype=env.action_space.dtype)
assert env.action_space.contains(action)
obs, reward, terminated, truncated, info = env.step(action)
assert env.observation_space.contains(obs)
Expand Down
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ def is_win():
"matplotlib",
"pygame",
"yapf",
"seaborn",
"tqdm",
"progressbar",
# "panda3d==1.10.8",
Expand Down

0 comments on commit 5e27400

Please sign in to comment.