from ipythonblocks import ImageGrid grid = ImageGrid(5, 3) grid.show() print 'grid width:', grid.width print 'grid height:', grid.height print 'grid lines on:', grid.lines_on position = (12.3, 45.6) print 'position is:', position color = (10, 20, 30) print 'color is:', color print 'first element of color is:', color[0] color[0] = 40 print 'first element of color after change:', color[0] row = ImageGrid(8, 1) row[0, 0] = (0, 0, 0) # no color => black row[1, 0] = (255, 255, 255) # all colors => white row[2, 0] = (255, 0, 0) # all red row[3, 0] = (0, 255, 0) # all green row[4, 0] = (0, 0, 255) # all blue row[5, 0] = (255, 255, 0) # red and green row[6, 0] = (255, 0, 255) # red and blue row[7, 0] = (0, 255, 255) # green and blue row.show() from ipythonblocks import show_color show_color(214, 90, 127) from ipythonblocks import colors c = ImageGrid(3, 2) c[0, 0] = colors['Fuchsia'] c[0, 1] = colors['Salmon'] c[1, 0] = colors['Orchid'] c[1, 1] = colors['Lavender'] c[2, 0] = colors['LimeGreen'] c[2, 1] = colors['HotPink'] c.show() num = 37 if num > 100: print 'greater' else: print 'not greater' print 'done' num = 53 print 'before conditional...' if num > 100: print '53 is greater than 100' print '...after conditional' def sign(num): if num > 0: return 1 elif num == 0: return 0 else: return -1 print 'sign of -3:', sign(-3) if (1 > 0) and (-1 > 0): print 'both parts are true' else: print 'one part is not true' if (1 < 0) or ('left' < 'right'): print 'at least one test is true' numbers = [-5, 3, 2, -1, 9, 6] total = 0 for n in numbers: if n >= 0: total = total + n print 'sum of positive values:', total pos_total = 0 neg_total = 0 for n in numbers: if n >= 0: pos_total = pos_total + n else: neg_total = neg_total + n print 'negative and positive sums are:', neg_total, pos_total for consonant in 'bcd': for vowel in 'ae': print consonant + vowel square = ImageGrid(5, 5) for x in range(square.width): for y in range(square.height): if x < y: square[x, y] = colors['Fuchsia'] elif x == y: square[x, y] = colors['Olive'] else: square[x, y] = colors['SlateGray'] square.show() import numpy as np data = np.loadtxt(fname='inflammation-01.csv', delimiter=',') print 'data shape:', data.shape width, height = data.shape heatmap = ImageGrid(width, height) for x in range(width): for y in range(height): if data[x, y] < data.mean(): heatmap[x, y] = colors['Red'] elif data[x, y] == data.mean(): heatmap[x, y] = colors['Green'] else: heatmap[x, y] = colors['Blue'] heatmap.show() flipped = data.transpose() width, height = flipped.shape heatmap = ImageGrid(width, height, block_size=5) center = flipped.mean() for x in range(width): for y in range(height): if flipped[x, y] < (0.8 * center): heatmap[x, y] = colors['Orchid'] elif flipped[x, y] > (1.2 * center): heatmap[x, y] = colors['HotPink'] else: heatmap[x, y] = colors['Fuchsia'] heatmap.show() def make_heatmap(values, low_color, mid_color, high_color, low_band, high_band, block_size): '''Make a 3-colored heatmap from a 2D array of data.''' width, height = values.shape result = ImageGrid(width, height, block_size=block_size) center = values.mean() for x in range(width): for y in range(height): if values[x, y] < low_band * center: result[x, y] = low_color elif values[x, y] > high_band * center: result[x, y] = high_color else: result[x, y] = mid_color return result h = make_heatmap(flipped, colors['Orchid'], colors['Fuchsia'], colors['HotPink'], 0.8, 1.2, 5) h.show() h = make_heatmap(flipped, colors['Gray'], colors['YellowGreen'], colors['SpringGreen'], 0.5, 1.5, 5) h.show() def make_heatmap(values, low_band=0.5, high_band=1.5, low_color=colors['Gray'], mid_color=colors['YellowGreen'], high_color=colors['SpringGreen'], block_size=5): '''Make a 3-colored heatmap from a 2D array of data. Default color scheme is gray to green.''' width, height = values.shape result = ImageGrid(width, height, block_size=block_size) center = values.mean() for x in range(width): for y in range(height): if values[x, y] < low_band * center: result[x, y] = low_color elif values[x, y] > high_band * center: result[x, y] = high_color else: result[x, y] = mid_color return result h = make_heatmap(flipped, 0.5, 1.5, colors['Gray'], colors['YellowGreen'], colors['SpringGreen'], 5) h.show() h = make_heatmap(flipped, 0.4, 1.6) h.show()