Skip to content

Commit 1f8395a

Browse files
committed
Added images from runner
1 parent 248e8ea commit 1f8395a

File tree

4 files changed

+43
-24
lines changed

4 files changed

+43
-24
lines changed
-7.81 KB
Loading
-30.4 KB
Loading
-14 KB
Loading

tests/graph/test_niche.py

Lines changed: 43 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@
2424
GROUPS = "celltype_mapped_refined"
2525

2626

27-
def test_neighborhood_profile_calculation(adata_seqfish: AnnData):
27+
def test_niche_calc_nhood(adata_seqfish: AnnData):
2828
"""Check whether niche calculation using neighborhood profile approach works as intended."""
29-
spatial_neighbors(adata_seqfish, coord_type="generic", delaunay=False, n_neighs=N_NEIGHBORS)
29+
spatial_neighbors(
30+
adata_seqfish, coord_type="generic", delaunay=False, n_neighs=N_NEIGHBORS
31+
)
3032
calculate_niche(
3133
adata_seqfish,
3234
groups=GROUPS,
@@ -48,8 +50,12 @@ def test_neighborhood_profile_calculation(adata_seqfish: AnnData):
4850
matrix = adata_seqfish.obsp[SPATIAL_CONNECTIVITIES_KEY].tocoo()
4951

5052
# get obs x category matrix where each column is the absolute/relative frequency of a category in the neighborhood
51-
rel_nhood_profile = _calculate_neighborhood_profile(adata_seqfish, groups=GROUPS, matrix=matrix, abs_nhood=False)
52-
abs_nhood_profile = _calculate_neighborhood_profile(adata_seqfish, groups=GROUPS, matrix=matrix, abs_nhood=True)
53+
rel_nhood_profile = _calculate_neighborhood_profile(
54+
adata_seqfish, groups=GROUPS, matrix=matrix, abs_nhood=False
55+
)
56+
abs_nhood_profile = _calculate_neighborhood_profile(
57+
adata_seqfish, groups=GROUPS, matrix=matrix, abs_nhood=True
58+
)
5359
# assert shape obs x groups
5460
assert rel_nhood_profile.shape == (
5561
adata_seqfish.n_obs,
@@ -63,10 +69,14 @@ def test_neighborhood_profile_calculation(adata_seqfish: AnnData):
6369
assert abs_nhood_profile.sum(axis=1).max() == N_NEIGHBORS
6470

6571

66-
def test_utag(adata_seqfish: AnnData):
72+
def test_niche_calc_utag(adata_seqfish: AnnData):
6773
"""Check whether niche calculation using UTAG approach works as intended."""
68-
spatial_neighbors(adata_seqfish, coord_type="generic", delaunay=False, n_neighs=N_NEIGHBORS)
69-
calculate_niche(adata_seqfish, flavor="utag", n_neighbors=N_NEIGHBORS, resolutions=[0.1, 1.0])
74+
spatial_neighbors(
75+
adata_seqfish, coord_type="generic", delaunay=False, n_neighs=N_NEIGHBORS
76+
)
77+
calculate_niche(
78+
adata_seqfish, flavor="utag", n_neighbors=N_NEIGHBORS, resolutions=[0.1, 1.0]
79+
)
7080

7181
niches = adata_seqfish.obs["utag_niche_res=1.0"]
7282
niches_low_res = adata_seqfish.obs["utag_niche_res=0.1"]
@@ -99,11 +109,15 @@ def test_utag(adata_seqfish: AnnData):
99109
raise AssertionError
100110

101111

102-
def test_cellcharter_approach(adata_seqfish: AnnData):
112+
def test_niche_calc_cellcharter(adata_seqfish: AnnData):
103113
"""Check whether niche calculation using CellCharter approach works as intended."""
104114

105-
spatial_neighbors(adata_seqfish, coord_type="generic", delaunay=False, n_neighs=N_NEIGHBORS)
106-
calculate_niche(adata_seqfish, groups=GROUPS, flavor="cellcharter", distance=3, n_components=5)
115+
spatial_neighbors(
116+
adata_seqfish, coord_type="generic", delaunay=False, n_neighs=N_NEIGHBORS
117+
)
118+
calculate_niche(
119+
adata_seqfish, groups=GROUPS, flavor="cellcharter", distance=3, n_components=5
120+
)
107121
niches = adata_seqfish.obs["cellcharter_niche"]
108122

109123
assert niches.nunique() == 5
@@ -132,19 +146,14 @@ def test_cellcharter_approach(adata_seqfish: AnnData):
132146
# TODO: add test for GMM
133147

134148

135-
def test_nhop(adjacency_matrix: np.array, n_hop_matrix: np.array):
136-
"""Test if n-hop neighbor computation works as expected."""
137-
138-
assert np.array_equal(adjacency_matrix @ adjacency_matrix, n_hop_matrix)
139-
adj_sparse = scipy.sparse.csr_matrix(adjacency_matrix)
140-
nhop_sparse = scipy.sparse.csr_matrix(n_hop_matrix)
141-
assert (adj_sparse @ adj_sparse != nhop_sparse).nnz == 0
142-
143-
144149
class TestNiches(PlotTester, metaclass=PlotTesterMeta):
145150
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)
151+
spatial_neighbors(
152+
adata_seqfish, coord_type="generic", delaunay=False, n_neighs=N_NEIGHBORS
153+
)
154+
calculate_niche(
155+
adata_seqfish, flavor="utag", n_neighbors=N_NEIGHBORS, resolutions=0.5
156+
)
148157

149158
sq.pl.spatial_scatter(
150159
adata_seqfish,
@@ -153,7 +162,9 @@ def test_plot_utag_niche(self, adata_seqfish: AnnData):
153162
)
154163

155164
def test_plot_neighborhood_niche(self, adata_seqfish: AnnData):
156-
spatial_neighbors(adata_seqfish, coord_type="generic", delaunay=False, n_neighs=N_NEIGHBORS)
165+
spatial_neighbors(
166+
adata_seqfish, coord_type="generic", delaunay=False, n_neighs=N_NEIGHBORS
167+
)
157168

158169
calculate_niche(
159170
adata_seqfish,
@@ -171,8 +182,16 @@ def test_plot_neighborhood_niche(self, adata_seqfish: AnnData):
171182
)
172183

173184
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)
185+
spatial_neighbors(
186+
adata_seqfish, coord_type="generic", delaunay=False, n_neighs=N_NEIGHBORS
187+
)
188+
calculate_niche(
189+
adata_seqfish,
190+
groups=GROUPS,
191+
flavor="cellcharter",
192+
distance=3,
193+
n_components=5,
194+
)
176195

177196
sq.pl.spatial_scatter(
178197
adata_seqfish,

0 commit comments

Comments
 (0)