forked from mengzaiqiao/CAN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreprocessing.py
132 lines (120 loc) · 4.83 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
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
import numpy as np
import scipy.sparse as sp
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 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, features_orig, placeholders):
# construct feed dictionary
feed_dict = dict()
feed_dict.update({placeholders['features']: features})
feed_dict.update({placeholders['features_orig']: features_orig})
feed_dict.update({placeholders['adj']: adj_normalized})
feed_dict.update({placeholders['adj_orig']: adj})
return feed_dict
def mask_test_edges(adj):
adj_row = adj.nonzero()[0]
adj_col = adj.nonzero()[1]
edges = []
edges_dic = {}
for i in range(len(adj_row)):
edges.append([adj_row[i], adj_col[i]])
edges_dic[(adj_row[i], adj_col[i])] = 1
false_edges_dic = {}
num_test = int(np.floor(len(edges) / 10.))
num_val = int(np.floor(len(edges) / 20.))
all_edge_idx = np.arange(len(edges))
np.random.shuffle(all_edge_idx)
val_edge_idx = all_edge_idx[:num_val]
test_edge_idx = all_edge_idx[num_val:(num_val + num_test)]
edges = np.array(edges)
test_edges = edges[test_edge_idx]
val_edges = edges[val_edge_idx]
train_edges = np.delete(edges, np.hstack([test_edge_idx, val_edge_idx]), axis=0)
test_edges_false = []
val_edges_false = []
while len(test_edges_false) < num_test or len(val_edges_false) < num_val:
i = np.random.randint(0, adj.shape[0])
j = np.random.randint(0, adj.shape[0])
if (i, j) in edges_dic:
continue
if (j, i) in edges_dic:
continue
if (i, j) in false_edges_dic:
continue
if (j, i) in false_edges_dic:
continue
else:
false_edges_dic[(i, j)] = 1
false_edges_dic[(j, i)] = 1
if np.random.random_sample() > 0.333 :
if len(test_edges_false) < num_test :
test_edges_false.append((i, j))
else:
if len(val_edges_false) < num_val :
val_edges_false.append([i, j])
else:
if len(val_edges_false) < num_val :
val_edges_false.append([i, j])
else:
if len(test_edges_false) < num_test :
test_edges_false.append([i, j])
data = np.ones(train_edges.shape[0])
adj_train = sp.csr_matrix((data, (train_edges[:, 0], train_edges[:, 1])), shape=adj.shape)
adj_train = adj_train + adj_train.T
return adj_train, train_edges, val_edges, val_edges_false, test_edges, test_edges_false
def mask_test_feas(features):
fea_row = features.nonzero()[0]
fea_col = features.nonzero()[1]
feas = []
feas_dic = {}
for i in range(len(fea_row)):
feas.append([fea_row[i], fea_col[i]])
feas_dic[(fea_row[i], fea_col[i])] = 1
false_feas_dic = {}
num_test = int(np.floor(len(feas) / 10.))
num_val = int(np.floor(len(feas) / 20.))
all_fea_idx = np.arange(len(feas))
np.random.shuffle(all_fea_idx)
val_fea_idx = all_fea_idx[:num_val]
test_fea_idx = all_fea_idx[num_val:(num_val + num_test)]
feas = np.array(feas)
test_feas = feas[test_fea_idx]
val_feas = feas[val_fea_idx]
train_feas = np.delete(feas, np.hstack([test_fea_idx, val_fea_idx]), axis=0)
test_feas_false = []
val_feas_false = []
while len(test_feas_false) < num_test or len(val_feas_false) < num_val:
i = np.random.randint(0, features.shape[0])
j = np.random.randint(0, features.shape[1])
if (i, j) in feas_dic:
continue
if (i, j) in false_feas_dic:
continue
else:
false_feas_dic[(i, j)] = 1
if np.random.random_sample() > 0.333 :
if len(test_feas_false) < num_test :
test_feas_false.append([i, j])
else:
if len(val_feas_false) < num_val :
val_feas_false.append([i, j])
else:
if len(val_feas_false) < num_val :
val_feas_false.append([i, j])
else:
if len(test_feas_false) < num_test :
test_feas_false.append([i, j])
data = np.ones(train_feas.shape[0])
fea_train = sp.csr_matrix((data, (train_feas[:, 0], train_feas[:, 1])), shape=features.shape)
return fea_train, train_feas, val_feas, val_feas_false, test_feas, test_feas_false