Skip to content
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

Support MultiPolygons in render_shapes() #93

Merged
merged 17 commits into from
Aug 23, 2023

Conversation

timtreis
Copy link
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

codecov-commenter commented Jun 14, 2023

Codecov Report

Merging #93 (08d2137) into main (a77e2f6) will increase coverage by 1.65%.
The diff coverage is 86.48%.

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 Changed Coverage Δ
src/spatialdata_plot/pl/utils.py 63.29% <74.35%> (+2.50%) ⬆️
src/spatialdata_plot/pl/render.py 94.07% <100.00%> (+2.30%) ⬆️

... and 1 file with indirect coverage changes

@giovp
Copy link
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
Member Author

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

@LucaMarconato
Copy link
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
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
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
4 checks passed
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