7
7
from pandas .testing import assert_frame_equal
8
8
from scipy .sparse import issparse
9
9
10
+ import squidpy as sq
10
11
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
12
21
13
22
SPATIAL_CONNECTIVITIES_KEY = "spatial_connectivities"
14
23
N_NEIGHBORS = 20
@@ -26,7 +35,7 @@ def test_neighborhood_profile_calculation(adata_seqfish: AnnData):
26
35
resolutions = [0.1 ],
27
36
min_niche_size = 100 ,
28
37
)
29
- niches = adata_seqfish .obs ["neighborhood_niche_res =0.1" ]
38
+ niches = adata_seqfish .obs ["nhood_niche_res =0.1" ]
30
39
31
40
# assert no nans, more niche labels than non-niche labels, and at least 100 obs per niche
32
41
assert niches .isna ().sum () == 0
@@ -59,20 +68,26 @@ def test_utag(adata_seqfish: AnnData):
59
68
spatial_neighbors (adata_seqfish , coord_type = "generic" , delaunay = False , n_neighs = N_NEIGHBORS )
60
69
calculate_niche (adata_seqfish , flavor = "utag" , n_neighbors = N_NEIGHBORS , resolutions = [0.1 , 1.0 ])
61
70
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" ]
64
73
65
74
assert niches .isna ().sum () == 0
66
75
assert niches .nunique () > niches_low_res .nunique ()
67
76
68
77
# 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
+ )
70
83
assert new_feature_matrix .shape == adata_seqfish .X .shape
71
84
assert issparse (new_feature_matrix )
72
85
73
86
spatial_neighbors (adata_seqfish , coord_type = "generic" , delaunay = False , n_neighs = 40 )
74
87
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 ,
76
91
)
77
92
78
93
# 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):
126
141
assert (adj_sparse @ adj_sparse != nhop_sparse ).nnz == 0
127
142
128
143
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