Ë
    D^(hG  ã                   ó  — d Z ddlZddlmZ g d¢Z ed«       ej                  d¬«      dd„«       «       Z ed«       ej                  d¬«      dd	„«       «       Z ed«       ej                  d¬«      dd
„«       «       Z	y)zTrophic levelsé    N)Únot_implemented_for)Útrophic_levelsÚtrophic_differencesÚtrophic_incoherence_parameterÚ
undirectedÚweight)Ú
edge_attrsc                 ó¾  — ddl }t        j                  | |¬«      j                  j	                  «       }|j                  |d¬«      }||dk7     dd…|dk7  f   }|||dk7     dd…|j                  f   z  }|j                  d   }|j                  |«      }	 |j                  j                  ||z
  «      }|j                  d¬«      dz   }i }d„ | j                  D «       }|D ]  }d||<   Œ	 d„ | j                  D «       }t        |«      D ]  \  }}|j                  |«      ||<   Œ |S # |j                  j                  $ r}	d}
t        j                  |
«      |	‚d}	~	ww xY w)	a’  Compute the trophic levels of nodes.

    The trophic level of a node $i$ is

    .. math::

        s_i = 1 + \frac{1}{k^{in}_i} \sum_{j} a_{ij} s_j

    where $k^{in}_i$ is the in-degree of i

    .. math::

        k^{in}_i = \sum_{j} a_{ij}

    and nodes with $k^{in}_i = 0$ have $s_i = 1$ by convention.

    These are calculated using the method outlined in Levine [1]_.

    Parameters
    ----------
    G : DiGraph
        A directed networkx graph

    Returns
    -------
    nodes : dict
        Dictionary of nodes with trophic level as the value.

    References
    ----------
    .. [1] Stephen Levine (1980) J. theor. Biol. 83, 195-207
    r   N©r   é   )ÚaxiszˆTrophic levels are only defined for graphs where every node has a path from a basal node (basal nodes are nodes with no incoming edges).c              3   ó2   K  — | ]  \  }}|d k(  sŒ|–— Œ y­w©r   N© ©Ú.0Únode_idÚdegrees      úd/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/networkx/algorithms/centrality/trophic.pyú	<genexpr>z!trophic_levels.<locals>.<genexpr>I   s   è ø€ ÒO¡ ¨&À6ÈQÃ;”WÑOùó   ‚�c              3   ó2   K  — | ]  \  }}|d k7  sŒ|–— Œ y­wr   r   r   s      r   r   z!trophic_levels.<locals>.<genexpr>N   s   è ø€ ÒR¡O G¨VÀfÐPQÃkœÑRùr   )ÚnumpyÚnxÚadjacency_matrixÚTÚtoarrayÚsumÚnewaxisÚshapeÚeyeÚlinalgÚinvÚLinAlgErrorÚNetworkXErrorÚ	in_degreeÚ	enumerateÚitem)ÚGr   ÚnpÚaÚrowsumÚpÚnnÚiÚnÚerrÚmsgÚyÚlevelsÚzero_node_idsr   Únonzero_node_idss                   r   r   r   	   sr  € óF ô 	×Ñ˜A fÔ-×/Ñ/×7Ñ7Ó9€Að �V‰V�A˜AˆVÓ€FØ	ˆ&�A‰+‰’q˜& A™+�~Ñ&€Aà	ˆF�6˜Q‘;Ñ¢ 2§:¡: Ñ.Ñ.€Að 
�‰�‰€BØ
�‰ˆr‹
€Að	-Ø�I‰I�M‰M˜!˜a™%Ó ˆð 	
�‰�1ˆ‹˜Ñ€Aà€Fñ P°A·K±KÔO€MØ ò ˆØˆˆwŠðñ S°q·{±{ÔRÐÜÐ 0Ó1ò $‰
ˆˆ7ØŸ&™& ›)ˆˆwŠð$ð €Møð- �9‰9× Ñ ò -ð)ð 	ô
 ×Ñ˜sÓ#¨Ð,ûð-ús   ÂD& Ä&EÄ?EÅEc                 ól   — t        | |¬«      }i }| j                  D ]  \  }}||   ||   z
  |||f<   Œ |S )as  Compute the trophic differences of the edges of a directed graph.

    The trophic difference $x_ij$ for each edge is defined in Johnson et al.
    [1]_ as:

    .. math::
        x_ij = s_j - s_i

    Where $s_i$ is the trophic level of node $i$.

    Parameters
    ----------
    G : DiGraph
        A directed networkx graph

    Returns
    -------
    diffs : dict
        Dictionary of edges with trophic differences as the value.

    References
    ----------
    .. [1] Samuel Johnson, Virginia Dominguez-Garcia, Luca Donetti, Miguel A.
        Munoz (2014) PNAS "Trophic coherence determines food-web stability"
    r   )r   Úedges)r)   r   r4   ÚdiffsÚuÚvs         r   r   r   U   sK   € ô8 ˜A fÔ-€FØ€EØ—‘ò .‰ˆˆ1Ø˜q™	 F¨1¡IÑ-ˆˆq�!ˆfŠð.à€Ló    c                 ó.  — ddl }|rt        | |¬«      }nQt        t        j                  | «      «      }|r"| j                  «       }|j                  |«       n| }t        ||¬«      }t        |j                  t        |j                  «       «      «      «      S )a+  Compute the trophic incoherence parameter of a graph.

    Trophic coherence is defined as the homogeneity of the distribution of
    trophic distances: the more similar, the more coherent. This is measured by
    the standard deviation of the trophic differences and referred to as the
    trophic incoherence parameter $q$ by [1].

    Parameters
    ----------
    G : DiGraph
        A directed networkx graph

    cannibalism: Boolean
        If set to False, self edges are not considered in the calculation

    Returns
    -------
    trophic_incoherence_parameter : float
        The trophic coherence of a graph

    References
    ----------
    .. [1] Samuel Johnson, Virginia Dominguez-Garcia, Luca Donetti, Miguel A.
        Munoz (2014) PNAS "Trophic coherence determines food-web stability"
    r   Nr   )
r   r   Úlistr   Úselfloop_edgesÚcopyÚremove_edges_fromÚfloatÚstdÚvalues)r)   r   Úcannibalismr*   r9   Ú
self_loopsÚG_2s          r   r   r   x   s{   € ó8 áÜ# A¨fÔ5‰ô œ"×+Ñ+¨AÓ.Ó/ˆ
Ùà—&‘&“(ˆCØ×!Ñ! *Õ-ð ˆCÜ# C°Ô7ˆÜ�—‘œ˜UŸ\™\›^Ó,Ó-Ó.Ð.r<   r   )r   F)
Ú__doc__Únetworkxr   Únetworkx.utilsr   Ú__all__Ú_dispatchabler   r   r   r   r<   r   ú<module>rM      s¡   ðÙ ã Ý .â
T€ñ �\Ó"Ø€×Ñ˜XÔ&òGó 'ó #ðGñT �\Ó"Ø€×Ñ˜XÔ&òó 'ó #ðñB �\Ó"Ø€×Ñ˜XÔ&ò)/ó 'ó #ñ)/r<   