Skip to content

feat(biomechanics): add Discrete Frechet distance and rollout trajectory comparison - #886

Open
M-Colley wants to merge 2 commits into
devfrom
feat/kinematic-frechet
Open

M-Colley wants to merge 2 commits into
devfrom
feat/kinematic-frechet

Conversation

@M-Colley

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

Copy link
Copy Markdown
Collaborator

Category: new scientific analysis feature + optional dependency extra (analysis = ["tslearn>=0.7"]; Milestone 3.2, base of #980; requires maintainer approval).

Summary

  • Phase-free Discrete Fréchet alignment (myosuite.biomechanics.kinematic_metrics): Adds FrechetAlignment, discrete_frechet_alignment, and discrete_frechet_distance (Eiter & Mannila 1994) for 1D (T,) and multidimensional (T, D) trajectories of equal or unequal length. Distances preserve the caller's input coordinate units (e.g., meters for site_xpos, radians for joint angles; zero_start=True subtracts each trajectory's initial sample before comparison).
  • Multi-quantity rollout trajectory comparison: Adds EntityTrajectoryMetrics and compare_rollout_trajectories, reporting per-quantity synchronous Euclidean error, normalized [0, 1] timeline-resampled Euclidean error (np.interp over normalized progress u in [0, 1]), Discrete Fréchet distance, and optional custom_measures callables (such as feat(biomechanics): add Dynamic Time Warping trajectory alignment to kinematic_metrics #980's DTW).
  • Optional analysis extra (pyproject.toml, uv.lock, .github/workflows/python-app.yml): Adds analysis = ["tslearn>=0.7"] (included in dev and CI). tslearn is imported lazily inside the Fréchet call path so import myosuite and import myosuite.biomechanics.kinematic_metrics never load tslearn or numba; calling Fréchet without the extra raises an actionable ImportError naming myosuite[analysis].

Ownership & Reuse (REVIEW.md)

  • Owner: MyoSuite (myosuite/biomechanics/kinematic_metrics.py — trajectory and rollout evaluation metrics; zero anatomical or physical model transforms, so nothing belongs in myo_sim).
  • Existing primitives reused: Delegates the dynamic-programming recurrence and diagonal-first coupling backtrack to tslearn.metrics.frechet / tslearn.metrics.frechet_path (BSD-2-Clause) and timeline interpolation to numpy.interp, placed alongside existing smoothness/Fitts metrics in myosuite/biomechanics/kinematic_metrics.py and registered in docs/source/developer/library-usage.md and test_biomechanics_layout.py. (similaritymeasures.frechet_dist was evaluated and rejected because it uses a pure-Python loop and returns no alignment path.)
  • Code removed/simplified: Replaced the initial in-repo NumPy DP recurrence and backtracking loop (77 lines removed in 1b9e1841) with tslearn.metrics.frechet_path (verified bit-identical on 300 random pairs including rounded tie cases; 6.8 ms vs 10.6 ms on 500x700x39).

Compatibility & Checks

  • Compatibility: Core pip install myosuite dependencies, registered environments, and checkpoints are unchanged (numba caps numpy<2.6, which is why tslearn>=0.7 lives in the optional analysis extra; numba/llvmlite/statsmodels ship cp310–cp315 wheels). First-call JIT compilation per process is ~6 s.
  • Checks run (rebased cleanly onto dev 0b8631a5 at 1b9e1841):
    • pre-commit run --from-ref origin/dev --to-ref HEAD: passed.
    • pytest myosuite/tests/test_kinematic_frechet.py myosuite/tests/test_kinematic_metrics.py myosuite/tests/test_biomechanics_layout.py myosuite/tests/test_import_footprint.py -v (Python 3.13.18, tslearn 0.8+/0.10, plus verified earlier on tslearn 0.7.0): passed.
    • All 5 AGENTS.md core verification suites (test_model_builder.py, test_terms_cpu.py, test_fragment_compat.py, test_parity.py, test_registry.py): 350 passed, 3 skipped.
  • Not run: Tutorials and full multi-OS matrix (covered on dev/release CI).

@M-Colley M-Colley added area:biomechanics IK, ID, static optimization, CMC, uncertainty, camera & loads enhancement New feature or request stack:standalone Targets dev directly with no open PR or unreleased prerequisites labels Nov 20, 2026

Copy link
Copy Markdown
Collaborator

Guideline / #895 review of 53a06c16 (feature/scientific API):

Scientific trajectory metrics belong with biomechanics; the custom_measures hook already lets DTW compose without a dependency on #867. No justified stack edge between these PRs. New Frechet/rollout comparison APIs need human design/implementation approval and documented distance units and resampling semantics; the added numerical tests are useful. No concrete runtime blocker established by this static review.

Checks: current diff/source, discussions and check-run metadata inspected; no fresh local runtime tests run. Current-head GitHub checks: CPU, mjlab, docs and pre-commit passed. Human design/implementation approval remains required; this review is not an auto-approval.

Copy link
Copy Markdown
Collaborator

Automated cross-repo audit (compatibility, composition, reuse) of myosuite#886 at 53a06c1, 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 force-pushed the feat/kinematic-frechet branch 2 times, most recently from a3cded5 to 3f9afb0 Compare November 26, 2026 15:29
@M-Colley M-Colley added this to the 3.2 milestone Dec 1, 2026
M-Colley added a commit to M-Colley/myosuite that referenced this pull request Dec 1, 2026
…etrics

Add DTWAlignment, dtw_alignment and dtw_distance for multidimensional
trajectories with an optional Sakoe-Chiba band (Sakoe & Chiba, 1978;
Berndt & Clifford, 1994). The Euclidean cost matrix is computed exactly in
NumPy and passed to tslearn.metrics.dtw_path_from_metric (precomputed), so
identical samples cost exactly zero. tslearn is imported lazily through the
optional analysis extra introduced for the Discrete Frechet metrics.

Costs match the previous hand-written dynamic program exactly on 300
random unconstrained pairs and on equal-length banded pairs (radius 0, 2,
5); about 12x faster on 500x600 steps. For unequal lengths the band now
follows tslearn's Sakoe-Chiba mask. Verified with tslearn 0.7.0 and 0.10.0.

Stacked on feat/kinematic-frechet (MyoHub#886).
@M-Colley

M-Colley commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator Author

@copilot resolve the merge conflicts in this pull request

Copilot AI commented Dec 1, 2026

Copy link
Copy Markdown

@copilot resolve the merge conflicts in this pull request

Merged the current dev base in e877eff. Git resolved the changes automatically, including the relocated documentation. CodeQL and configuration checks passed; pytest and pre-commit could not run because they are not installed in the environment.

Replace the hand-rolled Eiter & Mannila recurrence and coupling backtrack
with tslearn.metrics.frechet / frechet_path, and the linear resampler with
np.interp. tslearn is imported lazily from a new optional `analysis` extra
(added to `dev` and the CI install), so `import myosuite` and the module
itself load neither tslearn nor numba.

tslearn uses the same recurrence and the same diagonal-first tie-breaking:
distances and couplings match the previous implementation exactly on 300
random pairs, including tied cases. tslearn>=0.7 passes the test suite.
@M-Colley
M-Colley force-pushed the feat/kinematic-frechet branch from 3a40a76 to 1b9e184 Compare December 2, 2026 12:14
M-Colley added a commit to M-Colley/myosuite that referenced this pull request Dec 2, 2026
…etrics

Add DTWAlignment, dtw_alignment and dtw_distance for multidimensional
trajectories with an optional Sakoe-Chiba band (Sakoe & Chiba, 1978;
Berndt & Clifford, 1994). The Euclidean cost matrix is computed exactly in
NumPy and passed to tslearn.metrics.dtw_path_from_metric (precomputed), so
identical samples cost exactly zero. tslearn is imported lazily through the
optional analysis extra introduced for the Discrete Frechet metrics.

Costs match the previous hand-written dynamic program exactly on 300
random unconstrained pairs and on equal-length banded pairs (radius 0, 2,
5); about 12x faster on 500x600 steps. For unequal lengths the band now
follows tslearn's Sakoe-Chiba mask. Verified with tslearn 0.7.0 and 0.10.0.

Stacked on feat/kinematic-frechet (MyoHub#886).
@M-Colley M-Colley added dependencies Pull requests that update a dependency file python:uv Pull requests that update python:uv code stack:root Base PR of an open stack onto dev — review and merge first and removed stack:standalone Targets dev directly with no open PR or unreleased prerequisites 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 dependencies Pull requests that update a dependency file enhancement New feature or request python:uv Pull requests that update python:uv code 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.

3 participants