File size: 7,507 Bytes
12d2e9e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Copyright 2021, Dana-Farber Cancer Institute and Weill Cornell Medicine
License: GNU GPL 2.0
"""

import numpy as np
import pytest
import torch
from skimage.draw import ellipse
from skimage.measure import label, regionprops

from pathml.graph import KNNGraphBuilder, RAGGraphBuilder
from pathml.graph.preprocessing import MSTGraphBuilder


def make_fake_instance_maps(num, image_size, ellipse_height, ellipse_width):
    img = np.zeros(image_size)

    # Draw n ellipses
    for i in range(num):
        # Random center for each ellipse
        center_x = np.random.randint(ellipse_width, image_size[1] - ellipse_width)
        center_y = np.random.randint(ellipse_height, image_size[0] - ellipse_height)

        # Coordinates for the ellipse
        rr, cc = ellipse(
            center_y, center_x, ellipse_height, ellipse_width, shape=image_size
        )

        # Draw the ellipse
        img[rr, cc] = 1

    label_img = label(img.astype(int))

    return label_img


@pytest.mark.parametrize("k", [1, 10, 50])
@pytest.mark.parametrize("thresh", [0, 10, 200])
@pytest.mark.parametrize("add_loc_feats", [True, False])
@pytest.mark.parametrize("add_node_labels", [True, False])
@pytest.mark.parametrize("return_networkx", [True, False])
@pytest.mark.parametrize("use_centroids", [True, False])
def test_knn_graph_building(
    k, thresh, add_loc_feats, add_node_labels, return_networkx, use_centroids
):

    if not use_centroids:
        image_size = (1024, 2048)

        instance_map = make_fake_instance_maps(
            num=100, image_size=image_size, ellipse_height=10, ellipse_width=8
        )
        regions = regionprops(instance_map)
        features = torch.randn(len(regions), 512)
        num_nodes = len(regions)

        if add_node_labels:
            annotation = torch.randn(len(regions), 4)
        else:
            annotation = None

        graph_builder = KNNGraphBuilder(
            k=k,
            thresh=thresh,
            add_loc_feats=add_loc_feats,
            return_networkx=return_networkx,
        )

        graph = graph_builder.process(
            instance_map, features, annotation=annotation, target=1
        )

    elif use_centroids:
        centroids = torch.randn(100, 2)
        features = torch.randn(100, 512)
        if add_node_labels:
            annotation = torch.randn(100, 4)
        else:
            annotation = None
        num_nodes = 100

        graph_builder = KNNGraphBuilder(
            k=k,
            thresh=thresh,
            add_loc_feats=add_loc_feats,
            return_networkx=return_networkx,
        )

        graph = graph_builder.process_with_centroids(
            centroids,
            features,
            annotation=annotation,
            image_size=(1000, 1000),
            target=1,
        )

    if return_networkx:
        assert len(graph.nodes) == num_nodes
        if add_loc_feats:
            assert len(graph.nodes[0]["node_features"]) == 514
        else:
            assert len(graph.nodes[0]["node_features"]) == 512
    else:
        assert graph.node_centroids.shape == (num_nodes, 2)
        assert graph.edge_index.shape[0] == 2
        if add_loc_feats:
            assert graph.node_features.shape == (num_nodes, 512 + 2)
        else:
            assert graph.node_features.shape == (num_nodes, 512)

        if add_node_labels:
            assert graph.node_labels.shape == (num_nodes, 4)


@pytest.mark.parametrize("kernel_size", [1, 3, 10])
@pytest.mark.parametrize("hops", [1, 2, 5])
@pytest.mark.parametrize("add_loc_feats", [True, False])
@pytest.mark.parametrize("add_node_labels", [True, False])
@pytest.mark.parametrize("return_networkx", [True, False])
def test_rag_graph_building(
    kernel_size, hops, add_loc_feats, add_node_labels, return_networkx
):
    image_size = (1024, 2048)

    instance_map = make_fake_instance_maps(
        num=100, image_size=image_size, ellipse_height=10, ellipse_width=8
    )
    regions = regionprops(instance_map)
    num_nodes = len(regions)
    features = torch.randn(len(regions), 512)
    if add_node_labels:
        annotation = torch.randn(len(regions), 4)
    else:
        annotation = None

    graph_builder = RAGGraphBuilder(
        kernel_size=kernel_size,
        hops=hops,
        add_loc_feats=add_loc_feats,
        return_networkx=return_networkx,
    )

    graph = graph_builder.process(
        instance_map, features, annotation=annotation, target=1
    )

    if return_networkx:
        assert len(graph.nodes) == num_nodes
        if add_loc_feats:
            assert len(graph.nodes[0]["node_features"]) == 514
        else:
            assert len(graph.nodes[0]["node_features"]) == 512
    else:
        assert graph.node_centroids.shape == (num_nodes, 2)
        assert graph.edge_index.shape[0] == 2
        if add_loc_feats:
            assert graph.node_features.shape == (num_nodes, 512 + 2)
        else:
            assert graph.node_features.shape == (num_nodes, 512)

        if add_node_labels:
            assert graph.node_labels.shape == (num_nodes, 4)


@pytest.mark.parametrize("k", [1, 10, 50])
@pytest.mark.parametrize("thresh", [0, 10, 200])
@pytest.mark.parametrize("add_loc_feats", [True, False])
@pytest.mark.parametrize("add_node_labels", [True, False])
@pytest.mark.parametrize("return_networkx", [True, False])
@pytest.mark.parametrize("use_centroids", [True, False])
def test_mst_graph_building(
    k, thresh, add_loc_feats, add_node_labels, return_networkx, use_centroids
):

    if not use_centroids:
        image_size = (1024, 2048)

        instance_map = make_fake_instance_maps(
            num=100, image_size=image_size, ellipse_height=10, ellipse_width=8
        )
        regions = regionprops(instance_map)
        features = torch.randn(len(regions), 512)
        num_nodes = len(regions)

        if add_node_labels:
            annotation = torch.randn(len(regions), 4)
        else:
            annotation = None

        graph_builder = MSTGraphBuilder(
            k=k,
            thresh=thresh,
            add_loc_feats=add_loc_feats,
            return_networkx=return_networkx,
        )

        graph = graph_builder.process(
            instance_map, features, annotation=annotation, target=1
        )

    elif use_centroids:
        centroids = torch.randn(100, 2)
        features = torch.randn(100, 512)
        if add_node_labels:
            annotation = torch.randn(100, 4)
        else:
            annotation = None
        num_nodes = 100

        graph_builder = KNNGraphBuilder(
            k=k,
            thresh=thresh,
            add_loc_feats=add_loc_feats,
            return_networkx=return_networkx,
        )

        graph = graph_builder.process_with_centroids(
            centroids,
            features,
            annotation=annotation,
            image_size=(1000, 1000),
            target=1,
        )

    if return_networkx:
        assert len(graph.nodes) == num_nodes
        if add_loc_feats:
            assert len(graph.nodes[0]["node_features"]) == 514
        else:
            assert len(graph.nodes[0]["node_features"]) == 512
    else:
        assert graph.node_centroids.shape == (num_nodes, 2)
        assert graph.edge_index.shape[0] == 2
        if add_loc_feats:
            assert graph.node_features.shape == (num_nodes, 512 + 2)
        else:
            assert graph.node_features.shape == (num_nodes, 512)

        if add_node_labels:
            assert graph.node_labels.shape == (num_nodes, 4)