-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_helpers.py
352 lines (293 loc) · 11.9 KB
/
data_helpers.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
from __future__ import print_function
import random
import timeit
from collections import Counter
import networkx as nx
import igraph as ig
import numpy as np
import sys, pickle, logging
import copy, time
from nystroem import Nystroem
import graph_of_words as gow
from gensim.models import KeyedVectors
np.random.seed(None)
def load_data(ds_name, use_node_labels, data_type='text'):
Gs = []
labels = []
cats = {}
if data_type == 'text':
# encodings
if ds_name == 'bbc':
cats = {
'business': 1,
'entertainment': 2,
'politics': 3,
'sport': 4,
'tech': 5
}
elif ds_name == 'bbcsport':
cats = {
'athletics': 1,
'cricket': 2,
'football': 3,
'rugby': 4,
'tennis': 5
}
Gs, labels = gow.docs_to_networkx(ds_name, cats)
else:
node2graph = {}
ds_name_ = ds_name.split('/')[0]
with open("./datasets/%s/%s_graph_indicator.txt" % (ds_name, ds_name_), "r") as f:
c = 1
for line in f:
node2graph[c] = int(line[:-1])
if not node2graph[c] == len(Gs):
Gs.append(nx.Graph())
Gs[-1].add_node(c)
c += 1
with open("./datasets/%s/%s_A.txt" % (ds_name, ds_name_), "r") as f:
for line in f:
edge = line[:-1].split(",")
edge[1] = edge[1].replace(" ", "")
Gs[node2graph[int(edge[0])] - 1].add_edge(int(edge[0]), int(edge[1]))
if use_node_labels:
with open("./datasets/%s/%s_node_labels.txt" % (ds_name, ds_name_), "r") as f:
c = 1
for line in f:
node_label = int(line[:-1])
Gs[node2graph[c] - 1].node[c]['label'] = node_label
c += 1
with open("./datasets/%s/%s_graph_labels.txt" % (ds_name, ds_name_), "r") as f:
for line in f:
if line is None or line[:-1] == '':
continue
labels.append(int(line[:-1]))
labels = np.array(labels, dtype=np.float)
return Gs, labels
def load_embeddings(fname='embeddings/GoogleNews-vectors-negative300.bin.gz', fvocab=None, as_dict=True):
model = KeyedVectors.load_word2vec_format(fname=fname, fvocab=fvocab, binary=True)
if as_dict:
word_vecs = {}
for word in model.wv.vocab:
vec = model.wv[word]
word_vecs[word] = vec
return word_vecs
else:
return model
def load_embeddings_from_db(db='embeddings/word2vec.db', vocab=None):
import db
db.connect()
model = db.select(vocab)
if vocab is None:
return model
else:
vmodel = {}
for w in vocab:
vmodel[w] = model[w]
return model
def add_unknown_words(word_vecs, vocab, k=300):
# For words not existing in the pretrained dataset, create a separate word vector.
for word in vocab:
if word not in word_vecs:
word_vecs[word] = np.random.uniform(-0.25, 0.25, k)
def networkx_to_igraph(G):
mapping = dict(zip(G.nodes(), range(G.number_of_nodes())))
reverse_mapping = dict(zip(range(G.number_of_nodes()), G.nodes()))
G = nx.relabel_nodes(G, mapping)
G_ig = ig.Graph(len(G), list(zip(*list(zip(*nx.to_edgelist(G)))[:2])))
return G_ig, reverse_mapping
def neighbors_community(G):
communities = []
for v in G.nodes():
community = G.neighbors(v)
community.append(v)
communities.append(community)
return communities
def neighbors2_community(G, remove_duplicates=True, use_kcore=False):
Gc = None
if use_kcore:
Gc = G.copy()
Gc.remove_edges_from(Gc.selfloop_edges())
Gc = nx.k_core(Gc, 3)
# Gc = [cl for cl in nx.find_cliques(G)]
else:
Gc = G
communities = set()
for v in Gc.nodes():
neighs = G.neighbors(v)
community = []
for n in neighs:
community.append(n)
neighs2 = G.neighbors(n)
community.extend(neighs2)
if remove_duplicates:
community = list(set(community))
communities.add(tuple(community))
communities = list(map(list, communities)) # Convert tuples back into lists
return communities
def community_detection(G_networkx, community_detection_method):
if community_detection_method == "neighbors":
communities = neighbors_community(G_networkx)
return communities
if community_detection_method == "neighbors2":
communities = neighbors2_community(G_networkx)
return communities
G, reverse_mapping = networkx_to_igraph(G_networkx)
if community_detection_method == "eigenvector":
c = G.community_leading_eigenvector()
elif community_detection_method == "infomap":
c = G.community_infomap()
elif community_detection_method == "fastgreedy":
c = G.community_fastgreedy().as_clustering()
elif community_detection_method == "label_propagation":
c = G.community_label_propagation()
elif community_detection_method == "louvain":
c = G.community_multilevel()
elif community_detection_method == "spinglass":
c = G.community_spinglass()
elif community_detection_method == "walktrap":
c = G.community_walktrap().as_clustering()
elif community_detection_method == 'multilevel':
c = G.community_multilevel()
elif community_detection_method == 'edge_betweenness':
c = G.community_edge_betweenness().as_clustering()
else:
c = []
communities = []
for i in range(len(c)):
community = []
for j in range(len(c[i])):
community.append(reverse_mapping[G.vs[c[i][j]].index])
communities.append(community)
return communities
def compute_communities(graphs, use_node_labels, community_detection_method):
communities = []
subgraphs = []
counter = 0
coms = []
for G in graphs:
c = community_detection(G, community_detection_method)
coms.append(len(c))
subgraph = []
for i in range(len(c)):
communities.append(G.subgraph(c[i]))
subgraph.append(counter)
counter += 1
subgraphs.append(' '.join(str(s) for s in subgraph))
print("Average communities: ", np.mean(coms))
return communities, subgraphs
def compute_embeddings(ds_name, use_node_labels, community_detection_method, kernels):
graphs, labels = load_data(ds_name, use_node_labels)
communities, subgraphs = compute_communities(graphs, use_node_labels, community_detection_method)
Q = []
for idx, k in enumerate(kernels):
Q_t = k(communities, explicit=True)
dim = Q_t.shape[1]
Q_t = np.vstack([np.zeros(dim), Q_t])
Q.append(Q_t)
return Q, subgraphs, labels, Q_t.shape
def compute_kernel(ds_name, use_node_labels, community_detection_method):
graphs, labels = load_data(ds_name, use_node_labels)
communities, subgraphs = compute_communities(graphs, use_node_labels, community_detection_method)
if use_node_labels:
from graph_kernels_labeled import sp_kernel, graphlet_kernel, wl_kernel, pm_kernel
else:
from graph_kernels import sp_kernel, graphlet_kernel, wl_kernel, pm_kernel
K = wl_kernel(communities)
return K, subgraphs, labels
def generate_data(ds_name):
graphs = []
labels = []
ni = np.random.binomial(n=300, p=0.5, size=100000)
ei = np.random.binomial(n=3, p=0.3, size=100000)
for i in range(2000):
n = random.uniform(130, 180)
e = random.uniform(0.15, 0.45)
g1 = nx.fast_gnp_random_graph(n, e)
graphs.append(g1)
labels.append(1)
n = random.uniform(130, 180)
e = random.uniform(0.15, 0.45)
n = random.uniform(130, 180)
e = random.uniform(0.15, 0.45)
pass
def compute_nystroem(ds_name, use_node_labels, embedding_dim, community_detection_method, kernels):
start = time.time()
graphs, labels = load_data(ds_name, use_node_labels)
# graphs, labels = generate_data(ds_name)
end = time.time()
time2 = end - start
communities, subgraphs = compute_communities(graphs, use_node_labels, community_detection_method)
print("Total communities: ", len(communities))
lens = []
for community in communities:
lens.append(community.number_of_nodes())
print("Average size: ", np.mean(lens))
Q = []
for idx, k in enumerate(kernels):
model = Nystroem(k, n_components=embedding_dim)
model.fit(communities)
Q_t = model.transform(communities)
Q_t = np.vstack([np.zeros(embedding_dim), Q_t])
Q.append(Q_t)
return Q, subgraphs, labels, Q_t.shape, time2
def batch_iter(data, batch_size, num_epochs, shuffle=True):
"""
Generates a batch iterator for a dataset.
"""
data = np.array(data)
data_size = len(data)
num_batches_per_epoch = int((len(data) - 1) / batch_size) + 1
for epoch in range(num_epochs):
# Shuffle the data at each epoch
if shuffle:
shuffle_indices = np.random.permutation(np.arange(data_size))
shuffled_data = data[shuffle_indices]
else:
shuffled_data = data
for batch_num in range(num_batches_per_epoch):
start_index = batch_num * batch_size
end_index = min((batch_num + 1) * batch_size, data_size)
yield shuffled_data[start_index:end_index]
# def create_train_test_loaders(Q, x_train, x_test, y_train, y_test, batch_size):
# num_kernels = Q.shape[2]
# max_document_length = x_train.shape[1]
# dim = Q.shape[1]
#
# my_x = []
# for i in range(x_train.shape[0]):
# temp = np.zeros((1, num_kernels, max_document_length, dim))
# for j in range(num_kernels):
# for k in range(x_train.shape[1]):
# temp[0, j, k, :] = Q[x_train[i, k], :, j].squeeze()
# my_x.append(temp)
#
# if torch.cuda.is_available():
# tensor_x = torch.stack([torch.cuda.FloatTensor(i) for i in my_x]) # transform to torch tensors
# tensor_y = torch.cuda.LongTensor(y_train.tolist())
# else:
# tensor_x = torch.stack([torch.Tensor(i) for i in my_x]) # transform to torch tensors
# tensor_y = torch.from_numpy(np.asarray(y_train, dtype=np.int64))
#
# train_dataset = utils.TensorDataset(tensor_x, tensor_y)
# train_loader = utils.DataLoader(train_dataset, batch_size=batch_size, shuffle=True)
#
# my_x = []
# for i in range(x_test.shape[0]):
# temp = np.zeros((1, num_kernels, max_document_length, dim))
# for j in range(num_kernels):
# for k in range(x_test.shape[1]):
# temp[0, j, k, :] = Q[x_test[i, k], :, j].squeeze()
# my_x.append(temp)
#
# if torch.cuda.is_available():
# tensor_x = torch.stack([torch.cuda.FloatTensor(i) for i in my_x]) # transform to torch tensors
# tensor_y = torch.cuda.LongTensor(y_test.tolist())
# else:
# tensor_x = torch.stack([torch.Tensor(i) for i in my_x]) # transform to torch tensors
# tensor_y = torch.from_numpy(np.asarray(y_test, dtype=np.int64))
#
# test_dataset = utils.TensorDataset(tensor_x, tensor_y)
# test_loader = utils.DataLoader(test_dataset, batch_size=1, shuffle=False)
#
# return train_loader, test_loader