brainpy.connect module

Formatter functions

ij2mat(i, j[, num_pre, num_post])

Convert i-j connection to matrix connection.

mat2ij(conn_mat)

Get the i-j connections from connectivity matrix.

pre2post(i, j[, num_pre])

Get pre2post connections from i and j indexes.

post2pre(i, j[, num_post])

Get post2pre connections from i and j indexes.

pre2syn(i[, num_pre])

Get pre2syn connections from i and j indexes.

post2syn(j[, num_post])

Get post2syn connections from i and j indexes.

pre_slice(i, j[, num_pre])

Get post slicing connections by pre-synaptic ids.

post_slice(i, j[, num_post])

Get pre slicing connections by post-synaptic ids.

Regular Connections

One2One()

Connect two neuron groups one by one.

All2All([include_self])

Connect each neuron in first group to all neurons in the post-synaptic neuron groups.

GridFour([include_self])

The nearest four neighbors conn method.

GridEight([include_self])

The nearest eight neighbors conn method.

GridN([N, include_self])

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 post-synaptic 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

FixedPostNum(num[, include_self, seed])

Connect the post-synaptic neurons with fixed number for each pre-synaptic neuron.

FixedPreNum(num[, include_self, seed])

Connect the pre-synaptic neurons with fixed number for each post-synaptic neuron.

FixedProb(prob[, include_self, seed, method])

Connect the post-synaptic neurons with fixed probability.

GaussianProb(sigma[, p_min, normalize, ...])

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

GaussianWeight(sigma, w_max[, w_min, ...])

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

DOG(sigmas, ws_max[, w_min, normalize, ...])

Builds a Difference-Of-Gaussian (dog) conn pattern between the two populations.

SmallWorld(num_neighbor, prob[, directed, ...])

Build a Watts–Strogatz small-world graph.

ScaleFreeBA(m[, directed, seed])

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

ScaleFreeBADual(m1, m2, p[, directed, seed])

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

PowerLaw(m, p[, directed, seed])

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 post-synaptic neurons with fixed number for each pre-synaptic 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 pre-synaptic neurons with fixed number for each post-synaptic 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 post-synaptic 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{(x-x_c)^2+(y-y_c)^2}{2\sigma^2})\]

where \((x, y)\) is the position of the pre-synaptic neuron and \((x_c,y_c)\) is the position of the post-synaptic 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{(x-x_c)^2+(y-y_c)^2}{2\sigma^2})\]

where \((x, y)\) is the position of the pre-synaptic neuron (normalized to [0,1]) and \((x_c,y_c)\) is the position of the post-synaptic 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 Difference-Of-Gaussian (dog) conn pattern between the two populations.

Mathematically,

\[w(x, y) = w_{max}^+ \cdot \exp(-\frac{(x-x_c)^2+(y-y_c)^2}{2\sigma_+^2}) - w_{max}^- \cdot \exp(-\frac{(x-x_c)^2+(y-y_c)^2}{2\sigma_-^2})\]

where weights smaller than \(0.01 * abs(w_{max} - w_{min})\) are not created and self-connections 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 small-world 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 small-world 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 509-512, 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 $1-p$) 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 $1-p$

  • 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 satisfy 0 <= p <= 1.

References

1
  1. Moshiri “The dual-Barabasi-Albert 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 satisfy 0 <= p <= 1.

References

1

P. Holme and B. J. Kim, “Growing scale-free networks with tunable clustering”, Phys. Rev. E, 65, 026107, 2002.