Skip to content

feature / Port of SOMATA python package - #178

Open
tomdstone wants to merge 16 commits into
depasquale-lab:devfrom
tomdstone:feature/somata-port
Open

tomdstone wants to merge 16 commits into
depasquale-lab:devfrom
tomdstone:feature/somata-port

Conversation

@tomdstone

Copy link
Copy Markdown
Contributor

SOMATA is a python package built for state space modeling with Matsuda & Komaki oscillators and autoregressive models, made by the Purdon Lab at Stanford University. This PR is for a claude-implemented port of somata to pure Julia, as an opt-in submodule of StateSpaceDynamics. SOMATA carries its own license, which is included in the source code and a notice of it is printed upon the first use of a somata class / function in a given Julia session (but can be hidden, see instructions in the documentation in src/somata/Somata.jl).

The key features are:

  • Create AbstractLinearGaussianStateModel <: AbstractStateModel to specify linear gaussian in function dispatch
  • Somata.OscillatorStateModel <: AbstractLinearGaussianStateModel
    • Has structured state transition matrix to be damped rotation, state noise covariance to be block-diagonal.
  • Somata.AutoRegressiveStateModel <: AbstractLinearGaussianStateModel
    • Single variable AR(n) gets reformulated as multivariable AR(1).
  • Somata.CompositeStateModel <: AbstractLinearGaussianStateModel
    • For combining structured linear gaussian state models and taking advantage of block-diagonal structure.
  • function Somata.dosc "Decomposed Oscillator Search", a principled algorithm for learning the number of oscillations from data, rather than prespecifying a number

This package allows for faster inference on these subclasses of models, with a significant speedup (2-30x, greater for smaller dimensional state spaces) over the python package. I used these models a ton in my work in an anesthesia EEG lab

rsenne and others added 16 commits July 9, 2026 18:31
Brings the somata port (originally committed on `main`) over to `dev` and
adapts it to the changes dev has accumulated since the branches diverged.

Carried over unchanged:
- `src/somata/` — oscillator, AR(p), composite state models, dOsc search,
  residual diagnostics
- `src/numerics/block_diagonal.jl`, the `IGPrior`/`VMPrior` scalar priors
- `benchmark/somata/`, `test/Somata/`, `docs/examples/Somata.jl`

Adapted to dev:
- `src/stats/kalman.jl` was deleted on dev (depasquale-lab#152); its somata-side edits were
  pure signature widening, so they drop out with the file.
- `AbstractLinearGaussianStateModel` widening re-applied to dev's rewritten
  `fit_LDS.jl` / `fit_PLDS.jl` / `workspaces.jl` / `sufficient_statistics.jl`.
  The four overridable M-step hooks stay narrowed to `GaussianStateModel` so
  they remain the *unconstrained* implementations.
- New `_x0_prior` accessor for dev's `x0_prior` field (depasquale-lab#156):
  `OscillatorStateModel` does not override `update_initial_state_mean!`, so the
  generic update runs for it and must not assume the field exists.
- `_state_logprior` absorbs dev's inline state-prior blocks in both `elbo!`s
  and gains dev's `x0_prior` term.
- `_extract_state_params` widened to the abstract supertype.
- `init_yy` is a raw `Ref{Matrix}` on dev rather than a PDMat, so the two
  somata `_update_P0_block!` kernels read it without `.mat`.
- `sparse_hessian` / `steady_state_tol` re-plumbed through dev's
  `SmoothWorkspace`, plus the pool-size cap on `_fit_tridiag!`.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
- `_td_init_const_blocks!` / `estep!` take a `Data` object on dev, so the three
  `test/Somata` sufficient-statistic helpers build one instead of passing
  `(tsteps, y, ux, uy)` positionally.
- `fit!` input kwargs are `ux` / `uy` on dev, not `latent_inputs` /
  `obs_inputs`; `lds.state_input_dim` is `lds.ux_dim`.
- `_slds_prior_logdensity` read `state_model.Q_prior` and friends directly,
  which throws a `FieldError` for structured regimes. Routed through the
  `_q_prior` / `_p0_prior` / `_x0_prior` / `_ab_prior` accessors.
- Widened `loglikelihood` / `_filter_cov_pass` / `_filter_ll_trial` (dev's
  Kalman marginal-likelihood path, which names its state parameter `SM` and so
  was missed by the first widening pass).
- Retargeted the workspace prior-accessor regression test at `elbo`, since the
  `KalmanWorkspace` it asserted against was retired on dev; extended the
  accessor test to cover `_x0_prior`.
- JuliaFormatter pass (the longer abstract type name rewraps several `where`
  clauses).

Full suite: 2711 pass. The one remaining error is Aqua's "Persistent tasks"
check failing to locate `ChangesOfVariables`, which reproduces identically on
stock dev and is unrelated to this work.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Docs (`julia --project=docs docs/make.jl` now exits 0, doctests included):
- The `@docs` signatures in `api.md` / `LinearDynamicalSystems.md` still named
  `GaussianStateModel`, so seven widened methods no longer matched any listed
  signature and tripped Documenter's `missing_docs` check.
- Four `@ref`s to the structured models sat in *parent*-module docstrings
  (`lds/types.jl`, `stats/priors.jl`), where the unqualified name does not
  resolve — the types register as `Somata.X`. Qualified them, matching the
  existing usage in `numerics/block_tridiagonal.jl`.

Benchmark rerun on this branch against somata 0.5.6 (30 configurations,
25 EM iterations, median of 3 repeats):
- median speedup 8.4x -> 9.0x; every configuration within 0.89-1.23x of its
  previous value, so no regression beyond run-to-run noise.
- the parity metrics match the pre-port run to ~1e-7 relative, i.e. the port is
  numerically unchanged; the documented somata-vs-Julia gaps (the O(1/T)
  boundary convention and the AR `q_floor`) are unaffected.
- the in-process fairness audit reproduces: median 1.074 (was 1.073), range
  1.052-1.102 (was 1.053-1.102).

Results tables in both READMEs updated. `benchmark/somata/results/` is
gitignored, so the regenerated CSVs and figures stay local.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
`dosc` takes `fs` as a keyword, not positionally, so the README snippet threw a
MethodError as written. Pre-existing (the signature changed in cbbfaaf without
the README following), not introduced by the port.

Every numerical claim in that section reproduces exactly on this branch:
`ncomps` = 3, selected frequencies [2.09, 9.99, 22.15] Hz, direct-fit
frequencies [2.09, 9.99, 22.17] Hz and `a` = [0.982, 0.990, 0.950].

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown
Contributor

Benchmark Results (Julia v1)

Time benchmarks
main e3d7907... main / e3d7907...
GaussianLDS/("smooth", "latent=2", "obs=10", "T=100") 0.208 ± 0.0094 ms 0.187 ± 0.014 ms 1.11 ± 0.099
GaussianLDS/("smooth", "latent=2", "obs=10", "T=500") 1 ± 0.028 ms 0.863 ± 0.013 ms 1.16 ± 0.037
GaussianLDS/("smooth", "latent=2", "obs=20", "T=100") 0.228 ± 0.014 ms 0.191 ± 0.0093 ms 1.19 ± 0.094
GaussianLDS/("smooth", "latent=2", "obs=20", "T=500") 1.04 ± 0.049 ms 0.852 ± 0.0079 ms 1.22 ± 0.059
GaussianLDS/("smooth", "latent=2", "obs=5", "T=100") 0.209 ± 0.014 ms 0.179 ± 0.012 ms 1.17 ± 0.11
GaussianLDS/("smooth", "latent=2", "obs=5", "T=500") 1.01 ± 0.022 ms 0.838 ± 0.02 ms 1.2 ± 0.039
GaussianLDS/("smooth", "latent=4", "obs=10", "T=100") 0.415 ± 0.019 ms 0.29 ± 0.0073 ms 1.43 ± 0.074
GaussianLDS/("smooth", "latent=4", "obs=10", "T=500") 1.91 ± 0.036 ms 1.38 ± 0.044 ms 1.38 ± 0.051
GaussianLDS/("smooth", "latent=4", "obs=20", "T=100") 0.43 ± 0.02 ms 0.298 ± 0.018 ms 1.45 ± 0.11
GaussianLDS/("smooth", "latent=4", "obs=20", "T=500") 1.97 ± 0.062 ms 1.4 ± 0.027 ms 1.41 ± 0.052
GaussianLDS/("smooth", "latent=4", "obs=5", "T=100") 0.412 ± 0.013 ms 0.283 ± 0.011 ms 1.46 ± 0.072
GaussianLDS/("smooth", "latent=4", "obs=5", "T=500") 1.94 ± 0.029 ms 1.39 ± 0.014 ms 1.4 ± 0.025
GaussianLDS/("smooth", "latent=8", "obs=10", "T=100") 1.06 ± 0.058 ms 0.627 ± 0.023 ms 1.7 ± 0.11
GaussianLDS/("smooth", "latent=8", "obs=10", "T=500") 5.93 ± 2.2 ms 3.29 ± 0.55 ms 1.8 ± 0.72
GaussianLDS/("smooth", "latent=8", "obs=20", "T=100") 1.1 ± 0.042 ms 0.645 ± 0.0091 ms 1.7 ± 0.069
GaussianLDS/("smooth", "latent=8", "obs=20", "T=500") 7.45 ± 2.3 ms 3.61 ± 0.65 ms 2.07 ± 0.73
PoissonLDS/("smooth", "latent=2", "obs=10", "T=100") 1.07 ± 0.037 ms 0.9 ± 0.021 ms 1.19 ± 0.05
PoissonLDS/("smooth", "latent=2", "obs=10", "T=500") 5.3 ± 0.062 ms 4.5 ± 0.11 ms 1.18 ± 0.032
PoissonLDS/("smooth", "latent=2", "obs=5", "T=100") 1.16 ± 0.035 ms 0.96 ± 0.026 ms 1.21 ± 0.049
PoissonLDS/("smooth", "latent=2", "obs=5", "T=500") 6.31 ± 0.098 ms 5.23 ± 0.17 ms 1.21 ± 0.043
PoissonLDS/("smooth", "latent=4", "obs=10", "T=100") 1.83 ± 0.031 ms 1.57 ± 0.09 ms 1.17 ± 0.07
PoissonLDS/("smooth", "latent=4", "obs=10", "T=500") 9.82 ± 0.076 ms 8.34 ± 0.27 ms 1.18 ± 0.039
PoissonLDS/("smooth", "latent=4", "obs=5", "T=100") 1.45 ± 0.0071 ms 1.26 ± 0.013 ms 1.15 ± 0.013
PoissonLDS/("smooth", "latent=4", "obs=5", "T=500") 9.32 ± 0.19 ms 7.88 ± 0.1 ms 1.18 ± 0.029
time_to_load 0.616 ± 0.0033 s 0.926 ± 0.004 s 0.665 ± 0.0046
Memory benchmarks
main e3d7907... main / e3d7907...
GaussianLDS/("smooth", "latent=2", "obs=10", "T=100") 2.08 k allocs: 0.265 MB 2.02 k allocs: 0.157 MB 1.69
GaussianLDS/("smooth", "latent=2", "obs=10", "T=500") 9.3 k allocs: 1.24 MB 9.24 k allocs: 0.7 MB 1.77
GaussianLDS/("smooth", "latent=2", "obs=20", "T=100") 2.09 k allocs: 0.298 MB 2.03 k allocs: 0.181 MB 1.64
GaussianLDS/("smooth", "latent=2", "obs=20", "T=500") 9.31 k allocs: 1.31 MB 9.25 k allocs: 0.725 MB 1.8
GaussianLDS/("smooth", "latent=2", "obs=5", "T=100") 2.08 k allocs: 0.254 MB 2.02 k allocs: 0.149 MB 1.7
GaussianLDS/("smooth", "latent=2", "obs=5", "T=500") 9.3 k allocs: 1.22 MB 9.24 k allocs: 0.692 MB 1.76
GaussianLDS/("smooth", "latent=4", "obs=10", "T=100") 2.09 k allocs: 0.716 MB 2.03 k allocs: 0.325 MB 2.2
GaussianLDS/("smooth", "latent=4", "obs=10", "T=500") 9.3 k allocs: 3.48 MB 9.24 k allocs: 1.52 MB 2.29
GaussianLDS/("smooth", "latent=4", "obs=20", "T=100") 2.1 k allocs: 0.75 MB 2.04 k allocs: 0.352 MB 2.13
GaussianLDS/("smooth", "latent=4", "obs=20", "T=500") 9.31 k allocs: 3.55 MB 9.25 k allocs: 1.55 MB 2.29
GaussianLDS/("smooth", "latent=4", "obs=5", "T=100") 2.09 k allocs: 0.704 MB 2.03 k allocs: 0.317 MB 2.22
GaussianLDS/("smooth", "latent=4", "obs=5", "T=500") 9.3 k allocs: 3.45 MB 9.24 k allocs: 1.51 MB 2.28
GaussianLDS/("smooth", "latent=8", "obs=10", "T=100") 2.09 k allocs: 2.48 MB 2.03 k allocs: 0.981 MB 2.53
GaussianLDS/("smooth", "latent=8", "obs=10", "T=500") 9.3 k allocs: 12.3 MB 9.24 k allocs: 4.73 MB 2.59
GaussianLDS/("smooth", "latent=8", "obs=20", "T=100") 2.1 k allocs: 2.52 MB 2.04 k allocs: 1.01 MB 2.49
GaussianLDS/("smooth", "latent=8", "obs=20", "T=500") 9.31 k allocs: 12.3 MB 9.25 k allocs: 4.76 MB 2.59
PoissonLDS/("smooth", "latent=2", "obs=10", "T=100") 3.32 k allocs: 0.283 MB 2.05 k allocs: 0.234 MB 1.21
PoissonLDS/("smooth", "latent=2", "obs=10", "T=500") 15.3 k allocs: 1.33 MB 9.27 k allocs: 1.09 MB 1.22
PoissonLDS/("smooth", "latent=2", "obs=5", "T=100") 3.53 k allocs: 0.276 MB 2.05 k allocs: 0.228 MB 1.21
PoissonLDS/("smooth", "latent=2", "obs=5", "T=500") 17.4 k allocs: 1.34 MB 9.27 k allocs: 1.09 MB 1.23
PoissonLDS/("smooth", "latent=4", "obs=10", "T=100") 3.54 k allocs: 0.738 MB 2.06 k allocs: 0.626 MB 1.18
PoissonLDS/("smooth", "latent=4", "obs=10", "T=500") 17.4 k allocs: 3.6 MB 9.27 k allocs: 3.03 MB 1.19
PoissonLDS/("smooth", "latent=4", "obs=5", "T=100") 3.33 k allocs: 0.723 MB 2.06 k allocs: 0.618 MB 1.17
PoissonLDS/("smooth", "latent=4", "obs=5", "T=500") 17.4 k allocs: 3.58 MB 9.27 k allocs: 3.03 MB 1.18
time_to_load 0.205 k allocs: 12 kB 0.205 k allocs: 12 kB 1

@tomdstone tomdstone changed the title Port of SOMATA python package feature / Port of SOMATA python package Sep 17, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants