コード例 #1
0
ファイル: test_gpcca.py プロジェクト: theislab/cellrank
    def test_compute_priming_clusters(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()

        cat = adata_large.obs["clusters"].cat.categories[0]
        deg1 = mc.compute_lineage_priming(method="kl_divergence",
                                          early_cells={"clusters": [cat]})
        deg2 = mc.compute_lineage_priming(
            method="kl_divergence",
            early_cells=(adata_large.obs["clusters"] == cat).values,
        )

        assert_series_equal(deg1, deg2)
        # because passing it to a dataframe changes its name
        assert_series_equal(adata_large.obs[_pd(mc._abs_prob_key)],
                            deg1,
                            check_names=False)
        assert_series_equal(mc._get(A.PRIME_DEG), deg1)
コード例 #2
0
ファイル: test_gpcca.py プロジェクト: theislab/cellrank
def _check_abs_probs(mc: cr.tl.estimators.GPCCA, has_main_states: bool = True):
    if has_main_states:
        assert isinstance(mc._get(P.TERM), pd.Series)
        assert_array_nan_equal(mc.adata.obs[str(TermStatesKey.FORWARD)],
                               mc._get(P.TERM))
        np.testing.assert_array_equal(
            mc.adata.uns[_colors(TermStatesKey.FORWARD)],
            mc._get(A.TERM_ABS_PROBS)[list(mc._get(
                P.TERM).cat.categories)].colors,
        )

    assert isinstance(mc._get(P.PRIME_DEG), pd.Series)
    assert isinstance(mc._get(P.ABS_PROBS), cr.tl.Lineage)
    np.testing.assert_array_almost_equal(mc._get(P.ABS_PROBS).sum(1), 1.0)

    np.testing.assert_array_equal(mc.adata.obsm[str(AbsProbKey.FORWARD)],
                                  mc._get(P.ABS_PROBS).X)
    np.testing.assert_array_equal(mc.adata.uns[_lin_names(AbsProbKey.FORWARD)],
                                  mc._get(P.ABS_PROBS).names)
    np.testing.assert_array_equal(mc.adata.uns[_colors(AbsProbKey.FORWARD)],
                                  mc._get(P.ABS_PROBS).colors)

    np.testing.assert_array_equal(mc.adata.obs[_pd(AbsProbKey.FORWARD)],
                                  mc._get(P.PRIME_DEG))

    assert_array_nan_equal(mc.adata.obs[TermStatesKey.FORWARD.s],
                           mc._get(P.TERM))
    np.testing.assert_array_equal(mc.adata.obs[_probs(TermStatesKey.FORWARD)],
                                  mc._get(P.TERM_PROBS))
コード例 #3
0
    def _read_from_adata(self) -> None:
        self._set_or_debug(f"eig_{self._direction}", self.adata.uns, "_eig")

        self._set_or_debug(self._g2m_key, self.adata.obs, "_G2M_score")
        self._set_or_debug(self._s_key, self.adata.obs, "_S_score")

        self._set_or_debug(self._term_key, self.adata.obs, A.TERM.s)
        self._set_or_debug(_probs(self._term_key), self.adata.obs, A.TERM_PROBS)
        self._set_or_debug(_colors(self._term_key), self.adata.uns, A.TERM_COLORS)

        self._reconstruct_lineage(A.ABS_PROBS, self._abs_prob_key)
        self._set_or_debug(_pd(self._abs_prob_key), self.adata.obs, A.PRIME_DEG)
コード例 #4
0
    def compute_lineage_priming(
        self,
        method: Literal["kl_divergence", "entropy"] = "kl_divergence",
        early_cells: Optional[Union[Mapping[str, Sequence[str]], Sequence[str]]] = None,
    ) -> pd.Series:
        """
        %(lin_pd.full_desc)s

        Parameters
        ----------
        %(lin_pd.parameters)s
            Cell ids or a mask marking early cells. If `None`, use all cells. Only used when ``method='kl_divergence'``.
            If a :class:`dict`, the key species a cluster key in :attr:`anndata.AnnData.obs` and the values
            specify cluster labels containing early cells.

        Returns
        -------
        %(lin_pd.returns)s
        """  # noqa: D400
        abs_probs: Optional[Lineage] = self._get(P.ABS_PROBS)
        if abs_probs is None:
            raise RuntimeError(
                "Compute absorption probabilities first as `.compute_absorption_probabilities()`."
            )
        if isinstance(early_cells, dict):
            if len(early_cells) != 1:
                raise ValueError(
                    f"Expected a dictionary with only 1 key, found `{len(early_cells)}`."
                )
            key = next(iter(early_cells.keys()))
            if key not in self.adata.obs:
                raise KeyError(f"Unable to find clustering in `adata.obs[{key!r}]`.")
            early_cells = self.adata.obs[key].isin(early_cells[key])
        elif early_cells is not None:
            early_cells = np.asarray(early_cells)
            if not np.issubdtype(early_cells.dtype, np.bool_):
                early_cells = np.isin(self.adata.obs_names, early_cells)

        values = pd.Series(
            abs_probs.priming_degree(method, early_cells), index=self.adata.obs_names
        )

        self._set(A.PRIME_DEG, values)
        self.adata.obs[_pd(self._abs_prob_key)] = values

        logg.info(
            f"Adding `adata.obs[{_pd(self._abs_prob_key)!r}]`\n"
            f"       `.{P.PRIME_DEG}`"
        )

        return values
コード例 #5
0
    def test_compute_absorption_probabilities_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.compute_absorption_probabilities()
        mc.compute_lineage_priming()

        assert isinstance(mc._get(P.PRIME_DEG), pd.Series)
        assert _pd(AbsProbKey.FORWARD) in mc.adata.obs.keys()
        np.testing.assert_array_equal(
            mc._get(P.PRIME_DEG), mc.adata.obs[_pd(AbsProbKey.FORWARD)]
        )

        assert isinstance(mc._get(P.ABS_PROBS), cr.tl.Lineage)
        assert mc._get(P.ABS_PROBS).shape == (mc.adata.n_obs, 2)
        assert f"{AbsProbKey.FORWARD}" in mc.adata.obsm.keys()
        np.testing.assert_array_equal(
            mc._get(P.ABS_PROBS).X, mc.adata.obsm[f"{AbsProbKey.FORWARD}"]
        )

        assert _lin_names(AbsProbKey.FORWARD) in mc.adata.uns.keys()
        np.testing.assert_array_equal(
            mc._get(P.ABS_PROBS).names,
            mc.adata.uns[_lin_names(AbsProbKey.FORWARD)],
        )

        assert _colors(AbsProbKey.FORWARD) in mc.adata.uns.keys()
        np.testing.assert_array_equal(
            mc._get(P.ABS_PROBS).colors,
            mc.adata.uns[_colors(AbsProbKey.FORWARD)],
        )
        np.testing.assert_allclose(mc._get(P.ABS_PROBS).X.sum(1), 1, rtol=1e-6)