
    \Mh                     .   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      f/var/www/html/test/jupyter/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   )sourceszTrophic 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&6Q;;W;;;;OOr   c              3   ,   K   | ]\  }}|d k    |V  dS r   r   r   s      r   r    z!trophic_levels.<locals>.<genexpr>`   s+      RROGVfPQkkkkkk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 ?s17||++P
 
 	

  	Af---/7799A VVAAVF	&A+qqq&A+~&A	F6Q;2:..A 
B
r

A	-IMM!a%  9  - - -) 	
 s##,- 	
1AF POAKOOOM   w SRq{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 Af---FE . .1q	F1I-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#Af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   