Skip to content

Commit b25ec12

Browse files
fixed another bug to catogorical column in obs in anndata views (#390)
1 parent b1127fe commit b25ec12

2 files changed

Lines changed: 13 additions & 0 deletions

File tree

‎src/spatialdata/_core/query/relational_query.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ def _filter_table_by_coordinate_system(table: AnnData | None, coordinate_system:
4545
return None
4646
table_mapping_metadata = table.uns[TableModel.ATTRS_KEY]
4747
region_key = table_mapping_metadata[TableModel.REGION_KEY_KEY]
48+
table.obs = pd.DataFrame(table.obs)
4849
table = table[table.obs[region_key].isin(coordinate_system)].copy()
4950
table.uns[TableModel.ATTRS_KEY][TableModel.REGION_KEY] = table.obs[region_key].unique().tolist()
5051
return table

‎tests/core/query/test_relational_query.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from anndata import AnnData
55
from spatialdata import get_values, match_table_to_element
66
from spatialdata._core.query.relational_query import _locate_value, _ValueOrigin
7+
from spatialdata.models.models import TableModel
78

89

910
def test_match_table_to_element(sdata_query_aggregation):
@@ -188,3 +189,14 @@ def test_get_values_labels_bug(sdata_blobs):
188189
from spatialdata import get_values
189190

190191
get_values("channel_0_sum", sdata=sdata_blobs, element_name="blobs_labels")
192+
193+
194+
def test_filter_table_categorical_bug(shapes):
195+
# one bug that was triggered by: https://github.com/scverse/anndata/issues/1210
196+
adata = AnnData(obs={"categorical": pd.Categorical(["a", "a", "a", "b", "c"])})
197+
adata.obs["region"] = "circles"
198+
adata.obs["cell_id"] = np.arange(len(adata))
199+
adata = TableModel.parse(adata, region=["circles"], region_key="region", instance_key="cell_id")
200+
adata_subset = adata[adata.obs["categorical"] == "a"].copy()
201+
shapes.table = adata_subset
202+
shapes.filter_by_coordinate_system("global")

0 commit comments

Comments
 (0)