-
Notifications
You must be signed in to change notification settings - Fork 12
/
utils.py
executable file
·267 lines (225 loc) · 8.95 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
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
import numpy as np
import networkx as nx
import scipy.sparse as sp
import torch
import torch.nn as nn
import scipy.io as sio
import random
import dgl
###############################################
# Forked from GRAND-Lab/CoLA #
###############################################
def parse_skipgram(fname):
with open(fname) as f:
toks = list(f.read().split())
nb_nodes = int(toks[0])
nb_features = int(toks[1])
ret = np.empty((nb_nodes, nb_features))
it = 2
for i in range(nb_nodes):
cur_nd = int(toks[it]) - 1
it += 1
for j in range(nb_features):
cur_ft = float(toks[it])
ret[cur_nd][j] = cur_ft
it += 1
return ret
# Process a (subset of) a TU dataset into standard form
def process_tu(data, nb_nodes):
nb_graphs = len(data)
ft_size = data.num_features
features = np.zeros((nb_graphs, nb_nodes, ft_size))
adjacency = np.zeros((nb_graphs, nb_nodes, nb_nodes))
labels = np.zeros(nb_graphs)
sizes = np.zeros(nb_graphs, dtype=np.int32)
masks = np.zeros((nb_graphs, nb_nodes))
for g in range(nb_graphs):
sizes[g] = data[g].x.shape[0]
features[g, :sizes[g]] = data[g].x
labels[g] = data[g].y[0]
masks[g, :sizes[g]] = 1.0
e_ind = data[g].edge_index
coo = sp.coo_matrix((np.ones(e_ind.shape[1]), (e_ind[0, :], e_ind[1, :])), shape=(nb_nodes, nb_nodes))
adjacency[g] = coo.todense()
return features, adjacency, labels, sizes, masks
def micro_f1(logits, labels):
# Compute predictions
preds = torch.round(nn.Sigmoid()(logits))
# Cast to avoid trouble
preds = preds.long()
labels = labels.long()
# Count true positives, true negatives, false positives, false negatives
tp = torch.nonzero(preds * labels).shape[0] * 1.0
tn = torch.nonzero((preds - 1) * (labels - 1)).shape[0] * 1.0
fp = torch.nonzero(preds * (labels - 1)).shape[0] * 1.0
fn = torch.nonzero((preds - 1) * labels).shape[0] * 1.0
# Compute micro-f1 score
prec = tp / (tp + fp)
rec = tp / (tp + fn)
f1 = (2 * prec * rec) / (prec + rec)
return f1
"""
Prepare adjacency matrix by expanding up to a given neighbourhood.
This will insert loops on every node.
Finally, the matrix is converted to bias vectors.
Expected shape: [graph, nodes, nodes]
"""
def adj_to_bias(adj, sizes, nhood=1):
nb_graphs = adj.shape[0]
mt = np.empty(adj.shape)
for g in range(nb_graphs):
mt[g] = np.eye(adj.shape[1])
for _ in range(nhood):
mt[g] = np.matmul(mt[g], (adj[g] + np.eye(adj.shape[1])))
for i in range(sizes[g]):
for j in range(sizes[g]):
if mt[g][i][j] > 0.0:
mt[g][i][j] = 1.0
return -1e9 * (1.0 - mt)
###############################################
# Forked from tkipf/gcn #
###############################################
def parse_index_file(filename):
"""Parse index file."""
index = []
for line in open(filename):
index.append(int(line.strip()))
return index
def sample_mask(idx, l):
"""Create mask."""
mask = np.zeros(l)
mask[idx] = 1
return np.array(mask, dtype=np.bool)
def sparse_to_tuple(sparse_mx, insert_batch=False):
"""Convert sparse matrix to tuple representation."""
"""Set insert_batch=True if you want to insert a batch dimension."""
def to_tuple(mx):
if not sp.isspmatrix_coo(mx):
mx = mx.tocoo()
if insert_batch:
coords = np.vstack((np.zeros(mx.row.shape[0]), mx.row, mx.col)).transpose()
values = mx.data
shape = (1,) + mx.shape
else:
coords = np.vstack((mx.row, mx.col)).transpose()
values = mx.data
shape = mx.shape
return coords, values, shape
if isinstance(sparse_mx, list):
for i in range(len(sparse_mx)):
sparse_mx[i] = to_tuple(sparse_mx[i])
else:
sparse_mx = to_tuple(sparse_mx)
return sparse_mx
def standardize_data(f, train_mask):
"""Standardize feature matrix and convert to tuple representation"""
# standardize data
f = f.todense()
mu = f[train_mask == True, :].mean(axis=0)
sigma = f[train_mask == True, :].std(axis=0)
f = f[:, np.squeeze(np.array(sigma > 0))]
mu = f[train_mask == True, :].mean(axis=0)
sigma = f[train_mask == True, :].std(axis=0)
f = (f - mu) / sigma
return f
def preprocess_features(features):
"""Row-normalize feature matrix and convert to tuple representation"""
rowsum = np.array(features.sum(1))
r_inv = np.power(rowsum, -1).flatten()
r_inv[np.isinf(r_inv)] = 0.
r_mat_inv = sp.diags(r_inv)
features = r_mat_inv.dot(features)
return features.todense(), sparse_to_tuple(features)
def normalize_adj(adj):
"""Symmetrically normalize adjacency matrix."""
adj = sp.coo_matrix(adj)
rowsum = np.array(adj.sum(1))
d_inv_sqrt = np.power(rowsum, -0.5).flatten()
d_inv_sqrt[np.isinf(d_inv_sqrt)] = 0.
d_mat_inv_sqrt = sp.diags(d_inv_sqrt)
return adj.dot(d_mat_inv_sqrt).transpose().dot(d_mat_inv_sqrt).tocoo()
def preprocess_adj(adj):
"""Preprocessing of adjacency matrix for simple GCN model and conversion to tuple representation."""
adj_normalized = normalize_adj(adj + sp.eye(adj.shape[0]))
return sparse_to_tuple(adj_normalized)
def sparse_mx_to_torch_sparse_tensor(sparse_mx):
"""Convert a scipy sparse matrix to a torch sparse tensor."""
sparse_mx = sparse_mx.tocoo().astype(np.float32)
indices = torch.from_numpy(
np.vstack((sparse_mx.row, sparse_mx.col)).astype(np.int64))
values = torch.from_numpy(sparse_mx.data)
shape = torch.Size(sparse_mx.shape)
return torch.sparse.FloatTensor(indices, values, shape)
def adj_to_dict(adj,hop=1,min_len=8):
adj = np.array(adj.todense(),dtype=np.float64)
num_node = adj.shape[0]
# adj += np.eye(num_node)
adj_diff = adj
if hop > 1:
for _ in range(hop - 1):
adj_diff = adj_diff.dot(adj)
dict = {}
for i in range(num_node):
dict[i] = []
for j in range(num_node):
if adj_diff[i,j] > 0:
dict[i].append(j)
final_dict = dict.copy()
for i in range(num_node):
while len(final_dict[i]) < min_len:
final_dict[i].append(random.choice(dict[random.choice(dict[i])]))
return dict
def dense_to_one_hot(labels_dense, num_classes):
"""Convert class labels from scalars to one-hot vectors."""
num_labels = labels_dense.shape[0]
index_offset = np.arange(num_labels) * num_classes
labels_one_hot = np.zeros((num_labels, num_classes))
labels_one_hot.flat[index_offset+labels_dense.ravel()] = 1
return labels_one_hot
def load_mat(dataset, train_rate=0.3, val_rate=0.1):
data = sio.loadmat("./dataset/{}.mat".format(dataset))
label = data['Label'] if ('Label' in data) else data['gnd']
attr = data['Attributes'] if ('Attributes' in data) else data['X']
network = data['Network'] if ('Network' in data) else data['A']
adj = sp.csr_matrix(network)
feat = sp.lil_matrix(attr)
labels = np.squeeze(np.array(data['Class'],dtype=np.int64) - 1)
num_classes = np.max(labels) + 1
labels = dense_to_one_hot(labels,num_classes)
ano_labels = np.squeeze(np.array(label))
if 'str_anomaly_label' in data:
str_ano_labels = np.squeeze(np.array(data['str_anomaly_label']))
attr_ano_labels = np.squeeze(np.array(data['attr_anomaly_label']))
else:
str_ano_labels = None
attr_ano_labels = None
num_node = adj.shape[0]
num_train = int(num_node * train_rate)
num_val = int(num_node * val_rate)
all_idx = list(range(num_node))
random.shuffle(all_idx)
idx_train = all_idx[ : num_train]
idx_val = all_idx[num_train : num_train + num_val]
idx_test = all_idx[num_train + num_val : ]
return adj, feat, labels, idx_train, idx_val, idx_test, ano_labels, str_ano_labels, attr_ano_labels
def adj_to_dgl_graph(adj):
nx_graph = nx.from_scipy_sparse_matrix(adj)
dgl_graph = dgl.DGLGraph(nx_graph)
return dgl_graph
def generate_rwr_subgraph(dgl_graph, subgraph_size):
all_idx = list(range(dgl_graph.number_of_nodes()))
reduced_size = subgraph_size - 1
traces = dgl.contrib.sampling.random_walk_with_restart(dgl_graph, all_idx, restart_prob=1, max_nodes_per_seed=subgraph_size*3)
subv = []
for i,trace in enumerate(traces):
subv.append(torch.unique(torch.cat(trace),sorted=False).tolist())
retry_time = 0
while len(subv[i]) < reduced_size:
cur_trace = dgl.contrib.sampling.random_walk_with_restart(dgl_graph, [i], restart_prob=0.9, max_nodes_per_seed=subgraph_size*5)
subv[i] = torch.unique(torch.cat(cur_trace[0]),sorted=False).tolist()
retry_time += 1
if (len(subv[i]) <= 2) and (retry_time >10):
subv[i] = (subv[i] * reduced_size)
subv[i] = subv[i][:reduced_size]
subv[i].append(i)
return subv