-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
197 lines (178 loc) · 6.27 KB
/
utils.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
import matplotlib.pyplot as plt
import numpy as np
import re
from sklearn import metrics
from sklearn.metrics import f1_score
from sklearn.metrics import precision_recall_fscore_support as score
from sklearn.utils import resample
from torch_geometric.data import Data
from torch_geometric.data import DataLoader as gDataLoader
import scipy.spatial as spt
import torch
import torch_geometric
import torch_geometric.data as gdata
import torch_geometric.utils as gutils
import networkx as nx
from config import Config
config = Config()
def filter_positions(candidate_position):
"""
Remove some redundant positions
Args:
candidate_position: all possible positions
"""
cdd_position_set = set()
x,y,z = candidate_position
for item in zip(x, y):
cdd_position_set.add(item)
cdd_position_set = list(cdd_position_set)
new_x = []
new_y = []
for item_pos in cdd_position_set:
new_x.append(item_pos[0])
new_y.append(item_pos[1])
new_x = np.array(new_x)
new_y = np.array(new_y)
return new_x, new_y
def plot_graph(a_graph):
"""
plot the topology structure of a graph
Args:
a_graph: the graph need to be visualized
"""
g_nx = gutils.to_networkx(a_graph,to_undirected=True)
labeldict = {list(g_nx.nodes())[i]:str(a_graph.y[i].numpy()) for i in range(len(a_graph.y))}
nx.draw_kamada_kawai(g_nx, labels=labeldict, with_labels=True)
print(str(a_graph.graph_y.numpy()))
plt.show()
def get_dataset(raw_array, label_array, wsi_id, wsi_label):
"""
Generate graph data from raw data
Args:
raw_array: a matrix of patches' features extracted by ResNet
label_array: a matrix of patches' labels
wsi_id: Whole Slide Image ID
wsi_label: Whole Slide Image Label
"""
candidate_position = np.nonzero(raw_array)
pos_x, pos_y = filter_positions(candidate_position)
num_of_nodes = len(pos_x)
points = [ [pos_x[i], pos_y[i]] for i in range(num_of_nodes)]
nodes = np.array(
[raw_array[points[i][0]][points[i][1]] for i in range(len(points))])
node_labels = np.array(
[label_array[points[i][0]][points[i][1]] for i in range(len(points))])
node_labels = torch.from_numpy(node_labels).long()
pos = np.array([[points[i][0],points[i][1]] for i in range(len(points))])
pos = torch.from_numpy(pos)
if len(nodes.shape) < 2:
nodes = np.expand_dims(nodes, axis=1)
nodes = torch.from_numpy(nodes)
edges = []
attrs = []
label = torch.from_numpy(np.array(wsi_label).reshape(1))
slide_idx = wsi_id
ckt = spt.cKDTree(points)
for point_idx in range(len(points)):
d, x = ckt.query(points[point_idx],k=5)
d = d[1:]
x = x[1:]
min_dis_index = d==min(d)
edges.extend([[point_idx,x_idx] for x_idx in x])
attrs.extend([(1/dist) for dist in d])
edges = torch.from_numpy(np.transpose(np.array(edges)))
attrs = torch.from_numpy(np.array(attrs).reshape(-1,1))
a_graph = Data(
x=nodes.float(),
edge_index=edges,
edge_attr=attrs.squeeze(1).float(),
y=node_labels,
pos=pos,
graph_y=label,
slide_index=slide_idx)
return a_graph
def boots_ci(y_true, y_pred, bs_times=10000):
"""
Calculate confidence interval using bootstrap
Args:
y_true: Labels
y_pred: Predictions
bs_times: sample times
"""
alpha = 5.0
num_observations = len(y_true)
np.random.seed(1)
metrics_list = [0, 1, 2]
output_list = []
for metrc in metrics_list:
f_score_list = []
for i in range(bs_times):
indices = resample(range(num_observations),n_samples=num_observations)
bs_true = y_true[indices]
bs_pred = y_pred[indices]
f_score_list.append(score(bs_true, bs_pred)[metrc])
f_scores = np.array(f_score_list)
avg = np.median(f_scores, axis=0)
lower_p = alpha / 2.0
upper_p = (100 - alpha) + (alpha / 2.0)
lower = np.percentile(f_scores, lower_p,axis=0)
upper = np.percentile(f_scores, upper_p,axis=0)
output_list.append([avg, lower, upper])
return output_list
def auc_ci(y_true, y_score, bs_times=10000):
"""
Calculate confidence interval of AUC using boostrap
Args:
y_true: Labels
y_score: Probabilities
bs_times: sample times
"""
alpha = 5.0
num_observations = len(y_true)
np.random.seed(1)
output_list = []
num_cls = y_score.shape[1]
for a_cls in range(num_cls):
f_score_list = []
for i in range(bs_times):
indices = resample(range(num_observations), n_samples=num_observations)
bs_true = y_true[indices]
bs_score = y_score[indices, a_cls]
fpr, tpr, thresholds = metrics.roc_curve(
[item for item in bs_true], bs_score, pos_label=a_cls)
f_score_list.append(metrics.auc(fpr, tpr))
f_score_list = [item for item in f_score_list]
f_scores = np.array(f_score_list)
lower_p = alpha / 2.0
upper_p = (100 - alpha) + (alpha / 2.0)
fpr, tpr, thresholds = metrics.roc_curve(
[item for item in y_true], y_score[:, a_cls], pos_label=a_cls)
avg = metrics.auc(fpr, tpr)
lower = np.percentile(f_scores, lower_p, axis=0)
upper = np.percentile(f_scores, upper_p, axis=0)
output_list.append([avg, lower, upper])
return output_list
def boots_fscores(y_true, y_pred, bs_times=10000):
"""
Calculate confidence interval of f scores using boostrap
Args:
y_true: Labels
y_pred: Predictions
bs_times: sample times
"""
num_observations = len(y_true)
np.random.seed(1)
f_neo_list = []
f_neg_list = []
f_pos_list = []
f_all_list = []
for i in range(bs_times):
indices = resample(range(num_observations), n_samples=num_observations)
bs_true = y_true[indices]
bs_pred = y_pred[indices]
f_neo_list.append(score(bs_true, bs_pred)[2][0])
f_neg_list.append(score(bs_true, bs_pred)[2][1])
f_pos_list.append(score(bs_true, bs_pred)[2][2])
f_all_list.append(f1_score(bs_true, bs_pred, average='macro'))
f_scores = [f_neo_list, f_neg_list, f_pos_list, f_all_list]
return f_scores