attrmatrix.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. """
  2. Functions for constructing matrix-like objects from graph attributes.
  3. """
  4. __all__ = ["attr_matrix", "attr_sparse_matrix"]
  5. def _node_value(G, node_attr):
  6. """Returns a function that returns a value from G.nodes[u].
  7. We return a function expecting a node as its sole argument. Then, in the
  8. simplest scenario, the returned function will return G.nodes[u][node_attr].
  9. However, we also handle the case when `node_attr` is None or when it is a
  10. function itself.
  11. Parameters
  12. ----------
  13. G : graph
  14. A NetworkX graph
  15. node_attr : {None, str, callable}
  16. Specification of how the value of the node attribute should be obtained
  17. from the node attribute dictionary.
  18. Returns
  19. -------
  20. value : function
  21. A function expecting a node as its sole argument. The function will
  22. returns a value from G.nodes[u] that depends on `edge_attr`.
  23. """
  24. if node_attr is None:
  25. def value(u):
  26. return u
  27. elif not callable(node_attr):
  28. # assume it is a key for the node attribute dictionary
  29. def value(u):
  30. return G.nodes[u][node_attr]
  31. else:
  32. # Advanced: Allow users to specify something else.
  33. #
  34. # For example,
  35. # node_attr = lambda u: G.nodes[u].get('size', .5) * 3
  36. #
  37. value = node_attr
  38. return value
  39. def _edge_value(G, edge_attr):
  40. """Returns a function that returns a value from G[u][v].
  41. Suppose there exists an edge between u and v. Then we return a function
  42. expecting u and v as arguments. For Graph and DiGraph, G[u][v] is
  43. the edge attribute dictionary, and the function (essentially) returns
  44. G[u][v][edge_attr]. However, we also handle cases when `edge_attr` is None
  45. and when it is a function itself. For MultiGraph and MultiDiGraph, G[u][v]
  46. is a dictionary of all edges between u and v. In this case, the returned
  47. function sums the value of `edge_attr` for every edge between u and v.
  48. Parameters
  49. ----------
  50. G : graph
  51. A NetworkX graph
  52. edge_attr : {None, str, callable}
  53. Specification of how the value of the edge attribute should be obtained
  54. from the edge attribute dictionary, G[u][v]. For multigraphs, G[u][v]
  55. is a dictionary of all the edges between u and v. This allows for
  56. special treatment of multiedges.
  57. Returns
  58. -------
  59. value : function
  60. A function expecting two nodes as parameters. The nodes should
  61. represent the from- and to- node of an edge. The function will
  62. return a value from G[u][v] that depends on `edge_attr`.
  63. """
  64. if edge_attr is None:
  65. # topological count of edges
  66. if G.is_multigraph():
  67. def value(u, v):
  68. return len(G[u][v])
  69. else:
  70. def value(u, v):
  71. return 1
  72. elif not callable(edge_attr):
  73. # assume it is a key for the edge attribute dictionary
  74. if edge_attr == "weight":
  75. # provide a default value
  76. if G.is_multigraph():
  77. def value(u, v):
  78. return sum(d.get(edge_attr, 1) for d in G[u][v].values())
  79. else:
  80. def value(u, v):
  81. return G[u][v].get(edge_attr, 1)
  82. else:
  83. # otherwise, the edge attribute MUST exist for each edge
  84. if G.is_multigraph():
  85. def value(u, v):
  86. return sum(d[edge_attr] for d in G[u][v].values())
  87. else:
  88. def value(u, v):
  89. return G[u][v][edge_attr]
  90. else:
  91. # Advanced: Allow users to specify something else.
  92. #
  93. # Alternative default value:
  94. # edge_attr = lambda u,v: G[u][v].get('thickness', .5)
  95. #
  96. # Function on an attribute:
  97. # edge_attr = lambda u,v: abs(G[u][v]['weight'])
  98. #
  99. # Handle Multi(Di)Graphs differently:
  100. # edge_attr = lambda u,v: numpy.prod([d['size'] for d in G[u][v].values()])
  101. #
  102. # Ignore multiple edges
  103. # edge_attr = lambda u,v: 1 if len(G[u][v]) else 0
  104. #
  105. value = edge_attr
  106. return value
  107. def attr_matrix(
  108. G,
  109. edge_attr=None,
  110. node_attr=None,
  111. normalized=False,
  112. rc_order=None,
  113. dtype=None,
  114. order=None,
  115. ):
  116. """Returns the attribute matrix using attributes from `G` as a numpy array.
  117. If only `G` is passed in, then the adjacency matrix is constructed.
  118. Let A be a discrete set of values for the node attribute `node_attr`. Then
  119. the elements of A represent the rows and columns of the constructed matrix.
  120. Now, iterate through every edge e=(u,v) in `G` and consider the value
  121. of the edge attribute `edge_attr`. If ua and va are the values of the
  122. node attribute `node_attr` for u and v, respectively, then the value of
  123. the edge attribute is added to the matrix element at (ua, va).
  124. Parameters
  125. ----------
  126. G : graph
  127. The NetworkX graph used to construct the attribute matrix.
  128. edge_attr : str, optional
  129. Each element of the matrix represents a running total of the
  130. specified edge attribute for edges whose node attributes correspond
  131. to the rows/cols of the matrix. The attribute must be present for
  132. all edges in the graph. If no attribute is specified, then we
  133. just count the number of edges whose node attributes correspond
  134. to the matrix element.
  135. node_attr : str, optional
  136. Each row and column in the matrix represents a particular value
  137. of the node attribute. The attribute must be present for all nodes
  138. in the graph. Note, the values of this attribute should be reliably
  139. hashable. So, float values are not recommended. If no attribute is
  140. specified, then the rows and columns will be the nodes of the graph.
  141. normalized : bool, optional
  142. If True, then each row is normalized by the summation of its values.
  143. rc_order : list, optional
  144. A list of the node attribute values. This list specifies the ordering
  145. of rows and columns of the array. If no ordering is provided, then
  146. the ordering will be random (and also, a return value).
  147. Other Parameters
  148. ----------------
  149. dtype : NumPy data-type, optional
  150. A valid NumPy dtype used to initialize the array. Keep in mind certain
  151. dtypes can yield unexpected results if the array is to be normalized.
  152. The parameter is passed to numpy.zeros(). If unspecified, the NumPy
  153. default is used.
  154. order : {'C', 'F'}, optional
  155. Whether to store multidimensional data in C- or Fortran-contiguous
  156. (row- or column-wise) order in memory. This parameter is passed to
  157. numpy.zeros(). If unspecified, the NumPy default is used.
  158. Returns
  159. -------
  160. M : 2D NumPy ndarray
  161. The attribute matrix.
  162. ordering : list
  163. If `rc_order` was specified, then only the attribute matrix is returned.
  164. However, if `rc_order` was None, then the ordering used to construct
  165. the matrix is returned as well.
  166. Examples
  167. --------
  168. Construct an adjacency matrix:
  169. >>> G = nx.Graph()
  170. >>> G.add_edge(0, 1, thickness=1, weight=3)
  171. >>> G.add_edge(0, 2, thickness=2)
  172. >>> G.add_edge(1, 2, thickness=3)
  173. >>> nx.attr_matrix(G, rc_order=[0, 1, 2])
  174. array([[0., 1., 1.],
  175. [1., 0., 1.],
  176. [1., 1., 0.]])
  177. Alternatively, we can obtain the matrix describing edge thickness.
  178. >>> nx.attr_matrix(G, edge_attr="thickness", rc_order=[0, 1, 2])
  179. array([[0., 1., 2.],
  180. [1., 0., 3.],
  181. [2., 3., 0.]])
  182. We can also color the nodes and ask for the probability distribution over
  183. all edges (u,v) describing:
  184. Pr(v has color Y | u has color X)
  185. >>> G.nodes[0]["color"] = "red"
  186. >>> G.nodes[1]["color"] = "red"
  187. >>> G.nodes[2]["color"] = "blue"
  188. >>> rc = ["red", "blue"]
  189. >>> nx.attr_matrix(G, node_attr="color", normalized=True, rc_order=rc)
  190. array([[0.33333333, 0.66666667],
  191. [1. , 0. ]])
  192. For example, the above tells us that for all edges (u,v):
  193. Pr( v is red | u is red) = 1/3
  194. Pr( v is blue | u is red) = 2/3
  195. Pr( v is red | u is blue) = 1
  196. Pr( v is blue | u is blue) = 0
  197. Finally, we can obtain the total weights listed by the node colors.
  198. >>> nx.attr_matrix(G, edge_attr="weight", node_attr="color", rc_order=rc)
  199. array([[3., 2.],
  200. [2., 0.]])
  201. Thus, the total weight over all edges (u,v) with u and v having colors:
  202. (red, red) is 3 # the sole contribution is from edge (0,1)
  203. (red, blue) is 2 # contributions from edges (0,2) and (1,2)
  204. (blue, red) is 2 # same as (red, blue) since graph is undirected
  205. (blue, blue) is 0 # there are no edges with blue endpoints
  206. """
  207. import numpy as np
  208. edge_value = _edge_value(G, edge_attr)
  209. node_value = _node_value(G, node_attr)
  210. if rc_order is None:
  211. ordering = list({node_value(n) for n in G})
  212. else:
  213. ordering = rc_order
  214. N = len(ordering)
  215. undirected = not G.is_directed()
  216. index = dict(zip(ordering, range(N)))
  217. M = np.zeros((N, N), dtype=dtype, order=order)
  218. seen = set()
  219. for u, nbrdict in G.adjacency():
  220. for v in nbrdict:
  221. # Obtain the node attribute values.
  222. i, j = index[node_value(u)], index[node_value(v)]
  223. if v not in seen:
  224. M[i, j] += edge_value(u, v)
  225. if undirected:
  226. M[j, i] = M[i, j]
  227. if undirected:
  228. seen.add(u)
  229. if normalized:
  230. M /= M.sum(axis=1).reshape((N, 1))
  231. if rc_order is None:
  232. return M, ordering
  233. else:
  234. return M
  235. def attr_sparse_matrix(
  236. G, edge_attr=None, node_attr=None, normalized=False, rc_order=None, dtype=None
  237. ):
  238. """Returns a SciPy sparse array using attributes from G.
  239. If only `G` is passed in, then the adjacency matrix is constructed.
  240. Let A be a discrete set of values for the node attribute `node_attr`. Then
  241. the elements of A represent the rows and columns of the constructed matrix.
  242. Now, iterate through every edge e=(u,v) in `G` and consider the value
  243. of the edge attribute `edge_attr`. If ua and va are the values of the
  244. node attribute `node_attr` for u and v, respectively, then the value of
  245. the edge attribute is added to the matrix element at (ua, va).
  246. Parameters
  247. ----------
  248. G : graph
  249. The NetworkX graph used to construct the NumPy matrix.
  250. edge_attr : str, optional
  251. Each element of the matrix represents a running total of the
  252. specified edge attribute for edges whose node attributes correspond
  253. to the rows/cols of the matrix. The attribute must be present for
  254. all edges in the graph. If no attribute is specified, then we
  255. just count the number of edges whose node attributes correspond
  256. to the matrix element.
  257. node_attr : str, optional
  258. Each row and column in the matrix represents a particular value
  259. of the node attribute. The attribute must be present for all nodes
  260. in the graph. Note, the values of this attribute should be reliably
  261. hashable. So, float values are not recommended. If no attribute is
  262. specified, then the rows and columns will be the nodes of the graph.
  263. normalized : bool, optional
  264. If True, then each row is normalized by the summation of its values.
  265. rc_order : list, optional
  266. A list of the node attribute values. This list specifies the ordering
  267. of rows and columns of the array. If no ordering is provided, then
  268. the ordering will be random (and also, a return value).
  269. Other Parameters
  270. ----------------
  271. dtype : NumPy data-type, optional
  272. A valid NumPy dtype used to initialize the array. Keep in mind certain
  273. dtypes can yield unexpected results if the array is to be normalized.
  274. The parameter is passed to numpy.zeros(). If unspecified, the NumPy
  275. default is used.
  276. Returns
  277. -------
  278. M : SciPy sparse array
  279. The attribute matrix.
  280. ordering : list
  281. If `rc_order` was specified, then only the matrix is returned.
  282. However, if `rc_order` was None, then the ordering used to construct
  283. the matrix is returned as well.
  284. Examples
  285. --------
  286. Construct an adjacency matrix:
  287. >>> G = nx.Graph()
  288. >>> G.add_edge(0, 1, thickness=1, weight=3)
  289. >>> G.add_edge(0, 2, thickness=2)
  290. >>> G.add_edge(1, 2, thickness=3)
  291. >>> M = nx.attr_sparse_matrix(G, rc_order=[0, 1, 2])
  292. >>> M.toarray()
  293. array([[0., 1., 1.],
  294. [1., 0., 1.],
  295. [1., 1., 0.]])
  296. Alternatively, we can obtain the matrix describing edge thickness.
  297. >>> M = nx.attr_sparse_matrix(G, edge_attr="thickness", rc_order=[0, 1, 2])
  298. >>> M.toarray()
  299. array([[0., 1., 2.],
  300. [1., 0., 3.],
  301. [2., 3., 0.]])
  302. We can also color the nodes and ask for the probability distribution over
  303. all edges (u,v) describing:
  304. Pr(v has color Y | u has color X)
  305. >>> G.nodes[0]["color"] = "red"
  306. >>> G.nodes[1]["color"] = "red"
  307. >>> G.nodes[2]["color"] = "blue"
  308. >>> rc = ["red", "blue"]
  309. >>> M = nx.attr_sparse_matrix(G, node_attr="color", normalized=True, rc_order=rc)
  310. >>> M.toarray()
  311. array([[0.33333333, 0.66666667],
  312. [1. , 0. ]])
  313. For example, the above tells us that for all edges (u,v):
  314. Pr( v is red | u is red) = 1/3
  315. Pr( v is blue | u is red) = 2/3
  316. Pr( v is red | u is blue) = 1
  317. Pr( v is blue | u is blue) = 0
  318. Finally, we can obtain the total weights listed by the node colors.
  319. >>> M = nx.attr_sparse_matrix(G, edge_attr="weight", node_attr="color", rc_order=rc)
  320. >>> M.toarray()
  321. array([[3., 2.],
  322. [2., 0.]])
  323. Thus, the total weight over all edges (u,v) with u and v having colors:
  324. (red, red) is 3 # the sole contribution is from edge (0,1)
  325. (red, blue) is 2 # contributions from edges (0,2) and (1,2)
  326. (blue, red) is 2 # same as (red, blue) since graph is undirected
  327. (blue, blue) is 0 # there are no edges with blue endpoints
  328. """
  329. import numpy as np
  330. import scipy as sp
  331. import scipy.sparse # call as sp.sparse
  332. edge_value = _edge_value(G, edge_attr)
  333. node_value = _node_value(G, node_attr)
  334. if rc_order is None:
  335. ordering = list({node_value(n) for n in G})
  336. else:
  337. ordering = rc_order
  338. N = len(ordering)
  339. undirected = not G.is_directed()
  340. index = dict(zip(ordering, range(N)))
  341. M = sp.sparse.lil_array((N, N), dtype=dtype)
  342. seen = set()
  343. for u, nbrdict in G.adjacency():
  344. for v in nbrdict:
  345. # Obtain the node attribute values.
  346. i, j = index[node_value(u)], index[node_value(v)]
  347. if v not in seen:
  348. M[i, j] += edge_value(u, v)
  349. if undirected:
  350. M[j, i] = M[i, j]
  351. if undirected:
  352. seen.add(u)
  353. if normalized:
  354. M *= 1 / M.sum(axis=1)[:, np.newaxis] # in-place mult preserves sparse
  355. if rc_order is None:
  356. return M, ordering
  357. else:
  358. return M