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

Source Code for Module topo.pattern.random

  1  """ 
  2  Two-dimensional pattern generators drawing from various random distributions. 
  3   
  4  $Id: random.py 11293 2010-07-27 14:19:32Z ceball $ 
  5  """ 
  6  __version__='$Revision: 11293 $' 
  7   
  8  import numpy 
  9   
 10  import param 
 11  from param.parameterized import ParamOverrides 
 12   
 13  from topo.base.patterngenerator import PatternGenerator 
 14  from topo.pattern import Composite, Gaussian 
 15  from topo.base.sheetcoords import SheetCoordinateSystem 
 16   
 17   
18 -def seed(seed=None):
19 """ 20 Set the seed on the shared RandomState instance. 21 22 Convenience function: shortcut to RandomGenerator.random_generator.seed(). 23 """ 24 RandomGenerator.random_generator.seed(seed)
25 26
27 -class RandomGenerator(PatternGenerator):
28 """2D random noise pattern generator abstract class.""" 29 30 __abstract = True 31 32 # The orientation is ignored, so we don't show it in 33 # auto-generated lists of parameters (e.g. in the GUI) 34 orientation = param.Number(precedence=-1) 35 36 random_generator = param.Parameter( 37 default=numpy.random.RandomState(seed=(500,500)),precedence=-1,doc= 38 """ 39 numpy's RandomState provides methods for generating random 40 numbers (see RandomState's help for more information). 41 42 Note that all instances will share this RandomState object, 43 and hence its state. To create a RandomGenerator that has its 44 own state, set this parameter to a new RandomState instance. 45 """) 46 47
48 - def _distrib(self,shape,p):
49 """Method for subclasses to override with a particular random distribution.""" 50 raise NotImplementedError
51 52 # Optimization: We use a simpler __call__ method here to skip the 53 # coordinate transformations (which would have no effect anyway)
54 - def __call__(self,**params_to_override):
55 p = ParamOverrides(self,params_to_override) 56 57 shape = SheetCoordinateSystem(p.bounds,p.xdensity,p.ydensity).shape 58 59 result = self._distrib(shape,p) 60 self._apply_mask(p,result) 61 62 for of in p.output_fns: 63 of(result) 64 65 return result
66 67 68
69 -class UniformRandom(RandomGenerator):
70 """2D uniform random noise pattern generator.""" 71
72 - def _distrib(self,shape,p):
73 return p.random_generator.uniform(p.offset, p.offset+p.scale, shape)
74 75 76
77 -class BinaryUniformRandom(RandomGenerator):
78 """ 79 2D binary uniform random noise pattern generator. 80 81 Generates an array of random numbers that are 1.0 with the given 82 on_probability, or else 0.0, then scales it and adds the offset as 83 for other patterns. For the default scale and offset, the result 84 is a binary mask where some elements are on at random. 85 """ 86 87 on_probability = param.Number(default=0.5,bounds=[0.0,1.0],doc=""" 88 Probability (in the range 0.0 to 1.0) that the binary value 89 (before scaling) is on rather than off (1.0 rather than 0.0).""") 90
91 - def _distrib(self,shape,p):
92 rmin = p.on_probability-0.5 93 return p.offset+p.scale*(p.random_generator.uniform(rmin,rmin+1.0,shape).round())
94 95 96
97 -class GaussianRandom(RandomGenerator):
98 """ 99 2D Gaussian random noise pattern generator. 100 101 Each pixel is chosen independently from a Gaussian distribution 102 of zero mean and unit variance, then multiplied by the given 103 scale and adjusted by the given offset. 104 """ 105 106 scale = param.Number(default=0.25,softbounds=(0.0,2.0)) 107 offset = param.Number(default=0.50,softbounds=(-2.0,2.0)) 108
109 - def _distrib(self,shape,p):
110 return p.offset+p.scale*p.random_generator.standard_normal(shape)
111 112 113 # CEBALERT: in e.g. script_repr, an instance of this class appears to 114 # have only pattern.Constant() in its list of generators, which might 115 # be confusing. The Constant pattern has no effect because the 116 # generators list is overridden in __call__. Shouldn't the generators 117 # parameter be hidden for this class (and possibly for others based on 118 # pattern.Composite)? For that to be safe, we'd at least have to have 119 # a warning if someone ever sets a hidden parameter, so that having it 120 # revert to the default value would always be ok. 121
122 -class GaussianCloud(Composite):
123 """Uniform random noise masked by a circular Gaussian.""" 124 125 operator = param.Parameter(numpy.multiply) 126 127 gaussian_size = param.Number(default=1.0,doc="Size of the Gaussian pattern.") 128 129 aspect_ratio = param.Number(default=1.0,bounds=(0.0,None),softbounds=(0.0,2.0), 130 precedence=0.31,doc=""" 131 Ratio of gaussian width to height; width is gaussian_size*aspect_ratio.""") 132
133 - def __call__(self,**params_to_override):
134 p = ParamOverrides(self,params_to_override) 135 p.generators=[Gaussian(aspect_ratio=p.aspect_ratio,size=p.gaussian_size), 136 UniformRandom()] 137 return super(GaussianCloud,self).__call__(**p)
138