quality.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. """Functions for measuring the quality of a partition (into
  2. communities).
  3. """
  4. from itertools import combinations
  5. import networkx as nx
  6. from networkx import NetworkXError
  7. from networkx.algorithms.community.community_utils import is_partition
  8. from networkx.utils import not_implemented_for
  9. from networkx.utils.decorators import argmap
  10. __all__ = ["modularity", "partition_quality"]
  11. class NotAPartition(NetworkXError):
  12. """Raised if a given collection is not a partition."""
  13. def __init__(self, G, collection):
  14. msg = f"{collection} is not a valid partition of the graph {G}"
  15. super().__init__(msg)
  16. def _require_partition(G, partition):
  17. """Decorator to check that a valid partition is input to a function
  18. Raises :exc:`networkx.NetworkXError` if the partition is not valid.
  19. This decorator should be used on functions whose first two arguments
  20. are a graph and a partition of the nodes of that graph (in that
  21. order)::
  22. >>> @require_partition
  23. ... def foo(G, partition):
  24. ... print("partition is valid!")
  25. ...
  26. >>> G = nx.complete_graph(5)
  27. >>> partition = [{0, 1}, {2, 3}, {4}]
  28. >>> foo(G, partition)
  29. partition is valid!
  30. >>> partition = [{0}, {2, 3}, {4}]
  31. >>> foo(G, partition)
  32. Traceback (most recent call last):
  33. ...
  34. networkx.exception.NetworkXError: `partition` is not a valid partition of the nodes of G
  35. >>> partition = [{0, 1}, {1, 2, 3}, {4}]
  36. >>> foo(G, partition)
  37. Traceback (most recent call last):
  38. ...
  39. networkx.exception.NetworkXError: `partition` is not a valid partition of the nodes of G
  40. """
  41. if is_partition(G, partition):
  42. return G, partition
  43. raise nx.NetworkXError("`partition` is not a valid partition of the nodes of G")
  44. require_partition = argmap(_require_partition, (0, 1))
  45. @nx._dispatch
  46. def intra_community_edges(G, partition):
  47. """Returns the number of intra-community edges for a partition of `G`.
  48. Parameters
  49. ----------
  50. G : NetworkX graph.
  51. partition : iterable of sets of nodes
  52. This must be a partition of the nodes of `G`.
  53. The "intra-community edges" are those edges joining a pair of nodes
  54. in the same block of the partition.
  55. """
  56. return sum(G.subgraph(block).size() for block in partition)
  57. @nx._dispatch
  58. def inter_community_edges(G, partition):
  59. """Returns the number of inter-community edges for a partition of `G`.
  60. according to the given
  61. partition of the nodes of `G`.
  62. Parameters
  63. ----------
  64. G : NetworkX graph.
  65. partition : iterable of sets of nodes
  66. This must be a partition of the nodes of `G`.
  67. The *inter-community edges* are those edges joining a pair of nodes
  68. in different blocks of the partition.
  69. Implementation note: this function creates an intermediate graph
  70. that may require the same amount of memory as that of `G`.
  71. """
  72. # Alternate implementation that does not require constructing a new
  73. # graph object (but does require constructing an affiliation
  74. # dictionary):
  75. #
  76. # aff = dict(chain.from_iterable(((v, block) for v in block)
  77. # for block in partition))
  78. # return sum(1 for u, v in G.edges() if aff[u] != aff[v])
  79. #
  80. MG = nx.MultiDiGraph if G.is_directed() else nx.MultiGraph
  81. return nx.quotient_graph(G, partition, create_using=MG).size()
  82. def inter_community_non_edges(G, partition):
  83. """Returns the number of inter-community non-edges according to the
  84. given partition of the nodes of `G`.
  85. Parameters
  86. ----------
  87. G : NetworkX graph.
  88. partition : iterable of sets of nodes
  89. This must be a partition of the nodes of `G`.
  90. A *non-edge* is a pair of nodes (undirected if `G` is undirected)
  91. that are not adjacent in `G`. The *inter-community non-edges* are
  92. those non-edges on a pair of nodes in different blocks of the
  93. partition.
  94. Implementation note: this function creates two intermediate graphs,
  95. which may require up to twice the amount of memory as required to
  96. store `G`.
  97. """
  98. # Alternate implementation that does not require constructing two
  99. # new graph objects (but does require constructing an affiliation
  100. # dictionary):
  101. #
  102. # aff = dict(chain.from_iterable(((v, block) for v in block)
  103. # for block in partition))
  104. # return sum(1 for u, v in nx.non_edges(G) if aff[u] != aff[v])
  105. #
  106. return inter_community_edges(nx.complement(G), partition)
  107. def modularity(G, communities, weight="weight", resolution=1):
  108. r"""Returns the modularity of the given partition of the graph.
  109. Modularity is defined in [1]_ as
  110. .. math::
  111. Q = \frac{1}{2m} \sum_{ij} \left( A_{ij} - \gamma\frac{k_ik_j}{2m}\right)
  112. \delta(c_i,c_j)
  113. where $m$ is the number of edges, $A$ is the adjacency matrix of `G`,
  114. $k_i$ is the degree of $i$, $\gamma$ is the resolution parameter,
  115. and $\delta(c_i, c_j)$ is 1 if $i$ and $j$ are in the same community else 0.
  116. According to [2]_ (and verified by some algebra) this can be reduced to
  117. .. math::
  118. Q = \sum_{c=1}^{n}
  119. \left[ \frac{L_c}{m} - \gamma\left( \frac{k_c}{2m} \right) ^2 \right]
  120. where the sum iterates over all communities $c$, $m$ is the number of edges,
  121. $L_c$ is the number of intra-community links for community $c$,
  122. $k_c$ is the sum of degrees of the nodes in community $c$,
  123. and $\gamma$ is the resolution parameter.
  124. The resolution parameter sets an arbitrary tradeoff between intra-group
  125. edges and inter-group edges. More complex grouping patterns can be
  126. discovered by analyzing the same network with multiple values of gamma
  127. and then combining the results [3]_. That said, it is very common to
  128. simply use gamma=1. More on the choice of gamma is in [4]_.
  129. The second formula is the one actually used in calculation of the modularity.
  130. For directed graphs the second formula replaces $k_c$ with $k^{in}_c k^{out}_c$.
  131. Parameters
  132. ----------
  133. G : NetworkX Graph
  134. communities : list or iterable of set of nodes
  135. These node sets must represent a partition of G's nodes.
  136. weight : string or None, optional (default="weight")
  137. The edge attribute that holds the numerical value used
  138. as a weight. If None or an edge does not have that attribute,
  139. then that edge has weight 1.
  140. resolution : float (default=1)
  141. If resolution is less than 1, modularity favors larger communities.
  142. Greater than 1 favors smaller communities.
  143. Returns
  144. -------
  145. Q : float
  146. The modularity of the partition.
  147. Raises
  148. ------
  149. NotAPartition
  150. If `communities` is not a partition of the nodes of `G`.
  151. Examples
  152. --------
  153. >>> G = nx.barbell_graph(3, 0)
  154. >>> nx.community.modularity(G, [{0, 1, 2}, {3, 4, 5}])
  155. 0.35714285714285715
  156. >>> nx.community.modularity(G, nx.community.label_propagation_communities(G))
  157. 0.35714285714285715
  158. References
  159. ----------
  160. .. [1] M. E. J. Newman "Networks: An Introduction", page 224.
  161. Oxford University Press, 2011.
  162. .. [2] Clauset, Aaron, Mark EJ Newman, and Cristopher Moore.
  163. "Finding community structure in very large networks."
  164. Phys. Rev. E 70.6 (2004). <https://arxiv.org/abs/cond-mat/0408187>
  165. .. [3] Reichardt and Bornholdt "Statistical Mechanics of Community Detection"
  166. Phys. Rev. E 74, 016110, 2006. https://doi.org/10.1103/PhysRevE.74.016110
  167. .. [4] M. E. J. Newman, "Equivalence between modularity optimization and
  168. maximum likelihood methods for community detection"
  169. Phys. Rev. E 94, 052315, 2016. https://doi.org/10.1103/PhysRevE.94.052315
  170. """
  171. if not isinstance(communities, list):
  172. communities = list(communities)
  173. if not is_partition(G, communities):
  174. raise NotAPartition(G, communities)
  175. directed = G.is_directed()
  176. if directed:
  177. out_degree = dict(G.out_degree(weight=weight))
  178. in_degree = dict(G.in_degree(weight=weight))
  179. m = sum(out_degree.values())
  180. norm = 1 / m**2
  181. else:
  182. out_degree = in_degree = dict(G.degree(weight=weight))
  183. deg_sum = sum(out_degree.values())
  184. m = deg_sum / 2
  185. norm = 1 / deg_sum**2
  186. def community_contribution(community):
  187. comm = set(community)
  188. L_c = sum(wt for u, v, wt in G.edges(comm, data=weight, default=1) if v in comm)
  189. out_degree_sum = sum(out_degree[u] for u in comm)
  190. in_degree_sum = sum(in_degree[u] for u in comm) if directed else out_degree_sum
  191. return L_c / m - resolution * out_degree_sum * in_degree_sum * norm
  192. return sum(map(community_contribution, communities))
  193. @require_partition
  194. def partition_quality(G, partition):
  195. """Returns the coverage and performance of a partition of G.
  196. The *coverage* of a partition is the ratio of the number of
  197. intra-community edges to the total number of edges in the graph.
  198. The *performance* of a partition is the number of
  199. intra-community edges plus inter-community non-edges divided by the total
  200. number of potential edges.
  201. This algorithm has complexity $O(C^2 + L)$ where C is the number of communities and L is the number of links.
  202. Parameters
  203. ----------
  204. G : NetworkX graph
  205. partition : sequence
  206. Partition of the nodes of `G`, represented as a sequence of
  207. sets of nodes (blocks). Each block of the partition represents a
  208. community.
  209. Returns
  210. -------
  211. (float, float)
  212. The (coverage, performance) tuple of the partition, as defined above.
  213. Raises
  214. ------
  215. NetworkXError
  216. If `partition` is not a valid partition of the nodes of `G`.
  217. Notes
  218. -----
  219. If `G` is a multigraph;
  220. - for coverage, the multiplicity of edges is counted
  221. - for performance, the result is -1 (total number of possible edges is not defined)
  222. References
  223. ----------
  224. .. [1] Santo Fortunato.
  225. "Community Detection in Graphs".
  226. *Physical Reports*, Volume 486, Issue 3--5 pp. 75--174
  227. <https://arxiv.org/abs/0906.0612>
  228. """
  229. node_community = {}
  230. for i, community in enumerate(partition):
  231. for node in community:
  232. node_community[node] = i
  233. # `performance` is not defined for multigraphs
  234. if not G.is_multigraph():
  235. # Iterate over the communities, quadratic, to calculate `possible_inter_community_edges`
  236. possible_inter_community_edges = sum(
  237. len(p1) * len(p2) for p1, p2 in combinations(partition, 2)
  238. )
  239. if G.is_directed():
  240. possible_inter_community_edges *= 2
  241. else:
  242. possible_inter_community_edges = 0
  243. # Compute the number of edges in the complete graph -- `n` nodes,
  244. # directed or undirected, depending on `G`
  245. n = len(G)
  246. total_pairs = n * (n - 1)
  247. if not G.is_directed():
  248. total_pairs //= 2
  249. intra_community_edges = 0
  250. inter_community_non_edges = possible_inter_community_edges
  251. # Iterate over the links to count `intra_community_edges` and `inter_community_non_edges`
  252. for e in G.edges():
  253. if node_community[e[0]] == node_community[e[1]]:
  254. intra_community_edges += 1
  255. else:
  256. inter_community_non_edges -= 1
  257. coverage = intra_community_edges / len(G.edges)
  258. if G.is_multigraph():
  259. performance = -1.0
  260. else:
  261. performance = (intra_community_edges + inter_community_non_edges) / total_pairs
  262. return coverage, performance