This is an archival dump of old wiki content --- see scipy.org for current material.
Please see http://scipy-cookbook.readthedocs.org/

Attachment 'lic_demo.py'

Download

   1 import numpy as np
   2 import pylab as plt
   3 
   4 import lic_internal
   5 
   6 dpi = 100
   7 size = 700
   8 video = False
   9 
  10 vortex_spacing = 0.5
  11 extra_factor = 2.
  12 
  13 a = np.array([1,0])*vortex_spacing
  14 b = np.array([np.cos(np.pi/3),np.sin(np.pi/3)])*vortex_spacing
  15 rnv = int(2*extra_factor/vortex_spacing)
  16 vortices = [n*a+m*b for n in range(-rnv,rnv) for m in range(-rnv,rnv)]
  17 vortices = [(x,y) for (x,y) in vortices if -extra_factor<x<extra_factor and -extra_factor<y<extra_factor]
  18 
  19 
  20 xs = np.linspace(-1,1,size).astype(np.float32)[None,:]
  21 ys = np.linspace(-1,1,size).astype(np.float32)[:,None]
  22 
  23 vectors = np.zeros((size,size,2),dtype=np.float32)
  24 for (x,y) in vortices:
  25     rsq = (xs-x)**2+(ys-y)**2
  26     vectors[...,0] +=  (ys-y)/rsq
  27     vectors[...,1] += -(xs-x)/rsq
  28     
  29 texture = np.random.rand(size,size).astype(np.float32)
  30 
  31 plt.bone()
  32 frame=0
  33 
  34 if video:
  35     kernellen = 31
  36     for t in np.linspace(0,1,16*5):
  37         kernel = np.sin(np.arange(kernellen)*np.pi/kernellen)*(1+np.sin(2*np.pi*5*(np.arange(kernellen)/float(kernellen)+t)))
  38 
  39         kernel = kernel.astype(np.float32)
  40 
  41         image = lic_internal.line_integral_convolution(vectors, texture, kernel)
  42 
  43         plt.clf()
  44         plt.axis('off')
  45         plt.figimage(image)
  46         plt.gcf().set_size_inches((size/float(dpi),size/float(dpi)))
  47         plt.savefig("flow-%04d.png"%frame,dpi=dpi)
  48         frame += 1
  49 else:
  50     kernellen=31
  51     kernel = np.sin(np.arange(kernellen)*np.pi/kernellen)
  52     kernel = kernel.astype(np.float32)
  53 
  54     image = lic_internal.line_integral_convolution(vectors, texture, kernel)
  55 
  56     plt.clf()
  57     plt.axis('off')
  58     plt.figimage(image)
  59     plt.gcf().set_size_inches((size/float(dpi),size/float(dpi)))
  60     plt.savefig("flow-image.png",dpi=dpi)

New Attachment

File to upload
Rename to
Overwrite existing attachment of same name

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.