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
brainunitlibrary. 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.SparseMatrixare 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:
- 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
operatoror a compatible callable.reverse (
bool) – If False, computefn(self, other)via_binary_op. If True, computefn(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 directdt2timplementation.
- 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 directdt2t_transposedimplementation.
- property event_side: str#
Side of the event operand whose product is event-driven.
'left'(corder=True):events @ Mwalks only the active rows.'right'(corder=False):M @ eventswalks only the active columns. The product with events on the other side walks every random stream.M.Tflips the value, soevents @ MandM.T @ eventsalways run the same kernel.- Returns:
'left'or'right'.- Return type:
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,dt2tanddt2t_transposeddraw the connectivity fromprobandseedat trace time, so underjax.jitthey need the matrix closed over (concreteprob/seed), and the connectivity is embedded in the compiled program as constants. A prepared matrix instead carries it as leaves: passed intojax.jitas an argument, it is an input of the program (not a constant), andprob/seedmay 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;.Tand operations that changeproborseeddrop it. Do not changeproborseedof 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:
- Raises:
ValueError – If
proborseedis 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(), orNone.- 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],)and1(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
axisis notNone,0,1,-1or-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 oftocsr().- Return type:
brainunit.sparse.COO
See also
tocsrDirect count+fill materialisation to CSR.
tocscConvert 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 oftocsr().- Return type:
See also
tocsrDirect count+fill materialisation to CSR.
tocooConvert 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(...).