Skip to content

Commit

Permalink
Fix squidpy.pl.extract on views (#663)
Browse files Browse the repository at this point in the history
* Fix `squidpy.pl.extract` on views

* Fix docs linter
  • Loading branch information
michalk8 committed Mar 11, 2023
1 parent d56de03 commit fa4365d
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 24 deletions.
8 changes: 7 additions & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,13 @@
"enchant.tokenize.MentionFilter",
]
# see the solution from: https://github.com/sphinx-doc/sphinx/issues/7369
user_agent = "Mozilla/5.0 (X11; Linux x86_64; rv:25.0) Gecko/20100101 Firefox/25.0"
linkcheck_ignore = [
# 403 Client Error
"https://doi.org/10.1126/science.aar7042",
"https://doi.org/10.1126/science.aau5324",
"https://doi.org/10.1093/bioinformatics/btab164",
"https://www.ncbi.nlm.nih.gov/pmc/articles/PMC2716260/",
]

# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
Expand Down
4 changes: 2 additions & 2 deletions squidpy/pl/_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def centrality_scores(
df[cluster_key] = df.index.values

clusters = adata.obs[cluster_key].cat.categories
palette = _get_palette(adata, cluster_key=cluster_key, categories=clusters)
palette = _get_palette(adata, cluster_key=cluster_key, categories=clusters, palette=palette)

score = scores if score is None else score
score = _assert_non_empty_sequence(score, name="centrality scores")
Expand Down Expand Up @@ -292,7 +292,7 @@ def ripley(
legend_kwargs.setdefault("bbox_to_anchor", (1, 0.5))

categories = adata.obs[cluster_key].cat.categories
palette = _get_palette(adata, cluster_key=cluster_key, categories=categories) if palette is None else palette
palette = _get_palette(adata, cluster_key=cluster_key, categories=categories, palette=palette)
if ax is None:
fig, ax = plt.subplots(figsize=figsize, dpi=dpi)
else:
Expand Down
5 changes: 3 additions & 2 deletions squidpy/pl/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,14 @@ def _warn_if_exists_obs(adata: AnnData, obs_key: str) -> None:
for col in obsm.columns:
obs_key = f"{prefix[i]}{col}"
_warn_if_exists_obs(tmp_adata, obs_key)
tmp_adata.obs[obs_key] = obsm.loc[:, col]
tmp_adata.obs[obs_key] = obsm[col]
else:
# names will be integer indices
for j in range(obsm.shape[1]):
obs_key = f"{prefix[i]}{j}"
_warn_if_exists_obs(tmp_adata, obs_key)
tmp_adata.obs[obs_key] = obsm[:, j]
# https://github.com/scverse/squidpy/issues/646
tmp_adata.obs[obs_key] = pd.Series(obsm[:, j], index=tmp_adata.obs_names)

return tmp_adata

Expand Down
26 changes: 8 additions & 18 deletions tests/plotting/test_image.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import pytest
import scanpy as sc
from anndata import AnnData
Expand Down Expand Up @@ -67,15 +68,12 @@ def test_transpose_channelwise(self, small_cont_4d: ImageContainer, transpose: b
assert res is None, res


def test_extract(adata: AnnData, cont: ImageContainer, caplog):
"""
Calculate features and extract columns to obs
"""
# get obsm
@pytest.mark.parametrize("is_view", [False, True])
def test_extract(adata: AnnData, cont: ImageContainer, caplog, is_view: bool):
sq.im.calculate_image_features(adata, cont, features=["summary"])

# extract columns (default values)
extr_adata = sq.pl.extract(adata)
extr_adata = sq.pl.extract(adata[:10] if is_view else adata)
# Test that expected columns exist
for col in [
"summary_ch-0_quantile-0.9",
Expand All @@ -88,15 +86,15 @@ def test_extract(adata: AnnData, cont: ImageContainer, caplog):
"summary_ch-2_quantile-0.5",
"summary_ch-2_quantile-0.1",
]:
assert col in extr_adata.obs.columns
np.testing.assert_array_equal(np.isfinite(extr_adata.obs[col]), True)

# get obsm that is a numpy array
adata.obsm["pca_features"] = sc.pp.pca(np.asarray(adata.obsm["img_features"]), n_comps=3)
# extract columns
extr_adata = sq.pl.extract(adata, obsm_key="pca_features", prefix="pca_features")
extr_adata = sq.pl.extract(adata[3:10] if is_view else adata, obsm_key="pca_features", prefix="pca_features")
# Test that expected columns exist
for col in ["pca_features_0", "pca_features_1", "pca_features_2"]:
assert col in extr_adata.obs.columns
np.testing.assert_array_equal(np.isfinite(extr_adata.obs[col]), True)

# extract multiple obsm at once (no prefix)
extr_adata = sq.pl.extract(adata, obsm_key=["img_features", "pca_features"])
Expand All @@ -115,12 +113,4 @@ def test_extract(adata: AnnData, cont: ImageContainer, caplog):
"1",
"2",
]:
assert col in extr_adata.obs.columns

# TODO: test similarly to ligrec
# currently logging to stderr, and not captured by caplog
# extract obsm twice and make sure that warnings are issued
# with caplog.at_level(logging.WARNING):
# extr2_adata = sq.pl.extract(extr_adata, obsm_key=['pca_features'])
# log = caplog.text
# assert "will be overwritten by extract" in log
np.testing.assert_array_equal(np.isfinite(extr_adata.obs[col]), True)
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ basepython = python3.9
deps = -r{toxinidir}/docs/requirements.txt
skip_install = true
allowlist_externals = sphinx-build
passenv = SQUIDPY_DOWNLOAD_NOTEBOOKS
passenv = SQUIDPY_DOWNLOAD_NOTEBOOKS,PYENCHANT_LIBRARY_PATH
commands =
sphinx-build -W --keep-going -b spelling {toxinidir}/docs/source {toxinidir}/docs/build/spellcheck
sphinx-build -q -W --keep-going -b linkcheck {toxinidir}/docs/source {toxinidir}/docs/build/linkcheck
Expand Down

0 comments on commit fa4365d

Please sign in to comment.