forked from scverse/scanpy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_clustering.py
177 lines (148 loc) · 5.28 KB
/
test_clustering.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
from __future__ import annotations
import pytest
from sklearn.metrics.cluster import normalized_mutual_info_score
import scanpy as sc
from scanpy.testing._helpers.data import pbmc68k_reduced
from scanpy.testing._pytest.marks import needs
@pytest.fixture
def adata_neighbors():
return pbmc68k_reduced()
FLAVORS = [
pytest.param("igraph", marks=needs.igraph),
pytest.param("leidenalg", marks=needs.leidenalg),
]
@needs.leidenalg
@needs.igraph
@pytest.mark.parametrize("flavor", FLAVORS)
@pytest.mark.parametrize("resolution", [1, 2])
@pytest.mark.parametrize("n_iterations", [-1, 3])
def test_leiden_basic(adata_neighbors, flavor, resolution, n_iterations):
sc.tl.leiden(
adata_neighbors,
flavor=flavor,
resolution=resolution,
n_iterations=n_iterations,
directed=(flavor == "leidenalg"),
)
assert adata_neighbors.uns["leiden"]["params"]["resolution"] == resolution
assert adata_neighbors.uns["leiden"]["params"]["n_iterations"] == n_iterations
@needs.leidenalg
@needs.igraph
@pytest.mark.parametrize("flavor", FLAVORS)
def test_leiden_random_state(adata_neighbors, flavor):
is_leiden_alg = flavor == "leidenalg"
n_iterations = 2 if is_leiden_alg else -1
adata_1 = sc.tl.leiden(
adata_neighbors,
flavor=flavor,
random_state=1,
copy=True,
directed=is_leiden_alg,
n_iterations=n_iterations,
)
adata_1_again = sc.tl.leiden(
adata_neighbors,
flavor=flavor,
random_state=1,
copy=True,
directed=is_leiden_alg,
n_iterations=n_iterations,
)
adata_2 = sc.tl.leiden(
adata_neighbors,
flavor=flavor,
random_state=2,
copy=True,
directed=is_leiden_alg,
n_iterations=n_iterations,
)
assert (adata_1.obs["leiden"] == adata_1_again.obs["leiden"]).all()
# This random state produces different categories so can't check the arrays against each other.
assert (adata_2.obs["leiden"] != adata_1_again.obs["leiden"]).any()
@needs.igraph
def test_leiden_igraph_directed(adata_neighbors):
with pytest.raises(ValueError):
sc.tl.leiden(adata_neighbors, flavor="igraph")
@needs.leidenalg
@needs.igraph
def test_leiden_equal_defaults_same_args(adata_neighbors):
"""Ensure the two implementations are the same for the same args."""
leiden_alg_clustered = sc.tl.leiden(
adata_neighbors, flavor="leidenalg", copy=True, n_iterations=2
)
igraph_clustered = sc.tl.leiden(
adata_neighbors, flavor="igraph", copy=True, directed=False, n_iterations=2
)
assert (
normalized_mutual_info_score(
leiden_alg_clustered.obs["leiden"], igraph_clustered.obs["leiden"]
)
> 0.9
)
@needs.leidenalg
@needs.igraph
def test_leiden_equal_defaults(adata_neighbors):
"""Ensure that the old leidenalg defaults are close enough to the current default outputs."""
leiden_alg_clustered = sc.tl.leiden(
adata_neighbors, flavor="leidenalg", directed=True, copy=True
)
igraph_clustered = sc.tl.leiden(
adata_neighbors, copy=True, n_iterations=2, directed=False
)
assert (
normalized_mutual_info_score(
leiden_alg_clustered.obs["leiden"], igraph_clustered.obs["leiden"]
)
> 0.9
)
@needs.igraph
def test_leiden_objective_function(adata_neighbors):
"""Ensure that popping this as a `clustering_kwargs` and using it does not error out."""
sc.tl.leiden(
adata_neighbors,
objective_function="modularity",
flavor="igraph",
directed=False,
)
@needs.igraph
@pytest.mark.parametrize(
"clustering,key",
[
pytest.param(sc.tl.louvain, "louvain", marks=needs.louvain),
pytest.param(sc.tl.leiden, "leiden", marks=needs.leidenalg),
],
)
def test_clustering_subset(adata_neighbors, clustering, key):
clustering(adata_neighbors, key_added=key)
for c in adata_neighbors.obs[key].unique():
print("Analyzing cluster ", c)
cells_in_c = adata_neighbors.obs[key] == c
ncells_in_c = adata_neighbors.obs[key].value_counts().loc[c]
key_sub = str(key) + "_sub"
clustering(
adata_neighbors,
restrict_to=(key, [c]),
key_added=key_sub,
)
# Get new clustering labels
new_partition = adata_neighbors.obs[key_sub]
cat_counts = new_partition[cells_in_c].value_counts()
# Only original cluster's cells assigned to new categories
assert cat_counts.sum() == ncells_in_c
# Original category's cells assigned only to new categories
nonzero_cat = cat_counts[cat_counts > 0].index
common_cat = nonzero_cat.intersection(adata_neighbors.obs[key].cat.categories)
assert len(common_cat) == 0
@needs.louvain
@needs.igraph
def test_louvain_basic(adata_neighbors):
sc.tl.louvain(adata_neighbors)
sc.tl.louvain(adata_neighbors, use_weights=True)
sc.tl.louvain(adata_neighbors, use_weights=True, flavor="igraph")
sc.tl.louvain(adata_neighbors, flavor="igraph")
@needs.louvain
@needs.igraph
def test_partition_type(adata_neighbors):
import louvain
sc.tl.louvain(adata_neighbors, partition_type=louvain.RBERVertexPartition)
sc.tl.louvain(adata_neighbors, partition_type=louvain.SurpriseVertexPartition)