def test_interaction_matrix_values(adata_intmat: AnnData): result_weighted = interaction_matrix(adata_intmat, "cat", weights=True, copy=True) result_unweighted = interaction_matrix(adata_intmat, "cat", weights=False, copy=True) expected_weighted = np.array([ [5, 1], [2, 3], ]) expected_unweighted = np.array([ [4, 1], [2, 2], ]) np.testing.assert_array_equal(expected_weighted, result_weighted) np.testing.assert_array_equal(expected_unweighted, result_unweighted)
def test_interaction_matrix_nan_values(adata_intmat: AnnData): adata_intmat.obs["cat"].iloc[0] = np.nan result_weighted = interaction_matrix(adata_intmat, "cat", weights=True, copy=True) result_unweighted = interaction_matrix(adata_intmat, "cat", weights=False, copy=True) expected_weighted = np.array([ [2, 1], [2, 3], ]) expected_unweighted = np.array([ [1, 1], [2, 2], ]) np.testing.assert_array_equal(expected_weighted, result_weighted) np.testing.assert_array_equal(expected_unweighted, result_unweighted)
def test_interaction_matrix_normalize(nhood_data: AnnData, normalized: bool): adata = nhood_data res = interaction_matrix( adata=adata, cluster_key=_CK, connectivity_key="spatial", copy=True, normalized=normalized, ) n_cls = adata.obs["leiden"].nunique() assert isinstance(res, np.ndarray) assert res.shape == (n_cls, n_cls) if normalized: np.testing.assert_allclose(res.sum(1), 1.0), res.sum(1) else: assert len(adata.obsp["spatial_connectivities"].data) == res.sum()
def test_interaction_matrix_copy(nhood_data: AnnData, copy: bool): adata = nhood_data res = interaction_matrix( adata=adata, cluster_key=_CK, connectivity_key="spatial", copy=copy, ) key = Key.uns.interaction_matrix(_CK) n_cls = adata.obs[_CK].nunique() if not copy: assert res is None assert key in adata.uns_keys() res = adata.uns[key] else: assert key not in adata.uns_keys() assert isinstance(res, np.ndarray) assert res.shape == (n_cls, n_cls)
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")
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)