nstat.extras.latents.gpfa_bridge
Gaussian-Process Factor Analysis bridge via Elephant.
Wraps elephant.gpfa.GPFA (Yu et al. 2009, J. Neurophysiol.
102(1)) — smooth low-dimensional latent trajectories inferred from
simultaneous multi-trial spike trains.
Scope
GPFAConfig— frozen dataclass capturing the fit hyper-parameters.fit_gpfa()— main entry point. Acceptslist[Trial](native nstat) orlist[list[neo.SpikeTrain]](Elephant’s native shape).GPFAResult— frozen dataclass with per-trial latent trajectories(n_bins, x_dim)plus the underlyingelephant.gpfa.GPFAinstance (exposed so callers can re-transform()held-out data).
GPFA has no MATLAB nSTAT counterpart; this is a pure Python-only
extension and is opt-in via pip install nstat-toolbox[latents].
Install
pip install nstat-toolbox[latents]
Pulls Elephant (>=1.2) + Neo + quantities.
Functions
|
Fit a GPFA model to multi-trial spike data. |
Classes
|
Configuration for a GPFA fit. |
|
Fitted GPFA model and latent trajectories. |
- class nstat.extras.latents.gpfa_bridge.GPFAConfig(x_dim, bin_size_s=0.02, em_max_iter=500, em_tol=1e-08, min_var_frac=0.01)[source]
Bases:
objectConfiguration for a GPFA fit.
- Parameters:
x_dim (int) – Number of latent dimensions to recover.
bin_size_s (float) – Discretisation bin size in seconds. Default 0.02 (20 ms — the Yu et al. 2009 default).
em_max_iter (int) – Maximum EM iterations. Default 500.
em_tol (float) – Relative log-likelihood tolerance for convergence. Default 1e-8.
min_var_frac (float) – Minimum variance per latent dimension (numerical stability floor). Default 0.01.
- class nstat.extras.latents.gpfa_bridge.GPFAResult(latent_trajectories, x_dim, bin_size_s, n_trials, log_likelihood, elephant_model)[source]
Bases:
objectFitted GPFA model and latent trajectories.
- Variables:
latent_trajectories (list of (n_time_bins, x_dim) ndarrays) – Per-trial latent trajectories. Axis order is
(time, latent)— the nstat population-data convention. Elephant’s native order is(latent, time); this bridge transposes for consistency with other nstat extras.x_dim (int) – Number of recovered latent dimensions.
bin_size_s (float) – Bin size used for fitting (seconds).
n_trials (int) – Number of trials the model was fit on.
log_likelihood (float or None) – Final EM log-likelihood, or
Noneif elephant did not surface a finite value. (Elephant reports the LL only everyfreq_lliterations; intermediate entries areNaN— the bridge picks the last finite entry.)elephant_model (object) – The underlying
elephant.gpfa.GPFAfitted instance, exposed so callers can re-transform()held-out data viaresult.elephant_model.transform(new_spiketrains).
- Parameters:
- nstat.extras.latents.gpfa_bridge.fit_gpfa(spike_trains, *, config=None, seed=None)[source]
Fit a GPFA model to multi-trial spike data.
- Parameters:
spike_trains (Any) – Either a
list[Trial]of native nstat trial objects (each carrying annspikeCollSpikeTrainCollection), or alist[list[neo.SpikeTrain]]matching Elephant’s native input shape. Any other shape raisesTypeError.config (GPFAConfig | None) – Fit configuration.
Nonedefaults toGPFAConfig(x_dim=3)(the Elephant default).seed (int | None) – Optional integer seed. When provided, the legacy numpy global seed is temporarily set so the FA-init + EM fit are reproducible; the caller’s previous RNG state is restored on exit.
- Return type:
- Raises:
TypeError – If
spike_trainsis not one of the accepted shapes.ValueError – If
len(spike_trains) < 2— GPFA’s EM covariance estimation is degenerate on a single trial (Yu et al. 2009 §2.3).ImportError – If the
elephant/neo/quantitiesoptional dependency is missing. The error names the install command.
Notes
Elephant’s
elephant.gpfa.GPFAconsumes the module-level numpy random state (norng=parameter). Whenseedis provided, this bridge wraps the fit innp.random.get_state()/np.random.set_state()so reproducibility works AND the caller’s RNG context is preserved — a deliberate, isolated exception to the “default_rng-only” convention.