Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/spatialdata/_core/query/relational_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,9 @@ def _filter_table_by_elements(table: AnnData | None, elements_dict: dict[str, di

Returns
-------
The filtered table, or None if the input table is None or no rows match.
The filtered table, or None if the input table is None, has no annotation metadata or no rows match.
"""
if table is None:
if table is None or not table.uns.get(TableModel.ATTRS_KEY):
return None
elements_by_name = {
name: element
Expand Down
25 changes: 25 additions & 0 deletions tests/core/query/test_spatial_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,31 @@ def test_query_spatial_data(full_sdata):
assert_spatial_data_objects_are_identical(result0, result4, check_transformations=False)


@pytest.mark.parametrize("with_polygon_query", [True, False])
def test_query_filter_table_with_orphan_table(with_polygon_query: bool):
circles = ShapesModel.parse(np.array([[10, 10], [20, 20]]), geometry=0, radius=1)
table = AnnData(shape=(2, 0))
table.obs["region"] = pd.Categorical(["circles", "circles"])
table.obs["instance"] = [0, 1]
table = TableModel.parse(table, region="circles", region_key="region", instance_key="instance")
orphan = AnnData(shape=(2, 0))
sdata = SpatialData(shapes={"circles": circles}, tables={"table": table, "orphan": orphan})

if with_polygon_query:
polygon = Polygon([(15, 15), (15, 25), (25, 25), (25, 15)])
queried = polygon_query(sdata, polygon=polygon, target_coordinate_system="global", filter_table=True)
else:
queried = sdata.query.bounding_box(
axes=("y", "x"),
min_coordinate=np.array([15, 15]),
max_coordinate=np.array([25, 25]),
filter_table=True,
target_coordinate_system="global",
)
assert set(queried.tables) == {"table"}
assert len(queried["table"]) == 1


@pytest.mark.parametrize("with_polygon_query", [True, False])
def test_query_filter_table(with_polygon_query: bool):
coords0 = np.array([[10, 10], [20, 20]])
Expand Down
Loading