FixedOutDegree#

class braintools.conn.FixedOutDegree(k, *, allow_self_connections=True, with_replacement=False, weight=None, delay=None, seed=None, **kwargs)#

Connect every presynaptic neuron to exactly k postsynaptic neurons.

The k targets of each presynaptic neuron are drawn uniformly from the postsynaptic population, independently across presynaptic neurons.

Parameters:
  • k (int | float) – Out-degree. An int is an absolute count; a float in (0, 1] is a fraction of the allowed targets, resolved as round(k * post_num) (round(k * (post_num - 1)) when self-connections are excluded).

  • allow_self_connections (bool) – If False and the populations have equal size, neuron i never targets neuron i (the k targets are drawn from the other post_num - 1 neurons). Ignored when pre_num != post_num.

  • with_replacement (bool) – If True, targets are drawn with replacement, so a pair may appear more than once (duplicate edges) and k may exceed post_num.

  • weight (Initialization | float | int | ndarray | Array | Quantity | None) – Weight initialization (initializer, scalar, Quantity or array), drawn once per edge through braintools.init.param(). When both position arrays are given, per-edge distances are passed to the initializer so distance-dependent weights work. If None, no weights are generated.

  • delay (Initialization | float | int | ndarray | Array | Quantity | None) – Delay initialization, handled like weight. If None, no delays.

  • seed (int | None) – Seed of the generator’s private np.random.RandomState.

  • **kwargs – Further arguments of Connectivity (pre_size, post_size).

Raises:
  • TypeError – If k is neither an int nor a float.

  • ValueError – If k is negative or a float outside (0, 1]; at generation time if k exceeds the available targets (post_num, or post_num - 1 without self-connections) and with_replacement=False.

See also

FixedInDegree

Fixed number of inputs per postsynaptic neuron.

FixedTotalNum

Fixed total number of edges.

FixedProb

Independent per-pair connection probability.

ConnectionResult.to_fixed_num_per_pre

Export to brainevent.FixedNumPerPre.

Notes

Without replacement each row is a uniformly random k-subset, drawn by a vectorised sampler that redraws only duplicated entries (or, when k > post_num / 2, takes the k smallest of i.i.d. uniform keys in row chunks of at most 2**22 pairs). Memory is O(pre_num * k); time is O(pre_num * k), or O(pre_num * post_num) on the key path, where k is already a large fraction of post_num. Edges are returned sorted by presynaptic then postsynaptic index. metadata records 'degree' (the resolved int k), 'with_replacement' and 'allow_self_connections'.

Generation is host-side NumPy and is not traceable by jax.jit; the exported brainevent matrices are.

Examples

>>> import numpy as np
>>> import brainunit as u
>>> from braintools.conn import FixedOutDegree
>>> conn = FixedOutDegree(10, weight=0.5 * u.nS, seed=0)
>>> result = conn(pre_size=100, post_size=200)
>>> result.n_connections
1000
>>> np.unique(np.bincount(result.pre_indices, minlength=100))
array([10])
>>> w = result.to_fixed_num_per_pre()
>>> w.indices.shape
(100, 10)

>>> # k as a fraction of post_num, no self-connections
>>> result = FixedOutDegree(0.1, allow_self_connections=False, seed=0)(50, 50)
>>> result.metadata['degree']
5
>>> bool(np.any(result.pre_indices == result.post_indices))
False
generate(pre_size, post_size, pre_positions=None, post_positions=None, **kwargs)[source]#

Generate the fixed out-degree connections.

Return type:

ConnectionResult

property k: int | float#

The out-degree as given (int count or fraction).