ó
ú£Õ\c           @   sÌ   d  Z  d d l Z d d l m Z m Z m Z d d l m Z m	 Z	 d j
 d d g ƒ Z d d	 d
 d g Z d d d d d „ Z d d d d d „ Z d d „ Z d d „ Z d „  Z d „  Z d „  Z d S(   s:   Node assortativity coefficients and correlation measures.
iÿÿÿÿN(   t   degree_mixing_matrixt   attribute_mixing_matrixt   numeric_mixing_matrix(   t   node_degree_xyt   node_attribute_xyt    s%   Aric Hagberg <aric.hagberg@gmail.com>s+   Oleguer Sagarra <oleguer.sagarra@gmail.com>t&   degree_pearson_correlation_coefficientt    degree_assortativity_coefficientt#   attribute_assortativity_coefficientt!   numeric_assortativity_coefficientt   outt   inc      
   C   s.   t  |  d | d | d | d | ƒ} t | ƒ S(   sÇ  Compute degree assortativity of graph.

    Assortativity measures the similarity of connections
    in the graph with respect to the node degree.

    Parameters
    ----------
    G : NetworkX graph

    x: string ('in','out')
       The degree type for source node (directed graphs only).
    
    y: string ('in','out')
       The degree type for target node (directed graphs only).

    weight: string or None, optional (default=None)
       The edge attribute that holds the numerical value used 
       as a weight.  If None, then each edge has weight 1.
       The degree is the sum of the edge weights adjacent to the node.

    nodes: list or iterable (optional)
        Compute degree assortativity only for nodes in container. 
        The default is all nodes.

    Returns
    -------
    r : float
       Assortativity of graph by degree.
    
    Examples
    --------
    >>> G=nx.path_graph(4)
    >>> r=nx.degree_assortativity_coefficient(G)
    >>> print("%3.1f"%r)
    -0.5

    See Also
    --------
    attribute_assortativity_coefficient
    numeric_assortativity_coefficient
    neighbor_connectivity
    degree_mixing_dict
    degree_mixing_matrix

    Notes
    -----
    This computes Eq. (21) in Ref. [1]_ , where e is the joint
    probability distribution (mixing matrix) of the degrees.  If G is
    directed than the matrix e is the joint probability of the 
    user-specified degree type for the source and target.

    References
    ----------
    .. [1] M. E. J. Newman, Mixing patterns in networks,
       Physical Review E, 67 026126, 2003
    .. [2] Foster, J.G., Foster, D.V., Grassberger, P. & Paczuski, M. 
       Edge direction and the structure of networks, PNAS 107, 10815-20 (2010).
    t   xt   yt   nodest   weight(   R    t
   numeric_ac(   t   GR   R   R   R   t   M(    (    se   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/assortativity/correlation.pyR      s    <$c      
   C   s}   y d d l  j } Wn t k
 r2 t d ƒ ‚ n Xt |  d | d | d | d | ƒ} t | Œ  \ } } | j | | ƒ d S(	   s·  Compute degree assortativity of graph. 

    Assortativity measures the similarity of connections
    in the graph with respect to the node degree.

    This is the same as degree_assortativity_coefficient but uses the 
    potentially faster scipy.stats.pearsonr function.

    Parameters
    ----------
    G : NetworkX graph

    x: string ('in','out')
       The degree type for source node (directed graphs only).

    y: string ('in','out')
       The degree type for target node (directed graphs only).

    weight: string or None, optional (default=None)
       The edge attribute that holds the numerical value used 
       as a weight.  If None, then each edge has weight 1.
       The degree is the sum of the edge weights adjacent to the node.

    nodes: list or iterable (optional)
        Compute pearson correlation of degrees only for specified nodes.
        The default is all nodes.

    Returns
    -------
    r : float
       Assortativity of graph by degree.
    
    Examples
    --------
    >>> G=nx.path_graph(4)
    >>> r=nx.degree_pearson_correlation_coefficient(G) 
    >>> r 
    -0.5

    Notes
    -----
    This calls scipy.stats.pearsonr.

    References
    ----------
    .. [1] M. E. J. Newman, Mixing patterns in networks
           Physical Review E, 67 026126, 2003
    .. [2] Foster, J.G., Foster, D.V., Grassberger, P. & Paczuski, M. 
       Edge direction and the structure of networks, PNAS 107, 10815-20 (2010).
    iÿÿÿÿNs0   Assortativity requires SciPy: http://scipy.org/ R   R   R   R   i    (   t   scipy.statst   statst   ImportErrorR   t   zipt   pearsonr(   R   R   R   R   R   R   t   xy(    (    se   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/assortativity/correlation.pyR   P   s    4$c         C   s   t  |  | | ƒ } t | ƒ S(   s-  Compute assortativity for node attributes.

    Assortativity measures the similarity of connections
    in the graph with respect to the given attribute.
    
    Parameters
    ----------
    G : NetworkX graph

    attribute : string 
        Node attribute key

    nodes: list or iterable (optional)
        Compute attribute assortativity for nodes in container. 
        The default is all nodes. 

    Returns
    -------
    r: float
       Assortativity of graph for given attribute
    
    Examples
    --------
    >>> G=nx.Graph()
    >>> G.add_nodes_from([0,1],color='red')
    >>> G.add_nodes_from([2,3],color='blue')
    >>> G.add_edges_from([(0,1),(2,3)])
    >>> print(nx.attribute_assortativity_coefficient(G,'color'))
    1.0

    Notes
    -----
    This computes Eq. (2) in Ref. [1]_ , trace(M)-sum(M))/(1-sum(M),
    where M is the joint probability distribution (mixing matrix)
    of the specified attribute.

    References
    ----------
    .. [1] M. E. J. Newman, Mixing patterns in networks,
       Physical Review E, 67 026126, 2003
    (   R   t   attribute_ac(   R   t	   attributeR   R   (    (    se   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/assortativity/correlation.pyR   Ž   s    *c         C   s   t  |  | | ƒ } t | ƒ S(   s  Compute assortativity for numerical node attributes.

    Assortativity measures the similarity of connections
    in the graph with respect to the given numeric attribute.
    
    Parameters
    ----------
    G : NetworkX graph

    attribute : string 
        Node attribute key

    nodes: list or iterable (optional)
        Compute numeric assortativity only for attributes of nodes in 
        container. The default is all nodes.

    Returns
    -------
    r: float
       Assortativity of graph for given attribute
    
    Examples
    --------
    >>> G=nx.Graph()
    >>> G.add_nodes_from([0,1],size=2)
    >>> G.add_nodes_from([2,3],size=3)
    >>> G.add_edges_from([(0,1),(2,3)])
    >>> print(nx.numeric_assortativity_coefficient(G,'size'))
    1.0

    Notes
    -----
    This computes Eq. (21) in Ref. [1]_ , for the mixing matrix of 
    of the specified attribute.

    References
    ----------
    .. [1] M. E. J. Newman, Mixing patterns in networks
           Physical Review E, 67 026126, 2003
    (   R   R   (   R   R   R   t   a(    (    se   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/assortativity/correlation.pyR	   ¼   s    )c         C   s¢   y d d l  } Wn t k
 r/ t d ƒ ‚ n X|  j ƒ  d k r[ |  t |  j ƒ  ƒ }  n  | j |  ƒ }  |  |  j ƒ  } |  j ƒ  } | | d | } t | ƒ S(   sÐ  Compute assortativity for attribute matrix M.

    Parameters
    ----------
    M : numpy array or matrix
        Attribute mixing matrix.

    Notes
    -----
    This computes Eq. (2) in Ref. [1]_ , (trace(e)-sum(e))/(1-sum(e)),
    where e is the joint probability distribution (mixing matrix)
    of the specified attribute.

    References
    ----------
    .. [1] M. E. J. Newman, Mixing patterns in networks,
       Physical Review E, 67 026126, 2003
    iÿÿÿÿNs:   attribute_assortativity requires NumPy: http://scipy.org/ g      ð?i   (   t   numpyR   t   sumt   floatt   asmatrixt   trace(   R   R   t   st   tt   r(    (    se   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/assortativity/correlation.pyR   é   s    c         C   sB  y d d  l  } Wn  t k
 r2 t d d ƒ ‚ n X|  j ƒ  d k r^ |  t |  j ƒ  ƒ }  n  |  j \ } } | j | ƒ } | j | ƒ } |  j d d ƒ } |  j d d ƒ } | | d j ƒ  | | j ƒ  d } | | d j ƒ  | | j ƒ  d }	 | j | | ƒ }
 | j | | ƒ } |
 |  | j ƒ  | j | |	 ƒ S(	   Niÿÿÿÿs   numeric_assortativity requires s   NumPy: http://scipy.org/g      ð?t   axisi    i   i   (   R   R   R   R   t   shapet   aranget   outert   sqrt(   R   R   t   nxt   nyR   R   R   t   bt   varat   varbR   t   ab(    (    se   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/assortativity/correlation.pyR   
  s"    &&c         C   s`   d d l  m } y d d  l } Wn | d ƒ ‚ n Xy d d  l } Wn | d ƒ ‚ n Xd  S(   Niÿÿÿÿ(   t   SkipTests   NumPy not availables   SciPy not available(   t   noseR/   R   t   scipy(   t   moduleR/   R   R1   (    (    se   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/assortativity/correlation.pyt   setup_module!  s    (   t   __doc__t   networkxR)   t(   networkx.algorithms.assortativity.mixingR    R   R   t'   networkx.algorithms.assortativity.pairsR   R   t   joint
   __author__t   __all__t   NoneR   R   R   R	   R   R   R3   (    (    (    se   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/assortativity/correlation.pyt   <module>   s$   			?=.-	!	