Example #1
0
    def test_reproducibility(self, adata: AnnData, n_jobs: int):
        spatial_neighbors(adata)

        res1 = nhood_enrichment(adata,
                                cluster_key=_CK,
                                seed=42,
                                n_jobs=n_jobs,
                                n_perms=20,
                                copy=True)
        res2 = nhood_enrichment(adata,
                                cluster_key=_CK,
                                seed=42,
                                n_jobs=n_jobs,
                                n_perms=20,
                                copy=True)
        res3 = nhood_enrichment(adata,
                                cluster_key=_CK,
                                seed=43,
                                n_jobs=n_jobs,
                                n_perms=20,
                                copy=True)

        assert len(res1) == len(res2)
        assert len(res2) == len(res3)

        for key in range(len(res1)):
            np.testing.assert_array_equal(res2[key], res1[key])
            if key == 0:  # z-score
                with pytest.raises(AssertionError):
                    np.testing.assert_array_equal(res3[key], res2[key])
            else:  # counts
                np.testing.assert_array_equal(res3[key], res2[key])
Example #2
0
def test_spatial_neighbors_non_visium(non_visium_adata: AnnData):
    """
    check correctness of neighborhoods for non-visium coordinates
    """
    correct_knn_graph = np.array([
        [0.0, 1.0, 1.0, 1.0],
        [1.0, 0.0, 1.0, 1.0],
        [1.0, 1.0, 0.0, 1.0],
        [1.0, 1.0, 1.0, 0.0],
    ])

    correct_radius_graph = np.array([
        [0.0, 1.0, 0.0, 1.0],
        [1.0, 0.0, 0.0, 1.0],
        [0.0, 0.0, 0.0, 0.0],
        [1.0, 1.0, 0.0, 0.0],
    ])

    spatial_neighbors(non_visium_adata, n_neigh=3, coord_type=None)
    spatial_graph = non_visium_adata.obsp[Key.obsp.spatial_conn()].toarray()
    assert np.array_equal(spatial_graph, correct_knn_graph)

    spatial_neighbors(non_visium_adata, radius=5.0, coord_type=None)
    spatial_graph = non_visium_adata.obsp[Key.obsp.spatial_conn()].toarray()
    assert np.array_equal(spatial_graph, correct_radius_graph)
Example #3
0
    def test_plot_nhood_enrichment_dendro(self, adata: AnnData):
        gr.spatial_neighbors(adata)
        gr.nhood_enrichment(adata, cluster_key=C_KEY)

        # use count to avoid nan for scipy.cluster.hierarchy
        pl.nhood_enrichment(adata,
                            cluster_key=C_KEY,
                            mode="count",
                            method="single")
Example #4
0
    def test_plot_centrality_scores_single(self, adata: AnnData):
        selected_score = "degree_centrality"
        gr.spatial_neighbors(adata)
        gr.centrality_scores(adata, cluster_key=C_KEY)

        pl.centrality_scores(adata,
                             cluster_key=C_KEY,
                             score=selected_score,
                             dpi=DPI)
Example #5
0
    def test_parallel_works(self, adata: AnnData, backend: str):
        spatial_neighbors(adata)

        nhood_enrichment(adata,
                         cluster_key=_CK,
                         n_jobs=2,
                         n_perms=20,
                         backend=backend)

        self._assert_common(adata)
Example #6
0
    def test_plot_cbar_kwargs(self, adata: AnnData):
        gr.spatial_neighbors(adata)
        gr.nhood_enrichment(adata, cluster_key=C_KEY)

        pl.nhood_enrichment(adata,
                            cluster_key=C_KEY,
                            cbar_kwargs={
                                "label": "FOOBARBAZQUUX",
                                "filled": False
                            })
Example #7
0
def test_spatial_neighbors_visium(visium_adata: AnnData, n_rings: int,
                                  n_neigh: int, sum_dist: int):
    """
    check correctness of neighborhoods for visium coordinates
    """
    spatial_neighbors(visium_adata, n_rings=n_rings)
    assert visium_adata.obsp[Key.obsp.spatial_conn()][0].sum() == n_neigh
    assert visium_adata.uns[
        Key.uns.spatial_neighs()]["distances_key"] == Key.obsp.spatial_dist()
    if n_rings > 1:
        assert visium_adata.obsp[Key.obsp.spatial_dist()][0].sum() == sum_dist
Example #8
0
def test_spatial_neighbors_non_visium(non_visium_adata: AnnData):
    """
    check correctness of neighborhoods for non-visium coordinates
    """
    correct_knn_graph = np.array([
        [0.0, 1.0, 1.0, 1.0],
        [1.0, 0.0, 1.0, 1.0],
        [1.0, 1.0, 0.0, 1.0],
        [1.0, 1.0, 1.0, 0.0],
    ])

    correct_radius_graph = np.array([
        [0.0, 1.0, 0.0, 1.0],
        [1.0, 0.0, 0.0, 1.0],
        [0.0, 0.0, 0.0, 0.0],
        [1.0, 1.0, 0.0, 0.0],
    ])

    correct_delaunay_graph = np.array([[0.0, 1.0, 0.0, 1.0],
                                       [1.0, 0.0, 1.0, 1.0],
                                       [0.0, 1.0, 0.0, 1.0],
                                       [1.0, 1.0, 1.0, 0.0]])

    correct_delaunay_dist = np.array([
        [0.0, 2.0, 0.0, 4.12310563],
        [2.0, 0.0, 6.32455532, 5.0],
        [0.0, 6.32455532, 0.0, 5.38516481],
        [4.12310563, 5.0, 5.38516481, 0.0],
    ])

    spatial_neighbors(non_visium_adata, n_neigh=3, coord_type=None)
    spatial_graph = non_visium_adata.obsp[Key.obsp.spatial_conn()].toarray()
    assert np.array_equal(spatial_graph, correct_knn_graph)

    spatial_neighbors(non_visium_adata, radius=5.0, coord_type=None)
    spatial_graph = non_visium_adata.obsp[Key.obsp.spatial_conn()].toarray()
    assert np.array_equal(spatial_graph, correct_radius_graph)

    spatial_neighbors(non_visium_adata, delaunay=True, coord_type=None)
    spatial_graph = non_visium_adata.obsp[Key.obsp.spatial_conn()].toarray()
    spatial_dist = non_visium_adata.obsp[Key.obsp.spatial_dist()].toarray()
    assert np.array_equal(spatial_graph, correct_delaunay_graph)
    np.testing.assert_allclose(spatial_dist, correct_delaunay_dist)
Example #9
0
    def test_nhood_enrichment(self, adata: AnnData):
        spatial_neighbors(adata)
        nhood_enrichment(adata, cluster_key=_CK)

        self._assert_common(adata)
Example #10
0
    def test_plot_cbar_vmin_vmax(self, adata: AnnData):
        gr.spatial_neighbors(adata)
        gr.nhood_enrichment(adata, cluster_key=C_KEY)

        pl.nhood_enrichment(adata, cluster_key=C_KEY, vmin=10, vmax=20)
Example #11
0
    def test_plot_ripley_k_palette(self, adata_palette: AnnData):

        adata = adata_palette
        gr.spatial_neighbors(adata)
        gr.ripley_k(adata, cluster_key=C_KEY)
        pl.ripley_k(adata, cluster_key=C_KEY)
Example #12
0
    def test_plot_ripley_k(self, adata: AnnData):
        gr.spatial_neighbors(adata)
        gr.ripley_k(adata, cluster_key=C_KEY)

        pl.ripley_k(adata, cluster_key=C_KEY)
Example #13
0
    def test_plot_nhood_enrichment(self, adata: AnnData):
        gr.spatial_neighbors(adata)
        gr.nhood_enrichment(adata, cluster_key=C_KEY)

        pl.nhood_enrichment(adata, cluster_key=C_KEY)
Example #14
0
    def test_plot_centrality_scores(self, adata: AnnData):
        gr.spatial_neighbors(adata)
        gr.centrality_scores(adata, cluster_key=C_KEY)

        pl.centrality_scores(adata, cluster_key=C_KEY)
Example #15
0
    def test_plot_interaction_dendro(self, adata: AnnData):
        gr.spatial_neighbors(adata)
        gr.interaction_matrix(adata, cluster_key=C_KEY)

        pl.interaction_matrix(adata, cluster_key=C_KEY, method="single")
Example #16
0
    def test_plot_interaction(self, adata: AnnData):
        gr.spatial_neighbors(adata)
        gr.interaction_matrix(adata, cluster_key=C_KEY)

        pl.interaction_matrix(adata, cluster_key=C_KEY)