-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathpreprocessing.py
40 lines (32 loc) · 1.38 KB
/
preprocessing.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
import numpy as np
import scipy.sparse as sp
import pickle as pkl
def sparse_to_tuple(sparse_mx):
if not sp.isspmatrix_coo(sparse_mx):
sparse_mx = sparse_mx.tocoo()
coords = np.vstack((sparse_mx.row, sparse_mx.col)).transpose()
values = sparse_mx.data
shape = sparse_mx.shape
return coords, values, shape
def load_edges():
val_edges = np.load('../yale_val_edges.npy')
val_edges_false = np.load('../yale_val_edges_false.npy')
test_edges = np.load('../yale_test_edges.npy',)
test_edges_false = np.load('../yale_test_edges_false.npy')
with open('../yale_adj_train.pkl', 'rb') as handle:
adj_train = pkl.load(handle)
return adj_train, val_edges, val_edges_false, test_edges, test_edges_false
def preprocess_graph(adj):
adj = sp.coo_matrix(adj)
adj_ = adj + sp.eye(adj.shape[0])
rowsum = np.array(adj_.sum(1))
degree_mat_inv_sqrt = sp.diags(np.power(rowsum, -0.5).flatten())
adj_normalized = adj_.dot(degree_mat_inv_sqrt).transpose().dot(degree_mat_inv_sqrt).tocoo()
return sparse_to_tuple(adj_normalized)
def construct_feed_dict(adj_normalized, adj, features, placeholders):
# construct feed dictionary
feed_dict = dict()
feed_dict.update({placeholders['features']: features})
feed_dict.update({placeholders['adj']: adj_normalized})
feed_dict.update({placeholders['adj_orig']: adj})
return feed_dict