ó
    …~iÐ  ã                   óö   • S r SSKrSSKJr  / SQr\" S5      \R                  " SS9SS j5       5       r\" S5      \R                  " SS9SS	 j5       5       r\" S5      \R                  " SS9SS
 j5       5       r	g)zTrophic levelsé    N)Únot_implemented_for)Útrophic_levelsÚtrophic_differencesÚtrophic_incoherence_parameterÚ
undirectedÚweight)Ú
edge_attrsc                 ó   • U R                    VVs/ s H  u  p#US:X  d  M  UPM     nnnU(       d  [        R                  " S5      e[        R                  " XS9 VVs1 s H  oU  H  ofiM     M     nnn[	        U5      [	        U R
                  5      :w  a  [        R                  " S5      eSSKn[        R                  " XS9R                  R                  5       n	UR                  U	SS9n
XšS:g     SS2U
S:g  4   nXºU
S:g     SS2UR                  4   -  nUR                  S   nUR                  U5      n UR                  R                  XÛ-
  5      nUR                  SS9S-   n0 nS	 U R                    5       nU H  nSUU'   M
     S
 U R                    5       n[#        U5       H  u  nnUR%                  U5      UU'   M     U$ s  snnf s  snnf ! UR                  R                    a  nSn[        R                  " U5      UeSnAff = f)aB  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   z|This graph has no basal nodes (nodes with no incoming edges).Trophic levels are not defined without at least one basal node.)Ú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).N©r   é   )Úaxisc              3   ó:   #   • U  H  u  pUS :X  d  M  Uv •  M     g7f©r   N© ©Ú.0Únode_idÚdegrees      Úc/home/mande/repo/quber/.venv/lib/python3.13/site-packages/networkx/algorithms/centrality/trophic.pyÚ	<genexpr>Ú!trophic_levels.<locals>.<genexpr>[   s   é € ÐO²K¡ À6ÈQÁ;—W‘W²Kùó   ‚’	c              3   ó:   #   • U  H  u  pUS :w  d  M  Uv •  M     g7fr   r   r   s      r   r   r   `   s   é € ÐR²{¡O GÀfÐPQÁkŸ™²{ùr   )Ú	in_degreeÚnxÚNetworkXErrorÚ
bfs_layersÚlenÚnodesÚnumpyÚadjacency_matrixÚTÚtoarrayÚsumÚnewaxisÚshapeÚeyeÚlinalgÚinvÚLinAlgErrorÚ	enumerateÚitem)ÚGr   ÚnÚdegÚbasal_nodesÚlayerÚnodeÚreachable_nodesÚnpÚaÚrowsumÚpÚnnÚiÚerrÚmsgÚyÚlevelsÚzero_node_idsr   Únonzero_node_idss                        r   r   r   	   s	  € ðH $%§;¢;Ô;¢;™˜°#¸±(—1¡;€KÑ;ÞÜ×ÒðNó
ð 	
ô Ÿ-š-¨Ò?ôÚ?�ËEÀDŠÉE‰Ñ?ð ñ ô ˆ?Óœs 1§7¡7›|Ó+Ü×ÒðPó
ð 	
ó
 ô 	×Ò˜AÑ-×/Ñ/×7Ñ7Ó9€Að �V‰V�A˜AˆVÐ€FØ	�A‰+‰’q˜& A™+�~Ñ&€Aà	�6˜Q‘;Ñ¢ 2§:¡: Ñ.Ñ.€Að 
�‰�‰€BØ
�‰ˆr‹
€Að	-Ø�I‰I�M‰M˜!™%Ó ˆð 	
�‰�1ˆˆ˜Ñ€Aà€Fñ P°A·K²KÓO€MÛ ˆØˆˆw‹ñ !ñ S°q·{²{ÓRÐÜÐ 0Ö1‰
ˆˆ7ØŸ&™& ›)ˆˆw‹ñ 2ð €Mùóo <ùóøð4 �9‰9× Ñ ó -ð)ð 	ô
 ×Ò˜sÓ#¨Ð,ûð-ús(   �G	 G	ÁGÄ9G ÇHÇ/HÈHc                 ó`   • [        XS9n0 nU R                   H  u  pEX%   X$   -
  X4U4'   M     U$ )a+  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   r>   ÚdiffsÚuÚvs         r   r   r   g   s<   € ô8 ˜AÑ-€FØ€EØ—”‰ˆØ™	 F¡IÑ-ˆ�!ˆf‹ñ à€Ló    c                 ó4  • SSK nU(       a
  [        XS9nOS[        [        R                  " U 5      5      nU(       a"  U R                  5       nUR                  U5        OU n[        XaS9n[        UR                  [        UR                  5       5      5      5      $ )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   Úcannibalismr5   rC   Ú
self_loopsÚG_2s          r   r   r   Š   sw   € ó8 æÜ# AÑ5‰ô œ"×+Ò+¨AÓ.Ó/ˆ
Þà—&‘&“(ˆCØ×!Ñ! *Õ-ð ˆCÜ# CÑ7ˆÜ�—‘œ˜UŸ\™\›^Ó,Ó-Ó.Ð.rF   r   )r   F)
Ú__doc__Únetworkxr   Únetworkx.utilsr   Ú__all__Ú_dispatchabler   r   r   r   rF   r   Ú<module>rW      s›   ðÙ ã Ý .â
T€ñ �\Ó"Ø×Ò˜XÑ&óYó 'ó #ðYñx �\Ó"Ø×Ò˜XÑ&óó 'ó #ðñB �\Ó"Ø×Ò˜XÑ&ó)/ó 'ó #ñ)/rF   