-
Notifications
You must be signed in to change notification settings - Fork 1
/
convert_all.py
184 lines (131 loc) · 3.16 KB
/
convert_all.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
import sys
import random
input_file = sys.argv[1]
output_file = sys.argv[2]
ca = sys.argv[3]
ci = sys.argv[4]
test_file = sys.argv[5]
test_formatted = 'data/test.txt'
graphs_formatted = 'data/aids.txt'
ca_formatted = 'data/ca.txt'
ci_formatted = 'data/ci.txt'
init_test = 'test.txt'
init_train = 'train.txt'
MAX_CA = 20000.0
MAX_CI = 20000.0
with open(input_file) as f:
content = f.readlines()
content = [x.strip() for x in content]
f_ = open(graphs_formatted,'w')
f = open(output_file,'w')
i = 0
nums = 0
graph_map = {}
v_dict = {}
e_dict = {}
while i < len(content):
f.write( 't # '+str(nums)+'\n' )
f_.write( '#'+str(nums)+'\n' )
graph_map[int(content[i].split('#')[1])] = nums
i += 1
vertices = int(content[i])
f_.write(str(vertices)+'\n')
for j in xrange(1,vertices+1):
key = content[i+j]
if key not in v_dict:
v_dict[key] = len(v_dict)
f.write('v '+str(j-1)+' '+str(v_dict[key])+'\n')
f_.write(str(v_dict[key])+'\n')
i = i+vertices+1
edges = int(content[i])
f_.write(str(edges)+'\n')
for j in xrange(1,edges+1):
s, d, key = content[i+j].split(' ')
if key not in e_dict:
e_dict[key] = len(e_dict) + len(v_dict)
f.write('e '+s+' '+d+' '+str(e_dict[key])+'\n')
f_.write(s+' '+d+' '+str(e_dict[key])+'\n')
i = i+edges+1
nums += 1
f.close()
f_.close()
f = open(init_train,'w')
for x in range(100):
if(random.random() < 0.5):
f.write('1 0:1\n')
else:
f.write('-1 0:0\n')
f.close()
print "Number of graphs:", nums
with open(test_file) as f:
content = f.readlines()
content = [x.strip() for x in content]
f_ = open(test_formatted,'w')
i = 0
nums = 0
while i < len(content):
f_.write( '#'+str(nums)+'\n' )
i += 1
vertices = int(content[i])
f_.write(str(vertices)+'\n')
for j in xrange(1,vertices+1):
key = content[i+j]
if key not in v_dict:
v_dict[key] = len(v_dict)
f_.write(str(v_dict[key])+'\n')
i = i+vertices+1
edges = int(content[i])
f_.write(str(edges)+'\n')
for j in xrange(1,edges+1):
s, d, key = content[i+j].split(' ')
if key not in e_dict:
e_dict[key] = len(e_dict) + len(v_dict)
f_.write(s+' '+d+' '+str(e_dict[key])+'\n')
i = i+edges+1
nums += 1
f_.close()
f = open(init_test,'w')
for x in range(nums):
if(random.random() < 0.5):
f.write('0:1\n')
else:
f.write('0:0\n')
f.close()
print "Number of test_graphs:", nums
d = {}
with open(ci) as f:
content0 = f.readlines()
content0 = [int(x.strip()) for x in content0]
with open(ca) as f:
content1 = f.readlines()
content1 = [int(x.strip()) for x in content1]
f = open('labels.txt','w')
f_ca = open(ca_formatted,'w')
f_ci = open(ci_formatted,'w')
for x in content0:
# d[graph_map[x]] = 0
try:
d[graph_map[x]] = 0
if(random.random() < MAX_CI/len(content0)):
f_ci.write(str(graph_map[x])+'\n')
except Exception as e:
pass
for x in content1:
# d[graph_map[x]] = 0
try:
d[graph_map[x]] = 1
if(random.random() < MAX_CI/len(content1)):
f_ca.write(str(graph_map[x])+'\n')
except Exception as e:
pass
print len(graph_map)
print len(content0)+len(content1)
print len(d)
for x in xrange(0,len(graph_map)):
try:
f.write(str(d[x])+'\n')
except Exception as e:
f.write(str(0)+'\n')
f.close()
f_ca.close()
f_ci.close()