Skip to content

Fix but render_shapes() when coordinate system "global" not present #446

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 1 commit into from
Apr 16, 2025

Conversation

melonora
Copy link
Contributor

@melonora melonora commented Apr 16, 2025

closes: #447

In render shapes, when using datashader, all transforms were obtained instead of the transform to the coordinate system which is supposed to be rendered. This was a problem for the visium notebook when trying the following code snippet:

import spatialdata as sd
import spatialdata_plot  
import matplotlib.pyplot as plt

sdata = sd.read_zarr("C:\\Users\\w-mv\\PycharmProjects\\spatialdata-notebooks\\notebooks\\examples\\visium_hd.zarr")

plt.figure(figsize=(10, 10))
ax = plt.gca()

gene_name = "AA986860"
sdata.pl.render_shapes("Visium_HD_Mouse_Small_Intestine_square_016um", color=gene_name, method="datashader").pl.show(
    coordinate_systems="Visium_HD_Mouse_Small_Intestine", ax=ax
)

After fixing that, the transform was applied and again parsed by ShapesModel. This ensured that after that one can only have a shape with coordinate system global. This means that the following should always use coordinate system global:

plot_width, plot_height, x_ext, y_ext, factor = _get_extent_and_range_for_datashader_canvas(
            transformed_element, "global", ax, fig_params
        )

@codecov-commenter
Copy link

codecov-commenter commented Apr 16, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 85.21%. Comparing base (022a65a) to head (9793987).

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #446   +/-   ##
=======================================
  Coverage   85.21%   85.21%           
=======================================
  Files           8        8           
  Lines        1759     1759           
=======================================
  Hits         1499     1499           
  Misses        260      260           
Files with missing lines Coverage Δ
src/spatialdata_plot/pl/render.py 96.89% <100.00%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@melonora melonora merged commit 9a3d169 into scverse:main Apr 16, 2025
4 checks passed
@melonora melonora deleted the fix_visiumhd branch April 16, 2025 21:19
@@ -195,7 +195,7 @@ def _render_shapes(
)

plot_width, plot_height, x_ext, y_ext, factor = _get_extent_and_range_for_datashader_canvas(
transformed_element, coordinate_system, ax, fig_params
transformed_element, "global", ax, fig_params
Copy link
Member

@LucaMarconato LucaMarconato Apr 21, 2025

Choose a reason for hiding this comment

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

@melonora why "global" here? What if "global" is not used in the dataset that is being plotted?

Copy link
Member

Choose a reason for hiding this comment

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

@melonora why was this merged? I think Lucas concern is valid 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.

Already talked with Luca (sorry tagging does not work on github app) and it is due to the recent change in which global is not required. This caused errors for datasets not having global. However, here the element first gets transformed and after transform the default cs in case nothing is passed on is global. Hence, second call always uses global.

Copy link
Member

@timtreis timtreis Apr 22, 2025

Choose a reason for hiding this comment

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

But shouldn't we preserve whatever name the user gave the cs? I think we reuse that as the default title for a CS when plotting

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 will have another look when I am at my laptop again. Note that the 'coordinate_system' parameter itself is not being altered here. The point is that after the transform there is no other cs but global.

Copy link
Member

Choose a reason for hiding this comment

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

At the very end of basic.py we're assigning these as titles 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.

just as a last follow up, this is not an issue as the title is still correspondent to the coordinate system used. I would have to follow up but it could be that the coordinate system argument in the second call where now the value by default is "global" can be deprecated.

For now just as final proof of the visium hd example in the notebooks. The following code snippet before was failing:

import spatialdata as sd
import spatialdata_plot
import matplotlib.pyplot as plt

sdata = sd.read_zarr("visium_hd.zarr")

plt.figure(figsize=(10, 10))
ax = plt.gca()

gene_name = "AA986860"
sdata.pl.render_shapes("Visium_HD_Mouse_Small_Intestine_square_016um", color=gene_name, method="datashader").pl.show(
    coordinate_systems="Visium_HD_Mouse_Small_Intestine", ax=ax
)

However, this PR fixes the issue of having the wrong coordinate system as argument in the second call and gives you this plot:
image

@LucaMarconato LucaMarconato changed the title Get one shape transform Fix but render_shapes() when coordinate system "global" not present Apr 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

render_shapes() fails with datashader when the coordinate system is not global
4 participants