Grid2d

Grid2d#

class braintools.conn.Grid2d(connectivity='von_neumann', weight=None, delay=None, periodic=False, **kwargs)#

Two-dimensional grid connectivity for spatially arranged neural populations.

This class creates connections on a 2D regular lattice where each neuron connects to its immediate spatial neighbors. This pattern is commonly used to model cortical sheets, retinal ganglion cell arrays, or any neural population with regular 2D spatial organization.

Two types of neighborhoods are supported:

  • Von Neumann neighborhood (4 neighbors): Connects to orthogonal neighbors (up, down, left, right)

  • Moore neighborhood (8 neighbors): Connects to all 8 surrounding neighbors (orthogonal + diagonal)

The connectivity pattern respects or wraps around grid boundaries depending on the periodic parameter.

Parameters:
  • connectivity (str) –

    Type of neighborhood connectivity:

    • 'von_neumann': 4-neighbor connectivity (orthogonal only)

      · N ·
      W · E
      · S ·
      
    • 'moore': 8-neighbor connectivity (orthogonal + diagonal)

      NW N NE
      W  ·  E
      SW S SE
      

  • weight (Initialization | float | int | ndarray | Array | Quantity | None) –

    Initializer for synaptic weights. Supports:

    • Constant values (e.g., 1.0 * u.nS)

    • Stochastic initializers (e.g., Normal, Uniform)

    • None for unweighted connections

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

    Initializer for synaptic transmission delays. Supports:

    • Constant values (e.g., 1.0 * u.ms)

    • Stochastic delay distributions

    • None for connections without explicit delays

  • periodic (bool) –

    Whether to use periodic boundary conditions:

    • True: Grid wraps around at edges (torus topology)

    • False: Neurons at edges have fewer neighbors

  • **kwargs – Additional arguments passed to PointConnectivity, such as seed for reproducible random number generation.

Returns:

Connection result containing pre/post indices, weights, delays, and metadata about the grid connectivity pattern.

Return type:

ConnectionResult

Raises:

ValueError – If pre_size and post_size are not equal, not 2D tuples, or if connectivity type is invalid.

Notes

  • Grid connectivity requires pre_size == post_size as a 2D tuple (rows, cols)

  • The number of connections per neuron depends on connectivity type and boundary conditions:

    • Von Neumann, non-periodic: 2-4 neighbors (fewer at edges)

    • Von Neumann, periodic: 4 neighbors for all neurons

    • Moore, non-periodic: 3-8 neighbors (fewer at edges/corners)

    • Moore, periodic: 8 neighbors for all neurons

  • Neurons are indexed in row-major order: index = row × n_cols + col

  • Self-connections are excluded

  • Total connections:

    • Von Neumann: ~4 × rows × cols (periodic) or fewer (non-periodic)

    • Moore: ~8 × rows × cols (periodic) or fewer (non-periodic)

See also

Ring

One-dimensional ring connectivity

DistanceDependent

Distance-based connectivity for arbitrary spatial arrangements

Examples

Basic 2D grid with Von Neumann connectivity:

>>> import brainunit as u
>>> from braintools.conn import Grid2d
>>> from braintools.init import Constant
>>>
>>> # 10×10 grid with 4-neighbor connectivity
>>> grid = Grid2d(
...     connectivity='von_neumann',
...     weight=Constant(1.0 * u.nS),
...     periodic=False
... )
>>> result = grid(pre_size=(10, 10), post_size=(10, 10))

Moore neighborhood with periodic boundaries:

>>> # 8-neighbor connectivity with wraparound
>>> moore_grid = Grid2d(
...     connectivity='moore',
...     weight=Constant(1.5 * u.nS),
...     delay=Constant(1.0 * u.ms),
...     periodic=True  # Torus topology
... )
>>> result = moore_grid(pre_size=(20, 20), post_size=(20, 20))
>>> print(f"Connections per neuron: {len(result.pre_indices) // 400}")  # Should be 8

Stochastic weights on grid:

>>> from braintools.init import Normal
>>>
>>> grid = Grid2d(
...     connectivity='von_neumann',
...     weight=Normal(mean=2.0*u.nS, std=0.4*u.nS),
...     delay=Constant(0.5 * u.ms),
...     seed=42
... )

Modeling cortical sheet connectivity:

>>> # Model local connectivity in a cortical layer
>>> cortex_grid = Grid2d(
...     connectivity='moore',  # Neurons connect to all immediate neighbors
...     weight=Normal(mean=1.0*u.nS, std=0.2*u.nS),
...     delay=Constant(1.0 * u.ms),
...     periodic=False  # Cortex has edges, not periodic
... )
>>>
>>> # 50×50 grid representing a cortical microcircuit
>>> result = cortex_grid(pre_size=(50, 50), post_size=(50, 50))
>>>
>>> # Check edge vs. interior connectivity
>>> # Interior neurons have 8 connections, edge neurons have fewer

Retinal ganglion cell lattice with periodic boundaries:

>>> # Model retinotopic connectivity
>>> retina_grid = Grid2d(
...     connectivity='von_neumann',
...     weight=Constant(0.5 * u.nS),
...     periodic=True  # Periodic for theoretical modeling
... )
>>> result = retina_grid(pre_size=(30, 40), post_size=(30, 40))
generate(**kwargs)[source]#

Generate grid connectivity.

Return type:

ConnectionResult