Skip to content
Snippets Groups Projects
Commit 55eb9c51 authored by hagrid67's avatar hagrid67
Browse files

added render show_debug to show agent index - issue 146

parent fc34b470
No related branches found
No related tags found
No related merge requests found
...@@ -6,10 +6,10 @@ Subpackages ...@@ -6,10 +6,10 @@ Subpackages
.. toctree:: .. toctree::
flatland.core flatland.core
flatland.envs flatland.envs
flatland.evaluators flatland.evaluators
flatland.utils flatland.utils
Submodules Submodules
---------- ----------
...@@ -18,15 +18,15 @@ flatland.cli module ...@@ -18,15 +18,15 @@ flatland.cli module
------------------- -------------------
.. automodule:: flatland.cli .. automodule:: flatland.cli
:members: :members:
:undoc-members: :undoc-members:
:show-inheritance: :show-inheritance:
Module contents Module contents
--------------- ---------------
.. automodule:: flatland .. automodule:: flatland
:members: :members:
:undoc-members: :undoc-members:
:show-inheritance: :show-inheritance:
...@@ -4,7 +4,7 @@ import time ...@@ -4,7 +4,7 @@ import time
import tkinter as tk import tkinter as tk
import numpy as np import numpy as np
from PIL import Image, ImageDraw, ImageTk # , ImageFont from PIL import Image, ImageDraw, ImageTk, ImageFont
from numpy import array from numpy import array
from pkg_resources import resource_string as resource_bytes from pkg_resources import resource_string as resource_bytes
...@@ -90,6 +90,8 @@ class PILGL(GraphicsLayer): ...@@ -90,6 +90,8 @@ class PILGL(GraphicsLayer):
self.old_background_image = (None, None, None) self.old_background_image = (None, None, None)
self.create_layers() self.create_layers()
self.font = ImageFont.load_default()
def build_background_map(self, dTargets): def build_background_map(self, dTargets):
x = self.old_background_image x = self.old_background_image
rebuild = False rebuild = False
...@@ -167,8 +169,14 @@ class PILGL(GraphicsLayer): ...@@ -167,8 +169,14 @@ class PILGL(GraphicsLayer):
# quit but not destroy! # quit but not destroy!
self.__class__.window.quit() self.__class__.window.quit()
def text(self, *args, **kwargs): def text(self, xPx, yPx, strText, layer=RAIL_LAYER):
pass xyPixLeftTop = (xPx, yPx)
self.draws[layer].text(xyPixLeftTop, strText, font=self.font, fill=(0, 0, 0, 255))
def text_rowcol(self, rcTopLeft, strText, layer=AGENT_LAYER):
print("Text:", "rc:", rcTopLeft, "text:", strText, "layer:", layer)
xyPixLeftTop = tuple((array(rcTopLeft) * self.nPixCell)[[1, 0]])
self.text(*xyPixLeftTop, strText, layer)
def prettify(self, *args, **kwargs): def prettify(self, *args, **kwargs):
pass pass
...@@ -492,13 +500,17 @@ class PILSVG(PILGL): ...@@ -492,13 +500,17 @@ class PILSVG(PILGL):
False)[0] False)[0]
self.draw_image_row_col(colored_rail, (row, col), layer=PILGL.PREDICTION_PATH_LAYER) self.draw_image_row_col(colored_rail, (row, col), layer=PILGL.PREDICTION_PATH_LAYER)
def set_rail_at(self, row, col, binary_trans, target=None, is_selected=False, rail_grid=None): def set_rail_at(self, row, col, binary_trans, target=None, is_selected=False, rail_grid=None,
show_debug=True):
if binary_trans in self.pil_rail: if binary_trans in self.pil_rail:
pil_track = self.pil_rail[binary_trans] pil_track = self.pil_rail[binary_trans]
if target is not None: if target is not None:
target_img = self.station_colors[target % len(self.station_colors)] target_img = self.station_colors[target % len(self.station_colors)]
target_img = Image.alpha_composite(pil_track, target_img) target_img = Image.alpha_composite(pil_track, target_img)
self.draw_image_row_col(target_img, (row, col), layer=PILGL.TARGET_LAYER) self.draw_image_row_col(target_img, (row, col), layer=PILGL.TARGET_LAYER)
if show_debug:
self.text_rowcol((row+0.8, col+0.0), strText=str(target), layer=PILGL.TARGET_LAYER)
if binary_trans == 0: if binary_trans == 0:
if self.background_grid[col][row] <= 4: if self.background_grid[col][row] <= 4:
...@@ -579,7 +591,7 @@ class PILSVG(PILGL): ...@@ -579,7 +591,7 @@ class PILSVG(PILGL):
for color_idx, pil_zug_3 in enumerate(pils): for color_idx, pil_zug_3 in enumerate(pils):
self.pil_zug[(in_direction_2, out_direction_2, color_idx)] = pils[color_idx] self.pil_zug[(in_direction_2, out_direction_2, color_idx)] = pils[color_idx]
def set_agent_at(self, agent_idx, row, col, in_direction, out_direction, is_selected): def set_agent_at(self, agent_idx, row, col, in_direction, out_direction, is_selected, show_debug=False):
delta_dir = (out_direction - in_direction) % 4 delta_dir = (out_direction - in_direction) % 4
color_idx = agent_idx % self.n_agent_colors color_idx = agent_idx % self.n_agent_colors
# when flipping direction at a dead end, use the "out_direction" direction. # when flipping direction at a dead end, use the "out_direction" direction.
...@@ -593,6 +605,10 @@ class PILSVG(PILGL): ...@@ -593,6 +605,10 @@ class PILSVG(PILGL):
self.clear_layer(PILGL.SELECTED_AGENT_LAYER, 0) self.clear_layer(PILGL.SELECTED_AGENT_LAYER, 0)
self.draw_image_row_col(bg_svg, (row, col), layer=PILGL.SELECTED_AGENT_LAYER) self.draw_image_row_col(bg_svg, (row, col), layer=PILGL.SELECTED_AGENT_LAYER)
if show_debug:
print("Call text:")
self.text_rowcol((row+0.2, col+0.2,), str(agent_idx))
def set_cell_occupied(self, agent_idx, row, col): def set_cell_occupied(self, agent_idx, row, col):
occupied_im = self.cell_occupied[agent_idx % len(self.cell_occupied)] occupied_im = self.cell_occupied[agent_idx % len(self.cell_occupied)]
self.draw_image_row_col(occupied_im, (row, col), 1) self.draw_image_row_col(occupied_im, (row, col), 1)
......
...@@ -39,7 +39,9 @@ class RenderTool(object): ...@@ -39,7 +39,9 @@ class RenderTool(object):
theta = np.linspace(0, np.pi / 2, 5) theta = np.linspace(0, np.pi / 2, 5)
arc = array([np.cos(theta), np.sin(theta)]).T # from [1,0] to [0,1] arc = array([np.cos(theta), np.sin(theta)]).T # from [1,0] to [0,1]
def __init__(self, env, gl="PILSVG", jupyter=False, agent_render_variant=AgentRenderVariant.ONE_STEP_BEHIND): def __init__(self, env, gl="PILSVG", jupyter=False,
agent_render_variant=AgentRenderVariant.ONE_STEP_BEHIND,
show_debug=True):
self.env = env self.env = env
self.frame_nr = 0 self.frame_nr = 0
self.start_time = time.time() self.start_time = time.time()
...@@ -56,6 +58,7 @@ class RenderTool(object): ...@@ -56,6 +58,7 @@ class RenderTool(object):
self.gl = PILSVG(env.width, env.height, jupyter) self.gl = PILSVG(env.width, env.height, jupyter)
self.new_rail = True self.new_rail = True
self.show_debug = show_debug
self.update_background() self.update_background()
def reset(self): def reset(self):
...@@ -282,7 +285,7 @@ class RenderTool(object): ...@@ -282,7 +285,7 @@ class RenderTool(object):
if len(observation_dict) < 1: if len(observation_dict) < 1:
warnings.warn( warnings.warn(
"Predictor did not provide any predicted cells to render. \ "Predictor did not provide any predicted cells to render. \
Observaiton builder needs to populate: env.dev_obs_dict") Observation builder needs to populate: env.dev_obs_dict")
else: else:
for agent in agent_handles: for agent in agent_handles:
color = self.gl.get_agent_color(agent) color = self.gl.get_agent_color(agent)
...@@ -525,7 +528,7 @@ class RenderTool(object): ...@@ -525,7 +528,7 @@ class RenderTool(object):
is_selected = False is_selected = False
self.gl.set_rail_at(r, c, transitions, target=target, is_selected=is_selected, self.gl.set_rail_at(r, c, transitions, target=target, is_selected=is_selected,
rail_grid=env.rail.grid) rail_grid=env.rail.grid, show_debug=self.show_debug)
self.gl.build_background_map(targets) self.gl.build_background_map(targets)
...@@ -550,7 +553,8 @@ class RenderTool(object): ...@@ -550,7 +553,8 @@ class RenderTool(object):
# set_agent_at uses the agent index for the color # set_agent_at uses the agent index for the color
if self.agent_render_variant == AgentRenderVariant.ONE_STEP_BEHIND_AND_BOX: if self.agent_render_variant == AgentRenderVariant.ONE_STEP_BEHIND_AND_BOX:
self.gl.set_cell_occupied(agent_idx, *(agent.position)) self.gl.set_cell_occupied(agent_idx, *(agent.position))
self.gl.set_agent_at(agent_idx, *position, old_direction, direction, selected_agent == agent_idx) self.gl.set_agent_at(agent_idx, *position, old_direction, direction,
selected_agent == agent_idx, show_debug=self.show_debug)
else: else:
position = agent.position position = agent.position
direction = agent.direction direction = agent.direction
...@@ -562,7 +566,7 @@ class RenderTool(object): ...@@ -562,7 +566,7 @@ class RenderTool(object):
# set_agent_at uses the agent index for the color # set_agent_at uses the agent index for the color
self.gl.set_agent_at(agent_idx, *position, agent.direction, direction, self.gl.set_agent_at(agent_idx, *position, agent.direction, direction,
selected_agent == agent_idx) selected_agent == agent_idx, show_debug=self.show_debug)
# set_agent_at uses the agent index for the color # set_agent_at uses the agent index for the color
if self.agent_render_variant == AgentRenderVariant.AGENT_SHOWS_OPTIONS_AND_BOX: if self.agent_render_variant == AgentRenderVariant.AGENT_SHOWS_OPTIONS_AND_BOX:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment