diff --git a/src/spatialdata/_core/query/relational_query.py b/src/spatialdata/_core/query/relational_query.py index 1a672eefc..3c47589fe 100644 --- a/src/spatialdata/_core/query/relational_query.py +++ b/src/spatialdata/_core/query/relational_query.py @@ -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 diff --git a/tests/core/query/test_spatial_query.py b/tests/core/query/test_spatial_query.py index 3b6ff3581..422cdd9b1 100644 --- a/tests/core/query/test_spatial_query.py +++ b/tests/core/query/test_spatial_query.py @@ -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]])