Skip to content

Commit 0b675b0

Browse files
barlowa124meeseeksmachine
authored andcommitted
Backport PR #4385: fix: multi-column groupby crashing on non-string observations
1 parent 2f9ba38 commit 0b675b0

3 files changed

Lines changed: 46 additions & 23 deletions

File tree

‎docs/release-notes/4385.fix.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Multi-column `groupby` in {func}`~scanpy.pl.dotplot`, {func}`~scanpy.pl.matrixplot`,
2+
{func}`~scanpy.pl.stacked_violin`, {func}`~scanpy.pl.tracksplot`, and
3+
{func}`~scanpy.pl.heatmap` now accepts integer-categorical and numeric
4+
observation columns instead of raising `TypeError`.

‎src/scanpy/plotting/_anndata.py‎

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2148,22 +2148,21 @@ def _prepare_dataframe( # noqa: PLR0912
21482148
categorical = obs_tidy[groupby[0]].astype("category")
21492149
categorical.name = groupby[0]
21502150
else:
2151-
# join the groupby values using "_" to make a new 'category'
2152-
categorical = obs_tidy[groupby].apply("_".join, axis=1).astype("category")
2153-
categorical.name = "_".join(groupby)
2154-
2155-
# preserve category order
2151+
# join the groupby values using "_" to make a new 'category';
2152+
# rows missing any value stay NaN, `object` needed as `1` would get turned into `"1.0"`
2153+
parts = [
2154+
obs_tidy[g].map(str, na_action="ignore").astype(object) for g in groupby
2155+
]
2156+
# categories in product order of each column's categories (NaN-free, sorted if possible)
21562157
from itertools import product
21572158

2158-
order = {
2159-
"_".join(k): idx
2160-
for idx, k in enumerate(
2161-
product(*(obs_tidy[g].cat.categories for g in groupby))
2162-
)
2163-
}
2164-
categorical = categorical.cat.reorder_categories(
2165-
sorted(categorical.cat.categories, key=lambda x: order[x])
2166-
)
2159+
levels = [obs_tidy[g].astype("category").cat.categories for g in groupby]
2160+
all_cats = list(dict.fromkeys("_".join(map(str, k)) for k in product(*levels)))
2161+
categorical = pd.Series(
2162+
pd.Categorical(parts[0].str.cat(parts[1:], sep="_"), categories=all_cats),
2163+
index=obs_tidy.index,
2164+
name="_".join(groupby),
2165+
).cat.remove_unused_categories()
21672166
obs_tidy = obs_tidy[var_names].set_index(categorical)
21682167
categories = obs_tidy.index.categories
21692168

‎tests/plotting/legacy/test_plotting.py‎

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1642,10 +1642,27 @@ def test_groupby_index(plot_cmp):
16421642
plot_cmp("dotplot_groupby_index")
16431643

16441644

1645-
def test_groupby_list() -> None:
1646-
"""Test category order when groupby is a list.
1645+
def _mixed_with_nan(n: int) -> np.ndarray:
1646+
col = np.array(["control", 1] * (n // 2) + ["control"] * (n % 2), dtype=object)
1647+
col[:10] = np.nan
1648+
return col
16471649

1648-
See <https://github.com/scverse/scanpy/issues/1735>
1650+
1651+
@pytest.mark.parametrize(
1652+
"make_other",
1653+
[
1654+
pytest.param(lambda a: a.obs["cell_type"], id="categorical"),
1655+
pytest.param(
1656+
lambda a: pd.Categorical(np.arange(a.n_obs) % 2 + 1), id="int_categorical"
1657+
),
1658+
pytest.param(lambda a: np.where(np.arange(a.n_obs) % 2, 2.0, 0.5), id="float"),
1659+
pytest.param(lambda a: _mixed_with_nan(a.n_obs), id="mixed_with_nan"),
1660+
],
1661+
)
1662+
def test_groupby_list(make_other: Callable[[AnnData], object]) -> None:
1663+
"""Test category order when groupby is a list, incl. non-string and missing values.
1664+
1665+
See <https://github.com/scverse/scanpy/issues/1735> and #4384.
16491666
"""
16501667
rng = np.random.default_rng()
16511668
adata = krumsiek11()
@@ -1654,25 +1671,28 @@ def test_groupby_list() -> None:
16541671
cats = adata.obs.cell_type.cat.categories.tolist()
16551672
rng.shuffle(cats)
16561673
adata.obs["rand_cat"] = pd.Categorical(cat_val, categories=cats)
1674+
adata.obs["other"] = make_other(adata)
16571675

16581676
plot = sc.pl.dotplot(
16591677
adata,
16601678
["Gata1", "Gata2"],
1661-
["rand_cat", "cell_type"],
1679+
["rand_cat", "other"],
16621680
swap_axes=True,
16631681
show=False,
16641682
return_fig=True,
16651683
)
16661684
plot.make_figure()
16671685
ticks = [t.get_text() for t in plot.get_axes()["mainplot_ax"].get_xticklabels()]
16681686

1669-
# each group’s categories determine the tick order, unobserved pairs are dropped
1670-
observed = set(zip(adata.obs.rand_cat, adata.obs.cell_type, strict=True))
1687+
# each group’s categories determine the tick order,
1688+
# unobserved pairs and rows with missing values are dropped
1689+
other = adata.obs["other"]
1690+
observed = set(zip(adata.obs.rand_cat, other, strict=True))
16711691
assert ticks == [
1672-
f"{rand}_{cell}"
1692+
f"{rand}_{o}"
16731693
for rand in cats
1674-
for cell in adata.obs.cell_type.cat.categories
1675-
if (rand, cell) in observed
1694+
for o in other.astype("category").cat.categories
1695+
if (rand, o) in observed
16761696
]
16771697

16781698

0 commit comments

Comments
 (0)