-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.py
More file actions
170 lines (155 loc) · 6.56 KB
/
utils.py
File metadata and controls
170 lines (155 loc) · 6.56 KB
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
import pickle
import os
import sys
import random
from random import shuffle
import torch_geometric.transforms as T
import torch
from torch_geometric.data import Data
def save_graph(graph, file_name, dataset):
with open(f'graphs/graphs_{dataset}/{str(file_name)}.pickle', 'wb') as handle:
pickle.dump({'graph': graph}, handle, protocol=pickle.HIGHEST_PROTOCOL)
def load_graph(graph_name, dataset):
if 'pickle' in graph_name:
graph_name = graph_name.split('.')[0]
with open(f'graphs/graphs_{dataset}/{graph_name}.pickle', 'rb') as handle:
graph = pickle.load(handle)
return graph['graph']
def get_saved_graphs(dataset):
return [x for x in os.listdir('graphs/graphs_' + dataset) if x.endswith('pickle')]
def change_n_hop_neighborhood(graph, n_hops):
number_of_nodes = graph.x.shape[0]
edge_index = [[], []]
for i_from in range(0, number_of_nodes):
for i_to in range(i_from+1, i_from+n_hops):
if i_to < number_of_nodes:
edge_index[0].append(i_from)
edge_index[1].append(i_to)
new_edge_index = torch.LongTensor(edge_index)
new_graph = Data(
x=graph.x,
edge_index=new_edge_index,
y=graph.y
)
new_graph = T.RemoveDuplicatedEdges()(new_graph)
new_graph = T.ToUndirected()(new_graph)
return new_graph
def load_train_eval_test(dataset, num_per_class_train=20, num_per_class_eval=20, n_hop_neighborhood=2,
random_samples=True, random_seed=100):
if dataset == 'twitter':
pos_samples = []
neg_samples = []
for graph_name in get_saved_graphs(dataset=dataset):
graph = load_graph(graph_name, dataset)
if graph['y'].shape[0] != 1:
continue
if graph_name[:3] == 'neg':
neg_samples.append(graph)
if graph_name[:3] == 'pos':
pos_samples.append(graph)
if random_samples:
shuffle(pos_samples)
shuffle(neg_samples)
else:
random.Random(random_seed).shuffle(pos_samples)
random.Random(random_seed).shuffle(neg_samples)
train_graphs = pos_samples[:num_per_class_train] + neg_samples[:num_per_class_train]
eval_graphs = (pos_samples[num_per_class_train:num_per_class_train+num_per_class_eval] +
neg_samples[num_per_class_train:num_per_class_train+num_per_class_eval])
test_graphs = (pos_samples[num_per_class_train+num_per_class_eval:] +
neg_samples[num_per_class_train+num_per_class_eval:])
elif dataset == 'mr':
pos_samples = []
neg_samples = []
for graph_name in get_saved_graphs(dataset=dataset):
graph = load_graph(graph_name, dataset)
if graph['y'].shape[0] != 1 or graph['x'].shape[0] < 3:
continue
if graph_name[:3] == 'neg':
neg_samples.append(graph)
if graph_name[:3] == 'pos':
pos_samples.append(graph)
if random_samples:
shuffle(pos_samples)
shuffle(neg_samples)
else:
random.Random(random_seed).shuffle(pos_samples)
random.Random(random_seed).shuffle(neg_samples)
train_graphs = pos_samples[:num_per_class_train] + neg_samples[:num_per_class_train]
eval_graphs = (pos_samples[num_per_class_train:num_per_class_train + num_per_class_eval] +
neg_samples[num_per_class_train:num_per_class_train + num_per_class_eval])
test_graphs = (pos_samples[num_per_class_train + num_per_class_eval:] +
neg_samples[num_per_class_train + num_per_class_eval:])
elif dataset == 'snippets':
sample_dict = {
'0': [],
'1': [],
'2': [],
'3': [],
'4': [],
'5': [],
'6': [],
'7': [],
}
for graph_name in get_saved_graphs(dataset=dataset):
graph = load_graph(graph_name, dataset)
if graph['y'].shape[0] != 1 or graph['x'].shape[0] < 3:
continue
sample_dict[graph_name[0]].append(graph)
train_graphs = []
eval_graphs = []
test_graphs = []
for label in list(sample_dict.keys()):
if random_samples:
shuffle(sample_dict[label])
else:
random.Random(random_seed).shuffle(sample_dict[label])
train_graphs += sample_dict[label][:num_per_class_train]
eval_graphs += sample_dict[label][num_per_class_train:num_per_class_train + num_per_class_eval]
test_graphs += sample_dict[label][num_per_class_train + num_per_class_eval:]
elif dataset == 'tag_my_news':
sample_dict = {
'0': [],
'1': [],
'2': [],
'3': [],
'4': [],
'5': [],
'6': [],
}
for graph_name in get_saved_graphs(dataset=dataset):
graph = load_graph(graph_name, dataset)
if graph['y'].shape[0] != 1 or graph['x'].shape[0] < 3:
continue
sample_dict[graph_name[0]].append(graph)
train_graphs = []
eval_graphs = []
test_graphs = []
for label in list(sample_dict.keys()):
if random_samples:
shuffle(sample_dict[label])
else:
random.Random(random_seed).shuffle(sample_dict[label])
train_graphs += sample_dict[label][:num_per_class_train]
eval_graphs += sample_dict[label][num_per_class_train:num_per_class_train + num_per_class_eval]
test_graphs += sample_dict[label][num_per_class_train + num_per_class_eval:]
if n_hop_neighborhood != 3:
train_graphs = [change_n_hop_neighborhood(g, n_hop_neighborhood) for g in train_graphs]
eval_graphs = [change_n_hop_neighborhood(g, n_hop_neighborhood) for g in eval_graphs]
test_graphs = [change_n_hop_neighborhood(g, n_hop_neighborhood) for g in test_graphs]
if random_samples:
shuffle(train_graphs)
shuffle(eval_graphs)
shuffle(test_graphs)
else:
random.Random(random_seed).shuffle(train_graphs)
random.Random(random_seed).shuffle(eval_graphs)
random.Random(random_seed).shuffle(test_graphs)
return train_graphs, eval_graphs, test_graphs
if __name__ == '__main__':
all_files = [load_graph(x, 'mr') for x in get_saved_graphs('mr') if x.endswith('pickle')]
print(len(all_files))
for g in all_files[:10]:
print(g)
if g['y'].shape[0] != 1:
print(g)