-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathtest_graph.py
313 lines (259 loc) · 10.2 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
"""Test for graph construction tools."""
from __future__ import annotations
from typing import ClassVar, NoReturn
import numpy as np
import pytest
import torch
from matplotlib import pyplot as plt
from tiatoolbox.tools.graph import (
SlideGraphConstructor,
affinity_to_edge_index,
delaunay_adjacency,
edge_index_to_triangles,
triangle_signed_area,
)
RNG = np.random.default_rng() # Numpy Random Generator
def test_delaunay_adjacency_dthresh_type() -> None:
"""Test empty input raises a TypeError if dthresh is not a Number."""
with pytest.raises(TypeError, match="number"):
delaunay_adjacency(points=[[0, 0]], dthresh=None)
def test_delaunay_adjacency_empty() -> None:
"""Test empty input raises a ValueError."""
points = np.array([])
with pytest.raises(ValueError, match="Points must have length >= 4"):
delaunay_adjacency(points, 10)
def test_delaunay_adjacency_invalid_shape() -> None:
"""Test points with invalid shape (not NxM) raises a ValueError."""
points = RNG.random((4, 4, 4))
with pytest.raises(ValueError, match="NxM"):
delaunay_adjacency(points, 10)
def test_delaunay_adjacency_nothing_connected() -> None:
"""Test delaunay_adjacency does not connect points further than dthresh.
Nothing should connect for this case as all points are further
apart than dthresh.
"""
# Simple convex hull with the minimum of 4 points
points = np.array(
[
[0, 0],
[1, 1],
[1, 3],
[1, 6],
],
)
adjacency_matrix = delaunay_adjacency(points=points, dthresh=0.5)
assert np.sum(adjacency_matrix) == 0
def test_delaunay_adjacency_connected() -> None:
"""Test delaunay_adjacency connects expects points in handcrafted input."""
# Simple convex hull with the minimum of 4 points
points = np.array(
[
[0, 0],
[1, 1],
[1, 3],
[1, 6],
],
)
adjacency_matrix = delaunay_adjacency(points=points, dthresh=1.5)
# Expect 1 connection, symmetrical so dividing by 2
assert np.sum(adjacency_matrix) / 2 == 1
def test_affinity_to_edge_index_fuzz_output_shape() -> None:
"""Fuzz test that output shape is 2xM for affinity_to_edge.
Output is 2xM, where M is the number of edges in the graph, i.e.
the number of connections between nodes with a value > threshold.
"""
rng = np.random.default_rng(123)
for _ in range(1000):
# Generate some random square inputs
input_shape = [rng.integers(2, 10)] * 2
affinity_matrix = rng.random(input_shape)
threshold = rng.random()
# Convert to torch randomly
if rng.random() > 0.5:
affinity_matrix = torch.tensor(affinity_matrix)
edge_index = affinity_to_edge_index(affinity_matrix, threshold=threshold)
# Check the output has shape (2, M)
assert len(edge_index.shape) == 2
n = len(affinity_matrix)
two, m = edge_index.shape
assert two == 2
assert 0 <= m <= n**2
def test_affinity_to_edge_index_invalid_fuzz_input_shape() -> None:
"""Test that affinity_to_edge fails with non-square input."""
# Generate some random square inputs
rng = np.random.default_rng(123)
for _ in range(100):
input_shape = [rng.integers(2, 10)] * 2
input_shape[1] -= 1
affinity_matrix = rng.random(input_shape)
threshold = rng.random()
# Convert to torch randomly
if rng.random() > 0.5:
affinity_matrix = torch.tensor(affinity_matrix)
with pytest.raises(ValueError, match="square"):
_ = affinity_to_edge_index(affinity_matrix, threshold=threshold)
def test_edge_index_to_triangles_invalid_input() -> None:
"""Test edge_index_to_triangles fails with invalid input."""
edge_index = torch.tensor([[0, 1], [0, 2], [1, 2]])
with pytest.raises(ValueError, match="must be a 2xM"):
edge_index_to_triangles(edge_index)
def test_triangle_signed_area() -> None:
"""Test that the signed area of a triangle is correct."""
# Triangle with positive area
points = np.array([[0, 0], [1, 0], [0, 1]])
area = triangle_signed_area(points)
assert area == 0.5
# Triangle with negative area
points = np.array([[0, 0], [1, 0], [0, -1]])
area = triangle_signed_area(points)
assert area == -0.5
# Triangle with co-linear points
points = np.array([[0, 0], [1, 1], [2, 2]])
area = triangle_signed_area(points)
assert area == 0
# Triangle with larger area
points = np.array([[0, 0], [2, 0], [0, 2]])
area = triangle_signed_area(points)
assert area == 2
def test_triangle_signed_area_invalid_input() -> None:
"""Test that the signed area of a triangle with invalid input fails."""
points = RNG.random((3, 3))
with pytest.raises(ValueError, match="3x2"):
triangle_signed_area(points)
def test_edge_index_to_trainangles_single() -> None:
"""Test edge_index_to_triangles with a simple 2XM input matrix.
Basic test case for a single triangle.
0 -- 1
| /
| /
2
"""
edge_index = np.array([[0, 1], [0, 2], [1, 2]]).T
triangles = edge_index_to_triangles(edge_index)
assert triangles.shape == (1, 3)
assert np.array_equal(triangles, np.array([[0, 1, 2]]))
def test_edge_index_to_trainangles_many() -> None:
"""Test edge_index_to_triangles with a simple 2XM input matrix.
Moderate test case for a few trainangles.
4 -- 3
| / |
|/ |
0 -- 1
| /
| /
2
"""
edge_index = np.array([[0, 1], [0, 2], [1, 2], [0, 3], [1, 3], [0, 4], [4, 3]]).T
triangles = edge_index_to_triangles(edge_index)
assert triangles.shape == (3, 3)
def test_slidegraph_build_feature_range_thresh_none() -> None:
"""Test SlideGraphConstructor builds a graph without removing features."""
# Generate random points and features
rng = np.random.default_rng(123)
points = rng.random((100, 2))
features = rng.random((100, 100)) / 1e-5
# Build the graph
graph = SlideGraphConstructor.build(
points=points,
features=features,
feature_range_thresh=None,
)
assert graph["x"].shape[1] == 100
class TestConstructor:
"""Define class to test constructors."""
scenarios: ClassVar[tuple[str, dict]] = [
("SlideGraph", {"graph_constructor": SlideGraphConstructor}),
]
@staticmethod
def test_build(graph_constructor: SlideGraphConstructor) -> NoReturn:
"""Test that build outputs are in an expected format.
Check the lengths and ranges of outputs with random data as input.
"""
rng = np.random.default_rng(123)
points = np.concatenate(
[rng.random((25, 2)) * 100 + (offset * 1000) for offset in range(10)],
)
features = np.concatenate(
[rng.random((25, 100)) * 100 + (offset * 1000) for offset in range(10)],
)
graph = graph_constructor.build(points, features)
x = graph["x"]
assert len(x) > 0
assert len(x) <= len(points)
edge_index = graph["edge_index"]
two, m = edge_index.shape
n = len(x)
assert two == 2
assert 0 <= m <= n**2
@staticmethod
def test_visualise(graph_constructor: SlideGraphConstructor) -> NoReturn:
"""Test visualising a graph."""
rng = np.random.default_rng(123)
points = np.concatenate(
[rng.random((25, 2)) * 100 + (offset * 1000) for offset in range(10)],
)
features = np.concatenate(
[rng.random((25, 100)) * 100 + (offset * 1000) for offset in range(10)],
)
graph = graph_constructor.build(points, features)
graph_constructor.visualise(graph)
plt.close()
@staticmethod
def test_visualise_ax(graph_constructor: SlideGraphConstructor) -> NoReturn:
"""Test visualising a graph on a given axis."""
rng = np.random.default_rng(123)
points = np.concatenate(
[rng.random((25, 2)) * 100 + (offset * 1000) for offset in range(10)],
)
features = np.concatenate(
[rng.random((25, 100)) * 100 + (offset * 1000) for offset in range(10)],
)
_, ax = plt.subplots()
graph = graph_constructor.build(points, features)
graph_constructor.visualise(graph, ax=ax)
plt.close()
@staticmethod
def test_visualise_custom_color_function(
graph_constructor: SlideGraphConstructor,
) -> NoReturn:
"""Test visualising a graph with a custom color function."""
rng = np.random.default_rng(123)
points = np.concatenate(
[rng.random((25, 2)) * 100 + (offset * 1000) for offset in range(10)],
)
features = np.concatenate(
[rng.random((25, 100)) * 100 + (offset * 1000) for offset in range(10)],
)
graph = graph_constructor.build(points, features)
cmap = plt.get_cmap("viridis")
graph_constructor.visualise(
graph,
color=lambda g: cmap(np.mean(g["x"], axis=1)),
)
plt.close()
@staticmethod
def test_visualise_static_color(
graph_constructor: SlideGraphConstructor,
) -> NoReturn:
"""Test visualising a graph with a custom color function."""
rng = np.random.default_rng(123)
points = np.concatenate(
[rng.random((25, 2)) * 100 + (offset * 1000) for offset in range(10)],
)
features = np.concatenate(
[rng.random((25, 100)) * 100 + (offset * 1000) for offset in range(10)],
)
graph = graph_constructor.build(points, features)
graph_constructor.visualise(graph, color="orange")
plt.close()
@staticmethod
def test_visualise_invalid_input(
graph_constructor: SlideGraphConstructor,
) -> NoReturn:
"""Test visualising a graph with invalid input."""
with pytest.raises(ValueError, match="must contain key `x`"):
graph_constructor.visualise({})
with pytest.raises(ValueError, match="must contain key `edge_index`"):
graph_constructor.visualise({"x": []})
with pytest.raises(ValueError, match="must contain key `coordinates`"):
graph_constructor.visualise({"x": [], "edge_index": []})