Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scipy 1.15 compat, some test refactors #3409

Merged
merged 2 commits into from
Dec 19, 2024
Merged
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
2 changes: 1 addition & 1 deletion src/scanpy/tools/_rank_genes_groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -854,7 +854,7 @@ def filter_rank_genes_groups(

if not use_logfolds or not use_fraction:
sub_X = adata.raw[:, var_names].X if use_raw else adata[:, var_names].X
in_group = adata.obs[groupby] == cluster
in_group = (adata.obs[groupby] == cluster).to_numpy()
X_in = sub_X[in_group]
X_out = sub_X[~in_group]

Expand Down
4 changes: 2 additions & 2 deletions tests/test_backed.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ def test_log1p_backed_errors(backed_adata):

def test_scatter_backed(backed_adata):
sc.pp.pca(backed_adata, chunked=True)
sc.pl.scatter(backed_adata, color="0", basis="pca")
sc.pl.scatter(backed_adata, color="0", basis="pca", show=False)


def test_dotplot_backed(backed_adata):
sc.pl.dotplot(backed_adata, ["0", "1", "2", "3"], groupby="cat")
sc.pl.dotplot(backed_adata, ["0", "1", "2", "3"], groupby="cat", show=False)
211 changes: 74 additions & 137 deletions tests/test_filter_rank_genes_groups.py
Original file line number Diff line number Diff line change
@@ -1,159 +1,96 @@
from __future__ import annotations

import numpy as np
import pytest

from scanpy.tools import filter_rank_genes_groups, rank_genes_groups
from testing.scanpy._helpers.data import pbmc68k_reduced

names_no_reference = np.array(
NAMES_NO_REF = [
["CD3D", "ITM2A", "CD3D", "CCL5", "CD7", "nan", "CD79A", "nan", "NKG7", "LYZ"],
["CD3E", "CD3D", "nan", "NKG7", "CD3D", "AIF1", "CD79B", "nan", "GNLY", "CST3"],
["IL32", "RPL39", "nan", "CST7", "nan", "nan", "nan", "SNHG7", "CD7", "nan"],
["nan", "SRSF7", "IL32", "GZMA", "nan", "LST1", "IGJ", "nan", "CTSW", "nan"],
["nan", "nan", "CD2", "CTSW", "CD8B", "TYROBP", "ISG20", "SNHG8", "GZMB", "nan"],
]

NAMES_REF = [
["CD3D", "ITM2A", "CD3D", "nan", "CD3D", "nan", "CD79A", "nan", "CD7"],
["nan", "nan", "nan", "CD3D", "nan", "AIF1", "nan", "nan", "NKG7"],
["nan", "nan", "nan", "NKG7", "nan", "FCGR3A", "ISG20", "SNHG7", "CTSW"],
["nan", "CD3D", "nan", "CCL5", "CD7", "nan", "CD79B", "nan", "GNLY"],
["CD3E", "IL32", "nan", "IL32", "CD27", "FCER1G", "nan", "nan", "nan"],
]

NAMES_NO_REF_COMPARE_ABS = [
[
["CD3D", "ITM2A", "CD3D", "CCL5", "CD7", "nan", "CD79A", "nan", "NKG7", "LYZ"],
["CD3E", "CD3D", "nan", "NKG7", "CD3D", "AIF1", "CD79B", "nan", "GNLY", "CST3"],
["IL32", "RPL39", "nan", "CST7", "nan", "nan", "nan", "SNHG7", "CD7", "nan"],
["nan", "SRSF7", "IL32", "GZMA", "nan", "LST1", "IGJ", "nan", "CTSW", "nan"],
[
"nan",
"nan",
"CD2",
"CTSW",
"CD8B",
"TYROBP",
"ISG20",
"SNHG8",
"GZMB",
"nan",
],
]
)

names_reference = np.array(
*("CD3D", "ITM2A", "HLA-DRB1", "CCL5", "HLA-DPA1"),
*("nan", "CD79A", "nan", "NKG7", "LYZ"),
],
[
["CD3D", "ITM2A", "CD3D", "nan", "CD3D", "nan", "CD79A", "nan", "CD7"],
["nan", "nan", "nan", "CD3D", "nan", "AIF1", "nan", "nan", "NKG7"],
["nan", "nan", "nan", "NKG7", "nan", "FCGR3A", "ISG20", "SNHG7", "CTSW"],
["nan", "CD3D", "nan", "CCL5", "CD7", "nan", "CD79B", "nan", "GNLY"],
["CD3E", "IL32", "nan", "IL32", "CD27", "FCER1G", "nan", "nan", "nan"],
]
)

names_compare_abs = np.array(
*("HLA-DPA1", "nan", "CD3D", "NKG7", "HLA-DRB1"),
*("AIF1", "CD79B", "nan", "GNLY", "CST3"),
],
[
[
"CD3D",
"ITM2A",
"HLA-DRB1",
"CCL5",
"HLA-DPA1",
"nan",
"CD79A",
"nan",
"NKG7",
"LYZ",
],
[
"HLA-DPA1",
"nan",
"CD3D",
"NKG7",
"HLA-DRB1",
"AIF1",
"CD79B",
"nan",
"GNLY",
"CST3",
],
[
"nan",
"PSAP",
"CD74",
"CST7",
"CD74",
"PSAP",
"FCER1G",
"SNHG7",
"CD7",
"HLA-DRA",
],
[
"IL32",
"nan",
"HLA-DRB5",
"GZMA",
"HLA-DRB5",
"LST1",
"nan",
"nan",
"CTSW",
"HLA-DRB1",
],
[
"nan",
"FCER1G",
"HLA-DPB1",
"CTSW",
"HLA-DPB1",
"TYROBP",
"TYROBP",
"S100A10",
"GZMB",
"HLA-DPA1",
],
]
)


def test_filter_rank_genes_groups():
adata = pbmc68k_reduced()

# fix filter defaults
args = {
"adata": adata,
"key_added": "rank_genes_groups_filtered",
"min_in_group_fraction": 0.25,
"min_fold_change": 1,
"max_out_group_fraction": 0.5,
}

rank_genes_groups(
adata, "bulk_labels", reference="Dendritic", method="wilcoxon", n_genes=5
)
filter_rank_genes_groups(**args)

assert np.array_equal(
names_reference,
np.array(adata.uns["rank_genes_groups_filtered"]["names"].tolist()),
)
*("nan", "PSAP", "CD74", "CST7", "CD74"),
*("PSAP", "FCER1G", "SNHG7", "CD7", "HLA-DRA"),
],
[
*("IL32", "nan", "HLA-DRB5", "GZMA", "HLA-DRB5"),
*("LST1", "nan", "nan", "CTSW", "HLA-DRB1"),
],
[
*("nan", "FCER1G", "HLA-DPB1", "CTSW", "HLA-DPB1"),
*("TYROBP", "TYROBP", "S100A10", "GZMB", "HLA-DPA1"),
],
]

rank_genes_groups(adata, "bulk_labels", method="wilcoxon", n_genes=5)
filter_rank_genes_groups(**args)

assert np.array_equal(
names_no_reference,
np.array(adata.uns["rank_genes_groups_filtered"]["names"].tolist()),
)
EXPECTED = {
("Dendritic", False): np.array(NAMES_REF),
("rest", False): np.array(NAMES_NO_REF),
("rest", True): np.array(NAMES_NO_REF_COMPARE_ABS),
}

rank_genes_groups(adata, "bulk_labels", method="wilcoxon", pts=True, n_genes=5)
filter_rank_genes_groups(**args)

assert np.array_equal(
names_no_reference,
np.array(adata.uns["rank_genes_groups_filtered"]["names"].tolist()),
)
@pytest.mark.parametrize(
("reference", "pts", "abs"),
[
pytest.param("Dendritic", False, False, id="ref-no_pts-no_abs"),
pytest.param("rest", False, False, id="rest-no_pts-no_abs"),
pytest.param("rest", True, False, id="rest-pts-no_abs"),
pytest.param("rest", True, True, id="rest-pts-abs"),
],
)
def test_filter_rank_genes_groups(reference, pts, abs):
adata = pbmc68k_reduced()

# test compare_abs
rank_genes_groups(
adata, "bulk_labels", method="wilcoxon", pts=True, rankby_abs=True, n_genes=5
)

filter_rank_genes_groups(
adata,
compare_abs=True,
min_in_group_fraction=-1,
max_out_group_fraction=1,
min_fold_change=3.1,
"bulk_labels",
reference=reference,
pts=pts,
method="wilcoxon",
rankby_abs=abs,
n_genes=5,
)
if abs:
filter_rank_genes_groups(
adata,
compare_abs=True,
min_in_group_fraction=-1,
max_out_group_fraction=1,
min_fold_change=3.1,
)
else:
filter_rank_genes_groups(
adata,
min_in_group_fraction=0.25,
min_fold_change=1,
max_out_group_fraction=0.5,
)

assert np.array_equal(
names_compare_abs,
EXPECTED[reference, abs],
np.array(adata.uns["rank_genes_groups_filtered"]["names"].tolist()),
)
3 changes: 2 additions & 1 deletion tests/test_preprocessing_distributed.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ def adata() -> AnnData:
return a


@filter_oldformatwarning
@pytest.fixture(
params=[
pytest.param("direct", marks=[needs.zappy]),
pytest.param("dask", marks=[needs.dask, pytest.mark.anndata_dask_support]),
]
)
@filter_oldformatwarning
def adata_dist(request: pytest.FixtureRequest) -> AnnData:
# regular anndata except for X, which we replace on the next line
a = read_zarr(input_file)
Expand Down Expand Up @@ -75,6 +75,7 @@ def test_log1p(adata: AnnData, adata_dist: AnnData):
npt.assert_allclose(result, adata.X)


@pytest.mark.filterwarnings("ignore:Use sc.pp.normalize_total instead:FutureWarning")
def test_normalize_per_cell(
request: pytest.FixtureRequest, adata: AnnData, adata_dist: AnnData
):
Expand Down
Loading