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

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

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

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 66.66667% with 1 line in your changes missing coverage. Please review.

Project coverage is 83.90%. Comparing base (6ffe22b) to head (e652ba2).

Files with missing lines Patch % Lines
src/spatialdata_plot/pl/utils.py 66.66% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #368      +/-   ##
==========================================
+ Coverage   83.76%   83.90%   +0.14%     
==========================================
  Files           8        8              
  Lines        1540     1535       -5     
==========================================
- Hits         1290     1288       -2     
+ Misses        250      247       -3     
Files with missing lines Coverage Δ
src/spatialdata_plot/pl/basic.py 90.86% <ø> (ø)
src/spatialdata_plot/pl/utils.py 76.37% <66.66%> (+0.20%) ⬆️

@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.

Why are the two files different?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

just different color perhaps due to made changes and then generating the labels with a different color. Other than that there are no changes.

@@ -144,7 +144,7 @@ def test_plot_colorbar_respects_input_limits(self, sdata_blobs: SpatialData):
sdata_blobs.pl.render_shapes("blobs_polygons", color="cluster", groups=["c1"]).pl.show()

def test_plot_colorbar_can_be_normalised(self, sdata_blobs: SpatialData):
sdata_blobs["table"].obs["region"] = ["blobs_polygons"] * sdata_blobs["table"].n_obs
sdata_blobs["table"].obs["region"] = "blobs_polygons"
Copy link
Member

Choose a reason for hiding this comment

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

region is the instance_key right? Usually we have the region_key called region and the instance_key called instance_id. I'd consider making a separate PR to fix this repo-wide, unless this is only here, then I'd fix this now. CC @timtreis

Copy link
Contributor Author

Choose a reason for hiding this comment

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

no region is the region_key_key

@@ -144,7 +144,7 @@ def test_plot_colorbar_respects_input_limits(self, sdata_blobs: SpatialData):
sdata_blobs.pl.render_shapes("blobs_polygons", color="cluster", groups=["c1"]).pl.show()

def test_plot_colorbar_can_be_normalised(self, sdata_blobs: SpatialData):
Copy link
Member

Choose a reason for hiding this comment

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

What does groups=["c1"] do here? My understanding is that it doesn't have any effect because:

  • cluster is numerical and not categorical
  • the values of clusters do not include c1

But I may be missing something and be wrong.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it does nothing. @timtreis is this purely to test that the colorbar stays limited from 1-50?

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 removed the argument now as typically it is used to just color certain categorical values within a column, which from the title of the test function definition is not the intention.

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