Skip to content

[BUG] Pass series-shaped weights through in minkowski_pairwise_distance - #4012

Open
ompatil1906 wants to merge 6 commits into
aeon-toolkit:mainfrom
ompatil1906:fix/minkowski-pairwise-weights
Open

ompatil1906 wants to merge 6 commits into
aeon-toolkit:mainfrom
ompatil1906:fix/minkowski-pairwise-weights

Conversation

@ompatil1906

@ompatil1906 ompatil1906 commented Nov 13, 2026 •

Copy link
Copy Markdown

Description

Fixes #3861.

minkowski_pairwise_distance documented that w should match the shape of a single series, matching minkowski_distance. The two numba helpers instead indexed w[i] and reshaped, which:

  • read past the end of a series-shaped weight array (silent wrong distances),
  • crashed on multivariate series (channel dimension mismatch),
  • produced asymmetric X→y vs y→X results when a per-case weight matrix was supplied.

Root cause

_w = w[i].reshape((1, w.shape[1]))
distances[i, j] = minkowski_distance(..., _w)

Fix

  • Validate w.shape against every series in both converted collections before entering the parallel kernels, and reject negative weights and p < 1 up front.
  • Pass the same series-shaped w to every minkowski_distance call.
  • Update the docstring example that previously relied on the per-case indexing.
  • Add regression tests for univariate/multivariate self and X-to-y pairs, plus a clear ValueError on mismatched weight shape.

Verification

  • Reproduced the original OOB / wrong-value behaviour on main.
  • After the change, every pairwise entry equals the corresponding single-pair minkowski_distance call (max abs error 0).
  • Multivariate case no longer raises.
  • X-to-y and y-to-X are consistent under transpose.
  • New test test_minkowski_pairwise_weights_match_single_series passes.

Latest review follow-up

Reverted the two extra trailing blank lines in aeon/distances/tests/test_miscellaneous_distances.py so that this unrelated file no longer appears in the PR diff. The remaining changes concern Minkowski distances, their regression tests and the changelog.

Validation for this cleanup: pre-commit run --all-files — passed.

AI assistance

This pull request includes code written with the assistance of AI.
Maintainers have reviewed the main fix; the latest whitespace cleanup has not yet been reviewed by a human.

The pairwise helpers indexed w by case (w[i]), which contradicted the
docstring and the single-pair minkowski_distance API. That caused
out-of-bounds reads for series-shaped weights, crashes on multivariate
input, and asymmetric X-to-y vs y-to-X results when w had one row per
case.

Validate the weight shape once against a sample series before entering
the numba kernels, and pass the same series-shaped array to every pair.
Update the docstring example and add regression tests covering uni- and
multivariate self / X-to-y cases plus a clear ValueError on wrong shape.

Fixes aeon-toolkit#3861
@aeon-actions-bot aeon-actions-bot Bot added bug Something isn't working distances Distances package labels Nov 13, 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: distances. 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

@aashoday

Copy link
Copy Markdown
Contributor

Hi @ompatil1906
Stage the changes, run pre-commit run --all-files, and push a commit with the fixes. That should resolve the pre-commit CI failure.

@ompatil1906

Copy link
Copy Markdown
Author

Thanks for the feedback! I ran pre-commit and pushed the style fixes (latest commits). CI is now green on all checks including pre-commit. Let me know if anything else is needed.

@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 @ompatil1906, the main fix looks correct. A few changes before merging:

  1. Validate w against every series in X and y, not only X[0], if X is unequal length (a list not an array) , because Minkowski supports unequal-length collections. At the moment y isn't checked at all: with 3D array input, a y with fewer channels than w silently computes over the shared channels, and a shorter y silently truncates the weights. Errors raised inside the parallel kernels either surface as an opaque SystemError or are lost, so the checks need to happen up front. Keep your negative-weight check.
  2. Also validate p >= 1 up front. At the moment minkowski_pairwise_distance(X, p=0.5) silently returns a matrix of zeros, because the check inside minkowski_distance never reaches the user from inside the prange kernel.
  3. Move the tests to test_pairwise.py and parametrise them over the number of channels (1 and 2), covering self and cross distances and swap symmetry.
  4. Add tests for invalid inputs: negative weights, a weight shape that doesn't match, a mismatch only in y, unequal-length collections with weights, and p < 1.
  5. Update the w docstring to state that it must have the shape of a single series and that per-case weights are not supported, and add a changelog note, since the previous docstring example used per-case weights.

Address review feedback on aeon-toolkit#4012:

- Validate p >= 1 up front so invalid p no longer silently yields zeros
  from inside the parallel kernels.
- Validate weight shape against every series in X and y (not only X[0])
  so unequal-length collections and channel mismatches raise a clear
  ValueError before entering numba.
- Keep the non-negative weight check.
- Document that w must be series-shaped and that per-case weights are
  not supported.
- Move regression tests to test_pairwise.py, parametrise over channels,
  and add coverage for invalid p, negative weights, shape mismatches
  in X/y, and unequal-length collections.
- Add changelog note.
@ompatil1906

Copy link
Copy Markdown
Author

Thanks for the detailed review @TonyBagnall — I've addressed all five points:

  1. Weight shape is now validated against every series in both X and y (not only X[0]), so unequal-length collections and channel mismatches raise a clear ValueError before the parallel kernels.
  2. p >= 1 is validated up front; invalid p no longer silently produces a matrix of zeros.
  3. Regression tests moved to test_pairwise.py and parametrised over n_channels in {1, 2}, covering self / cross distances and swap symmetry.
  4. Added tests for negative weights, wrong weight shape, mismatch only in y, unequal-length collections, and p < 1.
  5. Docstring for w now states it must be series-shaped and that per-case weights are not supported; changelog entry added under Distances.

CI should re-run on this push. Happy to iterate further if anything else is needed.

@TonyBagnall

Copy link
Copy Markdown
Contributor

hi, failing precommit

aeon/distances/tests/test_miscellaneous_distances.py only gains two trailing blank lines, which is what fails pre-commit (end-of-file-fixer). Please revert that file so the PR only touches Minkowski.

@ompatil1906

ompatil1906 commented Nov 29, 2026 •

Copy link
Copy Markdown
Author

Reverted test_miscellaneous_distances.py to its upstream content; it no longer appears in the PR diff. pre-commit run --all-files passes locally. Pushed the cleanup in 9fc554d; CI is running.

@ompatil1906

ompatil1906 commented Nov 29, 2026 •

Copy link
Copy Markdown
Author

On the current head (9fc554d0), the Ubuntu Python 3.11 and 3.13 pytest jobs both stopped with “The hosted runner lost communication with the server”:

Both jobs were in the test step when communication was lost; their downloadable logs return BlobNotFound, so I cannot identify a failing test or confirm whether resource exhaustion caused it. Ubuntu Python 3.15 and the pre-commit workflow passed.

I attempted gh run rerun 46270809808 --failed, but GitHub denied it because my account lacks the required upstream permissions. Could a maintainer rerun only the failed jobs? No source or workflow changes were made for this diagnosis.

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 distances Distances package

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] minkowski_pairwise_distance indexes weights per case, giving wrong results, out-of-bounds reads and crashes

3 participants