-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake_dataset.py
203 lines (165 loc) · 8.7 KB
/
make_dataset.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
# -*- coding: utf-8 -*-
import os
import pickle as pkl
import sys
import networkx as nx
import numpy as np
import scipy.sparse as sp
from dataio import load_dataset
from preprocess import to_binary_bag_of_words, remove_underrepresented_classes, \
eliminate_self_loops, binarize_labels
def is_binary_bag_of_words(features):
features_coo = features.tocoo()
return all(single_entry == 1.0 for _, _, single_entry in zip(features_coo.row, features_coo.col, features_coo.data))
def get_dataset(name, data_path, standardize, train_examples_per_class=None, val_examples_per_class=None):
print('load ' +name)
dataset_graph = load_dataset(data_path)
# some standardization preprocessing
if standardize:
dataset_graph = dataset_graph.standardize()
else:
dataset_graph = dataset_graph.to_undirected()
dataset_graph = eliminate_self_loops(dataset_graph)
if train_examples_per_class is not None and val_examples_per_class is not None:
if name == 'cora_full':
# cora_full has some classes that have very few instances. We have to remove these in order for
# split generation not to fail
dataset_graph = remove_underrepresented_classes(dataset_graph,
train_examples_per_class, val_examples_per_class)
dataset_graph = dataset_graph.standardize()
# To avoid future bugs: the above two lines should be repeated to a fixpoint, otherwise code below might
# fail. However, for cora_full the fixpoint is reached after one iteration, so leave it like this for now.
graph_adj, node_features, labels = dataset_graph.unpack()
labels = binarize_labels(labels)
# convert to binary bag-of-words feature representation if necessary
if not is_binary_bag_of_words(node_features):
print(f"Converting features of dataset {name} to binary bag-of-words representation.")
node_features = to_binary_bag_of_words(node_features)
# some assertions that need to hold for all datasets
# adj matrix needs to be symmetric
assert (graph_adj != graph_adj.T).nnz == 0
# features need to be binary bag-of-word vectors
assert is_binary_bag_of_words(node_features), f"Non-binary node_features entry!"
return graph_adj, node_features, labels
def get_train_val_test_split(random_state,
labels,
train_examples_per_class=None, val_examples_per_class=None,
test_examples_per_class=None,
train_size=None, val_size=None, test_size=None):
num_samples, num_classes = labels.shape
remaining_indices = list(range(num_samples))
if train_examples_per_class is not None:
train_indices = sample_per_class(random_state, labels, train_examples_per_class)
else:
# select train examples with no respect to class distribution
train_indices = random_state.choice(remaining_indices, train_size, replace=False)
if val_examples_per_class is not None:
val_indices = sample_per_class(random_state, labels, val_examples_per_class, forbidden_indices=train_indices)
else:
remaining_indices = np.setdiff1d(remaining_indices, train_indices)
val_indices = random_state.choice(remaining_indices, val_size, replace=False)
forbidden_indices = np.concatenate((train_indices, val_indices))
if test_examples_per_class is not None:
test_indices = sample_per_class(random_state, labels, test_examples_per_class,
forbidden_indices=forbidden_indices)
elif test_size is not None:
remaining_indices = np.setdiff1d(remaining_indices, forbidden_indices)
test_indices = random_state.choice(remaining_indices, test_size, replace=False)
else:
test_indices = np.setdiff1d(remaining_indices, forbidden_indices)
print(len(set(train_indices)), len(train_indices))
# assert that there are no duplicates in sets
assert len(set(train_indices)) == len(train_indices)
assert len(set(val_indices)) == len(val_indices)
assert len(set(test_indices)) == len(test_indices)
# assert sets are mutually exclusive
assert len(set(train_indices) - set(val_indices)) == len(set(train_indices))
assert len(set(train_indices) - set(test_indices)) == len(set(train_indices))
assert len(set(val_indices) - set(test_indices)) == len(set(val_indices))
if test_size is None and test_examples_per_class is None:
# all indices must be part of the split
assert len(np.concatenate((train_indices, val_indices, test_indices))) == num_samples
if train_examples_per_class is not None:
train_labels = labels[train_indices, :]
train_sum = np.sum(train_labels, axis=0)
# assert all classes have equal cardinality
assert np.unique(train_sum).size == 1
if val_examples_per_class is not None:
val_labels = labels[val_indices, :]
val_sum = np.sum(val_labels, axis=0)
# assert all classes have equal cardinality
assert np.unique(val_sum).size == 1
if test_examples_per_class is not None:
test_labels = labels[test_indices, :]
test_sum = np.sum(test_labels, axis=0)
# assert all classes have equal cardinality
assert np.unique(test_sum).size == 1
return train_indices, val_indices, test_indices
def sample_per_class(random_state, labels, num_examples_per_class, forbidden_indices=None):
num_samples, num_classes = labels.shape
sample_indices_per_class = {index: [] for index in range(num_classes)}
# get indices sorted by class
for class_index in range(num_classes):
for sample_index in range(num_samples):
if labels[sample_index, class_index] > 0.0:
if forbidden_indices is None or sample_index not in forbidden_indices:
sample_indices_per_class[class_index].append(sample_index)
# get specified number of indices for each class
return np.concatenate(
[random_state.choice(sample_indices_per_class[class_index], num_examples_per_class, replace=False)
for class_index in range(len(sample_indices_per_class))
])
"""
def get_split_feed_dicts(train_indices, val_indices, test_indices):
dataset_indices_placeholder = tf.placeholder(tf.int32, shape=[None], name='dataset_indices_placeholder')
train_feed = {dataset_indices_placeholder: train_indices}
trainval_feed = {dataset_indices_placeholder: train_indices}
val_feed = {dataset_indices_placeholder: val_indices}
test_feed = {dataset_indices_placeholder: test_indices}
return dataset_indices_placeholder, train_feed, trainval_feed, val_feed, test_feed
"""
def get_dataset_and_split_planetoid(dataset, data_path, _log):
def parse_index_file(filename):
"""Parse index file."""
index = []
for line in open(filename):
index.append(int(line.strip()))
return index
if _log is not None:
_log.info('Loading dataset %s.' % dataset)
names = ['x', 'y', 'tx', 'ty', 'allx', 'ally', 'graph']
objects = []
for i in range(len(names)):
with open(os.path.join(data_path, "ind.{}.{}".format(dataset, names[i])), 'rb') as f:
if sys.version_info > (3, 0):
objects.append(pkl.load(f, encoding='latin1'))
else:
objects.append(pkl.load(f))
x, y, tx, ty, allx, ally, graph = tuple(objects)
test_idx_reorder = parse_index_file(
os.path.join(data_path, "ind.{}.test.index".format(dataset))
)
test_idx_range = np.sort(test_idx_reorder)
if dataset == 'citeseer':
# Fix citeseer dataset (there are some isolated nodes in the graph)
# Find isolated nodes, add them as zero-vecs into the right position
test_idx_range_full = range(min(test_idx_reorder), max(test_idx_reorder) + 1)
tx_extended = sp.lil_matrix((len(test_idx_range_full), x.shape[1]))
tx_extended[test_idx_range - min(test_idx_range), :] = tx
tx = tx_extended
ty_extended = np.zeros((len(test_idx_range_full), y.shape[1]))
ty_extended[test_idx_range - min(test_idx_range), :] = ty
ty = ty_extended
features = sp.vstack((allx, tx)).tolil()
features[test_idx_reorder, :] = features[test_idx_range, :]
adj = nx.adjacency_matrix(nx.from_dict_of_lists(graph))
# cast!!!
adj = adj.astype(np.float32)
features = features.tocsr()
features = features.astype(np.float32)
labels = np.vstack((ally, ty))
labels[test_idx_reorder, :] = labels[test_idx_range, :]
idx_test = test_idx_range.tolist()
idx_train = list(range(len(y)))
idx_val = list(range(len(y), len(y) + 500))
return adj, features, labels, idx_train, idx_val, idx_test