-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
354 lines (295 loc) · 11.7 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
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
import numpy as np
import pickle as pkl
import scipy.sparse as sp
from scipy.sparse.linalg.eigen.arpack import eigsh
import sys
import random
import re
import re
from io import StringIO
import tokenize
# import sparse
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 load_data(dataset_str, format="uni"):
"""
Loads input data from gcn/data directory
ind.dataset_str.x => the feature vectors and adjacency matrix of the training instances as list;
ind.dataset_str.tx => the feature vectors and adjacency matrix of the test instances as list;
ind.dataset_str.vx => the feature vectors and adjacency matrix of the validation instances as list;
ind.dataset_str.y => the labels of the labeled training instances as numpy.ndarray object;
ind.dataset_str.ty => the labels of the test instances as numpy.ndarray object;
ind.dataset_str.vy => the labels of the validation instances as numpy.ndarray object;
All objects above must be saved using python pickle module.
:param dataset_str: Dataset name
:return: All data input files loaded (as well the training/test data).
"""
view = ""
if format == "idx":
view = ".idx"
names = ['x_adj' + view, 'x_embed' + view, 'y' + view, 'tx_adj' + view, 'tx_embed' + view,
'ty' + view, 'vx_adj' + view, 'vx_embed' + view, 'vy' + view]
objects = []
for i in range(len(names)):
with open("data/ind.{}.{}".format(dataset_str, names[i]), 'rb') as f:
if sys.version_info > (3, 0):
objects.append(pkl.load(f, encoding='latin1'))
else:
objects.append(pkl.load(f))
x_adj, x_embed, y, tx_adj, tx_embed, ty, vx_adj, vx_embed, vy = tuple(objects)
# train_idx_ori = parse_index_file("data/{}.train.index".format(dataset_str))
# train_size = len(train_idx_ori)
train_adj = []
train_embed = []
val_adj = []
val_embed = []
test_adj = []
test_embed = []
for i in range(len(y)):
adj = x_adj[i].toarray()
embed = np.array(x_embed[i])
train_adj.append(adj)
train_embed.append(embed)
for i in range(len(vy)):
adj = vx_adj[i].toarray()
embed = np.array(vx_embed[i])
val_adj.append(adj)
val_embed.append(embed)
for i in range(len(ty)):
adj = tx_adj[i].toarray()
embed = np.array(tx_embed[i])
test_adj.append(adj)
test_embed.append(embed)
# train_adj = np.array(train_adj)
# val_adj = np.array(val_adj)
# test_adj = np.array(test_adj)
# train_embed = np.array(train_embed)
# val_embed = np.array(val_embed)
# test_embed = np.array(test_embed)
train_y = np.array(y)
val_y = np.array(vy)
test_y = np.array(ty)
return train_adj, train_embed, train_y, val_adj, val_embed, val_y, test_adj, test_embed, test_y
def sparse_to_tuple(sparse_mx):
"""Convert sparse matrix to tuple representation."""
def to_tuple(mx):
if not sp.isspmatrix_coo(mx):
mx = mx.tocoo()
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 coo_to_tuple(sparse_coo):
return (sparse_coo.coords.T, sparse_coo.data, sparse_coo.shape)
def preprocess_features(features):
"""Row-normalize feature matrix and convert to tuple representation"""
max_length = max([len(f) for f in features])
for i in range(len(features)):
feature = np.array(features[i])
pad = max_length - feature.shape[0] # padding for each epoch
feature = np.pad(feature, ((0, pad), (0, 0)), mode='constant')
features[i] = feature
return np.array(list(features))
def normalize_adj(adj):
"""Symmetrically normalize adjacency matrix."""
rowsum = np.array(adj.sum(1))
with np.errstate(divide='ignore'):
d_inv_sqrt = np.power(rowsum, -0.5).flatten()
d_inv_sqrt[np.isinf(d_inv_sqrt)] = 0.
d_mat_inv_sqrt = np.diag(d_inv_sqrt)
return adj.dot(d_mat_inv_sqrt).transpose().dot(d_mat_inv_sqrt)
def preprocess_adj(adj):
"""Preprocessing of adjacency matrix for simple GCN model and conversion to tuple representation."""
max_length = max([a.shape[0] for a in adj])
mask = np.zeros((len(adj), max_length, 1)) # mask for padding
for i in range(len(adj)):
adj_normalized = normalize_adj(adj[i]) # no self-loop
pad = max_length - adj_normalized.shape[0] # padding for each epoch
adj_normalized = np.pad(adj_normalized, ((0, pad), (0, pad)), mode='constant')
mask[i, :adj[i].shape[0], :] = 1.
adj[i] = adj_normalized
return np.array(list(adj)), mask # coo_to_tuple(sparse.COO(np.array(list(adj)))), mask
def construct_feed_dict(features, support, mask, labels, placeholders):
"""Construct feed dictionary."""
feed_dict = dict()
feed_dict.update({placeholders['labels']: labels})
feed_dict.update({placeholders['features']: features})
feed_dict.update({placeholders['support']: support})
feed_dict.update({placeholders['mask']: mask})
feed_dict.update({placeholders['num_features_nonzero']: features[1].shape})
return feed_dict
def chebyshev_polynomials(adj, k):
"""Calculate Chebyshev polynomials up to order k. Return a list of sparse matrices (tuple representation)."""
print("Calculating Chebyshev polynomials up to order {}...".format(k))
adj_normalized = normalize_adj(adj)
laplacian = sp.eye(adj.shape[0]) - adj_normalized
largest_eigval, _ = eigsh(laplacian, 1, which='LM')
scaled_laplacian = (2. / largest_eigval[0]) * laplacian - sp.eye(adj.shape[0])
t_k = list()
t_k.append(sp.eye(adj.shape[0]))
t_k.append(scaled_laplacian)
def chebyshev_recurrence(t_k_minus_one, t_k_minus_two, scaled_lap):
s_lap = sp.csr_matrix(scaled_lap, copy=True)
return 2 * s_lap.dot(t_k_minus_one) - t_k_minus_two
for i in range(2, k + 1):
t_k.append(chebyshev_recurrence(t_k[-1], t_k[-2], scaled_laplacian))
return sparse_to_tuple(t_k)
def loadWord2Vec(filename):
"""Read Word Vectors"""
vocab = []
embd = []
word_vector_map = {}
file = open(filename, 'r')
for line in file.readlines():
row = line.strip().split(' ')
if (len(row) > 2):
vocab.append(row[0])
vector = row[1:]
length = len(vector)
for i in range(length):
vector[i] = float(vector[i])
embd.append(vector)
word_vector_map[row[0]] = vector
print('Loaded Word Vectors!')
file.close()
return vocab, embd, word_vector_map
def clean_str(string):
"""
Tokenization/string cleaning for all datasets except for SST.
Original taken from https://github.com/yoonkim/CNN_sentence/blob/master/process_data.py
"""
string = re.sub(r"[^A-Za-z0-9(),!?\'\`]", " ", string)
string = re.sub(r"\'s", " \'s", string)
string = re.sub(r"\'ve", " \'ve", string)
string = re.sub(r"n\'t", " n\'t", string)
string = re.sub(r"\'re", " \'re", string)
string = re.sub(r"\'d", " \'d", string)
string = re.sub(r"\'ll", " \'ll", string)
string = re.sub(r",", " , ", string)
string = re.sub(r"!", " ! ", string)
string = re.sub(r"\(", " \( ", string)
string = re.sub(r"\)", " \) ", string)
string = re.sub(r"\?", " \? ", string)
string = re.sub(r"\s{2,}", " ", string)
return string.strip().lower()
def clean_str_sst(string):
"""
Tokenization/string cleaning for the SST dataset
"""
string = re.sub(r"[^A-Za-z0-9(),!?\'\`]", " ", string)
string = re.sub(r"\s{2,}", " ", string)
return string.strip().lower()
def remove_comments_and_docstrings(source, lang):
if lang in ['python']:
"""
Returns 'source' minus comments and docstrings.
"""
io_obj = StringIO(source)
out = ""
prev_toktype = tokenize.INDENT
last_lineno = -1
last_col = 0
for tok in tokenize.generate_tokens(io_obj.readline):
token_type = tok[0]
token_string = tok[1]
start_line, start_col = tok[2]
end_line, end_col = tok[3]
ltext = tok[4]
if start_line > last_lineno:
last_col = 0
if start_col > last_col:
out += (" " * (start_col - last_col))
# Remove comments:
if token_type == tokenize.COMMENT:
pass
# This series of conditionals removes docstrings:
elif token_type == tokenize.STRING:
if prev_toktype != tokenize.INDENT:
# This is likely a docstring; double-check we're not inside an operator:
if prev_toktype != tokenize.NEWLINE:
if start_col > 0:
out += token_string
else:
out += token_string
prev_toktype = token_type
last_col = end_col
last_lineno = end_line
temp = []
for x in out.split('\n'):
if x.strip() != "":
temp.append(x)
return '\n'.join(temp)
elif lang in ['ruby']:
return source
else:
def replacer(match):
s = match.group(0)
if s.startswith('/'):
return " " # note: a space and not an empty string
else:
return s
pattern = re.compile(
r'//.*?$|/\*.*?\*/|\'(?:\\.|[^\\\'])*\'|"(?:\\.|[^\\"])*"',
re.DOTALL | re.MULTILINE
)
temp = []
for x in re.sub(pattern, replacer, source).split('\n'):
if x.strip() != "":
temp.append(x)
return '\n'.join(temp)
def tree_to_token_index(root_node):
if (len(root_node.children) == 0 or root_node.type == 'string') and root_node.type != 'comment':
return [(root_node.start_point, root_node.end_point)]
else:
code_tokens = []
for child in root_node.children:
code_tokens += tree_to_token_index(child)
return code_tokens
def tree_to_token_index_ved(root_node):
if (len(root_node.children) == 0 or root_node.type == 'string') and root_node.type != 'comment':
return [(root_node.start_point, root_node.end_point, root_node.type)]
else:
code_tokens = []
for child in root_node.children:
code_tokens += tree_to_token_index_ved(child)
return code_tokens
def tree_to_variable_index(root_node, index_to_code):
if (len(root_node.children) == 0 or root_node.type == 'string') and root_node.type != 'comment':
index = (root_node.start_point, root_node.end_point)
_, code = index_to_code[index]
if root_node.type != code:
return [(root_node.start_point, root_node.end_point)]
else:
return []
else:
code_tokens = []
for child in root_node.children:
code_tokens += tree_to_variable_index(child, index_to_code)
return code_tokens
def index_to_code_token(index, code):
start_point = index[0]
end_point = index[1]
if start_point[0] == end_point[0]:
s = code[start_point[0]][start_point[1]:end_point[1]]
else:
s = ""
s += code[start_point[0]][start_point[1]:]
for i in range(start_point[0] + 1, end_point[0]):
s += code[i]
s += code[end_point[0]][:end_point[1]]
return s