WeightedArray#

class brainevent.WeightedArray(value)#

Array wrapper for value-weighted (count or graded) events.

WeightedArray represents a mostly-zero array whose non-zero entries carry a value, for example the number of spikes a neuron emitted in one step. Matrix multiplication with @ is exact (x @ W) but only visits rows/columns where the event is non-zero.

Parameters:

value (array_like) – Event values. Integer (int8, uint8, int16, int32) or floating-point dtypes; signed values are allowed. Boolean arrays are accepted and behave like BinaryArray.

See also

BinaryArray

Binary events (the value is not used).

Notes

Given an event vector x of shape (k,) and a weight matrix W of shape (k, n), x @ W computes

y[j] = sum_{i : x[i] != 0} x[i] * W[i, j]

Unlike BinaryArray, the value is not binarised, and the derivatives are exact:

  • d y / d W[i, j] = x[i];

  • d y / d x[i] = W[i, :] (integer events get float0 cotangents).

WeightedArray is not a subclass of BinaryArray.

Examples

>>> import jax.numpy as jnp
>>> import brainevent as be
>>> counts = be.WeightedArray(jnp.array([0, 2, 1], dtype=jnp.int8))
>>> W = jnp.array([[1., 2.], [3., 4.], [5., 6.]])
>>> counts @ W
Array([11., 14.], dtype=float32)
property T#

Transpose of the underlying array.

Returns:

The transposed raw array (not wrapped in WeightedArray).

Return type:

jax.Array

transpose(*axes)[source]#

Return the underlying array with axes permuted.

Parameters:

*axes (int, optional) – Axis permutation. If omitted, reverses the axis order.

Returns:

The transposed raw array (not wrapped in WeightedArray).

Return type:

jax.Array

tree_flatten()[source]#

Flatten this instance for JAX PyTree serialisation.

Returns:

  • children (tuple) – (value,).

  • aux_data (dict) – Empty dictionary (no static metadata).

classmethod tree_unflatten(aux_data, flat_contents)[source]#

Reconstruct a WeightedArray from its PyTree representation.

Parameters:
  • aux_data (dict) – Static metadata produced by tree_flatten.

  • flat_contents (tuple) – Dynamic leaves, the underlying array.

Returns:

A new instance wrapping the given array.

Return type:

WeightedArray