def test_compute_macrostates_min_chi_normal_run(self, adata_large: AnnData): vk = VelocityKernel(adata_large).compute_transition_matrix( softmax_scale=4) ck = ConnectivityKernel(adata_large).compute_transition_matrix() terminal_kernel = 0.8 * vk + 0.2 * ck mc = cr.tl.estimators.GPCCA(terminal_kernel) mc.compute_schur(n_components=10, method="krylov") mc.compute_macrostates(n_states=[2, 4], use_min_chi=True) _check_compute_macro(mc)
def test_rename_terminal_states_try_joinining_states( self, adata_large: AnnData): vk = VelocityKernel(adata_large).compute_transition_matrix( softmax_scale=4) ck = ConnectivityKernel(adata_large).compute_transition_matrix() terminal_kernel = 0.8 * vk + 0.2 * ck mc = cr.tl.estimators.CFLARE(terminal_kernel) mc.compute_eigendecomposition(k=5) mc.compute_terminal_states(use=2) with pytest.raises(ValueError): mc.rename_terminal_states({"0, 1": "foo"})
def test_compute_gdpt_cellname_iroot(self, adata_large: AnnData): vk = VelocityKernel(adata_large).compute_transition_matrix( softmax_scale=4) ck = ConnectivityKernel(adata_large).compute_transition_matrix() terminal_kernel = 0.8 * vk + 0.2 * ck mc = cr.tl.estimators.GPCCA(terminal_kernel) mc.adata.uns["iroot"] = mc.adata.obs_names[0] mc.compute_gdpt() assert "gdpt_pseudotime" in mc.adata.obs
def test_rename_terminal_states_normal_run(self, adata_large: AnnData): vk = VelocityKernel(adata_large).compute_transition_matrix(softmax_scale=4) ck = ConnectivityKernel(adata_large).compute_transition_matrix() terminal_kernel = 0.8 * vk + 0.2 * ck mc = cr.tl.estimators.CFLARE(terminal_kernel) mc.compute_eigendecomposition(k=5) mc.compute_terminal_states(use=2) mc.rename_terminal_states({"0": "foo", "1": "bar"}) np.testing.assert_array_equal(mc._get(P.TERM).cat.categories, ["foo", "bar"])
def test_manual_combination_no_precomputed(self, adata: AnnData): density_normalize = False vk = VelocityKernel(adata).compute_transition_matrix(softmax_scale=4) ck = ConnectivityKernel(adata).compute_transition_matrix( density_normalize=density_normalize) T_vk = vk.transition_matrix T_ck = ck.transition_matrix T_comb_manual = 0.8 * T_vk + 0.2 * T_ck vk = VelocityKernel(adata).compute_transition_matrix(softmax_scale=4) ck = ConnectivityKernel(adata).compute_transition_matrix( density_normalize=density_normalize) comb_kernel = 0.8 * vk + 0.2 * ck comb_kernel.compute_transition_matrix() T_comb_kernel = comb_kernel.transition_matrix np.testing.assert_allclose(T_comb_manual.A, T_comb_kernel.A, rtol=_rtol)
def test_plot_lineage_drivers_not_computed(self, adata_large: AnnData): vk = VelocityKernel(adata_large).compute_transition_matrix(softmax_scale=4) ck = ConnectivityKernel(adata_large).compute_transition_matrix() terminal_kernel = 0.8 * vk + 0.2 * ck mc = cr.tl.estimators.CFLARE(terminal_kernel) mc.compute_eigendecomposition(k=5) mc.compute_terminal_states(use=2) mc.compute_absorption_probabilities() with pytest.raises(RuntimeError): mc.plot_lineage_drivers("0")
def test_plot_lineage_drivers_normal_run(self, adata_large: AnnData): vk = VelocityKernel(adata_large).compute_transition_matrix( softmax_scale=4) ck = ConnectivityKernel(adata_large).compute_transition_matrix() final_kernel = 0.8 * vk + 0.2 * ck mc = cr.tl.estimators.CFLARE(final_kernel) mc.compute_eigendecomposition(k=5) mc.compute_final_states(use=2) mc.compute_absorption_probabilities() mc.compute_lineage_drivers(use_raw=False, cluster_key="clusters") mc.plot_lineage_drivers("0", use_raw=False)
def test_compute_gdpt_cellname_key_added(self, adata_large: AnnData): vk = VelocityKernel(adata_large).compute_transition_matrix( softmax_scale=4) ck = ConnectivityKernel(adata_large).compute_transition_matrix() terminal_kernel = 0.8 * vk + 0.2 * ck mc = cr.tl.estimators.GPCCA(terminal_kernel) mc.compute_schur(n_components=10, method="krylov") mc.compute_gdpt(key_added="foobar") assert "foobar" in mc.adata.obs
def test_compute_terminal_states_no_cells(self, adata_large: AnnData): vk = VelocityKernel(adata_large).compute_transition_matrix( softmax_scale=4) ck = ConnectivityKernel(adata_large).compute_transition_matrix() terminal_kernel = 0.8 * vk + 0.2 * ck mc = cr.tl.estimators.GPCCA(terminal_kernel) mc.compute_schur(n_components=10, method="krylov") mc.compute_macrostates(n_states=2) with pytest.raises(TypeError): mc.compute_terminal_states(n_cells=None)
def create_kernels( adata: AnnData, velocity_variances: Optional[str] = None, connectivity_variances: Optional[str] = None, ) -> Tuple[VelocityKernel, ConnectivityKernel]: vk = VelocityKernel(adata) vk._mat_scaler = adata.uns.get( velocity_variances, np.random.normal(size=(adata.n_obs, adata.n_obs)) ) ck = ConnectivityKernel(adata) ck._mat_scaler = adata.uns.get( connectivity_variances, np.random.normal(size=(adata.n_obs, adata.n_obs)) ) vk._transition_matrix = csr_matrix(np.eye(adata.n_obs)) ck._transition_matrix = np.eye(adata.n_obs, k=1) / 2 + np.eye(adata.n_obs) / 2 ck._transition_matrix[-1, -1] = 1 ck._transition_matrix = csr_matrix(ck._transition_matrix) np.testing.assert_allclose( np.sum(ck._transition_matrix.A, axis=1), 1 ) # sanity check return vk, ck
def test_compute_metastable_cache(self, adata_large: AnnData): vk = VelocityKernel(adata_large).compute_transition_matrix( softmax_scale=4) ck = ConnectivityKernel(adata_large).compute_transition_matrix() final_kernel = 0.8 * vk + 0.2 * ck mc = cr.tl.estimators.GPCCA(final_kernel) mc.compute_schur(n_components=11, method="krylov") mc.compute_metastable_states(n_states=2) assert mc._get(P.SCHUR).shape[1] == 11 assert mc._get(P.SCHUR_MAT).shape == (11, 11)
def test_plot_lineage_drivers_invalid_n_genes(self, adata_large: AnnData): vk = VelocityKernel(adata_large).compute_transition_matrix(softmax_scale=4) ck = ConnectivityKernel(adata_large).compute_transition_matrix() terminal_kernel = 0.8 * vk + 0.2 * ck mc = cr.tl.estimators.CFLARE(terminal_kernel) mc.compute_eigendecomposition(k=5) mc.compute_terminal_states(use=2) mc.compute_absorption_probabilities() mc.compute_lineage_drivers(use_raw=False, cluster_key="clusters") with pytest.raises(ValueError): mc.plot_lineage_drivers("0", use_raw=False, n_genes=0)
def test_rename_terminal_states_empty_mapping(self, adata_large: AnnData): vk = VelocityKernel(adata_large).compute_transition_matrix(softmax_scale=4) ck = ConnectivityKernel(adata_large).compute_transition_matrix() terminal_kernel = 0.8 * vk + 0.2 * ck mc = cr.tl.estimators.CFLARE(terminal_kernel) mc.compute_eigendecomposition(k=5) mc.compute_terminal_states(use=2) orig_cats = list(mc._get(P.TERM).cat.categories) mc.rename_terminal_states({}) np.testing.assert_array_equal(mc._get(P.TERM).cat.categories, orig_cats)
def test_compute_lineage_drivers_invalid_lineages(self, adata_large: AnnData): vk = VelocityKernel(adata_large).compute_transition_matrix( softmax_scale=4) ck = ConnectivityKernel(adata_large).compute_transition_matrix() final_kernel = 0.8 * vk + 0.2 * ck mc = cr.tl.estimators.CFLARE(final_kernel) mc.compute_eigendecomposition(k=5) mc.compute_final_states(use=2) mc.compute_absorption_probabilities() with pytest.raises(KeyError): mc.compute_lineage_drivers(use_raw=False, lineages=["foo"])
def test_set_final_states_from_metastable_states_non_positive_cells( self, adata_large: AnnData): vk = VelocityKernel(adata_large).compute_transition_matrix( softmax_scale=4) ck = ConnectivityKernel(adata_large).compute_transition_matrix() final_kernel = 0.8 * vk + 0.2 * ck mc = cr.tl.estimators.GPCCA(final_kernel) mc.compute_schur(n_components=10, method="krylov") mc.compute_metastable_states(n_states=2, n_cells=None) with pytest.raises(ValueError): mc.set_final_states_from_metastable_states(n_cells=0)
def test_set_final_states_from_metastable_states_invalid_name( self, adata_large: AnnData): vk = VelocityKernel(adata_large).compute_transition_matrix( softmax_scale=4) ck = ConnectivityKernel(adata_large).compute_transition_matrix() final_kernel = 0.8 * vk + 0.2 * ck mc = cr.tl.estimators.GPCCA(final_kernel) mc.compute_schur(n_components=10, method="krylov") mc.compute_metastable_states(n_states=2) with pytest.raises(KeyError): mc.set_final_states_from_metastable_states(names=["foobar"])
def test_compute_lineage_drivers_normal_run(self, adata_large: AnnData): vk = VelocityKernel(adata_large).compute_transition_matrix( softmax_scale=4) ck = ConnectivityKernel(adata_large).compute_transition_matrix() final_kernel = 0.8 * vk + 0.2 * ck mc = cr.tl.estimators.CFLARE(final_kernel) mc.compute_eigendecomposition(k=5) mc.compute_final_states(use=2) mc.compute_absorption_probabilities() mc.compute_lineage_drivers(use_raw=False, cluster_key="clusters") for lineage in ["0", "1"]: assert f"{DirPrefix.FORWARD} {lineage}" in mc.adata.var.keys()
def test_set_terminal_states_from_macrostates_rename_not_unique_new_names( self, adata_large: AnnData): vk = VelocityKernel(adata_large).compute_transition_matrix( softmax_scale=4) ck = ConnectivityKernel(adata_large).compute_transition_matrix() terminal_kernel = 0.8 * vk + 0.2 * ck mc = cr.tl.estimators.GPCCA(terminal_kernel) mc.compute_schur(n_components=10, method="krylov") mc.compute_macrostates(n_states=3, n_cells=5) with pytest.raises(ValueError): mc.set_terminal_states_from_macrostates({"0": "1", "1": "1"}) _check_renaming_no_write_terminal(mc)
def test_compute_initial_states_from_forward_no_macro( self, adata_large: AnnData): vk = VelocityKernel( adata_large, backward=False).compute_transition_matrix(softmax_scale=4) ck = ConnectivityKernel(adata_large, backward=False).compute_transition_matrix() terminal_kernel = 0.8 * vk + 0.2 * ck mc = cr.tl.estimators.GPCCA(terminal_kernel) mc.compute_schur(n_components=10, method="krylov") with pytest.raises(RuntimeError): mc._compute_initial_states(1)
def test_combination_correct_parameters(self, adata: AnnData): from cellrank.tl.kernels import CosineScheme k = VelocityKernel(adata).compute_transition_matrix( softmax_scale=4, seed=42, scheme="cosine", ) + (ConnectivityKernel(adata).compute_transition_matrix( density_normalize=False) + ConnectivityKernel(adata).compute_transition_matrix( density_normalize=True)) k.compute_transition_matrix() assert isinstance(k.params, dict) assert len(k.params) == 3 assert {"dnorm": True} in k.params.values() assert {"dnorm": False} in k.params.values() assert { "softmax_scale": 4, "mode": "deterministic", "seed": 42, "scheme": str(CosineScheme()), } in k.params.values()
def test_plot_lineage_drivers_not_computed(self, adata_large: AnnData): vk = VelocityKernel(adata_large).compute_transition_matrix( softmax_scale=4) ck = ConnectivityKernel(adata_large).compute_transition_matrix() final_kernel = 0.8 * vk + 0.2 * ck mc = cr.tl.estimators.GPCCA(final_kernel) mc.compute_schur(n_components=10, method="krylov") mc.compute_metastable_states(n_states=2) mc.set_final_states_from_metastable_states() mc.compute_absorption_probabilities() with pytest.raises(RuntimeError): mc.plot_lineage_drivers("0")
def test_adding_hidden_constants(self, adata: AnnData): k = VelocityKernel(adata) + ConnectivityKernel(adata) assert _is_bin_mult(k[0]) assert isinstance(k[0], KernelMul) assert isinstance(k[0][0], Constant) assert isinstance(k[0][1], VelocityKernel) assert k[0][0].transition_matrix == 1.0 assert _is_bin_mult(k[1]) assert isinstance(k[1], KernelMul) assert isinstance(k[1][0], Constant) assert isinstance(k[1][1], ConnectivityKernel) assert k[1][0].transition_matrix == 1.0
def test_plot_lineage_drivers_normal_run(self, adata_large: AnnData): vk = VelocityKernel(adata_large).compute_transition_matrix( softmax_scale=4) ck = ConnectivityKernel(adata_large).compute_transition_matrix() final_kernel = 0.8 * vk + 0.2 * ck mc = cr.tl.estimators.GPCCA(final_kernel) mc.compute_schur(n_components=10, method="krylov") mc.compute_metastable_states(n_states=2) mc.set_final_states_from_metastable_states() mc.compute_absorption_probabilities() mc.compute_lineage_drivers(use_raw=False, cluster_key="clusters") mc.plot_lineage_drivers("0", use_raw=False)
def test_compute_final_states(self, adata_large: AnnData): vk = VelocityKernel(adata_large).compute_transition_matrix( softmax_scale=4) ck = ConnectivityKernel(adata_large).compute_transition_matrix() final_kernel = 0.8 * vk + 0.2 * ck mc = cr.tl.estimators.GPCCA(final_kernel) mc.compute_schur(n_components=10, method="krylov") mc.compute_metastable_states(n_states=2) mc.compute_final_states(n_cells=5) mc.compute_absorption_probabilities() _check_abs_probs(mc)
def compare_with_scanpy(self, adata: AnnData): # check whether cellrank's transition matrix matches scanpy's density_normalize = False ck = ConnectivityKernel(adata).compute_transition_matrix( density_normalize=density_normalize) T_cr = ck.transition_matrix neigh = Neighbors(adata) neigh.compute_transitions(density_normalize=density_normalize) T_sc = neigh.transitions # check whether these are the same while leaving them sparse assert T_sc.shape == T_cr.shape assert len(T_sc.indices) == len(T_cr.indices) assert np.allclose((T_cr - T_sc).data, 0)
def test_compute_terminal_states_n_main_states(self, adata_large: AnnData): vk = VelocityKernel(adata_large).compute_transition_matrix( softmax_scale=4) ck = ConnectivityKernel(adata_large).compute_transition_matrix() terminal_kernel = 0.8 * vk + 0.2 * ck mc = cr.tl.estimators.GPCCA(terminal_kernel) mc.compute_schur(n_components=10, method="krylov") mc.compute_macrostates(n_states=2) mc.compute_terminal_states(n_cells=5, method="top_n", n_states=1) mc.compute_absorption_probabilities() mc.compute_lineage_priming() _check_abs_probs(mc)
def test_plot_lineage_drivers_invalid_n_genes(self, adata_large: AnnData): vk = VelocityKernel(adata_large).compute_transition_matrix( softmax_scale=4) ck = ConnectivityKernel(adata_large).compute_transition_matrix() terminal_kernel = 0.8 * vk + 0.2 * ck mc = cr.tl.estimators.GPCCA(terminal_kernel) mc.compute_schur(n_components=10, method="krylov") mc.compute_macrostates(n_states=2) mc.set_terminal_states_from_macrostates() mc.compute_absorption_probabilities() mc.compute_lineage_drivers(use_raw=False, cluster_key="clusters") with pytest.raises(ValueError): mc.plot_lineage_drivers("0", use_raw=False, n_genes=0)
def test_compute_approx_normal_run(self, adata_large: AnnData): vk = VelocityKernel(adata_large).compute_transition_matrix(softmax_scale=4) ck = ConnectivityKernel(adata_large).compute_transition_matrix() terminal_kernel = 0.8 * vk + 0.2 * ck mc = cr.tl.estimators.CFLARE(terminal_kernel) mc.compute_eigendecomposition(k=5) mc.compute_terminal_states(use=2) assert is_categorical_dtype(mc._get(P.TERM)) assert mc._get(P.TERM_PROBS) is not None assert TermStatesKey.FORWARD.s in mc.adata.obs.keys() assert _probs(TermStatesKey.FORWARD) in mc.adata.obs.keys() assert _colors(TermStatesKey.FORWARD) in mc.adata.uns.keys()
def test_compute_metastable_states_min_chi_dict_wrong_keys( self, adata_large: AnnData): vk = VelocityKernel(adata_large).compute_transition_matrix( softmax_scale=4) ck = ConnectivityKernel(adata_large).compute_transition_matrix() final_kernel = 0.8 * vk + 0.2 * ck mc = cr.tl.estimators.GPCCA(final_kernel) mc.compute_schur(n_components=10, method="krylov") with pytest.raises(KeyError): mc.compute_metastable_states(n_states={ "foo": 2, "max": 3 }, use_min_chi=True)
def test_manual_combination_sam(self, adata: AnnData): mode = "sampling" vk = VelocityKernel(adata).compute_transition_matrix(mode=mode, softmax_scale=4) ck = ConnectivityKernel(adata).compute_transition_matrix() T_vk = vk.transition_matrix T_ck = ck.transition_matrix T_comb_manual = 0.8 * T_vk + 0.2 * T_ck comb_kernel = 0.8 * vk + 0.2 * ck T_comb_kernel = comb_kernel.transition_matrix np.testing.assert_allclose(T_comb_kernel.A, T_comb_manual.A, rtol=_rtol)