Skip to content

Conform conversion of non-binary input in skeletonize functions - #7095

Merged
mkcor merged 7 commits into
scikit-image:mainfrom
lagru:binary-skeletonize
Nov 30, 2023
Merged

mkcor merged 7 commits into
scikit-image:mainfrom
lagru:binary-skeletonize

Conversation

@lagru

@lagru lagru commented Aug 15, 2023 •

Copy link
Copy Markdown
Member

Description

Our skeletonize API is currently dealing in multiple ways with non-binary input images. Sometimes it's just silently turned into a binary array and sometimes img_as_ubyte is used (which scales).

This PR adds a RuntimeWarning everywhere if the input image isn't binary, and conforms the conversion to image.astype(bool, copy=False). This is probably the least controversial solution to #7090.

This PR conforms the behavior. All functions convert non-binary input implicitly with .astype(bool, copy=False).

What do you think about deprecating the implicit conversion and raising a TypeError instead? That would make testing and maintenance a lot easier!

Closes #7090.

Checklist

Release note

Summarize the introduced changes in the code block below in one or a few sentences. The
summary will be included in the next release notes automatically:

Remove unexpected value scaling in `skimage.morphology.skeletonize_3d` for non-binary input images. `skeletonize_3d` now always returns a binary array like similar functions.

@lagru lagru added the 📜 type: API Involves API change(s) label Aug 15, 2023
jni
jni previously approved these changes Aug 16, 2023
@jni

jni commented Aug 16, 2023

Copy link
Copy Markdown
Member

It's not as uncontroversial as you think @lagru 😂 as I generally like the robustness principle ("be conservative in what you send, be liberal in what you accept"), and warnings are annoying, but I'm certainly happy to listen to the criticisms section there, so happy to go with this solution.

I don't think we need to deprecate in 1.0, but maybe we can just remove it in skimage2?

@lagru

lagru commented Aug 25, 2023

Copy link
Copy Markdown
Member Author

Theoretical rambling ahead

I sympathize with the robustness principle from a short-term user perspective. But from a maintainer perspective and long-term user perspective, I don't think "be liberal in what you accept" is a good trade-off in a lot of cases. It makes the API surface larger which can be problematic for all kinds of reasons, not the least because it makes API changes more difficult. Documentation of parameters and behavior is generally more complex and the number of (unforseen) edge cases increases as well.


That said, if I think about this issue again I mainly hate the inconsistency between behavior, and documentation. Either we accept non-binary arrays and document that or we don't. Users will start to rely on either behavior anyway. So I changed my opinion and don't think the warning is the proper way to deal with this.

but add a minimal test for at least int and float dtype.
@lagru

lagru commented Aug 25, 2023 •

Copy link
Copy Markdown
Member Author

I propose accepting non-binary images for now as implied by the docstring of skeletonize already but conforming the behavior of all those functions to .astype(bool, copy=False). Seems most pragmatic to me right now. Sorry for the noise.

But I'll the "convert non-binary images implicitly" issue as an item to the list of API changes for skimage2 so that we can discuss it again and in context of the full library.

@lagru lagru changed the title Warn about non-binary input in skeletonize functions Conform conversion of non-binary input in skeletonize functions Aug 25, 2023
@lagru
lagru requested a review from jni August 25, 2023 16:19
@lagru
lagru dismissed jni’s stale review August 25, 2023 16:20

Outdated

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

Thanks for addressing this inconsistency, @lagru!

Comment thread skimage/morphology/_skeletonize.py Resolved
Comment thread skimage/morphology/_skeletonize.py Resolved
Comment thread skimage/morphology/_skeletonize.py Resolved
@jni

jni commented Nov 28, 2023

Copy link
Copy Markdown
Member

But I'll the "convert non-binary images implicitly" issue as an item to the list of API changes for skimage2 so that we can discuss it again and in context of the full library.

That's great from my perspective, @lagru. It's amusing that shortly after I came here defending the robustness principle, it bit us in the ass in napari. That's karma for you! 😂 So I now 100% agree with strongly typing this in skimage2. But it's a good one to postpone/deprecate slowly rather than to just warn.

Comment thread skimage/morphology/_skeletonize.py Resolved
lagru and others added 2 commits November 28, 2023 15:42
Co-authored-by: Marianne Corvellec <[email protected]>
Co-authored-by: Marianne Corvellec <[email protected]>
@mkcor

mkcor commented Nov 29, 2023

Copy link
Copy Markdown
Member

@lagru in the context of these functions, are thin/thinned and skeletonize/skeletonized strict synonyms?

@lagru

lagru commented Nov 29, 2023

Copy link
Copy Markdown
Member Author

I was asking that myself. I am not sure. From Wikipedia on Topological skeleton

In the technical literature, the concepts of skeleton and medial axis are used interchangeably by some authors,[1][2] while some other authors[3][4][5] regard them as related, but not the same. Similarly, the concepts of skeletonization and thinning are also regarded as identical by some,[2] and not by others.[3]

So, I suggest to use "thin" at least in thin and skeletonize in medial_axis. That keeps the former status quo.

@mkcor
mkcor merged commit 604d077 into scikit-image:main Nov 30, 2023
@stefanv stefanv added this to the 0.23 milestone Nov 30, 2023
@lagru
lagru deleted the binary-skeletonize branch December 1, 2023 11:13
@lagru lagru mentioned this pull request Apr 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

📜 type: API Involves API change(s)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

skeletonize_3d output pixel values are doubled from input pixel values

4 participants