Skip to content

Commit

Permalink
add support for layer selection of get_values method (#818)
Browse files Browse the repository at this point in the history
* add support for layer selection of get_values method

* adding file formatted with wrong prettier

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* ignoring markdown files in prettier

* add test for get_values(table_layer=...)

* using different prettier mirror

---------

Co-authored-by: Sonja Stockhaus <[email protected]>
Co-authored-by: Luca Marconato <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
4 people authored Jan 5, 2025
1 parent 670ce20 commit 30f8103
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/spatialdata/_core/query/relational_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,7 @@ def get_values(
sdata: SpatialData | None = None,
element_name: str | None = None,
table_name: str | None = None,
table_layer: str | None = None,
return_obsm_as_is: bool = False,
) -> pd.DataFrame | ArrayLike:
"""
Expand All @@ -860,6 +861,8 @@ def get_values(
annotating the element_name.
table_name
Name of the table to get the values from.
table_layer
Layer of the table to get the values from. If None, the values are taken from X.
return_obsm_as_is
In case the value is in obsm the value of the key can be returned as is if return_obsm_as_is is True, otherwise
creates a dataframe and returns it.
Expand Down Expand Up @@ -930,7 +933,12 @@ def get_values(
df = obs[value_key_values].copy()
if origin == "var":
matched_table.obs = pd.DataFrame(obs)
x = matched_table[:, value_key_values].X
if table_layer is None:
x = matched_table[:, value_key_values].X
else:
if table_layer not in matched_table.layers:
raise ValueError(f"Layer {table_layer} was not found.")
x = matched_table[:, value_key_values].layers[table_layer]
import scipy

if isinstance(x, scipy.sparse.csr_matrix | scipy.sparse.csc_matrix | scipy.sparse.coo_matrix):
Expand Down
7 changes: 7 additions & 0 deletions tests/core/query/test_relational_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,13 @@ def test_get_values_table(sdata_blobs):
assert len(df) == 26


def test_get_values_table_different_layer(sdata_blobs):
sdata_blobs["table"].layers["layer"] = np.log1p(sdata_blobs["table"].X)
df = get_values(value_key="channel_0_sum", element=sdata_blobs["table"])
df_layer = get_values(value_key="channel_0_sum", element=sdata_blobs["table"], table_layer="layer")
assert np.allclose(np.log1p(df), df_layer)


def test_get_values_table_element_name(sdata_blobs):
sdata_blobs["table"].obs["region"] = sdata_blobs["table"].obs["region"].cat.add_categories("another_region")
sdata_blobs["table"].obs.loc["1", "region"] = "another_region"
Expand Down

0 comments on commit 30f8103

Please sign in to comment.