This repository has been archived by the owner on Sep 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testt.py
377 lines (283 loc) · 12.7 KB
/
testt.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
from dataClasses import dataLoader
import torch
import torch.nn.functional as F
from torch_geometric.nn import GCNConv, Linear, GATConv
from torch_geometric.nn.pool import EdgePooling
from torch_geometric.data import Data
import numpy as np
import sklearn.metrics as metrics
#Can we transform this to doing edge labelling instead of node?
class GraphConvNNEdge(torch.nn.Module):
archName = "Graph Convolutional Neural Network for Edge Prediction"
def __init__(self, node_features, num_classes):
super().__init__()
self.n_epochs = 10
self.num_classes = num_classes
if self.num_classes == 2: #binary
self.num_classes = 1
hidden_channel = 64
self.conv1 = GCNConv(node_features, hidden_channel)
self.conv2 = GCNConv(hidden_channel+node_features, hidden_channel)
self.conv3 = GCNConv(hidden_channel+node_features, hidden_channel)
self.conv4 = GCNConv(hidden_channel+node_features, hidden_channel)
self.lin1 = Linear(hidden_channel+node_features, hidden_channel)
self.lin2 = Linear(hidden_channel+node_features, out_channels=self.num_classes)
def BCELoss_class_weighted(weights):
def loss(input, target):
input = torch.clamp(input,min=1e-7,max=1-1e-7)
bce = - weights[1] * target * torch.log(input) - \
weights[0] * (1 - target) * torch.log(1 - input)
return torch.mean(bce)
return loss
self.loss_func = BCELoss_class_weighted([500,1])
self.optimizer = torch.optim.Adam(self.parameters(), lr=0.00025)
def forward(self, data):
data = Data(x=data[0], edge_index=data[1].t().contiguous())
x, edge_index = data.x, data.edge_index
#print(x.type())
#print(edge_index.type())
x_in = x.clone()
x = self.conv1(x, edge_index)
x = F.relu(x)
x = torch.cat((x_in, x), -1)
x = self.conv2(x, edge_index)
x = F.relu(x)
x = torch.cat((x_in, x), -1)
x = self.conv3(x, edge_index)
x = F.relu(x)
x = torch.cat((x_in, x), -1)
x = self.conv4(x, edge_index)
x = F.relu(x)
x = F.relu(self.lin1(torch.cat((x_in, x), -1)))
x = self.lin2(torch.cat((x_in, x), -1))
if self.num_classes == 1: #binary
return torch.flatten(torch.sigmoid(x))
return F.log_softmax(x, dim=1)
def train(self, data):
for edx in range(self.n_epochs+1):
pos_f1 = 0.0
neg_f1 = 0.0
tot_lss = 0.0
fpr = 0.0
selection_rate = 0.0
for patient in data:
nodes, edg, class_lbls = patient[0], patient[1], patient[2]
out = self.forward([nodes, edg])
loss = self.loss_func(out, class_lbls)
pos_f1 += metrics.f1_score(class_lbls.cpu().detach().numpy(), torch.round(out).cpu().detach().numpy(), zero_division=0)
neg_f1 += metrics.f1_score(class_lbls.cpu().detach().numpy(), torch.round(out).cpu().detach().numpy(), zero_division=0, pos_label=0)
fpr += torch.sum((class_lbls != torch.round(out))[(class_lbls == 0).nonzero()]) / torch.sum(class_lbls == 0)
selection_rate += torch.sum(torch.round(out)) / out.size(0)
tot_lss += loss.item()
loss.backward()
self.optimizer.step()
pos_f1 = pos_f1 / len(data)
neg_f1 = neg_f1 / len(data)
fpr = fpr / len(data)
selection_rate = selection_rate / len(data)
print(f"Epoch {edx} Positive F1: {pos_f1:.5f}, Negative F1: {neg_f1:.5f}, FPR: {fpr:.5f}, Selection rate: {selection_rate:.4f}")
class test_edgeNN(torch.nn.Module):
def __init__(self, node_features, num_classes):
super().__init__()
self.n_epochs = 10
self.num_classes = num_classes
if self.num_classes == 2: #binary
self.num_classes = 1
hidden_channel = 64
self.lin1 = Linear(node_features*2, hidden_channel)
self.lin2 = Linear(hidden_channel+node_features*2, hidden_channel)
self.lin3 = Linear(hidden_channel+node_features*2, hidden_channel)
self.linout = Linear(hidden_channel+node_features*2, out_channels=self.num_classes)
def BCELoss_class_weighted(weights):
def loss(input, target):
input = torch.clamp(input,min=1e-7,max=1-1e-7)
bce = - weights[1] * target * torch.log(input) - \
weights[0] * (1 - target) * torch.log(1 - input)
return torch.mean(bce)
return loss
self.loss_func = BCELoss_class_weighted([500,1])
self.optimizer = torch.optim.Adam(self.parameters(), lr=0.00025)
#Here, x is two nodes that have an edge
def forward(self, x):
x_in = x.clone()
x = F.relu(self.lin1(x))
x = torch.cat((x, x_in), -1)
x = F.relu(self.lin2(x))
x = torch.cat((x, x_in), -1)
x = F.relu(self.lin3(x))
x = torch.cat((x, x_in), -1)
x = self.linout(x)
return torch.flatten(torch.sigmoid(x))
def train(self, data):
for edx in range(self.n_epochs+1):
pos_f1 = 0.0
neg_f1 = 0.0
tot_lss = 0.0
fpr = 0.0
selection_rate = 0.0
for patient in data:
nodes, edg, labels = patient[0], patient[1].T, patient[2]
nodes_left = labels[edg[0]] #Get the nodes' labels
nodes_right = labels[edg[1]]
class_lbls = (nodes_left == nodes_right).type(torch.float)
e = torch.cat([nodes[edg[0]], nodes[edg[1]]], dim=-1)
out = self.forward(e)
loss = self.loss_func(out, class_lbls)
pos_f1 += metrics.f1_score(class_lbls.cpu().detach().numpy(), torch.round(out).cpu().detach().numpy(), zero_division=0)
neg_f1 += metrics.f1_score(class_lbls.cpu().detach().numpy(), torch.round(out).cpu().detach().numpy(), zero_division=0, pos_label=0)
fpr += torch.sum((class_lbls != torch.round(out))[(class_lbls == 0).nonzero()]) / torch.sum(class_lbls == 0)
selection_rate += torch.sum(torch.round(out)) / out.size(0)
tot_lss += loss.item()
loss.backward()
self.optimizer.step()
pos_f1 = pos_f1 / len(data)
neg_f1 = neg_f1 / len(data)
fpr = fpr / len(data)
selection_rate = selection_rate / len(data)
print(f"Epoch {edx} Positive F1: {pos_f1:.5f}, Negative F1: {neg_f1:.5f}, FPR: {fpr:.5f}, Selection rate: {selection_rate:.4f}")
dl = dataLoader(processed=False, asBinary=True)
tensors = dl.get_torch_data()
clf = test_edgeNN(tensors[0][0].size(1), 2)
clf.train(tensors)
#Lets try and flip the edges and nodes: each edge becomes the concatenation of the two nodes, each node determining to which edges they connect
new_nodes = []
new_edges = []
new_labels = []
new_tensors = []
for patient in tensors:
nodes, edges, labels, pname = patient
print(pname)
edges = edges.T
nodes_left = labels[edges[0]] #Get the nodes' labels
nodes_right = labels[edges[1]]
class_lbls = (nodes_left == nodes_right).type(torch.float)
n_nodes = torch.cat([nodes[edges[0]], nodes[edges[1]]], dim=-1) #Create the new nodes as a concatenation of the two participating in an edge
n_edges = [[], []]
for edge_id, e in enumerate(edges.T):
out_node, inc_node = e[0], e[1]
related_edges_left = torch.cat(((edges[0] == out_node).nonzero().flatten(), (edges[1] == out_node).nonzero().flatten())) #Get the edges attached to the left node
related_edges_right = torch.cat(((edges[0] == inc_node).nonzero().flatten(), (edges[1] == inc_node).nonzero().flatten())) #Get the eges attached to the right node
related_edges = torch.unique(torch.cat((related_edges_left, related_edges_right))).tolist()
n_edges[0].extend(edge_id for _ in range(len(related_edges)))
n_edges[1].extend(related_edges)
new_tensors.append([n_nodes, torch.tensor(n_edges, dtype=torch.long).T, class_lbls])
#new_nodes.append(n_nodes)
#new_edges.append(torch.tensor(n_edges, dtype=torch.long).T)
#new_labels.append(class_lbls)
#print(new_nodes[-1].size())
#print(new_edges[-1].size())
#print(new_labels[-1].size())
clf = GraphConvNNEdge(new_tensors[0][0].size(1), 2)
#print(new_tensors[0][0].T.type())
clf.train(new_tensors[:-10])
x = new_tensors[-1][0]
e = new_tensors[-1][1]
e = torch.cat([x[e[0]], x[e[1]]], dim=-1)
example = clf(e)
epool = EdgePooling(tensors[0][0].size(1))
batch = torch.tensor(np.zeros(x.shape[0])).long().to(x.get_device())
x, edge_index, batch, unpool1 = epool.__merge_edges_sigmoid__(x, e, batch, example)
dl.create_cluster_vis(unpool1.cluster_map, tensors[-1][-1], "Learned Edge Pool labels example")
exit()
error_cluster_sizes = [0 for x in range(30)]
def get_roots(nodes, edges):
roots = []
for node_idx, n in enumerate(nodes):
recipients = edges[1][(edges[0] == node_idx)].tolist()
is_root = True
if node_idx in recipients: recipients.remove(node_idx)
if len(recipients) > 1:
continue
elif nodes[recipients[0]][5] < n[5]: #If we are connected to a node with a lower predecessor label
continue
roots.append(node_idx)
return roots
def get_segment(node, predecessor, edges):
seg = []
recipients = True
while recipients:
recipients = edges[1][(edges[0] == node)].tolist()
if node in recipients: #No self loops
recipients.remove(node)
if predecessor in recipients: #Don't go backwards
recipients.remove(predecessor)
if len(recipients) == 1: #Append it
seg.append(node)
predecessor = node
node = recipients[0]
elif len(recipients) > 1: #Bifurcation
sub_segs = []
for r in recipients:
res = get_segment(r, node, edges)
sub_segs.append(res)
break
return seg
def fix_noise(segments, seen_green=False):
stop = 0
for item in segments:
if type(item) == list: #new part
break
stop += 1
section = segments[:stop]
length = 0
cur_nodes = []
for node in section:
if labels[node] == 0:
length += 1
cur_nodes.append(node)
else:
if length == 0: #Skip to the first 1 label
continue
if seen_green:
labels[cur_nodes] = 1 #Fix the labels
cur_nodes = []
else:
seen_green = True
length = 0
del segments[:stop]
for lists in segments:
fix_noise(lists, seen_green=seen_green)
def check_succesion(segments, nodes):
r = 0
stop = 0
for item in segments:
if type(item) == list: #new part
break
stop += 1
section = segments[:stop]
for i, node_idx in enumerate(section[1:]):
if nodes[node_idx][4] <= nodes[section[i-1]][4]:
r += 1
del segments[:stop]
for lists in segments:
r += check_succesion(lists)
return r
"""for patient in tensors:
nodes = patient[0]
edges = patient[1].T
labels = patient[2]
roots = get_roots(nodes, edges)
print(patient[3])
#print(nodes[roots].T[4:6].T)
for r in roots:
# print(nodes[r])
if nodes[r][5] != 0:
print("Root starting at: ", nodes[r][5])
#break
#print(len(roots) / len(nodes))
#print((labels == 1).nonzero().flatten().size())
for root_idx in roots: #Now get all of the segments
#print("hi")
segments = get_segment(root_idx, None, edges)
cnt = check_succesion(segments, nodes)
if cnt > 0:
print("Succesion feature noise: ", cnt, ". Proportionality: ", cnt/nodes.size(0))
#noises = get_noise(segments)
#for n in noises:
# print("Noise of length: ", n)
# error_cluster_sizes[n] += 1
break"""
#print(error_cluster_sizes)
#import matplotlib.pyplot as plt
#plt.bar(list(range(len(error_cluster_sizes))), error_cluster_sizes)
#plt.show()