Skip to content

Add musculoskeletal EMG validation modes and model fingerprinting - #812

Draft
M-Colley wants to merge 6 commits into
devfrom
feat/emg-validation
Draft

M-Colley wants to merge 6 commits into
devfrom
feat/emg-validation

Conversation

@M-Colley

@M-Colley M-Colley commented Nov 15, 2026 •

Copy link
Copy Markdown
Collaborator

Note

Draft (Post-3.0): Deferred from Milestone 3.2 so the 3.2 review queue stays focused; requires maintainer scope/design agreement before entering review (AGENTS.md).

Stack (2/4) — 3-Layer Musculoskeletal & EMG Validation Split

Summary

Adds the four-mode musculoskeletal model-data EMG validation engine, SHA-256 musculoskeletal fingerprinting, and lazy amathislab/mm-evalkit statistical & rollout-archive integration in myosuite/biomechanics/emg_validation.py (PR 2 of 4, stacked on feat/emg-signal):

  • Standardized EMGTrial .npz schema & cross-version EMGChannelMap: maps single-muscle or synergistic muscle-group sEMG channels to MuJoCo actuators with graceful partial resolution across model revisions (e.g. myo_sim PRs that add, rename, or split muscle actuators).
  • Musculoskeletal SHA-256 fingerprint (musculoskeletal_fingerprint): hashes actuator gain/bias/dyn/lengthrange parameters, tendon spring/stiffness/damping, joint ranges/stiffness, equality constraints, and wrap counts to detect model changes across worktrees.
  • Four complementary validation modes (validate_trial):
    1. Mode 1 (Inverse SO vs EMG): runs inverse dynamics + OSQP static optimization along recorded qpos + external wrenches and scores per-channel zero-lag $r$, optimal-lag $r$, lag [ms], cosine similarity, gain-invariant NRMSE, onset/offset F1, and representational similarity (rsa_spearman, rsa_pearson).
    2. Mode 2 (EMG-Bounded Feasibility): constrains muscle activations to dynamic bands around conditioned sEMG ([max(0, e - s), min(1, g*e + s)]) and measures minimum reserve torque ratios needed to close joint dynamics (unconstrained_reserve_ratio_p95 vs emg_bounded_reserve_ratio_p95).
    3. Mode 3 (Forward Sensitivity & Open-Loop Drift Audit): evaluates Jacobian-based pose sensitivity (actuator_pose_sensitivity, flagging 0.0 deg/ctrl dead actuators and condition spread) and short-horizon open-loop forward simulation drift.
    4. Mode 4 (Kinematics-Free Synergy & Ridge Predictability): NMF muscle-synergy temporal subspace similarity (temporal_subspace_similarity) and ridge-regression $R^2$ / $r$ for dense surface arrays without full joint-angle ground truth.
  • amathislab/mm-evalkit integration (lazy, zero core import overhead):
    • Shared statistical primitives: delegates RSM/RSA (compute_rsm, rsa) in validate_inverse_emg and paired cohort Wilcoxon signed-rank + Holm step-down + rank-biserial effect sizes (MetricSamples, MetricFamily, wilcoxon_table) in compare_emg_validation_reports lazily to mm_evalkit.stats when installed, with clean SciPy/NumPy fallbacks on Python 3.10.
    • Policy-free rollout archive & policies.yaml export (export_mm_evalkit_rollout, write_mm_evalkit_policy_config): solves bare untrained MjModels via either solver="static_optimization" or solver="cmc" (myosuite.biomechanics.computed_muscle_control) at dt=0.01 s (100 Hz, activation_sample_phase="pre_step") into .npz archives passing both mm_evalkit.workflows.bimanual.rollout.open_rollout and mm_evalkit.workflows.gait.rollout_validation.validate_policy_rollouts.

Verification

  • pytest myosuite/tests/test_emg.py myosuite/tests/test_emg_validation.py myosuite/tests/test_import_footprint.py -v
  • ruff check / ruff format --check / mypy

Ownership & Reuse (REVIEW.md)

  • Repository & package boundary: Dataset-free structural muscle/moment-arm checks live in MyoHub/myo_sim#197; external evaluation orchestration lives in amathislab/mm-evalkit; pure NumPy/SciPy/OSQP trial validation (EMGValidationResult, validate_emg_trial, compute_model_fingerprint) lives in myosuite.biomechanics.emg_validation, while .npz trial I/O (BaseEMGSource, resolve_stem_channel_map, NpzDirectoryEMGSource) and mm-evalkit rollout exporters live in myosuite.integrations.emg / myosuite.integrations.mm_evalkit (zero external third-party runtime dependencies).
  • Reused primitives: myosuite.biomechanics.emg, myosuite.biomechanics.inverse_dynamics, myosuite.biomechanics.static_optimization.StaticOptimizer, myosuite.biomechanics.computed_muscle_control.
  • Before (fails) -> After (works): Previously myosuite.biomechanics.emg processed signals in isolation with no standardized multi-mode (static_opt / cmc / recorded) concordance evaluator, model SHA-256 provenance fingerprint, or mm-evalkit rollout export; now validate_emg_trial evaluates phase-aligned EMG waveforms with explicit solver/channel diagnostics.
  • Literature References: SENIAM surface EMG recommendations (Hermens et al., J. Electromyogr. Kinesiol., 2000); Hicks et al., "Is My Model Good Enough? Best Practices for Verification and Validation of Musculoskeletal Models", J. Biomech. Eng., 137(2), 2015.

Fingerprint moved to myo_sim (release gate)

Per maintainer review, musculoskeletal_fingerprint is model-only, so it now lives in myo_sim (MyoHub/myo_sim#210, with project_tendon_nodes in myo_sim.physics.joint_coupling). The port yields identical hashes on six models (two test models, myoarm_r, myohand_r, myolegs, myofullbody). emg_validation.musculoskeletal_fingerprint is a thin lazy delegation with an actionable ImportError; the EMG validation engine stays here, and test_biomechanics_layout.py allows exactly myo_sim.fingerprint. Because every validate_trial report records the fingerprint, validation needs a myo-sim release containing MyoHub/myo_sim#210, so this PR is back to draft until then. The three tests that reach the fingerprint skip without it (needs_myo_sim_fingerprint in tests/support/optional_deps.py). Follow-up after that release: myosuite.biomechanics.emg.project_tendon_nodes can delegate to myo_sim as well.

Local checks (CI is out of credits; Windows, Python 3.11, MuJoCo 3.8.1): test_emg_validation.py + test_biomechanics_layout.py 7 passed with myo_sim#210 on PYTHONPATH, 4 passed and 3 skipped on myo-sim 0.2.5; the five verification modules 349 passed, 3 skipped on both. #813 and #814 are updated by merge.

  • Code removed/simplified: None (additive myosuite/biomechanics/emg_validation.py analysis workflow; model fingerprinting delegates to MyoHub/myo_sim#210).

Compatibility & Checks

  • Compatibility & validation: Draft (Post-3.0, stack:root): no registered env or checkpoint contract changes; gated on MyoHub/myo_sim#210 release.

@M-Colley
M-Colley added this pull request to stack #815 November 15, 2026 12:21
@M-Colley M-Colley added enhancement New feature or request stack:stacked Builds on another open PR or unmerged branch — merge after base area:biomechanics IK, ID, static optimization, CMC, uncertainty, camera & loads area:scaling-calibration Subject scaling, OpenSim validation, strength/mass/fatigue calibration labels Nov 15, 2026
@M-Colley
M-Colley force-pushed the feat/emg-validation branch from 04e9f7e to ef1e5d7 Compare November 15, 2026 16:14
@M-Colley
M-Colley force-pushed the feat/emg-validation branch 2 times, most recently from 5dc3745 to 1e1a329 Compare November 15, 2026 19:22
@M-Colley
M-Colley force-pushed the feat/emg-validation branch from 1e1a329 to 19d22f7 Compare November 18, 2026 16:49
Base automatically changed from feat/emg-signal to dev November 19, 2026 20:16
@M-Colley M-Colley added the needs:rebase Base PR already merged into dev or branch has merge conflicts label Nov 20, 2026
@M-Colley
M-Colley force-pushed the feat/emg-validation branch from 19d22f7 to 47847fc Compare November 21, 2026 11:46

Copy link
Copy Markdown
Collaborator

Current-head guideline review 47847fc7 (feature / architecture and scientific contracts).

biomechanics/emg_validation.py mixes scientific validation with NPZ persistence (save/load_emg_trial), mm_evalkit archive export and policies.yaml writing (export_mm_evalkit_rollout/write_mm_evalkit_policy_config). Separate those integration/workflow responsibilities before growing this 2,400-line scientific module (#895); preserve current public compatibility through small facades. No additional concrete runtime bug demonstrated.

Validation: source/diff, existing discussion and check-run metadata reviewed; no runtime tests run in this review. CI: Core (ubuntu-latest, py3.11, mjlab): success; docs: success; Core (ubuntu-latest, py3.11, cpu): success; pre-commit: success.

Merge: not auto-approved. Apply the current human design/implementation-approval gate; agent review is not eligible human approval.

@M-Colley

Copy link
Copy Markdown
Collaborator Author

Addressed #895 boundary review in b8569b9: moved pickle-free EMGTrial .npz persistence (EMG_TRIAL_META_KEY, save_emg_trial, load_emg_trial, NpzDirectoryEMGSource) into myosuite.integrations.emg.trial_io and mm-evalkit rollout/policies.yaml export (export_mm_evalkit_rollout, write_mm_evalkit_policy_config) into myosuite.integrations.mm_evalkit.rollout_export. Kept myosuite.biomechanics.emg_validation purely scientific (NumPy/SciPy/MuJoCo/OSQP only, zero integrations imports) with a PEP 562 compatibility facade, and tightened test_biomechanics_layout.py to enforce internal subpackage boundaries.

Copy link
Copy Markdown
Collaborator

Automated cross-repo audit (compatibility, composition, reuse) of myosuite#812 at b8569b9, run on MyoSuite dev as of 2026-11-22.

Nothing here blocks merging on its own; the items are for the next revision.


Generated by Claude Code

@M-Colley
M-Colley marked this pull request as draft November 23, 2026 15:34
@M-Colley M-Colley removed the needs:rebase Base PR already merged into dev or branch has merge conflicts label Nov 24, 2026
The hash moves to myo_sim.fingerprint (MyoHub/myo_sim#210) with
unchanged values. emg_validation imports it lazily and raises an actionable
ImportError on myo-sim 0.2.5. The tests that reach the fingerprint skip when
the API is absent.
@M-Colley
M-Colley force-pushed the feat/emg-validation branch from 15acc06 to eff214e Compare November 26, 2026 15:29
Resolve the test_biomechanics_layout.py conflict by keeping dev's
_MODULE_DEPENDENCIES entry for kinematic_metrics.py (tslearn) and this
branch's _MODULE_IMPORTS entry for emg_validation.py
(myo_sim.fingerprint) and its internal-subpackage tables.
dev reverted #867 (#978). Resolve the test_biomechanics_layout.py
conflict with this branch's version from eff214e, since dev's copy of
the file is back to the state that version was based on.
@M-Colley M-Colley added this to the Post-3.0 milestone Dec 2, 2026
@M-Colley M-Colley added stack:root Base PR of an open stack onto dev — review and merge first and removed stack:stacked Builds on another open PR or unmerged branch — merge after base labels Dec 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:biomechanics IK, ID, static optimization, CMC, uncertainty, camera & loads area:scaling-calibration Subject scaling, OpenSim validation, strength/mass/fatigue calibration enhancement New feature or request stack:root Base PR of an open stack onto dev — review and merge first

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants