o
    ˷e9                     @   s   d Z ddlmZ ddlZddlmZ ddlmZ ddlm	Z	 ddl
mZ g dZG d	d
 d
eZdd ZeedZdd Zdd Zdd Ze	dedd Zedd ZdddZedd ZdS ) zHFunctions for measuring the quality of a partition (into
communities).

    )combinationsN)NetworkXError)is_partition)not_implemented_for)argmap)coverage
modularityperformancepartition_qualityc                       s    e Zd ZdZ fddZ  ZS )NotAPartitionz0Raised if a given collection is not a partition.c                    s   | d| }t  | d S )Nz' is not a valid partition of the graph )super__init__)selfG
collectionmsg	__class__ \/var/www/ideatree/venv/lib/python3.10/site-packages/networkx/algorithms/community/quality.pyr      s   zNotAPartition.__init__)__name__
__module____qualname____doc__r   __classcell__r   r   r   r   r      s    r   c                 C   s   t | |r	| |fS td)a  Decorator to check that a valid partition is input to a function

    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)
        Traceback (most recent call last):
          ...
        networkx.exception.NetworkXError: `partition` is not a valid partition of the nodes of G
        >>> partition = [{0, 1}, {1, 2, 3}, {4}]
        >>> foo(G, partition)
        Traceback (most recent call last):
          ...
        networkx.exception.NetworkXError: `partition` is not a valid partition of the nodes of G

    z6`partition` is not a valid partition of the nodes of G)r   nxr   r   	partitionr   r   r   _require_partition   s   

r   )r      c                    s   t  fdd|D S )aR  Returns the number of intra-community edges for a partition of `G`.

    Parameters
    ----------
    G : NetworkX graph.

    partition : iterable of sets of nodes
        This 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.

    c                 3   s    | ]
}  | V  qd S N)subgraphsize).0blockr   r   r   	<genexpr>L   s    z(intra_community_edges.<locals>.<genexpr>)sumr   r   r%   r   intra_community_edges>   s   r(   c                 C   s(   |   rtjntj}tj| ||d S )a  Returns the number of inter-community edges for a partition of `G`.
    according to the given
    partition of the nodes of `G`.

    Parameters
    ----------
    G : NetworkX graph.

    partition : iterable of sets of nodes
        This 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 that of `G`.

    )create_using)is_directedr   MultiDiGraph
MultiGraphquotient_graphr"   )r   r   MGr   r   r   inter_community_edgesO   s   r/   c                 C   s   t t| |S )a  Returns the number of inter-community non-edges according to the
    given partition of the nodes of `G`.

    Parameters
    ----------
    G : NetworkX graph.

    partition : iterable of sets of nodes
        This 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/   r   
complementr   r   r   r   inter_community_non_edgesn   s   r1   
multigraphc                 C   sD   t | |}t| |}t| }||d  }|  s|d }|| | S )a  Returns the performance of a partition.

    .. deprecated:: 2.6
       Use `partition_quality` instead.

    The *performance* of a partition is the number of
    intra-community edges plus inter-community non-edges divided by 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
           <https://arxiv.org/abs/0906.0612>

    r      )r(   r1   lenr*   )r   r   intra_edgesinter_edgesntotal_pairsr   r   r   r	      s   
*
r	   c                 C   s   t | |}|  }|| S )a  Returns the coverage of a partition.

    .. deprecated:: 2.6
       Use `partition_quality` instead.

    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
           <https://arxiv.org/abs/0906.0612>

    )r(   number_of_edges)r   r   r5   total_edgesr   r   r   r      s   
)r   weightr   c                    s   t |ts	t|}t |st |  r6t jdt jdt	 dd  nt j
d t	 }|d d|d   fdd}tt||S )ax  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} - \gamma\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$, $\gamma$ is the resolution parameter,
    and $\delta(c_i, c_j)$ is 1 if $i$ and $j$ are in the same community else 0.

    According to [2]_ (and verified by some algebra) this can be reduced to

    .. math::
       Q = \sum_{c=1}^{n}
       \left[ \frac{L_c}{m} - \gamma\left( \frac{k_c}{2m} \right) ^2 \right]

    where the sum iterates over all communities $c$, $m$ is the number of edges,
    $L_c$ is the number of intra-community links for community $c$,
    $k_c$ is the sum of degrees of the nodes in community $c$,
    and $\gamma$ is the resolution parameter.

    The resolution parameter sets an arbitrary tradeoff between intra-group
    edges and inter-group edges. More complex grouping patterns can be
    discovered by analyzing the same network with multiple values of gamma
    and then combining the results [3]_. That said, it is very common to
    simply use gamma=1. More on the choice of gamma is in [4]_.

    The second formula is the one actually used in calculation of the modularity.
    For directed graphs the second formula replaces $k_c$ with $k^{in}_c k^{out}_c$.

    Parameters
    ----------
    G : NetworkX Graph

    communities : list or iterable of set of nodes
        These node sets must represent a partition of G's nodes.

    weight : string or None, optional (default="weight")
        The edge attribute that holds the numerical value used
        as a weight. If None or an edge does not have that attribute,
        then that edge has weight 1.

    resolution : float (default=1)
        If resolution is less than 1, modularity favors larger communities.
        Greater than 1 favors smaller communities.

    Returns
    -------
    Q : float
        The modularity of the paritition.

    Raises
    ------
    NotAPartition
        If `communities` is not a partition of the nodes of `G`.

    Examples
    --------
    >>> import networkx.algorithms.community as nx_comm
    >>> G = nx.barbell_graph(3, 0)
    >>> nx_comm.modularity(G, [{0, 1, 2}, {3, 4, 5}])
    0.35714285714285715
    >>> nx_comm.modularity(G, nx_comm.label_propagation_communities(G))
    0.35714285714285715

    References
    ----------
    .. [1] M. E. J. Newman "Networks: An Introduction", page 224.
       Oxford University Press, 2011.
    .. [2] Clauset, Aaron, Mark EJ Newman, and Cristopher Moore.
       "Finding community structure in very large networks."
       Phys. Rev. E 70.6 (2004). <https://arxiv.org/abs/cond-mat/0408187>
    .. [3] Reichardt and Bornholdt "Statistical Mechanics of Community Detection"
       Phys. Rev. E 74, 016110, 2006. https://doi.org/10.1103/PhysRevE.74.016110
    .. [4] M. E. J. Newman, "Equivalence between modularity optimization and
       maximum likelihood methods for community detection"
       Phys. Rev. E 94, 052315, 2016. https://doi.org/10.1103/PhysRevE.94.052315

    )r;   r   r3   c                    sv   t |  t fddj ddD }tfdd D }r-tfdd D n|}| | |   S )Nc                 3   s"    | ]\}}}| v r|V  qd S r    r   )r#   uvwtcommr   r   r&   Z  s     z=modularity.<locals>.community_contribution.<locals>.<genexpr>r   )datadefaultc                 3       | ]} | V  qd S r    r   r#   r<   )
out_degreer   r   r&   \      c                 3   rC   r    r   rD   )	in_degreer   r   r&   ]  rF   )setr'   edges)	communityL_cout_degree_sumin_degree_sumr   directedrG   mnormrE   
resolutionr;   r?   r   community_contributionX  s
   "z*modularity.<locals>.community_contribution)
isinstancelistr   r   r*   dictrE   rG   r'   valuesdegreemap)r   communitiesr;   rR   deg_sumrS   r   rN   r   r      s    
R

	r   c                 C   s   i }t |D ]\}}|D ]}|||< qq|  s-tdd t|dD }|  r,|d9 }nd}t| }||d  }|  sA|d }d}	|}
|  D ]}||d  ||d  kr\|	d7 }	qI|
d8 }
qI|	t| j }|  rrd}||fS |	|
 | }||fS )aS  Returns the coverage and performance of a partition of G.

    The *coverage* of a partition is the ratio of the number of
    intra-community edges to the total number of edges in the graph.

    The *performance* of a partition is the number of
    intra-community edges plus inter-community non-edges divided by the total
    number of potential edges.

    This algorithm has complexity $O(C^2 + L)$ where C is the number of communities and L is the number of links.

    Parameters
    ----------
    G : NetworkX graph

    partition : sequence
        Partition of the nodes of `G`, represented as a sequence of
        sets of nodes (blocks). Each block of the partition represents a
        community.

    Returns
    -------
    (float, float)
        The (coverage, performance) tuple 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;
        - for coverage, the multiplicity of edges is counted
        - for performance, the result is -1 (total number of possible edges is not defined)

    References
    ----------
    .. [1] Santo Fortunato.
           "Community Detection in Graphs".
           *Physical Reports*, Volume 486, Issue 3--5 pp. 75--174
           <https://arxiv.org/abs/0906.0612>
    c                 s   s$    | ]\}}t |t | V  qd S r    )r4   )r#   p1p2r   r   r   r&     s    
z$partition_quality.<locals>.<genexpr>r3   r   r   g      )	enumerateis_multigraphr'   r   r*   r4   rI   )r   r   node_communityirJ   nodepossible_inter_community_edgesr7   r8   r(   r1   er   r	   r   r   r   r
   d  s:   .


r
   )r;   r   )r   	itertoolsr   networkxr   r   -networkx.algorithms.community.community_utilsr   networkx.utilsr   networkx.utils.decoratorsr   __all__r   r   require_partitionr(   r/   r1   r	   r   r   r
   r   r   r   r   <module>   s,    
" 7

-o