Skip to content

Commit ca41d05

Browse files
committed
fixed & added tests
1 parent 4896263 commit ca41d05

File tree

4 files changed

+59
-7
lines changed

4 files changed

+59
-7
lines changed
25.5 KB
Loading
42.2 KB
Loading
29.8 KB
Loading

tests/graph/test_niche.py

Lines changed: 59 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,17 @@
77
from pandas.testing import assert_frame_equal
88
from scipy.sparse import issparse
99

10+
import squidpy as sq
1011
from squidpy.gr import calculate_niche, spatial_neighbors
11-
from squidpy.gr._niche import _aggregate, _calculate_neighborhood_profile, _hop, _normalize, _setdiag, _utag
12+
from squidpy.gr._niche import (
13+
_aggregate,
14+
_calculate_neighborhood_profile,
15+
_hop,
16+
_normalize,
17+
_setdiag,
18+
_utag,
19+
)
20+
from tests.conftest import PlotTester, PlotTesterMeta
1221

1322
SPATIAL_CONNECTIVITIES_KEY = "spatial_connectivities"
1423
N_NEIGHBORS = 20
@@ -26,7 +35,7 @@ def test_neighborhood_profile_calculation(adata_seqfish: AnnData):
2635
resolutions=[0.1],
2736
min_niche_size=100,
2837
)
29-
niches = adata_seqfish.obs["neighborhood_niche_res=0.1"]
38+
niches = adata_seqfish.obs["nhood_niche_res=0.1"]
3039

3140
# assert no nans, more niche labels than non-niche labels, and at least 100 obs per niche
3241
assert niches.isna().sum() == 0
@@ -59,20 +68,26 @@ def test_utag(adata_seqfish: AnnData):
5968
spatial_neighbors(adata_seqfish, coord_type="generic", delaunay=False, n_neighs=N_NEIGHBORS)
6069
calculate_niche(adata_seqfish, flavor="utag", n_neighbors=N_NEIGHBORS, resolutions=[0.1, 1.0])
6170

62-
niches = adata_seqfish.obs["utag_res=1.0"]
63-
niches_low_res = adata_seqfish.obs["utag_res=0.1"]
71+
niches = adata_seqfish.obs["utag_niche_res=1.0"]
72+
niches_low_res = adata_seqfish.obs["utag_niche_res=0.1"]
6473

6574
assert niches.isna().sum() == 0
6675
assert niches.nunique() > niches_low_res.nunique()
6776

6877
# assert shape obs x var and sparsity in new feature matrix
69-
new_feature_matrix = _utag(adata_seqfish, normalize_adj=True, spatial_connectivity_key=SPATIAL_CONNECTIVITIES_KEY)
78+
new_feature_matrix = _utag(
79+
adata_seqfish,
80+
normalize_adj=True,
81+
spatial_connectivity_key=SPATIAL_CONNECTIVITIES_KEY,
82+
)
7083
assert new_feature_matrix.shape == adata_seqfish.X.shape
7184
assert issparse(new_feature_matrix)
7285

7386
spatial_neighbors(adata_seqfish, coord_type="generic", delaunay=False, n_neighs=40)
7487
new_feature_matrix_more_neighs = _utag(
75-
adata_seqfish, normalize_adj=True, spatial_connectivity_key=SPATIAL_CONNECTIVITIES_KEY
88+
adata_seqfish,
89+
normalize_adj=True,
90+
spatial_connectivity_key=SPATIAL_CONNECTIVITIES_KEY,
7691
)
7792

7893
# matrix products should differ when using different amount of neighbors
@@ -126,4 +141,41 @@ def test_nhop(adjacency_matrix: np.array, n_hop_matrix: np.array):
126141
assert (adj_sparse @ adj_sparse != nhop_sparse).nnz == 0
127142

128143

129-
# TODO: comppare results to previously calculated niches
144+
class TestNiches(PlotTester, metaclass=PlotTesterMeta):
145+
def test_plot_utag_niche(self, adata_seqfish: AnnData):
146+
spatial_neighbors(adata_seqfish, coord_type="generic", delaunay=False, n_neighs=N_NEIGHBORS)
147+
calculate_niche(adata_seqfish, flavor="utag", n_neighbors=N_NEIGHBORS, resolutions=0.5)
148+
149+
sq.pl.spatial_scatter(
150+
adata_seqfish,
151+
color="utag_niche_res=0.5",
152+
shape=None,
153+
)
154+
155+
def test_plot_neighborhood_niche(self, adata_seqfish: AnnData):
156+
spatial_neighbors(adata_seqfish, coord_type="generic", delaunay=False, n_neighs=N_NEIGHBORS)
157+
158+
calculate_niche(
159+
adata_seqfish,
160+
groups=GROUPS,
161+
flavor="neighborhood",
162+
n_neighbors=N_NEIGHBORS,
163+
resolutions=0.5,
164+
min_niche_size=100,
165+
)
166+
167+
sq.pl.spatial_scatter(
168+
adata_seqfish,
169+
color="nhood_niche_res=0.5",
170+
shape=None,
171+
)
172+
173+
def test_plot_cellcharter_niche(self, adata_seqfish: AnnData):
174+
spatial_neighbors(adata_seqfish, coord_type="generic", delaunay=False, n_neighs=N_NEIGHBORS)
175+
calculate_niche(adata_seqfish, groups=GROUPS, flavor="cellcharter", distance=3, n_components=5)
176+
177+
sq.pl.spatial_scatter(
178+
adata_seqfish,
179+
color="cellcharter_niche",
180+
shape=None,
181+
)

0 commit comments

Comments
 (0)