-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhierarchy_metrics.py
364 lines (312 loc) · 13.1 KB
/
hierarchy_metrics.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
# -*- coding: utf-8 -*-
#
# Copyright (C) 2018 by
# Thomas Bonald <[email protected]>
# Bertrand Charpentier <[email protected]>
# All rights reserved
# BSD license
import networkx as nx
import numpy as np
## Aggregation functions
def init_aggregate_graph(graph):
"""Init the graph to be aggregated
Parameters
----------
graph : networkx.graph
An undirected graph with weighted edges (default weight = 1)
Nodes must be numbered from 0 to n - 1 (n nodes)
Returns
-------
aggregate_graph : networkx.graph
Same graph (copied) without self-loops and with node attributes (weight, size = 1)
total_weight : int
Total weight of nodes (twice the total weight of edges)
"""
aggregate_graph = graph.copy()
# remove self-loops, add node weights
node_weights = {u: 0. for u in aggregate_graph.nodes()}
total_weight = 0.
edges = list(aggregate_graph.edges())
for (u,v) in edges:
if u == v:
aggregate_graph.remove_edge(u,u)
else:
if 'weight' not in aggregate_graph[u][v]:
aggregate_graph[u][v]['weight'] = 1.
weight = aggregate_graph[u][v]['weight']
node_weights[u] += weight
node_weights[v] += weight
total_weight += 2 * weight
nx.set_node_attributes(aggregate_graph, node_weights, 'weight')
# add node sizes
nx.set_node_attributes(aggregate_graph, 1, 'size')
return aggregate_graph, total_weight
def merge_nodes(graph, u, v, new_node):
"""Merge two nodes of a graph and update the graph
Parameters
----------
graph : networkx.graph
An undirected graph with node attributes (weight, size)
Nodes must be numbered from 0 to n - 1 (n nodes)
Returns
-------
grap h: networkx.graph
Same graph with nodes u and v replaced by node new_node
u,v : int
Nodes to be merged
new_node : int
Node replacing u,v (with total weight and total size of u and v)
"""
neighbors_u = list(graph.neighbors(u))
neighbors_v = list(graph.neighbors(v))
graph.add_node(new_node)
# update edges
for node in neighbors_u:
graph.add_edge(new_node,node,weight = graph[u][node]['weight'])
for node in neighbors_v:
if graph.has_edge(new_node,node):
graph[new_node][node]['weight'] += graph[v][node]['weight']
else:
graph.add_edge(new_node,node,weight = graph[v][node]['weight'])
# updage node attributes
graph.node[new_node]['weight'] = graph.node[u]['weight'] + graph.node[v]['weight']
graph.node[new_node]['size'] = graph.node[u]['size'] + graph.node[v]['size']
graph.remove_node(u)
graph.remove_node(v)
return graph
## Clustering algorithms
def paris_hierarchy(graph):
"""Paris agglomerative algorithm
Parameters
----------
graph : networkx.graph
An undirected graph with weighted edges (default weight = 1)
Nodes must be numbered from 0 to n - 1 (n nodes)
Returns
-------
dendrogram : numpy.ndarray
2D array
Each row contains the two merged nodes, the height in the dendrogram, and the size of the corresponding cluster
Reference
---------
T. Bonald, B. Charpentier, A. Galland, A. Hollocou, Hierarchical graph clustering using node pair sampling, KDD Workshop, 2018
"""
# dendrogram as list of merges
dendrogram = []
aggregate_graph, total_weight = init_aggregate_graph(graph)
number_nodes = aggregate_graph.number_of_nodes()
new_node = number_nodes
while number_nodes > 0:
# nearest-neighbor chain
chain = [list(aggregate_graph.nodes())[0]]
while chain != []:
current_node = chain.pop()
# find nearest neighbor
distance_min = float("inf")
nearest_neighbor = -1
for node in aggregate_graph.neighbors(current_node):
if node != current_node:
distance = (aggregate_graph.node[current_node]['weight'] * aggregate_graph.node[node]['weight']
/ aggregate_graph[current_node][node]['weight'] / total_weight)
if distance < distance_min:
nearest_neighbor = node
distance_min = distance
elif distance == distance_min:
nearest_neighbor = min(nearest_neighbor,node)
distance = distance_min
if chain != []:
next_node = chain.pop()
if next_node == nearest_neighbor:
# merge nodes
size = aggregate_graph.node[current_node]['size'] + aggregate_graph.node[next_node]['size']
dendrogram.append([current_node,next_node,distance,size])
aggregate_graph = merge_nodes(aggregate_graph,current_node,next_node,new_node)
number_nodes -= 1
new_node += 1
else:
chain.append(next_node)
chain.append(current_node)
chain.append(nearest_neighbor)
elif nearest_neighbor >= 0:
chain.append(current_node)
chain.append(nearest_neighbor)
else:
number_nodes -= 1
return np.array(dendrogram, float)
def newman_hierarchy(graph):
"""Newman's agglomerative algorithm
Parameters
----------
graph : networkx.graph
An undirected graph with weighted edges (default weight = 1)
Nodes must be numbered from 0 to n - 1 (n nodes)
Returns
-------
dendrogram : numpy.ndarray
2D array
Each row contains the two merged nodes, the height in the dendrogram, and the size of the corresponding cluster
Reference
---------
M. E. Newman (2004). Fast algorithm for detecting community structure in networks. Physical review E.
"""
# dendrogram as list of merges
dendrogram = []
aggregate_graph, total_weight = init_aggregate_graph(graph)
# modularity increase
for (u,v) in aggregate_graph.edges():
aggregate_graph[u][v]['delta'] = 2 * (aggregate_graph[u][v]['weight']
- aggregate_graph.node[u]['weight']
* aggregate_graph.node[v]['weight']
/ total_weight) / total_weight
number_nodes = aggregate_graph.number_of_nodes()
new_node = number_nodes
while number_nodes > 1:
# find the best node pair for modularity increase
delta = nx.get_edge_attributes(aggregate_graph,'delta')
u,v = max(delta, key = delta.get)
# merge nodes
size = aggregate_graph.node[u]['size'] + aggregate_graph.node[v]['size']
dendrogram.append([u,v,size,size])
aggregate_graph = merge_nodes(aggregate_graph,u,v,new_node)
for u in aggregate_graph.neighbors(new_node):
aggregate_graph[u][new_node]['delta'] = 2 * (aggregate_graph[u][new_node]['weight']
- aggregate_graph.node[u]['weight']
* aggregate_graph.node[new_node]['weight']
/ total_weight) / total_weight
number_nodes -= 1
new_node += 1
return np.array(dendrogram, float)
def random_hierarchy(graph):
"""Random agglomerative algorithm
Parameters
----------
graph : networkx.graph
An undirected graph with weighted edges (default weight = 1)
Nodes must be numbered from 0 to n - 1 (n nodes)
Returns
-------
dendrogram : numpy.ndarray
2D array
Each row contains the two merged nodes, the height in the dendrogram, and the size of the corresponding cluster
"""
# dendrogram as list of merges
dendrogram = []
aggregate_graph, total_weight = init_aggregate_graph(graph)
number_nodes = aggregate_graph.number_of_nodes()
new_node = number_nodes
while number_nodes > 1:
# random edge
edges = list(aggregate_graph.edges())
u,v = edges[np.random.randint(len(edges))]
# merge nodes
size = aggregate_graph.node[u]['size'] + aggregate_graph.node[v]['size']
dendrogram.append([u,v,size,size])
aggregate_graph = merge_nodes(aggregate_graph,u,v,new_node)
number_nodes -= 1
new_node += 1
return np.array(dendrogram, float)
def hierarchical_clustering(graph, algorithm):
"""Hierarchical clustering
Parameters
----------
graph : networkx.graph
An undirected graph with weighted edges (default weight = 1)
Nodes must be numbered from 0 to n - 1 (n nodes)
The graph must be connected
algorithm : {"paris","newman","random"}
Clustering algorithm
Returns
-------
dendrogram : numpy.ndarray
2D array
Each row contains the two merged nodes, the height in the dendrogram, and the size of the corresponding cluster
"""
number_nodes = graph.number_of_nodes()
if set(graph.nodes()) != set(range(number_nodes)):
print("Error: Nodes must be numbered from 0 to n - 1.\nYou may consider the networkx function 'convert_node_labels_to_integers'.")
elif not nx.is_connected(graph):
print("Error: The graph is not connected.\nYou may consider the networkx function 'connected_component_subgraphs'.")
else:
if algorithm == "paris":
return paris_hierarchy(graph)
elif algorithm == "newman":
return newman_hierarchy(graph)
elif algorithm == "random":
return random_hierarchy(graph)
else:
print("Unknown algorithm")
## Metrics
def relative_entropy(graph, dendrogram, weighted = True):
"""Relative entropy of a hierarchy (quality metric)
Parameters
----------
graph : networkx.graph
An undirected graph with weighted edges (default weight = 1)
Nodes must be numbered from 0 to n - 1 (n nodes)
The graph must be connected
dendrogram : numpy.ndarray
2D array
Each row contains the two merged nodes, the height in the dendrogram, and the size of the corresponding cluster
weighted : bool, optional
The reference node sampling distribution is proportional to the weights if True and uniform if False
Returns
-------
quality : float
The relative entropy of the hierarchy (quality metric)
Reference
---------
T. Bonald, B. Charpentier (2018), Learning Graph Representations by Dendrograms, https://arxiv.org/abs/1807.05087
"""
aggregate_graph, total_weight = init_aggregate_graph(graph)
number_nodes = aggregate_graph.number_of_nodes()
if weighted:
pi = {u: aggregate_graph.node[u]['weight'] / total_weight for u in aggregate_graph.nodes()}
else:
pi = {u: 1. / number_nodes for u in aggregate_graph.nodes()}
quality = 0.
for t in range(number_nodes - 1):
u = int(dendrogram[t][0])
v = int(dendrogram[t][1])
if aggregate_graph.has_edge(u,v):
p = 2 * aggregate_graph[u][v]['weight'] / total_weight
quality += p * np.log(p / pi[u] / pi[v])
aggregate_graph = merge_nodes(aggregate_graph, u, v, number_nodes + t)
pi[number_nodes + t] = pi.pop(u) + pi.pop(v)
return quality
def dasgupta_cost(graph, dendrogram, weighted = True):
"""Dasgupa's cost of a hierarchy (cost function)
Parameters
----------
graph : networkx.graph
An undirected graph with weighted edges (default weight = 1)
Nodes must be numbered from 0 to n - 1 (n nodes)
The graph must be connected
dendrogram : numpy.ndarray
2D array
Each row contains the two merged nodes, the height in the dendrogram, and the size of the corresponding cluster
weighted : bool, optional
The reference node sampling distribution is proportional to the weights if True and uniform if False
Returns
-------
cost : float
Dasgupta's cost function (cost)
Reference
---------
S. Dasgupta (2016). A cost function for similarity-based hierarchical clustering. In Proceedings of ACM symposium on Theory of Computing.
"""
aggregate_graph, total_weight = init_aggregate_graph(graph)
number_nodes = aggregate_graph.number_of_nodes()
if weighted:
pi = {u: aggregate_graph.node[u]['weight'] / total_weight for u in aggregate_graph.nodes()}
else:
pi = {u: 1. / number_nodes for u in aggregate_graph.nodes()}
cost = 0.
for t in range(number_nodes - 1):
u = int(dendrogram[t][0])
v = int(dendrogram[t][1])
if aggregate_graph.has_edge(u,v):
p = 2 * aggregate_graph[u][v]['weight'] / total_weight
cost += p * (pi[u] + pi[v])
aggregate_graph = merge_nodes(aggregate_graph, u, v, number_nodes + t)
pi[number_nodes + t] = pi.pop(u) + pi.pop(v)
return cost