scverse / scverse/spatialdata

polygon_query returns only rectangles

Open
#1,017 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

needs: triage priority: medium
Dominant language
Python
Stars
394
Forks
95
Avg merge
4d 3h
Merged PRs (30d)
7

Description

Version
spatialdata version 0.6.0

Describe the bug
Hello,
I tried to reproduce a code that was working with a previous version of sdata objects (maybe before zarrV3?). The idea is that I want to select a Region Of Interest (ROI) in a xenium experiment using polygon_query. And recently the results of the query select a whole rectangle section with no consideration to the polygon shape.

I tried to reproduce this behavior with the blob dataset, I hope it can help.

Thanks in advance :)

Expected behavior
I expected to select element that are either entirely or at least touching the ROI and have then in the sdata.tables["table'] only cells within this region. While what I obtained was all cells in some sort of big window.

Working example

    ## Imports 
    import matplotlib.pyplot as plt
    from shapely.geometry import Point, Polygon, MultiPoint, MultiPolygon
    import spatialdata as sd
    from spatialdata.datasets import blobs
    import numpy as np

    ## 0.6.0
    print("version :", sd.__version__)


    sdata = blobs()


    ## Show all cells/blobs and the shape I want to query

    fig, ax = plt.subplots(figsize=(6, 6))
    ## Plot the label image
    im = ax.imshow(sdata.labels["blobs_labels"], origin="lower", cmap="tab20")
    ## Plot the query shape
    sdata.shapes["blobs_polygons"].plot(ax=ax, edgecolor="red", facecolor="none", linewidth=2 )
    plt.title("Polygons used for query over all cells")
    plt.show()



    ## Query the sdata
    sdata_query = sd.polygon_query(sdata, MultiPolygon(sdata.shapes["blobs_polygons"].geometry.values), target_coordinate_system="global")



    ## Show the results of the query
    ## We can see here that the query was made on a big rectangle, not the selected polygons
    fig, ax = plt.subplots(figsize=(6, 6))
    ## Plot the label image
    im = ax.imshow(sdata_query.labels["blobs_labels"] , origin="lower", cmap="tab20")
    plt.title("Results of the query")
    plt.show()


    ## Check if all cells present in the plot are also present in the tables
    ## We can see that all the blobs present in the plot are not filtered from the table, even though some of them were clearly out of the polygons we used to quarry 
    print("Unique cells in the plot :", np.unique(sdata_query.labels["blobs_labels"]))
    print("\nCells present in the table :\n\n", sdata_query.tables["table"].obs)
    ```

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the polygon_query entry point and reproduce the report using the blobs dataset and the provided MultiPolygon example. Check why the result behaves like a bounding rectangle, then verify that both labels and sdata.tables["table"] contain only cells entirely within or touching the polygon ROI.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
data
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.