KalMax: Kalman based neural decoding in Jax
KalMax = Kalman smoothing of Maximum likelihood estimates in Jax.
You provide $\mathbf{S} \in \mathbb{N}^{T \times N}$ (spike counts) and $\mathbf{X} \in \mathbb{R}^{T \times D}$ (a continuous variable, e.g. position) and KalMax provides jax-optimised functions and classes for:
- Fitting rate maps using kernel density estimation (KDE)
- Calculating likelihood maps $p(\mathbf{s}_t|\mathbf{x})$
- Kalman filter / smoother
Why are these functionalities combined into one package?...
Because Likelihood Estimation + Kalman filtering = Powerful neural decoding. By Kalman filtering/smoothing the maximum likelihood estimates (as opposed to the spikes themselves) we bypass the issues of naive Kalman filters (spikes are rarely linearly related to position) and maximum likelihood decoding (which does not account for temporal continuity in the trajectory), outperforming both for no extra computational cost.
Core KalMax functions are optimised and jit-compiled in jax making them very fast. For example KalMax kalman filtering is >13 times faster than an equivalent numpy implementation by the popular pykalman library (see demo).
Install
pip install kalmax
Development install
git clone https://github.com/TomGeorge1234/KalMax.git
cd KalMax
pip install -e ".[dev]" # installs with test/lint dependencies
To run tests and linting:
pytest # run test suite
ruff check src/ # lint
ruff format --check src/ # check formatting
Usage
A full demo is provided in
examples/kalmax_demo.ipynb. Pseudo-code is provided below.
import kalmax
import jax.numpy as jnp
# 0. PREPARE DATA IN JAX ARRAYS
S_train = jnp.array(...) # (T, N_CELLS) train spike counts
Z_train = jnp.array(...) # (T, DIMS) train continuous variable
S_test = jnp.array(...) # (T_TEST, N_CELLS) test spike counts
bins = jnp.array(...) # (N_BINS, DIMS) coordinates at which to estimate receptive fields / likelihoods)
# 1. FIT RECEPTIVE FIELDS using kalmax.kde
firing_rate = kalmax.kde.kde(
bins = bins,
trajectory = Z_train,
spikes = S_train,
kernel = kalmax.kernels.gaussian_kernel,
kernel_bandwidth = 0.01,
) # --> (N_CELLS, N_BINS)
# 2.1 CALCULATE LIKELIHOODS using kalmax.poisson_log_likelihood
log_likelihoods = kalmax.kde.poisson_log_likelihood(
spikes = S_test,
mean_rate = firing_rate,
) # --> (T_TEST, N_BINS)
# 2.2 FIT GAUSSIAN TO LIKELIHOODS using kalmax.utils.fit_gaussian
MLE_means, MLE_modes, MLE_covs = kalmax.utils.fit_gaussian_vmap(
x = bins,
likelihoods = jnp.exp(log_likelihoods),
) # --> (T_TEST, DIMS), (T_TEST, DIMS, DIMS)
# 3. KALMAN FILTER / SMOOTH using kalmax.KalmanFilter
kalman_filter = kalmax.KalmanFilter(
dim_Z = DIMS,
dim_Y = DIMS,
# SEE DEMO FOR HOW TO FIT/SET THESE
F=F, # state transition matrix
Q=Q, # state noise covariance
H=H, # observation matrix
R=R, # observation noise covariance
)
# [FILTER]
mus_f, sigmas_f = kalman_filter.filter(
Y = Y,
mu0 = mu0,
sigma0 = sigma0,
) # --> (T, DIMS), (T, DIMS, DIMS)
# [SMOOTH]
mus_s, sigmas_s = kalman_filter.smooth(
mus_f = mus_f,
sigmas_f = sigmas_f,
) # --> (T, DIMS), (T, DIMS, DIMS)
Metadata
Release files for kalmax 0.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| kalmax-0.3.0.tar.gz | 24.9 kB | Without provenance | Details for kalmax-0.3.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| kalmax-0.3.0-py3-none-any.whl | Python 3 | none | any | Details for kalmax-0.3.0-py3-none-any.whl |
Total release size: 42.6 kB
Release files / kalmax-0.3.0.tar.gz
| Download URL | kalmax-0.3.0.tar.gz |
|---|---|
| Size | 24.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
02ddd649ca80ef43a1ddd04a1325be2649282c40bc8253e500a15dbfe5836f2e
|
|
BLAKE2b-256 checksum How to use checksums |
3904d09747f91a699d8a0f267312f56e67db7240369b8cea9195829803dd5be1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.5
|
Release files / kalmax-0.3.0-py3-none-any.whl
| Download URL | kalmax-0.3.0-py3-none-any.whl |
|---|---|
| Size | 17.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
b0b6a87551fca0dadc60e5095fa187abf16edb1191389ec9663930795c2bca75
|
|
BLAKE2b-256 checksum How to use checksums |
a7b38352e925f1d8b9529b2415ea71bb7d1fa8587678b272f6def483ec1c071b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.5
|
Security for kalmax 0.3.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.