Skip to content

Update deprecated scipy distance function and address underspecified test - #8156

Merged
stefanv merged 7 commits into
scikit-image:mainfrom
blink1073:fix-minkowski_distance-deprecation
May 13, 2026
Merged

stefanv merged 7 commits into
scikit-image:mainfrom
blink1073:fix-minkowski_distance-deprecation

Conversation

@blink1073

@blink1073 blink1073 commented May 10, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #8155.

Replace deprecated usages of scipy distance_matrix function and updated test_polynomial_weighted_estimation to be more robust. We did not see the test_polynomial_weighted_estimation in the original nightly build because the output stopped after 5 failures, but it showed up after merging #8157.

Claude recommended using np.linalg.norm instead of scipy.distance.minkowski. I agree because we were using the default p=2 which is the Euclidean distance, and scipy.distance.minkowski only operates on 1-D arrays and would require computation per row.

For the test_polynomial_weighted_estimation it gave the following explanation:


The root cause is numerical instability from an inherently ill-conditioned problem.

What the test does: Fits a degree-10 polynomial to 8 points using SVD. For order=10, matrix A has shape (16, 133)
— 16 equations for 133 unknowns. The system is massively under-determined, so the null space has dimension ~117.

The SVD issue: The solution is extracted as V[-1] — the right singular vector for the smallest singular value
(line 2436). But with ~117 near-zero singular values, there's essentially no unique "smallest" eigenvector. The
SVD can legitimately return any linear combination of null space basis vectors. Different LAPACK implementations
or the nightly numpy-2.5.0.dev0 used in CI produce a numerically different but equally valid vector from this
degenerate null space.

For order=k, matrix A has shape (2n_pts, (order+1)(order+2) + 1). With 8 points that's 16 rows. Over-determined
requires 16 > (order+1)*(order+2):

  • order=2: 16 > 12 ✓
  • order=3: 16 > 20 ✗"

So order=2 is the highest order that's actually over-determined with the existing 8 points.


I did the best I could verifying the logic by reading PolynomialTransform._estimate, but a second pair of eyes would help.

Here is a passing build with the changes.

AI Usage

This PR was developed with the assistance of Claude Code (claude-sonnet-4-6).
I (@blink1073) have thoroughly reviewed the changes.

@blink1073 blink1073 added 🔧 type: Maintenance Refactoring and maintenance of internals ⬆️ Upstream Involves an upstream project labels May 10, 2026
@greptile-apps

greptile-apps Bot commented May 10, 2026

Copy link
Copy Markdown
Contributor

Reviews (1): Last reviewed commit: "trigger build" | Re-trigger Greptile

Comment thread src/skimage/measure/fit.py Resolved
@greptile-apps

greptile-apps Bot commented May 10, 2026

Copy link
Copy Markdown
Contributor

Reviews (2): Last reviewed commit: "debug" | Re-trigger Greptile

@greptile-apps

greptile-apps Bot commented May 10, 2026

Copy link
Copy Markdown
Contributor

Reviews (3): Last reviewed commit: "Update test_polynomial_weighted_estimati..." | Re-trigger Greptile

@greptile-apps

greptile-apps Bot commented May 10, 2026

Copy link
Copy Markdown
Contributor

Reviews (4): Last reviewed commit: "remove todo" | Re-trigger Greptile

@lagru lagru left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm far from familiar with this code and np.linalg.svd(A). But I spent some time reading the documentation and following the implementation with a debugger. Both seem to agree with the explanation in the PR title, so approving.

I'm okay with switching to np.linalg.norm, should be equivalent.

@blink1073

Copy link
Copy Markdown
Contributor Author

I'll wait for @stefanv to approve as well, based on conversation in #8157

@blink1073
blink1073 requested a review from stefanv May 12, 2026 16:58
@blink1073 blink1073 changed the title Update deprecated scipy distance usages Update deprecated scipy distance function and address underspecified test May 13, 2026
@greptile-apps

greptile-apps Bot commented May 13, 2026 •

Copy link
Copy Markdown
Contributor

Comments Outside Diff (1)

  1. src/skimage/measure/fit.py, line 626-632 (link)

    P1 The deprecated distance calls are still present despite this being the stated purpose of the PR. More critically, scipy.spatial.distance.minkowski(center, data) only accepts 1-D arrays — passing a 2-D data of shape (N, 2) on scipy ≥ 1.18 will either raise an error or silently produce a scalar instead of per-point distances, breaking the radius computation. The PR description explicitly calls for replacing both paths with np.linalg.norm, which handles 2-D input correctly via axis=1.

Reviews (5): Last reviewed commit: "Merge remote-tracking branch 'upstream/m..." | Re-trigger Greptile


import numpy as np
from scipy.spatial import distance_matrix
from scipy.spatial.distance import cdist

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I tend to prefer importing the higher level module, and then using:

spatial.distance.cdist

so future readers know where the function came from. But, obviously totally personal preference and this is fine.

@stefanv
stefanv merged commit a6a9591 into scikit-image:main May 13, 2026
26 checks passed
@stefanv stefanv added this to the 0.27 milestone May 13, 2026
@stefanv

stefanv commented May 13, 2026

Copy link
Copy Markdown
Member

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

⬆️ Upstream Involves an upstream project 🔧 type: Maintenance Refactoring and maintenance of internals

Projects

None yet

Development

Successfully merging this pull request may close these issues.

⚠ nightly test failed on main

3 participants