def vizarray(x, cmap=None, scale=None, vmin=None, vmax=None, block_size=None): """Visualize a NumPy array using ipythonblocks.""" if not (x.ndim == 2 or x.ndim == 1): raise TypeError('This function only works with 1 or 2 dimensional arrays') global _cmap, _scale, _vmin, _vmax, _block_size cmap = cmap if cmap is not None else _cmap scale = scale if scale is not None else _scale vmin = vmin if vmin is not None else _vmin vmax = vmax if vmax is not None else _vmax block_size = block_size if block_size is not None else _block_size base = x.base if x.base is not None else None data = x.copy() if scale: n = colors.Normalize(vmin=vmin, vmax=vmax) if base is not None: n.autoscale(base) data = n(data) if data.ndim == 1: rows = 1 cols = data.shape[0] bg = BlockGrid(cols, rows, block_size=block_size) for col in range(cols): bg[0,col] = _value_to_color(data[col], cmap) elif data.ndim == 2: rows = data.shape[0] cols = data.shape[1] bg = BlockGrid(cols, rows, block_size=block_size) for row in range(rows): for col in range(cols): bg[row, col] = _value_to_color(data[row, col], cmap) return bg
def __init__(self): ### initialize grid, agent, obstacles, etc., self.width = 6 self.height = 6 self.grid = BlockGrid(self.height, self.width, fill=(234, 123, 234)) # decide on obstacle and goal locations self.obstacles = [[1, 2], [3, 4], [2, 3], [2, 1]] # impenetrable obstacle locations self.goal = [4, 4] # goal block self.player = [1, 1] # initial location player # enumerate states based on obstacle locations self.states = [] for i in range(self.grid.height): for j in range(self.grid.width): block = [i, j] if block not in self.obstacles and block not in self.goal: self.states.append(str(i) + str(j)) # initialize Q^* matrix self.Q_star = np.zeros((self.grid.width**2 - len(self.obstacles), 5)) # initialize action choices self.action_choices = [[-1, 0], [0, -1], [1, 0], [0, 1], [0, 0]]
def draw_points(points, color=red): screen = BlockGrid(SCREEN_COLS, SCREEN_ROWS, fill=black) for p in points: r = p[0] c = p[1] if 0 <= r < screen.height and 0 <= c < screen.width: screen[r, c] = color return screen
def voirListeVoeu(voeuxClasses): nbVoeux = len(voeuxClasses) grille = BlockGrid(nbVoeux, 1, fill=(200, 200, 200)) for i, voeu in enumerate(voeuxClasses): typeCandidat = voeu["type"] couleur = couleursTypeCandidat[typeCandidat] grille[0, i].set_colors(*couleur) return grille
def __init__(self, width=5, height=5): super(CleanupPuzzleEnvironment, self).__init__() self.width = width self.height = height self.bg_color = (207, 216, 220) self.ball_color = (0, 191, 165) self.click_color = (244, 67, 54) self.grid = BlockGrid(width, height, fill=self.bg_color)
def swiss_flag(n_rows=10, n_cols=10): red = (255, 0, 0) white = (255, 255, 255) screen = BlockGrid(n_cols, n_rows, fill=(255, 0, 0)) screen.lines_on = False screen[2:8, 4:6] = white screen[4:6, 2:8] = white return screen
def show_as_blocks(self): block_size = 100 grid = BlockGrid(self._number, 1, block_size=block_size) for block, color in zip(grid, self._colors): block.rgb = color grid.show()
def boules_vers_grid(boules: Boules) -> BlockGrid: n = len(boules) grid = BlockGrid(n, 1, fill=(c_blanc)) for i, boule in enumerate(boules): if boule == BLEU: grid[0, i].set_colors(*c_bleu) elif boule == BLANC: grid[0, i].set_colors(*c_blanc) elif boule == ROUGE: grid[0, i].set_colors(*c_rouge) return grid
def chess_board(n_rows=8, n_cols=8, fg=None, bg=None): if not bg: bg = colors['black'] if not fg: fg = colors['yellow'] screen = BlockGrid(n_cols, n_rows, fill=bg) for row in range(n_rows): for col in range(n_cols): if (row + col) % 2 == 0: screen[row, col] = fg return screen
def __init__(self, width=10, height=10, boundary=True, color={}, display=False): """Define all the usual XYEnvironment characteristics, but initialise a BlockGrid for GUI too.""" super().__init__(width, height) self.grid = BlockGrid(width, height, fill=(200, 200, 200)) if display: self.grid.show() self.visible = True else: self.visible = False self.bounded = boundary self.colors = color
def draw_1(color=red): screen = BlockGrid(SCREEN_COLS, SCREEN_ROWS, fill=black) screen[1, 1] = color screen[0, 2] = color screen[1, 2] = color screen[2, 2] = color screen[3, 2] = color screen[4, 2] = color screen[5, 2] = color screen[6, 2] = color screen[6, 1] = color screen[6, 3] = color return screen
def show_selection(self, s_block): """Show a coloured table highlighting the selection""" grid = BlockGrid(self.ncols, self.nrows, fill=colors['LightGray'], block_size=15) if hasattr(s_block, '__array__'): # ipython blocks does not support boolean indexing rows, cols = np.nonzero(s_block) for row, col in zip(rows, cols): grid[int(row), int(col)] = colors['LightGreen'] else: grid[s_block] = colors['LightGreen'] return grid
def __init__(self, width=10, height=10, boundary=True, color={}, display=False): """Defina todas las características habituales del entorno XY, pero inicialice un BlockGrid para GUI también.""" super().__init__(width, height) self.grid = BlockGrid(width, height, fill=(200, 200, 200)) if display: self.grid.show() self.visible = True else: self.visible = False self.bounded = boundary self.colors = color
def dibujaTablero(self, fichasGanadoras=None): """Dibuja en formato grafico en ipython, el tablero que se le pasa, si se le pasa una lista con fichas ganadoras, las resalta en amarillo.""" grid = BlockGrid(width=self.colmax, height=self.filmax, block_size=25, lines_on=True) for fil in range(grid.height): for col in range(grid.width): if fichasGanadoras and [fil, col] in fichasGanadoras: grid[fil, col] = colors['Yellow'] continue if self.tablero[fil][col] == 1: grid[fil, col] = colors['Red'] elif self.tablero[fil][col] == -1: grid[fil, col] = colors['Green'] else: grid[fil, col] = colors['Black'] grid.show()
def show_as_blocks(self, block_size=100): """ Show colors in the IPython Notebook using ipythonblocks. Parameters ---------- block_size : int, optional Size of displayed blocks. """ from ipythonblocks import BlockGrid grid = BlockGrid(self.number, 1, block_size=block_size) for block, color in zip(grid, self.colors): block.rgb = color grid.show()
def color_grid(self): # remake + recolor grid self.grid = BlockGrid(self.width, self.height, fill=(234, 123, 234)) # color obstacles for i in range(len(self.obstacles)): self.grid[self.obstacles[i][0], self.obstacles[i][1]].red = 0 # make and color goal self.grid[self.goal[0], self.goal[1]].green = 255 self.grid[self.goal[0], self.goal[1]].red = 0 self.grid[self.goal[0], self.goal[1]].blue = 0 # color player location self.grid[self.player[0], self.player[1]].green = 0 self.grid[self.player[0], self.player[1]].red = 0 self.grid[self.player[0], self.player[1]].blue = 0 self.grid.show()
from PIL import Image im = Image.open('StarNight.jpg') im.size im = im.resize((125, 100), Image.ANTIALIAS) #Getdata() method gives an iterable sequence of RGB tuples starting at the top left image pixel and going down row by row imdata = im.getdata() #organize data structures import os import itertools with open('starry_night.txt', 'w') as f: s = [ '# width height', '{0} {1}'.format(im.size[0], im.size[1]), '# block size', '4', '# initial color', '0 0 0', '# row column red green blue' ] f.write(os.linesep.join(s) + os.linesep) for ((row, col), colors) in zip( itertools.product(range(im.size[1]), range(im.size[0])), imdata): things = [str(x) for x in (row, col) + colors] f.write(' '.join(things + ['\n'])) from ipythonblocks import BlockGrid grid = BlockGrid(125, 100, block_size=4, lines_on=False) for block, colors in zip(grid, imdata): block.rgb = colors grid
def pBlockGrid2(df, fill=(123, 234, 123), *args, **kwargs): (y, x) = df.shape b = BlockGrid(x, y, fill=fill, **kwargs) return b
def pBlockGrid(df): (y, x) = df.shape return BlockGrid(x, y)
from ipythonblocks import BlockGrid grid = BlockGrid(10, 10, fill=(123, 234, 123)) grid
import time SCREEN_ROWS = 7 SCREEN_COLS = 5 colors = { 'black': (0, 0, 0), 'white': (255, 255, 255), 'red': (255, 0, 0), 'yellow': (255, 255, 0), } black = colors['black'] red = colors['red'] screen = BlockGrid(SCREEN_COLS, SCREEN_ROWS, fill=black) def erase(screen, color=black): screen[:, :] = color def draw_1(color=red): screen = BlockGrid(SCREEN_COLS, SCREEN_ROWS, fill=black) screen[1, 1] = color screen[0, 2] = color screen[1, 2] = color screen[2, 2] = color screen[3, 2] = color screen[4, 2] = color screen[5, 2] = color
avg + (new / 100.0) for avg, new in zip(average_landings, board) ] average_landings = [avg / 10.0 for avg in average_landings] print('Probability (%) of landing on...') print('Reading Railroad:\t', average_landings[reading_rr]) print('Pennsylvania Railroad:\t', average_landings[pennsylvania_rr]) print('B. & O. Railroad:\t', average_landings[bo_rr]) print('Short Line Railroad:\t', average_landings[shortline_rr]) print('GO:\t\t\t', average_landings[go]) print('Mediterranean Avenue:\t', average_landings[mediterranean]) print('Boardwalk:\t\t', average_landings[boardwalk]) width = 11 height = 11 grid = BlockGrid(width=width, height=height, fill=(209, 194, 111)) # Set up palette tups = [(elt, p) for elt, p in zip(range(0, board_squares), average_landings)] tups.sort(key=lambda x: x[1], reverse=True) ranks = [(rank, elt[0], elt[1]) for rank, elt in zip(range(0, board_squares), tups)] palette = [0] * board_squares for i in range(0, board_squares): idx = ranks[i][1] palette[idx] = ranks[i][0] * 6.25 # Paint edges # Top row for i in range(0, width): colour = palette[20 + i]