Skip to content

Consolidate "elliptical" footprint API for skimage2 - #8249

Open
lagru wants to merge 47 commits into
scikit-image:mainfrom
lagru:sk2-footprint-ellipse
Open

lagru wants to merge 47 commits into
scikit-image:mainfrom
lagru:sk2-footprint-ellipse

Conversation

@lagru

@lagru lagru commented Jul 10, 2026 •

Copy link
Copy Markdown
Member

Supersedes #7628.

In skimage2: Replaces ellipsis, disk and ball with footprint_ellipse. The decomposition case is covered by the two new functions footprint_decomposed_disk and cross_decompose_footprint.

In skimage: ellipsis, disk and ball are now thin wrappers around the skimage2 API. The migration process is documented with the usual decorator.

  • Document how to migrate the decomposition case
  • Look for old issues & PRs that are relevant to this one.

(Kept up-to-date)

More details

The scope of this PR is to clean up our skimage2 API when it comes to functions that create elliptical footprints:

  • ellipsis
  • disk
  • ball

in the morphology module. In the process it would be very nice and elegant if a single n-dimensional algorithm could cover all these cases.

However, the legacy implementation makes that difficult. disk and ball have a strict_radius parameter that ellipsis is lacking. Both groups also evaluate the ellipsis equation differently: ellipsis uses <, while the other functions use <=. But even worse, both groups solve this equation in a different associative form which leads to floating errors compounding differently.

While getting to the root of this, I figured out a prototype footprint_ellipsis function (6f2cb7b04), that can be made to return identical results to the legacy functions. However, we probably don't want to keep parameters like _legacy_sphere_compatible around. But I'm uploading this draft so we can discuss how we want to approach this API change between ski and ski2.

Release note (optional)

...

This function can be made to return identical results to `ellipsis`,
`disk` and `ball` in `skimage.morphology` if its input parameters
are correctly configured. (Haven't tested yet if that is true for
the `strict_radius=` parameter as well.)

However, we probably don't want to keep parameters like
`_legacy_sphere_compatible` around. But I'm uploading this prototype
so we can discuss how we want to approach this API change between
ski and ski2.

Same is true for the `compare_func` parameter which may be overkill?
But I think it is more elegant than a boolean flag.
@lagru lagru self-assigned this Jul 10, 2026
@lagru lagru added the 🥾 Path to skimage2 A step towards the new "API 2.0" label Jul 10, 2026
@github-project-automation github-project-automation Bot moved this to Pitched in Work pitches Jul 10, 2026
@greptile-apps

greptile-apps Bot commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

Reviews (1): Last reviewed commit: "Add fully compatible `footprint_ellipsis..." | Re-trigger Greptile

Comment thread src/_skimage2/morphology/footprints.py Resolved
Comment thread src/_skimage2/morphology/footprints.py Resolved
Comment thread src/_skimage2/morphology/__init__.pyi Resolved
@lagru lagru moved this from Pitched to Doing in Work pitches Jul 10, 2026
@greptile-apps

greptile-apps Bot commented Jul 11, 2026

Copy link
Copy Markdown
Contributor

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

Comment thread src/_skimage2/morphology/footprints.py Outdated
continue

# Create coordinate space along current dimension
radius = length // 2 # Integer division for legacy compatibility

@lagru lagru Jul 11, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This is another wart forced by the legacy implementation which also uses integer division.

I'd argue that float division would be more intuitive, since there is no reason to artificially shrink the radius by 0.5 here. This implementation has no trouble placing the edge of the ellipse exactly on the image border.

However, I saw no easy way to recover legacy results when using float division here. In theory using

adjust_radii = tuple(1 / (s // 2 * 2) + 1 for s in shape)

should reproduce the results from _ellipse_in_shape but I think at larger radii it is running into float errors.

Not sure yet, if it's worth the same treatment as the other differences we allow ourselves with legacy ball and disk...

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I elected got keep using integer division here since that seems to align with what other libraries (Matlab, imagemagick) are doing.

Conflicts:
src/skimage/morphology/footprints.py
@greptile-apps

greptile-apps Bot commented Jul 11, 2026

Copy link
Copy Markdown
Contributor

Reviews (3): Last reviewed commit: "Merge branch 'main' into sk2-footprint-e..." | Re-trigger Greptile

@lagru
lagru marked this pull request as ready for review August 23, 2026 14:47
Comment thread src/_skimage2/filters/rank/bilateral.py Resolved
@greptile-apps

greptile-apps Bot commented Aug 23, 2026

Copy link
Copy Markdown
Contributor

Reviews (6): Last reviewed commit: "Avoid failure due to expected_warnings i..." | Re-trigger Greptile

Comment on lines +182 to +183
* Instead of ``strict_radius=True`` (default), use ``adjust_edge=0.001``
* Instead of ``strict_radius=False``, use ``adjust_edge=0.5``

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I'd like to get a second opinion here since this transition is the only time where we can freely chose this default.

The current default is adjust_edge=0.4 which I think leads to nicer looking footprints. I got the idea from imagemagick which recommends adding fractions between 0.3 and 0.5.

We could use adjust_edge=0.5 as the default, which would approximate the legacy behavior of strict_radius=False. But strict_radius=True is the default, so users need to adapt the signature in any case. What do you think?

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.

Using 0.4 is fine; we can advise the user on how to get 0.5 in the docs.

lagru added 7 commits August 25, 2026 20:10
Between skimage and skimage2, we are removing disk/ball in favor of
footprint_ellipse. So we need to duplicate docstring examples that make
use of these APIs. This is too complex for `adapt_doctests`. So create
thin wrappers in `skimage.filters.`, copy back the old docstrings.
and leave note about intentionally left implementation in there.
Conflicts:
src/_skimage2/morphology/__init__.pyi
src/_skimage2/morphology/footprints.py
src/skimage/morphology/footprints.py
tests/skimage2/morphology/test_footprints.py
@lagru lagru moved this from Doing to Review in Work pitches Aug 26, 2026
@greptile-apps

greptile-apps Bot commented Aug 26, 2026

Copy link
Copy Markdown
Contributor

Reviews (7): Last reviewed commit: "Merge branch 'main' into sk2-footprint-e..." | Re-trigger Greptile

@greptile-apps

greptile-apps Bot commented Aug 30, 2026

Copy link
Copy Markdown
Contributor

Reviews (8): Last reviewed commit: "Add migration wrapper for `footprint_fro..." | Re-trigger Greptile

Comment on lines +723 to +726
qname_old="skimage.morphology.disk",
qname_new="skimage.morphology.footpint_ellipse",
)
def disk(radius, dtype=np.uint8, *, strict_radius=True, decomposition=None):

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.

P1 Typo in qname_new for disk's migration decorator — footpint_ellipse is missing the letter r. Every user who calls disk(...) will see the wrong function name in the deprecation warning, leading them to try to import footpint_ellipse and hit a ImportError.

Suggested change
qname_old="skimage.morphology.disk",
qname_new="skimage.morphology.footpint_ellipse",
)
def disk(radius, dtype=np.uint8, *, strict_radius=True, decomposition=None):
qname_old="skimage.morphology.disk",
qname_new="skimage.morphology.footprint_ellipse",
)
def disk(radius, dtype=np.uint8, *, strict_radius=True, decomposition=None):

@stefanv stefanv 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.

Looks good so far!

from skimage._doctest_adapters import adapt_doctests

adapt_doctests(globals())
def mean_bilateral(

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.

This is part of the PR?

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.

Some other parts also seem like they may be unrelated.

@stefanv

stefanv commented Sep 1, 2026 •

Copy link
Copy Markdown
Member

Community call: drop shape, drop adjust_edge, recommend adjustment of radius in docs.

EDIT: realized that this is a double operation: increase radius + crop footprint.

So, we'll settle on a predictable outcome size: ceil(2*r + 1).
And then a dedicated parameter: adjust_edge=0.4, which gets added to radius, but does not impact shape.

It's not necessary to keep old functionality and complicates the API
and behavior significantly for something that's probably rarely used.
@greptile-apps

greptile-apps Bot commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

Reviews (9): Last reviewed commit: "Drop `shape` parameter from `footprint_e..." | Re-trigger Greptile

Comment on lines +890 to +892
qname_old="skimage.morphology.ball",
qname_new="skimage.morphology.footpint_ellipse",
)

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.

P1 The ball migration decorator shares the same typo as disk — footpint_ellipse is missing the r. Every call to ball(...) will produce a deprecation warning pointing users to skimage.morphology.footpint_ellipse, which does not exist, causing them to hit an ImportError when following the migration guide.

Suggested change
qname_old="skimage.morphology.ball",
qname_new="skimage.morphology.footpint_ellipse",
)
qname_old="skimage.morphology.ball",
qname_new="skimage.morphology.footprint_ellipse",
)

@lagru

lagru commented Sep 2, 2026

Copy link
Copy Markdown
Member Author

I dropped the shape parameter in 595c8f3 like we discussed.

So, we'll settle on a predictable outcome size: ceil(2*r + 1).
And then a dedicated parameter: adjust_edge=0.4, which gets added to radius, but does not impact shape.

The current implementation explicitly doesn't allow passing floats to radii (see _normalize_radii).

I'm happy to be convinced that the ceil behavior is a good idea. But currently I think it results in a worse function. If we allowed floating radii, there would be two parameters controlling the edge behavior. It would be harder to explain the effects of adjust_edge. adjust_edge=0 would no longer yield an ellipsoid touching the array border exactly, adjust_edge=0.4 wouldn't be a reasonable default for all cases anymore.

Right now it feels way cleaner to me: radii denotes the rough extend of the ellipsoid and the exact shape of the footprint array (radius * 2 + 1), adjust_edge only affects the sampling of the ellipsoid at its edge.

Comment on lines +639 to +641
# Intentionally left this old implementation in place so that
# compatibility tests comparing skimage and skimage2 behavior are
# meaningful

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Making sure you saw this @stefanv.


"""
if decomposition is None:
# Intentionally left this old implementation in place so that

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 think it would be helpful to add here what to get rid of and when.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Keep these as long as we keep the compatibility tests. Keep the tests, as long as we think we want to monitor the behavior of the ski2 algorithm to the legacy ones.

So I'd say, keep around until we remove the skimage namespace in question as part of the second phase.

Comment on lines +182 to +183
* Instead of ``strict_radius=True`` (default), use ``adjust_edge=0.001``
* Instead of ``strict_radius=False``, use ``adjust_edge=0.5``

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.

Using 0.4 is fine; we can advise the user on how to get 0.5 in the docs.

Parameters
----------
radius : int
The radius of the disk-shaped footprint.

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.

OK with this being an int for now.

Is there any way for the user to get the behavior they would have gotten from radius=9.5 were floats allowed?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Not sure I follow the question as you are commenting on the old skimage API?

The skimage1 code allows passing floats but this undocumented behavior isn't very straightforward: np.arange effectively computes the shape as ceil(radius) * 2 – which is of even size!

The new skimage2 API provides no way to support/reproduce that behavior since adjust_edge explicitly doesn't affect the shape.

If you want to support this, we'd need to add back (a) the optional shape parameter that I had proposed earlier, or (b), we allow floats for the radii parameter.

But (b) seems like a rather messy API with regards to how shape is computed.

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

🥾 Path to skimage2 A step towards the new "API 2.0"

Projects

Status: Review

Development

Successfully merging this pull request may close these issues.

3 participants