JITCScalarMatrix#
- class brainevent.JITCScalarMatrix(weight, prob=None, seed=None, *, shape, corder=False, backend=None, buffers=None)#
Base class for Just-In-Time Connectivity Scalar-weight matrices.
This abstract class serves as the foundation for sparse matrix representations that use homogeneous (scalar) weights with stochastic connectivity patterns. It stores a single weight value applied to all non-zero elements, along with connectivity probability and a random seed that determines the sparse structure.
- Parameters:
weight (WeightScalar or Tuple[WeightScalar, Prob, Seed]) – Either the homogeneous weight value for all non-zero elements, or a tuple containing
(weight, prob, seed).prob (Prob, optional) – Connection probability determining matrix sparsity.
seed (Seed, optional) – Random seed for reproducible sparse structure generation.
shape (
Tuple[int,int]) – The shape of the matrix as a tuple(rows, columns).corder (
bool) – Memory layout order flag, by default False.
- Returns:
A new scalar-weight JIT connectivity matrix instance.
- Return type:
- Raises:
ValueError – If
probis not a scalar, is not finite, or is outside[0, 1].
See also
JITCScalarRRow-oriented concrete subclass.
JITCScalarCColumn-oriented concrete subclass.
JITCMatrixParent class for all JIT connectivity matrices.
Notes
The matrix
Wis defined by a scalar weightw, a connection probabilityp, and a deterministic pseudo-random seeds. Each element is given by:W[i, j] = w * B[i, j]where
B[i, j] ~ Bernoulli(p)is a binary mask whose realization is fully determined by the seeds. Specifically, the mask is generated by a deterministic hash-based PRNG that, for a given(i, j, s)triple, always produces the same binary outcome. This means:The same
(weight, prob, seed, shape)always produces the identical matrix.The expected number of non-zeros per row is
p * n_cols.The matrix is never materialized in memory; it is regenerated on-the-fly during each operation (matvec, matmat, todense).
The connection length parameter
clen = 2 / pcontrols the average stride between successive non-zero entries in the sampling loop.Examples
>>> from brainevent import JITCScalarR >>> mat = JITCScalarR((0.5, 0.1, 42), shape=(100, 50)) >>> mat.weight # 0.5 >>> mat.prob # 0.1 >>> mat.seed # 42
- weight#
The homogeneous weight value applied to all non-zero elements in the matrix. Can be a plain JAX array or a quantity with units.
- Type:
Union[jax.Array, u.Quantity]
- prob#
Connection probability determining the sparsity of the matrix. Values range from 0 (no connections) to 1 (fully connected).
- Type:
Union[float, jax.Array]
- seed#
Random seed controlling the specific pattern of connections. Using the same seed produces identical connectivity patterns.
- Type:
Union[int, jax.Array]
- shape#
Tuple specifying the dimensions of the matrix as (rows, columns).
- Type:
MatrixShape
- corder#
Flag indicating the memory layout order of the matrix. False (default) for Fortran-order (column-major), True for C-order (row-major).
- Type:
- backend#
The computation backend to use (e.g.,
'numba','pallas'). IfNone, the default backend is selected automatically.- Type:
str or None
- property data: Number | ndarray | Array | Quantity#
Return the trainable weight of the matrix.
Only the trainable value parameter is exposed here. The structural parameters
probandseedare non-trainable and are therefore excluded. This property mirrorswith_data(), which accepts exactly the value returned here, somat.with_data(mat.data)round-trips.- Returns:
The homogeneous weight value for the non-zero elements.
- Return type:
WeightScalar
See also
with_dataRebuild the matrix from the value returned here.
- dt2t(y_dim_arr, w_dim_arr)[source]#
Generate per-synapse
weight * y[row]using the matrix parameters.w_dim_arris required by theDataRepresentationprotocol and is not used. JITC scalar connectivity and weights are generated from this matrix’s own metadata, includingweight,prob,seed,shape,corder, andbackend.- Return type:
Array|Quantity
- dt2t_transposed(y_dim_arr, w_dim_arr)[source]#
Generate per-synapse
weight * y[col]using the matrix parameters.w_dim_arris required by theDataRepresentationprotocol and is not used. JITC scalar connectivity and weights are generated from this matrix’s own metadata, includingweight,prob,seed,shape,corder, andbackend.- Return type:
Array|Quantity
- property dtype#
Get the data type of the matrix elements.
- Returns:
The data type of the weight values in the matrix.
- Return type:
Notes
This property inherits the dtype directly from the weight attribute, ensuring consistent data typing throughout operations involving this matrix.
- tocsr()[source]#
Convert the sparse scalar-weight matrix to Compressed Sparse Row (CSR) format.
Generates the non-zero structure
(data, indices, indptr)directly from the connectivity parameters using dedicated CPU/CUDA operators, without ever materializing the dense matrix. The resultingCSRreproduces exactly the same matrix astodense()for the active compute backend; every stored value equals the constant weightw.- Returns:
A
CSRmatrix with the same shape and values astodense(). The data type matches the weight, and physical units (brainunit.Quantity) are preserved on the stored values.- Return type:
See also
todenseMaterialize the matrix as a dense array.
Notes
Generation uses a count pass followed by a fill pass; the number of stored elements is read back between the two passes, so
tocsris an eager-only conversion and cannot be traced underjax.jit. Peak memory isO(nnz)rather thanO(rows * cols).Examples
>>> from brainevent import JITCScalarR >>> mat = JITCScalarR((1.5, 0.2, 42), shape=(10, 10)) >>> csr = mat.tocsr() >>> csr.shape (10, 10)
- tree_flatten()[source]#
Flatten the matrix into leaves and auxiliary data for JAX pytree registration.
- Returns:
A 2-tuple where the first element is a tuple of JAX-traceable leaves
(weight, prob, seed)and the second element is a dict of static auxiliary data{'shape': ..., 'corder': ..., 'backend': ...}.- Return type:
See also
tree_unflattenReconstruct the matrix from flattened representation.
- classmethod tree_unflatten(aux_data, children)[source]#
Reconstruct a matrix instance from flattened pytree data.
- Parameters:
- Returns:
A reconstructed matrix instance with attributes restored from both
childrenandaux_data.- Return type:
See also
tree_flattenFlatten the matrix for JAX pytree operations.
- with_data(data)[source]#
Create a new matrix instance with updated weight, preserving all other structure.
Accepts exactly the value returned by
data(the trainable weight), while keeping the sameprob,seed,shape,corder,backend, and buffers. It is useful for updating the weight without changing the connectivity pattern.- Parameters:
data (
Number|ndarray|Array|Quantity) – The new weight value to use. Must have the same shape and unit as the current weight.- Returns:
A new matrix instance of the same concrete type with the updated weight.
- Return type:
- Raises:
AssertionError – If the provided weight has a different shape or unit than the current weight.
See also
dataProperty returning the value accepted here.