Mercurial > repos > shellac > sam_consensus_v3
comparison env/lib/python3.9/site-packages/networkx/algorithms/reciprocity.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 """Algorithms to calculate reciprocity in a directed graph.""" | |
2 from networkx import NetworkXError | |
3 from ..utils import not_implemented_for | |
4 | |
5 __all__ = ["reciprocity", "overall_reciprocity"] | |
6 | |
7 | |
8 @not_implemented_for("undirected", "multigraph") | |
9 def reciprocity(G, nodes=None): | |
10 r"""Compute the reciprocity in a directed graph. | |
11 | |
12 The reciprocity of a directed graph is defined as the ratio | |
13 of the number of edges pointing in both directions to the total | |
14 number of edges in the graph. | |
15 Formally, $r = |{(u,v) \in G|(v,u) \in G}| / |{(u,v) \in G}|$. | |
16 | |
17 The reciprocity of a single node u is defined similarly, | |
18 it is the ratio of the number of edges in both directions to | |
19 the total number of edges attached to node u. | |
20 | |
21 Parameters | |
22 ---------- | |
23 G : graph | |
24 A networkx directed graph | |
25 nodes : container of nodes, optional (default=whole graph) | |
26 Compute reciprocity for nodes in this container. | |
27 | |
28 Returns | |
29 ------- | |
30 out : dictionary | |
31 Reciprocity keyed by node label. | |
32 | |
33 Notes | |
34 ----- | |
35 The reciprocity is not defined for isolated nodes. | |
36 In such cases this function will return None. | |
37 | |
38 """ | |
39 # If `nodes` is not specified, calculate the reciprocity of the graph. | |
40 if nodes is None: | |
41 return overall_reciprocity(G) | |
42 | |
43 # If `nodes` represents a single node in the graph, return only its | |
44 # reciprocity. | |
45 if nodes in G: | |
46 reciprocity = next(_reciprocity_iter(G, nodes))[1] | |
47 if reciprocity is None: | |
48 raise NetworkXError("Not defined for isolated nodes.") | |
49 else: | |
50 return reciprocity | |
51 | |
52 # Otherwise, `nodes` represents an iterable of nodes, so return a | |
53 # dictionary mapping node to its reciprocity. | |
54 return dict(_reciprocity_iter(G, nodes)) | |
55 | |
56 | |
57 def _reciprocity_iter(G, nodes): | |
58 """ Return an iterator of (node, reciprocity). | |
59 """ | |
60 n = G.nbunch_iter(nodes) | |
61 for node in n: | |
62 pred = set(G.predecessors(node)) | |
63 succ = set(G.successors(node)) | |
64 overlap = pred & succ | |
65 n_total = len(pred) + len(succ) | |
66 | |
67 # Reciprocity is not defined for isolated nodes. | |
68 # Return None. | |
69 if n_total == 0: | |
70 yield (node, None) | |
71 else: | |
72 reciprocity = 2.0 * float(len(overlap)) / float(n_total) | |
73 yield (node, reciprocity) | |
74 | |
75 | |
76 @not_implemented_for("undirected", "multigraph") | |
77 def overall_reciprocity(G): | |
78 """Compute the reciprocity for the whole graph. | |
79 | |
80 See the doc of reciprocity for the definition. | |
81 | |
82 Parameters | |
83 ---------- | |
84 G : graph | |
85 A networkx graph | |
86 | |
87 """ | |
88 n_all_edge = G.number_of_edges() | |
89 n_overlap_edge = (n_all_edge - G.to_undirected().number_of_edges()) * 2 | |
90 | |
91 if n_all_edge == 0: | |
92 raise NetworkXError("Not defined for empty graphs") | |
93 | |
94 return float(n_overlap_edge) / float(n_all_edge) |