diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index efe6d9a3..62e86ed4 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -27,7 +27,7 @@ repos: additional_dependencies: [numpy, types-requests] exclude: tests/|docs/ - repo: https://github.com/astral-sh/ruff-pre-commit - rev: v0.3.3 + rev: v0.3.4 hooks: - id: ruff args: [--fix, --exit-non-zero-on-fix] diff --git a/CHANGELOG.md b/CHANGELOG.md index 2087a6c1..dc9bb844 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,14 @@ and this project adheres to [Semantic Versioning][]. [keep a changelog]: https://keepachangelog.com/en/1.0.0/ [semantic versioning]: https://semver.org/spec/v2.0.0.html +## [0.1.1] - xxxx-xx-xx + +### Added + +#### + +- Added method `update_annotated_regions_metadata() which updates the `region`value automatically from the `region_key` columns + ## [0.1.0] - 2024-03-24 ### Added diff --git a/src/spatialdata/_core/spatialdata.py b/src/spatialdata/_core/spatialdata.py index fd267366..9dec8c20 100644 --- a/src/spatialdata/_core/spatialdata.py +++ b/src/spatialdata/_core/spatialdata.py @@ -396,6 +396,37 @@ def _change_table_annotation_target( check_target_region_column_symmetry(table, table_region_key, region) attrs[TableModel.REGION_KEY] = region + @staticmethod + def update_annotated_regions_metadata(table: AnnData, region_key: str | None = None) -> AnnData: + """ + Update the annotation target of the table using the region_key column in table.obs. + + The table must already contain annotation metadata, e.g. the region, region_key and instance_key + must already be specified for the table. If this is not the case please use TableModel.parse instead + and specify the annotation metadata by passing the correct arguments to that function. + + Parameters + ---------- + table + The AnnData table for which to set the annotation target. + region_key + The column in table.obs containing the rows specifying the SpatialElements being annotated. + If None the current value for region_key in the annotation metadata of the table is used. If + specified but different from the current region_key, the current region_key is overwritten. + + Returns + ------- + The table for which the annotation target has been set. + """ + attrs = table.uns.get(TableModel.ATTRS_KEY) + if attrs is None: + raise ValueError("The table has no annotation metadata. Please parse the table using `TableModel.parse`.") + region_key = region_key if region_key else attrs[TableModel.REGION_KEY_KEY] + if attrs[TableModel.REGION_KEY_KEY] != region_key: + attrs[TableModel.REGION_KEY_KEY] = region_key + attrs[TableModel.REGION_KEY] = table.obs[region_key].unique().tolist() + return table + def set_table_annotates_spatialelement( self, table_name: str, diff --git a/tests/io/test_multi_table.py b/tests/io/test_multi_table.py index 50d02677..c6b00a8f 100644 --- a/tests/io/test_multi_table.py +++ b/tests/io/test_multi_table.py @@ -246,3 +246,24 @@ def test_concatenate_sdata_multitables(): assert merged_sdata.tables["table2"].n_obs == 300 assert all(merged_sdata.tables["table"].obs.region.unique() == ["poly_1", "poly_2", "poly_3"]) assert all(merged_sdata.tables["table2"].obs.region.unique() == ["multipoly_1", "multipoly_2", "multipoly_3"]) + + +def test_static_set_annotation_target(): + test_sdata = SpatialData( + shapes={ + "test_shapes": test_shapes["poly"], + } + ) + table = _get_table(region="test_non_shapes") + table_target = table.copy() + table_target.obs["region"] = "test_shapes" + table_target = SpatialData.update_annotated_regions_metadata(table_target) + assert table_target.uns[TableModel.ATTRS_KEY][TableModel.REGION_KEY] == ["test_shapes"] + + test_sdata["another_table"] = table_target + + table.obs["diff_region"] = "test_shapes" + table = SpatialData.update_annotated_regions_metadata(table, region_key="diff_region") + assert table.uns[TableModel.ATTRS_KEY][TableModel.REGION_KEY] == ["test_shapes"] + + test_sdata["yet_another_table"] = table