Package topo :: Package pattern :: Module rds
[hide private]
[frames] | no frames]

Source Code for Module topo.pattern.rds

  1  """ 
  2  Random-dot stereogram patterns. 
  3   
  4  $Id: rds.py 11293 2010-07-27 14:19:32Z ceball $ 
  5  """ 
  6  __version__='$Revision: 11293 $' 
  7   
  8  from numpy.oldnumeric import zeros,floor,where,choose,less,greater,Int 
  9  from numpy.oldnumeric.random_array import random,seed 
 10   
 11  import param 
 12  from param.parameterized import ParamOverrides 
 13   
 14  from topo.base.patterngenerator import PatternGenerator 
 15  from topo.base.sheetcoords import SheetCoordinateSystem 
 16   
 17   
 18  ### JABHACKALERT: This code seems to work fine when the input regions 
 19  ### are all the same size and shape, but for 
 20  ### e.g. examples/hierarchical.ty the resulting images in the Test 
 21  ### Pattern preview window are square (instead of the actual 
 22  ### rectangular shapes), matching between the eyes (instead of the 
 23  ### actual two different rectangles), and with dot sizes that don't 
 24  ### match between the eyes.  It's not clear why this happens. 
 25   
26 -class RandomDotStereogram(PatternGenerator):
27 """ 28 Random dot stereogram using rectangular black and white patches. 29 30 Based on Matlab code originally from Jenny Read, implemented in 31 Topographica by Tikesh Ramtohul (2006). 32 """ 33 34 # Suppress unused parameters 35 x = param.Number(precedence=-1) 36 y = param.Number(precedence=-1) 37 size = param.Number(precedence=-1) 38 orientation = param.Number(precedence=-1) 39 40 # Override defaults to make them appropriate 41 scale = param.Number(default=0.5) 42 offset = param.Number(default=0.5) 43 44 # New parameters for this pattern 45 46 #JABALERT: Should rename xdisparity and ydisparity to x and y, and simply 47 #set them to different values for each pattern to get disparity 48 xdisparity = param.Number(default=0.0,bounds=(-1.0,1.0),softbounds=(-0.5,0.5), 49 precedence=0.50,doc="Disparity in the horizontal direction.") 50 51 ydisparity = param.Number(default=0.0,bounds=(-1.0,1.0),softbounds=(-0.5,0.5), 52 precedence=0.51,doc="Disparity in the vertical direction.") 53 54 dotdensity = param.Number(default=0.5,bounds=(0.0,None),softbounds=(0.1,0.9), 55 precedence=0.52,doc="Number of dots per unit area; 0.5=50% coverage.") 56 57 dotsize = param.Number(default=0.1,bounds=(0.0,None),softbounds=(0.05,0.15), 58 precedence=0.53,doc="Edge length of each square dot.") 59 60 random_seed=param.Integer(default=500,bounds=(0,1000), 61 precedence=0.54,doc="Seed value for the random position of the dots.") 62 63
64 - def __call__(self,**params_to_override):
65 p = ParamOverrides(self,params_to_override) 66 67 xsize,ysize = SheetCoordinateSystem(p.bounds,p.xdensity,p.ydensity).shape 68 xsize,ysize = int(round(xsize)),int(round(ysize)) 69 70 xdisparity = int(round(xsize*p.xdisparity)) 71 ydisparity = int(round(xsize*p.ydisparity)) 72 dotsize = int(round(xsize*p.dotsize)) 73 74 bigxsize = 2*xsize 75 bigysize = 2*ysize 76 ndots=int(round(p.dotdensity * (bigxsize+2*dotsize) * (bigysize+2*dotsize) / 77 min(dotsize,xsize) / min(dotsize,ysize))) 78 halfdot = floor(dotsize/2) 79 80 # Choose random colors and locations of square dots 81 random_seed = p.random_seed 82 83 seed(random_seed*12,random_seed*99) 84 col=where(random((ndots))>=0.5, 1.0, -1.0) 85 86 seed(random_seed*122,random_seed*799) 87 xpos=floor(random((ndots))*(bigxsize+2*dotsize)) - halfdot 88 89 seed(random_seed*1243,random_seed*9349) 90 ypos=floor(random((ndots))*(bigysize+2*dotsize)) - halfdot 91 92 # Construct arrays of points specifying the boundaries of each 93 # dot, cropping them by the big image size (0,0) to (bigxsize,bigysize) 94 x1=xpos.astype(Int) ; x1=choose(less(x1,0),(x1,0)) 95 y1=ypos.astype(Int) ; y1=choose(less(y1,0),(y1,0)) 96 x2=(xpos+(dotsize-1)).astype(Int) ; x2=choose(greater(x2,bigxsize),(x2,bigxsize)) 97 y2=(ypos+(dotsize-1)).astype(Int) ; y2=choose(greater(y2,bigysize),(y2,bigysize)) 98 99 # Draw each dot in the big image, on a blank background 100 bigimage = zeros((bigysize,bigxsize)) 101 for i in range(ndots): 102 bigimage[y1[i]:y2[i]+1,x1[i]:x2[i]+1] = col[i] 103 104 result = p.offset + p.scale*bigimage[ (ysize/2)+ydisparity:(3*ysize/2)+ydisparity , 105 (xsize/2)+xdisparity:(3*xsize/2)+xdisparity ] 106 107 for of in p.output_fns: 108 of(result) 109 110 return result
111