Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/squidpy/_constants/_pkg_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,18 @@ def _sort_haystack(
class obsp:
@classmethod
def spatial_dist(cls, value: str | None = None) -> str:
return f"{Key.obsm.spatial}_distances" if value is None else f"{value}_distances"
if value is None:
return f"{Key.obsm.spatial}_distances"
# Handle case where suffix is already present
if value.endswith("_distances"):
return value
return f"{value}_distances"

@classmethod
def spatial_conn(cls, value: str | None = None) -> str:
return f"{Key.obsm.spatial}_connectivities" if value is None else f"{value}_connectivities"
if value is None:
return f"{Key.obsm.spatial}_connectivities"
# Handle case where suffix is already present
if value.endswith("_connectivities"):
return value
return f"{value}_connectivities"
10 changes: 9 additions & 1 deletion src/squidpy/gr/_ligrec.py
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,7 @@ def ligrec(
copy: bool = False,
key_added: str | None = None,
gene_symbols: str | None = None,
table_key: str = "table",
**kwargs: Any,
) -> Mapping[str, pd.DataFrame] | None:
"""
Expand All @@ -654,13 +655,20 @@ def ligrec(
%(PT_test.parameters)s
gene_symbols
Key in :attr:`anndata.AnnData.var` to use instead of :attr:`anndata.AnnData.var_names`.
table_key
Key in :attr:`spatialdata.SpatialData.tables` where the table is stored.
Only used if ``adata`` is a :class:`spatialdata.SpatialData`.

Returns
-------
%(ligrec_test_returns)s
""" # noqa: D400
if isinstance(adata, SpatialData):
adata = adata.table
if table_key not in adata.tables:
raise ValueError(
f"Table '{table_key}' not found in SpatialData. Available tables: {list(adata.tables.keys())}"
)
adata = adata.tables[table_key]
with _genesymbols(adata, key=gene_symbols, use_raw=use_raw, make_unique=False):
return ( # type: ignore[no-any-return]
PermutationTest(adata, use_raw=use_raw)
Expand Down
30 changes: 27 additions & 3 deletions src/squidpy/gr/_nhood.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ def nhood_enrichment(
n_jobs: int | None = None,
backend: str = "loky",
show_progress_bar: bool = True,
table_key: str = "table",
) -> NhoodEnrichmentResult | None:
"""
Compute neighborhood enrichment by permutation test.
Expand All @@ -161,6 +162,9 @@ def nhood_enrichment(
%(seed)s
%(copy)s
%(parallelize)s
table_key
Key in :attr:`spatialdata.SpatialData.tables` where the table is stored.
Only used if ``adata`` is a :class:`spatialdata.SpatialData`.

Returns
-------
Expand All @@ -172,7 +176,11 @@ def nhood_enrichment(
- :attr:`anndata.AnnData.uns` ``['{cluster_key}_nhood_enrichment']['count']`` - the enrichment count.
"""
if isinstance(adata, SpatialData):
adata = adata.table
if table_key not in adata.tables:
raise ValueError(
f"Table '{table_key}' not found in SpatialData. Available tables: {list(adata.tables.keys())}"
)
adata = adata.tables[table_key]
connectivity_key = Key.obsp.spatial_conn(connectivity_key)
_assert_categorical_obs(adata, cluster_key)
_assert_connectivity_key(adata, connectivity_key)
Expand Down Expand Up @@ -239,6 +247,7 @@ def centrality_scores(
n_jobs: int | None = None,
backend: str = "loky",
show_progress_bar: bool = False,
table_key: str = "table",
) -> pd.DataFrame | None:
"""
Compute centrality scores per cluster or cell type.
Expand All @@ -260,6 +269,9 @@ def centrality_scores(
%(conn_key)s
%(copy)s
%(parallelize)s
table_key
Key in :attr:`spatialdata.SpatialData.tables` where the table is stored.
Only used if ``adata`` is a :class:`spatialdata.SpatialData`.

Returns
-------
Expand All @@ -269,7 +281,11 @@ def centrality_scores(
as mentioned above.
"""
if isinstance(adata, SpatialData):
adata = adata.table
if table_key not in adata.tables:
raise ValueError(
f"Table '{table_key}' not found in SpatialData. Available tables: {list(adata.tables.keys())}"
)
adata = adata.tables[table_key]
connectivity_key = Key.obsp.spatial_conn(connectivity_key)
_assert_categorical_obs(adata, cluster_key)
_assert_connectivity_key(adata, connectivity_key)
Expand Down Expand Up @@ -333,6 +349,7 @@ def interaction_matrix(
normalized: bool = False,
copy: bool = False,
weights: bool = False,
table_key: str = "table",
) -> NDArrayA | None:
"""
Compute interaction matrix for clusters.
Expand All @@ -347,6 +364,9 @@ def interaction_matrix(
%(copy)s
weights
Whether to use edge weights or binarize.
table_key
Key in :attr:`spatialdata.SpatialData.tables` where the table is stored.
Only used if ``adata`` is a :class:`spatialdata.SpatialData`.

Returns
-------
Expand All @@ -357,7 +377,11 @@ def interaction_matrix(
- :attr:`anndata.AnnData.uns` ``['{cluster_key}_interactions']`` - the interaction matrix.
"""
if isinstance(adata, SpatialData):
adata = adata.table
if table_key not in adata.tables:
raise ValueError(
f"Table '{table_key}' not found in SpatialData. Available tables: {list(adata.tables.keys())}"
)
adata = adata.tables[table_key]
connectivity_key = Key.obsp.spatial_conn(connectivity_key)
_assert_categorical_obs(adata, cluster_key)
_assert_connectivity_key(adata, connectivity_key)
Expand Down
20 changes: 18 additions & 2 deletions src/squidpy/gr/_ppatterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ def spatial_autocorr(
n_jobs: int | None = None,
backend: str = "loky",
show_progress_bar: bool = True,
table_key: str = "table",
) -> pd.DataFrame | None:
"""
Calculate Global Autocorrelation Statistic (Moran’s I or Geary's C).
Expand Down Expand Up @@ -107,6 +108,9 @@ def spatial_autocorr(
%(seed)s
%(copy)s
%(parallelize)s
table_key
Key in :attr:`spatialdata.SpatialData.tables` where the table is stored.
Only used if ``adata`` is a :class:`spatialdata.SpatialData`.

Returns
-------
Expand All @@ -129,7 +133,11 @@ def spatial_autocorr(
- :attr:`anndata.AnnData.uns` ``['gearyC']`` - the above mentioned dataframe, if ``mode = {sp.GEARY.s!r}``.
"""
if isinstance(adata, SpatialData):
adata = adata.table
if table_key not in adata.tables:
raise ValueError(
f"Table '{table_key}' not found in SpatialData. Available tables: {list(adata.tables.keys())}"
)
adata = adata.tables[table_key]
_assert_connectivity_key(adata, connectivity_key)

def extract_X(adata: AnnData, genes: str | Sequence[str] | None) -> tuple[NDArrayA | spmatrix, Sequence[Any]]:
Expand Down Expand Up @@ -352,6 +360,7 @@ def co_occurrence(
n_jobs: int | None = None,
backend: str = "loky",
show_progress_bar: bool = True,
table_key: str = "table",
) -> tuple[NDArrayA, NDArrayA] | None:
"""
Compute co-occurrence probability of clusters.
Expand All @@ -369,6 +378,9 @@ def co_occurrence(
Number of splits in which to divide the spatial coordinates in
:attr:`anndata.AnnData.obsm` ``['{spatial_key}']``.
%(parallelize)s
table_key
Key in :attr:`spatialdata.SpatialData.tables` where the table is stored.
Only used if ``adata`` is a :class:`spatialdata.SpatialData`.

Returns
-------
Expand All @@ -383,7 +395,11 @@ def co_occurrence(
"""

if isinstance(adata, SpatialData):
adata = adata.table
if table_key not in adata.tables:
raise ValueError(
f"Table '{table_key}' not found in SpatialData. Available tables: {list(adata.tables.keys())}"
)
adata = adata.tables[table_key]
_assert_categorical_obs(adata, key=cluster_key)
_assert_spatial_basis(adata, key=spatial_key)

Expand Down
10 changes: 9 additions & 1 deletion src/squidpy/gr/_ripley.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def ripley(
n_steps: int = 50,
seed: int | None = None,
copy: bool = False,
table_key: str = "table",
) -> dict[str, pd.DataFrame | NDArrayA]:
r"""
Calculate various Ripley's statistics for point processes.
Expand Down Expand Up @@ -93,6 +94,9 @@ def ripley(
Number of steps for the support.
%(seed)s
%(copy)s
table_key
Key in :attr:`spatialdata.SpatialData.tables` where the table is stored.
Only used if ``adata`` is a :class:`spatialdata.SpatialData`.

Returns
-------
Expand All @@ -105,7 +109,11 @@ def ripley(
or :cite:`Baddeley2015-lm`.
"""
if isinstance(adata, SpatialData):
adata = adata.table
if table_key not in adata.tables:
raise ValueError(
f"Table '{table_key}' not found in SpatialData. Available tables: {list(adata.tables.keys())}"
)
adata = adata.tables[table_key]
_assert_categorical_obs(adata, key=cluster_key)
_assert_spatial_basis(adata, key=spatial_key)
coordinates = adata.obsm[spatial_key]
Expand Down
10 changes: 9 additions & 1 deletion src/squidpy/gr/_sepal.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def sepal(
n_jobs: int | None = None,
backend: str = "loky",
show_progress_bar: bool = True,
table_key: str = "table",
) -> pd.DataFrame | None:
"""
Identify spatially variable genes with *Sepal*.
Expand Down Expand Up @@ -79,6 +80,9 @@ def sepal(
Whether to access :attr:`anndata.AnnData.raw`.
%(copy)s
%(parallelize)s
table_key
Key in :attr:`spatialdata.SpatialData.tables` where the table is stored.
Only used if ``adata`` is a :class:`spatialdata.SpatialData`.

Returns
-------
Expand All @@ -94,7 +98,11 @@ def sepal(
consider re-running the function with increased ``n_iter``.
"""
if isinstance(adata, SpatialData):
adata = adata.table
if table_key not in adata.tables:
raise ValueError(
f"Table '{table_key}' not found in SpatialData. Available tables: {list(adata.tables.keys())}"
)
adata = adata.tables[table_key]
_assert_connectivity_key(adata, connectivity_key)
_assert_spatial_basis(adata, key=spatial_key)
if max_neighs not in (4, 6):
Expand Down
10 changes: 9 additions & 1 deletion src/squidpy/tl/_sliding_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def sliding_window(
spatial_key: str = "spatial",
drop_partial_windows: bool = False,
copy: bool = False,
table_key: str = "table",
) -> pd.DataFrame | None:
"""
Divide a tissue slice into regulary shaped spatially contiguous regions (windows).
Expand All @@ -46,6 +47,9 @@ def sliding_window(
If True, drop windows that are smaller than the window size at the borders.
copy: bool
If True, return the result, otherwise save it to the adata object.
table_key
Key in :attr:`spatialdata.SpatialData.tables` where the table is stored.
Only used if ``adata`` is a :class:`spatialdata.SpatialData`.

Returns
-------
Expand All @@ -56,7 +60,11 @@ def sliding_window(
raise ValueError("Overlap must be non-negative.")

if isinstance(adata, SpatialData):
adata = adata.table
if table_key not in adata.tables:
raise ValueError(
f"Table '{table_key}' not found in SpatialData. Available tables: {list(adata.tables.keys())}"
)
adata = adata.tables[table_key]

# we don't want to modify the original adata in case of copy=True
if copy:
Expand Down