From 93ba5d80be63961c842b7460b4a627b6d555ced5 Mon Sep 17 00:00:00 2001 From: luigiberducci Date: Thu, 14 Sep 2023 19:10:21 +0200 Subject: [PATCH] black --- gym/f110_gym/envs/f110_env.py | 6 ++++-- gym/f110_gym/envs/rendering/__init__.py | 19 ++++++++++++++++--- .../envs/rendering/rendering_pygame.py | 19 +++++++++++-------- .../envs/rendering/rendering_pyglet.py | 9 +++------ gym/f110_gym/test/benchmark_renderer.py | 6 ++---- 5 files changed, 36 insertions(+), 23 deletions(-) diff --git a/gym/f110_gym/envs/f110_env.py b/gym/f110_gym/envs/f110_env.py index 6739eeca..e0453d12 100644 --- a/gym/f110_gym/envs/f110_env.py +++ b/gym/f110_gym/envs/f110_env.py @@ -178,7 +178,9 @@ def __init__(self, config: dict = None, render_mode=None, **kwargs): # stateful observations for rendering self.render_obs = None self.render_mode = render_mode - self.renderer, self.render_spec = make_renderer(track=self.track, agent_ids=self.agent_ids, render_mode=render_mode) + self.renderer, self.render_spec = make_renderer( + track=self.track, agent_ids=self.agent_ids, render_mode=render_mode + ) self.metadata["render_fps"] = self.render_spec.render_fps def __del__(self): @@ -470,4 +472,4 @@ def close(self): """ if self.renderer is not None: self.renderer.close() - super().close() \ No newline at end of file + super().close() diff --git a/gym/f110_gym/envs/rendering/__init__.py b/gym/f110_gym/envs/rendering/__init__.py index f1d8f41f..d6c500b5 100644 --- a/gym/f110_gym/envs/rendering/__init__.py +++ b/gym/f110_gym/envs/rendering/__init__.py @@ -5,17 +5,30 @@ from f110_gym.envs.track import Track -def make_renderer(track: Track, agent_ids: List[str], render_mode: str = None) -> Tuple[EnvRenderer, RenderSpec]: +def make_renderer( + track: Track, agent_ids: List[str], render_mode: str = None +) -> Tuple[EnvRenderer, RenderSpec]: from f110_gym.envs.rendering.rendering_pygame import PygameEnvRenderer + cfg_file = pathlib.Path(__file__).parent.absolute() / "rendering.yaml" render_spec = RenderSpec.from_yaml(cfg_file) if render_mode in ["human_fast"]: cfg_file = pathlib.Path(__file__).parent.absolute() / "rendering_fast.yaml" render_spec = RenderSpec.from_yaml(cfg_file) - renderer = PygameEnvRenderer(track=track, agent_ids=agent_ids, render_spec=render_spec, render_mode=render_mode) + renderer = PygameEnvRenderer( + track=track, + agent_ids=agent_ids, + render_spec=render_spec, + render_mode=render_mode, + ) elif render_mode in ["human", "rgb_array"]: - renderer = PygameEnvRenderer(track=track, agent_ids=agent_ids, render_spec=render_spec, render_mode=render_mode) + renderer = PygameEnvRenderer( + track=track, + agent_ids=agent_ids, + render_spec=render_spec, + render_mode=render_mode, + ) else: renderer = None return renderer, render_spec diff --git a/gym/f110_gym/envs/rendering/rendering_pygame.py b/gym/f110_gym/envs/rendering/rendering_pygame.py index 11d8f60f..25964583 100644 --- a/gym/f110_gym/envs/rendering/rendering_pygame.py +++ b/gym/f110_gym/envs/rendering/rendering_pygame.py @@ -54,7 +54,7 @@ def render(self, time: float, display: pygame.Surface): class Info: - def __init__(self, window_shape=(1000,1000)): + def __init__(self, window_shape=(1000, 1000)): font_size = int(32 * window_shape[0] / 1000) self.font = pygame.font.SysFont("Arial", font_size) self.text = self.font.render("", True, (125, 125, 125)) @@ -130,7 +130,6 @@ def render(self, display): front_right = (vertices[1] * lam + vertices[2] * (1 - lam)).astype(int) arrow_length = self.steering_arrow_len / self.resolution - for mid_point in [front_left, front_right]: end_point = mid_point + 0.5 * arrow_length * np.array( [np.cos(self.steering_angle), np.sin(self.steering_angle)] @@ -319,10 +318,10 @@ def render_points( pygame.draw.circle(self.map_canvas, color, point, size) def render_lines( - self, - points: Union[List, np.ndarray], - color: Tuple[int, int, int] = (0, 0, 255), - size: int = 1, + self, + points: Union[List, np.ndarray], + color: Tuple[int, int, int] = (0, 0, 255), + size: int = 1, ): """ Render a sequence of lines segments. @@ -337,7 +336,9 @@ def render_lines( points = ((points - origin[:2]) / resolution).astype(int) size = math.ceil(size / self.ppu) - pygame.draw.lines(self.map_canvas, color, closed=False, points=points, width=size) + pygame.draw.lines( + self.map_canvas, color, closed=False, points=points, width=size + ) def render_closed_lines( self, @@ -358,7 +359,9 @@ def render_closed_lines( points = ((points - origin[:2]) / resolution).astype(int) size = math.ceil(size / self.ppu) - pygame.draw.lines(self.map_canvas, color, closed=True, points=points, width=size) + pygame.draw.lines( + self.map_canvas, color, closed=True, points=points, width=size + ) def close(self): if self.render_mode == "human" or self.render_mode == "human_fast": diff --git a/gym/f110_gym/envs/rendering/rendering_pyglet.py b/gym/f110_gym/envs/rendering/rendering_pyglet.py index ab1837d5..05faea2a 100644 --- a/gym/f110_gym/envs/rendering/rendering_pyglet.py +++ b/gym/f110_gym/envs/rendering/rendering_pyglet.py @@ -55,7 +55,6 @@ class PygletEnvRenderer(pyglet.window.Window, EnvRenderer): A window class inherited from pyglet.window.Window, handles the camera/projection interaction, resizing window, and rendering the environment """ - def __init__(self, track: Track, render_spec: RenderSpec): """ Class constructor @@ -69,9 +68,7 @@ def __init__(self, track: Track, render_spec: RenderSpec): """ conf = Config(sample_buffers=1, samples=4, depth_size=16, double_buffer=True) width, height = render_spec.window_size, render_spec.window_size - super().__init__( - width, height, config=conf, resizable=True, vsync=False - ) + super().__init__(width, height, config=conf, resizable=True, vsync=False) # gl init glClearColor(9 / 255, 32 / 255, 87 / 255, 1.0) @@ -360,11 +357,11 @@ def update(self, state): self.score_label.text = ( "Lap Time: {laptime:.2f}, Ego Lap Count: {count:.0f}".format( - laptime=state["lap_times"][0], count=state["lap_counts"][state["ego_idx"]] + laptime=state["lap_times"][0], + count=state["lap_counts"][state["ego_idx"]], ) ) - def render_map(self): pass diff --git a/gym/f110_gym/test/benchmark_renderer.py b/gym/f110_gym/test/benchmark_renderer.py index 99543254..c32ebd32 100644 --- a/gym/f110_gym/test/benchmark_renderer.py +++ b/gym/f110_gym/test/benchmark_renderer.py @@ -36,8 +36,8 @@ def pretty_print(dict: dict, col_width=15): print("|" + "-" * col_width, end="") print("|") -class BenchmarkRenderer: +class BenchmarkRenderer: @staticmethod def _make_env(config={}, render_mode=None) -> F110Env: import gymnasium as gym @@ -95,7 +95,6 @@ def benchmark_single_agent_rendering(self): pretty_print(results) - def benchmark_n_agents_human_rendering(self): """ This is meant to benchmark the human rendering mode, for increasing nr of agents. @@ -135,7 +134,6 @@ def benchmark_n_agents_human_rendering(self): pretty_print(results) - def benchmark_callbacks_human_rendering(self): import time @@ -216,7 +214,7 @@ def render_position(self, e): pretty_print(results) -if __name__ == '__main__': +if __name__ == "__main__": benchmark = BenchmarkRenderer() benchmark.benchmark_single_agent_rendering()