Beispiel #1
0
 def connect(self, projection):
     """Connect-up a Projection."""
     if self.delays is None:
         self.delays = projection._simulator.state.min_delay
     if projection.pre.size == projection.post.size:
         N                 = projection.post.size
         local             = projection.post._mask_local
         if isinstance(self.weights, basestring) or isinstance(self.delays, basestring):
             raise Exception('Expression for weights or delays is not supported for OneToOneConnector !')
         weights_generator = WeightGenerator(self.weights, local, projection, self.safe)
         delays_generator  = DelayGenerator(self.delays, local, kernel=projection._simulator.state, safe=self.safe)                
         weights           = weights_generator.get(N)
         delays            = delays_generator.get(N)
         self.progressbar(len(projection.post.local_cells))                        
         count             = 0            
         create            = numpy.arange(0, N)[local]
         sources           = projection.pre.all_cells[create] 
         homogeneous       = numpy.isscalar(delays_generator.source)
         
         for tgt, src, w, d in zip(projection.post.local_cells, sources, weights, delays):
             # the float is in case the values are of type numpy.float64, which NEST chokes on
             projection._divergent_connect(src, [tgt], float(w), float(d), homogeneous)
             self.progression(count, projection._simulator.state.mpi_rank)
             count += 1
     else:
         raise errors.InvalidDimensionsError("OneToOneConnector does not support presynaptic and postsynaptic Populations of different sizes.")
Beispiel #2
0
    def connect(self, projection):
        """Connect-up a Projection."""
        if self.delays is None:
            self.delays = projection._simulator.state.min_delay
        if projection.pre.size == projection.post.size:
            N = projection.post.size
            local = projection.post._mask_local
            if isinstance(self.weights, basestring) or isinstance(
                    self.delays, basestring):
                raise Exception(
                    'Expression for weights or delays is not supported for OneToOneConnector !'
                )
            weights_generator = WeightGenerator(self.weights, local,
                                                projection, self.safe)
            delays_generator = DelayGenerator(
                self.delays,
                local,
                kernel=projection._simulator.state,
                safe=self.safe)
            weights = weights_generator.get(N)
            delays = delays_generator.get(N)
            self.progressbar(len(projection.post.local_cells))
            count = 0
            create = numpy.arange(0, N)[local]
            sources = projection.pre.all_cells[create]
            homogeneous = numpy.isscalar(delays_generator.source)

            for tgt, src, w, d in zip(projection.post.local_cells, sources,
                                      weights, delays):
                # the float is in case the values are of type numpy.float64, which NEST chokes on
                projection._divergent_connect(src, [tgt], float(w), float(d),
                                              homogeneous)
                self.progression(count, projection._simulator.state.mpi_rank)
                count += 1
        else:
            raise errors.InvalidDimensionsError(
                "OneToOneConnector does not support presynaptic and postsynaptic Populations of different sizes."
            )
Beispiel #3
0
class FastProbabilisticConnector(Connector):
    def __init__(self,
                 projection,
                 weights=0.0,
                 delays=None,
                 allow_self_connections=True,
                 space=Space(),
                 safe=True):

        Connector.__init__(self, weights, delays, space, safe)
        if isinstance(projection.rng, random.NativeRNG):
            raise Exception("Use of NativeRNG not implemented.")
        else:
            self.rng = projection.rng

        self.N = projection.pre.size
        idx = numpy.arange(self.N * rank(), self.N * (rank() + 1))
        self.M = num_processes() * self.N
        self.local = numpy.ones(self.N, bool)
        self.local_long = numpy.zeros(self.M, bool)
        self.local_long[idx] = True
        self.weights_generator = WeightGenerator(weights, self.local_long,
                                                 projection, safe)
        self.delays_generator = DelayGenerator(delays, self.local_long, safe)
        self.probas_generator = ProbaGenerator(
            random.RandomDistribution('uniform', (0, 1), rng=self.rng),
            self.local_long)
        self.distance_matrix = DistanceMatrix(projection.pre.positions,
                                              self.space, self.local)
        self.projection = projection
        self.candidates = projection.pre.all_cells
        self.allow_self_connections = allow_self_connections

    def _probabilistic_connect(self,
                               tgt,
                               p,
                               n_connections=None,
                               rewiring=None):
        """
        Connect-up a Projection with connection probability p, where p may be either
        a float 0<=p<=1, or a dict containing a float array for each pre-synaptic
        cell, the array containing the connection probabilities for all the local
        targets of that pre-synaptic cell.
        """
        if numpy.isscalar(p) and p == 1:
            precreate = numpy.arange(self.N)
        else:
            rarr = self.probas_generator.get(self.M)
            if not core.is_listlike(rarr) and numpy.isscalar(
                    rarr):  # if N=1, rarr will be a single number
                rarr = numpy.array([rarr])
            precreate = numpy.where(rarr < p)[0]
        self.distance_matrix.set_source(tgt.position)

        if not self.allow_self_connections and self.projection.pre == self.projection.post:
            idx_tgt = numpy.where(self.candidates == tgt)
            if len(idx_tgt) > 0:
                i = numpy.where(precreate == idx_tgt[0])
                if len(i) > 0:
                    precreate = numpy.delete(precreate, i[0])

        if (rewiring is not None) and (rewiring > 0):
            if not self.allow_self_connections and self.projection.pre == self.projection.post:
                i = numpy.where(self.candidates == tgt)[0]
                idx = numpy.delete(self.candidates, i)

            rarr = self.probas_generator.get(self.M)[precreate]
            rewired = numpy.where(rarr < rewiring)[0]
            N = len(rewired)
            if N > 0:
                new_idx = (len(idx) - 1) * self.probas_generator.get(
                    self.M)[precreate]
                precreate[rewired] = idx[new_idx.astype(int)]

        if (n_connections is not None) and (len(precreate) > 0):
            create = numpy.array([], int)
            while len(
                    create
            ) < n_connections:  # if the number of requested cells is larger than the size of the
                # presynaptic population, we allow multiple connections for a given cell
                create = numpy.concatenate(
                    (create, self.projection.rng.permutation(precreate)))
            create = create[:n_connections]
        else:
            create = precreate

        sources = self.candidates[create]
        weights = self.weights_generator.get(self.M, self.distance_matrix,
                                             create)
        delays = self.delays_generator.get(self.M, self.distance_matrix,
                                           create)

        if len(sources) > 0:
            self.projection.connection_manager.convergent_connect(
                sources.tolist(), tgt, weights, delays)
Beispiel #4
0
class FastProbabilisticConnector(Connector):
    
    def __init__(self, projection, weights=0.0, delays=None, allow_self_connections=True, space=Space(), safe=True):

        Connector.__init__(self, weights, delays, space, safe)
        if isinstance(projection.rng, random.NativeRNG):
            raise Exception("Use of NativeRNG not implemented.")
        else:
            self.rng = projection.rng
        if self.delays is None:
            self.delays = projection._simulator.state.min_delay
        self.N                 = projection.pre.size
        mpi_rank               = projection._simulator.state.mpi_rank
        num_processes          = projection._simulator.state.num_processes
        idx                    = numpy.arange(self.N*mpi_rank, self.N*(mpi_rank+1), dtype=numpy.int)        
        self.M                 = num_processes*self.N
        self.local             = numpy.ones(self.N, bool)        
        self.local_long        = numpy.zeros(self.M, bool)
        self.local_long[idx]   = True
        self.weights_generator = WeightGenerator(weights, self.local_long, projection, safe)
        self.delays_generator  = DelayGenerator(self.delays, self.local_long, kernel=projection._simulator.state, safe=safe)
        self.probas_generator  = ProbaGenerator(random.RandomDistribution('uniform',(0,1), rng=self.rng), self.local_long)
        self.distance_matrix   = DistanceMatrix(projection.pre.positions, self.space, self.local)
        self.projection        = projection
        self.candidates        = projection.pre.all_cells
        self.allow_self_connections = allow_self_connections
                
    def _probabilistic_connect(self, tgt, p, n_connections=None, rewiring=None):
        """
        Connect-up a Projection with connection probability p, where p may be either
        a float 0<=p<=1, or a dict containing a float array for each pre-synaptic
        cell, the array containing the connection probabilities for all the local
        targets of that pre-synaptic cell.
        """
        if numpy.isscalar(p) and p == 1:
            precreate = numpy.arange(self.N, dtype=numpy.int)
        else:
            rarr   = self.probas_generator.get(self.M)
            if not core.is_listlike(rarr) and numpy.isscalar(rarr): # if N=1, rarr will be a single number
                rarr = numpy.array([rarr])
            precreate = numpy.where(rarr < p)[0]  
        self.distance_matrix.set_source(tgt.position)
        
        if not self.allow_self_connections and self.projection.pre == self.projection.post:
            idx_tgt   = numpy.where(self.candidates == tgt)
            if len(idx_tgt) > 0:
                i     = numpy.where(precreate == idx_tgt[0])
                if len(i) > 0:
                    precreate = numpy.delete(precreate, i[0])
                
        if (rewiring is not None) and (rewiring > 0):
            idx = numpy.arange(self.N, dtype=numpy.int)          
            if not self.allow_self_connections and self.projection.pre == self.projection.post:
                i   = numpy.where(self.candidates == tgt)[0]
                idx = numpy.delete(idx, i)
            
            rarr    = self.probas_generator.get(self.M)[precreate]
            rewired = numpy.where(rarr < rewiring)[0]
            N       = len(rewired)
            if N > 0:
                new_idx            = (len(idx)-1) * self.probas_generator.get(self.M)[precreate]
                precreate[rewired] = idx[new_idx.astype(int)]    
        
        if (n_connections is not None) and (len(precreate) > 0):
            create = numpy.array([], dtype=numpy.int)
            while len(create) < n_connections: # if the number of requested cells is larger than the size of the
                                               # presynaptic population, we allow multiple connections for a given cell
                create = numpy.concatenate((create, self.projection.rng.permutation(precreate)))
            create = create[:n_connections]
        else:
            create = precreate   

        sources = self.candidates[create]        
        weights = self.weights_generator.get(self.M, self.distance_matrix, create)
        delays  = self.delays_generator.get(self.M, self.distance_matrix, create)        
        
        if len(sources) > 0:
            self.projection._convergent_connect(sources.tolist(), tgt, weights, delays)