Changeset 421

Show
Ignore:
Timestamp:
07/17/08 09:34:30 (1 month ago)
Author:
pierre
Message:

Modification of the RandomDistribution? object to allow the specification of boundaries, and the way we want to deal with numbers drawn out of those boundaries. Because for distribution that are not uniform (Gaussian, Gamma, ...) we may still want to have a control on the overall range of the random numbers. Numbers could be clipped to min/max values, or either redrawn till they fall within min/max values

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/random.py

    r366 r421  
    145145       numbers from a given distribution.""" 
    146146        
    147     def __init__(self, distribution='uniform', parameters=[], rng=None): 
     147    def __init__(self, distribution='uniform', parameters=[], rng=None, boundaries=None, constrain="clip"): 
    148148        """ 
    149149        If present, rng should be a NumpyRNG or GSLRNG object. 
     
    153153            by the underlying method in the correct order. named arguments are 
    154154            not yet supported. 
     155        boundaries is a tuple (min, max) used to specify explicitely, for distribution  
     156            like Gaussian, Gamma or others, hard boundaries for the parameters. If  
     157            parameters are drawn outside those boundaries, the policy applied will depend  
     158            on the constrain parameter. 
     159        constrain control the policy for weights out of the specified boundaries.  
     160            If "clip", random numbers are clipped to the boundaries.  
     161            If "redraw", random numbers are drawn till they fall within the boundaries. 
    155162        Note that NumpyRNG and GSLRNG distributions may not have the same names, 
    156163            e.g., 'normal' for NumpyRNG and 'gaussian' for GSLRNG, and the 
     
    160167        assert isinstance(parameters, (list, tuple, dict)), "The parameters argument must be a list or tuple or dict" 
    161168        self.parameters = parameters 
     169        self.boundaries = boundaries 
     170        self.constrain  = constrain 
    162171        if rng: 
    163172            assert isinstance(rng, AbstractRNG), "rng must be a pyNN.random RNG object" 
     
    168177    def next(self, n=1): 
    169178        """Return n random numbers from the distribution.""" 
    170         return self.rng.next(n=n, 
     179        if self.boundaries: 
     180            res = self.rng.next(n=n, 
     181                             distribution=self.name, 
     182                             parameters=self.parameters) 
     183            if self.constrain == "clip": 
     184                return numpy.maximum(numpy.minimum(res,self.boundaries[1]),self.boundaries[0]) 
     185            elif self.constrain == "redraw": 
     186                idx = numpy.where((res > self.boundaries[1]) | (res < self.boundaries[0]))[0] 
     187                while len(idx) > 0: 
     188                    res[idx] = self.rng.next(len(idx),distribution=self.name,parameters=self.parameters) 
     189                    idx = numpy.where((res > self.boundaries[1]) | (res < self.boundaries[0]))[0] 
     190                return res 
     191            else: 
     192                raise Exception("This constrain method (%s) does not exist" %self.constrain) 
     193        else: 
     194            return self.rng.next(n=n, 
    171195                             distribution=self.name, 
    172196                             parameters=self.parameters) 
  • trunk/test/brunel.py

    r415 r421  
    170170print "input --> E\t", len(input_to_E), "connections" 
    171171 
    172 print E_to_E.describe() 
    173  
    174172print "%d Connecting inhibitory population." %myid 
    175173E_to_I = Projection(E_net, I_net, E_Connector, rng=rng, target="excitatory")