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

Add arrow display styles to Vectors layer #5740

Merged
merged 33 commits into from
Jun 29, 2023
Merged

Add arrow display styles to Vectors layer #5740

merged 33 commits into from
Jun 29, 2023

Conversation

jules-vanaret
Copy link
Contributor

Fixes/Closes

Closes #4149

Description

This is a follow-up of the discussion #4149 in which adding different display style to the vectors of the Vectors layer was requested. As suggested by users @brisvag and @jni, I implemented a ComboBox in the GUI with 3 choices:

  1. rectangle which is the old style of vectors, and currently the default value
  2. triangle which displays vectors as triangles
  3. arrow which displays a "classical" vector shape, with a rectangular shaft and a triangular head

napari_vectors_display_styles

Most of this contribution comes from additional functions to compute vectors meshes in _vispy/layers/vectors.py. Because changes are mainly in the function generate_vector_meshes_2D, there was no need to change anything in the functions _on_data_change or generate_vector_meshes, i.e it works natively with the 3D visualization.

The Vectors layer now provides a string parameter edge_display_style. Right now I check that any input string is one of [rectangle, triangle, arrow] in the setter of edge_display_style (in layers/vectors/vectors.py), but it does not feel like the right place. Also, I am not sure of my use of trans..
I have not included any real tests, and I am pretty sure that I have forgotten to update the description of at least one function...

Type of change

  • New feature (non-breaking change which adds functionality)
  • This change requires a documentation update

Final checklist:

  • My PR is the minimum possible work for the desired functionality
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have added tests that prove my fix is effective or that my feature works
  • If I included new strings, I have used trans. to make them localizable.

@jules-vanaret jules-vanaret changed the title Added different display styles to the vectors of the Vectors layer #4149 Added different display styles to the vectors of the Vectors layer Apr 17, 2023
@github-actions github-actions bot added the qt Relates to qt label Apr 17, 2023
@psobolewskiPhD
Copy link
Member

That looks great!
Thanks for the contribution!
I've activated CI and requested some reviews!
❤️

@psobolewskiPhD psobolewskiPhD added the feature New feature or request label Apr 17, 2023
@psobolewskiPhD psobolewskiPhD added this to the 0.5.0 milestone Apr 17, 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.

Awesome work! Love that you added the arrows as well, this is so much better than what we have :)

General suggestions:

  • we should use an enum instead of strings to encode the different styles. You can look at how Blending works for examples on how to do that (base layer controls and base layer model).
    • I would also prefer line to rectangle which I find more intuitive
  • I would just call it display_style or even just style or mode rather than edge_display_style... in fact I think we should ditch edge from everything in vectors, but that's another topic ^^' Surely @jni has opinions here

Other than that, I think having some more comments and docstrings in the new code would help reviewing, otherwise the math is a bit hard to follow :)

napari/_vispy/layers/vectors.py Outdated Show resolved Hide resolved
napari/layers/vectors/vectors.py Outdated Show resolved Hide resolved
@brisvag
Copy link
Contributor

brisvag commented Apr 19, 2023

Quick note so I don't forget: make sure that display_out_of_slice still works!

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.

Awesome work! Thanks for the beatiful comments and docstrings, super valuable :) Sorry this slipped through the cracks, I hope you still have time to follow up. Don't hesitate to ping me directly if this happens!

One general issue: It seems that the length parameter interacts only with the position of the tip of the vector. This results in the arrow style being quite deformed by the length change. Try this code and change the length to 10. Even worse, if you go smaller than 1 the arrow folds onto itself xD

import napari
import numpy as np
v = napari.Viewer(ndisplay=3)
a = np.array([[0, 0, 0], [10, 10, 10]])
v.add_vectors(a, vector_style='arrow')

Other than that, I think we're basically ready to merge!

napari/_qt/layer_controls/qt_vectors_controls.py Outdated Show resolved Hide resolved
@github-actions github-actions bot added the tests Something related to our tests label Jun 2, 2023
@jules-vanaret
Copy link
Contributor Author

jules-vanaret commented Jun 2, 2023

Thank you very much for your review, and no problem for the delay; there seem to be a lot of PRs these past months, which is great for the community ! :)

Great catch on the arrow head issue with length. This was due to a simple missing 'length' factor. Nevertheless, even with the fix, I think arrow-styled vectors look quite ugly when the length parameters is set below the typical width of the vectors. I could make the width vary along with the length, but I don't think this a good idea.

I noticed that a test was not passing. I'm quite unfamiliar with testing suites, but it seems to be related to one testing scripts for the 'test_generate_vector_meshes' and 'test_generate_vector_meshes2D' functions. I modified it, but I am not so confident about my changes...

@jni jni added the highlight PR that should be mentioned in next release notes label Jun 27, 2023
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.

assuming d65a030 fixes the test suite, this is ready imho! 🚀

@jni
Copy link
Member

jni commented Jun 28, 2023

WHOO ALL GREEN! 🚀

@jni jni added the ready to merge Last chance for comments! Will be merged in ~24h label Jun 28, 2023
@jni jni changed the title Added different display styles to the vectors of the Vectors layer Add arrow display styles to Vectors layer Jun 29, 2023
@jni jni merged commit d86cf5a into napari:main Jun 29, 2023
36 checks passed
@jni
Copy link
Member

jni commented Jun 29, 2023

Thank you so much for your work here @jules-vanaret! This has been weighing on my mind for so long you don't even know! 🙏 🚀 🌔

@jni jni removed the ready to merge Last chance for comments! Will be merged in ~24h label Jun 29, 2023
@jules-vanaret
Copy link
Contributor Author

Oops, I was on vacation so I missed the conclusion of the PR ! 😅
Many thanks to @jni and @brisvag for your patience and guidance. That was my very first PR to the Napari project, and I learnt a great deal. Hopefully more PRs will follow in the near future. 😉

For the sake of completeness regarding the Vectors layer, I have a few suggestions for follow-up PRs:

  1. The PR was merged before I could finish tidying up all the comments and docstrings. This is minor, but I will try to find the time to finish it in a follow-up mini-PR.

Yeah, I always doung length was a bad name, but it's pretty central and user-facing, so if we change it we should introduce deprecations (shouln't be too hard to do, happy to help here if you need it). I like scaling_factor, or ever more explicitly length_multiplier or length_scaling. @jni?

  1. In the message above, @brisvag suggested renaming length for something more suggestive, e.g length_multiplier. If anyone versed in deprecations could help me on this one, this could be a quick fix.

Last comment about the length parameter: right now, if the "length changed" event is triggered (same for the width parameter by the way), generate_vector_meshes is launched again so that every meshes and triangles are computed again, which is highly ineffective as only the positions of a few vertices would actually need to be multiplied by a scalar.

Maybe a next PR could tackle this optimization issue.

  1. In the message above, I point out an optimization issue regarding the length and width parameters: when they are changed, vectors are recomputed from scratch but only a few vertices/meshes would actually need to be modified.

  2. Right now vectors are always specified and drawn starting from their base (the "tail" of the vector). A combobox could be added to choose the "tethering point" of the vectors. To me, offering this choice to the user is important because (i) the current implementation is useful when you want to draw simple vectors, (ii) tethering at the center of the vector could be useful for displaying nematics fields (in a nutshell, vectors without direction e.g liquid crystals), (iii) tethering at the tip of the vector could be useful to easily make arrows that point directly to a given position.

If any of these ideas are of particular interest to the community, please let me know!

@brisvag
Copy link
Contributor

brisvag commented Jul 1, 2023

  1. The PR was merged before I could finish tidying up all the comments and docstrings

Always good to keep those updated, if we have missed something in the review 😅

The rest are all good options for followup PRs. I wouldn't call any of them high priority (for myself or from what I've seen discussed in the past), so feel free to tackle whichever is most blocking for your own work, and I'll be happy to review!

@jni
Copy link
Member

jni commented Jul 3, 2023

In the message above, @brisvag suggested renaming length for something more suggestive, e.g length_multiplier. If anyone versed in deprecations could help me on this one, this could be a quick fix.

@jules-vanaret currently, this change is unreleased, so a quick followup PR would not need to go through the deprecation pathway!

Currently I don't have this slated to go into 0.4.18 (that is a special release off a branch that requires manual cherry-picking of changes to that branch), so we would have time, but I'm quite tempted to add it — if you think you could change that in the next ~24h I could add both and there would be no need for a deprecation!

Having said that 0.5 will contain lots of deprecations (that's why we are making 0.4.18), so one more doesn't bother me. 😅

tidying up the comments and docstrings

Definitely not urgent, but a PR would definitely be appreciated! 😊

optimization issue regarding the length and width parameters

As above — performance fixes (especially for relatively "rare" events) can be made in subsequent PRs — and will be much appreciated. 😊 But they are not essential.

Right now vectors are always specified and drawn starting from their base (the "tail" of the vector). A combobox could be added to choose the "tethering point" of the vectors.

I like this!

For all of the issues in that list @jules-vanaret, if you are not fixing them more or less immediately, it would be good to create separate issues so we keep track of them! 🙏

@brisvag
Copy link
Contributor

brisvag commented Jul 3, 2023

@jules-vanaret currently, this change is unreleased, so a quick followup PR would not need to go through the deprecation pathway!

No, we're talking about renaming the Vectors.length parameter, which has been there for years, so that definitely needs deprecation!

@jules-vanaret
Copy link
Contributor Author

Thank you both for the feedback. I have tons of things to do this week so I probably won't be able to work on anything immediately. I'll try to create the new issues rapidly though.

@jules-vanaret jules-vanaret deleted the vectors_display_styles branch July 3, 2023 11:05
melissawm pushed a commit to melissawm/napari that referenced this pull request Jul 3, 2023
# Fixes/Closes

Closes napari#4149

# Description

This is a follow-up of the discussion napari#4149 in which adding different
display style to the vectors of the Vectors layer was requested. As
suggested by users @brisvag and @jni, I implemented a ComboBox in the
GUI with 3 choices:

1. `rectangle` which is the old style of vectors, and currently the
default value
2. `triangle` which displays vectors as triangles
3. `arrow` which displays a "classical" vector shape, with a rectangular
shaft and a triangular head

![napari_vectors_display_styles](https://user-images.githubusercontent.com/94963445/232590807-9f812cfc-293a-4bd5-87ac-8b7acfdc7b87.gif)

Most of this contribution comes from additional functions to compute
vectors meshes in _vispy/layers/vectors.py. Because changes are mainly
in the function `generate_vector_meshes_2D`, there was no need to change
anything in the functions `_on_data_change` or `generate_vector_meshes`,
i.e it works natively with the 3D visualization.

The Vectors layer now provides a parameter `edge_display_style`.
Right now the possible values are: `[rectangle, triangle, arrow]`

---------

Co-authored-by: Juan Nunez-Iglesias <jni@fastmail.com>
Co-authored-by: Lorenzo Gaifas <brisvag@gmail.com>
@jni
Copy link
Member

jni commented Jul 3, 2023

No, we're talking about renaming the Vectors.length parameter, which has been there for years, so that definitely needs deprecation!

Ah, sorry, I get it now! But on the plus side this wasn't changed here, right?

@jules-vanaret
Copy link
Contributor Author

jules-vanaret commented Jul 3, 2023

But on the plus side this wasn't changed here, right?

Absolutely, I haven't touched it at all! We simply pointed out at some point in the discussion that it might be a good idea to slowly change its name. :)

@jni jni modified the milestones: 0.5.0, 0.4.18 Jul 4, 2023
jni added a commit that referenced this pull request Jul 4, 2023
# Fixes/Closes

Closes #4149

# Description

This is a follow-up of the discussion #4149 in which adding different
display style to the vectors of the Vectors layer was requested. As
suggested by users @brisvag and @jni, I implemented a ComboBox in the
GUI with 3 choices:

1. `rectangle` which is the old style of vectors, and currently the
default value
2. `triangle` which displays vectors as triangles
3. `arrow` which displays a "classical" vector shape, with a rectangular
shaft and a triangular head

![napari_vectors_display_styles](https://user-images.githubusercontent.com/94963445/232590807-9f812cfc-293a-4bd5-87ac-8b7acfdc7b87.gif)

Most of this contribution comes from additional functions to compute
vectors meshes in _vispy/layers/vectors.py. Because changes are mainly
in the function `generate_vector_meshes_2D`, there was no need to change
anything in the functions `_on_data_change` or `generate_vector_meshes`,
i.e it works natively with the 3D visualization.

The Vectors layer now provides a parameter `edge_display_style`.
Right now the possible values are: `[rectangle, triangle, arrow]`

---------

Co-authored-by: Juan Nunez-Iglesias <jni@fastmail.com>
Co-authored-by: Lorenzo Gaifas <brisvag@gmail.com>
@imagesc-bot
Copy link

This pull request has been mentioned on Image.sc Forum. There might be relevant details there:

https://forum.image.sc/t/announcement-napari-0-4-18-released/83322/1

andy-sweet pushed a commit that referenced this pull request Jul 20, 2023
# Description
This is a follow-up micro-PR to the discussion in #5740 about adding
last comments to the Vectors layer code.

## Type of change
- [x] Enhancement (non-breaking improvements of an existing feature)
- [x] Documentation (update of docstrings and deprecation information)

## Final checklist:
- [x] My PR is the minimum possible work for the desired functionality
- [x] I have commented my code, particularly in hard-to-understand areas
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request highlight PR that should be mentioned in next release notes qt Relates to qt tests Something related to our tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add arrow to vectors in napari vector layer
5 participants