Пример #1
0
def _create_cflare(n_obs: int,
                   *,
                   backward: bool = False) -> Tuple[AnnData, CFLARE]:
    adata = _create_dummy_adata(n_obs)
    sc.tl.paga(adata, groups="clusters")
    try:
        vk = VelocityKernel(adata,
                            backward=backward).compute_transition_matrix()
        ck = ConnectivityKernel(adata,
                                backward=backward).compute_transition_matrix()
        final_kernel = 0.8 * vk + 0.2 * ck

        mc = cr.tl.CFLARE(final_kernel)

        mc.compute_partition()
        mc.compute_eig()
        mc.compute_metastable_states(use=2)  # can fail for small #cells
        mc.compute_lin_probs()
        mc.compute_lineage_drivers(cluster_key="clusters", use_raw=False)

        assert adata is mc.adata
        if backward:
            assert str(LinKey.BACKWARD) in adata.obsm
        else:
            assert str(LinKey.FORWARD) in adata.obsm
    except:
        mc = None

    return adata, mc
Пример #2
0
    def test_backward_manual_dense_norm(self, adata):
        backward = True
        vk = VelocityKernel(adata, backward=backward).compute_transition_matrix(
            density_normalize=False
        )
        ck = ConnectivityKernel(adata, backward=backward).compute_transition_matrix(
            density_normalize=False
        )

        # combine the kernels
        comb = 0.8 * vk + 0.2 * ck
        T_1 = comb.transition_matrix
        conn = get_neighs(adata, "connectivities")
        T_1 = density_normalization(T_1, conn)
        T_1 = _normalize(T_1)

        transition_matrix(
            adata,
            diff_kernel="sum",
            weight_diffusion=0.2,
            density_normalize=True,
            backward=backward,
        )
        T_2 = adata.uns[_transition(Direction.BACKWARD)]["T"]

        np.testing.assert_allclose(T_1.A, T_2.A, rtol=_rtol)
Пример #3
0
    def test_inversion_propagation(self, adata):
        c = ConnectivityKernel(adata, backward=False)
        v = VelocityKernel(adata, backward=False)
        k = ~(c + v)

        assert c.backward
        assert v.backward
        assert k.backward
Пример #4
0
    def test_initialized(self, adata):
        k = (
            VelocityKernel(adata).compute_transition_matrix()
            + ConnectivityKernel(adata).compute_transition_matrix()
        )
        k.compute_transition_matrix()

        assert k.transition_matrix is not None
Пример #5
0
    def test_constant_normalize_2(self, adata):
        k = (9 * VelocityKernel(adata).compute_transition_matrix() +
             1 * ConnectivityKernel(adata).compute_transition_matrix())
        k.compute_transition_matrix()
        c1, c2 = _is_bin_mult(k[0]), _is_bin_mult(k[1])

        assert c1.transition_matrix == 9 / 10
        assert c2.transition_matrix == 1 / 10
Пример #6
0
def create_kernels(
    adata: AnnData,
    var_key_connectivities: str = "connectivity_variances",
    var_key_velocities: str = "velocity_variances",
) -> Tuple[VelocityKernel, ConnectivityKernel]:
    vk = VelocityKernel(adata, var_key=var_key_velocities)
    ck = ConnectivityKernel(adata, var_key=var_key_connectivities)
    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
Пример #7
0
    def test_compute_schur_invalid_eig_sort(self, adata_large: AnnData):
        vk = VelocityKernel(adata_large).compute_transition_matrix()
        ck = ConnectivityKernel(adata_large).compute_transition_matrix()
        final_kernel = 0.8 * vk + 0.2 * ck

        mc = cr.tl.GPCCA(final_kernel)
        with pytest.raises(ValueError):
            mc.compute_schur(which="foobar", method="brandts")
Пример #8
0
    def test_compute_lin_probs_no_arcs(self, adata_large: AnnData):
        vk = VelocityKernel(adata_large).compute_transition_matrix()
        ck = ConnectivityKernel(adata_large).compute_transition_matrix()
        final_kernel = 0.8 * vk + 0.2 * ck

        mc = cr.tl.CFLARE(final_kernel)
        with pytest.raises(RuntimeError):
            mc.compute_lin_probs()
Пример #9
0
    def test_uninitialized_one(self, adata):
        k = (
            VelocityKernel(adata)
            + ConnectivityKernel(adata).compute_transition_matrix()
        )

        with pytest.raises(RuntimeError):
            k.compute_transition_matrix()
Пример #10
0
    def test_backward_negate_differ(self, adata):
        backward = True
        backward_mode = "negate"
        vk = VelocityKernel(adata, backward=backward)

        vk.compute_transition_matrix(density_normalize=True,
                                     backward_mode=backward_mode)
        T_1 = vk.transition_matrix
        transition_matrix(
            adata,
            density_normalize=False,
            backward=backward,
            backward_mode=backward_mode,
        )
        T_2 = adata.uns["T_bwd"]["T"]

        assert not np.allclose(T_1.A, T_2.A, rtol=_rtol)
Пример #11
0
    def test_not_none_transition_matrix_accessor(self, adata):
        vk = VelocityKernel(adata)
        ck = ConnectivityKernel(adata)
        pk = PalantirKernel(adata, time_key="latent_time")

        assert vk.transition_matrix is not None
        assert ck.transition_matrix is not None
        assert pk.transition_matrix is not None
Пример #12
0
    def test_compute_metastable_states_no_eig(self, adata_large: AnnData):
        vk = VelocityKernel(adata_large).compute_transition_matrix()
        ck = ConnectivityKernel(adata_large).compute_transition_matrix()
        final_kernel = 0.8 * vk + 0.2 * ck

        mc = cr.tl.GPCCA(final_kernel)
        with pytest.raises(RuntimeError):
            mc.compute_metastable_states(n_states=None)
Пример #13
0
    def test_addition_3_kernels(self, adata):
        vk, ck = create_kernels(adata)  # diagonal + upper diag
        vk1 = VelocityKernel(adata)
        vk1._transition_matrix = np.eye(adata.n_obs, k=-1) / 2 + np.eye(
            adata.n_obs) / 2
        vk1._transition_matrix[0, 0] = 1
        np.testing.assert_allclose(np.sum(ck._transition_matrix, axis=1),
                                   1)  # sanity check

        k = (vk + ck + vk1).compute_transition_matrix()
        expected = (np.eye(adata.n_obs) * (1 / 3 + 1 / 6 + 1 / 6) +
                    np.eye(adata._n_obs, k=1) * 1 / 6 +
                    np.eye(adata.n_obs, k=-1) * 1 / 6)
        expected[0, 0] = expected[-1, -1] = 2 / 3 + 1 / 3 * 0.5
        expected[0, 1] = expected[-1, -2] = 1 - expected[0, 0]

        np.testing.assert_allclose(k.transition_matrix.A, expected)
Пример #14
0
    def test_parent(self, adata):
        vk = VelocityKernel(adata)
        ck = ConnectivityKernel(adata)
        k = vk + ck

        assert vk._parent._parent is k  # invisible constants
        assert ck._parent._parent is k
        assert k._parent is None
Пример #15
0
    def test_compute_approx_rcs_no_eig(self, adata_large: AnnData):
        vk = VelocityKernel(adata_large).compute_transition_matrix()
        ck = ConnectivityKernel(adata_large).compute_transition_matrix()
        final_kernel = 0.8 * vk + 0.2 * ck

        mc = cr.tl.MarkovChain(final_kernel)
        with pytest.raises(RuntimeError):
            mc.compute_approx_rcs(use=2)
Пример #16
0
    def test_compute_schur_write_eigvals(self, adata_large: AnnData):
        vk = VelocityKernel(adata_large).compute_transition_matrix()
        ck = ConnectivityKernel(adata_large).compute_transition_matrix()
        final_kernel = 0.8 * vk + 0.2 * ck

        mc = cr.tl.GPCCA(final_kernel)
        mc.compute_schur(n_components=10, method="brandts")

        _check_eigdecomposition(mc)
Пример #17
0
    def test_backward_negate_dense_norm(self, adata):
        backward = True
        dense_norm = True
        backward_mode = "negate"
        vk = VelocityKernel(adata, backward=backward)

        vk.compute_transition_matrix(density_normalize=dense_norm,
                                     backward_mode=backward_mode)
        T_1 = vk.transition_matrix
        transition_matrix(
            adata,
            density_normalize=dense_norm,
            backward=backward,
            backward_mode=backward_mode,
        )
        T_2 = adata.uns["T_bwd"]["T"]

        np.testing.assert_allclose(T_1.A, T_2.A, rtol=_rtol)
Пример #18
0
    def test_compute_gdpt_invalid_n_comps(self, adata_large: AnnData):
        vk = VelocityKernel(adata_large).compute_transition_matrix()
        ck = ConnectivityKernel(adata_large).compute_transition_matrix()
        final_kernel = 0.8 * vk + 0.2 * ck

        mc = cr.tl.GPCCA(final_kernel)

        with pytest.raises(ValueError):
            mc.compute_gdpt(n_components=1)
Пример #19
0
    def test_compute_metastable_invalid_cluster_key(self, adata_large: AnnData):
        vk = VelocityKernel(adata_large).compute_transition_matrix()
        ck = ConnectivityKernel(adata_large).compute_transition_matrix()
        final_kernel = 0.8 * vk + 0.2 * ck

        mc = cr.tl.GPCCA(final_kernel)
        mc.compute_schur(n_components=10, method="brandts")
        with pytest.raises(KeyError):
            mc.compute_metastable_states(n_states=2, cluster_key="foobar")
Пример #20
0
    def test_constant_normalize_3(self, adata):
        k = (VelocityKernel(adata).compute_transition_matrix() +
             ConnectivityKernel(adata).compute_transition_matrix() +
             ConnectivityKernel(adata).compute_transition_matrix())
        k.compute_transition_matrix()
        c1, c2, c3 = _is_bin_mult(k[0]), _is_bin_mult(k[1]), _is_bin_mult(k[2])

        assert c1.transition_matrix == 1 / 3
        assert c2.transition_matrix == 1 / 3
        assert c3.transition_matrix == 1 / 3
Пример #21
0
    def test_compute_gdpt_no_iroot(self, adata_large: AnnData):
        vk = VelocityKernel(adata_large).compute_transition_matrix()
        ck = ConnectivityKernel(adata_large).compute_transition_matrix()
        final_kernel = 0.8 * vk + 0.2 * ck

        mc = cr.tl.GPCCA(final_kernel)
        mc.adata.uns.pop("iroot", None)

        with pytest.raises(KeyError):
            mc.compute_gdpt()
Пример #22
0
    def test_compute_gdpt_no_schur(self, adata_large: AnnData):
        vk = VelocityKernel(adata_large).compute_transition_matrix()
        ck = ConnectivityKernel(adata_large).compute_transition_matrix()
        final_kernel = 0.8 * vk + 0.2 * ck

        mc = cr.tl.GPCCA(final_kernel)

        mc.compute_gdpt(method="brandts")

        assert "gdpt_pseudotime" in mc.adata.obs
Пример #23
0
    def test_compute_metastable_states_schur(self, adata_large: AnnData):
        vk = VelocityKernel(adata_large).compute_transition_matrix()
        ck = ConnectivityKernel(adata_large).compute_transition_matrix()
        final_kernel = 0.8 * vk + 0.2 * ck

        mc = cr.tl.GPCCA(final_kernel)
        mc.compute_schur(n_components=10, method="brandts")
        mc.compute_metastable_states(n_states=2)

        _check_compute_meta(mc)
Пример #24
0
    def test_compute_metastable_none_states(self, adata_large: AnnData):
        vk = VelocityKernel(adata_large).compute_transition_matrix()
        ck = ConnectivityKernel(adata_large).compute_transition_matrix()
        final_kernel = 0.8 * vk + 0.2 * ck

        mc = cr.tl.GPCCA(final_kernel)
        mc.compute_eig()
        mc.compute_metastable_states(n_states=None)

        _check_compute_meta(mc)
Пример #25
0
    def test_comp_cond_num_or_policy(self, adata):
        vk = VelocityKernel(adata,
                            compute_cond_num=True).compute_transition_matrix()
        ck = ConnectivityKernel(
            adata, compute_cond_num=False).compute_transition_matrix()
        v = (vk + ck).compute_transition_matrix()

        assert isinstance(vk.condition_number, float)
        assert ck.condition_number is None
        assert isinstance(v.condition_number, float)
Пример #26
0
    def test_compute_lineage_drivers_no_lineages(self, adata_large: AnnData):
        vk = VelocityKernel(adata_large).compute_transition_matrix()
        ck = ConnectivityKernel(adata_large).compute_transition_matrix()
        final_kernel = 0.8 * vk + 0.2 * ck

        mc = cr.tl.CFLARE(final_kernel)
        mc.compute_eig(k=5)
        mc.compute_metastable_states(use=2)
        with pytest.raises(RuntimeError):
            mc.compute_lineage_drivers()
Пример #27
0
    def test_compute_metastable_states_too_large_use(self,
                                                     adata_large: AnnData):
        vk = VelocityKernel(adata_large).compute_transition_matrix()
        ck = ConnectivityKernel(adata_large).compute_transition_matrix()
        final_kernel = 0.8 * vk + 0.2 * ck

        mc = cr.tl.CFLARE(final_kernel)
        mc.compute_eig(k=2)
        with pytest.raises(ValueError):
            mc.compute_metastable_states(use=1000)
Пример #28
0
    def foo(self, adata):
        vk = VelocityKernel(adata).compute_transition_matrix()
        ck = ConnectivityKernel(adata).compute_transition_matrix()
        final_kernel = 0.8 * vk + 0.2 * ck

        mc_fwd = cr.tl.MarkovChain(final_kernel)

        mc_fwd.compute_lin_probs()

        mc_fwd.compute_lineage_drivers(cluster_key="clusters", use_raw=False)
Пример #29
0
    def test_compute_lineage_drivers_no_lineages(self, adata_large: AnnData):
        vk = VelocityKernel(adata_large).compute_transition_matrix()
        ck = ConnectivityKernel(adata_large).compute_transition_matrix()
        final_kernel = 0.8 * vk + 0.2 * ck

        mc = cr.tl.GPCCA(final_kernel)
        mc.compute_schur(n_components=10, method="brandts")
        mc.compute_metastable_states(n_states=2)

        with pytest.raises(RuntimeError):
            mc.compute_lineage_drivers()
Пример #30
0
    def test_compute_gdpt_cellname_iroot(self, adata_large: AnnData):
        vk = VelocityKernel(adata_large).compute_transition_matrix()
        ck = ConnectivityKernel(adata_large).compute_transition_matrix()
        final_kernel = 0.8 * vk + 0.2 * ck

        mc = cr.tl.GPCCA(final_kernel)
        mc.adata.uns["iroot"] = mc.adata.obs_names[0]

        mc.compute_gdpt()

        assert "gdpt_pseudotime" in mc.adata.obs