-
Notifications
You must be signed in to change notification settings - Fork 8
/
hypergraph.py
395 lines (329 loc) · 13.1 KB
/
hypergraph.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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
'''
An object of class hypergraph is a list of tuples on a specified node set, which can be implicit.
It is equipped with methods for computing hypergraph moments of interest and running Markov Chain Monte Carlo.
'''
import numpy as np
import networkx as nx
from collections import Counter
from itertools import accumulate
from bisect import bisect
import random
import itertools
from scipy.special import binom
import random
class hypergraph:
def __init__(self, C, n_nodes = None, node_labels = None):
self.C = [tuple(sorted(f)) for f in C] # edge list
self.nodes = list(set([v for f in self.C for v in f])) # node list
self.n = len(self.nodes) # number of nodes
# optional node labels -- not really used for anything at the moment
if node_labels is not None:
self.node_labels = node_labels
# number of edges
self.m = len(self.C)
# node degree vector
D = np.zeros(self.n)
for f in self.C:
for v in f:
D[v] += 1
self.D = D
# edge dimension sequence
K = np.array([len(f) for f in self.C])
self.K = K
# bookkeeping for Monte Carlo
self.MH_rounds = 0
self.MH_steps = 0
self.acceptance_rate = 0
def node_degrees(self, by_dimension = False):
'''
Return a np.array() of node degrees. If by_dimension, return a 2d np.array()
in which each entry gives the number of edges of each dimension incident upon the given node.
'''
if not by_dimension:
return(self.D)
else:
D = np.zeros((len(self.D), max(self.K)))
for f in self.C:
for v in f:
D[v, len(f)-1] += 1
return(D)
def edge_dimensions(self):
'''
Return an np.array() of edge dimensions.
'''
return(self.K)
def node_dimension_matrix(self):
'''
Return a matrix in which the i,j entry gives the number of dimension j edges incident on node i.
'''
A = np.zeros((self.n, max([len(f) for f in self.C])+1))
for f in self.C:
for v in f:
A[v, len(f)] += 1
return(A)
def line_graph(self):
'''
Return a networkx Graph() in which each node corresponds to a hyperedge
and two nodes are linked if the corresponding edges intersect in the primal hypergraph.
'''
H = nx.Graph()
counts = Counter(self.C)
d = {v : counts[v] for v in counts}
H.add_nodes_from(d.keys())
nx.set_node_attributes(H, values = d, name = 'm')
node_list = list(H.nodes())
n_nodes = len(node_list)
for u in range(n_nodes):
for v in range(u+1, n_nodes):
j = len(set(node_list[u]).intersection(set(node_list[v])))
if j > 0:
H.add_edge(node_list[u],node_list[v], weight = j)
return(H)
def get_edges(self, node):
'''
Return a list of edges incident upon a specified node.
'''
return([f for f in self.C if node in f])
def remove_degeneracy(self, verbose = True):
'''
Use pairwise reshuffles to remove degenerate edges, as may be generated in stub-matching.
'''
m_degenerate = self.check_degeneracy()
while self.check_degeneracy() > 0:
for i in range(len(self.C)):
while is_degenerate(self.C[i]):
j = np.random.choice(range(len(self.C)))
f1, f2 = self.C[i], self.C[j]
self.C[i], self.C[j] = pairwise_reshuffle(f1, f2, True)
if verbose:
print(str(m_degenerate) + ' degeneracies removed, ' + str(self.check_degeneracy()) + ' remain.')
def MH(self, n_steps = 1000, verbose = True, label = 'edge', n_clash = 1, detailed = False, **kwargs):
'''
Conduct Markov Chain Monte Carlo in order to approximately sample from the space of appropriately-labeled graphs.
n_steps: number of steps to perform
verbose: if True, print a finishing message with descriptive summaries of the algorithm run.
label: the label space to use. Can take values in ['vertex' , 'stub', 'edge'].
n_clash: the number of clashes permitted when updating the edge counts in vertex-labeled MH. n_clash = 0 will be exact but very slow. n_clash >= 2 may lead to performance gains at the cost of decreased accuracy.
detailed: if True, preserve the number of edges of given dimension incident to each node
**kwargs: additional arguments passed to sample_fun
'''
if (label == 'edge') or (label == 'stub'):
self.stub_edge_MH(n_steps = n_steps, verbose = verbose, label = label, detailed = detailed, **kwargs)
elif label == 'vertex':
self.vertex_labeled_MH(n_steps = n_steps, verbose = verbose, n_clash = n_clash, detailed = detailed, **kwargs)
else:
print('not implemented')
def stub_edge_MH(self, n_steps = 1000, verbose = True, label = 'edge', detailed = False, message = True, **kwargs):
'''
See description of self.MH()
'''
C_new = [list(c) for c in self.C]
m = len(C_new)
proposal = proposal_generator(m, detailed)
def MH_step(label = 'edge'):
i, j, f1, f2, g1, g2 = proposal(C_new)
C_new[i] = sorted(g1)
C_new[j] = sorted(g2)
n = 0
n_rejected = 0
while n < n_steps:
MH_step()
n += 1
self.C = [tuple(sorted(f)) for f in C_new]
self.MH_steps += n
self.MH_rounds += 1
if message:
print(str(n_steps) + ' steps completed.')
def vertex_labeled_MH(self, n_steps = 10000, sample_every = 500, sample_fun = None, verbose = False, n_clash = 0, message = True, detailed = False, **kwargs):
'''
See description of self.MH()
'''
rand = np.random.rand
randint = np.random.randint
k = 0
done = False
c = Counter(self.C)
epoch_num = 0
n_rejected = 0
m = sum(c.values())
while not done:
# initialize epoch
l = list(c.elements())
add = []
remove = []
end_epoch = False
num_clash = 0
epoch_num += 1
# within each epoch
k_rand = 20000 # generate many random numbers at a time
k_ = 0
IJ = randint(0, m, k_rand)
A = rand(k_rand)
while True:
if k_ >= k_rand/2.0:
IJ = randint(0, m, k_rand)
A = rand(k_rand)
k_ = 0
i,j = (IJ[k_],IJ[k_+1])
k_ += 2
f1, f2 = l[i], l[j]
while f1 == f2:
i,j = (IJ[k_],IJ[k_+1])
k_ += 2
f1, f2 = l[i], l[j]
if detailed:
while len(f1) != len(f2):
i,j = (IJ[k_],IJ[k_+1])
k_ += 2
f1, f2 = l[i], l[j]
while f1 == f2:
i,j = (IJ[k_],IJ[k_+1])
k_ += 2
f1, f2 = l[i], l[j]
inter = 2**(-len((set(f1).intersection(set(f2)))))
if A[k_] > inter /(c[f1] * c[f2]):
n_rejected += 1
k += 1
else: # if proposal was accepted
g1, g2 = pairwise_reshuffle(f1, f2, True)
num_clash += remove.count(f1) + remove.count(f2)
if (num_clash >= n_clash) & (n_clash >=1):
break
else:
remove.append(f1)
remove.append(f2)
add.append(g1)
add.append(g2)
k += 1
if n_clash == 0:
break
add = Counter(add)
add.subtract(Counter(remove))
c.update(add)
done = k - n_rejected>=n_steps
if message:
print(str(epoch_num) + ' epochs completed, ' + str(k - n_rejected) + ' steps taken, ' + str(n_rejected) + ' steps rejected.')
self.C = [tuple(sorted(f)) for f in list(c.elements())]
self.MH_steps += k - n_rejected
self.MH_rounds += 1
self.acceptance_rate = (1.0*(k - n_rejected)) / (k)
def check_degeneracy(self):
'''
Find the number of degeneracies in self.C
'''
return np.sum([is_degenerate(f) for f in self.C])
def choose_nodes(self, n_samples, choice_function = 'uniform'):
'''
Utility function for choosing pairs of nodes from self.C, used in assortativity calculations.
'''
D = self.node_degrees()
def uniform(x):
i = np.random.randint(len(x))
j = i
while i == j:
j = np.random.randint(len(x))
return(np.array([x[i],x[j]]))
def top_2(x):
ind = np.argpartition(D[x,], -2)[-2:]
y = np.array(x)[ind]
random.shuffle(y)
return(y)
def top_bottom(x):
top = np.argmax(D[x,])
bottom = np.argmin(D[x,])
y = np.array(x)[[bottom, top]]
random.shuffle(y)
return(y)
choice_functions = {
'uniform': uniform,
'top_2' : top_2,
'top_bottom' : top_bottom,
'NA' : uniform
}
n = 0
v = []
while True:
edge = self.C[np.random.randint(self.m)]
if len(edge) < 2:
continue
x = choice_functions[choice_function](edge)
v.append(x)
n+=1
if n > n_samples:
break
return(v)
def assortativity(self, n_samples = 10, choice_function = 'uniform', method = 'pearson'):
'''
Compute the approximate degree assortativity of a hypergraph using the specified choice_function and method in ['spearman', 'pearson']
'''
D = self.node_degrees()
arr = np.array(self.choose_nodes(n_samples, choice_function))
arr = D[arr]
if method == 'spearman':
order = np.argsort(arr, axis = 0)
arr = np.argsort(order, axis = 0)
elif method == 'pearson':
arr = arr - 1
return(np.corrcoef(arr.T))[0,1]
is_degenerate = lambda x: len(set(x)) < len(x)
def proposal_generator(m, detailed = False):
'''
Propose a transition in stub- and edge-labeled MH.
'''
def proposal(edge_list):
i,j = np.random.randint(0,m,2)
f1, f2 = edge_list[i], edge_list[j]
if detailed:
while len(f1) != len(f2):
i,j = np.random.randint(0,m,2)
f1, f2 = edge_list[i], edge_list[j]
g1, g2 = pairwise_reshuffle(f1, f2, True)
return(i, j, f1, f2, g1, g2)
return(proposal)
def pairwise_reshuffle(f1, f2, preserve_dimensions = True):
'''
Randomly reshuffle the nodes of two edges while preserving their sizes.
'''
f = list(f1) + list(f2)
s = set(f)
intersection = set(f1).intersection(set(f2))
ix = list(intersection)
g1 = ix.copy()
g2 = ix.copy()
for v in ix:
f.remove(v)
f.remove(v)
for v in f:
if (len(g1) < len(f1)) & (len(g2) < len(f2)):
if np.random.rand() < .5:
g1.append(v)
else:
g2.append(v)
elif len(g1) < len(f1):
g1.append(v)
elif len(g2) < len(f2):
g2.append(v)
if len(g1) != len(f1):
print('oops')
print(f1, f2, g1, g2)
return (tuple(sorted(g1)), tuple(sorted(g2)))
def projected_graph(C, weighted = False, as_hyper = False, multi = True):
'''
Compute the projected (clique) graph corresponding to a given hypergraph. Can be slow when many high-dimensional edges are present.
'''
if not as_hyper:
if multi:
G = nx.MultiGraph()
else:
G = nx.Graph()
G.add_nodes_from(C.nodes)
for f in C.C:
if weighted:
if len(f) >= 2:
G.add_edges_from(itertools.combinations(f, 2), weight = 1.0/(len(f) - 1))
else :
G.add_edges_from(itertools.combinations(f, 2))
return(G)
else:
G = [f for F in C.C for f in itertools.combinations(F, 2)]
return(hypergraph(G, n_nodes = len(C.nodes)))