-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdata_utils.py
324 lines (246 loc) · 11 KB
/
data_utils.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
import torch
import pickle
import os
import ipdb
import numpy as np
import pandas as pd
from torch_geometric.data import Data
from torch_geometric.data import InMemoryDataset
from torch_sparse import coalesce
from sklearn.feature_extraction.text import CountVectorizer
def load_LE_dataset(path, dataset):
# load edges, features, and labels.
print('Loading {} dataset...'.format(dataset))
file_name = f'{dataset}.content'
p2idx_features_labels = os.path.join(path, file_name)
idx_features_labels = np.genfromtxt(p2idx_features_labels,
dtype=np.dtype(str))
features = sp.csr_matrix(idx_features_labels[:, 1:-1], dtype=np.float32)
labels = torch.LongTensor(idx_features_labels[:, -1].astype(float))
print ('load features')
# build graph
idx = np.array(idx_features_labels[:, 0], dtype=np.int32)
idx_map = {j: i for i, j in enumerate(idx)}
file_name = f'{dataset}.edges'
p2edges_unordered = os.path.join(path, file_name)
edges_unordered = np.genfromtxt(p2edges_unordered,
dtype=np.int32)
edges = np.array(list(map(idx_map.get, edges_unordered.flatten())),
dtype=np.int32).reshape(edges_unordered.shape)
print ('load edges')
projected_features = torch.FloatTensor(np.array(features.todense()))
# From adjacency matrix to edge_list
edge_index = edges.T
assert edge_index[0].max() == edge_index[1].min() - 1
# check if values in edge_index is consecutive. i.e. no missing value for node_id/he_id.
assert len(np.unique(edge_index)) == edge_index.max() + 1
num_nodes = edge_index[0].max() + 1
num_he = edge_index[1].max() - num_nodes + 1
edge_index = np.hstack((edge_index, edge_index[::-1, :]))
# build torch data class
data = Data(
x = torch.FloatTensor(np.array(features[:num_nodes].todense())),
edge_index = torch.LongTensor(edge_index),
y = labels[:num_nodes])
# data.coalesce()
# There might be errors if edge_index.max() != num_nodes.
# used user function to override the default function.
# the following will also sort the edge_index and remove duplicates.
total_num_node_id_he_id = len(np.unique(edge_index))
data.edge_index, data.edge_attr = coalesce(data.edge_index,
None,
total_num_node_id_he_id,
total_num_node_id_he_id)
data.num_features = data.x.shape[-1]
data.num_classes = len(np.unique(labels[:num_nodes].numpy()))
data.num_nodes = num_nodes
data.num_hyperedges = num_he
return data
def load_citation_dataset(path, dataset):
'''
this will read the citation dataset from HyperGCN, and convert it edge_list to
[[ -V- | -E- ]
[ -E- | -V- ]]
'''
print(f'Loading hypergraph dataset from hyperGCN: {dataset}')
# first load node features:
with open(os.path.join(path, 'features.pickle'), 'rb') as f:
features = pickle.load(f)
features = features.todense()
# then load node labels:
with open(os.path.join(path, 'labels.pickle'), 'rb') as f:
labels = pickle.load(f)
num_nodes, feature_dim = features.shape
assert num_nodes == len(labels)
print(f'number of nodes:{num_nodes}, feature dimension: {feature_dim}')
features = torch.FloatTensor(features)
labels = torch.LongTensor(labels)
# The last, load hypergraph.
with open(os.path.join(path, 'hypergraph.pickle'), 'rb') as f:
# hypergraph in hyperGCN is in the form of a dictionary.
# { hyperedge: [list of nodes in the he], ...}
hypergraph = pickle.load(f)
print(f'number of hyperedges: {len(hypergraph)}')
edge_idx = num_nodes
node_list = []
edge_list = []
for he in hypergraph.keys():
cur_he = hypergraph[he]
cur_size = len(cur_he)
node_list += list(cur_he)
edge_list += [edge_idx] * cur_size
edge_idx += 1
edge_index = np.array([ node_list + edge_list,
edge_list + node_list], dtype = np.int)
edge_index = torch.LongTensor(edge_index)
data = Data(x = features,
edge_index = edge_index,
y = labels)
# data.coalesce()
# There might be errors if edge_index.max() != num_nodes.
# used user function to override the default function.
# the following will also sort the edge_index and remove duplicates.
total_num_node_id_he_id = edge_index.max() + 1
data.edge_index, data.edge_attr = coalesce(data.edge_index,
None,
total_num_node_id_he_id,
total_num_node_id_he_id)
data.num_features = features.shape[-1]
data.num_classes = len(np.unique(labels.numpy()))
data.num_nodes = num_nodes
data.num_hyperedges = len(hypergraph)
return data
def load_yelp_dataset(path, dataset, name_dictionary_size = 1000):
'''
this will read the yelp dataset from source files, and convert it edge_list to
[[ -V- | -E- ]
[ -E- | -V- ]]
each node is a restaurant, a hyperedge represent a set of restaurants one user had been to.
node features:
- latitude, longitude
- state, in one-hot coding.
- city, in one-hot coding.
- name, in bag-of-words
node label:
- average stars from 2-10, converted from original stars which is binned in x.5, min stars = 1
'''
print(f'Loading hypergraph dataset from {dataset}')
# first load node features:
# load longtitude and latitude of restaurant.
latlong = pd.read_csv(os.path.join(path, 'yelp_restaurant_latlong.csv')).values
# city - zipcode - state integer indicator dataframe.
loc = pd.read_csv(os.path.join(path, 'yelp_restaurant_locations.csv'))
state_int = loc.state_int.values
city_int = loc.city_int.values
num_nodes = loc.shape[0]
state_1hot = np.zeros((num_nodes, state_int.max()))
state_1hot[np.arange(num_nodes), state_int - 1] = 1
city_1hot = np.zeros((num_nodes, city_int.max()))
city_1hot[np.arange(num_nodes), city_int - 1] = 1
# convert restaurant name into bag-of-words feature.
vectorizer = CountVectorizer(max_features = name_dictionary_size, stop_words = 'english', strip_accents = 'ascii')
res_name = pd.read_csv(os.path.join(path, 'yelp_restaurant_name.csv')).values.flatten()
name_bow = vectorizer.fit_transform(res_name).todense()
features = np.hstack([latlong, state_1hot, city_1hot, name_bow])
# then load node labels:
df_labels = pd.read_csv(os.path.join(path, 'yelp_restaurant_business_stars.csv'))
labels = df_labels.values.flatten()
num_nodes, feature_dim = features.shape
assert num_nodes == len(labels)
print(f'number of nodes:{num_nodes}, feature dimension: {feature_dim}')
features = torch.FloatTensor(features)
labels = torch.LongTensor(labels)
# The last, load hypergraph.
# Yelp restaurant review hypergraph is store in a incidence matrix.
H = pd.read_csv(os.path.join(path, 'yelp_restaurant_incidence_H.csv'))
node_list = H.node.values - 1
edge_list = H.he.values - 1 + num_nodes
edge_index = np.vstack([node_list, edge_list])
edge_index = np.hstack([edge_index, edge_index[::-1, :]])
edge_index = torch.LongTensor(edge_index)
data = Data(x = features,
edge_index = edge_index,
y = labels)
assert data.y.min().item() == 0
data.y = data.y - data.y.min()
# data.coalesce()
# There might be errors if edge_index.max() != num_nodes.
# used user function to override the default function.
# the following will also sort the edge_index and remove duplicates.
total_num_node_id_he_id = edge_index.max() + 1
data.edge_index, data.edge_attr = coalesce(data.edge_index,
None,
total_num_node_id_he_id,
total_num_node_id_he_id)
data.num_features = features.shape[-1]
data.num_classes = len(np.unique(labels.numpy()))
data.num_nodes = num_nodes
data.num_hyperedges = H.he.values.max()
return data
def load_cornell_dataset(path, dataset, feature_noise = 0.1, feature_dim = None):
'''
this will read the yelp dataset from source files, and convert it edge_list to
[[ -V- | -E- ]
[ -E- | -V- ]]
each node is a restaurant, a hyperedge represent a set of restaurants one user had been to.
node features:
- add gaussian noise with sigma = nosie, mean = one hot coded label.
node label:
- average stars from 2-10, converted from original stars which is binned in x.5, min stars = 1
'''
print(f'Loading hypergraph dataset from cornell: {dataset}')
# first load node labels
df_labels = pd.read_csv(os.path.join(path, f'node-labels-{dataset}.txt'), names = ['node_label'])
num_nodes = df_labels.shape[0]
labels = df_labels.values.flatten()
# then create node features.
num_classes = df_labels.values.max()
features = np.zeros((num_nodes, num_classes))
features[np.arange(num_nodes), labels - 1] = 1
if feature_dim is not None:
num_row, num_col = features.shape
zero_col = np.zeros((num_row, feature_dim - num_col), dtype = features.dtype)
features = np.hstack((features, zero_col))
features = np.random.normal(features, feature_noise, features.shape)
print(f'number of nodes:{num_nodes}, feature dimension: {features.shape[1]}')
features = torch.FloatTensor(features)
labels = torch.LongTensor(labels)
labels = labels - labels.min() # shift label to 0
# The last, load hypergraph.
# Corenll datasets are stored in lines of hyperedges. Each line is the set of nodes for that edge.
p2hyperedge_list = os.path.join(path, f'hyperedges-{dataset}.txt')
node_list = []
he_list = []
he_id = num_nodes
with open(p2hyperedge_list, 'r') as f:
for line in f:
if line[-1] == '\n':
line = line[:-1]
cur_set = line.split(',')
cur_set = [int(x) for x in cur_set]
node_list += cur_set
he_list += [he_id] * len(cur_set)
he_id += 1
# shift node_idx to start with 0.
node_idx_min = np.min(node_list)
node_list = [x - node_idx_min for x in node_list]
edge_index = [node_list + he_list,
he_list + node_list]
edge_index = torch.LongTensor(edge_index)
data = Data(x = features,
edge_index = edge_index,
y = labels)
assert data.y.min().item() == 0
# There might be errors if edge_index.max() != num_nodes.
# used user function to override the default function.
# the following will also sort the edge_index and remove duplicates.
total_num_node_id_he_id = edge_index.max() + 1
data.edge_index, data.edge_attr = coalesce(data.edge_index,
None,
total_num_node_id_he_id,
total_num_node_id_he_id)
data.num_features = features.shape[-1]
data.num_classes = len(np.unique(labels.numpy()))
data.num_nodes = num_nodes
data.num_hyperedges = he_id - num_nodes
return data