8000 Prevent Shapes corruption when drawing tiny polygons with lasso by Czaki · Pull Request #7914 · napari/napari · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Prevent Shapes corruption when drawing tiny polygons with lasso #7914

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 15, 2025

Conversation

Czaki
Copy link
Collaborator
@Czaki Czaki commented May 13, 2025

References and relevant issues

Closes #7903

Description

Fix bug in adding shapes, by first removing points based on epsilon, then checking if there are enough point to create a polygon.

@Czaki Czaki added this to the 0.6.1 milestone May 13, 2025
@Czaki Czaki added the bugfix PR with bugfix label May 13, 2025
@Czaki
Copy link
Collaborator Author
Czaki commented May 13, 2025

Should I add tests?

Copy link
codecov bot commented May 13, 2025

Codecov Report

Attention: Patch coverage is 83.33333% with 1 line in your changes missing coverage. Please review.

Project coverage is 92.92%. Comparing base (366ceed) to head (697b630).
Report is 5 commits behind head on main.

Files with missing lines Patch % Lines
napari/layers/shapes/shapes.py 83.33% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #7914      +/-   ##
==========================================
+ Coverage   92.90%   92.92%   +0.01%     
==========================================
  Files         643      643              
  Lines       60663    60673      +10     
==========================================
+ Hits        56360    56381      +21     
+ Misses       4303     4292      -11     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@TimMonko
Copy link
Contributor

Hmm, I'd wait to add tests until we like the implementation, speaking of which:

This PR does prevent the current error message(s) in #7914, but I'm not sure if this is the behavior that we want. Below shown with default RDP epsilon, notice that small shapes dissappear (even some that are a few pixels in size) as well as the long line structures that @coreyelowsky see users cause the error on.

python_zgEoPoXhEK

  1. Do we want such small lasso'd shapes to dissappear? I think this could be confusing for somebody, However, lowering RDP epsilon to 0 does keep the small shapes, so it's still possible to get that behavior if desired (we would just need to document this better)
  2. Do we want these line-like lasso'd shapes to dissapear? Again this could be confusing

However, personally I like this behavior as I think it will result in only thoughtful shapes being drawn. And this won't be a regression from current behavior, so the only thing this PR ultimately does is prevent the error -- as previously the shape also wouldn't be drawn, and the layer would break.

@Czaki
Copy link
Collaborator Author
Czaki commented May 13, 2025

However, personally I like this behavior as I think it will result in only thoughtful shapes being drawn. And this won't be a regression from current behavior, so the only thing this PR ultimately does is prevent the error -- as previously the shape also wouldn't be drawn, and the layer would break.

I think that it is a practical solution for 0.6.1. All others should land in rdp function

Copy link
Contributor
@TimMonko TimMonko left a comment

Choose a reason for hiding this comment

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

I'm approving and adding ready to merge, hopefully we can get an additional point of feedback. I would like this to be included in 0.6.1 if others are ok with the implementation as-is.

@TimMonko TimMonko added the ready to merge Last chance for comments! Will be merged in ~24h label May 13, 2025
Copy link
Member
@jni jni left a comment

Choose a reason for hiding this comment

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

I actually agree with @TimMonko's original point that this doesn't actually fix the issue. To me, it feels more broken than before, because it's silently breaking. Since this is not a bug introduced in 0.6.0 but actually dates back to the original implementation of lasso, I don't think it's critical to get this in 0.6.1, and I'd rather we took a bit more time to think about a better solution.

@jni jni removed the ready to merge Last chance for comments! Will be merged in ~24h label May 13, 2025
@jni
Copy link
Member
jni commented May 13, 2025

Is there a lasso section in the docs? I think the best thing to do for 0.6.1 is to add an admonition in the docs about the RDP setting for small lasso shapes.

@psobolewskiPhD
Copy link
Member

There actually is extensive documentation for the lasso.
Go to:
https://napari.org/stable/howtos/layers/shapes.html#buttons
then scroll and expand the details

@psobolewskiPhD
Copy link
Member
psobolewskiPhD commented May 13, 2025

So the question is what are the units on our RDP epsilon?
And how big of a performance hit is setting 0 as the default?

@Czaki
Copy link
Collaborator Author
Czaki commented May 13, 2025

And how big of a performance hit is setting 0 as the default?

Without compiled backend may be huge.

Units are layer units. 1 is one pixel on image layer.

@Czaki
Copy link
Collaborator Author
Czaki commented May 13, 2025

To me, it feels more broken than before, because it's silently breaking.

Did you try to reproduce the original bug? It just blocks the Shapes layer without a way to fix. You need to remove it and start drawing from scratch...

Ad I think that I do not see a good way to fix it. Because we point that, we remove every point that is less than 0.5 pixel from the line to smooth it. And it works as described.

It is something like set black background in #7904.

@jni
Copy link
Member
jni commented May 13, 2025

So the question is what are the units on our RDP epsilon?

Units are layer units. 1 is one pixel on image layer.

We call these world coordinates. (ie if the image has a scale, it's not 1 pixel, it's 1 "world unit".)

@jni
Copy link
Member
jni commented May 13, 2025

Did you try to reproduce the original bug? It just blocks the Shapes layer without a way to fix. You need to remove it and start drawing from scratch...

At least, there should be a warning. My point is that in the previous case, it was broken, but it was broken enough that you would probably come to the website and raise an issue, and you could then get help. With the silent break it just looks like the feature is broken, and you might just conclude that napari is not the right tool.

@psobolewskiPhD
Copy link
Member

So one fix would be to use screen pixels for the epsilon, thus accounting for zoom?
That way if you zoom in a lot and draw fine structures you won't trigger this? It feels unexpected to be based off of image pixels and not the pixels of the vertexes you are actually placing.

@TimMonko TimMonko modified the milestones: 0.6.1, 0.6.2 May 14, 2025
@Czaki
Copy link
Collaborator Author
Czaki commented May 14, 2025

I have added a warning. It is calling show_warning that is not a good practice, but the best we could do now.

napari-2025-05-14_08.18.05.mp4

So one fix would be to use screen pixels for the epsilon, thus accounting for zoom?

To do this, we need to do total refactor of whole napari code. We already push too much information about rendering state into the layer.

Also, the problem could be triggered on any scale if user draws a straight line.

napari-2025-05-14_08.31.52.mp4

@jni
Copy link
Member
jni commented May 14, 2025

It is calling show_warning that is not a good practice, but the best we could do now.

Doesn't using warnings.warn surface the warning automagically?

Alternatively, with the new logger, maybe a log entry is a better idea…

Comment on lines 2657 to 2659
'Polygon must have at least 3 vertices. With current rep epsilon value {eps},'
' the {removed_vertices} vertices were removed.',
'Cannot create polygon.',
Copy link
Member
8000

Choose a reason for hiding this comment

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

Whether we end up using show_warning, warnings.warn, or logging.log (could we hook up log warnings and errors to our notifications?), here is my suggestion for the message:

Polygons must have three vertices. Lasso polygons are simplified using an algorithm called RDP, which may cause polygons smaller than the RDP epsilon parameter to disappear entirely. You can change this parameter in napari > Settings > Experimental > RDP epsilon.

Maybe this is better in the docs and then we can just link to the docs:

Simplified polygon has too few vertices. See [link] for details.

@Czaki
Copy link
Collaborator Author
Czaki commented May 14, 2025

Doesn't using warnings.warn surface the warning automagically?

A warning is emitted one time. We want to show information every time user meet the problem.

Logging is not currently displayed to user. And it is out of the scope of this PR to do this.

@TimMonko
Copy link
Contributor

Thanks for the great notes from the community meeting! Looks like y'all talked about sov much. What's the sense of how we want to move forward with the lasso bug for 0.6.1. I see merit to both sides, but would prefer a solution that doesn't break someone's hard work in a shapes layer, but does give them feedback on what might be happening. It looks like a warning has been added. Are we happy with that implementation or need to iterate more? Happy to play later when I can.

Copy link
Member
@jni jni left a comment

Choose a reason for hiding this comment

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

@TimMonko yeah @Czaki convinced me that the bug is severe enough (totally broken Shapes layer, which could lose hours of manual annotation work) that it should get fixed, even if we're not happy with the messaging infrastructure being used currently. Let's get this in to 0.6.1 now that there is a warning, and then iterate in later versions.

This is what the warning looks like when you expand the toast, I think it's pretty good:

CleanShot 2025-05-14 at 23 17 14@2x

@jni jni modified the milestones: 0.6.2, 0.6.1 May 14, 2025
@jni jni added the ready to merge Last chance for comments! Will be merged in ~24h label May 14, 2025
@jni
Copy link
Member
jni commented May 14, 2025

(I still like my idea linking to the docs but I won't get a chance to update them tonight. I think let's just ship this and iterate docs, message, and code in 0.6.2+. But whatever folks want to do while I'm asleep is ok by me 😂)

@Czaki
Copy link
Collaborator Author
Czaki commented May 14, 2025

I have tested and it looks like adding a link will require to update QElidingLabel that comes from supertqt so maybe in 0.6.2

@TimMonko
Copy link
Contributor

Good good, glad to see we are moving forward with this. I'll add this to rc1 tonight.

@jni jni changed the title Fix drawing polygon on small area Prevent Shapes corruption when drawing tiny polygons with lasso May 15, 2025
@TimMonko
Copy link
Contributor

@jni for me its cut off :/
image
any suggestions?

Co-authored-by: Tim Monko <timmonko@gmail.com>
@jni
Copy link
Member
jni commented May 15, 2025

Nice condensification @TimMonko! I've committed it. 👍

@TimMonko TimMonko merged commit c6c7132 into napari:main May 15, 2025
30 checks passed
@github-actions github-actions bot removed the ready to merge Last chance for comments! Will be merged in ~24h label May 15, 2025
@jni jni deleted the fix_7903 branch May 15, 2025 04:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugfix PR with bugfix
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Error when drawing with polygon lasso tool in shapes layer while very zoomed in
4 participants
0