forked from aestrivex/bctpy
/
centrality.py
838 lines (695 loc) · 25.8 KB
/
centrality.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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
"""
Metrics which identify the most important nodes in graphs.!!
"""
from __future__ import division, print_function
import numpy as np
from ..core import kcore_bd, kcore_bu
from ..distance import reachdist
from ..utils import invert
from ..due import due, Doi, BibTeX
@due.dcite(Doi('10.2307/3033543'),
description='First formal description of betweenness centrality.')
def betweenness_bin(G):
"""
Node betweenness centrality is the fraction of all shortest paths in
the network that contain a given node. Nodes with high values of
betweenness centrality participate in a large number of shortest paths.
Parameters
----------
A : NxN :obj:`numpy.ndarray`
binary directed/undirected connection matrix
BC : Nx1 :obj:`numpy.ndarray`
node betweenness centrality vector
Notes
-----
Betweenness centrality may be normalised to the range [0,1] as
BC/[(N-1)(N-2)], where N is the number of nodes in the network.
"""
G = np.array(G, dtype=float) # force G to have float type so it can be
# compared to float np.inf
n = len(G) # number of nodes
eye = np.eye(n) # identity matrix
d = 1 # path length
NPd = G.copy() # number of paths of length |d|
NSPd = G.copy() # number of shortest paths of length |d|
NSP = G.copy() # number of shortest paths of any length
L = G.copy() # length of shortest paths
NSP[np.where(eye)] = 1
L[np.where(eye)] = 1
# calculate NSP and L
while np.any(NSPd):
d += 1
NPd = np.dot(NPd, G)
NSPd = NPd * (L == 0)
NSP += NSPd
L = L + d * (NSPd != 0)
L[L == 0] = np.inf # L for disconnected vertices is inf
L[np.where(eye)] = 0
NSP[NSP == 0] = 1 # NSP for disconnected vertices is 1
DP = np.zeros((n, n)) # vertex on vertex dependency
diam = d - 1
# calculate DP
for d in range(diam, 1, -1):
DPd1 = np.dot(((L == d) * (1 + DP) / NSP), G.T) * \
((L == (d - 1)) * NSP)
DP += DPd1
return np.sum(DP, axis=0)
def betweenness_wei(G):
"""
Node betweenness centrality is the fraction of all shortest paths in
the network that contain a given node. Nodes with high values of
betweenness centrality participate in a large number of shortest paths.
Parameters
----------
L : NxN :obj:`numpy.ndarray`
directed/undirected weighted connection matrix
Returns
-------
BC : Nx1 :obj:`numpy.ndarray`
node betweenness centrality vector
Notes
-----
The input matrix must be a connection-length matrix, typically
obtained via a mapping from weight to length. For instance, in a
weighted correlation network higher correlations are more naturally
interpreted as shorter distances and the input matrix should
consequently be some inverse of the connectivity matrix.
Betweenness centrality may be normalised to the range [0,1] as
BC/[(N-1)(N-2)], where N is the number of nodes in the network.
"""
n = len(G)
BC = np.zeros((n,)) # vertex betweenness
for u in range(n):
D = np.tile(np.inf, (n,))
D[u] = 0 # distance from u
NP = np.zeros((n,))
NP[u] = 1 # number of paths from u
S = np.ones((n,), dtype=bool) # distance permanence
P = np.zeros((n, n)) # predecessors
Q = np.zeros((n,), dtype=int) # indices
q = n - 1 # order of non-increasing distance
G1 = G.copy()
V = [u]
while True:
S[V] = 0 # distance u->V is now permanent
G1[:, V] = 0 # no in-edges as already shortest
for v in V:
Q[q] = v
q -= 1
W, = np.where(G1[v, :]) # neighbors of v
for w in W:
Duw = D[v] + G1[v, w] # path length to be tested
if Duw < D[w]: # if new u->w shorter than old
D[w] = Duw
NP[w] = NP[v] # NP(u->w) = NP of new path
P[w, :] = 0
P[w, v] = 1 # v is the only predecessor
elif Duw == D[w]: # if new u->w equal to old
NP[w] += NP[v] # NP(u->w) sum of old and new
P[w, v] = 1 # v is also predecessor
if D[S].size == 0:
break # all nodes were reached
if np.isinf(np.min(D[S])): # some nodes cannot be reached
Q[:q + 1], = np.where(np.isinf(D)) # these are first in line
break
V, = np.where(D == np.min(D[S]))
DP = np.zeros((n,))
for w in Q[:n - 1]:
BC[w] += DP[w]
for v in np.where(P[w, :])[0]:
DP[v] += (1 + DP[w]) * NP[v] / NP[w]
return BC
@due.dcite(BibTeX("""@article{shannon1948mathematical,
title={A mathematical theory of communication},
author={Shannon, Claude Elwood},
journal={The Bell System Technical Journal},
volume={27},
pages={379--423},
year={1948}
}"""), description='Citation for the Shannon entropy.')
def diversity_coef_sign(W, ci):
"""
The Shannon entropy-based diversity coefficient measures the diversity
of intermodular connections of individual nodes and ranges from 0 to 1.
Parameters
----------
W : NxN :obj:`numpy.ndarray`
undirected connection matrix with positive and negative weights
ci : Nx1 :obj:`numpy.ndarray`
community affiliation vector
Returns
-------
Hpos : Nx1 :obj:`numpy.ndarray`
diversity coefficient based on positive connections
Hneg : Nx1 :obj:`numpy.ndarray`
diversity coefficient based on negative connections
"""
n = len(W) # number of nodes
_, ci = np.unique(ci, return_inverse=True)
ci += 1
m = np.max(ci) # number of modules
def entropy(w_):
S = np.sum(w_, axis=1) # strength
Snm = np.zeros((n, m)) # node-to-module degree
for i in range(m):
Snm[:, i] = np.sum(w_[:, ci == i + 1], axis=1)
pnm = Snm / (np.tile(S, (m, 1)).T)
pnm[np.isnan(pnm)] = 0
pnm[np.logical_not(pnm)] = 1
return -1 * np.sum(pnm * np.log(pnm), axis=1) / np.log(m)
# explicitly ignore compiler warning for division by zero
with np.errstate(invalid='ignore'):
Hpos = entropy(W * (W > 0))
Hneg = entropy(-W * (W < 0))
return Hpos, Hneg
def edge_betweenness_bin(G):
"""
Edge betweenness centrality is the fraction of all shortest paths in
the network that contain a given edge. Edges with high values of
betweenness centrality participate in a large number of shortest paths.
Parameters
----------
A : NxN :obj:`numpy.ndarray`
binary directed/undirected connection matrix
Returns
-------
EBC : NxN :obj:`numpy.ndarray`
edge betweenness centrality matrix
BC : Nx1 :obj:`numpy.ndarray`
node betweenness centrality vector
Notes
-----
Betweenness centrality may be normalised to the range [0,1] as
BC/[(N-1)(N-2)], where N is the number of nodes in the network.
"""
n = len(G)
BC = np.zeros((n,)) # vertex betweenness
EBC = np.zeros((n, n)) # edge betweenness
for u in range(n):
D = np.zeros((n,))
D[u] = 1 # distance from u
NP = np.zeros((n,))
NP[u] = 1 # number of paths from u
P = np.zeros((n, n)) # predecessors
Q = np.zeros((n,), dtype=int) # indices
q = n - 1 # order of non-increasing distance
Gu = G.copy()
V = np.array([u])
while V.size:
Gu[:, V] = 0 # remove remaining in-edges
for v in V:
Q[q] = v
q -= 1
W, = np.where(Gu[v, :]) # neighbors of V
for w in W:
if D[w]:
NP[w] += NP[v] # NP(u->w) sum of old and new
P[w, v] = 1 # v is a predecessor
else:
D[w] = 1
NP[w] = NP[v] # NP(u->v) = NP of new path
P[w, v] = 1 # v is a predecessor
V, = np.where(np.any(Gu[V, :], axis=0))
if np.any(np.logical_not(D)): # if some vertices unreachable
Q[:q], = np.where(np.logical_not(D)) # ...these are first in line
DP = np.zeros((n,)) # dependency
for w in Q[:n - 1]:
BC[w] += DP[w]
for v in np.where(P[w, :])[0]:
DPvw = (1 + DP[w]) * NP[v] / NP[w]
DP[v] += DPvw
EBC[v, w] += DPvw
return EBC, BC
def edge_betweenness_wei(G):
"""
Edge betweenness centrality is the fraction of all shortest paths in
the network that contain a given edge. Edges with high values of
betweenness centrality participate in a large number of shortest paths.
Parameters
----------
L : NxN :obj:`numpy.ndarray`
directed/undirected weighted connection matrix
Returns
-------
EBC : NxN :obj:`numpy.ndarray`
edge betweenness centrality matrix
BC : Nx1 :obj:`numpy.ndarray`
nodal betweenness centrality vector
Notes
-----
The input matrix must be a connection-length matrix, typically
obtained via a mapping from weight to length. For instance, in a
weighted correlation network higher correlations are more naturally
interpreted as shorter distances and the input matrix should
consequently be some inverse of the connectivity matrix.
Betweenness centrality may be normalised to the range [0,1] as
BC/[(N-1)(N-2)], where N is the number of nodes in the network.
"""
n = len(G)
BC = np.zeros((n,)) # vertex betweenness
EBC = np.zeros((n, n)) # edge betweenness
for u in range(n):
D = np.tile(np.inf, n)
D[u] = 0 # distance from u
NP = np.zeros((n,))
NP[u] = 1 # number of paths from u
S = np.ones((n,), dtype=bool) # distance permanence
P = np.zeros((n, n)) # predecessors
Q = np.zeros((n,), dtype=int) # indices
q = n - 1 # order of non-increasing distance
G1 = G.copy()
V = [u]
while True:
S[V] = 0 # distance u->V is now permanent
G1[:, V] = 0 # no in-edges as already shortest
for v in V:
Q[q] = v
q -= 1
W, = np.where(G1[v, :]) # neighbors of v
for w in W:
Duw = D[v] + G1[v, w] # path length to be tested
if Duw < D[w]: # if new u->w shorter than old
D[w] = Duw
NP[w] = NP[v] # NP(u->w) = NP of new path
P[w, :] = 0
P[w, v] = 1 # v is the only predecessor
elif Duw == D[w]: # if new u->w equal to old
NP[w] += NP[v] # NP(u->w) sum of old and new
P[w, v] = 1 # v is also a predecessor
if D[S].size == 0:
break # all nodes reached, or
if np.isinf(np.min(D[S])): # some cannot be reached
Q[:q], = np.where(np.isinf(D)) # these are first in line
break
V, = np.where(D == np.min(D[S]))
DP = np.zeros((n,)) # dependency
for w in Q[:n - 1]:
BC[w] += DP[w]
for v in np.where(P[w, :])[0]:
DPvw = (1 + DP[w]) * NP[v] / NP[w]
DP[v] += DPvw
EBC[v, w] += DPvw
return EBC, BC
@due.dcite(Doi('10.1016/j.socnet.2007.04.002'),
description='Discusses eigenvector centrality')
def eigenvector_centrality_und(CIJ):
"""
Eigenector centrality is a self-referential measure of centrality:
nodes have high eigenvector centrality if they connect to other nodes
that have high eigenvector centrality. The eigenvector centrality of
node i is equivalent to the ith element in the eigenvector
corresponding to the largest eigenvalue of the adjacency matrix.
Parameters
----------
CIJ : NxN :obj:`numpy.ndarray`
binary/weighted undirected adjacency matrix
v : Nx1 :obj:`numpy.ndarray`
eigenvector associated with the largest eigenvalue of the matrix
"""
from scipy import linalg
# n = len(CIJ)
vals, vecs = linalg.eig(CIJ)
i = np.argmax(vals)
return np.abs(vecs[:, i])
def erange(CIJ):
"""
Shortcuts are central edges which significantly reduce the
characteristic path length in the network.
Parameters
----------
CIJ : NxN :obj:`numpy.ndarray`
binary directed connection matrix
Returns
-------
Erange : NxN :obj:`numpy.ndarray`
range for each edge, i.e. the length of the shortest path from i to j
for edge c(i,j) after the edge has been removed from the graph
eta : float
average range for the entire graph
Eshort : NxN :obj:`numpy.ndarray`
entries are ones for shortcut edges
fs : float
fractions of shortcuts in the graph
Notes
-----
Follows the treatment of 'shortcuts' by Duncan Watts
"""
N = len(CIJ)
K = np.size(np.where(CIJ)[1])
Erange = np.zeros((N, N))
i, j = np.where(CIJ)
for c in range(len(i)):
CIJcut = CIJ.copy()
CIJcut[i[c], j[c]] = 0
R, D = reachdist(CIJcut)
Erange[i[c], j[c]] = D[i[c], j[c]]
# average range (ignore Inf)
eta = (np.sum(Erange[np.logical_and(Erange > 0, Erange < np.inf)]) /
len(Erange[np.logical_and(Erange > 0, Erange < np.inf)]))
# Original entries of D are ones, thus entries of Erange
# must be two or greater.
# If Erange(i,j) > 2, then the edge is a shortcut.
# 'fshort' is the fraction of shortcuts over the entire graph.
Eshort = Erange > 2
fs = len(np.where(Eshort)) / K
return Erange, eta, Eshort, fs
@due.dcite(Doi('10.1073/pnas.0701519104'),
description='Introduces the flow coefficient.')
def flow_coef_bd(CIJ):
"""
Computes the flow coefficient for each node and averaged over the
network, as described in Honey et al. (2007) PNAS. The flow coefficient
is similar to betweenness centrality, but works on a local
neighborhood. It is mathematically related to the clustering
coefficient (cc) at each node as, fc+cc <= 1.
Parameters
----------
CIJ : NxN :obj:`numpy.ndarray`
binary directed connection matrix
Returns
-------
fc : Nx1 :obj:`numpy.ndarray`
flow coefficient for each node
FC : float
average flow coefficient over the network
total_flo : int
number of paths that "flow" across the central node
"""
N = len(CIJ)
fc = np.zeros((N,))
total_flo = np.zeros((N,))
max_flo = np.zeros((N,))
# loop over nodes
for v in range(N):
# find neighbors - note: both incoming and outgoing connections
nb, = np.where(CIJ[v, :] + CIJ[:, v].T)
fc[v] = 0
if np.where(nb)[0].size:
CIJflo = -CIJ[np.ix_(nb, nb)]
for i in range(len(nb)):
for j in range(len(nb)):
if CIJ[nb[i], v] and CIJ[v, nb[j]]:
CIJflo[i, j] += 1
total_flo[v] = np.sum(
(CIJflo == 1) * np.logical_not(np.eye(len(nb))))
max_flo[v] = len(nb) * len(nb) - len(nb)
fc[v] = total_flo[v] / max_flo[v]
fc[np.isnan(fc)] = 0
FC = np.mean(fc)
return fc, FC, total_flo
@due.dcite(Doi('10.1140/epjb/e2014-40800-7'),
description='Introduces the gateway coefficient.')
def gateway_coef_sign(W, ci, centrality_type='degree'):
"""
The gateway coefficient is a variant of participation coefficient.
It is weighted by how critical the connections are to intermodular
connectivity (e.g. if a node is the only connection between its
module and another module, it will have a higher gateway coefficient,
unlike participation coefficient).
Parameters
----------
W : NxN :obj:`numpy.ndarray`
undirected signed connection matrix
ci : Nx1 :obj:`numpy.ndarray`
community affiliation vector
centrality_type : enum
'degree' - uses the weighted degree (i.e, node strength)
'betweenness' - uses the betweenness centrality
Returns
-------
Gpos : Nx1 :obj:`numpy.ndarray`
gateway coefficient for positive weights
Gneg : Nx1 :obj:`numpy.ndarray`
gateway coefficient for negative weights
References
----------
.. [1] Vargas ER, Wahl LM, Eur Phys J B (2014) 87:1-10
"""
_, ci = np.unique(ci, return_inverse=True)
ci += 1
n = len(W)
np.fill_diagonal(W, 0)
def gcoef(W):
# strength
s = np.sum(W, axis=1)
# neighbor community affiliation
Gc = np.inner((W != 0), np.diag(ci))
# community specific neighbors
Sc2 = np.zeros((n,))
# extra modular weighting
ksm = np.zeros((n,))
# intra modular wieghting
centm = np.zeros((n,))
if centrality_type == 'degree':
cent = s.copy()
elif centrality_type == 'betweenness':
cent = betweenness_wei(invert(W))
nr_modules = int(np.max(ci))
for i in range(1, nr_modules+1):
ks = np.sum(W * (Gc == i), axis=1)
print(np.sum(ks))
Sc2 += ks ** 2
for j in range(1, nr_modules+1):
# calculate extramodular weights
ksm[ci == j] += ks[ci == j] / np.sum(ks[ci == j])
# calculate intramodular weights
centm[ci == i] = np.sum(cent[ci == i])
# print(Gc)
# print(centm)
# print(ksm)
# print(ks)
centm = centm / max(centm)
# calculate total weights
gs = (1 - ksm * centm) ** 2
Gw = 1 - Sc2 * gs / s ** 2
Gw[np.where(np.isnan(Gw))] = 0
Gw[np.where(np.logical_not(Gw))] = 0
return Gw
G_pos = gcoef(W * (W > 0))
G_neg = gcoef(-W * (W < 0))
return G_pos, G_neg
def kcoreness_centrality_bd(CIJ):
"""
The k-core is the largest subgraph comprising nodes of degree at least
k. The coreness of a node is k if the node belongs to the k-core but
not to the (k+1)-core. This function computes k-coreness of all nodes
for a given binary directed connection matrix.
Parameters
----------
CIJ : NxN :obj:`numpy.ndarray`
binary directed connection matrix
Returns
-------
coreness : (N,) :obj:`numpy.ndarray`
node coreness
kn : (N,) :obj:`numpy.ndarray`
size of k-core
"""
N = len(CIJ)
coreness = np.zeros((N,))
kn = np.zeros((N,))
for k in range(N):
CIJkcore, kn[k] = kcore_bd(CIJ, k)
ss = np.sum(CIJkcore, axis=0) > 0
coreness[ss] = k
return coreness, kn
def kcoreness_centrality_bu(CIJ):
"""
The k-core is the largest subgraph comprising nodes of degree at least
k. The coreness of a node is k if the node belongs to the k-core but
not to the (k+1)-core. This function computes the coreness of all nodes
for a given binary undirected connection matrix.
Parameters
----------
CIJ : NxN :obj:`numpy.ndarray`
binary undirected connection matrix
Returns
-------
coreness : (N,) :obj:`numpy.ndarray`
node coreness
kn : (N,) :obj:`numpy.ndarray`
size of k-core
"""
N = len(CIJ)
# determine if the network is undirected -- if not, compute coreness
# on the corresponding undirected network
CIJund = CIJ + CIJ.T
if np.any(CIJund > 1):
CIJ = np.array(CIJund > 0, dtype=float)
coreness = np.zeros((N,))
kn = np.zeros((N,))
for k in range(N):
CIJkcore, kn[k] = kcore_bu(CIJ, k)
ss = np.sum(CIJkcore, axis=0) > 0
coreness[ss] = k
return coreness, kn
def module_degree_zscore(W, ci, flag=0):
"""
The within-module degree z-score is a within-module version of degree
centrality.
Parameters
----------
W : NxN :obj:`numpy.ndarray`
binary/weighted directed/undirected connection matrix
ci : Nx1 np.array_like
community affiliation vector
flag : int
Graph type. 0: undirected graph (default)
1: directed graph in degree
2: directed graph out degree
3: directed graph in and out degree
Returns
-------
Z : Nx1 :obj:`numpy.ndarray`
within-module degree Z-score
"""
_, ci = np.unique(ci, return_inverse=True)
ci += 1
if flag == 2:
W = W.copy()
W = W.T
elif flag == 3:
W = W.copy()
W = W + W.T
n = len(W)
Z = np.zeros((n,)) # number of vertices
for i in range(1, int(np.max(ci) + 1)):
Koi = np.sum(W[np.ix_(ci == i, ci == i)], axis=1)
Z[np.where(ci == i)] = (Koi - np.mean(Koi)) / np.std(Koi)
Z[np.where(np.isnan(Z))] = 0
return Z
@due.dcite(Doi('10.1016/S0169-7552(98)00110-X'),
description='Introduces PageRank centrality.')
def pagerank_centrality(A, d, falff=None):
"""
The PageRank centrality is a variant of eigenvector centrality. This
function computes the PageRank centrality of each vertex in a graph.
Formally, PageRank is defined as the stationary distribution achieved
by instantiating a Markov chain on a graph. The PageRank centrality of
a given vertex, then, is proportional to the number of steps (or amount
of time) spent at that vertex as a result of such a process.
The PageRank index gets modified by the addition of a damping factor,
d. In terms of a Markov chain, the damping factor specifies the
fraction of the time that a random walker will transition to one of its
current state's neighbors. The remaining fraction of the time the
walker is restarted at a random vertex. A common value for the damping
factor is d = 0.85.
Parameters
----------
A : NxN :obj:`numpy.ndarray`
adjacency matrix
d : float
damping factor (see description)
falff : Nx1 :obj:`numpy.ndarray` or None
Initial page rank probability, non-negative values. Default value is
None. If not specified, a naive bayesian prior is used.
Returns
-------
r : Nx1 :obj:`numpy.ndarray`
vectors of page rankings
Notes
-----
The algorithm will work well for smaller matrices (number of
nodes around 1000 or less)
"""
from scipy import linalg
N = len(A)
if falff is None:
norm_falff = np.ones((N,)) / N
else:
norm_falff = falff / np.sum(falff)
deg = np.sum(A, axis=0)
deg[deg == 0] = 1
D1 = np.diag(1 / deg)
B = np.eye(N) - d * np.dot(A, D1)
b = (1 - d) * norm_falff
r = linalg.solve(B, b)
r /= np.sum(r)
return r
def participation_coef(W, ci, degree='undirected'):
"""
Participation coefficient is a measure of diversity of intermodular
connections of individual nodes.
Parameters
----------
W : NxN :obj:`numpy.ndarray`
binary/weighted directed/undirected connection matrix
ci : Nx1 :obj:`numpy.ndarray`
community affiliation vector
degree : {'undirected', 'in', 'out'}, optional
Flag to describe nature of graph. 'undirected': For undirected graphs,
'in': Uses the in-degree, 'out': Uses the out-degree
Returns
-------
P : Nx1 :obj:`numpy.ndarray`
participation coefficient
"""
if degree == 'in':
W = W.T
_, ci = np.unique(ci, return_inverse=True)
ci += 1
n = len(W) # number of vertices
Ko = np.sum(W, axis=1) # (out) degree
Gc = np.dot((W != 0), np.diag(ci)) # neighbor community affiliation
Kc2 = np.zeros((n,)) # community-specific neighbors
for i in range(1, int(np.max(ci)) + 1):
Kc2 += np.square(np.sum(W * (Gc == i), axis=1))
P = np.ones((n,)) - Kc2 / np.square(Ko)
# P=0 if for nodes with no (out) neighbors
P[np.where(np.logical_not(Ko))] = 0
return P
def participation_coef_sign(W, ci):
"""
Participation coefficient is a measure of diversity of intermodular
connections of individual nodes.
Parameters
----------
W : NxN :obj:`numpy.ndarray`
undirected connection matrix with positive and negative weights
ci : Nx1 :obj:`numpy.ndarray`
community affiliation vector
Returns
-------
Ppos : Nx1 :obj:`numpy.ndarray`
participation coefficient from positive weights
Pneg : Nx1 :obj:`numpy.ndarray`
participation coefficient from negative weights
"""
_, ci = np.unique(ci, return_inverse=True)
ci += 1
n = len(W) # number of vertices
def pcoef(W_):
S = np.sum(W_, axis=1) # strength
# neighbor community affil.
Gc = np.dot(np.logical_not(W_ == 0), np.diag(ci))
Sc2 = np.zeros((n,))
for i in range(1, int(np.max(ci) + 1)):
Sc2 += np.square(np.sum(W_ * (Gc == i), axis=1))
P = np.ones((n,)) - Sc2 / np.square(S)
P[np.where(np.isnan(P))] = 0
P[np.where(np.logical_not(P))] = 0 # p_ind=0 if no (out)neighbors
return P
# explicitly ignore compiler warning for division by zero
with np.errstate(invalid='ignore'):
Ppos = pcoef(W * (W > 0))
Pneg = pcoef(-W * (W < 0))
return Ppos, Pneg
def subgraph_centrality(CIJ):
"""
The subgraph centrality of a node is a weighted sum of closed walks of
different lengths in the network starting and ending at the node. This
function returns a vector of subgraph centralities for each node of the
network.
Parameters
----------
CIJ : NxN :obj:`numpy.ndarray`
binary adjacency matrix
Cs : Nx1 :obj:`numpy.ndarray`
subgraph centrality
"""
from scipy import linalg
vals, vecs = linalg.eig(CIJ) # compute eigendecomposition
# lambdas=np.diag(vals)
# compute eigenvector centr.
Cs = np.real(np.dot(vecs * vecs, np.exp(vals)))
return Cs # imaginary part from precision error