-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
308 lines (259 loc) · 12.1 KB
/
utils.py
File metadata and controls
308 lines (259 loc) · 12.1 KB
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
import numpy as np
import pickle as pkl
import networkx as nx
import scipy.sparse as sp
import sys
import tensorflow as tf
from scipy.sparse import csgraph
from scipy.linalg import eigh
import gudhi as gd
# diagrams utils
def get_base_simplex(A):
num_vertices = A.shape[0]
st = gd.SimplexTree()
for i in range(num_vertices):
st.insert([i], filtration=-1e10)
for j in range(i + 1, num_vertices):
if A[i, j] > 0:
st.insert([i, j], filtration=-1e10)
return st.get_filtration()
# graph utils
def hks_signature(eigenvectors, eigenvals, time):
return np.square(eigenvectors).dot(np.diag(np.exp(-time * eigenvals))).sum(axis=1)
def apply_graph_extended_persistence(A, filtration_val, basesimplex):
num_vertices = A.shape[0]
(xs, ys) = np.where(np.triu(A))
num_edges = len(xs)
if len(filtration_val.shape) == 1:
min_val, max_val = filtration_val.min(), filtration_val.max()
else:
min_val = min([filtration_val[xs[i], ys[i]] for i in range(num_edges)])
max_val = max([filtration_val[xs[i], ys[i]] for i in range(num_edges)])
st = gd.SimplexTree()
st.set_dimension(2)
for simplex, filt in basesimplex:
st.insert(simplex=simplex + [-2], filtration=-3)
if len(filtration_val.shape) == 1:
if max_val == min_val:
fa = -.5 * np.ones(filtration_val.shape)
fd = .5 * np.ones(filtration_val.shape)
else:
fa = -2 + (filtration_val - min_val) / (max_val - min_val)
fd = 2 - (filtration_val - min_val) / (max_val - min_val)
for vid in range(num_vertices):
st.assign_filtration(simplex=[vid], filtration=fa[vid])
st.assign_filtration(simplex=[vid, -2], filtration=fd[vid])
else:
if max_val == min_val:
fa = -.5 * np.ones(filtration_val.shape)
fd = .5 * np.ones(filtration_val.shape)
else:
fa = -2 + (filtration_val - min_val) / (max_val - min_val)
fd = 2 - (filtration_val - min_val) / (max_val - min_val)
for eid in range(num_edges):
vidx, vidy = xs[eid], ys[eid]
#print(vidx, vidy)
st.assign_filtration(simplex=[vidx, vidy], filtration=fa[vidx, vidy])
st.assign_filtration(simplex=[vidx, vidy, -2], filtration=fd[vidx, vidy])
for vid in range(num_vertices):
if len(np.where(A[vid, :] > 0)[0]) > 0:
st.assign_filtration(simplex=[vid], filtration=min(fa[vid, np.where(A[vid, :] > 0)[0]]))
st.assign_filtration(simplex=[vid, -2], filtration=min(fd[vid, np.where(A[vid, :] > 0)[0]]))
st.make_filtration_non_decreasing()
distorted_dgm = st.persistence()
normal_dgm = dict()
normal_dgm["Ord0"], normal_dgm["Rel1"], normal_dgm["Ext0"], normal_dgm["Ext1"] = [], [], [], []
for point in range(len(distorted_dgm)):
dim, b, d = distorted_dgm[point][0], distorted_dgm[point][1][0], distorted_dgm[point][1][1]
pt_type = "unknown"
if (-2 <= b <= -1 and -2 <= d <= -1) or (b == -.5 and d == -.5):
pt_type = "Ord" + str(dim)
if (1 <= b <= 2 and 1 <= d <= 2) or (b == .5 and d == .5):
pt_type = "Rel" + str(dim)
if (-2 <= b <= -1 and 1 <= d <= 2) or (b == -.5 and d == .5):
pt_type = "Ext" + str(dim)
if np.isinf(d):
continue
else:
b, d = min_val + (2 - abs(b)) * (max_val - min_val), min_val + (2 - abs(d)) * (max_val - min_val)
if b <= d:
normal_dgm[pt_type].append(tuple([distorted_dgm[point][0], tuple([b, d])]))
else:
normal_dgm[pt_type].append(tuple([distorted_dgm[point][0], tuple([d, b])]))
dgmOrd0 = np.array([normal_dgm["Ord0"][point][1] for point in range(len(normal_dgm["Ord0"]))])
dgmExt0 = np.array([normal_dgm["Ext0"][point][1] for point in range(len(normal_dgm["Ext0"]))])
dgmRel1 = np.array([normal_dgm["Rel1"][point][1] for point in range(len(normal_dgm["Rel1"]))])
dgmExt1 = np.array([normal_dgm["Ext1"][point][1] for point in range(len(normal_dgm["Ext1"]))])
if dgmOrd0.shape[0] == 0:
dgmOrd0 = np.zeros([0, 2])
if dgmExt1.shape[0] == 0:
dgmExt1 = np.zeros([0, 2])
if dgmExt0.shape[0] == 0:
dgmExt0 = np.zeros([0, 2])
if dgmRel1.shape[0] == 0:
dgmRel1 = np.zeros([0, 2])
return dgmOrd0, dgmExt0, dgmRel1, dgmExt1
#print(egvectors.dot(np.diag(np.exp(-1. * egvals))).dot(egvectors.T)) # similar to single_wavelet_generator
def single_wavelet_generator(A, heat_coefficient, node):
"""
Calculating the characteristic function for a given node, using the eigendecomposition.
:param node: Node that is being embedded.
"""
L = csgraph.laplacian(A, normed=True)
eigen_values, eigen_vectors = eigh(L)
number_of_nodes = A.shape[0]
impulse = np.zeros((number_of_nodes))
impulse[node] = 1.0
diags = np.diag(np.exp(-heat_coefficient * eigen_values))
eigen_diag = np.dot(eigen_vectors, diags)
waves = np.dot(eigen_diag, np.transpose(eigen_vectors))
wavelet_coefficients = np.dot(waves, impulse)
return wavelet_coefficients
A = np.array([[0,1,1,1,0,0],[1,0,1,0,0,0],[1,1,0,0,1,0],[1,0,0,0,1,0],[0,0,1,1,0,1],[0,0,0,0,1,0]], dtype = np.float32)
def LDgm(A, hks_time):
L = csgraph.laplacian(A, normed=True)
egvals, egvectors = eigh(L)
basesimplex = get_base_simplex(A)
filtration_val = hks_signature(egvectors, egvals, time=hks_time)
bnds = [0., 1., 0., 1.]
dgmOrd0, dgmExt0, dgmRel1, dgmExt1 = apply_graph_extended_persistence(A, filtration_val, basesimplex)
DD = np.array([[bnds[0], bnds[2]], [bnds[1], bnds[2]], [bnds[0], bnds[3]], [bnds[1], bnds[3]]])
LDgm = np.vstack([dgmOrd0, dgmExt0, dgmExt1, dgmRel1, DD])
return LDgm# dgmOrd0, dgmExt0, dgmRel1, dgmExt1, LDgm
def LDgm_edge_weights(A, hks_time):
basesimplex = get_base_simplex(A)
filtration_val = hks_time
bnds = [0., 1., 0., 1.]
dgmOrd0, dgmExt0, dgmRel1, dgmExt1 = apply_graph_extended_persistence(A, filtration_val, basesimplex)
DD = np.array([[bnds[0], bnds[2]], [bnds[1], bnds[2]], [bnds[0], bnds[3]], [bnds[1], bnds[3]]])
LDgm = np.vstack([dgmOrd0, dgmExt0, dgmExt1, dgmRel1, DD])
return LDgm# dgmOrd0, dgmExt0, dgmRel1, dgmExt1, LDgm
def parse_index_file(filename):
"""Parse index file."""
index = []
for line in open(filename):
index.append(int(line.strip()))
return index
def sample_mask(idx, l):
"""Create mask."""
mask = np.zeros(l)
mask[idx] = 1
return np.array(mask, dtype=np.bool)
def load_data(dataset_str):
names = ['x', 'y', 'tx', 'ty', 'allx', 'ally', 'graph']
objects = []
for i in range(len(names)):
with open("./data/ind.{}.{}".format(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("./data/ind.{}.test.index".format(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))
#adj_mat = adj.toarray()
#dgm_low = LDgm(adj_mat, hks_time=t_1) # 0.1 is better than 0.01
#dgm_high = LDgm(adj_mat, hks_time=t_2)
labels = np.vstack((ally, ty))
labels[test_idx_reorder, :] = labels[test_idx_range, :]
idx_test = test_idx_range.tolist()
idx_train = range(len(y))
idx_val = range(len(y), len(y)+500)
train_mask = sample_mask(idx_train, labels.shape[0])
val_mask = sample_mask(idx_val, labels.shape[0])
test_mask = sample_mask(idx_test, labels.shape[0])
y_train = np.zeros(labels.shape)
y_val = np.zeros(labels.shape)
y_test = np.zeros(labels.shape)
y_train[train_mask, :] = labels[train_mask, :]
y_val[val_mask, :] = labels[val_mask, :]
y_test[test_mask, :] = labels[test_mask, :]
return adj, features, y_train, y_val, y_test, train_mask, val_mask, test_mask, labels
def sparse_to_tuple(sparse_mx):
"""Convert sparse matrix to tuple representation."""
def to_tuple(mx):
if not sp.isspmatrix_coo(mx):
mx = mx.tocoo()
coords = np.vstack((mx.row, mx.col)).transpose()
values = mx.data
shape = mx.shape
return coords, values, shape
if isinstance(sparse_mx, list):
for i in range(len(sparse_mx)):
sparse_mx[i] = to_tuple(sparse_mx[i])
else:
sparse_mx = to_tuple(sparse_mx)
return sparse_mx
def preprocess_features(features):
"""Row-normalize feature matrix and convert to tuple representation"""
rowsum = np.array(features.sum(1))
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 sparse_to_tuple(features)
def normalize_adj(adj, alpha):
"""Symmetrically normalize adjacency matrix."""
adj = sp.coo_matrix(adj)
rowsum = np.array(adj.sum(1))
d_inv_sqrt = np.power(rowsum, alpha).flatten()
d_inv_sqrt[np.isinf(d_inv_sqrt)] = 0.
d_mat_inv_sqrt = sp.diags(d_inv_sqrt)
return adj.dot(d_mat_inv_sqrt).transpose().dot(d_mat_inv_sqrt).tocoo()
def normalize_weighted_adj(adj, weighted_adj, alpha):
"""Symmetrically normalize adjacency matrix."""
adj = sp.coo_matrix(adj)
weighted_adj = sp.coo_matrix(weighted_adj)
rowsum = np.array(adj.sum(1))
d_inv_sqrt = np.power(rowsum, alpha).flatten()
d_inv_sqrt[np.isinf(d_inv_sqrt)] = 0.
d_mat_inv_sqrt = sp.diags(d_inv_sqrt)
return weighted_adj.dot(d_mat_inv_sqrt).transpose().dot(d_mat_inv_sqrt).tocoo()
def preprocess_adj(adj, alpha):
"""Preprocessing of adjacency matrix for simple GCN model and conversion to tuple representation."""
adj_normalized = normalize_adj(adj + sp.eye(adj.shape[0]), alpha)
return sparse_to_tuple(adj_normalized)
def preprocess_untuple_adj(adj, alpha):
"""Preprocessing of adjacency matrix for simple GCN model and conversion to tuple representation."""
adj_normalized = normalize_adj(adj + sp.eye(adj.shape[0]), alpha)
return adj_normalized
def laplacian_adj(adj, alpha):
"""Preprocessing of adjacency matrix for simple GCN model and conversion to tuple representation."""
adj_normalized = normalize_adj(adj + sp.eye(adj.shape[0]), alpha)
return adj_normalized
def construct_feed_dict(features, support, labels, labels_mask, placeholders, adj):#upper_topo_mask
"""Construct feed dictionary."""
feed_dict = dict()
feed_dict.update({placeholders['labels']: labels})
feed_dict.update({placeholders['labels_mask']: labels_mask})
#feed_dict.update({placeholders['upper_topo_mask']: upper_topo_mask})
feed_dict.update({placeholders['features']: features})
feed_dict.update({placeholders['support'][i]: support[i] for i in range(len(support))})
feed_dict.update({placeholders['num_features_nonzero']: features[1].shape})
return feed_dict
def masked_softmax_cross_entropy(preds, labels, mask):
loss = tf.nn.softmax_cross_entropy_with_logits(logits=preds, labels=labels)
mask = tf.cast(mask, dtype=tf.float32)
mask /= tf.reduce_mean(mask)
loss *= mask
return tf.reduce_mean(loss)
def masked_accuracy(preds, labels, mask):
correct_prediction = tf.equal(tf.argmax(preds, 1), tf.argmax(labels, 1))
accuracy_all = tf.cast(correct_prediction, tf.float32)
mask = tf.cast(mask, dtype=tf.float32)
mask /= tf.reduce_mean(mask)
accuracy_all *= mask
return tf.reduce_mean(accuracy_all)