Skip to content

Fix clims when plotting shapes element annotations with matplotlib rendering #368

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 11 commits into from
Oct 10, 2024

Conversation

melonora
Copy link
Contributor

@melonora melonora commented Oct 3, 2024

closes #324

This PR fixes the issue described in #324. When creating the patch collection a default Normalize was always created instead of using the one provided by the user. Furthermore in this PR some legacy code was removed which did not do anything.

@melonora melonora changed the title Fix clims when plotting shapes element annotations Fix clims when plotting shapes element annotations with matplotlib rendering Oct 3, 2024
@codecov-commenter
Copy link

codecov-commenter commented Oct 4, 2024

Codecov Report

Attention: Patch coverage is 54.54545% with 5 lines in your changes missing coverage. Please review.

Project coverage is 83.73%. Comparing base (6ffe22b) to head (16a5a38).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
src/spatialdata_plot/pl/basic.py 50.00% 4 Missing ⚠️
src/spatialdata_plot/pl/utils.py 66.66% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #368      +/-   ##
==========================================
- Coverage   83.76%   83.73%   -0.04%     
==========================================
  Files           8        8              
  Lines        1540     1543       +3     
==========================================
+ Hits         1290     1292       +2     
- Misses        250      251       +1     
Files with missing lines Coverage Δ
src/spatialdata_plot/pl/utils.py 76.37% <66.66%> (+0.20%) ⬆️
src/spatialdata_plot/pl/basic.py 89.26% <50.00%> (-1.60%) ⬇️

@melonora melonora marked this pull request as ready for review October 4, 2024 08:36
@melonora
Copy link
Contributor Author

melonora commented Oct 4, 2024

There were some tests that had an expected figure that was wrong in the first place so I fixed that. Think it is good to go for now. Here and there in the test code base I also saw no copies of anndata being created, spamming warnings so I silenced those.

elif vcenter is None:
norm = Normalize(vmin=vmin, vmax=vmax, clip=True)
else:
norm = TwoSlopeNorm(vmin=vmin, vmax=vmax, vcenter=vcenter)
Copy link
Member

Choose a reason for hiding this comment

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

Now that vcenter is removed, it should be removed also from the function signature. Also, kwargs is in the signature but not used, so I would remove it.

@@ -339,7 +338,7 @@ def _get_collection_shape(
c = cmap(c)
else:
try:
norm = colors.Normalize(vmin=min(c), vmax=max(c))
norm = colors.Normalize(vmin=min(c), vmax=max(c)) if norm is None else norm
Copy link
Member

Choose a reason for hiding this comment

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

In this function Normalize() is initialized without clip, while in _prepare_cmap_norm() the default is to set clip=True. I would choose one of the two as our default choice. The user will be able to specify clip, vcenter, etc by passing a norm object directly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

hmm let me double check that if we don't pass norm as user, whether ultimately the norm is always created anyway, then we can get rid of normalize instance initiated here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok there is code left over of when vmin and vmax were removed. Not certain whether to address this in a different PR.

Copy link
Member

Choose a reason for hiding this comment

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

I'd address the choice of the value of clip in this PR please, because it's easy to forget about this in a new PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

default is set to False now

@@ -490,11 +490,8 @@ def _prepare_cmap_norm(
cmap: Colormap | str | None = None,
norm: Normalize | None = None,
na_color: ColorLike | None = None,
vmin: float | None = None,
Copy link
Member

Choose a reason for hiding this comment

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

@timtreis I don't remember the outcome of the discussion with the user that reported this. Is this the way to go (=letting users only use norm and not vmin, vmax) or shall we remove vcenter only and keep vmin, vmax and use them to initialize the default Normalize object?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In the discussion it was stated that vmin and vmax are removed. This function is only internally called

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So we agree on clip 'True' by default if user does not provide normaloze object?

Copy link
Member

Choose a reason for hiding this comment

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

If vmin and vmax are not exposed to the user (and hence they are None), then clip will have no effect because when exposed vmin, vmax are None, the data limits are used. So I would keep the default clip to be False (which is matplotlib's default).

Copy link
Member

Choose a reason for hiding this comment

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

One thing, if vmin, vmax are removed from pl.render_shapes(), we should throw an informative exception or deprecation warning, explaining to the user that norm should be used instead. Could you add that please?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

They have not been removed in this PR though and the public functions thus already did not contain these parameters

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I did add a deprecation warning in case of the arguments being passed as kwargs

@LucaMarconato
Copy link
Member

LucaMarconato commented Oct 9, 2024

Thanks @melonora, pre-approving. I kindly ask you just two minor adjustments:

  • please add the deprecation warning for vmin, vmax also in the other 3 render functions.
  • please modify the warning saying to pass the Normalize object to norm, otherwise it may not be clear how to use it.
    After this feel free to merge thanks.

@melonora
Copy link
Contributor Author

Thanks @melonora, pre-approving. I kindly ask you just two minor adjustments:

  • please add the deprecation warning for vmin, vmax also in the other 3 render functions.
  • please modify the warning saying to pass the Normalize object to norm, otherwise it may not be clear how to use it.
    After this feel free to merge thanks.

added todo as well to add this to tutorial notebook.

@melonora melonora merged commit 6cce44e into scverse:main Oct 10, 2024
5 checks passed
@melonora melonora deleted the issue_#328 branch October 10, 2024 11:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unable to set vmin vmax when plotting vector data
3 participants