FixedTotalNum#

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

Draw exactly n edges uniformly over all allowed (pre, post) pairs.

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

  • allow_self_connections (bool) – If False and the populations have equal size, pairs (i, i) are never drawn. Ignored when pre_num != post_num.

  • with_replacement (bool) – If True, the n pairs are drawn independently with replacement, so a pair may appear more than once and n may exceed the number of pairs.

  • weight (Initialization | float | int | ndarray | Array | Quantity | None) – Weight initialization (initializer, scalar, Quantity or array), drawn once per edge through braintools.init.param(). 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 n is neither an int nor a float.

  • ValueError – If n is negative or a float outside (0, 1]; at generation time if n exceeds the number of allowed pairs and with_replacement=False.

Notes

The pairs are n distinct flat indices into the (virtual) pre_num x post_num matrix (pre_num x (post_num - 1) without self-connections, remapped by shifting), drawn by rejection of duplicates when n is at most half the pairs and by a permutation of at most 2 n values otherwise, so memory is O(n). Unlike FixedProb, the edge count is exact rather than binomial. Edges are returned sorted by presynaptic then postsynaptic index. metadata records 'n' (the resolved int count), 'with_replacement' and 'allow_self_connections'.

Generation is host-side NumPy and is not traceable by jax.jit.

Examples

>>> import numpy as np
>>> from braintools.conn import FixedTotalNum
>>> result = FixedTotalNum(500, allow_self_connections=False, seed=0)(100, 100)
>>> result.n_connections
500
>>> codes = result.pre_indices * 100 + result.post_indices
>>> np.unique(codes).size, bool(np.any(result.pre_indices == result.post_indices))
(500, False)
generate(pre_size, post_size, pre_positions=None, post_positions=None, **kwargs)[source]#

Generate the fixed-total-number connections.

Return type:

ConnectionResult

property n: int | float#

The number of edges as given (int count or fraction).