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

MeshedRegion.plot() with PropertyField #1526

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

PProfizi
Copy link
Contributor

Add the possibility to plot a PropertyField on a MeshedRegion.
Will have to add PropertyField.plot(mesh) with mandatory mesh as a PropertyField currently most likely does not have a mesh asosciated.

The PyVista options for the scalar bar still need a little bit of tweaking. Some colors are even missing from the scalar bar:
image

@PProfizi PProfizi added the enhancement New feature or request label Apr 22, 2024
@PProfizi PProfizi added this to the v0.12.1 milestone Apr 22, 2024
@PProfizi PProfizi self-assigned this Apr 22, 2024
@PProfizi PProfizi requested a review from rafacanton April 22, 2024 15:25
@PProfizi
Copy link
Contributor Author

Hi @AlejandroFernandezLuces, could you please take a look at this PR?
The goal is to plot a discrete and potentially discontinuous field of integers, each value colored.
Do you think the approach with scalar_bar and categories options is the right one?

@codecov-commenter
Copy link

codecov-commenter commented Apr 22, 2024

Codecov Report

Attention: Patch coverage is 89.18919% with 4 lines in your changes are missing coverage. Please review.

Project coverage is 84.20%. Comparing base (ae7e203) to head (aea653e).
Report is 6 commits behind head on master.

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

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1526      +/-   ##
==========================================
+ Coverage   84.16%   84.20%   +0.03%     
==========================================
  Files          82       82              
  Lines        9655     9672      +17     
==========================================
+ Hits         8126     8144      +18     
+ Misses       1529     1528       -1     

Copy link

@AlejandroFernandezLuces AlejandroFernandezLuces left a comment

Choose a reason for hiding this comment

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

Yes, I think activating categories flag and using the scalars_bar is the best way to represent what you want.

A side comment, why are you filling a kwargs map instead of using the arguments in the function calls? I personally find it clearer to use the parameters directly.

Comment on lines 230 to 232
kwargs = self._set_scalar_bar_title(kwargs)
kwargs["scalar_bar_args"]["n_labels"] = len(set(field.data))
kwargs["scalar_bar_args"]["fmt"] = "%.0f"

Choose a reason for hiding this comment

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

I would prefer to directly use the add_scalar_bar directly, I think something like this should work in this context

Suggested change
kwargs = self._set_scalar_bar_title(kwargs)
kwargs["scalar_bar_args"]["n_labels"] = len(set(field.data))
kwargs["scalar_bar_args"]["fmt"] = "%.0f"
self._plotter.add_scalar_bar(
title='Your title',
n_labels=len(set(field.data))
fmt='%.0f',
)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@AlejandroFernandezLuces I think the original idea of using kwargs is to use setdefault so that if the argument is given as a kwarg to the plot function by the user then it is not overriden by what we put here. I'll improve the code to do that correctly.

@PProfizi
Copy link
Contributor Author

Thanks @AlejandroFernandezLuces!
Also, any idea why a color is missing in the scalar bar of the plot I shared above?

@AlejandroFernandezLuces
Copy link

AlejandroFernandezLuces commented Apr 23, 2024

Thanks @AlejandroFernandezLuces! Also, any idea why a color is missing in the scalar bar of the plot I shared above?

Not sure what is happening there, you can try to set n_colors=len(set(field.data)) in add_scalar_bar or in kwargs, so you make sure that you have a color per label on PyVista side. If not, maybe you have a bug on pyDPF that causes a misrepresentation between the fields and the meshes?

@PProfizi
Copy link
Contributor Author

I managed to get this look with the latest commit
image

@PProfizi
Copy link
Contributor Author

PProfizi commented Apr 23, 2024

NB: the default cmap used is tab20 as it is discrete and ensures a clear difference between neighboring values, however it only has 20 values and will not "circle back" if more values are present. It will simply associate several consecutive values to one color.
Improvement should be made to create a dynamic color map based on semi-random colors, and to properly handle discontinuous discrete series of integers.
For example:
having mat_id = [1, 2, 29, 30] will result in 1 and 2 being both the bottom color and 29 and 30 being both the top color. It will use an identical spacing to associate numbers with colors.

@PProfizi PProfizi modified the milestones: v0.12.1, v0.12.2 May 13, 2024
@PProfizi PProfizi modified the milestones: v0.12.2, v0.12.3 Jun 4, 2024
@PProfizi PProfizi modified the milestones: v0.12.3, v0.12.4 Jun 25, 2024
PProfizi added 8 commits July 19, 2024 15:14
Signed-off-by: paul.profizi <[email protected]>
Signed-off-by: paul.profizi <[email protected]>
Signed-off-by: paul.profizi <[email protected]>
Signed-off-by: paul.profizi <[email protected]>
Signed-off-by: paul.profizi <[email protected]>
Signed-off-by: paul.profizi <[email protected]>
@PProfizi PProfizi force-pushed the feat/property_field_plot branch from 3ef8f12 to c8eb2b3 Compare July 22, 2024 08:00
os.environ["PYVISTA_OFF_SCREEN"] = "true"
core.settings.bypass_pv_opengl_osmesa_crash()
os.environ["MPLBACKEND"] = "Agg"
# core.settings.disable_off_screen_rendering()
Copy link
Contributor

Choose a reason for hiding this comment

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

This needs to be reverted before merging, right?

@@ -585,6 +597,8 @@ def plot(
show_axes=kwargs.pop("show_axes", True),
**kwargs,
)
if isinstance(field_or_fields_container, PropertyField):
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if isinstance(field_or_fields_container, PropertyField):
elif isinstance(field_or_fields_container, PropertyField):

@PProfizi PProfizi marked this pull request as draft July 22, 2024 08:34
@PProfizi PProfizi removed this from the v0.13.1 milestone Nov 4, 2024
@PProfizi PProfizi modified the milestones: v0.13.2, 0.13.3 Nov 4, 2024
@PProfizi PProfizi modified the milestones: 0.13.3, 0.13.4 Nov 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants