ó žÃÒYc@ sõdZddlmZddlmZddlmZddlZddlm Z ddl m Z ddl m Z d d d gZd e fd „ƒYZd„Zd„Zd„Zd„Ze dƒed„ƒƒZed„ƒZdd„ZdS(sHFunctions for measuring the quality of a partition (into communities). iÿÿÿÿ(tdivision(twraps(tproductN(t NetworkXError(tnot_implemented_for(t is_partitiontcoveraget modularityt performancet NotAPartitioncB seZdZd„ZRS(s6Raised if a given collection is not a partition. cC s2d}|j||ƒ}tt|ƒj|ƒdS(Ns+{} is not a valid partition of the graph {}(tformattsuperR t__init__(tselftGt collectiontmsg((s{/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/community/quality.pyR s(t__name__t __module__t__doc__R (((s{/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/community/quality.pyR sc stˆƒ‡fd†ƒ}|S(sJDecorator that raises an exception if a partition is not a valid partition of the nodes of a graph. Raises :exc:`networkx.NetworkXError` if the partition is not valid. This decorator should be used on functions whose first two arguments are a graph and a partition of the nodes of that graph (in that order):: >>> @require_partition ... def foo(G, partition): ... print('partition is valid!') ... >>> G = nx.complete_graph(5) >>> partition = [{0, 1}, {2, 3}, {4}] >>> foo(G, partition) partition is valid! >>> partition = [{0}, {2, 3}, {4}] >>> foo(G, partition) # doctest: +IGNORE_EXCEPTION_DETAIL Traceback (most recent call last): ... NetworkXError: `partition` is not a valid partition of the nodes of G >>> partition = [{0, 1}, {1, 2, 3}, {4}] >>> foo(G, partition) # doctest: +IGNORE_EXCEPTION_DETAIL Traceback (most recent call last): ... NetworkXError: `partition` is not a valid partition of the nodes of G c s/t|d Œs"tjdƒ‚nˆ||ŽS(Nis6`partition` is not a valid partition of the nodes of G(RtnxR(targstkw(tfunc(s{/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/community/quality.pytnew_funcDs(R(RR((Rs{/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/community/quality.pytrequire_partition%sc st‡fd†|DƒƒS(s9Returns the number of intra-community edges according to the given partition of the nodes of `G`. `G` must be a NetworkX graph. `partition` must be a partition of the nodes of `G`. The "intra-community edges" are those edges joining a pair of nodes in the same block of the partition. c3 s$|]}ˆj|ƒjƒVqdS(N(tsubgraphtsize(t.0tblock(R(s{/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/community/quality.pys Zs(tsum(Rt partition((Rs{/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/community/quality.pytintra_community_edgesNs cC s"tj||dtjƒƒjƒS(sÎReturns the number of inter-community edges according to the given partition of the nodes of `G`. `G` must be a NetworkX graph. `partition` must be a partition of the nodes of `G`. The *inter-community edges* are those edges joining a pair of nodes in different blocks of the partition. Implementation note: this function creates an intermediate graph that may require the same amount of memory as required to store `G`. t create_using(Rtquotient_grapht MultiGraphR(RR((s{/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/community/quality.pytinter_community_edges]scC sttj|ƒ|ƒS(sHReturns the number of inter-community non-edges according to the given partition of the nodes of `G`. `G` must be a NetworkX graph. `partition` must be a partition of the nodes of `G`. A *non-edge* is a pair of nodes (undirected if `G` is undirected) that are not adjacent in `G`. The *inter-community non-edges* are those non-edges on a pair of nodes in different blocks of the partition. Implementation note: this function creates two intermediate graphs, which may require up to twice the amount of memory as required to store `G`. (R$Rt complement(RR((s{/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/community/quality.pytinter_community_non_edgesxst multigraphcC s]t||ƒ}t||ƒ}t|ƒ}||d}|jƒsQ|d}n|||S(s”Returns the performance of a partition. The *performance* of a partition is the ratio of the number of intra-community edges plus inter-community non-edges with the total number of potential edges. Parameters ---------- G : NetworkX graph A simple graph (directed or undirected). partition : sequence Partition of the nodes of `G`, represented as a sequence of sets of nodes. Each block of the partition represents a community. Returns ------- float The performance of the partition, as defined above. Raises ------ NetworkXError If `partition` is not a valid partition of the nodes of `G`. References ---------- .. [1] Santo Fortunato. "Community Detection in Graphs". *Physical Reports*, Volume 486, Issue 3--5 pp. 75--174 ii(R R&tlent is_directed(RRt intra_edgest inter_edgestnt total_pairs((s{/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/community/quality.pyR•s(   cC s#t||ƒ}|jƒ}||S(sŽReturns the coverage of a partition. The *coverage* of a partition is the ratio of the number of intra-community edges to the total number of edges in the graph. Parameters ---------- G : NetworkX graph partition : sequence Partition of the nodes of `G`, represented as a sequence of sets of nodes. Each block of the partition represents a community. Returns ------- float The coverage of the partition, as defined above. Raises ------ NetworkXError If `partition` is not a valid partition of the nodes of `G`. Notes ----- If `G` is a multigraph, the multiplicity of edges is counted. References ---------- .. [1] Santo Fortunato. "Community Detection in Graphs". *Physical Reports*, Volume 486, Issue 3--5 pp. 75--174 (R tnumber_of_edges(RRR*t total_edges((s{/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/community/quality.pyRÌs& tweightc sÿtˆ|ƒs!tˆ|ƒ‚nˆjƒ‰ˆjƒ‰ˆjdˆƒ}ˆrŽtˆjdˆƒƒ‰tˆjdˆƒƒ‰d|‰n,tˆjdˆƒƒ‰ˆ‰dd|‰‡‡‡‡‡‡‡fd†‰t ‡fd†|Dƒƒ}|ˆS(s#Returns the modularity of the given partition of the graph. Modularity is defined in [1]_ as .. math:: Q = \frac{1}{2m} \sum_{ij} \left( A_{ij} - \frac{k_ik_j}{2m}\right) \delta(c_i,c_j) where $m$ is the number of edges, $A$ is the adjacency matrix of `G`, $k_i$ is the degree of $i$ and $\delta(c_i, c_j)$ is 1 if $i$ and $j$ are in the same community and 0 otherwise. Parameters ---------- G : NetworkX Graph communities : list List of sets of nodes of `G` representing a partition of the nodes. Returns ------- Q : float The modularity of the paritition. Raises ------ NotAPartition If `communities` is not a partition of the nodes of `G`. Examples -------- >>> G = nx.barbell_graph(3, 0) >>> nx.algorithms.community.modularity(G, [{0, 1, 2}, {3, 4, 5}]) 0.35714285714285704 References ---------- .. [1] M. E. J. Newman *Networks: An Introduction*, page 224. Oxford University Press, 2011. R0iic s£yQˆr6t‡fd†ˆ||jƒDƒƒ}nˆ||jˆdƒ}Wntk rjd}nX||kr‹ˆ r‹|d9}n|ˆ|ˆ|ˆS(Nc3 s'|]\}}|jˆdƒVqdS(iN(tget(Rtktd(R0(s{/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/community/quality.pys 5siii(RtitemsR1tKeyError(tutvtw(Rtdirectedt in_degreeR'tnormt out_degreeR0(s{/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/community/quality.pytval2s-   c3 s=|]3}t|ddƒD]\}}ˆ||ƒVqqdS(trepeatiN(R(RtcR6R7(R=(s{/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/community/quality.pys ?s( RR t is_multigraphR)RtdictR<R:tdegreeR(Rt communitiesR0tmtQ((RR9R:R'R;R<R=R0s{/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/community/quality.pyR÷s,   ! (Rt __future__Rt functoolsRt itertoolsRtnetworkxRRtnetworkx.utilsRt-networkx.algorithms.community.community_utilsRt__all__R RR R$R&RRR(((s{/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/community/quality.pyt s"  )    6+