Conv2dKernel

Conv2dKernel#

class braintools.conn.Conv2dKernel(kernel, kernel_size, threshold=0.0, allow_self_connections=False, weight=None, delay=None, **kwargs)#

Convolutional kernel connectivity for spatially arranged point neurons.

Applies a 2D convolution kernel to neuron positions, creating connections where the kernel weight magnitude exceeds a threshold. This allows implementing receptive field structures in spiking neural networks.

Only the first two position dimensions (x, y) are used; any extra columns (e.g. z) are ignored. kernel_size is the physical support of the kernel along BOTH axes: the kernel rows are mapped onto the y extent and the kernel columns onto the x extent, each bounded independently so non-square kernels cover the correct neighbourhood.

Parameters:
  • kernel (ndarray) – 2D convolution kernel array.

  • kernel_size (Array | ndarray | bool | number | bool | int | float | complex | Quantity) – Physical size of the kernel support (along both axes) in position units.

  • threshold (float) – Connection threshold - only kernel values whose magnitude exceeds this create connections. Thresholding on magnitude keeps essential negative coefficients (e.g. for edge-detection kernels). Default 0.0.

  • allow_self_connections (bool) – Whether a neuron may connect to itself when pre and post are the same population and coincide with the kernel centre (default: False).

  • weight (Initialization | float | int | ndarray | Array | Quantity | None) – Weight initialization (kernel values are multiplied by this).

  • delay (Initialization | float | int | ndarray | Array | Quantity | None) – Delay initialization.

Notes

Candidate pairs are found with a k-d tree over the positions and handled in chunks of bounded size, so memory grows with the number of edges rather than with pre_size * post_size. The edges, their order and the weights are the same as in version 0.3 (up to rounding). Generation runs on the host with NumPy and cannot be traced by jax.jit, jax.vmap or jax.grad.

Examples

>>> import brainunit as u
>>> import numpy as np
>>> from braintools.conn import Conv2dKernel
>>> # Create 5x5 Gaussian-like kernel
>>> kernel = np.array([
...     [0.04, 0.12, 0.18, 0.12, 0.04],
...     [0.12, 0.37, 0.56, 0.37, 0.12],
...     [0.18, 0.56, 1.00, 0.56, 0.18],
...     [0.12, 0.37, 0.56, 0.37, 0.12],
...     [0.04, 0.12, 0.18, 0.12, 0.04]
... ])
>>> positions = np.random.RandomState(0).uniform(0, 1000, (500, 2)) * u.um
>>> conn = Conv2dKernel(
...     kernel=kernel,
...     kernel_size=100 * u.um,
...     threshold=0.1,
...     weight=1.0 * u.nS
... )
>>> result = conn(
...     pre_size=500, post_size=500,
...     pre_positions=positions, post_positions=positions
... )
>>> result.n_connections
2230
generate(pre_size, post_size, pre_positions=None, post_positions=None, **kwargs)[source]#

Generate convolutional kernel connections.

Return type:

ConnectionResult