Skip to content

render_shapes() fails with datashader when the coordinate system is not global #447

@LucaMarconato

Description

@LucaMarconato

As the example below shows, rendering shapes that don't have the global coordinate system works with matplotlib, but not with datashader. This because of the line

  File "/Users/macbook/embl/projects/basel/spatialdata-plot/src/spatialdata_plot/pl/render.py", line 188, in _render_shapes
    element_trans = get_transformation(sdata_filt.shapes[element])

where get_transformation() is called without passing the to_coordinate_system argument.

import spatialdata_plot
from spatialdata.datasets import blobs
from spatialdata.transformations import Scale, set_transformation
import matplotlib.pyplot as plt

sdata = blobs()

set_transformation(
    sdata["blobs_polygons"],
    transformation={"not_global": Scale([1, 5], axes=("x", "y"))},
    set_all=True,
)

_, axes = plt.subplots(1, 2)
sdata.pl.render_shapes("blobs_polygons", method="matplotlib").pl.show(
    ax=axes[0], coordinate_systems="not_global"
)

sdata.pl.render_shapes("blobs_polygons", method="datashader").pl.show(
    ax=axes[1], coordinate_systems="not_global"
)
plt.tight_layout()
plt.show()
Full traceback:
/opt/miniconda3/envs/ome311/bin/python /Applications/PyCharm.app/Contents/plugins/python-ce/helpers/pydev/pydevconsole.py --mode=client --host=127.0.0.1 --port=50561 
import sys; print('Python %s on %s' % (sys.version, sys.platform))
sys.path.extend(['/Users/macbook/embl/projects/basel/spatialdata', '/Users/macbook/embl/projects/basel/spatialdata/src'])
PyDev console: using IPython 8.32.0
Python 3.11.0 | packaged by conda-forge | (main, Jan 14 2023, 12:26:40) [Clang 14.0.6 ] on darwin
import spatialdata_plot
from spatialdata.datasets import blobs
from spatialdata.transformations import Scale, set_transformation
import matplotlib.pyplot as plt
sdata = blobs()
set_transformation(
    sdata["blobs_points"],
    transformation=Scale([1, 5], axes=("x", "y")),
)
_, axes = plt.subplots(1, 2)
sdata.pl.render_points("blobs_points", method="matplotlib").pl.show(ax=axes[0])
sdata.pl.render_points("blobs_points", method="datashader").pl.show(ax=axes[1])
plt.tight_layout()
plt.show()
/opt/miniconda3/envs/ome311/lib/python3.11/site-packages/dask/dataframe/__init__.py:31: FutureWarning: The legacy Dask DataFrame implementation is deprecated and will be removed in a future version. Set the configuration option `dataframe.query-planning` to `True` or None to enable the new Dask Dataframe implementation and silence this warning.
  warnings.warn(
/opt/miniconda3/envs/ome311/lib/python3.11/site-packages/anndata/_core/anndata.py:401: FutureWarning: The dtype argument is deprecated and will be removed in late 2024.
  warnings.warn(
/Users/macbook/embl/projects/basel/spatialdata/src/spatialdata/models/models.py:1144: UserWarning: Converting `region_key: region` to categorical dtype.
  return convert_region_column_to_categorical(adata)
/opt/miniconda3/envs/ome311/lib/python3.11/site-packages/anndata/_core/anndata.py:401: FutureWarning: The dtype argument is deprecated and will be removed in late 2024.
  warnings.warn(
/opt/miniconda3/envs/ome311/lib/python3.11/site-packages/anndata/_core/aligned_df.py:68: ImplicitModificationWarning: Transforming to str index.
  warnings.warn("Transforming to str index.", ImplicitModificationWarning)
/Users/macbook/embl/projects/basel/spatialdata/src/spatialdata/_core/_elements.py:115: UserWarning: Key `blobs_points` already exists. Overwriting it in-memory.
  self._check_key(key, self.keys(), self._shared_keys)
/Users/macbook/embl/projects/basel/spatialdata-plot/src/spatialdata_plot/pl/render.py:682: UserWarning: No data for colormapping provided via 'c'. Parameters 'cmap', 'norm' will be ignored
  _cax = ax.scatter(
/opt/miniconda3/envs/ome311/lib/python3.11/site-packages/anndata/_core/anndata.py:401: FutureWarning: The dtype argument is deprecated and will be removed in late 2024.
  warnings.warn(
/opt/miniconda3/envs/ome311/lib/python3.11/site-packages/anndata/_core/aligned_df.py:68: ImplicitModificationWarning: Transforming to str index.
  warnings.warn("Transforming to str index.", ImplicitModificationWarning)
/Users/macbook/embl/projects/basel/spatialdata/src/spatialdata/_core/_elements.py:115: UserWarning: Key `blobs_points` already exists. Overwriting it in-memory.
  self._check_key(key, self.keys(), self._shared_keys)
INFO     Using 'datashader' backend with 'None' as reduction method to speed up 
         plotting. Depending on the reduction method, the value range of the    
         plot might change. Set method to 'matplotlib' do disable this          
         behaviour.                                                             
import spatialdata_plot
from spatialdata.datasets import blobs
from spatialdata.transformations import Scale, set_transformation
import matplotlib.pyplot as plt
sdata = blobs()
set_transformation(
    sdata["blobs_points"],
    transformation={"global": Scale([1, 5], axes=("x", "y"))},
    set_all=True,
)
_, axes = plt.subplots(1, 2)
sdata.pl.render_points("blobs_points", method="matplotlib", color='black').pl.show(
    ax=axes[0], coordinate_systems="global"
)
sdata.pl.render_points("blobs_points", method="datashader", color='black').pl.show(
    ax=axes[1], coordinate_systems="global"
)
plt.tight_layout()
plt.show()
/opt/miniconda3/envs/ome311/lib/python3.11/site-packages/anndata/_core/anndata.py:401: FutureWarning: The dtype argument is deprecated and will be removed in late 2024.
  warnings.warn(
/Users/macbook/embl/projects/basel/spatialdata/src/spatialdata/models/models.py:1144: UserWarning: Converting `region_key: region` to categorical dtype.
  return convert_region_column_to_categorical(adata)
INFO     Value for parameter 'color' appears to be a color, using it as such.   
/opt/miniconda3/envs/ome311/lib/python3.11/site-packages/anndata/_core/anndata.py:401: FutureWarning: The dtype argument is deprecated and will be removed in late 2024.
  warnings.warn(
/opt/miniconda3/envs/ome311/lib/python3.11/site-packages/anndata/_core/aligned_df.py:68: ImplicitModificationWarning: Transforming to str index.
  warnings.warn("Transforming to str index.", ImplicitModificationWarning)
/Users/macbook/embl/projects/basel/spatialdata/src/spatialdata/_core/_elements.py:115: UserWarning: Key `blobs_points` already exists. Overwriting it in-memory.
  self._check_key(key, self.keys(), self._shared_keys)
/Users/macbook/embl/projects/basel/spatialdata-plot/src/spatialdata_plot/pl/render.py:682: UserWarning: No data for colormapping provided via 'c'. Parameters 'cmap', 'norm' will be ignored
  _cax = ax.scatter(
INFO     Value for parameter 'color' appears to be a color, using it as such.   
/opt/miniconda3/envs/ome311/lib/python3.11/site-packages/anndata/_core/anndata.py:401: FutureWarning: The dtype argument is deprecated and will be removed in late 2024.
  warnings.warn(
/opt/miniconda3/envs/ome311/lib/python3.11/site-packages/anndata/_core/aligned_df.py:68: ImplicitModificationWarning: Transforming to str index.
  warnings.warn("Transforming to str index.", ImplicitModificationWarning)
/Users/macbook/embl/projects/basel/spatialdata/src/spatialdata/_core/_elements.py:115: UserWarning: Key `blobs_points` already exists. Overwriting it in-memory.
  self._check_key(key, self.keys(), self._shared_keys)
INFO     Using 'datashader' backend with 'None' as reduction method to speed up 
         plotting. Depending on the reduction method, the value range of the    
         plot might change. Set method to 'matplotlib' do disable this          
         behaviour.                                                             
import spatialdata_plot
from spatialdata.datasets import blobs
from spatialdata.transformations import Scale, set_transformation
import matplotlib.pyplot as plt
sdata = blobs()
set_transformation(
    sdata["blobs_points"],
    transformation={"global": Scale([1, 5], axes=("x", "y"))},
    set_all=True,
)
_, axes = plt.subplots(1, 2)
sdata.pl.render_points("blobs_points", method="matplotlib").pl.show(
    ax=axes[0], coordinate_systems="global"
)
sdata.pl.render_points("blobs_points", method="datashader").pl.show(
    ax=axes[1], coordinate_systems="global"
)
plt.tight_layout()
plt.show()
/opt/miniconda3/envs/ome311/lib/python3.11/site-packages/anndata/_core/anndata.py:401: FutureWarning: The dtype argument is deprecated and will be removed in late 2024.
  warnings.warn(
/Users/macbook/embl/projects/basel/spatialdata/src/spatialdata/models/models.py:1144: UserWarning: Converting `region_key: region` to categorical dtype.
  return convert_region_column_to_categorical(adata)
/opt/miniconda3/envs/ome311/lib/python3.11/site-packages/anndata/_core/anndata.py:401: FutureWarning: The dtype argument is deprecated and will be removed in late 2024.
  warnings.warn(
/opt/miniconda3/envs/ome311/lib/python3.11/site-packages/anndata/_core/aligned_df.py:68: ImplicitModificationWarning: Transforming to str index.
  warnings.warn("Transforming to str index.", ImplicitModificationWarning)
/Users/macbook/embl/projects/basel/spatialdata/src/spatialdata/_core/_elements.py:115: UserWarning: Key `blobs_points` already exists. Overwriting it in-memory.
  self._check_key(key, self.keys(), self._shared_keys)
/Users/macbook/embl/projects/basel/spatialdata-plot/src/spatialdata_plot/pl/render.py:682: UserWarning: No data for colormapping provided via 'c'. Parameters 'cmap', 'norm' will be ignored
  _cax = ax.scatter(
/opt/miniconda3/envs/ome311/lib/python3.11/site-packages/anndata/_core/anndata.py:401: FutureWarning: The dtype argument is deprecated and will be removed in late 2024.
  warnings.warn(
/opt/miniconda3/envs/ome311/lib/python3.11/site-packages/anndata/_core/aligned_df.py:68: ImplicitModificationWarning: Transforming to str index.
  warnings.warn("Transforming to str index.", ImplicitModificationWarning)
/Users/macbook/embl/projects/basel/spatialdata/src/spatialdata/_core/_elements.py:115: UserWarning: Key `blobs_points` already exists. Overwriting it in-memory.
  self._check_key(key, self.keys(), self._shared_keys)
INFO     Using 'datashader' backend with 'None' as reduction method to speed up 
         plotting. Depending on the reduction method, the value range of the    
         plot might change. Set method to 'matplotlib' do disable this          
         behaviour.                                                             
import spatialdata_plot
from spatialdata.datasets import blobs
from spatialdata.transformations import Scale, set_transformation
import matplotlib.pyplot as plt
sdata = blobs()
set_transformation(
    sdata["blobs_points"],
    transformation=Scale([1, 5], axes=("x", "y")),
)
_, axes = plt.subplots(1, 2)
sdata.pl.render_points("blobs_points", method="matplotlib").pl.show(ax=axes[0])
sdata.pl.render_points("blobs_points", method="datashader").pl.show(ax=axes[1])
plt.tight_layout()
plt.show()
/opt/miniconda3/envs/ome311/lib/python3.11/site-packages/anndata/_core/anndata.py:401: FutureWarning: The dtype argument is deprecated and will be removed in late 2024.
  warnings.warn(
/Users/macbook/embl/projects/basel/spatialdata/src/spatialdata/models/models.py:1144: UserWarning: Converting `region_key: region` to categorical dtype.
  return convert_region_column_to_categorical(adata)
/opt/miniconda3/envs/ome311/lib/python3.11/site-packages/anndata/_core/anndata.py:401: FutureWarning: The dtype argument is deprecated and will be removed in late 2024.
  warnings.warn(
/opt/miniconda3/envs/ome311/lib/python3.11/site-packages/anndata/_core/aligned_df.py:68: ImplicitModificationWarning: Transforming to str index.
  warnings.warn("Transforming to str index.", ImplicitModificationWarning)
/Users/macbook/embl/projects/basel/spatialdata/src/spatialdata/_core/_elements.py:115: UserWarning: Key `blobs_points` already exists. Overwriting it in-memory.
  self._check_key(key, self.keys(), self._shared_keys)
/Users/macbook/embl/projects/basel/spatialdata-plot/src/spatialdata_plot/pl/render.py:682: UserWarning: No data for colormapping provided via 'c'. Parameters 'cmap', 'norm' will be ignored
  _cax = ax.scatter(
/opt/miniconda3/envs/ome311/lib/python3.11/site-packages/anndata/_core/anndata.py:401: FutureWarning: The dtype argument is deprecated and will be removed in late 2024.
  warnings.warn(
/opt/miniconda3/envs/ome311/lib/python3.11/site-packages/anndata/_core/aligned_df.py:68: ImplicitModificationWarning: Transforming to str index.
  warnings.warn("Transforming to str index.", ImplicitModificationWarning)
/Users/macbook/embl/projects/basel/spatialdata/src/spatialdata/_core/_elements.py:115: UserWarning: Key `blobs_points` already exists. Overwriting it in-memory.
  self._check_key(key, self.keys(), self._shared_keys)
INFO     Using 'datashader' backend with 'None' as reduction method to speed up 
         plotting. Depending on the reduction method, the value range of the    
         plot might change. Set method to 'matplotlib' do disable this          
         behaviour.                                                             
import spatialdata_plot
from spatialdata.datasets import blobs
from spatialdata.transformations import Scale, set_transformation
import matplotlib.pyplot as plt
sdata = blobs()
set_transformation(
    sdata["blobs_points"],
    transformation=Scale([1, 5], axes=("x", "y")),
)
_, axes = plt.subplots(1, 2)
sdata.pl.render_points("blobs_points", method="matplotlib", color='black').pl.show(
    ax=axes[0])
sdata.pl.render_points("blobs_points", method="datashader", color='black').pl.show(ax=axes[1])
plt.tight_layout()
plt.show()
/opt/miniconda3/envs/ome311/lib/python3.11/site-packages/anndata/_core/anndata.py:401: FutureWarning: The dtype argument is deprecated and will be removed in late 2024.
  warnings.warn(
/Users/macbook/embl/projects/basel/spatialdata/src/spatialdata/models/models.py:1144: UserWarning: Converting `region_key: region` to categorical dtype.
  return convert_region_column_to_categorical(adata)
INFO     Value for parameter 'color' appears to be a color, using it as such.   
/opt/miniconda3/envs/ome311/lib/python3.11/site-packages/anndata/_core/anndata.py:401: FutureWarning: The dtype argument is deprecated and will be removed in late 2024.
  warnings.warn(
/opt/miniconda3/envs/ome311/lib/python3.11/site-packages/anndata/_core/aligned_df.py:68: ImplicitModificationWarning: Transforming to str index.
  warnings.warn("Transforming to str index.", ImplicitModificationWarning)
/Users/macbook/embl/projects/basel/spatialdata/src/spatialdata/_core/_elements.py:115: UserWarning: Key `blobs_points` already exists. Overwriting it in-memory.
  self._check_key(key, self.keys(), self._shared_keys)
/Users/macbook/embl/projects/basel/spatialdata-plot/src/spatialdata_plot/pl/render.py:682: UserWarning: No data for colormapping provided via 'c'. Parameters 'cmap', 'norm' will be ignored
  _cax = ax.scatter(
INFO     Value for parameter 'color' appears to be a color, using it as such.   
/opt/miniconda3/envs/ome311/lib/python3.11/site-packages/anndata/_core/anndata.py:401: FutureWarning: The dtype argument is deprecated and will be removed in late 2024.
  warnings.warn(
/opt/miniconda3/envs/ome311/lib/python3.11/site-packages/anndata/_core/aligned_df.py:68: ImplicitModificationWarning: Transforming to str index.
  warnings.warn("Transforming to str index.", ImplicitModificationWarning)
/Users/macbook/embl/projects/basel/spatialdata/src/spatialdata/_core/_elements.py:115: UserWarning: Key `blobs_points` already exists. Overwriting it in-memory.
  self._check_key(key, self.keys(), self._shared_keys)
INFO     Using 'datashader' backend with 'None' as reduction method to speed up 
         plotting. Depending on the reduction method, the value range of the    
         plot might change. Set method to 'matplotlib' do disable this          
         behaviour.                                                             
import spatialdata_plot
from spatialdata.datasets import blobs
from spatialdata.transformations import Scale, set_transformation
import matplotlib.pyplot as plt
sdata = blobs()
set_transformation(
    sdata["blobs_shapes"],
    transformation={"not_global": Scale([1, 5], axes=("x", "y"))},
    set_all=True,
)
_, axes = plt.subplots(1, 2)
sdata.pl.render_shapes("blobs_shapes", method="matplotlib").pl.show(
    ax=axes[0], coordinate_systems="not_global"
)
sdata.pl.render_shapes("blobs_shapes", method="datashader").pl.show(
    ax=axes[1], coordinate_systems="not_global"
)
plt.tight_layout()
plt.show()
/opt/miniconda3/envs/ome311/lib/python3.11/site-packages/anndata/_core/anndata.py:401: FutureWarning: The dtype argument is deprecated and will be removed in late 2024.
  warnings.warn(
/Users/macbook/embl/projects/basel/spatialdata/src/spatialdata/models/models.py:1144: UserWarning: Converting `region_key: region` to categorical dtype.
  return convert_region_column_to_categorical(adata)
Traceback (most recent call last):
  File "/opt/miniconda3/envs/ome311/lib/python3.11/site-packages/IPython/core/interactiveshell.py", line 3579, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-7-bad6cee1da49>", line 9, in <module>
    sdata["blobs_shapes"],
    ~~~~~^^^^^^^^^^^^^^^^
  File "/Users/macbook/embl/projects/basel/spatialdata/src/spatialdata/_core/spatialdata.py", line 2352, in __getitem__
    _, _, element = self._find_element(item)
                    ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/macbook/embl/projects/basel/spatialdata/src/spatialdata/_core/spatialdata.py", line 2233, in _find_element
    raise KeyError(f"Could not find element with name {element_name!r}")
KeyError: "Could not find element with name 'blobs_shapes'"
import spatialdata_plot
from spatialdata.datasets import blobs
from spatialdata.transformations import Scale, set_transformation
import matplotlib.pyplot as plt
sdata = blobs()
set_transformation(
    sdata["blobs_polygons"],
    transformation={"not_global": Scale([1, 5], axes=("x", "y"))},
    set_all=True,
)
_, axes = plt.subplots(1, 2)
sdata.pl.render_shapes("blobs_polygons", method="matplotlib").pl.show(
    ax=axes[0], coordinate_systems="not_global"
)
sdata.pl.render_shapes("blobs_polygons", method="datashader").pl.show(
    ax=axes[1], coordinate_systems="not_global"
)
plt.tight_layout()
plt.show()
/opt/miniconda3/envs/ome311/lib/python3.11/site-packages/anndata/_core/anndata.py:401: FutureWarning: The dtype argument is deprecated and will be removed in late 2024.
  warnings.warn(
/Users/macbook/embl/projects/basel/spatialdata/src/spatialdata/models/models.py:1144: UserWarning: Converting `region_key: region` to categorical dtype.
  return convert_region_column_to_categorical(adata)
/Users/macbook/embl/projects/basel/spatialdata/src/spatialdata/_core/spatialdata.py:184: UserWarning: The table is annotating 'blobs_labels', which is not present in the SpatialData object.
  self.validate_table_in_spatialdata(v)
/Users/macbook/embl/projects/basel/spatialdata/src/spatialdata/_core/spatialdata.py:184: UserWarning: The table is annotating 'blobs_labels', which is not present in the SpatialData object.
  self.validate_table_in_spatialdata(v)
Traceback (most recent call last):
  File "/opt/miniconda3/envs/ome311/lib/python3.11/site-packages/IPython/core/interactiveshell.py", line 3579, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-8-aab19a7ed965>", line 19, in <module>
    sdata.pl.render_shapes("blobs_polygons", method="datashader").pl.show(
  File "/Users/macbook/embl/projects/basel/spatialdata-plot/src/spatialdata_plot/pl/basic.py", line 936, in show
    _render_shapes(
  File "/Users/macbook/embl/projects/basel/spatialdata-plot/src/spatialdata_plot/pl/render.py", line 188, in _render_shapes
    element_trans = get_transformation(sdata_filt.shapes[element])
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/macbook/embl/projects/basel/spatialdata/src/spatialdata/transformations/operations.py", line 125, in get_transformation
    raise ValueError(f"Transformation to {to_coordinate_system} not found in element {element}.")
ValueError: Transformation to global not found in element                                             geometry
0  POLYGON ((340.197 258.214, 316.177 197.065, 29...
1  POLYGON ((284.141 420.454, 267.249 371.319, 25...
2  POLYGON ((203.195 229.528, 285.506 204.414, 19...
3  POLYGON ((240.46 196.846, 227.206 188.623, 149...
4  POLYGON ((446.703 349.433, 421.086 258.9, 369.....
INFO     Using 'datashader' backend with 'sum' as reduction method to speed up  
         plotting. Depending on the reduction method, the value range of the    
         plot might change. Set method to 'matplotlib' do disable this          
         behaviour.                                                             

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions