Skip to content

Support MultiPolygons in render_shapes()#93

Merged
LucaMarconato merged 17 commits intomainfrom
feature/202306_support_multipolygons
Aug 23, 2023
Merged

Support MultiPolygons in render_shapes()#93
LucaMarconato merged 17 commits intomainfrom
feature/202306_support_multipolygons

Conversation

@timtreis
Copy link
Copy Markdown
Member

@timtreis timtreis commented Jun 8, 2023

import spatialdata as sd
import spatialdata_plot
from spatialdata.models import ShapesModel, TableModel
from shapely.geometry import Polygon, MultiPolygon, Point
import pandas as pd
import geopandas as gpd
import anndata

import numpy as np
def _make_multi():
    hole = MultiPolygon([(
        ((0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0)),
         [((0.2,0.2), (0.2,0.8), (0.8,0.8), (0.8,0.2))]
    )])
    overlap = MultiPolygon([
        Polygon([(2.0, 0.0), (2.0, 0.8), (2.8, 0.8), (2.8, 0.0)]),
        Polygon([(2.2, 0.2), (2.2, 1.0), (3.0, 1.0), (3.0, 0.2)])
    ])
    poly = Polygon([(4.0, 0.0), (4.0, 1.0), (5.0, 1.0), (5.0, 0.0)])
    circ = Point(6.0, 0.5)
    polygon_series = gpd.GeoSeries([hole, overlap, poly, circ])
    cell_polygon_table = gpd.GeoDataFrame(geometry=polygon_series)
    sd_polygons = ShapesModel.parse(cell_polygon_table)
    sd_polygons.loc[:, "radius"] = [None, None, None, 0.3]
    
    return sd_polygons

multi = _make_multi()
sdata = sd.SpatialData(shapes={"p": _make_multi()})
adata = anndata.AnnData(pd.DataFrame({"p": ["hole", "overlap", "square", "circle"]}))
adata.obs.loc[:, "region"] = "p"
adata.obs.loc[:, "val"] = [1,2,3,4]
table = TableModel.parse(adata, region="p", region_key="region", instance_key="val")
sdata.table = table
print(sdata)
sdata.pl.render_shapes(color="val", outline=True, fill_alpha=0.3).pl.show()

image

Todo:

  • verify that other plotting elements work with this solution, f.e. coloring by columns -> most likely fails because multipolygons result in a patch list that is longer than the color list

@timtreis timtreis self-assigned this Jun 8, 2023
@timtreis timtreis added the enhancement New feature or request label Jun 8, 2023
@timtreis timtreis linked an issue Jun 8, 2023 that may be closed by this pull request
@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Jun 14, 2023

Codecov Report

❌ Patch coverage is 86.48649% with 10 lines in your changes missing coverage. Please review.
✅ Project coverage is 78.19%. Comparing base (a77e2f6) to head (08d2137).
⚠️ Report is 462 commits behind head on main.

Files with missing lines Patch % Lines
src/spatialdata_plot/pl/utils.py 74.35% 10 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #93      +/-   ##
==========================================
+ Coverage   76.53%   78.19%   +1.65%     
==========================================
  Files          10       10              
  Lines        1219     1284      +65     
==========================================
+ Hits          933     1004      +71     
+ Misses        286      280       -6     
Files with missing lines Coverage Δ
src/spatialdata_plot/pl/render.py 94.07% <100.00%> (+2.30%) ⬆️
src/spatialdata_plot/pl/utils.py 63.29% <74.35%> (+2.50%) ⬆️

... and 1 file with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@giovp
Copy link
Copy Markdown
Member

giovp commented Jun 28, 2023

this is quite nice, how's the coloring gonna work out? plot same color/annotation for all polygons in a multipolygon entry?

@timtreis
Copy link
Copy Markdown
Member Author

Yeah, that was the idea. Didn't get to implementing that yet though

@LucaMarconato
Copy link
Copy Markdown
Member

Yeah, that was the idea. Didn't get to implementing that yet though

Is it ready for review? (if not please mark the PR as draft).

@timtreis timtreis marked this pull request as draft June 29, 2023 02:26
@timtreis timtreis marked this pull request as ready for review August 16, 2023 13:14
@timtreis timtreis requested review from LucaMarconato and removed request for LucaMarconato August 16, 2023 13:18
@LucaMarconato
Copy link
Copy Markdown
Member

Reviewed at a high-level, looks good to me, great work! I let @Sonja-Stockhaus do a more in-depth review of the code.

Copy link
Copy Markdown
Collaborator

@Sonja-Stockhaus Sonja-Stockhaus left a comment

Choose a reason for hiding this comment

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

Looks nice!
Didn't notice anything odd and everything worked as it should when I ran it on my machine

@LucaMarconato LucaMarconato merged commit 187f75b into main Aug 23, 2023
@timtreis timtreis deleted the feature/202306_support_multipolygons branch April 6, 2026 11:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

issues with plotting multipolygons

5 participants