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 test_really_simple0(self): A = numpy.zeros((3, )) B = numpy.zeros((3, 5)) D = DistanceMatrix(B, Space()) D.set_source(A) assert_arrays_almost_equal(D.as_array(), numpy.zeros((5, ), float), 1e-12)
def test_really_simple1(self): A = numpy.ones((3,)) B = numpy.zeros((3,5)) D = DistanceMatrix(B, Space()) D.set_source(A) assert_arrays_almost_equal(D.as_array(), numpy.sqrt(3*numpy.ones((5,), float)), 1e-12)
def test_extract_with_simple_d_expr(self): B = numpy.zeros((3, 5)) dist = DistanceMatrix(B, Space()) gen = ConnectionAttributeGenerator(source="d*d", local_mask=None, safe=False) dist.set_source(numpy.zeros((3, ))) assert_arrays_almost_equal(gen.extract(5, dist, sub_mask=None), numpy.zeros((5, ), float), 1e-12) dist.set_source(numpy.array([1, 0, 0])) assert_arrays_almost_equal(gen.extract(5, dist, sub_mask=None), numpy.ones((5, ), float), 1e-12) dist.set_source(numpy.array([2, 0, 0])) assert_arrays_almost_equal(gen.extract(5, dist, sub_mask=None), 4 * numpy.ones((5, ), float), 1e-12)
def test_extract_with_simple_d_expr(self): B = numpy.zeros((3,5)) dist = DistanceMatrix(B, Space()) gen = ConnectionAttributeGenerator( source="d*d", local_mask=None, safe=False) dist.set_source(numpy.zeros((3,))) assert_arrays_almost_equal(gen.extract(5, dist, sub_mask=None), numpy.zeros((5,), float), 1e-12) dist.set_source(numpy.array([1,0,0])) assert_arrays_almost_equal(gen.extract(5, dist, sub_mask=None), numpy.ones((5,), float), 1e-12) dist.set_source(numpy.array([2,0,0])) assert_arrays_almost_equal(gen.extract(5, dist, sub_mask=None), 4*numpy.ones((5,), float), 1e-12)
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
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)
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)