comparison env/lib/python3.9/site-packages/networkx/algorithms/assortativity/mixing.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 Mixing matrices for node attributes and degree.
3 """
4 from networkx.utils import dict_to_numpy_array
5 from networkx.algorithms.assortativity.pairs import node_degree_xy, node_attribute_xy
6
7 __all__ = [
8 "attribute_mixing_matrix",
9 "attribute_mixing_dict",
10 "degree_mixing_matrix",
11 "degree_mixing_dict",
12 "numeric_mixing_matrix",
13 "mixing_dict",
14 ]
15
16
17 def attribute_mixing_dict(G, attribute, nodes=None, normalized=False):
18 """Returns dictionary representation of mixing matrix for attribute.
19
20 Parameters
21 ----------
22 G : graph
23 NetworkX graph object.
24
25 attribute : string
26 Node attribute key.
27
28 nodes: list or iterable (optional)
29 Unse nodes in container to build the dict. The default is all nodes.
30
31 normalized : bool (default=False)
32 Return counts if False or probabilities if True.
33
34 Examples
35 --------
36 >>> G = nx.Graph()
37 >>> G.add_nodes_from([0, 1], color="red")
38 >>> G.add_nodes_from([2, 3], color="blue")
39 >>> G.add_edge(1, 3)
40 >>> d = nx.attribute_mixing_dict(G, "color")
41 >>> print(d["red"]["blue"])
42 1
43 >>> print(d["blue"]["red"]) # d symmetric for undirected graphs
44 1
45
46 Returns
47 -------
48 d : dictionary
49 Counts or joint probability of occurrence of attribute pairs.
50 """
51 xy_iter = node_attribute_xy(G, attribute, nodes)
52 return mixing_dict(xy_iter, normalized=normalized)
53
54
55 def attribute_mixing_matrix(G, attribute, nodes=None, mapping=None, normalized=True):
56 """Returns mixing matrix for attribute.
57
58 Parameters
59 ----------
60 G : graph
61 NetworkX graph object.
62
63 attribute : string
64 Node attribute key.
65
66 nodes: list or iterable (optional)
67 Use only nodes in container to build the matrix. The default is
68 all nodes.
69
70 mapping : dictionary, optional
71 Mapping from node attribute to integer index in matrix.
72 If not specified, an arbitrary ordering will be used.
73
74 normalized : bool (default=True)
75 Return counts if False or probabilities if True.
76
77 Returns
78 -------
79 m: numpy array
80 Counts or joint probability of occurrence of attribute pairs.
81 """
82 d = attribute_mixing_dict(G, attribute, nodes)
83 a = dict_to_numpy_array(d, mapping=mapping)
84 if normalized:
85 a = a / a.sum()
86 return a
87
88
89 def degree_mixing_dict(G, x="out", y="in", weight=None, nodes=None, normalized=False):
90 """Returns dictionary representation of mixing matrix for degree.
91
92 Parameters
93 ----------
94 G : graph
95 NetworkX graph object.
96
97 x: string ('in','out')
98 The degree type for source node (directed graphs only).
99
100 y: string ('in','out')
101 The degree type for target node (directed graphs only).
102
103 weight: string or None, optional (default=None)
104 The edge attribute that holds the numerical value used
105 as a weight. If None, then each edge has weight 1.
106 The degree is the sum of the edge weights adjacent to the node.
107
108 normalized : bool (default=False)
109 Return counts if False or probabilities if True.
110
111 Returns
112 -------
113 d: dictionary
114 Counts or joint probability of occurrence of degree pairs.
115 """
116 xy_iter = node_degree_xy(G, x=x, y=y, nodes=nodes, weight=weight)
117 return mixing_dict(xy_iter, normalized=normalized)
118
119
120 def degree_mixing_matrix(G, x="out", y="in", weight=None, nodes=None, normalized=True):
121 """Returns mixing matrix for attribute.
122
123 Parameters
124 ----------
125 G : graph
126 NetworkX graph object.
127
128 x: string ('in','out')
129 The degree type for source node (directed graphs only).
130
131 y: string ('in','out')
132 The degree type for target node (directed graphs only).
133
134 nodes: list or iterable (optional)
135 Build the matrix using only nodes in container.
136 The default is all nodes.
137
138 weight: string or None, optional (default=None)
139 The edge attribute that holds the numerical value used
140 as a weight. If None, then each edge has weight 1.
141 The degree is the sum of the edge weights adjacent to the node.
142
143 normalized : bool (default=True)
144 Return counts if False or probabilities if True.
145
146 Returns
147 -------
148 m: numpy array
149 Counts, or joint probability, of occurrence of node degree.
150 """
151 d = degree_mixing_dict(G, x=x, y=y, nodes=nodes, weight=weight)
152 s = set(d.keys())
153 for k, v in d.items():
154 s.update(v.keys())
155 m = max(s)
156 mapping = {x: x for x in range(m + 1)}
157 a = dict_to_numpy_array(d, mapping=mapping)
158 if normalized:
159 a = a / a.sum()
160 return a
161
162
163 def numeric_mixing_matrix(G, attribute, nodes=None, normalized=True):
164 """Returns numeric mixing matrix for attribute.
165
166 The attribute must be an integer.
167
168 Parameters
169 ----------
170 G : graph
171 NetworkX graph object.
172
173 attribute : string
174 Node attribute key. The corresponding attribute must be an integer.
175
176 nodes: list or iterable (optional)
177 Build the matrix only with nodes in container. The default is all nodes.
178
179 normalized : bool (default=True)
180 Return counts if False or probabilities if True.
181
182 Returns
183 -------
184 m: numpy array
185 Counts, or joint, probability of occurrence of node attribute pairs.
186 """
187 d = attribute_mixing_dict(G, attribute, nodes)
188 s = set(d.keys())
189 for k, v in d.items():
190 s.update(v.keys())
191 m = max(s)
192 mapping = {x: x for x in range(m + 1)}
193 a = dict_to_numpy_array(d, mapping=mapping)
194 if normalized:
195 a = a / a.sum()
196 return a
197
198
199 def mixing_dict(xy, normalized=False):
200 """Returns a dictionary representation of mixing matrix.
201
202 Parameters
203 ----------
204 xy : list or container of two-tuples
205 Pairs of (x,y) items.
206
207 attribute : string
208 Node attribute key
209
210 normalized : bool (default=False)
211 Return counts if False or probabilities if True.
212
213 Returns
214 -------
215 d: dictionary
216 Counts or Joint probability of occurrence of values in xy.
217 """
218 d = {}
219 psum = 0.0
220 for x, y in xy:
221 if x not in d:
222 d[x] = {}
223 if y not in d:
224 d[y] = {}
225 v = d[x].get(y, 0)
226 d[x][y] = v + 1
227 psum += 1
228
229 if normalized:
230 for k, jdict in d.items():
231 for j in jdict:
232 jdict[j] /= psum
233 return d