Skip to content

[MNT] add regression tests for barycentre averaging with derivative distances - #3937

Open
mayuriphad wants to merge 4 commits into
aeon-toolkit:mainfrom
mayuriphad:fix/ba-derivative-distance-alignment
Open

mayuriphad wants to merge 4 commits into
aeon-toolkit:mainfrom
mayuriphad:fix/ba-derivative-distance-alignment

Conversation

@mayuriphad

Copy link
Copy Markdown

Reference Issues/PRs

Fixes #3920.

What does this implement/fix? Explain your changes.

ddtw_alignment_path and wddtw_alignment_path align the derivative series, which have n - 2 points, so their path indices run from 0 to n - 3. The barycentre averaging (petitjean, subgradient and kasba) applied those indices to the original series. Two consequences:

  • Petitjean: positions n - 2 and n - 1 of the barycentre never got a contribution, so they were 0 / 0 = NaN, and every other position was updated from the wrong time point (derivative point j is centred on original point j + 1). TimeSeriesKMeans(distance="ddtw", averaging_method="ba") then had NaN centres and could not fit.
  • Subgradient and KASBA: no NaN, but the last two points were never updated and the rest were off by one.

_get_alignment_path now maps the path of ddtw and wddtw onto the original series: each pair (j, k) becomes (j + 1, k + 1), and the two end points are added as (0, 0) and (n - 1, m - 1), so every position is covered. The costs are unchanged. The other distances are unchanged.

New tests in aeon/clustering/averaging/tests/test_ba_derivative_distances.py:

  • the mapped path starts at (0, 0), ends at (n - 1, m - 1) and touches every time point of both series;
  • the barycentre of ddtw and wddtw is finite for petitjean, subgradient and kasba;
  • TimeSeriesKMeans with ddtw / wddtw and ba averaging fits with non-empty clusters.

8 of the 10 fail on main and all pass with the change. The existing tests in aeon/clustering/averaging/tests (263 passed, 50 skipped) and the ESMOTE tests also pass.

Does your contribution introduce a new dependency? If yes, which one?

No.

Any other comments?

This pull request includes code written with the assistance of AI.
The code has not yet been reviewed by a human.

The paths of ddtw and wddtw index the derivative series, which has two points fewer, but the barycenter averaging applied them to the original series. The last two points of the Petitjean barycenter were NaN, and the other methods left them unchanged and were off by one. Shift the path by one and add the two end points.

Signed-off-by: mayuriphad <mayuriphad656@gmail.com>
Copilot AI balanced review requested due to automatic review settings October 22, 2026 05:36

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

@aeon-actions-bot aeon-actions-bot Bot added bug Something isn't working clustering Clustering package labels Oct 22, 2026
@aeon-actions-bot

Copy link
Copy Markdown
Contributor

Thank you for contributing to aeon

I have added the following labels to this PR based on the title: bug.
I have added the following labels to this PR based on the changes made: clustering. Feel free to change these if they do not properly represent the PR.

The Checks tab will show the status of our automated tests. You can click on individual test runs in the tab or "Details" in the panel below to see more information if there is a failure.

If our pre-commit code quality check fails, please run pre-commit locally and push the fixes to your PR branch.

Don't hesitate to ask questions on the aeon Discord channel if you have any.

PR CI actions

These checkboxes will add labels to enable or disable CI functionality for this PR. This may not take effect immediately, and a new commit may be required to run the new configuration.

  • Run pre-commit checks for all files
  • Run mypy typecheck tests
  • Run all pytest tests and configurations
  • Run all notebook example tests
  • Run numba-disabled codecov tests
  • Disable numba cache loading
  • Regenerate expected results for testing
  • Push an empty commit to re-run CI checks

@mayuriphad

Copy link
Copy Markdown
Author

The red pytest and test-no-soft-deps jobs look like a stale numba cache rather than a problem in the change. _get_alignment_path is called from _ba_petitjean.py, _ba_subgradient.py and _kasba_average.py. Those files are untouched here, so the restored .numba_cache can serve their old compiled code that still calls the pre-fix path. That matches the failures: the last two barycenter points stay NaN for petitjean, and subgradient returns the mean.

Locally the new tests pass (10 passed) on this branch, on this branch merged into current main, and with NUMBA_DISABLE_JIT=1, after clearing the cache.

Could a maintainer add the no numba cache label so the jobs rerun without the restored cache? Happy to push a change to the caller files instead if you would rather not use the label.

…tance-alignment

Signed-off-by: mayuriphad <mayuriphad656@gmail.com>
@mayuriphad

Copy link
Copy Markdown
Author

The six failures are the new tests seeing the pre-fix behaviour (NaN in the last two points, last column equal to the mean). That's the restored numba cache serving the old compiled _get_alignment_path: on a clean cache all of test_ba_derivative_distances.py passes for ddtw and wddtw with petitjean, subgradient and kasba, plus the two kmeans cases. I've merged current main into the branch. Could a maintainer add the no numba cache label so the run compiles from source?

@mayuriphad

Copy link
Copy Markdown
Author

The pytest failures come from the restored numba cache: _get_alignment_path is cache=True, so CI runs the cached version built from main, without this change. The failures (barycenter end points equal to the mean, empty clusters) are what the unfixed code produces. Run locally against this branch, the tests pass with numba enabled and with NUMBA_DISABLE_JIT=1. Could a maintainer add the no numba cache label and re-run the checks?

@TonyBagnall

Copy link
Copy Markdown
Contributor

thanks for this, #4013 fixes the derivative alignment path indices directly in ddtw_alignment_path and wddtw_alignment_path, so the additional mapping in this PR is no longer required.

Please remove _derivative_path_to_original and the associated conversions in _get_alignment_path, but retain the barycentre averaging and KMeans regression tests. These should now pass using the corrected paths from #4013.

Please also rebase on main once #4013 is merged and resolve the CI failures.

… paths

aeon-toolkit#4013 returns ddtw and wddtw alignment paths on the original series,
so _get_alignment_path no longer needs to shift them. The barycentre
and KMeans regression tests stay and now run against those paths.

Signed-off-by: mayuriphad <mayuriphad656@gmail.com>
@mayuriphad

Copy link
Copy Markdown
Author

Thanks. I merged current main (with #4013) into the branch and removed _derivative_path_to_original along with its conversions in _get_alignment_path, so _ba_utils.py now matches main. test_ba_derivative_distances.py keeps the alignment path, barycentre averaging and KMeans regression tests, which now run against the corrected paths from #4013. I merged instead of rebasing so the branch history isn't rewritten. I can rebase if you'd rather have that.

@mayuriphad

Copy link
Copy Markdown
Author

The two red pytest (ubuntu-24.04, 3.12/3.13, true) jobs stopped during runner setup with fallocate: fallocate failed: No space left on device, before any tests ran. Could a maintainer re-run them?

@TonyBagnall TonyBagnall changed the title [BUG] Map ddtw and wddtw alignment paths to the original series in BA [[MNT] add regression tests for barycentre averaging with derivative distances Nov 28, 2026
@TonyBagnall TonyBagnall changed the title [[MNT] add regression tests for barycentre averaging with derivative distances [MNT] add regression tests for barycentre averaging with derivative distances Nov 28, 2026

@TonyBagnall TonyBagnall left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, more coverage always welcome

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

bug Something isn't working clustering Clustering package

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Petitjean barycentre averaging with "ddtw"/"wddtw" returns NaN in the last two time points

3 participants