JITCMatrix#

class brainevent.JITCMatrix(*args, shape, buffers=None)#

Just-in-time Connectivity (JITC) matrix.

A base class for just-in-time connectivity matrices that inherits from the SparseMatrix class in the brainunit library. This class serves as an abstraction for sparse matrices that are generated or computed on demand rather than stored in full.

JITC matrices are particularly useful in neural network simulations where connectivity patterns might be large but follow specific patterns that can be efficiently computed rather than explicitly stored in memory.

Notes

This is a base class and should be subclassed for specific implementations of JITC matrices. All attributes from brainunit.sparse.SparseMatrix are inherited.

apply(fn)[source]#

Apply a function to matrix value parameters while keeping structure.

Parameters:

fn (callable) – Unary callable applied by subclasses to their value parameters.

Returns:

A new matrix-like object with transformed values.

Return type:

JITCMatrix

apply2(other, fn, *, reverse=False)[source]#

Apply a binary function with consistent sparse-matrix semantics.

Parameters:
  • other (Any) – Right-hand operand for normal operations, or left-hand operand when reverse=True.

  • fn (callable) – Binary function from operator or a compatible callable.

  • reverse (bool) – If False, compute fn(self, other) via _binary_op. If True, compute fn(other, self) via _binary_rop. Defaults to False.

Returns:

Result of the operation.

Return type:

JITCMatrix or Any

dt2t(y_dim_arr, w_dim_arr)[source]#

Unsupported fallback for JITC subclasses without direct dt2t.

Parameters:
  • y_dim_arr (Array | ndarray | Quantity) – Values in the source vector dimension.

  • w_dim_arr (Array | ndarray | Quantity) – Per-synapse weight values.

Returns:

Per-synapse result for subclasses that implement this protocol.

Return type:

Array | Quantity

Raises:

UnsupportedOperationError – If the subclass does not override this method. Materialise first with mat.tocsr().dt2t(...) or use a subclass with a direct dt2t implementation.

dt2t_transposed(y_dim_arr, w_dim_arr)[source]#

Unsupported fallback for JITC subclasses without direct transposed dt2t.

Parameters:
  • y_dim_arr (Array | ndarray | Quantity) – Values in the transposed vector dimension.

  • w_dim_arr (Array | ndarray | Quantity) – Per-synapse weight values.

Returns:

Per-synapse result for subclasses that implement this protocol.

Return type:

Array | Quantity

Raises:

UnsupportedOperationError – If the subclass does not override this method. Materialise first with mat.tocsr().dt2t_transposed(...) or use a subclass with a direct dt2t_transposed implementation.

property event_side: str#

Side of the event operand whose product is event-driven.

'left' (corder=True): events @ M walks only the active rows. 'right' (corder=False): M @ events walks only the active columns. The product with events on the other side walks every random stream. M.T flips the value, so events @ M and M.T @ events always run the same kernel.

Returns:

'left' or 'right'.

Return type:

str

Examples

>>> import brainevent
>>> M = brainevent.JITCScalarR((1.5, 0.1, 42), shape=(10, 20))
>>> M.event_side
'left'
>>> M.T.event_side
'right'
classmethod fromdense(dense, **kwargs)[source]#

Unsupported: JITC connectivity cannot be recovered from a dense matrix.

Raises:

UnsupportedOperationError – Always. The generating (prob, seed) cannot be inferred from a materialised matrix.

prepare_structure()[source]#

Return a copy of the matrix that carries its connectivity as pytree leaves.

tocsr, dt2t and dt2t_transposed draw the connectivity from prob and seed at trace time, so under jax.jit they need the matrix closed over (concrete prob / seed), and the connectivity is embedded in the compiled program as constants. A prepared matrix instead carries it as leaves: passed into jax.jit as an argument, it is an input of the program (not a constant), and prob / seed may be traced.

Closing over a matrix embeds the connectivity in the program: compiling is slow for large matrices (seconds at a few million connections), but XLA then optimizes the gathers over the constant indices. A prepared matrix passed as an argument compiles in a fraction of that time and runs the gathers over input indices, which can be slower per call.

The structure costs O(nnz) device memory. Weight-only operations (with_data, scaling, …) keep it; .T and operations that change prob or seed drop it. Do not change prob or seed of a prepared matrix by other means: the stored connectivity would no longer match.

Returns:

A copy holding the structure, with a concrete seed.

Return type:

JITCMatrix

Raises:

ValueError – If prob or seed is traced.

Examples

>>> import jax
>>> import jax.numpy as jnp
>>> import brainevent
>>> M = brainevent.JITCNormalR((1.0, 0.1, 0.2, 42), shape=(100, 50)).prepare_structure()
>>> f = jax.jit(lambda m, y: m.dt2t(y, None))
>>> f(M, jnp.ones(100)).shape == (M.structure.nnz,)
True
property structure#

Connectivity stored by prepare_structure(), or None.

Returns:

The canonical CSR connectivity carried as pytree leaves.

Return type:

JITCStructure or None

sum(axis=None)[source]#

Sum of the represented matrix entries.

Computed exactly from the generated connectivity with one matrix-vector product, without materializing the matrix.

Parameters:

axis (int | None) – None (default) sums every entry, 0 (or -2) gives the column sums of shape (shape[1],) and 1 (or -1) the row sums of shape (shape[0],).

Returns:

The sum, carrying the unit of the weights.

Return type:

jax.Array or u.Quantity

Raises:

ValueError – If axis is not None, 0, 1, -1 or -2.

Examples

>>> import jax.numpy as jnp
>>> import brainevent
>>> mat = brainevent.JITCScalarR((1.5, 0.5, 42), shape=(4, 6))
>>> bool(jnp.allclose(mat.sum(), mat.todense().sum()))
True
>>> mat.sum(axis=0).shape
(6,)
tocoo()[source]#

Convert to COO by materialising through tocsr().

Returns:

The same logical matrix in COO format. Eager-only (O(nnz)), inheriting the tracing restriction of tocsr().

Return type:

brainunit.sparse.COO

See also

tocsr

Direct count+fill materialisation to CSR.

tocsc

Convert to compressed sparse column format.

tocsc()[source]#

Convert to CSC by materialising through tocsr().

Returns:

The same logical matrix in CSC format. Eager-only (O(nnz)), inheriting the tracing restriction of tocsr().

Return type:

CSC

See also

tocsr

Direct count+fill materialisation to CSR.

tocoo

Convert to coordinate format.

update_on_post(*args, **kwargs)[source]#

Unsupported: JITC connectivity has no per-synapse plastic weight.

Raises:

UnsupportedOperationError – Always. Materialise first with mat.tocsr().update_on_post(...).

update_on_pre(*args, **kwargs)[source]#

Unsupported: JITC connectivity has no per-synapse plastic weight.

Raises:

UnsupportedOperationError – Always. Materialise first with mat.tocsr().update_on_pre(...).