Skip to content

Commit 3241cdb

Browse files
Sonja-StockhausSonja Stockhaustimtreis
authored
handle empty points in the image (#133)
Co-authored-by: Sonja Stockhaus <[email protected]> Co-authored-by: Tim Treis <[email protected]>
1 parent 19bc083 commit 3241cdb

File tree

6 files changed

+16
-2
lines changed

6 files changed

+16
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning][].
1313
### Fixed
1414

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

1718
## [0.0.2] - 2023-06-25
1819

src/spatialdata_plot/pl/basic.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,10 @@ def render_shapes(
195195
kwargs
196196
Additional arguments to be passed to cmap and norm.
197197
198+
Notes
199+
-------
200+
- Empty geometries will be removed at the time of plotting.
201+
198202
Returns
199203
-------
200204
None

src/spatialdata_plot/pl/render.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ def _get_collection_shape(
121121
) -> PatchCollection:
122122
patches = []
123123
for shape in shapes:
124+
# remove empty points/polygons
125+
shape = shape[shape["geometry"].apply(lambda geom: not geom.is_empty)]
124126
# We assume that all elements in one collection are of the same type
125127
if shape["geometry"].iloc[0].geom_type == "Polygon":
126128
patches += [Polygon(p.exterior.coords, closed=True) for p in shape["geometry"]]

src/spatialdata_plot/pl/utils.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,11 +314,13 @@ def get_point_bb(
314314

315315
# Split by Point and Polygon:
316316
tmp_points = sdata.shapes[e_id][
317-
sdata.shapes[e_id]["geometry"].apply(lambda geom: geom.geom_type == "Point")
317+
sdata.shapes[e_id]["geometry"].apply(
318+
lambda geom: (geom.geom_type == "Point" and not geom.is_empty)
319+
)
318320
]
319321
tmp_polygons = sdata.shapes[e_id][
320322
sdata.shapes[e_id]["geometry"].apply(
321-
lambda geom: geom.geom_type in ["Polygon", "MultiPolygon"]
323+
lambda geom: (geom.geom_type in ["Polygon", "MultiPolygon"] and not geom.is_empty)
322324
)
323325
]
324326

Loading

tests/pl/test_render_shapes.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import geopandas as gpd
12
import matplotlib
23
import scanpy as sc
34
import spatialdata_plot # noqa: F401
@@ -46,3 +47,7 @@ def test_plot_can_render_polygons_with_rgba_colored_outline(self, sdata_blobs: S
4647
sdata_blobs.pl.render_shapes(
4748
elements="blobs_polygons", outline=True, outline_color=(0.0, 1.0, 0.0, 1.0)
4849
).pl.show()
50+
51+
def test_plot_can_render_empty_geometry(self, sdata_blobs: SpatialData):
52+
sdata_blobs.shapes["blobs_circles"].at[0, "geometry"] = gpd.points_from_xy([None], [None])[0]
53+
sdata_blobs.pl.render_shapes().pl.show()

0 commit comments

Comments
 (0)