brainpy.connect
module¶
Formatter functions¶

Convert ij connection to matrix connection. 

Get the ij connections from connectivity matrix. 

Get pre2post connections from i and j indexes. 

Get post2pre connections from i and j indexes. 

Get pre2syn connections from i and j indexes. 

Get post2syn connections from i and j indexes. 

Get post slicing connections by presynaptic ids. 

Get pre slicing connections by postsynaptic ids. 
Regular Connections¶

Connect two neuron groups one by one. 

Connect each neuron in first group to all neurons in the postsynaptic neuron groups. 

The nearest four neighbors conn method. 

The nearest eight neighbors conn method. 

The nearest (2*N+1) * (2*N+1) neighbors conn method. 
 class brainpy.simulation.connectivity.One2One[source]¶
Connect two neuron groups one by one. This means The two neuron groups should have the same size.
 class brainpy.simulation.connectivity.All2All(include_self=True)[source]¶
Connect each neuron in first group to all neurons in the postsynaptic neuron groups. It means this kind of conn will create (num_pre x num_post) synapses.
 class brainpy.simulation.connectivity.GridFour(include_self=False)[source]¶
The nearest four neighbors conn method.
 class brainpy.simulation.connectivity.GridEight(include_self=False)[source]¶
The nearest eight neighbors conn method.
 class brainpy.simulation.connectivity.GridN(N=1, include_self=False)[source]¶
The nearest (2*N+1) * (2*N+1) neighbors conn method.
 Parameters
N (int) –
Extend of the conn scope. For example: When N=1,
[x x x] [x I x] [x x x]
 When N=2,
[x x x x x] [x x x x x] [x x I x x] [x x x x x] [x x x x x]
include_self (bool) – Whether create (i, i) conn ?
Random Connections¶

Connect the postsynaptic neurons with fixed number for each presynaptic neuron. 

Connect the presynaptic neurons with fixed number for each postsynaptic neuron. 

Connect the postsynaptic neurons with fixed probability. 

Builds a Gaussian conn pattern between the two populations, where the conn probability decay according to the gaussian function. 

Builds a Gaussian conn pattern between the two populations, where the weights decay with gaussian function. 

Builds a DifferenceOfGaussian (dog) conn pattern between the two populations. 

Build a Watts–Strogatz smallworld graph. 

Build a random graph according to the Barabási–Albert preferential attachment model. 

Build a random graph according to the dual Barabási–Albert preferential attachment model. 

Holme and Kim algorithm for growing graphs with powerlaw degree distribution and approximate average clustering. 
 class brainpy.simulation.connectivity.FixedPostNum(num, include_self=True, seed=None)[source]¶
Connect the postsynaptic neurons with fixed number for each presynaptic neuron.
 Parameters
num (float, int) – The conn probability (if “num” is float) or the fixed number of connectivity (if “num” is int).
include_self (bool) – Whether create (i, i) conn ?
seed (None, int) – Seed the random generator.
 class brainpy.simulation.connectivity.FixedPreNum(num, include_self=True, seed=None)[source]¶
Connect the presynaptic neurons with fixed number for each postsynaptic neuron.
 Parameters
num (float, int) – The conn probability (if “num” is float) or the fixed number of connectivity (if “num” is int).
include_self (bool) – Whether create (i, i) conn ?
seed (None, int) – Seed the random generator.
 class brainpy.simulation.connectivity.FixedProb(prob, include_self=True, seed=None, method='matrix')[source]¶
Connect the postsynaptic neurons with fixed probability.
 Parameters
prob (float) – The conn probability.
include_self (bool) – Whether create (i, i) conn ?
seed (None, int) – Seed the random generator.
 class brainpy.simulation.connectivity.GaussianProb(sigma, p_min=0.0, normalize=True, include_self=True, seed=None)[source]¶
Builds a Gaussian conn pattern between the two populations, where the conn probability decay according to the gaussian function.
Specifically,
\[p=\exp(\frac{(xx_c)^2+(yy_c)^2}{2\sigma^2})\]where \((x, y)\) is the position of the presynaptic neuron and \((x_c,y_c)\) is the position of the postsynaptic neuron.
 Parameters
sigma (float) – Width of the Gaussian function.
normalize (bool) – Whether normalize the coordination.
include_self (bool) – Whether create the conn at the same position.
seed (bool) – The random seed.
 class brainpy.simulation.connectivity.GaussianWeight(sigma, w_max, w_min=None, normalize=True, include_self=True, seed=None)[source]¶
Builds a Gaussian conn pattern between the two populations, where the weights decay with gaussian function.
Specifically,
\[w(x, y) = w_{max} \cdot \exp(\frac{(xx_c)^2+(yy_c)^2}{2\sigma^2})\]where \((x, y)\) is the position of the presynaptic neuron (normalized to [0,1]) and \((x_c,y_c)\) is the position of the postsynaptic neuron (normalized to [0,1]), \(w_{max}\) is the maximum weight. In order to void creating useless synapses, \(w_{min}\) can be set to restrict the creation of synapses to the cases where the value of the weight would be superior to \(w_{min}\). Default is \(0.01 w_{max}\).
 Parameters
sigma (float) – Width of the Gaussian function.
w_max (float) – The weight amplitude of the Gaussian function.
w_min (float, None) – The minimum weight value below which synapses are not created (default: 0.01 * w_max).
normalize (bool) – Whether normalize the coordination.
include_self (bool) – Whether create the conn at the same position.
 class brainpy.simulation.connectivity.DOG(sigmas, ws_max, w_min=None, normalize=True, include_self=True)[source]¶
Builds a DifferenceOfGaussian (dog) conn pattern between the two populations.
Mathematically,
\[w(x, y) = w_{max}^+ \cdot \exp(\frac{(xx_c)^2+(yy_c)^2}{2\sigma_+^2})  w_{max}^ \cdot \exp(\frac{(xx_c)^2+(yy_c)^2}{2\sigma_^2})\]where weights smaller than \(0.01 * abs(w_{max}  w_{min})\) are not created and selfconnections are avoided by default (parameter allow_self_connections).
 Parameters
sigmas (tuple) – Widths of the positive and negative Gaussian functions.
ws_max (tuple) – The weight amplitudes of the positive and negative Gaussian functions.
w_min (float, None) – The minimum weight value below which synapses are not created (default: \(0.01 * w_{max}^+  w_{min}^\)).
normalize (bool) – Whether normalize the coordination.
include_self (bool) – Whether create the conn at the same position.
 class brainpy.simulation.connectivity.SmallWorld(num_neighbor, prob, directed=False, include_self=False)[source]¶
Build a Watts–Strogatz smallworld graph.
 Parameters
num_neighbor (int) – Each node is joined with its k nearest neighbors in a ring topology.
prob (float) – The probability of rewiring each edge
directed (bool) – Whether the graph is a directed graph.
include_self (bool) – Whether include the node self.
Notes
First create a ring over $num_node$ nodes [1]_. Then each node in the ring is joined to its $num_neighbor$ nearest neighbors (or $num_neighbor  1$ neighbors if $num_neighbor$ is odd). Then shortcuts are created by replacing some edges as follows: for each edge $(u, v)$ in the underlying “$num_node$ring with $num_neighbor$ nearest neighbors” with probability $prob$ replace it with a new edge $(u, w)$ with uniformly random choice of existing node $w$.
References
 1
Duncan J. Watts and Steven H. Strogatz, Collective dynamics of smallworld networks, Nature, 393, pp. 440–442, 1998.
 class brainpy.simulation.connectivity.ScaleFreeBA(m, directed=False, seed=None)[source]¶
Build a random graph according to the Barabási–Albert preferential attachment model.
A graph of $num_node$ nodes is grown by attaching new nodes each with $m$ edges that are preferentially attached to existing nodes with high degree.
 Parameters
m (int) – Number of edges to attach from a new node to existing nodes
seed (integer, random_state, or None (default)) – Indicator of random number generation state.
 Raises
ValueError – If m does not satisfy
1 <= m < n
.
References
 1
A. L. Barabási and R. Albert “Emergence of scaling in random networks”, Science 286, pp 509512, 1999.
 class brainpy.simulation.connectivity.ScaleFreeBADual(m1, m2, p, directed=False, seed=None)[source]¶
Build a random graph according to the dual Barabási–Albert preferential attachment model.
A graph of $num_node$ nodes is grown by attaching new nodes each with either $m_1$ edges (with probability $p$) or $m_2$ edges (with probability $1p$) that are preferentially attached to existing nodes with high degree.
 Parameters
m1 (int) – Number of edges to attach from a new node to existing nodes with probability $p$
m2 (int) – Number of edges to attach from a new node to existing nodes with probability $1p$
p (float) – The probability of attaching $m_1$ edges (as opposed to $m_2$ edges)
seed (integer, random_state, or None (default)) – Indicator of random number generation state.
 Raises
ValueError – If m1 and m2 do not satisfy
1 <= m1,m2 < n
or p does not satisfy0 <= p <= 1
.
References
 1
Moshiri “The dualBarabasiAlbert model”, arXiv:1810.10538.
 class brainpy.simulation.connectivity.PowerLaw(m, p, directed=False, seed=None)[source]¶
Holme and Kim algorithm for growing graphs with powerlaw degree distribution and approximate average clustering.
 Parameters
m (int) – the number of random edges to add for each new node
p (float,) – Probability of adding a triangle after adding a random edge
seed (integer, random_state, or None (default)) – Indicator of random number generation state.
Notes
The average clustering has a hard time getting above a certain cutoff that depends on m. This cutoff is often quite low. The transitivity (fraction of triangles to possible triangles) seems to decrease with network size.
It is essentially the Barabási–Albert (BA) growth model with an extra step that each random edge is followed by a chance of making an edge to one of its neighbors too (and thus a triangle).
This algorithm improves on BA in the sense that it enables a higher average clustering to be attained if desired.
It seems possible to have a disconnected graph with this algorithm since the initial m nodes may not be all linked to a new node on the first iteration like the BA model.
 Raises
ValueError – If m does not satisfy
1 <= m <= n
or p does not satisfy0 <= p <= 1
.
References
 1
P. Holme and B. J. Kim, “Growing scalefree networks with tunable clustering”, Phys. Rev. E, 65, 026107, 2002.