24
24
GROUPS = "celltype_mapped_refined"
25
25
26
26
27
- def test_neighborhood_profile_calculation (adata_seqfish : AnnData ):
27
+ def test_niche_calc_nhood (adata_seqfish : AnnData ):
28
28
"""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
+ )
30
32
calculate_niche (
31
33
adata_seqfish ,
32
34
groups = GROUPS ,
@@ -48,8 +50,12 @@ def test_neighborhood_profile_calculation(adata_seqfish: AnnData):
48
50
matrix = adata_seqfish .obsp [SPATIAL_CONNECTIVITIES_KEY ].tocoo ()
49
51
50
52
# 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
+ )
53
59
# assert shape obs x groups
54
60
assert rel_nhood_profile .shape == (
55
61
adata_seqfish .n_obs ,
@@ -63,10 +69,14 @@ def test_neighborhood_profile_calculation(adata_seqfish: AnnData):
63
69
assert abs_nhood_profile .sum (axis = 1 ).max () == N_NEIGHBORS
64
70
65
71
66
- def test_utag (adata_seqfish : AnnData ):
72
+ def test_niche_calc_utag (adata_seqfish : AnnData ):
67
73
"""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
+ )
70
80
71
81
niches = adata_seqfish .obs ["utag_niche_res=1.0" ]
72
82
niches_low_res = adata_seqfish .obs ["utag_niche_res=0.1" ]
@@ -99,11 +109,15 @@ def test_utag(adata_seqfish: AnnData):
99
109
raise AssertionError
100
110
101
111
102
- def test_cellcharter_approach (adata_seqfish : AnnData ):
112
+ def test_niche_calc_cellcharter (adata_seqfish : AnnData ):
103
113
"""Check whether niche calculation using CellCharter approach works as intended."""
104
114
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
+ )
107
121
niches = adata_seqfish .obs ["cellcharter_niche" ]
108
122
109
123
assert niches .nunique () == 5
@@ -132,19 +146,14 @@ def test_cellcharter_approach(adata_seqfish: AnnData):
132
146
# TODO: add test for GMM
133
147
134
148
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
-
144
149
class TestNiches (PlotTester , metaclass = PlotTesterMeta ):
145
150
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
+ )
148
157
149
158
sq .pl .spatial_scatter (
150
159
adata_seqfish ,
@@ -153,7 +162,9 @@ def test_plot_utag_niche(self, adata_seqfish: AnnData):
153
162
)
154
163
155
164
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
+ )
157
168
158
169
calculate_niche (
159
170
adata_seqfish ,
@@ -171,8 +182,16 @@ def test_plot_neighborhood_niche(self, adata_seqfish: AnnData):
171
182
)
172
183
173
184
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
+ )
176
195
177
196
sq .pl .spatial_scatter (
178
197
adata_seqfish ,
0 commit comments