Esempio n. 1
0
    def test_normal_initialization(self, adata_cflare: AnnData):
        m = GAMR(adata_cflare)

        assert not m.prepared
        assert m._lineage is None
        assert m._gene is None
        assert m._offset is None
Esempio n. 2
0
def gamr_model(
    adata_gamr: AnnData, tmp_path_factory: Path, worker_id: str
) -> Optional[GAMR]:
    model = None

    if version_info[:2] <= (3, 6):
        pytest.skip("Pickling of Enums doesn't work in Python3.6.")
    elif worker_id == "master":
        model = _create_gamr_model(adata_gamr)
    else:
        root_tmp_dir = tmp_path_factory.getbasetemp().parent
        fn = root_tmp_dir / "model.pickle"

        with FileLock(f"{fn}.lock"):
            if fn.is_file():
                model = GAMR.read(fn)
            else:
                model = _create_gamr_model(adata_gamr)
                if model is not None:
                    model.write(fn)

    if model is None:
        pytest.skip("Unable to create `cellrank.ul.models.GAMR`.")

    return model
Esempio n. 3
0
    def test_negative_binomial_offset_automatic(self, adata_cflare: AnnData):
        assert _OFFSET_KEY not in adata_cflare.obs
        g = GAMR(adata_cflare, offset="default", distribution="nb")

        assert _OFFSET_KEY in adata_cflare.obs
        np.testing.assert_array_equal(adata_cflare.obs[_OFFSET_KEY].values, g._offset)
        assert g._offset.shape == (adata_cflare.n_obs,)
        assert "offset(offset)" in g._formula
Esempio n. 4
0
 def test_negative_binomial_invalid_offset_shape(self,
                                                 adata_cflare: AnnData):
     with pytest.raises(ValueError):
         GAMR(
             adata_cflare,
             offset=np.empty(adata_cflare.n_obs + 1, ),
             distribution="nb",
         )
Esempio n. 5
0
    def test_density_knotlocs(self, adata_cflare: AnnData):
        g = GAMR(adata_cflare, knotlocs="density")
        g.prepare(adata_cflare.var_names[0], "0", n_test_points=300).fit()
        g.predict(level=0.95)

        assert g.y_test.shape == (300,)
        assert g.conf_int.shape == (300, 2)
Esempio n. 6
0
def _create_gamr_model(_adata: AnnData) -> Optional[GAMR]:
    try:
        m = GAMR(_adata)
        m.prepare(_adata.var_names[0], "0").fit()
        m.predict(level=0.95)
        return m
    except:
        return None
Esempio n. 7
0
    def test_do_nothing_bulk_fit(self, gamr_model: GAMR):
        gamr_model._is_bulk = True
        fm = FailedModel(gamr_model)
        expected_dict = fm.__dict__.copy()

        for fn in [
            "prepare",
            "fit",
            "predict",
            "confidence_interval",
            "default_confidence_interval",
            "plot",
        ]:
            getattr(fm, fn)()

        assert expected_dict == fm.__dict__
Esempio n. 8
0
    def test_manually_call_conf_int_not_in_predict(self, adata_cflare: AnnData):
        g = GAMR(adata_cflare).prepare(adata_cflare.var_names[0], "1").fit()
        g.predict(level=None)
        assert g.conf_int is None

        ci_95 = g.confidence_interval(level=0.95)
        np.testing.assert_array_equal(g.conf_int, ci_95)

        ci_100 = g.confidence_interval(level=1)
        np.testing.assert_array_equal(g.conf_int, ci_100)

        assert not np.allclose(ci_95, ci_100)
Esempio n. 9
0
    def test_sharing_library(self, gamr_model: GAMR):
        actual = gamr_model.copy()

        assert actual._lib_name == gamr_model._lib_name
        assert actual._lib is gamr_model._lib
Esempio n. 10
0
    def test_negative_binomial_offset_ignored_if_not_nb(self, adata_cflare: AnnData):
        g = GAMR(adata_cflare, offset="default", distribution="gaussian")

        assert _OFFSET_KEY not in adata_cflare.obs
        assert g._offset is None
Esempio n. 11
0
 def test_negative_binomial_invalid_offset_str(self, adata_cflare: AnnData):
     with pytest.raises(ValueError):
         GAMR(adata_cflare, offset="foobar", distribution="nb")
Esempio n. 12
0
 def test_invalid_knotlocs(self, adata: AnnData):
     with pytest.raises(ValueError):
         _ = GAMR(adata, knotlocs="foobar")
Esempio n. 13
0
 def test_invalid_smoothing_penalty(self, adata: AnnData):
     with pytest.raises(ValueError):
         _ = GAMR(adata, smoothing_penalty=-0.001)
Esempio n. 14
0
 def test_invalid_n_knots(self, adata: AnnData):
     with pytest.raises(ValueError):
         _ = GAMR(adata, n_knots=0)