comparison env/lib/python3.9/site-packages/networkx/algorithms/assortativity/correlation.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 """Node assortativity coefficients and correlation measures.
2 """
3 from networkx.algorithms.assortativity.mixing import (
4 degree_mixing_matrix,
5 attribute_mixing_matrix,
6 numeric_mixing_matrix,
7 )
8 from networkx.algorithms.assortativity.pairs import node_degree_xy
9
10 __all__ = [
11 "degree_pearson_correlation_coefficient",
12 "degree_assortativity_coefficient",
13 "attribute_assortativity_coefficient",
14 "numeric_assortativity_coefficient",
15 ]
16
17
18 def degree_assortativity_coefficient(G, x="out", y="in", weight=None, nodes=None):
19 """Compute degree assortativity of graph.
20
21 Assortativity measures the similarity of connections
22 in the graph with respect to the node degree.
23
24 Parameters
25 ----------
26 G : NetworkX graph
27
28 x: string ('in','out')
29 The degree type for source node (directed graphs only).
30
31 y: string ('in','out')
32 The degree type for target node (directed graphs only).
33
34 weight: string or None, optional (default=None)
35 The edge attribute that holds the numerical value used
36 as a weight. If None, then each edge has weight 1.
37 The degree is the sum of the edge weights adjacent to the node.
38
39 nodes: list or iterable (optional)
40 Compute degree assortativity only for nodes in container.
41 The default is all nodes.
42
43 Returns
44 -------
45 r : float
46 Assortativity of graph by degree.
47
48 Examples
49 --------
50 >>> G = nx.path_graph(4)
51 >>> r = nx.degree_assortativity_coefficient(G)
52 >>> print(f"{r:3.1f}")
53 -0.5
54
55 See Also
56 --------
57 attribute_assortativity_coefficient
58 numeric_assortativity_coefficient
59 neighbor_connectivity
60 degree_mixing_dict
61 degree_mixing_matrix
62
63 Notes
64 -----
65 This computes Eq. (21) in Ref. [1]_ , where e is the joint
66 probability distribution (mixing matrix) of the degrees. If G is
67 directed than the matrix e is the joint probability of the
68 user-specified degree type for the source and target.
69
70 References
71 ----------
72 .. [1] M. E. J. Newman, Mixing patterns in networks,
73 Physical Review E, 67 026126, 2003
74 .. [2] Foster, J.G., Foster, D.V., Grassberger, P. & Paczuski, M.
75 Edge direction and the structure of networks, PNAS 107, 10815-20 (2010).
76 """
77 M = degree_mixing_matrix(G, x=x, y=y, nodes=nodes, weight=weight)
78 return numeric_ac(M)
79
80
81 def degree_pearson_correlation_coefficient(G, x="out", y="in", weight=None, nodes=None):
82 """Compute degree assortativity of graph.
83
84 Assortativity measures the similarity of connections
85 in the graph with respect to the node degree.
86
87 This is the same as degree_assortativity_coefficient but uses the
88 potentially faster scipy.stats.pearsonr function.
89
90 Parameters
91 ----------
92 G : NetworkX graph
93
94 x: string ('in','out')
95 The degree type for source node (directed graphs only).
96
97 y: string ('in','out')
98 The degree type for target node (directed graphs only).
99
100 weight: string or None, optional (default=None)
101 The edge attribute that holds the numerical value used
102 as a weight. If None, then each edge has weight 1.
103 The degree is the sum of the edge weights adjacent to the node.
104
105 nodes: list or iterable (optional)
106 Compute pearson correlation of degrees only for specified nodes.
107 The default is all nodes.
108
109 Returns
110 -------
111 r : float
112 Assortativity of graph by degree.
113
114 Examples
115 --------
116 >>> G = nx.path_graph(4)
117 >>> r = nx.degree_pearson_correlation_coefficient(G)
118 >>> print(f"{r:3.1f}")
119 -0.5
120
121 Notes
122 -----
123 This calls scipy.stats.pearsonr.
124
125 References
126 ----------
127 .. [1] M. E. J. Newman, Mixing patterns in networks
128 Physical Review E, 67 026126, 2003
129 .. [2] Foster, J.G., Foster, D.V., Grassberger, P. & Paczuski, M.
130 Edge direction and the structure of networks, PNAS 107, 10815-20 (2010).
131 """
132 try:
133 import scipy.stats as stats
134 except ImportError as e:
135 raise ImportError("Assortativity requires SciPy:" "http://scipy.org/ ") from e
136 xy = node_degree_xy(G, x=x, y=y, nodes=nodes, weight=weight)
137 x, y = zip(*xy)
138 return stats.pearsonr(x, y)[0]
139
140
141 def attribute_assortativity_coefficient(G, attribute, nodes=None):
142 """Compute assortativity for node attributes.
143
144 Assortativity measures the similarity of connections
145 in the graph with respect to the given attribute.
146
147 Parameters
148 ----------
149 G : NetworkX graph
150
151 attribute : string
152 Node attribute key
153
154 nodes: list or iterable (optional)
155 Compute attribute assortativity for nodes in container.
156 The default is all nodes.
157
158 Returns
159 -------
160 r: float
161 Assortativity of graph for given attribute
162
163 Examples
164 --------
165 >>> G = nx.Graph()
166 >>> G.add_nodes_from([0, 1], color="red")
167 >>> G.add_nodes_from([2, 3], color="blue")
168 >>> G.add_edges_from([(0, 1), (2, 3)])
169 >>> print(nx.attribute_assortativity_coefficient(G, "color"))
170 1.0
171
172 Notes
173 -----
174 This computes Eq. (2) in Ref. [1]_ , (trace(M)-sum(M^2))/(1-sum(M^2)),
175 where M is the joint probability distribution (mixing matrix)
176 of the specified attribute.
177
178 References
179 ----------
180 .. [1] M. E. J. Newman, Mixing patterns in networks,
181 Physical Review E, 67 026126, 2003
182 """
183 M = attribute_mixing_matrix(G, attribute, nodes)
184 return attribute_ac(M)
185
186
187 def numeric_assortativity_coefficient(G, attribute, nodes=None):
188 """Compute assortativity for numerical node attributes.
189
190 Assortativity measures the similarity of connections
191 in the graph with respect to the given numeric attribute.
192 The numeric attribute must be an integer.
193
194 Parameters
195 ----------
196 G : NetworkX graph
197
198 attribute : string
199 Node attribute key. The corresponding attribute value must be an
200 integer.
201
202 nodes: list or iterable (optional)
203 Compute numeric assortativity only for attributes of nodes in
204 container. The default is all nodes.
205
206 Returns
207 -------
208 r: float
209 Assortativity of graph for given attribute
210
211 Examples
212 --------
213 >>> G = nx.Graph()
214 >>> G.add_nodes_from([0, 1], size=2)
215 >>> G.add_nodes_from([2, 3], size=3)
216 >>> G.add_edges_from([(0, 1), (2, 3)])
217 >>> print(nx.numeric_assortativity_coefficient(G, "size"))
218 1.0
219
220 Notes
221 -----
222 This computes Eq. (21) in Ref. [1]_ , for the mixing matrix of
223 of the specified attribute.
224
225 References
226 ----------
227 .. [1] M. E. J. Newman, Mixing patterns in networks
228 Physical Review E, 67 026126, 2003
229 """
230 a = numeric_mixing_matrix(G, attribute, nodes)
231 return numeric_ac(a)
232
233
234 def attribute_ac(M):
235 """Compute assortativity for attribute matrix M.
236
237 Parameters
238 ----------
239 M : numpy.ndarray
240 2D ndarray representing the attribute mixing matrix.
241
242 Notes
243 -----
244 This computes Eq. (2) in Ref. [1]_ , (trace(e)-sum(e^2))/(1-sum(e^2)),
245 where e is the joint probability distribution (mixing matrix)
246 of the specified attribute.
247
248 References
249 ----------
250 .. [1] M. E. J. Newman, Mixing patterns in networks,
251 Physical Review E, 67 026126, 2003
252 """
253 try:
254 import numpy
255 except ImportError as e:
256 raise ImportError(
257 "attribute_assortativity requires " "NumPy: http://scipy.org/"
258 ) from e
259 if M.sum() != 1.0:
260 M = M / M.sum()
261 s = (M @ M).sum()
262 t = M.trace()
263 r = (t - s) / (1 - s)
264 return r
265
266
267 def numeric_ac(M):
268 # M is a numpy matrix or array
269 # numeric assortativity coefficient, pearsonr
270 try:
271 import numpy
272 except ImportError as e:
273 raise ImportError(
274 "numeric_assortativity requires " "NumPy: http://scipy.org/"
275 ) from e
276 if M.sum() != 1.0:
277 M = M / float(M.sum())
278 nx, ny = M.shape # nx=ny
279 x = numpy.arange(nx)
280 y = numpy.arange(ny)
281 a = M.sum(axis=0)
282 b = M.sum(axis=1)
283 vara = (a * x ** 2).sum() - ((a * x).sum()) ** 2
284 varb = (b * x ** 2).sum() - ((b * x).sum()) ** 2
285 xy = numpy.outer(x, y)
286 ab = numpy.outer(a, b)
287 return (xy * (M - ab)).sum() / numpy.sqrt(vara * varb)