
    hG                         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                    SSK n[        R                  " XS9R                  R	                  5       nUR                  USS9nX4S:g     SS2US:g  4   nXTUS:g     SS2UR                  4   -  nUR                  S   nUR                  U5      n UR                  R                  Xu-
  5      nUR                  SS9S-   n0 nS U R                   5       nU H  nSX'   M	     S U R                   5       n[        U5       H  u  p~UR                  U5      X'   M     U$ ! 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   Nr      )axiszTrophic 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   :   #    U  H  u  pUS :X  d  M  Uv   M     g7fr   N .0node_iddegrees      X/var/www/html/env/lib/python3.13/site-packages/networkx/algorithms/centrality/trophic.py	<genexpr>!trophic_levels.<locals>.<genexpr>I   s     OK6Q;WWK   	c              3   :   #    U  H  u  pUS :w  d  M  Uv   M     g7fr   r   r   s      r   r   r   N   s     R{OGfPQk{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   	   sZ   F  	A-//779A VVAAVF	A+q&A+~&A	6Q;2::..A 
B
r
A	-IIMM!%  	
1AF PAKKOM  ! Sq{{R 01
&&) 2 M- 99   -) 	
 s#,-s   D E5EE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   r5   diffsuvs         r   r   r   U   s<    8 A-FE	FI-!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   cannibalismr+   r:   
self_loopsG_2s          r   r   r   x   sw    8 #A5 "++A./
&&(C!!*- C#C7U\\^,-..r=   r   )r   F)
__doc__networkxr   networkx.utilsr   __all___dispatchabler   r   r   r   r=   r   <module>rN      s      .
T \"X&G ' #GT \"X& ' #B \"X&)/ ' #)/r=   