-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyutils.py
396 lines (331 loc) · 14.3 KB
/
myutils.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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
import sys
import javalang
from timeit import default_timer as timer
import tensorflow.keras as keras
import tensorflow.keras.utils
import numpy as np
import tensorflow as tf
import networkx as nx
import random
import threading
import sqlite3
import tokenizer
start = 0
end = 0
def prep(msg):
global start
statusout(msg)
start = timer()
def statusout(msg):
sys.stdout.write(msg)
sys.stdout.flush()
def drop():
global start
global end
end = timer()
sys.stdout.write('done, %s seconds.\n' % (round(end - start, 2)))
sys.stdout.flush()
def index2word(tok):
i2w = {}
for word, index in tok.w2i.items():
i2w[index] = word
return i2w
def seq2sent(seq, tokenizer):
sent = []
check = index2word(tokenizer)
for i in seq:
sent.append(check[i])
return(' '.join(sent))
class batch_gen(tensorflow.keras.utils.Sequence):
def __init__(self, seqdata, extradata, tt, config, training=True):
self.comvocabsize = config['comvocabsize']
self.tt = tt
self.batch_size = config['batch_size']
self.extradata = extradata
#self.filedb = sqlite3.connect(sqlfile)
#self.filedbcur = self.filedb.cursor()
#self.sqlfile = sqlfile
self.seqdata = dict()
self.seqdata['dt%s' % tt] = seqdata.get('/dt%s' % tt)
self.seqdata['ds%s' % tt] = seqdata.get('/ds%s' % tt)
self.seqdata['s%s' % tt] = seqdata.get('/s%s' % tt)
self.seqdata['c%s' % tt] = seqdata.get('/c%s' % tt)
self.allfidlocs = list(range(0, np.array(self.seqdata['dt%s' % tt]).shape[0]))
self.config = config
self.training = training
if not self.training:
comstart = np.zeros(self.config['comlen'])
stk = self.extradata['comstok'].w2i['<s>']
comstart[0] = stk
self.comstart = comstart
random.shuffle(self.allfidlocs)
def __getitem__(self, idx):
start = (idx*self.batch_size)
end = self.batch_size*(idx+1)
batchfidlocs = self.allfidlocs[start:end]
return self.make_batch(batchfidlocs)
def make_batch(self, batchfidlocs):
return self.divideseqs(batchfidlocs)
def __len__(self):
return int(np.ceil(np.array(self.seqdata['dt%s' % (self.tt)]).shape[0])/self.batch_size)
def on_epoch_end(self):
random.shuffle(self.allfidlocs)
#def get_tdat(self, fid, maxlen, filedb, filedbcur):
# tdatstok = self.extradata['tdatstok']
# filedbcur.execute('select tdat from fundats where fid={}'.format(fid))
# filedb.commit()
# for tdat in filedbcur.fetchall():
# tdatraw = tdat[0]
# tdat = tdatstok.texts_to_sequences(tdatraw, maxlen=maxlen)[0]
# return(tdat)
def divideseqs(self, batchfidlocs):
tdatseqs = list()
sdatseqs = list()
comseqs = list()
smlseqs = list()
smlpaths = list()
smlnodes = list()
smledges = list()
callnodes = list()
calledges = list()
bios = list()
comouts = list()
badfids = list()
fiddat = dict()
with_tdats = False
with_sdats = False
with_coms = False
with_smlseqs = False
with_smlpaths = False
with_smlnodes = False
with_smledges = False
with_callnodes = False
with_calledges = False
with_bio = False
#filedb = sqlite3.connect(self.sqlfile)
#filedbcur = filedb.cursor()
comseqpos = 0
for c, inp in enumerate(self.config['batch_config'][0]):
if inp == 'tdat':
with_tdats = True
elif inp == 'sdat':
with_sdats = True
elif inp == 'com':
with_coms = True
comseqpos = c
elif inp == 'smlseq':
with_smlseqs = True
elif inp == 'smlpath':
with_smlpaths = True
elif inp == 'smlnode':
with_smlnodes = True
elif inp == 'smledge':
with_smledges = True
elif inp == 'callnode':
with_callnodes = True
elif inp == 'calledge':
with_calledges = True
elif inp == 'bio':
with_bio = True
for fidloc in batchfidlocs:
fid = self.config['locfid']['c%s' % self.tt][fidloc]
wcomseq = self.seqdata['c%s' % self.tt][fidloc]
wcomseq = wcomseq[:self.config['comlen']]
#fid = wcomseq[:1][0]
#wcomseq = wcomseq[1:self.config['comlen']+1]
if with_tdats:
#wtdatseq = self.get_tdat(fid, self.config['tdatlen'], filedb, filedbcur)
wtdatseq = self.seqdata['dt%s' % self.tt][fidloc]
wtdatseq = wtdatseq[:self.config['tdatlen']]
if with_sdats:
wsdatseq = self.seqdata['ds%s' % self.tt][fidloc]
wsdatseq = wsdatseq[:self.config['sdatlen']]
wsdatseq = np.delete(wsdatseq, slice(self.config['stdatlen'],None), 1)
if with_smlseqs:
wsmlseq = self.seqdata['s%s' % self.tt][fidloc]
wsmlseq = wsmlseq[:self.config['smllen']]
if with_smlnodes or with_smlpaths:
wsmlnodes = self.extradata['s%s_nodes' % (self.tt)][fid]
wsmlnodeslen = len(wsmlnodes)
# crop/expand ast sequence
wsmlnodes = wsmlnodes[:self.config['maxastnodes']]
tmp = np.zeros(self.config['maxastnodes'], dtype='int32')
tmp[:wsmlnodes.shape[0]] = wsmlnodes
wsmlnodes = np.int32(tmp)
if with_smledges or with_smlpaths:
wsmledges = self.extradata['s%s_edges' % (self.tt)][fid]
if (wsmledges.shape[0] > 1000):
badfids.append(fid)
continue
# crop/expand ast adjacency matrix to dense
wsmledges = np.asarray(wsmledges.todense())
wsmledges = wsmledges[:self.config['maxastnodes'], :self.config['maxastnodes']]
tmp = np.zeros((self.config['maxastnodes'], self.config['maxastnodes']), dtype='int32')
tmp[:wsmledges.shape[0], :wsmledges.shape[1]] = wsmledges
wsmledges = np.int32(tmp)
if with_smlpaths:
g = nx.from_numpy_matrix(wsmledges)
astpaths = nx.all_pairs_shortest_path(g, cutoff=self.config['pathlen'])
wsmlpaths = list()
for astpath in astpaths:
source = astpath[0]
if len([n for n in g.neighbors(source)]) > 1:
continue
for path in astpath[1].values():
if len([n for n in g.neighbors(path[-1])]) > 1:
continue # ensure only terminals as in Alon et al
if len(path) > 1 and len(path) <= self.config['pathlen']:
newpath = self.idx2tok(wsmlnodes, path)
tmp = [0] * (self.config['pathlen'] - len(newpath))
newpath.extend(tmp)
wsmlpaths.append(newpath)
random.shuffle(wsmlpaths) # Alon et al stipulate random selection of paths
wsmlpaths = wsmlpaths[:self.config['maxpaths']] # Alon et al use 200, crop/expand to size
if len(wsmlpaths) < self.config['maxpaths']:
wsmlpaths.extend([[0]*self.config['pathlen']] * (self.config['maxpaths'] - len(wsmlpaths)))
wsmlpaths = np.asarray(wsmlpaths)
if with_callnodes:
wcallnodes = np.asarray(self.extradata['callnodedata'][fid])
# cropping call chain nodes
wcallnodes = wcallnodes[:self.config['maxcallnodes'],:self.config['tdatlen']]
tmp3 = np.zeros((self.config['maxcallnodes'],self.config['tdatlen']), dtype='int32')
tmp3[:wcallnodes.shape[0],:wcallnodes.shape[1]] = wcallnodes
wcallnodes = np.int32(tmp3)
if with_calledges:
try:
wcalledges = self.extradata['calledges'][fid]
wcalledges = np.asarray(wcalledges.todense())
except:
wcalledges = np.zeros((self.config['maxcallnodes'],self.config['maxcallnodes']), dtype=int)
# cropping call chain edges
wcalledges = wcalledges[:self.config['maxcallnodes'], :self.config['maxcallnodes']]
tmp2 = np.zeros((self.config['maxcallnodes'], self.config['maxcallnodes']), dtype='int32')
tmp2[:wcalledges.shape[0], :wcalledges.shape[1]] = wcalledges
wcalledges = np.int32(tmp2)
if with_bio:
humanattnres = self.extradata['biodats'][fid]
humanattnres = humanattnres[:wsmlnodeslen]
whumanattn = list()
for s in humanattnres:
whumanattn.append((s[0]+1)/2)
for i in range(self.config['maxastnodes'] - wsmlnodeslen):
whumanattn.append(1)
whumanattn = np.asarray(whumanattn, dtype="float32")
if not self.training:
wcomseq = self.comstart
inps = list()
for inp in self.config['batch_config'][0]:
if inp == 'tdat':
inps.append(wtdatseq)
elif inp == 'sdat':
inps.append(wsdatseq)
elif inp == 'com':
inps.append(wcomseq)
elif inp == 'smlseq':
inps.append(wsmlseq)
elif inp == 'smlpath':
inps.append(wsmlpaths)
elif inp == 'smlnode':
inps.append(wsmlnodes)
elif inp == 'smledge':
inps.append(wsmledges)
elif inp == 'callnode':
inps.append(wcallnodes)
elif inp == 'calledge':
inps.append(wcalledges)
elif inp == 'bio':
inps.append(whumanattn)
comseqs.append(wcomseq)
fiddat[fid] = inps
else:
for i in range(0, len(wcomseq)):
if with_tdats:
tdatseqs.append(wtdatseq)
if with_sdats:
sdatseqs.append(wsdatseq)
if with_smlseqs:
smlseqs.append(wsmlseq)
if with_smlpaths:
smlpaths.append(wsmlpaths)
if with_smlnodes:
smlnodes.append(wsmlnodes)
if with_smledges:
smledges.append(wsmledges)
if with_callnodes:
callnodes.append(wcallnodes)
if with_calledges:
calledges.append(wcalledges)
if with_bio:
bios.append(whumanattn)
# slice up whole comseq into seen sequence and current sequence
# [a b c d] => [] [a], [a] [b], [a b] [c], [a b c] [d], ...
comseq = wcomseq[0:i]
comout = wcomseq[i]
comout = keras.utils.to_categorical(comout, num_classes=self.comvocabsize)
# extend length of comseq to expected sequence size
# the model will be expecting all input vectors to have the same size
for j in range(0, len(wcomseq)):
try:
comseq[j]
except IndexError as ex:
comseq = np.append(comseq, 0)
comseqs.append(comseq)
comouts.append(np.asarray(comout))
if self.training:
if with_tdats:
tdatseqs = np.asarray(tdatseqs)
if with_sdats:
sdatseqs = np.asarray(sdatseqs)
if with_smlseqs:
smlseqs = np.asarray(smlseqs)
if with_smlpaths:
smlpaths = np.asarray(smlpaths)
if with_smlnodes:
smlnodes = np.asarray(smlnodes)
if with_smledges:
smledges = np.asarray(smledges)
if with_callnodes:
callnodes = np.asarray(callnodes)
if with_calledges:
calledges = np.asarray(calledges)
if with_bio:
bios = np.asarray(bios)
comseqs = np.asarray(comseqs)
comouts = np.asarray(comouts)
inps = list()
oups = list()
for inp in self.config['batch_config'][0]:
if inp == 'tdat':
inps.append(tdatseqs)
elif inp == 'sdat':
inps.append(sdatseqs)
elif inp == 'com':
inps.append(comseqs)
elif inp == 'smlseq':
inps.append(smlseqs)
elif inp == 'smlpath':
inps.append(smlpaths)
elif inp == 'smlnode':
inps.append(smlnodes)
elif inp == 'smledge':
inps.append(smledges)
elif inp == 'callnode':
inps.append(callnodes)
elif inp == 'calledge':
inps.append(calledges)
elif inp == 'bio':
inps.append(bios)
for oup in self.config['batch_config'][1]:
if oup == 'comout':
oups.append(comouts)
if len(oups) == 1:
oups = oups[0]
if not self.training:
return (fiddat, badfids, comseqpos)
else:
return (inps, oups)
def idx2tok(self, nodelist, path):
out = list()
for idx in path:
out.append(nodelist[idx])
return out