-
Notifications
You must be signed in to change notification settings - Fork 46
/
conftest.py
360 lines (272 loc) · 11.7 KB
/
conftest.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
import pathlib
import warnings
from typing import Optional, Tuple
import pytest
from _helpers import create_model
import numpy as np
from numba.core.errors import NumbaPerformanceWarning
import matplotlib
import scanpy as sc
from anndata import AnnData
import cellrank as cr
from cellrank.estimators import CFLARE, GPCCA
from cellrank.kernels import ConnectivityKernel, VelocityKernel
from cellrank.models import GAM, GAMR, SKLearnModel
_adata_small = sc.read("tests/_ground_truth_adatas/adata_50.h5ad")
_adata_medium = sc.read("tests/_ground_truth_adatas/adata_100.h5ad")
_adata_large = sc.read("tests/_ground_truth_adatas/adata_200.h5ad")
def pytest_sessionstart(session: pytest.Session) -> None:
matplotlib.use("Agg")
matplotlib.rcParams["figure.max_open_warning"] = 0
np.random.seed(42) # noqa: NPY002
# https://github.com/theislab/cellrank/issues/683
warnings.simplefilter("ignore", NumbaPerformanceWarning)
# removes overly verbose and useless logging errors for rpy2
# see: https://github.com/pytest-dev/pytest/issues/5502#issuecomment-647157873
def pytest_sessionfinish(session: pytest.Session, exitstatus) -> None:
import logging
loggers = [logging.getLogger()] + list(logging.Logger.manager.loggerDict.values())
for logger in loggers:
handlers = getattr(logger, "handlers", [])
for handler in handlers:
logger.removeHandler(handler)
def _create_cflare(*, backward: bool = False) -> Tuple[AnnData, CFLARE]:
adata = _adata_medium.copy()
sc.tl.paga(adata, groups="clusters")
vk = VelocityKernel(adata, backward=backward).compute_transition_matrix(softmax_scale=4)
ck = ConnectivityKernel(adata).compute_transition_matrix()
final_kernel = 0.8 * vk + 0.2 * ck
mc = CFLARE(final_kernel)
final_kernel.write_to_adata()
mc.compute_eigendecomposition()
mc.predict(use=2, method="kmeans")
mc.compute_fate_probabilities(use_petsc=False)
mc.compute_lineage_drivers(cluster_key="clusters", use_raw=False)
assert adata is mc.adata
np.testing.assert_allclose(mc.fate_probabilities.X.sum(1), 1.0, rtol=1e-6)
return adata, mc
def _create_gpcca(*, backward: bool = False) -> Tuple[AnnData, GPCCA]:
adata = _adata_medium.copy()
sc.tl.paga(adata, groups="clusters")
vk = VelocityKernel(adata, backward=backward).compute_transition_matrix(softmax_scale=4)
ck = ConnectivityKernel(adata).compute_transition_matrix()
final_kernel = 0.8 * vk + 0.2 * ck
mc = GPCCA(final_kernel)
final_kernel.write_to_adata()
mc.compute_eigendecomposition()
mc.compute_schur(method="krylov")
mc.compute_macrostates(n_states=2)
mc.set_terminal_states()
mc.compute_fate_probabilities(use_petsc=False)
mc.compute_lineage_drivers(cluster_key="clusters", use_raw=False)
assert adata is mc.adata
np.testing.assert_allclose(mc.fate_probabilities.X.sum(1), 1.0, rtol=1e-6)
return adata, mc
def _create_gamr_model(_adata: AnnData) -> Optional[GAMR]:
try:
m = GAMR(_adata)
m.prepare(_adata.var_names[0], "0", "latent_time").fit()
m.predict(level=0.95)
return m
except Exception: # noqa: BLE001
return None
@pytest.fixture()
def adata() -> AnnData:
return _adata_small.copy()
@pytest.fixture()
def adata_large() -> AnnData:
return _adata_large.copy()
@pytest.fixture()
def adata_cflare_fwd(
adata_cflare=_create_cflare(backward=False), # noqa: B008
) -> Tuple[AnnData, CFLARE]:
adata, cflare = adata_cflare
return adata.copy(), cflare
@pytest.fixture()
def adata_gpcca_fwd(adata_gpcca=_create_gpcca(backward=False)) -> Tuple[AnnData, GPCCA]: # noqa: B008
adata, gpcca = adata_gpcca
return adata.copy(), gpcca
@pytest.fixture()
def adata_gpcca_bwd(adata_gpcca=_create_gpcca(backward=True)) -> Tuple[AnnData, GPCCA]: # noqa: B008
adata, gpcca = adata_gpcca
return adata.copy(), gpcca
@pytest.fixture()
def adata_cflare(adata_cflare=_create_cflare(backward=False)) -> AnnData: # noqa: B008
return adata_cflare[0].copy()
@pytest.fixture()
def g(adata_gpcca=_create_gpcca(backward=False)) -> Tuple[AnnData, GPCCA]: # noqa: B008
return adata_gpcca[1].copy()
@pytest.fixture(scope="session")
def adata_gamr(adata_cflare=_create_cflare(backward=False)) -> AnnData: # noqa: B008
return adata_cflare[0].copy()
@pytest.fixture(scope="session")
def gamr_model(adata_gamr: AnnData, tmp_path_factory: pathlib.Path, worker_id: str) -> Optional[GAMR]:
if worker_id == "master":
model = _create_gamr_model(adata_gamr)
else:
root_tmp_dir = tmp_path_factory.getbasetemp().parent
fn = root_tmp_dir / "model.pickle"
if fn.is_file():
model = GAMR.read(fn)
else:
model = _create_gamr_model(adata_gamr)
if model is not None:
model.write(fn)
if model is None:
pytest.skip("Unable to create `cellrank.models.GAMR`.")
return model
@pytest.fixture()
def pygam_model(adata_cflare: AnnData) -> GAM:
m = GAM(adata_cflare)
m.prepare(adata_cflare.var_names[0], "0", "latent_time").fit()
m.predict()
m.confidence_interval()
return m
@pytest.fixture()
def sklearn_model(adata_cflare: AnnData) -> SKLearnModel:
m = create_model(adata_cflare)
assert isinstance(m, SKLearnModel), m
m.prepare(adata_cflare.var_names[0], "0", "latent_time").fit()
m.predict()
m.confidence_interval()
return m
@pytest.fixture()
def lineage():
x = cr._utils.Lineage(
np.array(
[
[1.23459664e-01, 1.29965675e-01, 1.92828002e-01, 9.39402664e-01],
[1.05635239e00, 4.45833459e-01, 2.29080759e00, 1.90132652e00],
[6.77880737e-02, 4.97556864e-02, 1.18428661e00, 2.02318999e-01],
[4.87500398e-01, 1.00657498e00, 2.20834882e-02, 5.03008905e-01],
[6.27190917e00, 7.27864781e00, 1.03978903e00, 1.55903460e01],
[3.85149269e-01, 3.54765380e-01, 1.77871487e-01, 8.22138648e-02],
[7.06618729e00, 1.33133671e01, 1.44904591e00, 5.79813391e00],
[8.18005744e-02, 5.36844933e-01, 1.86646162e00, 2.41141727e00],
[1.44892035e-01, 2.34036215e-01, 6.32392890e-01, 1.13211403e-02],
[2.44926466e-01, 2.50293183e-01, 1.77540208e-01, 3.27240144e-01],
]
),
names=["foo", "bar", "baz", "quux"],
)
return x / x.sum(1)
@pytest.fixture()
def kernel(adata_large: AnnData):
vk = VelocityKernel(adata_large).compute_transition_matrix(softmax_scale=4)
ck = ConnectivityKernel(adata_large).compute_transition_matrix()
return (0.8 * vk + 0.2 * ck).compute_transition_matrix()
@pytest.fixture(scope="session")
def test_matrix_1() -> np.ndarray:
"""
Parameters
----------
Returns
-------
:class:`numpy.ndarray`
Row-normalized transition matrix. This matrix is
- connected
- irreducible
- not reversible
"""
# fmt: off
return np.array([
# 0. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11.
[0.0, 0.8, 0.2, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], # 0
[0.2, 0.0, 0.6, 0.2, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], # 1
[0.6, 0.2, 0.0, 0.2, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], # 2
[0.0, 0.05, 0.05, 0.0, 0.6, 0.3, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], # 3
[0.0, 0.0, 0.0, 0.25, 0.0, 0.25, 0.4, 0.0, 0.0, 0.1, 0.0, 0.0], # 4
[0.0, 0.0, 0.0, 0.25, 0.25, 0.0, 0.1, 0.0, 0.0, 0.4, 0.0, 0.0], # 5
[0.0, 0.0, 0.0, 0.0, 0.05, 0.05, 0.0, 0.7, 0.2, 0.0, 0.0, 0.0], # 6
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.2, 0.0, 0.8, 0.0, 0.0, 0.0], # 7
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.8, 0.2, 0.0, 0.0, 0.0, 0.0], # 8
[0.0, 0.0, 0.0, 0.0, 0.05, 0.05, 0.0, 0.0, 0.0, 0.0, 0.7, 0.2], # 9
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.2, 0.0, 0.8], # 10
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.8, 0.2, 0.0], # 11
])
# fmt: on
@pytest.fixture(scope="session")
def test_matrix_2() -> np.ndarray:
"""
Parameters
----------
Returns
-------
:class:`numpy.ndarray`
Row-normalized transition matrix. This matrix is
- connected
- not irreducible (1 recurrent, 1 transient class)
- not reversible
"""
# fmt: off
return np.array([
# 0. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13.
[0.0, 0.8, 0.2, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], # 0
[0.2, 0.0, 0.6, 0.2, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], # 1
[0.6, 0.2, 0.0, 0.2, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], # 2
[0.0, 0.05, 0.05, 0.0, 0.6, 0.3, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], # 3
[0.0, 0.0, 0.0, 0.25, 0.0, 0.25, 0.4, 0.0, 0.0, 0.1, 0.0, 0.0, 0.0, 0.0], # 4
[0.0, 0.0, 0.0, 0.25, 0.25, 0.0, 0.1, 0.0, 0.0, 0.3, 0.0, 0.0, 0.1, 0.0], # 5
[0.0, 0.0, 0.0, 0.0, 0.05, 0.05, 0.0, 0.7, 0.2, 0.0, 0.0, 0.0, 0.0, 0.0], # 6
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.2, 0.0, 0.8, 0.0, 0.0, 0.0, 0.0, 0.0], # 7
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.8, 0.2, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], # 8
[0.0, 0.0, 0.0, 0.0, 0.05, 0.05, 0.0, 0.0, 0.0, 0.0, 0.7, 0.2, 0.0, 0.0], # 9
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.2, 0.0, 0.8, 0.0, 0.0], # 10
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.8, 0.2, 0.0, 0.0, 0.0], # 11
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.6, 0.4], # 12
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.3, 0.7], # 13
])
# fmt: on
@pytest.fixture(scope="session")
def test_matrix_3() -> np.ndarray:
"""
Parameters
----------
Returns
-------
:class:`numpy.ndarray`
Row-normalized transition matrix. This matrix is
- not connected
- not irreducible (2 recurrent classes)
- not reversible
"""
# fmt: off
return np.array([
# 0. 1. 2. 3.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0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.3, 0.7], # 13
])
# fmt: on
@pytest.fixture(scope="session")
def test_matrix_4() -> np.ndarray:
"""
Parameters
----------
Returns
-------
:class:`numpy.ndarray`
Symmetric matrix. Not a transition matrix.
"""
# fmt: off
return np.array(
[
# 0. 1. 2. 3.
[0.0, 0.8, 0.2, 0.0], # 0
[0.8, 0.0, 0.6, 0.2], # 1
[0.2, 0.6, 0.0, 0.2], # 2
[0.0, 0.2, 0.2, 0.0], # 3
]
)
# fmt: on