forked from Attn-to-FC/Attn-to-FC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
astpathmaker.py
149 lines (110 loc) · 3.55 KB
/
astpathmaker.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
import pickle
import sys
import os
import math
import traceback
import argparse
import signal
import atexit
import time
import random
import tensorflow as tf
import numpy as np
import networkx as nx
from myutils import prep, drop
prep('loading sequences... ')
seqdata = pickle.load(open('/nfs/projects/attn-to-fc/data/standard_3dfiles_graphast/dataset.pkl', 'rb'))
drop()
#fid=122380
#print(wsmlnodes)
#print(wsmledges)
def idx2tok(nodelist, path):
out = list()
for idx in path:
out.append(nodelist[idx])
return out
# one way
def getpaths(tt):
wsmlpaths = dict()
i = 0
for fid in seqdata['s%s_nodes' % (tt)].keys():
wsmlnodes = seqdata['s%s_nodes' % (tt)][fid]
wsmledges = seqdata['s%s_edges' % (tt)][fid]
g = nx.from_numpy_matrix(wsmledges.todense())
astpaths = nx.all_pairs_shortest_path(g, cutoff=8)
outpaths = 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
if len(path) > 1 and len(path) <= 8:
newpath = idx2tok(wsmlnodes, path)
tmp = [0] * (8 - len(newpath))
newpath.extend(tmp)
outpaths.append(newpath)
if(len(outpaths) >= 1000):
break
wsmlpaths[fid] = outpaths
if i % 500 == 0:
print('.', end='')
sys.stdout.flush()
if i % 500000 == 0:
astpaths = dict()
astpaths['s%s_paths' % tt] = wsmlpaths
pickle.dump(astpaths, open('astpaths-' + str(i) + '-' + tt + '.pkl', 'wb'))
wsmlpaths = dict()
i += 1
return wsmlpaths
astpaths = dict()
prep('calculating shortest paths for test set... ')
tt = 'test'
astpaths['s%s_paths' % tt] = getpaths(tt)
drop()
prep('calculating shortest paths for val set... ')
tt = 'val'
astpaths['s%s_paths' % tt] = getpaths(tt)
drop()
prep('dumping astpaths to pickle... ')
pickle.dump(astpaths, open('astpaths-val-test.pkl', 'wb'))
drop()
astpaths = dict()
prep('calculating shortest paths for train set... ')
tt = 'train'
astpaths['s%s_paths' % tt] = getpaths(tt)
drop()
prep('dumping astpaths to pickle... ')
pickle.dump(astpaths, open('astpaths-final-train.pkl', 'wb'))
drop()
# another way
#for fid in seqdata['s%s_nodes' % (tt)].keys():
#wsmlnodes = seqdata['s%s_nodes' % (tt)][fid]
#wsmledges = seqdata['s%s_edges' % (tt)][fid]
#g = nx.from_numpy_matrix(wsmledges.todense())
#outpaths = list()
#terminals = list()
#for n in g.nodes():
#if len([q for q in g.neighbors(n)]) == 1:
#terminals.append(n)
#for s in terminals:
#for t in terminals:
#if s == t:
#continue
#astpath = nx.shortest_path(g, source=s, target=t)
#if len(astpath) > 1 and len(astpath) <= 8:
#newpath = idx2tok(wsmlnodes, astpath)
#tmp = [0] * (8 - len(newpath))
#newpath.extend(tmp)
#outpaths.append(newpath)
#wsmlpaths[fid] = outpaths
#if i % 100 == 0:
#print('.', end='')
#sys.stdout.flush()
#i += 1
#for k in wsmlpaths.keys():
#v = wsmlpaths[k]
#for path in v:
#print(path)
#print(len(v))