def update_figure(spikes): logging.debug("Updating figure") global window, frame, ax, fig, bar, stim # draw in matplotlib ax.cla() ax.hist(spikes, bins=np.linspace(-0.1, 0.5, 25), color='k') ax.vlines(0, 0, ax.get_ylim()[1], color='b') ax.set_title("%s" % str(stim)) fig.canvas.draw() buffer = fig.canvas.buffer_rgba(0, 0) x, y, w, h = fig.bbox.bounds F = np.fromstring(buffer, np.uint8).copy() F.shape = h, w, 4 # Replace 'transparent' color with a real transparent color v = np.array(np.array([1, 2, 3, 255]), dtype=np.uint8).view(dtype=np.int32)[0] Ft = np.where(F.view(dtype=np.int32) == v, 0, F) # Create main frame frame = glumpy.Image(Ft, interpolation='nearest') frame.update() bar.update() glut.glutPostRedisplay()
def imshow(X, cmap=None, norm=None, aspect=None, interpolation=None, alpha=1.0, vmin=None, vmax=None, origin=None, extent=None, **kwargs): '''pylab imshow proxy function. This function first call the pylab original imshow function using an alpha level of 0 (fully transparent). It then set axes background with a dedicated 'transparent' color that will be replaced later by a truly transparent color. Warning: any pixel with 'transparent' color will be replaced by a transparent pixel. If this causes problem, you will have to change the definition of 'transparent' to something else. ''' axis = plt.imshow(X, cmap=cmap, norm=norm, aspect=aspect, interpolation='nearest', alpha=0, vmin=vmin, vmax=vmax, origin=origin, extent=extent, **kwargs) r, g, b, a = _transparent / 255.0 cmap = axis.cmap under = cmap(-1.0) over = cmap(2.0) axis.cmap.set_over((r, g, b), alpha=0) axis.cmap.set_under((r, g, b), alpha=0) axis.cmap.set_bad((r, g, b), alpha=0) axes = axis.get_axes() axes.patch.set_facecolor((r, g, b)) axes.patch.set_alpha(a) # Build glumpy colormap from matplotlib colormap colors = [] for i in range(510): colors.append((i / 510.0, cmap(i / 510.0, alpha=alpha))) cmap = glumpy.colormap.Colormap(*colors, over=glumpy.Color(over), under=glumpy.Color(under)) image = glumpy.Image(X, interpolation=interpolation, cmap=cmap, vmin=vmin, vmax=vmax) image.update() _items.append([image, axis, alpha]) return axis
def on_key_press(symbol, modifiers): if chr(symbol) not in commands: print 'unused key', chr(symbol), modifiers return pos = state['pos'] commands[chr(symbol)](state) if pos == state['pos']: return else: img_i = img_array[state['pos']] if contrast_norm == 'each': # -- force copy img_i = np.array(img_i, 'float32') img_i -= img_i.min() img_i /= max(img_i.max(), 1e-12) #print img_i.shape #print img_i.dtype #print img_i.max() #print img_i.min() state['I'] = glumpy.Image(img_i, colormap=cmap, vmin=0.0, vmax=1.0 ) print state['pos'], [o[state['pos']] for o in arrays_to_print] fig.redraw()
def glumpy_viewer( img_array, arrays_to_print=[], commands=None, cmap=glumpy.colormap.IceAndFire, window_shape=(512, 512), ): """ Setup and start glumpy main loop to visualize Image array `img_array`. img_array - an array-like object whose elements are float32 or uint8 ndarrays that glumpy can show. larray objects work here. arrays_to_print - arrays whose elements will be printed to stdout after a keypress changes the current position. """ try: n_imgs = len(img_array) except TypeError: n_imgs = None state = dict(window=glumpy.Window(*window_shape), pos=0, I=glumpy.Image(img_array[0], cmap=cmap), len=n_imgs) window = state['window'] # put in scope of handlers for convenience if commands is None: commands = _commands @window.event def on_draw(): window.clear() state['I'].blit(0, 0, window.width, window.height) @window.event def on_key_press(symbol, modifiers): if chr(symbol) not in commands: print 'unused key', chr(symbol), modifiers return pos = state['pos'] commands[chr(symbol)](state) if pos == state['pos']: return else: img_i = img_array[state['pos']] #print img_i.shape #print img_i.dtype #print img_i.max() #print img_i.min() state['I'] = glumpy.Image(img_array[state['pos']], cmap=cmap) print state['pos'], [o[state['pos']] for o in arrays_to_print] window.draw() window.mainloop()
def render_image(self, frame): if use_glumpy_for_blit: I = glumpy.Image(self.im_array, interpolation='bilinear', cmap=glumpy.colormap.Grey) I.blit(-1, -1, 2, 2) return else: # old way... self.texture = glGenTextures(1) glColor4f(1., 1., 1., 1.) glBindTexture(GL_TEXTURE_2D, self.texture) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR) glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, self.im_array.shape[1], self.im_array.shape[0], 0, GL_LUMINANCE, # Recast im_array from camera as uint8 regardless for GUI display: GL_UNSIGNED_BYTE, #self.im_array.astype(uint8), #GL_UNSIGNED_SHORT, self.im_array.astype(uint16), ) # print self.im_array #print "imarray: " + str(self.im_array.shape) glBindTexture(GL_TEXTURE_2D, self.texture) glBegin(GL_QUADS) glTexCoord2f(0., 0.) glVertex3f(-1., 1., 0.) # Bottom Left glTexCoord2f(1., 0.) glVertex3f(1., 1., 0.) # Bottom Right glTexCoord2f(1., 1.) glVertex3f(1., -1., 0.) # Top Right glTexCoord2f(0., 1.) glVertex3f(-1., -1., 0.) # Top Left glEnd() glBindTexture(GL_TEXTURE_2D, 0) glDeleteTextures(self.texture)
def on_key_press(symbol, modifiers): if chr(symbol) not in commands: print 'unused key', chr(symbol), modifiers return pos = state['pos'] commands[chr(symbol)](state) if pos == state['pos']: return else: img_i = img_array[state['pos']] #print img_i.shape #print img_i.dtype #print img_i.max() #print img_i.min() state['I'] = glumpy.Image(img_array[state['pos']], cmap=cmap) print state['pos'], [o[state['pos']] for o in arrays_to_print] window.draw()
def on_resize(width, height): global frame fig = plt.gcf() dpi = float(fig.dpi) winch = width / dpi hinch = height / dpi fig.set_size_inches(winch, hinch) fig.canvas.draw() buffer = fig.canvas.buffer_rgba(0, 0) x, y, w, h = fig.bbox.bounds F = numpy.fromstring(buffer, numpy.uint8).copy() F.shape = h, w, 4 # Replace 'transparent' color with a real transparent color v = numpy.array(_transparent, dtype=numpy.uint8).view(dtype=numpy.int32)[0] Ft = numpy.where(F.view(dtype=numpy.int32) == v, 0, F) # Create main frame frame = glumpy.Image(Ft, interpolation=interpolation) frame.update()
#----------------------------------------------------------------------------- # Copyright (C) 2009-2010 Nicolas P. Rougier # # Distributed under the terms of the BSD License. The full license is in # the file COPYING, distributed as part of this software. #----------------------------------------------------------------------------- import numpy, glumpy import OpenGL.GL as gl n = 512 Z = numpy.random.randint(0, 2, (n, n)).astype(numpy.float32) window = glumpy.Window(512, 512) viewport = [0, 0, 1] Zi = glumpy.Image(Z, interpolation='nearest', cmap=glumpy.colormap.Grey, vmin=1, vmax=0) gl.glEnable(gl.GL_BLEND) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) @window.event def on_mouse_motion(x, y, dx, dy): zoom = viewport[2] x = x / float(window.width) y = y / float(window.height) x = min(max(x, 0), 1) y = min(max(y, 0), 1) viewport[0] = x * window.width * (1 - zoom) viewport[1] = y * window.height * (1 - zoom)
label_data = label_data[1:] count = rows * columns pixels = struct.unpack('%dB' % count, image_data[:count]) image_data = image_data[count:] yield label, numpy.array(pixels).astype(float).reshape((rows, columns)) if __name__ == '__main__': import sys import glumpy # pass in label_file, then image_file iterator = iterimages(sys.argv[1], sys.argv[2], False) composites = [numpy.zeros((28, 28), 'f') for _ in range(10)] images = [glumpy.Image(c) for c in composites] win = glumpy.Window(800, 600) @win.event def on_draw(): win.clear() w, h = win.get_size() for i, image in enumerate(images): image.blit(w * (i % 5) / 5., h * (i // 5) / 2., w / 5., h / 2.) @win.event def on_idle(dt): try: label, pixels = iterator.next() except StopIteration:
def glumpy_viewer(img_array, arrays_to_print = [], commands=None, cmap=None, window_shape=(512, 512), contrast_norm=None ): """ Setup and start glumpy main loop to visualize Image array `img_array`. img_array - an array-like object whose elements are float32 or uint8 ndarrays that glumpy can show. larray objects work here. arrays_to_print - arrays whose elements will be printed to stdout after a keypress changes the current position. """ if contrast_norm not in (None, 'each', 'all'): raise ValueError('contrast_norm', contrast_norm) if contrast_norm == 'all': np.array(img_array, 'float32') img_array -= img_array.min() img_array /= max(img_array.max(), 1e-12) try: n_imgs = len(img_array) except TypeError: n_imgs = None state = dict( pos=0, fig=glumpy.figure((window_shape[1], window_shape[0])), I=glumpy.Image(img_array[0], colormap=cmap), len=n_imgs ) fig = state['fig'] if commands is None: commands = _commands @fig.event def on_draw(): fig.clear() state['I'].draw(x=0, y=0, z=0, width=fig.width, height=fig.height) @fig.event def on_key_press(symbol, modifiers): if chr(symbol) not in commands: print 'unused key', chr(symbol), modifiers return pos = state['pos'] commands[chr(symbol)](state) if pos == state['pos']: return else: img_i = img_array[state['pos']] if contrast_norm == 'each': # -- force copy img_i = np.array(img_i, 'float32') img_i -= img_i.min() img_i /= max(img_i.max(), 1e-12) #print img_i.shape #print img_i.dtype #print img_i.max() #print img_i.min() state['I'] = glumpy.Image(img_i, colormap=cmap, vmin=0.0, vmax=1.0 ) print state['pos'], [o[state['pos']] for o in arrays_to_print] fig.redraw() glumpy.show()
if __name__ == '__main__': window = glumpy.Window(900, 600) trackball = glumpy.Trackball(60, 30, 0.85) mesh = Mesh(64) def func3(x, y): return (1 - x / 2 + x**5 + y**3) * numpy.exp(-x**2 - y**2) dx, dy = .05, .05 x = numpy.arange(-3.0, 3.0, dx, dtype=numpy.float32) y = numpy.arange(-3.0, 3.0, dy, dtype=numpy.float32) Z = func3(*numpy.meshgrid(x, y)) I = glumpy.Image(Z, interpolation='bicubic', cmap=glumpy.colormap.Hot, lighted=True, gridsize=(31.0, 31.0, 0.0), elevation=0.5) def draw_background(): viewport = gl.glGetIntegerv(gl.GL_VIEWPORT) gl.glDisable(gl.GL_LIGHTING) gl.glDisable(gl.GL_DEPTH_TEST) gl.glPolygonMode(gl.GL_FRONT_AND_BACK, gl.GL_FILL) gl.glBegin(gl.GL_QUADS) #gl.glColor(0.75,0.75,1.0) gl.glColor(1.0, 1.0, 0.75) gl.glVertex(0, 0, -1) gl.glVertex(viewport[2], 0, -1) gl.glColor(1.0, 1.0, 1.0) gl.glVertex(viewport[2], viewport[3], 0)
#!/usr/bin/env python # -*- coding: utf-8 -*- #----------------------------------------------------------------------------- # Copyright (C) 2009-2010 Nicolas P. Rougier # # Distributed under the terms of the BSD License. The full license is in # the file COPYING, distributed as part of this software. #----------------------------------------------------------------------------- import sys import numpy, glumpy from OpenGL.GL import * window = glumpy.Window(512, 512) Z = numpy.random.random((32, 32)).astype(numpy.float32) _I = glumpy.Image(Z, interpolation='nearest', cmap=glumpy.colormap.Grey) @window.timer(30.0) def draw(dt): window.clear() _I.update() _I.blit(0, 0, window.width, window.height) window.draw() @window.event def on_key_press(key, modifiers): if key == glumpy.key.ESCAPE: sys.exit()
gl.glEnd() # ----------------------------------------------------------------------------- if __name__ == '__main__': image = Image.open('lena.png') S = numpy.asarray(image, dtype=numpy.float32)/256. # Visual scene R = numpy.zeros((256,256,3),dtype=numpy.float32) V = numpy.zeros((256,256,3),dtype=numpy.float32) Px,Py = retinotopy(R.shape[:2],V.shape[:2]) X,Y = 0,0 window = glumpy.Window(S.shape[1]+2*V.shape[1], max(S.shape[0],2*V.shape[0])) Si = glumpy.Image(S, cmap=glumpy.colormap.Grey) Vi = glumpy.Image(V, cmap=glumpy.colormap.Grey) gl.glBlendFunc (gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) gl.glEnable(gl.GL_BLEND) @window.event def on_mouse_motion(x, y, dx, dy): global X,Y X = min(max(x-V.shape[1]//2,1), S.shape[1]-V.shape[1]) Y = min(max((window.height-y)-V.shape[0]//2,1), S.shape[0]-V.shape[0]) r,g,b = S[Y,X] S[Y,X] = 0 V[...] = S[Y+Px,X+Py] S[Y,X] = r,g,b window.draw()
#!/usr/bin/env python # -*- coding: utf-8 -*- #----------------------------------------------------------------------------- # Copyright (C) 2009-2010 Nicolas P. Rougier # # Distributed under the terms of the BSD License. The full license is in # the file COPYING, distributed as part of this software. #----------------------------------------------------------------------------- import numpy, glumpy import OpenGL.GL as gl window = glumpy.Window(512, 512) A = glumpy.Image(numpy.random.random((100, 100)).astype(numpy.float32), cmap=glumpy.colormap.Grey) B = glumpy.Image(numpy.random.random((50, 50)).astype(numpy.float32), cmap=glumpy.colormap.Grey) C = glumpy.Image(numpy.random.random((30, 30)).astype(numpy.float32), cmap=glumpy.colormap.Grey) shape, items = glumpy.layout([[(A, 1.05), '-'], [(C, 5 / 3.), B]], padding=5, border=5) window.set_size(int(shape[0] * 600), int(shape[1] * 600)) @window.event def on_draw(): window.clear() gl.glColor4f(1, 1, 1, 1) for item in items:
dt = 0.1 diff = 0.0 visc = 0.0 force = 1 source = 25.0 u = numpy.zeros((size,size), dtype=numpy.float32) u_ = numpy.zeros((size,size), dtype=numpy.float32) v = numpy.zeros((size,size), dtype=numpy.float32) v_ = numpy.zeros((size,size), dtype=numpy.float32) dens = numpy.zeros((size,size), dtype=numpy.float32) dens_ = numpy.zeros((size,size), dtype=numpy.float32) Z = numpy.zeros((N,N),dtype=numpy.float32) cmap = glumpy.colormap.Colormap("BlueGrey", (0., (0.,0.,0.)), (1., (0.75,0.75,1.00))) I = glumpy.Image(Z, interpolation='bicubic', cmap=cmap, vmin=0, vmax=5) t, t0, frames = 0,0,0 window = glumpy.Window(800,800) window.last_drag = None @window.event def on_mouse_drag(x, y, dx, dy, button): window.last_drag = x,y,dx,dy,button @window.event def on_mouse_motion(x, y, dx, dy): window.last_drag = x,y,dx,dy,0 @window.event def on_key_press(key, modifiers):
if __name__ == '__main__': logging.basicConfig( stream=sys.stdout, level=logging.DEBUG, format='%(levelname).1s %(asctime)s [%(module)s:%(lineno)d] %(message)s' ) opts, args = FLAGS.parse_args() visibles = numpy.zeros((opts.rows // 2, 28, 28), 'f') hiddens = numpy.zeros((opts.rows // 2, opts.rows, opts.cols), 'f') weights = numpy.zeros((opts.rows, opts.cols, 28, 28), 'f') m = opts.binary and 1.5 or 0.5 kwargs = dict(cmap=glumpy.colormap.Grey, vmin=0, vmax=255) _visibles = [glumpy.Image(v, **kwargs) for v in visibles] _hiddens = [glumpy.Image(h) for h in hiddens] _weights = [[glumpy.Image(w, vmin=-m, vmax=m) for w in ws] for ws in weights] W = 100 * (opts.cols + 1) + 4 H = 100 * opts.rows + 4 win = glumpy.Window(W, H) loaded = False updates = -1 batches = 0. recent = collections.deque(maxlen=20) errors = [collections.deque(maxlen=20) for _ in range(10)] testset = [None] * 10
#!/usr/bin/env python # -*- coding: utf-8 -*- #----------------------------------------------------------------------------- # Copyright (C) 2009-2010 Nicolas P. Rougier # # Distributed under the terms of the BSD License. The full license is in # the file COPYING, distributed as part of this software. #----------------------------------------------------------------------------- import numpy, glumpy window = glumpy.Window(512, 512) Z = numpy.random.random((128, 128)).astype(numpy.float32) I = glumpy.Image(Z, interpolation='bicubic', cmap=glumpy.colormap.Grey) viewport = [0, 0, 1] @window.event def on_mouse_motion(x, y, dx, dy): zoom = viewport[2] x = x / float(window.width) y = y / float(window.height) x = min(max(x, 0), 1) y = min(max(y, 0), 1) viewport[0] = x * window.width * (1 - zoom) viewport[1] = y * window.height * (1 - zoom) window.draw() @window.event def on_mouse_scroll(x, y, scroll_x, scroll_y): zoom = viewport[2]
foxData.append(len(foxes)) # mp.imshow(z) # mp.gray() # mp.show() # plt.imshow(z) # plt.gray() # plt.show() ################################################################### import glumpy import numpy as np # Generate some data... x, y = np.meshgrid(np.linspace(-2, 2, 200), np.linspace(-2, 2, 200)) x, y = x - x.mean(), y - y.mean() z = x * np.exp(-x**2 - y**2) window = glumpy.Window(512, 512) im = glumpy.Image(z.astype(np.float32), cmap=glumpy.colormap.Grey) @window.event def on_draw(): im.blit(0, 0, window.width, window.height) window.mainloop()
# return (1-x/2+x**5+y**3)*numpy.exp(-x**2-y**2) # dx, dy = .01, .01 # x = numpy.arange(-3.0, 3.0, dx, dtype=numpy.float32) # y = numpy.arange(-3.0, 3.0, dy, dtype=numpy.float32) # Z = func3(*numpy.meshgrid(x, y)) n = 64.0 X = numpy.empty((n, n), dtype=numpy.float32) X.flat = numpy.arange(n) * 2 * numpy.pi / n * 2 Y = numpy.empty((n, n), dtype=numpy.float32) Y.flat = numpy.arange(n) * 2 * numpy.pi / n * 2 Y = numpy.transpose(Y) Z = numpy.sin(X) + numpy.cos(Y) I = glumpy.Image(Z, interpolation='bilinear', cmap=glumpy.colormap.Hot, gridsize=(31.0, 31.0, 10.0), elevation=0.25) def draw_background(): viewport = gl.glGetIntegerv(gl.GL_VIEWPORT) gl.glDisable(gl.GL_LIGHTING) gl.glDisable(gl.GL_DEPTH_TEST) gl.glPolygonMode(gl.GL_FRONT_AND_BACK, gl.GL_FILL) gl.glBegin(gl.GL_QUADS) #gl.glColor(0.75,0.75,1.0) gl.glColor(1.0, 1.0, 0.75) gl.glVertex(0, 0, -1) gl.glVertex(viewport[2], 0, -1) gl.glColor(1.0, 1.0, 1.0) gl.glVertex(viewport[2], viewport[3], 0)
def func3(x, y): return (1 - x / 2 + x**5 + y**3) * numpy.exp(-x**2 - y**2) dx, dy = .01, .01 x = numpy.arange(-3.0, 3.0, dx, dtype=numpy.float32) y = numpy.arange(-3.0, 3.0, dy, dtype=numpy.float32) X, Y = numpy.meshgrid(x, y) Z = func3(X, Y) Zc = Z.copy() t0, frames, t = 0, 0, 0 window = glumpy.Window(512, 512) I = glumpy.Image(Zc, interpolation='bicubic', cmap=glumpy.colormap.IceAndFire, vmin=-0.5, vmax=1.0) @window.event def on_draw(): window.clear() I.blit(0, 0, 512, 512) @window.event def on_idle(dt): global t, t0, frames t += dt
SparseConnection(Z('U'), Z('Lu'), K, toric=True) SparseConnection(Z('V'), Z('Lv'), K, toric=True) Z['u'] = 1.0 Z['v'] = 0.0 r = 20 Z['u'][n / 2 - r:n / 2 + r, n / 2 - r:n / 2 + r] = 0.50 Z['v'][n / 2 - r:n / 2 + r, n / 2 - r:n / 2 + r] = 0.25 Z['u'] += 0.05 * np.random.random((n, n)) Z['v'] += 0.05 * np.random.random((n, n)) Z['U'] = Z['u'] Z['V'] = Z['v'] cmap = glumpy.colormap.Colormap("blue", (0.00, (0.2, 0.2, 1.0)), (1.00, (1.0, 1.0, 1.0))) Zu = glumpy.Image(Z['u'], interpolation='bicubic', cmap=cmap) window = glumpy.Window(512, 512) @window.event def on_mouse_drag(x, y, dx, dy, button): global Z, n center = (int((1 - y / float(window.width)) * (n - 1)), int(x / float(window.height) * (n - 1))) def distance(x, y): return np.sqrt((x - center[0])**2 + (y - center[1])**2) D = np.fromfunction(distance, (n, n)) M = np.where(D <= 5, True, False).astype(np.float32) Z['U'] = Z['u'] = (1 - M) * Z['u'] + M * 0.50
yield label, np.array(pixels).astype(float).reshape((rows, columns)) if __name__ == '__main__': import sys import glumpy iterator = iterimages(sys.argv[1], sys.argv[2], False) composites = [np.zeros((28, 28), 'f') for _ in range(10)] fig = glumpy.Figure() images_and_frames = [] for i, c in enumerate(composites): frame = fig.add_figure(rows=2, cols=5, position=divmod(i, 2)).add_frame(aspect=1) images_and_frames.append((glumpy.Image(c), frame)) @fig.event def on_draw(): fig.clear() for image, frame in images_and_frames: image.update() frame.draw(x=frame.x, y=frame.y) image.draw(x=frame.x, y=frame.y, z=0, width=frame.width, height=frame.height) @fig.event def on_idle(dt):
#!/usr/bin/env python # -*- coding: utf-8 -*- #----------------------------------------------------------------------------- # Copyright (C) 2009-2010 Nicolas P. Rougier # # Distributed under the terms of the BSD License. The full license is in # the file COPYING, distributed as part of this software. #----------------------------------------------------------------------------- from PIL import Image import numpy, glumpy window = glumpy.Window(512, 512) Z = numpy.asarray(Image.open('lena.png')) n = 12 L = glumpy.Image(Z, interpolation='bicubic') N = glumpy.Image(numpy.ones((n, n, 3), dtype=numpy.uint8), interpolation='nearest') BL = glumpy.Image(numpy.zeros((n, n, 3), dtype=numpy.uint8), interpolation='bilinear') BC = glumpy.Image(numpy.zeros((n, n, 3), dtype=numpy.uint8), interpolation='bicubic') z = 512 / (3.0 * n) shape, items = glumpy.layout([[L, '-', '-'], [(N, z), (BL, z), (BC, z)]], padding=0, border=0) window.set_size(int(shape[0] * 600), int(shape[1] * 600)) @window.event def on_mouse_motion(x, y, dx, dy):
def show(interpolation='nearest'): ''' pylab show proxy function. ''' global frame # Draw current figure to rgba buffer fig = plt.gcf() fig.canvas.draw() buffer = fig.canvas.buffer_rgba(0, 0) x, y, w, h = fig.bbox.bounds F = numpy.fromstring(buffer, numpy.uint8).copy() F.shape = h, w, 4 # Replace 'transparent' color with a real transparent color v = numpy.array(_transparent, dtype=numpy.uint8).view(dtype=numpy.int32)[0] Ft = numpy.where(F.view(dtype=numpy.int32) == v, 0, F) # Create main frame window = glumpy.Window(512, 512) #Ft.shape[1], Ft.shape[0]) frame = glumpy.Image(Ft, interpolation=interpolation) frame.update() gl.glEnable(gl.GL_BLEND) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) @window.event def on_resize(width, height): global frame fig = plt.gcf() dpi = float(fig.dpi) winch = width / dpi hinch = height / dpi fig.set_size_inches(winch, hinch) fig.canvas.draw() buffer = fig.canvas.buffer_rgba(0, 0) x, y, w, h = fig.bbox.bounds F = numpy.fromstring(buffer, numpy.uint8).copy() F.shape = h, w, 4 # Replace 'transparent' color with a real transparent color v = numpy.array(_transparent, dtype=numpy.uint8).view(dtype=numpy.int32)[0] Ft = numpy.where(F.view(dtype=numpy.int32) == v, 0, F) # Create main frame frame = glumpy.Image(Ft, interpolation=interpolation) frame.update() @window.event def on_draw(): window.clear() for image, axis, alpha in _items: x, y, w, h = axis.get_axes().bbox.bounds x /= float(frame.shape[1]) y /= float(frame.shape[0]) w /= float(frame.shape[1]) h /= float(frame.shape[0]) gl.glColor4f(1, 1, 1, alpha) image.blit(x * window.width, y * window.height, w * window.width, h * window.height) gl.glColor4f(1, 1, 1, 1) frame.blit(0, 0, window.width, window.height) @window.event def on_key_press(symbol, modifiers): #if symbol == pyglet.window.key.SPACE: # savefig('test.png', window.width, window.height) if symbol == glumpy.key.ESCAPE: if (window.width == frame.shape[1] and window.height == frame.shape[0]): sys.exit() else: window.set_size(frame.shape[1], frame.shape[0]) return True return window
#!/usr/bin/env python # -*- coding: utf-8 -*- #----------------------------------------------------------------------------- # Copyright (C) 2009-2010 Nicolas P. Rougier # # Distributed under the terms of the BSD License. The full license is in # the file COPYING, distributed as part of this software. #----------------------------------------------------------------------------- import numpy, glumpy window = glumpy.Window(512, 512) Z = numpy.random.random((32, 32)).astype(numpy.float32) I = glumpy.Image(Z) @window.event def on_draw(): I.blit(0, 0, window.width, window.height) window.mainloop()
# # Distributed under the terms of the BSD License. The full license is in # the file COPYING, distributed as part of this software. #----------------------------------------------------------------------------- import numpy, glumpy n = 256 Z = numpy.random.randint(-1,2,(n,n)).astype(numpy.float32) N = numpy.zeros(Z.shape).astype(numpy.float32) P = numpy.zeros((n,), dtype=[('x',int),('y',int), ('c',float)]) P['x'] = numpy.random.randint(0,n,P.shape) P['y'] = numpy.random.randint(0,n,P.shape) P['c'] = numpy.random.random(P.shape)*2-1 window = glumpy.Window(512, 512) I = glumpy.Image(Z, interpolation='bicubic', cmap=glumpy.colormap.Hot, vmin=-1, vmax=1) @window.event def on_draw(): window.clear() I.blit(0,0,window.width,window.height) @window.event def on_idle(dt): global N,P,Z P['x'] += (numpy.random.randint(0,3,P.size)-1) P['y'] += (numpy.random.randint(0,3,P.size)-1) P['x'] = numpy.minimum(numpy.maximum(P['x'],0), Z.shape[0]-1) P['y'] = numpy.minimum(numpy.maximum(P['y'],0), Z.shape[0]-1) Z[P['x'], P['y']] = P['c']
FD.MediumSetup(LayerInterProps=[2200, 1800, 967], LayerInterDim=[3.9, 4.2], LayerIntercepting=True) FD.absSetup() FD.receivers_setup(pr, pz, TimeIter) FD.Init_Fields_CL() start = time.time() if Plotting: Z = FD.SV.transpose() fig = glumpy.figure((int(FD.NRI * 0.5), int(FD.NZI * 0.5))) I = glumpy.Image(Z, interpolation='bilinear', colormap=glumpy.colormap.IceAndFire, vmin=-50, vmax=0) @fig.event def on_key_press(key, modifiers): if key == glumpy.window.key.ESCAPE: sys.exit() else: pass @fig.event def on_draw(): fig.clear() I.draw(x=0, y=0,
Z = np.zeros((n, n), dtype=np.float32) # output Z_ = np.zeros((n, n), dtype=np.float32) # membrane potential # Kernel K = fromdistance(w, (2 * n + 1, 2 * n + 1)) USV = scipy.linalg.svd(K) # Output decoding X, Y = np.mgrid[0:n, 0:n] X = 2 * X / float(n - 1) - 1 Y = 2 * Y / float(n - 1) - 1 window = glumpy.Window(2 * 512, 512) Ii = glumpy.Image(I, interpolation='bicubic', cmap=glumpy.colormap.Grey_r, vmin=0.0, vmax=2.5) Zi = glumpy.Image(Z, interpolation='bicubic', cmap=glumpy.colormap.Grey_r, vmin=0.0, vmax=0.25) @window.event def on_draw(): global Zi, Ii window.clear() Ii.blit(0, 0, 512, 512) Zi.blit(512, 0, 512, 512)