Skip to content

Pufferlib-inspired API redesign: proposal + kernel core (+ Optuna parallelization)#19

Open
RaulSimpetru wants to merge 33 commits into
mainfrom
feat/puffer-api
Open

Pufferlib-inspired API redesign: proposal + kernel core (+ Optuna parallelization)#19
RaulSimpetru wants to merge 33 commits into
mainfrom
feat/puffer-api

Conversation

@RaulSimpetru

@RaulSimpetru RaulSimpetru commented Jun 23, 2026

Copy link
Copy Markdown
Member

Summary

This branch bundles three related-but-distinct workstreams (kept together per request). Each is independently reviewable.

1. Pufferlib-inspired API redesign — proposal + kernel core

A design proposal and the first working slice (a backend-agnostic simulation kernel) for a vectorized, backend-swappable MyoGen API.

  • Proposal (docs/superpowers/specs/2026-06-23-pufferlib-inspired-api-design.md): 7-layer architecture around a shared flat SimState buffer, a closed-loop stage graph (afferent feedback), and two backend seams — neural dynamics (NEURON → jaxley) and muscle/force (analytic → FEM). Buffer-first data model with opt-in neo/NWB.
  • Kernel (myogen/kernel/): SimState, Stage/Backend protocols, the Simulation lockstep driver with one-tick feedback, and SimResult with lazy to_neo()/to_nwb(). 23 tests, CI-runnable with no NEURON/jaxley/FEM. Purely additive — no existing code touched.

2. Optuna example parallelization (~5×)

examples/03_papers/watanabe/02 now fans its 25 independent Optuna trials across worker processes sharing one SQLite study: ~2000 s → 139 s, equivalent optimum. Verified faithful (byte-identical sim/objective, deterministic per seed, independently audited). h5py added to the nwb extra so the NWB/HDF5 examples are fully declarable.

3. Docs: Sphinx → properdocs (MkDocs)

Migrates the documentation to properdocs (mkdocs-material + mkdocstrings), matching MyoGestic / Virtual-Hand-Interface.

  • properdocs.yml (material theme, mkdocstrings API, section-index), flat markdown under docs/ (home, getting-started, neo-blocks), 8 mkdocstrings API pages, and a mkdocs-gallery example gallery.
  • The gallery is source-only by default: examples render as prose + code with full-.py download links, but are not executed — many examples use multiprocessing/joblib (which breaks under the gallery's exec() context) or form slow inter-dependent chains. Opt into execution with MKDOCS_GALLERY_PLOT=true.
  • New .github/workflows/docs.yml builds + deploys to GitHub Pages; the Sphinx config, conf.py, rst pages, and make/Makefile are removed.

Notes for reviewers

  • Open questions (proposal §10) steer the next plan and are yours to decide: jaxley default vs. opt-in; FEM as per-step stage vs. offline precompute; clean-break vs. compatibility window; analysis/viz + config format.
  • Pages source: docs.yml deploys via GitHub Actions, so repo Settings → Pages → Source must be set to "GitHub Actions" (the old workflow pushed to a gh-pages branch).
  • properdocs build --strict surfaces 8 pre-existing griffe docstring/signature warnings in myogen source (muscle.py, afferents.py) — not introduced here; the non-strict build (used by CI) passes.

Design proposal for a backend-swappable, vectorized MyoGen API: a shared
flat SimState buffer stepped in lockstep by a backend-agnostic driver, with
two backend seams (neural dynamics NEURON->jaxley; muscle/force analytic->FEM)
and a closed-loop stage graph for afferent feedback. Buffer-first data model
with neo/NWB as opt-in adapters. Maps the full current public surface onto the
new layers and lists net-new work and open questions for team review.
Bite-sized TDD plan for the backend-agnostic simulation kernel (myogen/kernel):
SimState shared buffer, Stage/Backend protocols, the Simulation lockstep driver
with one-tick closed-loop feedback, and the buffer-first SimResult with lazy
neo/NWB adapters. Seven tasks, fully CI-testable without NEURON/jaxley/FEM.
NEURONBackend, real stages, jaxley and FEM are scoped as follow-on plans.
SimResult.from_state() extracts spike times in seconds, copies force and
surface EMG arrays, infers grid_shape from EMG buffer dimensions, and
handles missing optional buffers gracefully. Simulation.run() now returns
SimResult instead of bare SimState; all kernel tests updated to read
buffers through sim.state.
…rocesses

Extract simulation logic and constants into _oscillating_dc_helpers.py,
add _optimize_dc_worker.py subprocess worker, and rewrite the launch in
02_optimize_oscillating_dc.py to fan out N workers that all write to one
shared SQLite Optuna study. Avoids NEURON h singleton / macOS spawn
constraints; gallery-safe via conf.py ignore_pattern for the _ files.
Example 14 (calibrate_noise_from_real) imports h5py directly to load real
recordings, but it was only available transitively. Declaring it in the nwb
extra makes 'uv sync --extra nwb' sufficient to run the NWB/HDF5 examples
(11, 13, 14).
Many MyoGen examples use multiprocessing/joblib (which breaks under the
gallery's exec context) or form slow inter-dependent chains, so executing
the gallery in CI is unreliable. Render examples as source + prose with
full-code download links instead; opt into execution with MKDOCS_GALLERY_PLOT=true.
Add MuscleGeometry/JointGeometry/JointBiomechanics to the simulator API,
the four missing type aliases, and a RANDOM_GENERATOR/SEED note; restore the
How-to-cite, Contributing, and License sections on the home page and the
MUAP simulate/access step in the quick-start (per migration-coverage audit).
Capture one representative figure per example (run standalone, where they work)
and wire them as mkdocs-gallery thumbnails via per-example
mkdocs_gallery_thumbnail_path directives + a logo default_thumb_file fallback.
Gives the source-only gallery real pictures on the overview grid without
executing examples in the docs build (which spawn-bombs on the multiprocessing
examples).
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.

1 participant