Skip to content

handle empty points in the image #133

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 8 commits into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning][].
### Fixed

- Multi-scale images/labels are now correctly substituted and the action is logged (#131).
- Empty geometries among the shapes can be handeled (#133).

## [0.0.2] - 2023-06-25

Expand Down
4 changes: 4 additions & 0 deletions src/spatialdata_plot/pl/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,10 @@ def render_shapes(
kwargs
Additional arguments to be passed to cmap and norm.

Notes
-------
- Empty geometries will be removed at the time of plotting.

Returns
-------
None
Expand Down
2 changes: 2 additions & 0 deletions src/spatialdata_plot/pl/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ def _get_collection_shape(
) -> PatchCollection:
patches = []
for shape in shapes:
# remove empty points/polygons
shape = shape[shape["geometry"].apply(lambda geom: not geom.is_empty)]
# We assume that all elements in one collection are of the same type
if shape["geometry"].iloc[0].geom_type == "Polygon":
patches += [Polygon(p.exterior.coords, closed=True) for p in shape["geometry"]]
Expand Down
6 changes: 4 additions & 2 deletions src/spatialdata_plot/pl/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,11 +314,13 @@ def get_point_bb(

# Split by Point and Polygon:
tmp_points = sdata.shapes[e_id][
sdata.shapes[e_id]["geometry"].apply(lambda geom: geom.geom_type == "Point")
sdata.shapes[e_id]["geometry"].apply(
lambda geom: (geom.geom_type == "Point" and not geom.is_empty)
)
]
tmp_polygons = sdata.shapes[e_id][
sdata.shapes[e_id]["geometry"].apply(
lambda geom: geom.geom_type in ["Polygon", "MultiPolygon"]
lambda geom: (geom.geom_type in ["Polygon", "MultiPolygon"] and not geom.is_empty)
)
]

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions tests/pl/test_render_shapes.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import geopandas as gpd
import matplotlib
import scanpy as sc
import spatialdata_plot # noqa: F401
Expand Down Expand Up @@ -46,3 +47,7 @@ def test_plot_can_render_polygons_with_rgba_colored_outline(self, sdata_blobs: S
sdata_blobs.pl.render_shapes(
elements="blobs_polygons", outline=True, outline_color=(0.0, 1.0, 0.0, 1.0)
).pl.show()

def test_plot_can_render_empty_geometry(self, sdata_blobs: SpatialData):
sdata_blobs.shapes["blobs_circles"].at[0, "geometry"] = gpd.points_from_xy([None], [None])[0]
sdata_blobs.pl.render_shapes().pl.show()