-
Notifications
You must be signed in to change notification settings - Fork 0
/
prepare_datasets.py
141 lines (123 loc) · 3.37 KB
/
prepare_datasets.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
import numpy as np
# inputfile = './data/aido99_all.txt'
inputfile = './data/small_data.txt'
out_PAFI = './data/pafi_data.txt'
out_gSPAN = './data/gspan_data.txt'
gSPAN_labels = './data/gspan_v_labels.txt'
graph_num = './data/graph_num.txt'
active_file = './data/ca.txt'
inactive_file = './data/ci.txt'
labels = {}
with open(active_file, 'r') as fr:
line = fr.readline()
while line:
line = line.split(" ")
if len(line)!=0:
graph_id = int(line[0])
labels[graph_id]=1
line = fr.readline()
fr.close()
with open(inactive_file, 'r') as fr:
line = fr.readline()
while line:
line = line.split(" ")
if len(line)!=0:
graph_id = int(line[0])
labels[graph_id]=1
line = fr.readline()
fr.close()
print(len(list(labels.keys())))
# with open(inputfile, 'r') as fr, open(out_PAFI, 'a') as fw:
# line = fr.readline()
# while line:
# line = line.strip()
# if line[0]=='#':
# to_write = 't ' + str(line) + '\n'
# fw.write(to_write)
# # read vertices
# num_vertices = int(fr.readline().strip())
# for i in range(num_vertices):
# v_label = fr.readline()
# to_write = 'v ' + str(i) + ' ' + v_label
# fw.write(to_write)
# num_edges = int(fr.readline().strip())
# for i in range(num_edges):
# edge = fr.readline()
# to_write = 'u ' + edge
# fw.write(to_write)
# else:
# print(line + ' oooooooops !!!!')
# break
# line = fr.readline()
####################### GSPAN ####################
vertex_labels = {}
curr_v_label = -1
with open(inputfile, 'r') as fr, open(gSPAN_labels, 'a') as f_label:
line = fr.readline()
while line:
line = line.strip()
if len(line)!=0:
if line[0]=='#':
graph_id = int(line[1:])
# if graph is labelled
if graph_id in labels.keys():
# read vertices
num_vertices = int(fr.readline().strip())
for i in range(num_vertices):
v_label = fr.readline().rstrip()
if v_label not in vertex_labels.keys():
curr_v_label += 1
vertex_labels[v_label] = curr_v_label
f_label.write(str(curr_v_label) + ' ' + v_label+'\n')
num_edges = int(fr.readline().strip())
for i in range(num_edges):
edge = fr.readline()
# else:
# fr.readline()
# else:
# print(graph_id)
# else:
# print(line + "oooooooops!!")
# else:
# line = fr.readline()
line = fr.readline()
j = 0
i = 0
with open(inputfile, 'r') as fr, open(out_gSPAN, 'a') as fw, open(graph_num, 'a') as f_num:
line = fr.readline()
t_num = -1
while line:
line = line.strip()
if len(line)!=0:
if line[0]=='#':
graph_id = int(line[1:])
# if graph is labelled
if graph_id in labels.keys():
i+=1
t_num += 1
to_write = 't # ' + str(t_num) + '\n'
fw.write(to_write)
f_num.write(line[1:] + '\n')
# read vertices
num_vertices = int(fr.readline().strip())
for i in range(num_vertices):
v_label = fr.readline().rstrip()
to_write = 'v ' + str(i) + ' ' + str(vertex_labels[v_label]) + '\n'
fw.write(to_write)
# read edges
num_edges = int(fr.readline().strip())
for i in range(num_edges):
edge = fr.readline()
edge_nums = edge.split(" ")
to_write = 'e ' + edge_nums[0] + " " + edge_nums[1] + " " + str(int(edge_nums[2])-1) + "\n"
fw.write(to_write)
else:
j+=1
# print(j)
# else:
# line = fr.readline()
# else:
# fr.readline()
line = fr.readline()
print(i)
print(j)