forked from scverse/squidpy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_graph.py
199 lines (142 loc) · 8.26 KB
/
test_graph.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
from copy import deepcopy
from typing import Mapping
import pytest
from anndata import AnnData
import scanpy as sc
import numpy as np
import pandas as pd
from squidpy import gr, pl
from tests.conftest import DPI, PlotTester, PlotTesterMeta
C_KEY = "leiden"
sc.pl.set_rcParams_defaults()
sc.set_figure_params(dpi=40, color_map="viridis")
# WARNING:
# 1. all classes must both subclass PlotTester and use metaclass=PlotTesterMeta
# 2. tests which produce a plot must be prefixed with `test_plot_`
# 3. if the tolerance needs to be change, don't prefix the function with `test_plot_`, but with something else
# the comp. function can be accessed as `self.compare(<your_filename>, tolerance=<your_tolerance>)`
# ".png" is appended to <your_filename>, no need to set it
class TestGraph(PlotTester, metaclass=PlotTesterMeta):
def test_plot_interaction(self, adata: AnnData):
gr.spatial_neighbors(adata)
gr.interaction_matrix(adata, cluster_key=C_KEY)
pl.interaction_matrix(adata, cluster_key=C_KEY)
def test_plot_interaction_dendro(self, adata: AnnData):
gr.spatial_neighbors(adata)
gr.interaction_matrix(adata, cluster_key=C_KEY)
pl.interaction_matrix(adata, cluster_key=C_KEY, method="single")
def test_plot_centrality_scores(self, adata: AnnData):
gr.spatial_neighbors(adata)
gr.centrality_scores(adata, cluster_key=C_KEY)
pl.centrality_scores(adata, cluster_key=C_KEY)
def test_plot_centrality_scores_single(self, adata: AnnData):
selected_score = "degree_centrality"
gr.spatial_neighbors(adata)
gr.centrality_scores(adata, cluster_key=C_KEY)
pl.centrality_scores(adata, cluster_key=C_KEY, score=selected_score, dpi=DPI)
def test_plot_nhood_enrichment(self, adata: AnnData):
gr.spatial_neighbors(adata)
gr.nhood_enrichment(adata, cluster_key=C_KEY)
pl.nhood_enrichment(adata, cluster_key=C_KEY)
def test_plot_nhood_enrichment_dendro(self, adata: AnnData):
gr.spatial_neighbors(adata)
gr.nhood_enrichment(adata, cluster_key=C_KEY)
# use count to avoid nan for scipy.cluster.hierarchy
pl.nhood_enrichment(adata, cluster_key=C_KEY, mode="count", method="single")
def test_plot_ripley_l(self, adata_ripley: AnnData):
adata = adata_ripley
gr.ripley(adata, cluster_key=C_KEY, mode="L")
pl.ripley(adata, cluster_key=C_KEY, mode="L")
def test_plot_ripley_f(self, adata_ripley: AnnData):
adata = adata_ripley
gr.ripley(adata, cluster_key=C_KEY, mode="F")
pl.ripley(adata, cluster_key=C_KEY, mode="F")
def test_plot_ripley_g(self, adata_ripley: AnnData):
adata = adata_ripley
gr.ripley(adata, cluster_key=C_KEY, mode="G")
pl.ripley(adata, cluster_key=C_KEY, mode="G")
def test_plot_ripley_f_nopalette(self, adata_ripley: AnnData):
adata = adata_ripley
adata.uns.pop(f"{C_KEY}_colors")
gr.ripley(adata, cluster_key=C_KEY, mode="F")
pl.ripley(adata, cluster_key=C_KEY, mode="F")
def test_tol_plot_co_occurrence(self, adata: AnnData):
gr.co_occurrence(adata, cluster_key=C_KEY)
pl.co_occurrence(adata, cluster_key=C_KEY, clusters=["0", "2"])
self.compare("Graph_co_occurrence", tolerance=70)
def test_tol_plot_co_occurrence_palette(self, adata_palette: AnnData):
adata = adata_palette
gr.co_occurrence(adata, cluster_key=C_KEY)
pl.co_occurrence(adata, cluster_key=C_KEY, clusters=["0", "2"])
self.compare("Graph_co_occurrence_palette", tolerance=70)
class TestHeatmap(PlotTester, metaclass=PlotTesterMeta):
def test_plot_cbar_vmin_vmax(self, adata: AnnData):
gr.spatial_neighbors(adata)
gr.nhood_enrichment(adata, cluster_key=C_KEY)
pl.nhood_enrichment(adata, cluster_key=C_KEY, vmin=10, vmax=20)
def test_plot_cbar_kwargs(self, adata: AnnData):
gr.spatial_neighbors(adata)
gr.nhood_enrichment(adata, cluster_key=C_KEY)
pl.nhood_enrichment(adata, cluster_key=C_KEY, cbar_kwargs={"label": "FOOBARBAZQUUX", "filled": False})
class TestLigrec(PlotTester, metaclass=PlotTesterMeta):
def test_invalid_type(self):
with pytest.raises(TypeError, match=r"Expected `adata` .+ found `int`."):
pl.ligrec(42)
def test_invalid_key(self, adata: AnnData):
with pytest.raises(KeyError, match=r"Key `foobar_ligrec` not found in `adata.uns`."):
pl.ligrec(adata, cluster_key="foobar")
def test_valid_key_invalid_object(self, adata: AnnData):
adata.uns["foobar_ligrec"] = "baz"
with pytest.raises(TypeError, match=r"Expected `adata` .+ found `str`."):
pl.ligrec(adata, cluster_key="foobar")
def test_invalid_alpha(self, ligrec_result: Mapping[str, pd.DataFrame]):
with pytest.raises(ValueError, match=r"Expected `alpha`"):
pl.ligrec(ligrec_result, alpha=1.2)
def test_invalid_means_range_size(self, ligrec_result: Mapping[str, pd.DataFrame]):
with pytest.raises(ValueError, match=r"Expected `means_range` to be a sequence of size `2`, found `3`."):
pl.ligrec(ligrec_result, means_range=[0, 1, 2])
def test_invalid_clusters(self, ligrec_result: Mapping[str, pd.DataFrame]):
with pytest.raises(ValueError, match=r"No valid clusters have been selected."):
pl.ligrec(ligrec_result, source_groups="foo", target_groups="bar")
def test_all_interactions_empty(self, ligrec_result: Mapping[str, pd.DataFrame]):
empty = pd.DataFrame(np.nan, index=ligrec_result["pvalues"].index, columns=ligrec_result["pvalues"].columns)
with pytest.raises(ValueError, match=r"After removing rows with only NaN interactions, none remain."):
pl.ligrec({"means": empty, "pvalues": empty, "metadata": empty}, remove_empty_interactions=True)
def test_plot_source_clusters(self, ligrec_result: Mapping[str, pd.DataFrame]):
src_cls = ligrec_result["pvalues"].columns.get_level_values(0)[0]
pl.ligrec(ligrec_result, source_groups=src_cls)
def test_plot_target_clusters(self, ligrec_result: Mapping[str, pd.DataFrame]):
tgt_cls = ligrec_result["pvalues"].columns.get_level_values(1)[0]
pl.ligrec(ligrec_result, target_groups=tgt_cls)
def test_plot_no_remove_empty_interactions(self, ligrec_result: Mapping[str, pd.DataFrame]):
tmp = deepcopy(ligrec_result)
tmp["pvalues"].values[:2, :] = np.nan
pl.ligrec(tmp, remove_empty_interactions=False)
def test_plot_pvalue_threshold(self, ligrec_result: Mapping[str, pd.DataFrame]):
pl.ligrec(ligrec_result, pvalue_threshold=0.05)
def test_plot_means_range(self, ligrec_result: Mapping[str, pd.DataFrame]):
pl.ligrec(ligrec_result, means_range=(0.5, 1))
def test_plot_dendrogram_pairs(self, ligrec_result: Mapping[str, pd.DataFrame]):
np.random.seed(42)
pl.ligrec(ligrec_result, dendrogram="interacting_molecules")
def test_plot_dendrogram_clusters(self, ligrec_result: Mapping[str, pd.DataFrame]):
np.random.seed(42)
pl.ligrec(ligrec_result, dendrogram="interacting_clusters")
def test_plot_dendrogram_both(self, ligrec_result: Mapping[str, pd.DataFrame]):
np.random.seed(42)
pl.ligrec(ligrec_result, dendrogram="both")
def test_plot_swap_axes(self, ligrec_result: Mapping[str, pd.DataFrame]):
pl.ligrec(ligrec_result, swap_axes=True)
def test_plot_swap_axes_dedrogram(self, ligrec_result: Mapping[str, pd.DataFrame]):
pl.ligrec(ligrec_result, swap_axes=True, dendrogram="interacting_molecules")
def test_plot_alpha(self, ligrec_result: Mapping[str, pd.DataFrame]):
pl.ligrec(ligrec_result, alpha=1)
def test_plot_alpha_none(self, ligrec_result: Mapping[str, pd.DataFrame]):
pl.ligrec(ligrec_result, alpha=None)
def test_plot_cmap(self, ligrec_result: Mapping[str, pd.DataFrame]):
pl.ligrec(ligrec_result, cmap="inferno")
def test_plot_kwargs(self, ligrec_result: Mapping[str, pd.DataFrame]):
# color_on is intentionally ignored
pl.ligrec(ligrec_result, grid=False, color_on="square", x_padding=2, y_padding=2)
def test_plot_remove_nonsig_interactions(self, ligrec_result: Mapping[str, pd.DataFrame]):
pl.ligrec(ligrec_result, remove_nonsig_interactions=True, alpha=1e-4)