Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/cellcharter/gr/_nhood.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,10 @@ def _nhood_enrichment(

enrichment = np.log2(observed / expected) if log_fold_change else observed - expected
if only_inter:
np.fill_diagonal(observed.values, np.nan)
np.fill_diagonal(expected.values, np.nan)
np.fill_diagonal(enrichment.values, np.nan)
diagonal_mask = np.eye(len(observed), dtype=bool)
observed = observed.mask(diagonal_mask, np.nan)
expected = expected.mask(diagonal_mask, np.nan)
enrichment = enrichment.mask(diagonal_mask, np.nan)

result = {"enrichment": enrichment}

Expand Down
4 changes: 2 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ def _download_codex(path: Path) -> None:

@pytest.fixture()
def non_visium_adata() -> ad.AnnData:
non_visium_coords = np.array([[1, 0], [3, 0], [5, 6], [0, 4]])
adata = ad.AnnData(X=non_visium_coords, dtype=int)
non_visium_coords = np.array([[1, 0], [3, 0], [5, 6], [0, 4]], dtype=int)
adata = ad.AnnData(X=non_visium_coords)
adata.obsm[Key.obsm.spatial] = non_visium_coords
return adata

Expand Down
28 changes: 28 additions & 0 deletions tests/graph/test_nhood_local.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import numpy as np
import pandas as pd
import scipy.sparse as sps
from anndata import AnnData
from squidpy._constants._pkg_constants import Key

import cellcharter as cc


def test_nhood_enrichment_only_inter_sets_diagonal_to_nan():
adata = AnnData(X=np.ones((4, 2)))
adata.obs["cluster"] = pd.Categorical(["0", "0", "1", "1"])
adata.obsp[Key.obsp.spatial_conn()] = sps.csr_matrix(
np.array(
[
[0, 1, 1, 0],
[1, 0, 1, 1],
[1, 1, 0, 1],
[0, 1, 1, 0],
]
)
)

result = cc.gr.nhood_enrichment(adata, cluster_key="cluster", only_inter=True, observed_expected=True, copy=True)

assert np.all(np.isnan(np.diag(result["observed"])))
assert np.all(np.isnan(np.diag(result["expected"])))
assert np.all(np.isnan(np.diag(result["enrichment"])))
Loading