comparison env/lib/python3.9/site-packages/networkx/algorithms/assortativity/neighbor_degree.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 __all__ = ["average_neighbor_degree"]
2
3
4 def _average_nbr_deg(G, source_degree, target_degree, nodes=None, weight=None):
5 # average degree of neighbors
6 avg = {}
7 for n, deg in source_degree(nodes, weight=weight):
8 # normalize but not by zero degree
9 if deg == 0:
10 deg = 1
11 nbrdeg = target_degree(G[n])
12 if weight is None:
13 avg[n] = sum(d for n, d in nbrdeg) / float(deg)
14 else:
15 avg[n] = sum((G[n][nbr].get(weight, 1) * d for nbr, d in nbrdeg)) / float(
16 deg
17 )
18 return avg
19
20
21 def average_neighbor_degree(G, source="out", target="out", nodes=None, weight=None):
22 r"""Returns the average degree of the neighborhood of each node.
23
24 The average neighborhood degree of a node `i` is
25
26 .. math::
27
28 k_{nn,i} = \frac{1}{|N(i)|} \sum_{j \in N(i)} k_j
29
30 where `N(i)` are the neighbors of node `i` and `k_j` is
31 the degree of node `j` which belongs to `N(i)`. For weighted
32 graphs, an analogous measure can be defined [1]_,
33
34 .. math::
35
36 k_{nn,i}^{w} = \frac{1}{s_i} \sum_{j \in N(i)} w_{ij} k_j
37
38 where `s_i` is the weighted degree of node `i`, `w_{ij}`
39 is the weight of the edge that links `i` and `j` and
40 `N(i)` are the neighbors of node `i`.
41
42
43 Parameters
44 ----------
45 G : NetworkX graph
46
47 source : string ("in"|"out")
48 Directed graphs only.
49 Use "in"- or "out"-degree for source node.
50
51 target : string ("in"|"out")
52 Directed graphs only.
53 Use "in"- or "out"-degree for target node.
54
55 nodes : list or iterable, optional
56 Compute neighbor degree for specified nodes. The default is
57 all nodes in the graph.
58
59 weight : string or None, optional (default=None)
60 The edge attribute that holds the numerical value used as a weight.
61 If None, then each edge has weight 1.
62
63 Returns
64 -------
65 d: dict
66 A dictionary keyed by node with average neighbors degree value.
67
68 Examples
69 --------
70 >>> G = nx.path_graph(4)
71 >>> G.edges[0, 1]["weight"] = 5
72 >>> G.edges[2, 3]["weight"] = 3
73
74 >>> nx.average_neighbor_degree(G)
75 {0: 2.0, 1: 1.5, 2: 1.5, 3: 2.0}
76 >>> nx.average_neighbor_degree(G, weight="weight")
77 {0: 2.0, 1: 1.1666666666666667, 2: 1.25, 3: 2.0}
78
79 >>> G = nx.DiGraph()
80 >>> nx.add_path(G, [0, 1, 2, 3])
81 >>> nx.average_neighbor_degree(G, source="in", target="in")
82 {0: 1.0, 1: 1.0, 2: 1.0, 3: 0.0}
83
84 >>> nx.average_neighbor_degree(G, source="out", target="out")
85 {0: 1.0, 1: 1.0, 2: 0.0, 3: 0.0}
86
87 Notes
88 -----
89 For directed graphs you can also specify in-degree or out-degree
90 by passing keyword arguments.
91
92 See Also
93 --------
94 average_degree_connectivity
95
96 References
97 ----------
98 .. [1] A. Barrat, M. Barthélemy, R. Pastor-Satorras, and A. Vespignani,
99 "The architecture of complex weighted networks".
100 PNAS 101 (11): 3747–3752 (2004).
101 """
102 source_degree = G.degree
103 target_degree = G.degree
104 if G.is_directed():
105 direction = {"out": G.out_degree, "in": G.in_degree}
106 source_degree = direction[source]
107 target_degree = direction[target]
108 return _average_nbr_deg(G, source_degree, target_degree, nodes=nodes, weight=weight)
109
110
111 # obsolete
112 # def average_neighbor_in_degree(G, nodes=None, weight=None):
113 # if not G.is_directed():
114 # raise nx.NetworkXError("Not defined for undirected graphs.")
115 # return _average_nbr_deg(G, G.in_degree, G.in_degree, nodes, weight)
116 # average_neighbor_in_degree.__doc__=average_neighbor_degree.__doc__
117
118 # def average_neighbor_out_degree(G, nodes=None, weight=None):
119 # if not G.is_directed():
120 # raise nx.NetworkXError("Not defined for undirected graphs.")
121 # return _average_nbr_deg(G, G.out_degree, G.out_degree, nodes, weight)
122 # average_neighbor_out_degree.__doc__=average_neighbor_degree.__doc__