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

Source Code for Module topo.patterns.random

 1  """ 
 2  Two-dimensional pattern generators drawing from various random distributions. 
 3   
 4  $Id: random.py 7861 2008-02-07 06:01:22Z ceball $ 
 5  """ 
 6  __version__='$Revision: 7861 $' 
 7   
 8  import numpy.oldnumeric.random_array as RandomArray 
 9   
10  from topo.base.parameterizedobject import ParamOverrides 
11  from topo.base.parameterclasses import Number,Parameter 
12  from topo.base.patterngenerator import PatternGenerator 
13  from topo.base.sheetcoords import SheetCoordinateSystem 
14   
15  from topo.outputfns.basic import IdentityOF 
16   
17   
18 -class RandomGenerator(PatternGenerator):
19 """2D random noise pattern generator abstract class.""" 20 21 __abstract = True 22 23 # The standard x, y, and orientation variables are currently ignored, 24 # so they aren't shown in auto-generated lists of parameters (e.g. in the GUI) 25 x = Number(precedence=-1) 26 y = Number(precedence=-1) 27 size = Number(precedence=-1) 28 orientation = Number(precedence=-1) 29
30 - def _distrib(self,shape,pos):
31 """Method for subclasses to override with a particular random distribution.""" 32 raise NotImplementedError
33 34 # Optimization: We use a simpler __call__ method here to skip the 35 # coordinate transformations (which would have no effect anyway)
36 - def __call__(self,**params_to_override):
37 self._check_params(params_to_override) 38 params = ParamOverrides(self,params_to_override) 39 40 shape = SheetCoordinateSystem(params['bounds'],params['xdensity'],params['ydensity']).shape 41 42 result = self._distrib(shape,params) 43 44 mask = params['mask'] 45 if mask is not None: 46 result*=mask 47 48 output_fn = params['output_fn'] 49 if output_fn is not IdentityOF: # Optimization (but may not actually help) 50 output_fn(result) 51 52 return result
53 54 55
56 -class UniformRandom(RandomGenerator):
57 """2D uniform random noise pattern generator.""" 58
59 - def _distrib(self,shape,params):
60 return RandomArray.uniform(params['offset'], params['offset']+params['scale'], shape)
61 62
63 -class GaussianRandom(RandomGenerator):
64 """ 65 2D Gaussian random noise pattern generator. 66 67 Each pixel is chosen independently from a Gaussian distribution 68 of zero mean and unit variance, then multiplied by the given 69 scale and adjusted by the given offset. 70 """ 71 72 scale = Number(default=0.25,softbounds=(0.0,2.0)) 73 offset = Number(default=0.50,softbounds=(-2.0,2.0)) 74
75 - def _distrib(self,shape,params):
76 return params['offset']+params['scale']*RandomArray.standard_normal(shape)
77