8000 Move the _is_created assignment to the top by mstabrin · Pull Request #5078 · napari/napari · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Move the _is_created assignment to the top #5078

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 16 commits into from
Jul 16, 2024

Conversation

mstabrin
Copy link
Contributor
@mstabrin mstabrin commented Sep 14, 2022

Description

Now playing with the Shapes layer, I found an issue with the _is_creating flag.
I am creating a plugin which is connecting the set_data event of the layer to identify when data is changed.
However, during the creation of a Path shape (should also happen for Polygon) I want to wait until the editing is finished and skip the update if it is still in creation.

Therefore, I use:

...
    layer.events.set_data.connect(self.my_func)
...

def my_func(self, event):
     if event.source._is_creating:
          return
      ....
....

But it turns out, that I cannot rely on the _is_creating flag, because it is set too late in the drawing process.
Therefore, I propose to move the assignment before the creation of the very first vertex.

Before (The False after the very first click on the left side):

before

After:

after

Addition (The True after the very first click on the left side):

If one clicks two times while polygon or path is selected, an error is thrown. I also added a test and a fix for this scenario.

Type of change

  • Bug-fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)

@github-actions github-actions bot added the tests Something related to our tests label Sep 14, 2022
@codecov
Copy link
codecov bot commented Sep 14, 2022

Codecov Report

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

Please upload report for BASE (main@883b72c). Learn more about missing BASE report.

Files Patch % Lines
napari/layers/shapes/_shapes_mouse_bindings.py 66.66% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main    #5078   +/-   ##
=======================================
  Coverage        ?   92.88%           
=======================================
  Files           ?      618           
  Lines           ?    56484           
  Branches        ?        0           
=======================================
  Hits            ?    52465           
  Misses          ?     4019           
  Partials        ?        0           

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

@github-actions
Copy link
Contributor

Click here to download the docs artifacts
docs
(zip file)

@brisvag
Copy link
Contributor
brisvag commented Sep 15, 2022

Sounds like a reasonable change! However, should we then somehow expose this publicly a bit more clearly? Maybe by having a separate event for like shape_completed?

@mstabrin
Copy link
Contributor Author

To be honest I am not sure if a shape_completed event will make it more clearl as it furthers separates the way Point and Shapes layers and different Shapes within the Shapes layer behave.
I would rather add a flag about the current state of the event much like we did with the parent attribute in the Source layer: #5028

That way the information would be present in the set_data event itself?

@brisvag
Copy link
Contributor
brisvag commented Sep 15, 2022

Yeah, maybe we can somehow stick this in the Event object, which does have machinery to handle arbitrary kwargs. I'm not super familiar with its usage in napari, though. @Czaki maybe has some insight?

@Czaki
Copy link
Collaborator
Czaki commented Sep 15, 2022

That way the information would be present in the set_data event itself?

You could pass arbitrary kwargs when emmit event. But You need to update the docstring and all places where the event is emitted.

@mstabrin
Copy link
Contributor Author

Hello @brisvag and @Czaki :)
What is the status of this change?
Shall we first keep it as it is or should we work on a solution which includes this into the Event itself?

@brisvag
Copy link
Contributor
brisvag commented Oct 27, 2022

I would suggest to go directly for the Event approach, and do as @Czaki suggested and simply add a kwarg to this event, called completed or something like that.

@jni
Copy link
Member
jni commented May 25, 2023

@Czaki @brisvag given the long wait time on this and that this solves a real issue and has tests to go with it, what do you think about merging it as-is and leaving a proper fix until a later PR?

@jni jni added the bugfix PR with bugfix label May 25, 2023
Copy link
Contributor
@brisvag brisvag left a comment

Choose a reason for hiding this comment

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

Yeah, definitely fine to merge as is!

# If there was no move event between two clicks value[1] is None
# and needs to be taken care of.
if value[1] is None:
value = layer._moving_value
Copy link
Contributor

Choose a reason for hiding this comment

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

Does any of this have a negative effect on the layer._last_cursor_position = np.array(event.pos) line in the add_vertex_to_path function? That's the only line in this function that didn't originally exist when this PR was first created. I assume the tests will pick up any problems?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, it won't be reset to None after completing drawing the path when clicking at the same position twice with Add path enabled.

Copy link
Contributor

Choose a reason for hiding this comment

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

This seems not to be an issue anymore as clicking twice will always create a small line.

@GenevieveBuckley
Copy link
Contributor

Discussed at the PR party, we'd like @melonora to weigh in also.

@andy-sweet
Copy link
Member

If we do want to merge this, I pushed some changes to use read_only_mouse_event instead of ReadOnlyWrapper. The tests changed passed locally, so hopefully they pass on CI too.

@GenevieveBuckley GenevieveBuckley added the ready to merge Last chance for comments! Will be merged in ~24h label Mar 1, 2024
@Czaki Czaki added this to the 0.5.0 milestone Mar 1, 2024
@melonora
Copy link
Contributor
melonora commented Mar 1, 2024

Discussed at the PR party, we'd like @melonora to weigh in also.

I did have another look. The approach that @Czaki has suggested has been implemented in the meantime and is documented.

The use case of the user is covered by this:

from napari import Viewer
import napari

def add_data(event):
    if event.action == "added":
        print("Added")

viewer = Viewer()
viewer.add_shapes()
viewer.layers[0].events.set_data.connect(set_data)
viewer.layers[0].events.data.connect(add_data)
napari.run()

If you add any shape now it will print added only after the shape has been completed by the user, with the added benefit that this event can also provide you with information of all kinds of events, adding, added, moving, moved etc with information of what data is being changed event.data_indices, event.vertex_indices.

That being said the question with this PR is more if the set_data event on itself is behaving as expected. In the current state on main that is not the case because when you add a shape for initial move, _is_creating is False. This PR fixes that issue. However, the issue of _last_cursor_position not being reset to None when clicking twice when in Add path mode should be resolved.

Copy link
Contributor
@melonora melonora left a comment

Choose a reason for hiding this comment

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

As stated the issue with not None should be resolved before merging.

mouse_release_callbacks(layer, event)

# If there was no move event between the two clicks, we expect value[1] must be None
assert layer.get_value(event.position, world=True)[1] is None
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this a sufficient test for the concern you raised earlier @melonora?

Copy link
Contributor

Choose a reason for hiding this comment

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

I suspect it is not, because it passes without any changes to the code, but I don't quite understand what we need to check if not this exact thing. Can you clarify things for me?

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

@melonora would you have time to update the PR with those changes? It would be good to get this over the line...

@jni jni modified the milestones: 0.5.0, 0.5.1 Jul 8, 2024
@jni
Copy link
Member
jni commented Jul 8, 2024

ping @melonora

Copy link
Contributor
@melonora melonora left a comment

Choose a reason for hiding this comment

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

Given that there is no problem anymore with layer._last_cursor_position this PR is good to merge. The behaviour of drawing the line has changes to always complete a line even when double clicking at the same position.

@melonora melonora added the ready to merge Last chance for comments! Will be merged in ~24h label Jul 8, 2024
@GenevieveBuckley
Copy link
Contributor

Ok, tests are passing, there are three approvals, and it's been marked ready to merge for the last week (seems like plenty of time). Thank you @melonora!

@GenevieveBuckley GenevieveBuckley merged commit c51e291 into napari:main Jul 16, 2024
37 checks passed
@github-actions github-actions bot removed the ready to merge Last chance for comments! Will be merged in ~24h label Jul 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugfix PR with bugfix contribute:pr-party-update-async Prior event tests Something related to our tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants
0