-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdataset.py
253 lines (217 loc) · 9.6 KB
/
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# adopted from https://github.com/CUAI/Non-Homophily-Benchmarks/blob/main/dataset.py
from parse import args
import pickle as pkl
import networkx as nx
import scipy.sparse as sp
import numpy as np
import scipy.io
import scipy
import sys
import csv
import json
import torch
SMALL_OFFSET = 1e-10
DATAPATH = args.DATAPATH
class NCDataset(object):
def __init__(self, name):
self.name = name
self.graph = {}
self.label = None
def __getitem__(self, idx):
assert idx == 0, 'This dataset has only one graph'
return self.graph, self.label
def __len__(self):
return 1
def __repr__(self):
return '{}({})'.format(self.__class__.__name__, len(self))
def load_nc_dataset(dataname, sub_dataname=''):
""" Loader for NCDataset
Returns NCDataset
"""
if dataname == 'twitch-e':
if sub_dataname not in ('DE', 'ENGB', 'ES', 'FR', 'PTBR', 'RU', 'TW'):
print('Invalid sub_dataname, deferring to DE graph')
sub_dataname = 'DE'
dataset = load_twitch_dataset(sub_dataname)
elif dataname in ('chameleon', 'cornell', 'film', 'squirrel', 'texas', 'wisconsin'):
dataset = load_geom_gcn_dataset(dataname)
elif dataname in ('cora', 'citeseer', 'pubmed'):
dataset = load_citation_dataset(dataname)
elif dataname in ("computers", "photo"):
dataset = load_amazon_dataset(dataname)
else:
raise ValueError('Invalid dataname')
return dataset
def load_twitch_dataset(lang):
assert lang in ('DE', 'ENGB', 'ES', 'FR', 'PTBR', 'RU', 'TW'), 'Invalid dataset'
A, label, features = load_twitch(lang)
dataset = NCDataset(lang)
edge_index = torch.tensor(A.nonzero(), dtype=torch.long)
node_feat = torch.tensor(features, dtype=torch.float)
num_nodes = node_feat.shape[0]
dataset.graph = {'edge_index': edge_index,
'edge_feat': None,
'node_feat': node_feat,
'num_nodes': num_nodes}
dataset.label = torch.tensor(label)
return dataset
def load_amazon_dataset(datname):
assert datname in ('computers', 'photo'), 'Invalid dataset'
loader = dict(np.load(f"{DATAPATH}amazon_dataset/amazon_electronics_{datname}.npz"))
A = sp.csr_matrix((loader['adj_data'], loader['adj_indices'], loader['adj_indptr']), shape=loader['adj_shape'])
node_feat = sp.csr_matrix((loader['attr_data'], loader['attr_indices'], loader['attr_indptr']), shape=loader['attr_shape']).todense()
label = loader['labels']
dataset = NCDataset(datname)
edge_index = torch.tensor(A.nonzero(), dtype=torch.long)
node_feat = torch.tensor(node_feat, dtype=torch.float)
dataset.graph = {'edge_index': edge_index,
'node_feat': node_feat,
'edge_feat': None,
'num_nodes': len(label)}
label = torch.tensor(label, dtype=torch.long)
dataset.label = label
return dataset
def load_geom_gcn_dataset(name):
fulldata = scipy.io.loadmat(f'{DATAPATH}{name}.mat')
edge_index = fulldata['edge_index']
node_feat = fulldata['node_feat']
label = np.array(fulldata['label'], dtype=np.int).flatten()
num_nodes = node_feat.shape[0]
dataset = NCDataset(name)
edge_index = torch.tensor(edge_index, dtype=torch.long)
node_feat = torch.tensor(node_feat, dtype=torch.float)
dataset.graph = {'edge_index': edge_index,
'node_feat': node_feat,
'edge_feat': None,
'num_nodes': num_nodes}
label = torch.tensor(label, dtype=torch.long)
dataset.label = label
return dataset
def load_citation_dataset(dataset_str):
# adopted from https://github.com/tkipf/gcn/blob/master/gcn/utils.py
"""
Loads input data from gcn/data directory
ind.dataset_str.x => the feature vectors of the training instances as scipy.sparse.csr.csr_matrix object;
ind.dataset_str.tx => the feature vectors of the test instances as scipy.sparse.csr.csr_matrix object;
ind.dataset_str.allx => the feature vectors of both labeled and unlabeled training instances
(a superset of ind.dataset_str.x) as scipy.sparse.csr.csr_matrix object;
ind.dataset_str.y => the one-hot labels of the labeled training instances as numpy.ndarray object;
ind.dataset_str.ty => the one-hot labels of the test instances as numpy.ndarray object;
ind.dataset_str.ally => the labels for instances in ind.dataset_str.allx as numpy.ndarray object;
ind.dataset_str.graph => a dict in the format {index: [index_of_neighbor_nodes]} as collections.defaultdict
object;
ind.dataset_str.test.index => the indices of test instances in graph, for the inductive setting as list object.
All objects above must be saved using python pickle module.
:param dataset_str: Dataset name
:return: All data input files loaded (as well the training/test data).
"""
names = ['x', 'y', 'tx', 'ty', 'allx', 'ally', 'graph']
objects = []
for i in range(len(names)):
with open("{}ind.{}.{}".format(DATAPATH + "citation_net/", dataset_str, 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("{}ind.{}.test.index".format(DATAPATH + "citation_net/", dataset_str))
test_idx_range = np.sort(test_idx_reorder)
if dataset_str == '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))
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))
stand_idx_dic = {"tst_idx": idx_test, "trn_idx": idx_train, "val_idx": idx_val}
edge_index = torch.tensor(adj.nonzero(), dtype=torch.long)
node_feat = torch.tensor(preprocess_features(features).todense(), dtype=torch.float)
labels = categorical_label(labels)
num_nodes = node_feat.shape[0]
dataset = NCDataset(dataset_str)
dataset.graph = {'edge_index': edge_index,
'node_feat': node_feat,
'edge_feat': None,
'num_nodes': num_nodes,
"stand_idx_dic": stand_idx_dic}
labels = torch.tensor(labels, dtype=torch.long)
dataset.label = labels
return dataset
def load_twitch(lang):
# adopted from https://github.com/CUAI/Non-Homophily-Benchmarks/blob/main/load_data.py
assert lang in ('DE', 'ENGB', 'ES', 'FR', 'PTBR', 'RU', 'TW'), 'Invalid dataset'
filepath = "{}twitch/{}".format(DATAPATH, lang)
label = []
node_ids = []
src = []
targ = []
uniq_ids = set()
with open(f"{filepath}/musae_{lang}_target.csv", 'r') as f:
reader = csv.reader(f)
next(reader)
for row in reader:
node_id = int(row[5])
# handle FR case of non-unique rows
if node_id not in uniq_ids:
uniq_ids.add(node_id)
label.append(int(row[2] == "True"))
node_ids.append(int(row[5]))
node_ids = np.array(node_ids, dtype=np.int)
with open(f"{filepath}/musae_{lang}_edges.csv", 'r') as f:
reader = csv.reader(f)
next(reader)
for row in reader:
src.append(int(row[0]))
targ.append(int(row[1]))
with open(f"{filepath}/musae_{lang}_features.json", 'r') as f:
j = json.load(f)
src = np.array(src)
targ = np.array(targ)
label = np.array(label)
inv_node_ids = {node_id: idx for (idx, node_id) in enumerate(node_ids)}
reorder_node_ids = np.zeros_like(node_ids)
for i in range(label.shape[0]):
reorder_node_ids[i] = inv_node_ids[i]
n = label.shape[0]
A = scipy.sparse.csr_matrix((np.ones(len(src)),
(np.array(src), np.array(targ))),
shape=(n, n))
features = np.zeros((n, 3170))
for node, feats in j.items():
if int(node) >= n:
continue
features[int(node), np.array(feats, dtype=int)] = 1
features = features[:, np.sum(features, axis=0) != 0] # remove zero cols
new_label = label[reorder_node_ids]
label = new_label
return A, label, features
def categorical_label(input):
r, c = np.where(input == 1)
labels = np.ones(input.shape[0]) * (-1)
labels[r] = c
return labels
def preprocess_features(features):
"""Row-normalize feature matrix and convert to tuple representation"""
rowsum = np.array(features.sum(1)) + SMALL_OFFSET
r_inv = np.power(rowsum, -1).flatten()
r_inv[np.isinf(r_inv)] = 0.
r_mat_inv = sp.diags(r_inv)
features = r_mat_inv.dot(features)
return features
def parse_index_file(filename):
"""Parse index file."""
index = []
for line in open(filename):
index.append(int(line.strip()))
return index