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

Vectorize labels into polygons #560

Merged
merged 10 commits into from
May 27, 2024
Merged

Conversation

quentinblampey
Copy link
Contributor

@quentinblampey quentinblampey commented May 15, 2024

Converting a mask (2D Labels, multiscale or not) into shapes, i.e. a GeoDataFrame.

Speed test

1 min on 300k cells locally

Notes

  • vectorization is applied on chunks
  • for each chunks, scikit_image is used to find contours (for each label, a cropped view is extracted, then vectorized, and then translated according to the bounding box coords)
  • polygons are also translated according to the chunk coords
  • labels on multiple chunks are merged via the dissolve method

Future steps

Copy link

codecov bot commented May 25, 2024

Codecov Report

Attention: Patch coverage is 90.65421% with 10 lines in your changes missing coverage. Please review.

Project coverage is 92.52%. Comparing base (3733514) to head (52404d7).
Report is 82 commits behind head on main.

Files Patch % Lines
src/spatialdata/_core/operations/vectorize.py 92.39% 7 Missing ⚠️
src/spatialdata/models/models.py 62.50% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #560      +/-   ##
==========================================
- Coverage   92.55%   92.52%   -0.03%     
==========================================
  Files          42       42              
  Lines        6003     6086      +83     
==========================================
+ Hits         5556     5631      +75     
- Misses        447      455       +8     
Files Coverage Δ
src/spatialdata/__init__.py 100.00% <100.00%> (ø)
src/spatialdata/_core/operations/aggregate.py 94.05% <100.00%> (ø)
src/spatialdata/_core/query/_utils.py 95.65% <ø> (+1.53%) ⬆️
src/spatialdata/_core/query/spatial_query.py 95.19% <100.00%> (+0.01%) ⬆️
src/spatialdata/models/models.py 87.52% <62.50%> (-0.47%) ⬇️
src/spatialdata/_core/operations/vectorize.py 94.33% <92.39%> (-1.61%) ⬇️

mask = np.pad(region_props.image, 1)
contours = skimage.measure.find_contours(mask, 0.5)

polygons = [Polygon(contour[:, [1, 0]]) for contour in contours if contour.shape[0] >= 4]
Copy link
Member

Choose a reason for hiding this comment

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

I would add a comment explaining the choice of 4.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added a comment in the code.
When the number of vertices is 3, the shape is basically a line (2 points + repetition of the first point), which would raise a Shapely error. This is why I added this filtering.
Though, we should never have a shape with less than three coordinates: even a single pixel would be converted into a square. I just added this filtering for safety, but I'm not sure if skimage can produce a shape with less than 3 coordinates.

@LucaMarconato
Copy link
Member

LucaMarconato commented May 25, 2024

Excellent PR! I just added a minor comment, but otherwise looks great! I will check/implement the following minor cases (copying them from the hackmd):

small things

  • to_circles() seems not to be implemented for points
  • to_circles() seems not to be implemented for circles
  • circles_to_polygons() should be more explicit: if values_geotypes == ['Point'], elif is.subset(['Polygon', 'MultiPolygon']) ... , else fail

other small things

  • remove circles_to_polygons() and use to_polygons() instead
  • to_polygons() should work for polygons, multipolygons, circles and points (or maybe fail on points)

I will merge after I complete them.

@LucaMarconato LucaMarconato merged commit 3a158a4 into scverse:main May 27, 2024
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants