Skip to content
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

Emit event from Points data setter #6117

Merged
merged 6 commits into from Aug 8, 2023
Merged

Conversation

jni
Copy link
Member

@jni jni commented Aug 4, 2023

Closes #6116

Description

Adds back an event emission from the Points layer data setter. Commits summary:

  • Add failing test for layer data setter event
  • Emit event on points data setter

Type of change

  • Bug-fix (non-breaking change which fixes an issue)

@jni jni added the bugfix PR with bugfix label Aug 4, 2023
@github-actions github-actions bot added the tests Something related to our tests label Aug 4, 2023
@jni jni added this to the 0.4.19 milestone Aug 4, 2023
@jni jni requested review from Czaki and melonora August 4, 2023 08:01
@codecov
Copy link

codecov bot commented Aug 4, 2023

Codecov Report

Merging #6117 (3be54a7) into main (9186881) will increase coverage by 0.00%.
The diff coverage is 100.00%.

❗ Current head 3be54a7 differs from pull request most recent head 87331e1. Consider uploading reports for the commit 87331e1 to get more accurate results

@@           Coverage Diff           @@
##             main    #6117   +/-   ##
=======================================
  Coverage   91.61%   91.62%           
=======================================
  Files         579      579           
  Lines       50636    50656   +20     
=======================================
+ Hits        46392    46414   +22     
+ Misses       4244     4242    -2     
Files Changed Coverage Δ
napari/layers/points/_tests/test_points.py 100.00% <100.00%> (ø)
napari/layers/points/points.py 97.74% <100.00%> (+<0.01%) ⬆️

... and 1 file with indirect coverage changes

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.

I think I found out why I removed the line you mentioned. When testing using this code:

import napari


def test(event):
    print(event.data_indices)
    print(event.vertex_indices)
    print(event.action)

viewer = napari.Viewer()
viewer.add_points()
viewer.layers[0].events.data.connect(test)
napari.run()

if you now start adding points you see that each time there are 2 emits. One coming from the data.setter which actually gives wrong information. I am not certain why in the shapes layer having the line self.events.data(value=self.data) in the setter does not gives issues while something similar in Points does. I will have a look after the break, but at least it seems to be the case that the data setter gets called every time the data is changed.

@jni
Copy link
Member Author

jni commented Aug 4, 2023

Ah, that's ok, we've encountered situations like this in the past — the trick is then to create a "set data quietly" private method that all the emitting methods use, and then the setter simply uses that method and then emits.

The editing methods are using the setter because NumPy arrays aren't expandable — we could/should optimise that with an expandable array but it's never happened 😅, because NumPy is fast enough when creating an array with a million points anyway! 😅

@melonora
Copy link
Contributor

melonora commented Aug 4, 2023

Ah, that's ok, we've encountered situations like this in the past — the trick is then to create a "set data quietly" private method that all the emitting methods use, and then the setter simply uses that method and then emits.

The editing methods are using the setter because NumPy arrays aren't expandable — we could/should optimise that with an expandable array but it's never happened 😅, because NumPy is fast enough when creating an array with a million points anyway! 😅

If I understand correctly, the difference with Shapes is that here the data is edited in layer._data_view which is still separate from the data.setter. Am I correct?

@jni
Copy link
Member Author

jni commented Aug 6, 2023

@melonora here you go, added a regression test and everything. 😊

Copy link
Collaborator

@Czaki Czaki left a comment

Choose a reason for hiding this comment

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

I think that we should use unittest Mock in tests instead of append on list.

@jni
Copy link
Member Author

jni commented Aug 6, 2023

... because?

I'm not really sure how Mock would improve things here. As far as I can tell the list tests exactly what it purports to test?

@melonora
Copy link
Contributor

melonora commented Aug 6, 2023

Thanks @jni! I am happy with the change, but do have one question where I am not certain whether it would be something to change as part of this PR. The data event in the data.setter of shapes is different now from the data.setter for points. I would have data emitted in the same manner, where for the consistent API I would have the data.setter of Points be adapted by Shapes.

@jni
Copy link
Member Author

jni commented Aug 6, 2023

@melonora i think that should come in a separate PR. It's good to keep PRs as compact as possible to achieve what they need to do (in this case, fix a bug in the points layer). Maybe make an issue?

@melonora
Copy link
Contributor

melonora commented Aug 7, 2023

@melonora i think that should come in a separate PR. It's good to keep PRs as compact as possible to achieve what they need to do (in this case, fix a bug in the points layer). Maybe make an issue?

Yeah I agree, I will open an issue and will approve this PR, though lets wait for the response of @Czaki before starting the clock.

Copy link
Collaborator

@Czaki Czaki left a comment

Choose a reason for hiding this comment

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

Readability?

napari/layers/points/_tests/test_points.py Outdated Show resolved Hide resolved
napari/layers/points/_tests/test_points.py Outdated Show resolved Hide resolved
Co-authored-by: Grzegorz Bokota <bokota+github@gmail.com>
@jni
Copy link
Member Author

jni commented Aug 7, 2023

Thanks for the suggestions. 🙏

As someone who is unfamiliar with Mock but intimately familiar with lists (which I suspect describes many Python users), I actually find this less readable. But I don't feel strongly about it at all. This should be ready! 🚀

@Czaki
Copy link
Collaborator

Czaki commented Aug 7, 2023

For me, Mock is more readable, as I do not need to parse connected functions.

@Czaki Czaki added the ready to merge Last chance for comments! Will be merged in ~24h label Aug 7, 2023
@melonora
Copy link
Contributor

melonora commented Aug 8, 2023

In it goes

@melonora melonora merged commit 3cbd0d0 into napari:main Aug 8, 2023
30 checks passed
@jni jni deleted the fix-points-data-event branch August 9, 2023 00:40
@melonora melonora mentioned this pull request Aug 10, 2023
8 tasks
melonora added a commit that referenced this pull request Aug 18, 2023
# Fixes/Closes

Closes #6125 

# Description
With #6117 the emitted data in the `data.setter` of `Points` has been
made more consistent with the emitted data when adding, changing or
removing data within the layer. However, in `Shapes` only the data
itself was emitted. Furthermore,
since the `data.setter` in Shapes called the public `.add` method and
emits data itself, a `layers.data` event was emitted twice.
This PR is a slight refactor so that `data.setter` can directly call the
private `_add_shapes` method, bypassing the `layer.data` emit in `.add`.


## Type of change

- [x] Bug-fix (non-breaking change which fixes an issue)
- [x] Maintenance (changes required to run napari, tests, & CI smoothly)

# How has this been tested?

- [x] example: the test suite for my feature covers cases x, y, and z
- [x] example: all tests pass with my change
- [x] example: I check if my changes works with both PySide and PyQt
backends
      as there are small differences between the two Qt bindings.  

## Final checklist:
- [x] My PR is the minimum possible work for the desired functionality
- [x] I have added tests that prove my fix is effective or that my
feature works
- [x] If I included new strings, I have used `trans.` to make them
localizable.
For more information see our [translations
guide](https://napari.org/developers/translations.html).
@Czaki Czaki removed the ready to merge Last chance for comments! Will be merged in ~24h label Aug 21, 2023
Czaki added a commit that referenced this pull request Oct 17, 2023
Closes #6116

# Description

Adds back an event emission from the Points layer data setter. Commits
summary:

- Add failing test for layer data setter event
- Emit event on points data setter

## Type of change
<!-- Please delete options that are not relevant. -->
- [x] Bug-fix (non-breaking change which fixes an issue)

---------

Co-authored-by: Grzegorz Bokota <bokota+github@gmail.com>
Czaki pushed a commit that referenced this pull request Oct 17, 2023
# Fixes/Closes

Closes #6125 

# Description
With #6117 the emitted data in the `data.setter` of `Points` has been
made more consistent with the emitted data when adding, changing or
removing data within the layer. However, in `Shapes` only the data
itself was emitted. Furthermore,
since the `data.setter` in Shapes called the public `.add` method and
emits data itself, a `layers.data` event was emitted twice.
This PR is a slight refactor so that `data.setter` can directly call the
private `_add_shapes` method, bypassing the `layer.data` emit in `.add`.


## Type of change

- [x] Bug-fix (non-breaking change which fixes an issue)
- [x] Maintenance (changes required to run napari, tests, & CI smoothly)

# How has this been tested?

- [x] example: the test suite for my feature covers cases x, y, and z
- [x] example: all tests pass with my change
- [x] example: I check if my changes works with both PySide and PyQt
backends
      as there are small differences between the two Qt bindings.  

## Final checklist:
- [x] My PR is the minimum possible work for the desired functionality
- [x] I have added tests that prove my fix is effective or that my
feature works
- [x] If I included new strings, I have used `trans.` to make them
localizable.
For more information see our [translations
guide](https://napari.org/developers/translations.html).
@Czaki Czaki changed the title Emit event from points data setter Emit event from Points data setter Oct 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugfix PR with bugfix tests Something related to our tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Regression: Points.events.data is no longer fired from the data setter
3 participants