-
Notifications
You must be signed in to change notification settings - Fork 1
/
transforms.py
520 lines (421 loc) · 16.2 KB
/
transforms.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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
import pynet,netext,percolator
import random
import numpy as np
def mst(net,maximum=False):
"""Find a minimum/maximum spanning tree
"""
return mst_kruskal(net,True,maximum)
def mst_kruskal(net,randomize=True,maximum=False):
"""Find a minimum/maximum spanning tree using Kruskal's algorithm
If random is set to true and the mst is not unique, a random
mst is chosen.
>>> t=pynet.SymmNet()
>>> t[1,2]=1
>>> t[2,3]=2
>>> t[3,1]=3
>>> m=mst_kruskal(t)
>>> print m.edges
[[1, 2, 1], [2, 3, 2]]
"""
edges=list(net.edges)
if randomize:
random.shuffle(edges) #the sort has been stable since python version 2.3
edges.sort(lambda x,y:cmp(x[2],y[2]),reverse=maximum)
mst=pynet.SymmNet()
numberOfNodes=len(net)
ktree=percolator.Ktree() #just use dict
addedEdges=0
#First add the nodes
for node in net:
mst.addNode(node)
#Add the edges
for edge in edges:
if ktree.getParent(edge[0])!=ktree.getParent(edge[1]):
mst[edge[0],edge[1]]=edge[2]
ktree.setParent(edge[0],edge[1])
addedEdges+=1
if addedEdges==numberOfNodes-1:
#the mst is a tree
netext.copyNodeProperties(net,mst)
return mst
# else it is a forest
netext.copyNodeProperties(net,mst)
return mst
def snowball(net, seed, depth, includeLeafEdges=False):
"""Snowball sampling
Works for both directed and undirected networks. For directed
networks all edges all followed during the sampling (as opposed to
following only outbound edges).
Parameters
----------
net : pynet.SymmNet or pynet.Net object
The network to be sampled.
seed : int or a sequence of ints
The seed of the snowball, either a single node index or
several indices.
depth : int
The depth of the snowball. Depth 1 corresponds to first
neighbors of the seed only.
includeLeafEdges : bool (default: False)
If True, then the edges between the leaves (i.e. the nodes at
final depth) will also be included in the snowball network. By
default these edges are not included.
Return
------
snowball : pynet.SymmNet or pynet.Net object
The snowball sample, will be of the same type as `net`.
"""
if isinstance(seed, int):
seed = [seed]
toVisit=set(seed)
# Create a network for the sample with the same type as `net`.
newNet=type(net)()
visited=set()
for d in range(1,depth+1):
#print "Depth: ",d," visited ", len(visited)," to visit ", len(toVisit)
visited=visited|toVisit
newToVisit=set()
if len(toVisit) == 0:
break
for nodeIndex in toVisit:
node = net[nodeIndex]
# Go through outbound edges (this equals all neighbors in
# an undirected network.
for outIndex in node.iterOut():
newNet[nodeIndex][outIndex] = net[nodeIndex][outIndex]
if outIndex not in visited:
newToVisit.add(outIndex)
# If we are dealing with a directed network, then we must
# also go through the inbound edges.
if isinstance(net, pynet.Net):
for inIndex in node.iterIn():
newNet[inIndex][nodeIndex] = net[inIndex][nodeIndex]
if inIndex not in visited:
newToVisit.add(inIndex)
# If this is the last depth and `includeLeafEdges` is
# True, we add the edges between the most recently added
# nodes, that is, those currently in the set `newToVisit`.
if d == depth and includeLeafEdges:
for nodeIndex in newToVisit:
node = net[nodeIndex]
for outIndex in node.iterOut():
if outIndex in newToVisit:
newNet[nodeIndex][outIndex] = net[nodeIndex][outIndex]
if isinstance(net, pynet.Net):
for inIndex in node.iterIn():
if inIndex in newToVisit:
newNet[inIndex][nodeIndex] = net[inIndex][nodeIndex]
# The nodes to be visited on the next round are the leaves
# found in the current round.
toVisit=newToVisit
netext.copyNodeProperties(net,newNet)
return newNet
def collapseIndices(net, returnIndexMap=False):
"""Changes the indices of net to run from 0 to len(net)-1.
"""
newNet = type(net)()
indexmap = {}
index = 0
for i in net:
newNet.addNode(index)
indexmap[i] = index;
index += 1
for edge in net.edges:
i,j,w=edge
newNet[indexmap[i]][indexmap[j]] = w
netext.copyNodeProperties(net,newNet)
if returnIndexMap:
return newNet, indexmap
else:
return newNet
def threshold_by_value(net,threshold,accept="<",keepIsolatedNodes=False):
'''Generates a new network by thresholding the input network.
If using option keepIsolatedNodes=True, all nodes in the
original network will be included in the thresholded network;
otherwise only those nodes which have links will remain (this
is the default).
Inputs: net = network, threshold = threshold value,
accept = "foobar": accept weights foobar threshold (e.g accept = "<": accept weights < threshold)
Returns a network.'''
newnet=pynet.SymmNet()
edges=list(net.edges)
if accept == "<":
for edge in edges:
if (edge[2] < threshold):
newnet[edge[0],edge[1]]=edge[2]
elif accept == ">":
for edge in edges:
if (edge[2] > threshold):
newnet[edge[0],edge[1]]=edge[2]
elif accept == ">=":
for edge in edges:
if (edge[2] >= threshold):
newnet[edge[0],edge[1]]=edge[2]
elif accept == "<=":
for edge in edges:
if (edge[2] <= threshold):
newnet[edge[0],edge[1]]=edge[2]
else:
raise Exception("Parameter 'accept' must be either '<', '>', '<=' or '>='.")
# Add isolated nodes to the network.
if keepIsolatedNodes==True:
for node in net:
if not newnet.__contains__(node):
newnet.addNode(node)
netext.copyNodeProperties(net,newnet)
return newnet
def dist_to_weights(net,epsilon=0.001):
'''Transforms a distance matrix / network to a weight
matrix / network using the formula W = 1 - D / max(D)+epsilon.
Returns a matrix/network'''
N=len(net._nodes)
if (isinstance(net,pynet.SymmFullNet)):
newmat=pynet.SymmFullNet(N)
else:
newmat=pynet.SymmNet()
edges=list(net.edges)
maxd=0.0
for edge in edges:
if edge[2]>maxd:
maxd=edge[2]
# epsilon trick; lowest weight will be almost but
# not entirely zero
for edge in edges:
newmat[edge[0]][edge[1]]=1-edge[2]/maxd+epsilon
netext.copyNodeProperties(net,newmat)
return newmat
def filterNet(net,keep_these_nodes):
return getSubnet(net,keep_these_nodes)
def getSubnet(net,nodes):
"""Get induced subgraph.
Parameters
----------
net: pynet.Net, pynet.SymmNet or pynet.SymmFullNet
The original network.
nodes : sequence
The nodes that span the induces subgraph.
Return
------
subnet : type(net)
The induced subgraph that contains only nodes given in
`nodes` and the edges between those nodes that are
present in `net`. Node properties etc are left untouched.
"""
# Handle both directed and undirected networks.
newnet = type(net)() # Initialize to same type as `net`.
degsum=0
for node in nodes:
degsum += net[node].deg()
newnet.addNode(node)
if degsum >= len(nodes)*(len(nodes)-1)/2:
othernodes=set(nodes)
for node in nodes:
if net.isSymmetric():
othernodes.remove(node)
for othernode in othernodes:
if net[node,othernode]!=0:
newnet[node,othernode]=net[node,othernode]
else:
for node in nodes:
for neigh in net[node]:
if neigh in nodes:
newnet[node,neigh]=net[node,neigh]
netext.copyNodeProperties(net, newnet)
return newnet
def collapseBipartiteNet(net,nodesToRemove):
"""
Returns an unipartite projection of a bipartite network.
"""
newNet=pynet.SymmNet()
for node in nodesToRemove:
degree=float(net[node].deg())
for node1 in net[node]:
for node2 in net[node]:
if node1.__hash__()>node2.__hash__():
newNet[node1,node2]=newNet[node1,node2]+1.0/degree
netext.copyNodeProperties(net,newNet)
return newNet
def local_threshold_by_value(net,threshold):
'''Generates a new network by thresholding the input network.
Inputs: net = network, threshold = threshold value,
mode = 0 (accept weights < threshold), 1 (accept weights > threshold)
Returns a network. Note! threshold is really alpha which is defined in
"Extracting the multiscale backbone of complex weighted networks"
http://www.pnas.org/content/106/16/6483.full.pdf'''
newnet=pynet.SymmNet()
for node in net:
s=net[node].strength()
k=net[node].deg()
for neigh in net[node]:
w=net[node,neigh]
if (1-w/s)**(k-1)<threshold:
newnet[node,neigh]=w
netext.copyNodeProperties(net,newnet)
return newnet
def getLineGraph(net, useWeights=False, output=None, format='edg'):
"""Return a line graph constructed from `net`.
The nodes in the line graph correspond to edges in the original
graph, and there is an edge between two nodes if they have a
common incident node in the original graph.
If weights are not used (`useWeights = False`), the resulting
network will be undirected and the weight of each new edge will be
1/(k_i-1), where k_i is the degree of the common node in `net`.
If weights are used (`useWeights = True`), the resulting network
will be directed and the weight of edge (e_ij, e_jk) will be
w_jk/sum_{x != i} w_jx, where the indices i, j and k refer to
nodes in `net`.
Parameters
----------
net : pynet.SymmNet object
The original graph that is used for constructing the line
graph.
useWeights : boolean
If True, the edge weights will be used when constructing the
line graph.
output : file object
If given, the edges will be written to output in edg-format
instead of returning a pynet.Net() or pynet.SymmNet() object.
format : str, 'edg' or 'net'
If `output` is specified, `format` specifies how the output is
written. 'edg' is the standard edge format (FROM TO WEIGHT)
and 'net' gives the Pajek format.
Return
------
IF `output` is None:
linegraph : pynet.SymmNet or pynet.Net object
The weighted line graph.
id_array : numpy.array with shape (len(net.edges), 2)
Array for converting the nodes in the line graph back into the
edges of the original graph. id_array[EDGE_ID] contains the
two end nodes of given edge, where EDGE_ID is the same as used
in `linegraph`.
"""
if output is None:
if useWeights:
linegraph = pynet.Net()
else:
linegraph = pynet.SymmNet()
edge_map = dict() # edge_map[sorted([n_i, n_j])] = new_node_ID
if output is not None and format == 'net':
# Print Pajek file header.
N_edges = len(list(net.edges))
output.write("*Vertices %d\n" % N_edges)
for i in range(N_edges):
output.write('%d "%d"\n' % (i, i))
N_edge_links = 0
for n in net:
degree = len(list(net[n]))
N_edge_links += (degree*(degree-1))/2
if useWeights:
output.write("*Arcs %d\n" % (2*N_edge_links,))
else:
output.write("*Edges %d\n" % N_edge_links)
# Go through all nodes (n_c = center node), and for each node, go
# through all pairs of neighbours (n_i and n_j). The edges
# e_i=(n_c,n_i) and e_j=(n_c,n_j) are nodes in the line graph, so
# we add a link between them.
for n_c in net:
strength = net[n_c].strength()
nb = list(net[n_c]) # List of neighbours
for i, n_i in enumerate(nb):
e_i = edge_map.setdefault(tuple(sorted([n_c,n_i])), len(edge_map))
other_nb = (nb[:i]+nb[i+1:] if useWeights else nb[i+1:])
for n_j in other_nb:
e_j = edge_map.setdefault(tuple(sorted([n_c,n_j])), len(edge_map))
if useWeights:
w = net[n_c][n_j]/(strength - net[n_c][n_i])
else:
w = 1.0/(len(nb)-1)
if output is None:
linegraph[e_i][e_j] = w
else:
output.write(" ".join(map(str, [e_i, e_j, w])) + "\n")
# Construct id_array from edge_map
id_array = np.zeros((len(edge_map), 2), int)
for node_pair, edgeID in edge_map.iteritems():
id_array[edgeID] = list(node_pair)
if output is None:
return linegraph, id_array
else:
return id_array
def netConfiguration(net, keepsOrigNet=False, seed=None):
"""Generate configuration network
This function generates a configuration network from any arbitrary
net. It retains the degree of each node but randomize the edges
between them.
Parameters
----------
net : pynet.SymmNet object
The network to be used as the basis for the configuration
model.
keepsOrigNet : bool (default: False)
If False, the input network, `net`, will be overwritten by the
configuration network.
seed : int (default: None)
A seed for the random number generator. If None, the RNG is
not be re-initialized but the current state is used.
Return
------
configuration_net : pynet.SymmNet object
The shuffled network. Note that if `keepsOrigNet` is False,
the returned value will be identical to `net`.
"""
if seed is not None:
random.seed(int(seed))
newNet = pynet.SymmNet()
if keepsOrigNet:
testNet = pynet.SymmNet()
for edge in net.edges:
testNet[edge[0],edge[1]] = edge[2]
else:
testNet=net
edgeList = list(net.edges)
for i in range(len(edgeList)):
j=i
while j==i:
j=random.randint(0,len(edgeList)-1)
if ((edgeList[i][1]==edgeList[j][0])
or (edgeList[j][1]==edgeList[i][0])):
continue
if ((edgeList[i][1]==edgeList[j][0])
and (edgeList[j][1]==edgeList[i][0])):
continue
if ((edgeList[i][0]==edgeList[j][0])
or (edgeList[i][1]==edgeList[j][1])):
continue
if ((newNet[edgeList[i][0],edgeList[j][1]]>0.0)
or (newNet[edgeList[j][0],edgeList[i][1]]>0.0)):
continue
if ((testNet[edgeList[i][0],edgeList[j][1]]>0.0)
or (testNet[edgeList[j][0],edgeList[i][1]]>0.0)):
continue
edgeList[i][1]+=edgeList[j][1]
edgeList[j][1]=edgeList[i][1]-edgeList[j][1]
edgeList[i][1]=edgeList[i][1]-edgeList[j][1]
newNet[edgeList[i][0],edgeList[j][1]]=0.0
newNet[edgeList[j][0],edgeList[i][1]]=0.0
testNet[edgeList[i][0],edgeList[j][1]]=0.0
testNet[edgeList[j][0],edgeList[i][1]]=0.0
newNet[edgeList[i][0],edgeList[i][1]]=1.0
newNet[edgeList[j][0],edgeList[j][1]]=1.0
testNet[edgeList[i][0],edgeList[i][1]]=1.0
testNet[edgeList[j][0],edgeList[j][1]]=1.0
return newNet
def copyNet(net):
""" Copy a network. Also the node properties are copied.
Parameters
----------
net : any pynet object
The network to be copied.
Return
------
newNet : net.__class__ object
A copy of the network.
"""
newNet=net.__copy__()
netext.copyNodeProperties(net,newNet)
return newNet
if __name__ == '__main__':
"""Run unit tests if called."""
from tests.test_transforms import *
unittest.main()