FixedNumConn#
- class brainevent.FixedNumConn(*args, shape, backend=None, buffers=None)[source]#
Unified, layout-aware sparse matrix with a fixed number of connections.
A
FixedNumConnrepresents a single logical weight matrixWof shape(num_pre, num_post)stored row-major in fixed-connection ELL format (data/indices). The concrete subclasses fix the orientation:FixedNumPerPre(≡brainevent.CSR): each pre-synaptic neuron has a fixed number of outgoing connections;indicesare post-synaptic ids.FixedNumPerPost(≡brainevent.CSC): each post-synaptic neuron has a fixed number of incoming connections;indicesare pre-synaptic ids.
Event-driven matrix-vector products follow the same favorable/unfavorable dispatch as
brainevent.CSR/brainevent.CSC. When the event vector indexes the ELL stored axis the product is a direct column-scatter (brainevent._fcn.binary.binary_fcnmv()withtranspose=True). Otherwise the product would require a gather over every stored synapse; instead the structure is converted once to a column-major (CSC) view –(indptr, indices, perm)built bybrainevent._misc.fixed_conn_num_csc_structure()– and the reused, perm-fused CSR kernel (brainevent.binary_csrmv_indexed()) readsdata[perm[j]]so only active columns are touched. The CSC view (the mirror) is built lazily on first need from concrete indices and shared by every matrix with the same structure, including the transpose. The matrix is a JAX pytree with leavesdata,indicesand, once built, the three mirror arrays, so it can be constructed and transformed insidejax.jit. A matrix whose indices are traced and that carries no mirror computes the unfavorable product with a gather instead, and warns once withbrainevent.PerformanceWarning; callbuild_weight_indices()beforejax.jitto keep it event-driven. Differentiate with respect toM.dataand rebuild withwith_data().- Parameters:
See also
FixedNumPerPreConcrete subclass for fixed post-synaptic connections.
FixedNumPerPostConcrete subclass for fixed pre-synaptic connections.
- apply(fn)[source]#
Apply a function while keeping the connectivity structure.
- Parameters:
fn (callable) – Function applied once to
self.data. It may change dtype or physical unit but must preserve the physical buffer shape.- Returns:
Same concrete fixed-connection representation with transformed values.
- Return type:
- Raises:
ValueError – If the transformed value buffer does not have the same shape as
self.data.
- apply2(other, fn, *, reverse=False)[source]#
Apply a binary function while preserving fixed-connectivity semantics.
- build_weight_indices()[source]#
Return a matrix sharing these arrays with the CSC mirror built.
Parity with
brainevent.CSR.build_weight_indices(). The mirror drives the event-driven kernels of the unfavorable product direction and plasticity update. It is built lazily on first use when the indices are concrete, so call this beforejax.jitwhen the matrix enters the jitted function as an argument.- Returns:
A matrix of the same class sharing
dataandindices, with the mirror cached (and carried as pytree leaves).- Return type:
- dt2t(y_dim_arr, w_dim_arr)[source]#
Per-synapse
w * ywithyindexed by the row (pre) ofW.For every stored connection, returns
w * y[row]whererowis the pre-synaptic index of that connection, regardless of storage axis. This is the fixed-connection analog ofbrainevent.CSR.dt2t()and implements thedt2tprotocol ofbrainevent.DataRepresentation.- Parameters:
y_dim_arr (
Array|ndarray|Quantity) – Pre-synaptic (row) vector, sizedshape[0].w_dim_arr (
Array|ndarray|Quantity) – Per-synapse weights of shapeindices.shape(or size-1).
- Returns:
Per-synapse result of shape
self.indices.shape.- Return type:
Array|Quantity
See also
dt2t_transposedyindexed by the column (post) ofW.
- dt2t_transposed(y_dim_arr, w_dim_arr)[source]#
Per-synapse
w * ywithyindexed by the column (post) ofW.Adjoint counterpart of
dt2t(): for every stored connection, returnsw * y[col]wherecolis the post-synaptic index of that connection, regardless of storage axis.- Parameters:
y_dim_arr (
Array|ndarray|Quantity) – Post-synaptic (column) vector, sizedshape[1].w_dim_arr (
Array|ndarray|Quantity) – Per-synapse weights of shapeindices.shape(or size-1).
- Returns:
Per-synapse result of shape
self.indices.shape.- Return type:
Array|Quantity
See also
dt2tyindexed by the row (pre) ofW.
- register_buffer(name, value=None)[source]#
Store the mirror under its legacy name
'csc'(seebuffers).
- sum(axis=None)[source]#
Return the sum of all logically represented matrix entries.
- Parameters:
axis (int, sequence of int, or None, optional) – Axis or axes to reduce. Only
Noneis currently supported.- Returns:
Scalar sum of every fixed connection. A homogeneous size-one value is counted once per represented connection.
- Return type:
jax.Array or brainunit.Quantity
- Raises:
NotImplementedError – If
axisis notNone.
- tocoo()[source]#
Convert to coordinate (COO) format.
Builds the COO of the logical matrix
Wof shape(num_pre, num_post)from the stored ELL structure, irrespective of orientation. Promotes the per-subclass_to_coohelper to the common conversion contract. Homogeneous (size-1) weights are broadcast to one entry per stored element.- Returns:
The same logical matrix in COO format,
shapeunchanged.- Return type:
COO
Notes
Building the coordinate layout reads concrete indices, so – like
tocsr()– it must run outsidejax.jit.
- tocsc()[source]#
Convert to a
brainevent.CSCmatrix of the logical matrixW.The result is a Compressed Sparse Column view of the same logical weight matrix
Wof shape(num_pre, num_post). It is built by transposing to the opposite orientation, taking the CSR view ofW^T, and reinterpreting it as the (array-identical) CSC view ofW– so the same outside-jitrequirement astocsr()applies.- Returns:
Equivalent matrix in CSC format with the same
shape,dtype, unit, andbackendasself.- Return type:
See also
tocsrConvert to Compressed Sparse Row format.
todenseConvert to a dense matrix.
- tocsr()[source]#
Convert to a
brainevent.CSRmatrix of the logical matrixW.The result is a Compressed Sparse Row view of the same logical weight matrix
Wof shape(num_pre, num_post), irrespective of the storage orientation (FixedNumPerPreorFixedNumPerPost). Duplicate connections are preserved as repeated entries within a row (CSR matmul /CSR.todense()sum them, matchingtodense()). Homogeneous (size-1) weights are kept as a single shared value.- Returns:
Equivalent matrix in CSR format with the same
shape,dtype, unit, andbackendasself.- Return type:
Notes
Building the CSR layout reorders the stored connections by row, which requires concrete indices. Like the lazy CSC mirror, it must therefore run outside
jax.jit/brainstate.transform.jit; construct the connection and call this method before entering a jitted function.See also
tocscConvert to Compressed Sparse Column format.
todenseConvert to a dense matrix.
Examples
>>> import jax.numpy as jnp >>> from brainevent import FixedNumPerPre >>> >>> data = jnp.array([[1., 2.], [3., 4.]]) >>> indices = jnp.array([[0, 1], [1, 2]]) >>> mat = FixedNumPerPre((data, indices), shape=(2, 3)) >>> csr = mat.tocsr() >>> bool((csr.todense() == mat.todense()).all()) True
- classmethod tree_unflatten(aux_data, children)[source]#
Reconstruct without validation (leaves may be tracers or placeholders).
- update_on_post(pre_trace, post_spike, w_min=None, w_max=None)[source]#
Apply a post-spike-triggered STDP update, returning a new matrix.
For each firing post neuron
jevery stored synapse is updatedW[i, j] <- clip(W[i, j] + pre_trace[i], w_min, w_max). Unfavorable forFixedNumPerPre, favorable (row-driven) forFixedNumPerPost. Concrete behavior is defined per subclass.- Parameters:
pre_trace (jax.Array or Quantity) – Pre-synaptic trace, shape
(shape[0],).post_spike (jax.Array) – Post-synaptic spikes, shape
(shape[1],).w_min (jax.Array, Quantity, number, or None, optional) – Clip bounds;
Nonedisables the corresponding bound.w_max (jax.Array, Quantity, number, or None, optional) – Clip bounds;
Nonedisables the corresponding bound.
- Returns:
A new matrix of the same subclass with updated data and identical structure.
- Return type:
See also
update_on_prePre-spike-triggered counterpart.
- update_on_pre(pre_spike, post_trace, w_min=None, w_max=None)[source]#
Apply a pre-spike-triggered STDP update, returning a new matrix.
For each firing pre neuron
ievery stored synapse is updatedW[i, j] <- clip(W[i, j] + post_trace[j], w_min, w_max). Favorable (row-driven) forFixedNumPerPre, unfavorable forFixedNumPerPost. Concrete behavior is defined per subclass.- Parameters:
pre_spike (jax.Array) – Pre-synaptic spikes, shape
(shape[0],).post_trace (jax.Array or Quantity) – Post-synaptic trace, shape
(shape[1],).w_min (jax.Array, Quantity, number, or None, optional) – Clip bounds;
Nonedisables the corresponding bound.w_max (jax.Array, Quantity, number, or None, optional) – Clip bounds;
Nonedisables the corresponding bound.
- Returns:
A new matrix of the same subclass with updated data and identical structure.
- Return type:
See also
update_on_postPost-spike-triggered counterpart.