Example #1
0
def show_grid_gradient(env, matrix_vals, colormap='Reds', tile_size=TILE_PIXELS, scale=(0, 10), alpha=0.75, title="My Visualization"):
    if not matrix_vals.shape == (env.width, env.height):
        raise ValueError("Expected matrix_vals shape {} but got {}".format((env.width, env.height), matrix_vals.shape))
    
    if scale:
        low, high = scale
    else:
        low, high = np.min(matrix_vals[1:-1, 1:-1]), np.max(matrix_vals[1:-1, 1:-1])

    img = env.grid.render(tile_size=TILE_PIXELS)
    matrix_vals = (matrix_vals - low) / (high - low)
    my_cmap = color.get_cmap(colormap)
    color_array = np.zeros((env.width, env.height, 4))
    color_array[1:-1, 1:-1, :] = my_cmap(matrix_vals[1:-1, 1:-1])

    for i in range(1, env.width-1):
        for j in range(1, env.height-1):
            ymin = j * tile_size
            ymax = (j+1) * tile_size
            xmin = i * tile_size
            xmax = (i+1) * tile_size
            tile = img[ymin:ymax, xmin:xmax, :]
            highligh_color = color_array[i, j, :-1] * 255
            highlight_img(tile, highligh_color, alpha=alpha)

    
    window = gym_minigrid.window.Window(title)
    window.show_img(img)
    window.show()
    return img
Example #2
0
    def render_tile(cls, obj, highlight=False, tile_size=TILE_PIXELS, subdivs=3):
        if obj is None:
            key = (
                tile_size,
                highlight,
            )
        else:
            key = (tile_size, highlight, *obj.encode())

        if key in cls.tile_cache:
            img = cls.tile_cache[key]
        else:
            img = np.zeros(
                shape=(tile_size * subdivs, tile_size * subdivs, 3), dtype=np.uint8
            )

            # Draw the grid lines (top and left edges)
            fill_coords(img, point_in_rect(0, 0.031, 0, 1), (100, 100, 100))
            fill_coords(img, point_in_rect(0, 1, 0, 0.031), (100, 100, 100))

            if obj != None:
                obj.render(img)

            if highlight:
                highlight_img(img)

            img = downsample(img, subdivs)

            cls.tile_cache[key] = img

        return img
Example #3
0
    def render_tile(
        cls,
        obj,
        highlight=None,
        tile_size=minigrid.TILE_PIXELS,
        subdivs=3,
        cell_type=None,
    ):
        """Render a tile and cache the result."""
        # Hash map lookup key for the cache
        if isinstance(highlight, list):
            key = (tuple(highlight), tile_size)
        else:
            key = (highlight, tile_size)
        key = obj.encode() + key if obj else key

        if key in cls.tile_cache:
            return cls.tile_cache[key]

        img = np.zeros(shape=(tile_size * subdivs, tile_size * subdivs, 3),
                       dtype=np.uint8)

        # Draw the grid lines (top and left edges)
        rendering.fill_coords(img, rendering.point_in_rect(0, 0.031, 0, 1),
                              (100, 100, 100))
        rendering.fill_coords(img, rendering.point_in_rect(0, 1, 0, 0.031),
                              (100, 100, 100))

        if obj is not None and obj.type != "agent":
            obj.render(img)

        # Highlight the cell if needed (do not highlight walls)
        if highlight and not (cell_type is not None and cell_type == "wall"):
            if isinstance(highlight, list):
                for a, agent_highlight in enumerate(highlight):
                    if agent_highlight:
                        rendering.highlight_img(img, color=AGENT_COLOURS[a])
            else:
                # Default highlighting for agent's partially observed views
                rendering.highlight_img(img)

        # Render agents after highlight to avoid highlighting agent triangle (the
        # combination of colours makes it difficult to ID agent)
        if obj is not None and obj.type == "agent":
            obj.render(img)

        # Downsample the image to perform supersampling/anti-aliasing
        img = rendering.downsample(img, subdivs)

        # Cache the rendered tile
        cls.tile_cache[key] = img

        return img