attrmatrix.py 16 KB

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