§
    bŠtjÐ  ã                   ó.  — 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	dS )zTrophic levelsé    N)Únot_implemented_for)Útrophic_levelsÚtrophic_differencesÚtrophic_incoherence_parameterÚ
undirectedÚweight)Ú
edge_attrsc                 óÌ  — d„ | j         D ¦   «         }|st          j        d¦  «        ‚d„ t          j        | |¬¦  «        D ¦   «         }t	          |¦  «        t	          | j        ¦  «        k    rt          j        d¦  «        ‚ddl}t          j        | |¬¦  «        j         	                    ¦   «         }| 
                    |d	¬
¦  «        }||dk             dd…|dk    f         }|||dk             dd…|j        f         z  }|j        d         }|                     |¦  «        }		 |j                             |	|z
  ¦  «        }
n.# |j        j        $ r}d}t          j        |¦  «        |‚d}~ww xY w|
 
                    d	¬
¦  «        d	z   }i }d„ | j         D ¦   «         }|D ]}d	||<   Œd„ | j         D ¦   «         }t#          |¦  «        D ]\  }	}|                     |	¦  «        ||<   Œ|S )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
    c                 ó$   — g | ]\  }}|d k    ¯|‘ŒS )r   © )Ú.0ÚnÚdegs      úd/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/networkx/algorithms/centrality/trophic.pyú
<listcomp>z"trophic_levels.<locals>.<listcomp>-   s!   € Ð;Ð;Ð;™˜˜C°#¸²(°(�1°(°(°(ó    z|This graph has no basal nodes (nodes with no incoming edges).Trophic levels are not defined without at least one basal node.c                 ó   — h | ]	}|D ]}|’ŒŒ
S r   r   )r   ÚlayerÚnodes      r   ú	<setcomp>z!trophic_levels.<locals>.<setcomp>4   s9   € ð ð ð ØÈEðð ØDHˆðð ð ð r   )Úsourceszˆ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).r   N©r   é   )Úaxisc              3   ó,   K  — | ]\  }}|d k    ¯|V — ŒdS ©r   Nr   ©r   Únode_idÚdegrees      r   ú	<genexpr>z!trophic_levels.<locals>.<genexpr>[   s*   è è € ÐOÐO¡ ¨&À6ÈQÂ;À;�WÀ;À;À;À;ÐOÐOr   c              3   ó,   K  — | ]\  }}|d k    ¯|V — ŒdS r   r   r   s      r   r    z!trophic_levels.<locals>.<genexpr>`   s+   è è € ÐRÐR¡O G¨VÀfÐPQÂkÀk˜ÀkÀkÀkÀkÐRÐRr   )Ú	in_degreeÚnxÚNetworkXErrorÚ
bfs_layersÚlenÚnodesÚnumpyÚadjacency_matrixÚTÚtoarrayÚsumÚnewaxisÚshapeÚeyeÚlinalgÚinvÚLinAlgErrorÚ	enumerateÚitem)ÚGr   Úbasal_nodesÚreachable_nodesÚnpÚaÚrowsumÚpÚnnÚir   ÚerrÚmsgÚyÚlevelsÚzero_node_idsr   Únonzero_node_idss                     r   r   r   	   sH  € ðH <Ð; 1¤;Ð;Ñ;Ô;€KØð 
ÝÔðNñ
ô 
ð 	
ð
ð Ýœ-¨°;Ð?Ñ?Ô?ðñ ô €Oõ ˆ?ÑÔ�s 1¤7™|œ|Ò+Ð+ÝÔðPñ
ô 
ð 	
ð
 ÐÐÐõ 	Ô˜A fÐ-Ñ-Ô-Ô/×7Ò7Ñ9Ô9€Að �VŠV�A˜AˆVÑÔ€FØ	ˆ&�AŠ+Œ�q�q�q˜& Aš+�~Ô&€Aà	ˆF�6˜Q’;Ô    2¤: Ô.Ñ.€Að 
Œ�Œ€BØ
�Šˆr‰
Œ
€Að	-ØŒI�MŠM˜!˜a™%Ñ Ô ˆˆøØŒ9Ô ð -ð -ð -ð)ð 	õ
 Ô˜sÑ#Ô#¨Ð,øøøøð-øøøð 	
�Š�1ˆ‰Œ˜Ñ€Aà€Fð PÐO°A´KÐOÑOÔO€MØ ð ð ˆØˆˆw‰ˆð SÐR°q´{ÐRÑRÔRÐÝÐ 0Ñ1Ô1ð $ð $‰
ˆˆ7ØŸ&š& ™)œ)ˆˆw‰ˆà€Ms   Ä$E ÅE-ÅE(Å(E-c                 ór   — 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)r5   r   rA   ÚdiffsÚuÚvs         r   r   r   g   sQ   € õ8 ˜A fÐ-Ñ-Ô-€FØ€EØ”ð .ð .‰ˆˆ1Ø˜qœ	 F¨1¤IÑ-ˆˆq�!ˆf‰ˆØ€Lr   Fc                 ót  — ddl }|rt          | |¬¦  «        }n`t          t          j        | ¦  «        ¦  «        }|r*|                      ¦   «         }|                     |¦  «         n| }t          ||¬¦  «        }t          |                     t          | 	                    ¦   «         ¦  «        ¦  «        ¦  «        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)r5   r   Úcannibalismr8   rF   Ú
self_loopsÚG_2s          r   r   r   Š   sµ   € ð8 ÐÐÐàð 8Ý# A¨fÐ5Ñ5Ô5ˆˆõ �"Ô+¨AÑ.Ô.Ñ/Ô/ˆ
Øð 	à—&’&‘(”(ˆCØ×!Ò! *Ñ-Ô-Ð-Ð-ð ˆCÝ# C°Ð7Ñ7Ô7ˆÝ�—’�˜UŸ\š\™^œ^Ñ,Ô,Ñ-Ô-Ñ.Ô.Ð.r   r   )r   F)
Ú__doc__Únetworkxr#   Únetworkx.utilsr   Ú__all__Ú_dispatchabler   r   r   r   r   r   ú<module>rY      s  ðØ Ð à Ð Ð Ð Ø .Ð .Ð .Ð .Ð .Ð .à
TÐ
TÐ
T€ð Ð�\Ñ"Ô"Ø€Ô˜XÐ&Ñ&Ô&ðYð Yð Yñ 'Ô&ñ #Ô"ðYðx Ð�\Ñ"Ô"Ø€Ô˜XÐ&Ñ&Ô&ðð ð ñ 'Ô&ñ #Ô"ððB Ð�\Ñ"Ô"Ø€Ô˜XÐ&Ñ&Ô&ð)/ð )/ð )/ñ 'Ô&ñ #Ô"ð)/ð )/ð )/r   