forked from theislab/cellrank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_pipeline.py
154 lines (112 loc) · 5.48 KB
/
test_pipeline.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
import cellrank as cr
from anndata import AnnData
from cellrank.kernels import VelocityKernel, ConnectivityKernel
from cellrank._utils._key import Key
import numpy as np
import pandas as pd
from pandas.api.types import is_categorical_dtype
def _assert_has_all_keys(adata: AnnData, bwd: bool = False) -> None:
# fmt: off
# term states
key = Key.obs.term_states(bwd)
assert is_categorical_dtype(adata.obs[key])
assert Key.obs.probs(key) in adata.obs
assert Key.uns.colors(key) in adata.uns
# fate probabilities
fate_probs = adata.obsm[Key.obsm.fate_probs(bwd)]
assert isinstance(fate_probs, cr.Lineage)
np.testing.assert_array_equal(fate_probs.names, adata.obs[key].cat.categories)
np.testing.assert_array_equal(fate_probs.colors, adata.uns[Key.uns.colors(key)])
np.testing.assert_allclose(fate_probs.X.sum(1), 1.0, rtol=1e-3)
# drivers
assert isinstance(adata.varm[Key.varm.lineage_drivers(bwd)], pd.DataFrame)
# fmt: on
class TestLowLevelPipeline:
def test_fwd_pipeline_cflare(self, adata: AnnData):
vk = VelocityKernel(adata).compute_transition_matrix(softmax_scale=4)
ck = ConnectivityKernel(adata).compute_transition_matrix()
final_kernel = 0.8 * vk + 0.2 * ck
estimator_fwd = cr.estimators.CFLARE(final_kernel)
estimator_fwd.compute_eigendecomposition()
estimator_fwd.plot_spectrum()
estimator_fwd.plot_spectrum(real_only=True)
estimator_fwd.predict(use=1, method="leiden")
estimator_fwd.plot_macrostates(which="terminal")
estimator_fwd.compute_fate_probabilities()
estimator_fwd.plot_fate_probabilities()
estimator_fwd.compute_lineage_drivers(cluster_key="clusters", use_raw=False)
_assert_has_all_keys(adata)
def test_bwd_pipeline_cflare(self, adata: AnnData):
vk = VelocityKernel(adata, backward=True).compute_transition_matrix(
softmax_scale=4
)
ck = ConnectivityKernel(adata).compute_transition_matrix()
final_kernel = 0.8 * vk + 0.2 * ck
estimator_bwd = cr.estimators.CFLARE(final_kernel)
estimator_bwd.compute_eigendecomposition()
estimator_bwd.plot_spectrum()
estimator_bwd.plot_spectrum(real_only=True)
estimator_bwd.predict(use=1, method="kmeans")
estimator_bwd.plot_macrostates(which="terminal")
estimator_bwd.compute_fate_probabilities()
estimator_bwd.plot_fate_probabilities()
estimator_bwd.compute_lineage_drivers(cluster_key="clusters", use_raw=False)
_assert_has_all_keys(adata, bwd=True)
def test_fwd_pipeline_gpcca(self, adata: AnnData):
vk = VelocityKernel(adata).compute_transition_matrix(softmax_scale=4)
ck = ConnectivityKernel(adata).compute_transition_matrix()
final_kernel = 0.8 * vk + 0.2 * ck
estimator_fwd = cr.estimators.GPCCA(final_kernel)
estimator_fwd.compute_eigendecomposition()
estimator_fwd.plot_spectrum()
estimator_fwd.plot_spectrum(real_only=True)
estimator_fwd.compute_schur(5, method="brandts")
estimator_fwd.compute_macrostates(3, n_cells=10)
estimator_fwd.plot_macrostates(which="all")
estimator_fwd.plot_coarse_T(show_initial_dist=True, show_stationary_dist=True)
estimator_fwd.plot_schur_matrix()
# select all states
estimator_fwd.set_terminal_states(n_cells=10)
estimator_fwd.plot_macrostates(which="terminal")
estimator_fwd.compute_fate_probabilities()
estimator_fwd.compute_lineage_drivers(cluster_key="clusters", use_raw=False)
_assert_has_all_keys(adata)
# select a subset of states
estimator_fwd.set_terminal_states(
n_cells=16,
states=estimator_fwd.macrostates.cat.categories[:2],
)
estimator_fwd.plot_macrostates(which="terminal")
estimator_fwd.compute_fate_probabilities()
estimator_fwd.compute_lineage_drivers(cluster_key="clusters", use_raw=False)
_assert_has_all_keys(adata)
def test_bwd_pipeline_gpcca(self, adata: AnnData):
vk = VelocityKernel(adata, backward=True).compute_transition_matrix(
softmax_scale=4
)
ck = ConnectivityKernel(adata).compute_transition_matrix()
final_kernel = 0.8 * vk + 0.2 * ck
estimator_bwd = cr.estimators.GPCCA(final_kernel)
estimator_bwd.compute_eigendecomposition()
estimator_bwd.plot_spectrum()
estimator_bwd.plot_spectrum(real_only=True)
estimator_bwd.compute_schur(5, method="brandts")
estimator_bwd.compute_macrostates(3, n_cells=16)
estimator_bwd.plot_macrostates(which="all")
estimator_bwd.plot_coarse_T(show_initial_dist=True, show_stationary_dist=True)
estimator_bwd.plot_schur_matrix()
# select all cells
estimator_bwd.set_terminal_states(n_cells=16)
estimator_bwd.plot_macrostates(which="terminal")
estimator_bwd.compute_fate_probabilities()
estimator_bwd.compute_lineage_drivers(cluster_key="clusters", use_raw=False)
_assert_has_all_keys(adata, bwd=True)
# select a subset of states
estimator_bwd.set_terminal_states(
n_cells=16,
states=estimator_bwd.macrostates.cat.categories[:2],
)
estimator_bwd.plot_macrostates(which="terminal")
estimator_bwd.compute_fate_probabilities()
estimator_bwd.compute_lineage_drivers(cluster_key="clusters", use_raw=False)
_assert_has_all_keys(adata, bwd=True)