Ejemplo n.º 1
0
    def test_prepare_n_test_points(self, adata_cflare):
        model = create_model(adata_cflare)
        model = model.prepare(adata_cflare.var_names[0],
                              "0",
                              n_test_points=300)

        assert len(model.x_test) == 300
Ejemplo n.º 2
0
    def test_create_models_lineage_specific_fallback(self, adata: AnnData):
        m1 = create_model(adata)
        m2 = GAM(adata)

        models = _create_models(
            {"foo": {"baz": m1, "*": m2}, "bar": {"quux": m2, "*": m1}},
            ["foo", "bar"],
            ["baz", "quux", "quas", "wex"],
        )
        assert set(models.keys()) == {"foo", "bar"}

        for k, vs in models.items():
            assert set(vs.keys()) == {"baz", "quux", "quas", "wex"}

        assert isinstance(models["foo"]["baz"], type(m1))
        assert models["foo"]["baz"] is not m1

        for l in {"quux", "quas", "wex"}:
            assert isinstance(models["foo"][l], type(m2))
            assert models["foo"][l] is not m2

        assert isinstance(models["bar"]["quux"], type(m2))
        assert models["bar"]["quux"] is not m2

        for l in {"baz", "quas", "wex"}:
            assert isinstance(models["bar"][l], type(m1))
            assert models["bar"][l] is not m1
Ejemplo n.º 3
0
    def test_from_model_normal_run(self, adata_cflare: AnnData):
        m = create_model(adata_cflare).prepare(adata_cflare.var_names[0],
                                               "1").fit()
        m.predict()
        m.confidence_interval()

        fm = FittedModel.from_model(m)

        assert fm.prepared
        assert fm._gene == m._gene
        assert fm._lineage == m._lineage

        np.testing.assert_array_equal(fm.x_test, m.x_test)
        assert fm.x_test is not m.x_test
        np.testing.assert_array_equal(fm.y_test, m.y_test)
        assert fm.y_test is not m.y_test
        np.testing.assert_array_equal(fm.conf_int, m.conf_int)
        assert fm.conf_int is not m.conf_int

        np.testing.assert_array_equal(fm.x_all, m.x_all)
        assert fm.x_all is not m.x_all
        np.testing.assert_array_equal(fm.y_all, m.y_all)
        assert fm.y_all is not m.y_all
        np.testing.assert_array_equal(fm.w_all, m.w_all)
        assert fm.y_all is not m.w_all
Ejemplo n.º 4
0
 def test_invalid_n_perms(self, adata_cflare):
     model = create_model(adata_cflare)
     with pytest.raises(ValueError):
         _ = cr.tl.gene_importance(adata_cflare,
                                   model,
                                   adata_cflare.var_names[:10],
                                   "0",
                                   n_perms=-1)
Ejemplo n.º 5
0
    def test_create_models_1_model(self, adata: AnnData):
        m = create_model(adata)
        models = _create_models(m, ["foo"], ["bar"])

        assert set(models.keys()) == {"foo"}
        assert set(models["foo"].keys()) == {"bar"}
        assert isinstance(models["foo"]["bar"], type(m))
        assert models["foo"]["bar"] is not m
Ejemplo n.º 6
0
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").fit()
    m.predict()
    m.confidence_interval()

    return m
Ejemplo n.º 7
0
    def test_confidence_interval(self, adata_cflare):
        model = create_model(adata_cflare)
        model = model.prepare(adata_cflare.var_names[0], "0").fit()
        _ = model.predict()
        ci = model.confidence_interval()

        assert isinstance(model.conf_int, np.ndarray)
        assert len(model.y_test) == len(model.conf_int)
        assert ci is model.conf_int
Ejemplo n.º 8
0
    def test_perms_fdr_correction(self, adata_cflare):
        model = create_model(adata_cflare)
        res = cr.tl.gene_importance(adata_cflare,
                                    model,
                                    adata_cflare.var_names,
                                    "0",
                                    n_perms=10)

        assert "pval" in res.columns
        assert "qval" in res.columns
Ejemplo n.º 9
0
    def test_predict(self, adata_cflare):
        model = create_model(adata_cflare)
        model = model.prepare(adata_cflare.var_names[0], "0").fit()
        y_hat = model.predict()

        assert isinstance(model.y_test, np.ndarray)
        assert len(model.x_test) == len(model.y_test)
        assert y_hat is model.y_test

        assert model.conf_int is None
Ejemplo n.º 10
0
 def test_heatmap_lineages(self, adata: AnnData, fpath: str):
     model = create_model(adata)
     cr.pl.heatmap(
         adata,
         model,
         GENES[:10],
         kind="lineages",
         time_key="latent_time",
         dpi=DPI,
         save=fpath,
     )
Ejemplo n.º 11
0
    def test_perms_no_fdr_correction(self, adata_cr: AnnData):
        model = create_model(adata_cr)
        res = cr.tl.gene_importance(adata_cr,
                                    model,
                                    adata_cr.var_names,
                                    "0",
                                    n_perms=10,
                                    fdr_correction=None)

        assert "pval" in res.columns
        assert "qval" not in res.columns
Ejemplo n.º 12
0
 def test_cluster_lineage(self, adata: AnnData, fpath: Path):
     model = create_model(adata)
     cr.pl.cluster_lineage(
         adata,
         model,
         adata.var_names[:10],
         "0",
         time_key="latent_time",
         dpi=DPI,
         save=fpath,
     )
Ejemplo n.º 13
0
    def test_prepare_normal_run(self, adata_cflare):
        model = create_model(adata_cflare)
        model = model.prepare(adata_cflare.var_names[0], "0")

        assert isinstance(model.x, np.ndarray)
        assert isinstance(model.w, np.ndarray)
        assert isinstance(model.y, np.ndarray)
        assert isinstance(model.x_test, np.ndarray)
        assert len(model.x_test) == 200
        assert model.y_test is None
        assert model.conf_int is None
Ejemplo n.º 14
0
 def test_trends(self, adata: AnnData, fpath: Path):
     model = create_model(adata)
     cr.pl.gene_trends(
         adata,
         model,
         adata.var_names[:3],
         data_key="Ms",
         dirname="trends_simple",
         dpi=DPI,
         save=fpath,
     )
Ejemplo n.º 15
0
 def test_trends_same_plot(self, adata: AnnData, fpath: Path):
     model = create_model(adata)
     cr.pl.gene_trends(
         adata,
         model,
         adata.var_names[:3],
         data_key="Ms",
         same_plot=True,
         dpi=DPI,
         save=fpath,
     )
Ejemplo n.º 16
0
 def test_heatmap_genes(self, adata: AnnData, fpath: Path):
     model = create_model(adata)
     cr.pl.heatmap(
         adata,
         model,
         adata.var_names[:10],
         kind="genes",
         time_key="latent_time",
         dpi=DPI,
         save=fpath,
     )
Ejemplo n.º 17
0
 def test_trends_lineage_cmap(self, adata: AnnData, fpath: str):
     model = create_model(adata)
     cr.pl.gene_trends(
         adata,
         model,
         GENES[0],
         data_key="Ms",
         same_plot=True,
         lineage_cmap=cm.Set2,
         dpi=DPI,
         save=fpath,
     )
Ejemplo n.º 18
0
 def test_trends_lineage_cell_color(self, adata: AnnData, fpath: str):
     model = create_model(adata)
     cr.pl.gene_trends(
         adata,
         model,
         GENES[0],
         data_key="Ms",
         same_plot=True,
         cell_color="red",
         dpi=DPI,
         save=fpath,
     )
Ejemplo n.º 19
0
 def test_trends_margins(self, adata: AnnData, fpath: str):
     model = create_model(adata)
     cr.pl.gene_trends(
         adata,
         model,
         GENES[0],
         data_key="Ms",
         same_plot=True,
         margins=0.2,
         dpi=DPI,
         save=fpath,
     )
Ejemplo n.º 20
0
 def test_heatmap_cmap(self, adata: AnnData, fpath: str):
     model = create_model(adata)
     cr.pl.heatmap(
         adata,
         model,
         GENES[:5],
         kind="genes",
         time_key="latent_time",
         cmap=cm.viridis,
         dpi=DPI,
         save=fpath,
     )
Ejemplo n.º 21
0
 def test_trends_lineage_alpha(self, adata: AnnData, fpath: Path):
     model = create_model(adata)
     cr.pl.gene_trends(
         adata,
         model,
         adata.var_names[0],
         data_key="Ms",
         same_plot=True,
         lineage_alpha=1,
         dpi=DPI,
         save=fpath,
     )
Ejemplo n.º 22
0
 def test_cluster_lineage_no_norm(self, adata: AnnData, fpath: str):
     model = create_model(adata)
     cr.pl.cluster_lineage(
         adata,
         model,
         GENES[:10],
         "0",
         time_key="latent_time",
         norm=False,
         dpi=DPI,
         save=fpath,
     )
Ejemplo n.º 23
0
 def test_heatmap_lineage_height(self, adata: AnnData, fpath: Path):
     model = create_model(adata)
     cr.pl.heatmap(
         adata,
         model,
         adata.var_names[:10],
         kind="lineages",
         time_key="latent_time",
         lineage_height=0.2,
         dpi=DPI,
         save=fpath,
     )
Ejemplo n.º 24
0
 def test_trends_cbar(self, adata: AnnData, fpath: str):
     model = create_model(adata)
     cr.pl.gene_trends(
         adata,
         model,
         GENES[0],
         data_key="Ms",
         same_plot=True,
         show_cbar=False,
         dpi=DPI,
         save=fpath,
     )
Ejemplo n.º 25
0
 def test_trends_cell_alpha(self, adata: AnnData, fpath: str):
     model = create_model(adata)
     cr.pl.gene_trends(
         adata,
         model,
         GENES[0],
         data_key="Ms",
         same_plot=True,
         cell_alpha=0,
         dpi=DPI,
         save=fpath,
     )
Ejemplo n.º 26
0
 def test_trends_sharey(self, adata: AnnData, fpath: str):
     model = create_model(adata)
     cr.pl.gene_trends(
         adata,
         model,
         GENES[:3],
         data_key="Ms",
         sharey=False,
         dirname="trends_sharey",
         dpi=DPI,
         save=fpath,
     )
Ejemplo n.º 27
0
    def test_no_perms(self, adata_cflare):
        model = create_model(adata_cflare)
        res = cr.tl.gene_importance(adata_cflare,
                                    model,
                                    adata_cflare.var_names[:10],
                                    "0",
                                    n_perms=None)

        assert isinstance(res, pd.DataFrame)
        assert res.shape == (10, 1)
        assert set(res.index) == set(adata_cflare.var_names[:10])
        assert "importance" in res.columns
Ejemplo n.º 28
0
    def test_create_models_lineage_specific(self, adata: AnnData):
        m1 = create_model(adata)
        m2 = GAM(adata)

        models = _create_models(
            {"foo": {"bar": m1, "baz": m2}}, ["foo"], ["bar", "baz"]
        )
        assert set(models["foo"].keys()) == {"bar", "baz"}
        assert isinstance(models["foo"]["bar"], type(m1))
        assert models["foo"]["bar"] is not m1

        assert isinstance(models["foo"]["baz"], type(m2))
        assert models["foo"]["baz"] is not m2
Ejemplo n.º 29
0
 def test_heatmap_start_end_clusters(self, adata: AnnData, fpath: Path):
     model = create_model(adata)
     cr.pl.heatmap(
         adata,
         model,
         adata.var_names[:10],
         kind="lineages",
         time_key="latent_time",
         start_lineage="0",
         end_lineage="1",
         dpi=DPI,
         save=fpath,
     )
Ejemplo n.º 30
0
    def test_model_1_lineage(self, adata_cflare):
        adata_cflare.obsm[AbsProbKey.FORWARD.s] = Lineage(np.ones(
            (adata_cflare.n_obs, 1)),
                                                          names=["foo"])
        model = create_model(adata_cflare)
        model = model.prepare(adata_cflare.var_names[0],
                              "foo",
                              n_test_points=100).fit()
        _ = model.predict()

        assert model.x_test.shape == (100, 1)
        xtest, xall = model.x_test, model.x_all
        np.testing.assert_allclose(np.r_[xtest[0], xtest[-1]],
                                   np.r_[np.min(xall),
                                         np.max(xall)])