-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgram_tasks.py
executable file
·716 lines (550 loc) · 18.7 KB
/
gram_tasks.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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
from pyTasks import task
from pyTasks.task import Task, Parameter
from pyTasks.task import Optional, containerHash, TaskProgressHelper
from pyTasks.target import CachedTarget, LocalTarget, NetworkXService, ManagedTarget
from pyTasks.target import FileTarget, JsonService
from .graph_tasks import GraphPruningTask
from .ranking_task import ExtractInfoTask
import networkx as nx
import numpy as np
from .prepare_tasks import GraphIndexTask
import os
from os.path import dirname
from .ranking_task import DefineClassTask
from .kernel_function import select_kernel
import csv
from sklearn.manifold import MDS
import matplotlib.pyplot as plt
from pyTasks.utils import tick
from scipy.sparse import coo_matrix, diags
try:
import mmh3
except ImportError:
import pymmh3 as mmh3
def parseList(s):
if not isinstance(s, str):
return s
state = 0
tmp = ''
out = []
for i in range(len(s)):
c = s[i]
if state == 0:
if c == '[':
state = 1
elif state == 1:
if c == ',':
out.append(tmp)
tmp = ''
elif c == "\'":
state = 2
elif c == ']':
return out
elif state == 2:
if c == '\'':
state = 1
else:
tmp += c
raise ValueError(str(s)+" is malformed")
def indexMap(key, mapping):
counter = 0
if 'counter' in mapping:
counter = mapping['counter']
if key not in mapping:
mapping[key] = counter
mapping['counter'] = counter + 1
return mapping[key]
class PrepareKernelTask(Task):
out_dir = Parameter('./gram/')
timeout = Parameter(None)
rainbow = Parameter(False)
descriptive = Parameter(False)
def __init__(self, graph, h, D):
self.graph = graph
self.h = h
self.D = D
def require(self):
if self.h <= 0:
return GraphPruningTask(self.graph, self.D)
else:
return PrepareKernelTask(self.graph, self.h - 1, self.D)
def output(self):
path = self.out_dir.value + self.__taskid__() + '.pickle'
return CachedTarget(
LocalTarget(path, service=NetworkXService)
)
def __taskid__(self):
return 'PrepareKernelTask_%s_%d_%d' % (self.graph,
self.h, self.D)
def __repr__(self):
return 'PrepareKernelTask(graph: %s, h: %d, D: %d)' %\
(self.graph, self.h, self.D)
def _hash(self, obj):
hash = mmh3.hash(obj)
if self.rainbow.value:
if not hasattr(self, 'rainbow_table'):
self.rainbow_table = {}
self.rainbow_table[hash] = obj
return hash
def _describe(self, source, neighbours):
prefix = ''
if self.descriptive.value:
prefix = str(source)[:min(8, len(source))]
neighbours.append(source)
return prefix + str(self._hash(
'_'.join(
[str(t) for t in neighbours
]
)
))
def _collect_labels(self, graph):
ret = {}
for u, v, d in graph.in_edges(data=True):
tick(self)
source = graph.node[u]['label']
edge_t = d['type']
truth = d['truth']
long_edge_label = self._describe(
source, [edge_t, truth]
)
if v not in ret:
ret[v] = []
ret[v].append(long_edge_label)
return ret
def run(self):
with self.input()[0] as graphInput:
graph = graphInput.query()
count = {}
if self.h > 0:
# Step 1
M = self._collect_labels(graph)
# Step 2, 3, 4
for n, d in graph.nodes(data=True):
tick(self)
if n not in M:
continue
label = self._describe(d['label'], sorted(M[n]))
if label not in count:
count[label] = 0
count[label] += 1
d['label'] = label
else:
for n, d in graph.nodes(data=True):
tick(self)
label = d['label']
if label not in count:
count[label] = 0
count[label] += 1
graph.graph['label_count'] = count
if hasattr(self, 'rainbow_table'):
graph.graph['rainbow_table'] = self.rainbow_table
with self.output() as out_dirput:
out_dirput.emit(graph)
class WLCollectorTask(Task):
out_dir = Parameter('./gram/')
def __init__(self, graphs, h, D):
self.graphs = graphs
self.h = h
self.D = D
def require(self):
task = []
if self.h > 0:
task.append(
WLCollectorTask(self.graphs, self.h - 1, self.D))
for g in self.graphs:
task.append(Optional(PrepareKernelTask(g, self.h, self.D)))
return task
def output(self):
return ManagedTarget(self)
def __taskid__(self):
return "WLCollector_%d_%d_%s" %\
(self.h, self.D,
str(containerHash(self.graphs, large=True)))
def __repr__(self):
return 'WLCollector(h: %d, D: %d)' % (self.h, self.D)
def run(self):
M = {}
s = 0
if self.h > 0:
with self.input()[0] as i:
M = i.query()
s = 1
paths = {}
for i in range(s, len(self.input())):
inputDep = self.input()[i]
g = self.graphs[i - s]
if inputDep is None:
if g in M:
del M[g]
else:
paths[g] = inputDep
for g, p in paths.items():
if g not in M:
M[g] = {}
if self.h not in M[g]:
M[g][self.h] = {}
with p as pin:
G = pin.query()
count = G.graph['label_count']
del G
for n, c in count.items():
M[g][self.h][n] = c
with self.output() as o:
o.emit(M)
class WLKernelTask(Task):
out_dir = Parameter('./gram/')
def __init__(self, graphs, h, D):
self.graphs = graphs
self.h = h
self.D = D
def require(self):
return WLCollectorTask(self.graphs, self.h, self.D)
def output(self):
return ManagedTarget(self)
def __taskid__(self):
return "WLKernel_%d_%d_%s" %\
(self.h, self.D,
str(containerHash(self.graphs, large=True)))
def __repr__(self):
return 'WLKernel(h: %d, D: %d)' % (self.h, self.D)
def run(self):
with self.input()[0] as i:
M = i.query()
graphIndex = {}
nodeIndex = {}
K = [None] * (self.h + 1)
for g, D in M.items():
gI = indexMap(g, graphIndex)
for h, N in D.items():
row = []
column = []
data = []
for n, c in N.items():
nI = indexMap(n, nodeIndex)
row.append(gI)
column.append(nI)
data.append(c)
K[h] = {
'row': row,
'column': column,
'data': data
}
del M
GR = None
for h, D in enumerate(K):
phi = coo_matrix((D['data'], (D['row'], D['column'])),
shape=(graphIndex['counter'], nodeIndex['counter']),
dtype=np.uint64).tocsr()
del K[h]
T = phi.dot(phi.transpose())
if GR is None:
GR = T
else:
GR += T
del K
with self.output() as o:
o.emit((graphIndex, GR))
class CustomKernelTask(Task):
out_dir = Parameter('./gram/')
def __init__(self, kernel_type, graphs, h, D):
self.kernel_type = kernel_type
self.graphs = graphs
self.h = h
self.D = D
def require(self):
return WLCollectorTask(self.graphs, self.h, self.D)
def output(self):
return ManagedTarget(self)
def __taskid__(self):
return "CustomKernelTask(%s)_%d_%d_%s" %\
(self.kernel_type, self.h, self.D,
str(containerHash(self.graphs, large=True)))
def __repr__(self):
return 'CustomKernel[%s](h: %d, D: %d)' % (self.kernel_type, self.h,
self.D)
@staticmethod
def pairwise_index(D1, D2):
index = {}
O1 = {}
for d, v in D1.items():
O1[indexMap(d, index)] = v
O2 = {}
for d, v in D2.items():
O2[indexMap(d, index)] = v
V1 = np.zeros((index['counter']), dtype=np.int64)
for o, v in O1.items():
V1[o] = v
V2 = np.zeros((index['counter']), dtype=np.int64)
for o, v in O2.items():
V2[o] = v
return V1, V2
def pairwise_kernel(self, X, Y):
VX, VY = CustomKernelTask.pairwise_index(X, Y)
return self._kernel(VX, VY)
@staticmethod
def dis_to_sim(X):
MAX = np.full(X.shape, np.amax(X), dtype=np.float64)
return MAX - X
def run(self):
self._kernel = select_kernel(self.kernel_type)
with self.input()[0] as i:
M = i.query()
graphIndex = {}
K = [None] * (self.h + 1)
for g, D in M.items():
gI = indexMap(g, graphIndex)
for h, N in D.items():
if K[h] is None:
K[h] = {}
K[h][gI] = N
del M
GR = None
for h, D in enumerate(K):
T_GR = np.zeros((graphIndex['counter'], graphIndex['counter']),
dtype=np.float64)
for i in range(graphIndex['counter']):
for j in range(graphIndex['counter']):
if i <= j:
T_GR[i, j] = self.pairwise_kernel(D[i], D[j])
T_GR[j, i] = T_GR[i, j]
if T_GR[0, 0] == 0:
T_GR = CustomKernelTask.dis_to_sim(T_GR)
if GR is None:
GR = T_GR
else:
GR += T_GR
del K[h]
del K
with self.output() as o:
o.emit((graphIndex, GR))
class NormalizedWLKernelTask(Task):
out_dir = Parameter('./gram/')
custom_kernel = Parameter(None)
def __init__(self, graphs, h, D):
self.graphs = graphs
self.h = h
self.D = D
def require(self):
if self.custom_kernel.value is None:
return WLKernelTask(self.graphs, self.h, self.D)
else:
return CustomKernelTask(self.custom_kernel.value, self.graphs,
self.h, self.D)
def output(self):
return ManagedTarget(self)
def __taskid__(self):
return "NormWLKernel_%d_%d_%s" %\
(self.h, self.D,
str(containerHash(self.graphs, large=True)))
def __repr__(self):
return 'NormalizedKernel(h: %d, D: %d)' % (self.h, self.D)
def run(self):
with self.input()[0] as i:
graphIndex, GR = i.query()
D = diags(1/np.sqrt(GR.diagonal()))
GR = D * GR * D
with self.output() as o:
o.emit((graphIndex, GR))
class ExtractKernelBagTask(Task):
out_dir = Parameter('./gram/')
def __init__(self, graph, h, D):
self.graph = graph
self.h = h
self.D = D
def require(self):
return PrepareKernelTask(self.graph, self.h, self.D)
def output(self):
path = self.out_dir.value + self.__taskid__() + '.json'
return CachedTarget(
LocalTarget(path, service=JsonService)
)
def __taskid__(self):
return 'ExtractKernelBagTask_%s_%d_%d' % (self.graph,
self.h, self.D)
def __repr__(self):
return 'ExtractKernelBagTask(graph: %s, h: %d, D: %d)' %\
(self.graph, self.h, self.D)
def run(self):
with self.input()[0] as i:
G = i.query()
with self.output() as o:
o.emit(G.graph['label_count'])
class ExtractKernelEntitiesTask(Task):
out_dir = Parameter('./gram/')
def __init__(self, graphs, h, D):
self.graphs = graphs
self.h = h
self.D = D
def require(self):
out = [ExtractInfoTask(self.graphs), GraphIndexTask()]
for g in self.graphs:
out.append(Optional(ExtractKernelBagTask(g, self.h, self.D)))
return out
def output(self):
path = self.out_dir.value + self.__taskid__() + '.json'
return CachedTarget(
LocalTarget(path, service=JsonService)
)
def __taskid__(self):
return 'ExtractKernelEntitiesTask_%d_%d_%s' % (self.h, self.D,
str(
containerHash(self.graphs)
))
def run(self):
with self.input()[0] as i:
info = i.query()
with self.input()[1] as i:
index = i.query()['index']
out_dict = {}
for i, g in enumerate(self.graphs):
name = index[g]
if self.input()[i + 2] is None:
continue
with self.input()[i + 2] as bag_input:
bag = bag_input.query()
out_dict[g] = {
'file': name,
'kernel_bag': bag,
'label': info[g]
}
with self.output() as out_file:
out_file.emit(out_dict)
class MDSTask(Task):
out_dir = Parameter('./gram/')
def __init__(self, graphs, h, D):
self.graphs = graphs
self.h = h
self.D = D
def require(self):
return [
NormalizedWLKernelTask(self.graphs, self.h, self.D),
DefineClassTask(self.graphs)
]
def output(self):
return FileTarget(self.out_dir.value+self.__taskid__()+'.png')
def __taskid__(self):
return "MDS_%d_%d_%s" %\
(self.h, self.D,
str(containerHash(self.graphs, large=True)))
def __repr__(self):
return 'MDS(h: %d, D: %d)' % (self.h, self.D)
def __evalScore(self, score):
out = None
for k, v in score.items():
if v == 'correct':
if out is None:
out = k
else:
out = 'UNKNOWN'
return out
def __evalTime(self, t_rank):
testSet = set([])
allSet = set([])
for u, smaller, v in t_rank:
if smaller:
testSet.add(v)
allSet.add(u)
allSet.add(v)
out = []
for k in allSet:
if k not in testSet:
out.append(k)
if len(out) > 1:
return 'UNKNOWN'
return out[0]
def run(self):
with self.input()[0] as i:
graphIndex, GR = i.query()
with self.input()[1] as i:
R = i.query()
dis = np.ones(GR.shape, dtype=GR.dtype) - GR
colors = ['grey', 'green', 'red']
tName = ['UNKNOWN', 'IUV', 'ESBMC']
aName = ['UNKNOWN', 'Tester', 'Verificator']
lScore = np.zeros(len(GR))
lTime = np.zeros(len(GR))
for index, D in R.items():
if index not in graphIndex:
continue
gI = graphIndex[index]
score = self.__evalScore(D['score'])
time = self.__evalTime(D['time_rank'])
for i, t in enumerate(tName):
if score == t:
lScore[gI] = i
if time == t:
lTime[gI] = i
mds = MDS(n_components=2, dissimilarity="precomputed", n_init=10)
X_r = mds.fit_transform(dis)
stress = mds.stress_
plt.figure(1)
plt.suptitle('MDS of GRAM dataset (h: %s, D: %s) [%s points] (Stress: %2.2f)' %
(str(self.h), str(self.D), str(len(X_r)), stress))
plt.subplot(121)
for color, i, t in zip(colors, range(len(aName)), aName):
plt.scatter(X_r[lScore == i, 0], X_r[lScore == i, 1],
color=color, alpha=.8,
lw=2,
label=t)
plt.legend(loc='best', shadow=False, scatterpoints=1)
plt.subplot(122)
for color, i, t in zip(colors, range(len(aName)), aName):
plt.scatter(X_r[lTime == i, 0], X_r[lTime == i, 1],
color=color, alpha=.8,
lw=2,
label=t)
plt.legend(loc='best', shadow=False, scatterpoints=1)
path = self.output().path
directory = dirname(path)
if not os.path.exists(directory):
os.makedirs(directory)
plt.savefig(path)
plt.close()
if __name__ == '__main__':
config = {
"GraphSubTask": {
"graphPath": "/Users/cedricrichter/Documents/Arbeit/Ranking/PyPRSVT/static/results-tb-raw/",
"out_dir": "./test/",
"cpaChecker": "/Users/cedricrichter/Documents/Arbeit/Ranking/cpachecker"
},
"GraphConvertTask": {
"out_dir": "./test/"
},
"CategoryLookupTask": {
"graphPaths": "/Users/cedricrichter/Documents/Arbeit/Ranking/PyPRSVT/static/results-tb-raw/"
},
"MemcachedTarget": {
"baseDir": "./cache/"
},
"GraphIndexTask": {
"categories": ['array-examples',
'array-industry-pattern',
"bitvector-loops",
"bitvector-regression",
"bitvector"]
},
"GraphPruningTask": {
"out_dir": "./test/"
},
"RemoteTarget": {
"host": "pc-wehr-serv1.cs.upb.de",
"user": "cedricr",
"password": "wehrheim01",
"remote_path": "./test/"
}
}
injector = task.ParameterInjector(config)
planner = task.TaskPlanner(injector=injector)
exe = task.TaskExecutor()
task = GraphIndexTask()
plan = planner.plan(task)
helper = TaskProgressHelper(plan)
exe.executePlan(plan)
with helper.output(task) as js:
index = js.query()
graphs = []
for k, v in index['categories'].items():
graphs.extend(v)
task = MDSTask(graphs, 2, 5)
plan = planner.plan(task, graph=plan)
exe.executePlan(plan)