-
Notifications
You must be signed in to change notification settings - Fork 608
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Scipy 1.15 compat, some test refactors (#3409)
- Loading branch information
1 parent
e3efba2
commit 1cd5a00
Showing
4 changed files
with
79 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters