Skip to content

Commit 93ac9f0

Browse files
committed
simplify tests
1 parent 5cd0d25 commit 93ac9f0

1 file changed

Lines changed: 29 additions & 71 deletions

File tree

‎tests/plotting/legacy/test_plotting.py‎

Lines changed: 29 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -395,68 +395,6 @@ def test_dotplot_style_no_reset():
395395
assert plot.cmap == "winter", "style() should not reset unspecified parameters"
396396

397397

398-
@pytest.mark.parametrize(
399-
"plot_fn",
400-
[sc.pl.dotplot, sc.pl.matrixplot],
401-
ids=["dotplot", "matrixplot"],
402-
)
403-
@pytest.mark.parametrize(
404-
("values", "expected_suffixes"),
405-
[
406-
pytest.param(
407-
pd.Categorical(np.tile([1, 2], 350)), ("_1", "_2"), id="int_categorical"
408-
),
409-
pytest.param(np.tile([0.5, 2.0], 350), ("_0.5", "_2.0"), id="numeric"),
410-
],
411-
)
412-
def test_multi_groupby_nonstring(plot_fn, values, expected_suffixes):
413-
"""Multi-column `groupby` accepts non-string columns."""
414-
adata = pbmc68k_reduced()
415-
adata.obs["num"] = values
416-
plot = plot_fn(adata, adata.var_names[:4], ["bulk_labels", "num"], return_fig=True)
417-
categories = list(plot.categories)
418-
assert len(categories) == 2 * adata.obs["bulk_labels"].nunique()
419-
first_label = adata.obs["bulk_labels"].cat.categories[0]
420-
assert categories[:2] == [f"{first_label}{s}" for s in expected_suffixes]
421-
422-
423-
@pytest.mark.parametrize(
424-
"plot_fn",
425-
[sc.pl.dotplot, sc.pl.matrixplot],
426-
ids=["dotplot", "matrixplot"],
427-
)
428-
def test_multi_groupby_partial_nan(plot_fn):
429-
"""Rows missing any of several `groupby` values get no joined category."""
430-
adata = pbmc68k_reduced()
431-
labels = adata.obs["bulk_labels"].astype(object)
432-
labels.iloc[:10] = np.nan
433-
adata.obs["part_nan"] = pd.Categorical(labels)
434-
plot = plot_fn(adata, adata.var_names[:4], ["part_nan", "phase"], return_fig=True)
435-
assert not any("nan" in str(c) for c in plot.categories)
436-
437-
438-
@pytest.mark.parametrize(
439-
"plot_fn",
440-
[sc.pl.dotplot, sc.pl.matrixplot],
441-
ids=["dotplot", "matrixplot"],
442-
)
443-
def test_multi_groupby_mixed_type(plot_fn):
444-
"""Multi-column `groupby` accepts object columns mixing types and missing values."""
445-
adata = pbmc68k_reduced()
446-
mixed = np.empty(len(adata), dtype=object)
447-
mixed[::2] = "control"
448-
mixed[1::2] = 1
449-
mixed[:10] = np.nan
450-
adata.obs["mixed"] = mixed
451-
plot = plot_fn(
452-
adata, adata.var_names[:4], ["bulk_labels", "mixed"], return_fig=True
453-
)
454-
categories = [str(c) for c in plot.categories]
455-
assert not any("nan" in c for c in categories)
456-
assert any(c.endswith("_control") for c in categories)
457-
assert any(c.endswith("_1") for c in categories)
458-
459-
460398
def test_dotplot_add_totals(plot_cmp):
461399
pbmc = pbmc68k_reduced()
462400
markers = {"T-cell": "CD3D", "B-cell": "CD79A", "myeloid": "CST3"}
@@ -1750,10 +1688,27 @@ def test_groupby_index(plot_cmp):
17501688
plot_cmp("dotplot_groupby_index")
17511689

17521690

1753-
def test_groupby_list() -> None:
1754-
"""Test category order when groupby is a list.
1691+
def _mixed_with_nan(n: int) -> np.ndarray:
1692+
col = np.array(["control", 1] * (n // 2) + ["control"] * (n % 2), dtype=object)
1693+
col[:10] = np.nan
1694+
return col
1695+
1696+
1697+
@pytest.mark.parametrize(
1698+
"make_other",
1699+
[
1700+
pytest.param(lambda a: a.obs["cell_type"], id="categorical"),
1701+
pytest.param(
1702+
lambda a: pd.Categorical(np.arange(a.n_obs) % 2 + 1), id="int_categorical"
1703+
),
1704+
pytest.param(lambda a: np.where(np.arange(a.n_obs) % 2, 2.0, 0.5), id="float"),
1705+
pytest.param(lambda a: _mixed_with_nan(a.n_obs), id="mixed_with_nan"),
1706+
],
1707+
)
1708+
def test_groupby_list(make_other: Callable[[AnnData], object]) -> None:
1709+
"""Test category order when groupby is a list, incl. non-string and missing values.
17551710
1756-
See <https://github.com/scverse/scanpy/issues/1735>
1711+
See <https://github.com/scverse/scanpy/issues/1735> and #4384.
17571712
"""
17581713
rng = np.random.default_rng()
17591714
adata = krumsiek11()
@@ -1762,25 +1717,28 @@ def test_groupby_list() -> None:
17621717
cats = adata.obs.cell_type.cat.categories.tolist()
17631718
rng.shuffle(cats)
17641719
adata.obs["rand_cat"] = pd.Categorical(cat_val, categories=cats)
1720+
adata.obs["other"] = make_other(adata)
17651721

17661722
plot = sc.pl.dotplot(
17671723
adata,
17681724
["Gata1", "Gata2"],
1769-
["rand_cat", "cell_type"],
1725+
["rand_cat", "other"],
17701726
swap_axes=True,
17711727
show=False,
17721728
return_fig=True,
17731729
)
17741730
plot.make_figure()
17751731
ticks = [t.get_text() for t in plot.get_axes()["mainplot_ax"].get_xticklabels()]
17761732

1777-
# each group’s categories determine the tick order, unobserved pairs are dropped
1778-
observed = set(zip(adata.obs.rand_cat, adata.obs.cell_type, strict=True))
1733+
# each group’s categories determine the tick order,
1734+
# unobserved pairs and rows with missing values are dropped
1735+
other = adata.obs["other"]
1736+
observed = set(zip(adata.obs.rand_cat, other, strict=True))
17791737
assert ticks == [
1780-
f"{rand}_{cell}"
1738+
f"{rand}_{o}"
17811739
for rand in cats
1782-
for cell in adata.obs.cell_type.cat.categories
1783-
if (rand, cell) in observed
1740+
for o in other.astype("category").cat.categories
1741+
if (rand, o) in observed
17841742
]
17851743

17861744

0 commit comments

Comments
 (0)