Mercurial > repos > shellac > sam_consensus_v3
comparison env/lib/python3.9/site-packages/networkx/algorithms/bipartite/spectral.py @ 0:4f3585e2f14b draft default tip
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author | shellac |
---|---|
date | Mon, 22 Mar 2021 18:12:50 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4f3585e2f14b |
---|---|
1 """ | |
2 Spectral bipartivity measure. | |
3 """ | |
4 import networkx as nx | |
5 | |
6 __all__ = ["spectral_bipartivity"] | |
7 | |
8 | |
9 def spectral_bipartivity(G, nodes=None, weight="weight"): | |
10 """Returns the spectral bipartivity. | |
11 | |
12 Parameters | |
13 ---------- | |
14 G : NetworkX graph | |
15 | |
16 nodes : list or container optional(default is all nodes) | |
17 Nodes to return value of spectral bipartivity contribution. | |
18 | |
19 weight : string or None optional (default = 'weight') | |
20 Edge data key to use for edge weights. If None, weights set to 1. | |
21 | |
22 Returns | |
23 ------- | |
24 sb : float or dict | |
25 A single number if the keyword nodes is not specified, or | |
26 a dictionary keyed by node with the spectral bipartivity contribution | |
27 of that node as the value. | |
28 | |
29 Examples | |
30 -------- | |
31 >>> from networkx.algorithms import bipartite | |
32 >>> G = nx.path_graph(4) | |
33 >>> bipartite.spectral_bipartivity(G) | |
34 1.0 | |
35 | |
36 Notes | |
37 ----- | |
38 This implementation uses Numpy (dense) matrices which are not efficient | |
39 for storing large sparse graphs. | |
40 | |
41 See Also | |
42 -------- | |
43 color | |
44 | |
45 References | |
46 ---------- | |
47 .. [1] E. Estrada and J. A. Rodríguez-Velázquez, "Spectral measures of | |
48 bipartivity in complex networks", PhysRev E 72, 046105 (2005) | |
49 """ | |
50 try: | |
51 import scipy.linalg | |
52 except ImportError as e: | |
53 raise ImportError( | |
54 "spectral_bipartivity() requires SciPy: ", "http://scipy.org/" | |
55 ) from e | |
56 nodelist = list(G) # ordering of nodes in matrix | |
57 A = nx.to_numpy_array(G, nodelist, weight=weight) | |
58 expA = scipy.linalg.expm(A) | |
59 expmA = scipy.linalg.expm(-A) | |
60 coshA = 0.5 * (expA + expmA) | |
61 if nodes is None: | |
62 # return single number for entire graph | |
63 return coshA.diagonal().sum() / expA.diagonal().sum() | |
64 else: | |
65 # contribution for individual nodes | |
66 index = dict(zip(nodelist, range(len(nodelist)))) | |
67 sb = {} | |
68 for n in nodes: | |
69 i = index[n] | |
70 sb[n] = coshA[i, i] / expA[i, i] | |
71 return sb |