Skip to content

Commit

Permalink
Merge branch 'main' into setup-python-v4-with-pip-cache
Browse files Browse the repository at this point in the history
  • Loading branch information
LucaMarconato committed Mar 25, 2024
2 parents 9d83b67 + d40224b commit 253b996
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 31 additions & 0 deletions src/spatialdata/_core/spatialdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
21 changes: 21 additions & 0 deletions tests/io/test_multi_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 253b996

Please sign in to comment.