ó
ú£Õ\c           @   sÛ   d  Z  d d l Z d d l m Z d d l m Z m Z d j d g ƒ Z	 d d d	 d
 d d g Z
 d e d „ Z d d e d „ Z d d d d e d „ Z d d d d e d „ Z d e d „ Z e d „ Z d „  Z d S(   s1   
Mixing matrices for node attributes and degree.
iÿÿÿÿN(   t   dict_to_numpy_array(   t   node_degree_xyt   node_attribute_xyt    s%   Aric Hagberg <aric.hagberg@gmail.com>t   attribute_mixing_matrixt   attribute_mixing_dictt   degree_mixing_matrixt   degree_mixing_dictt   numeric_mixing_matrixt   mixing_dictc         C   s"   t  |  | | ƒ } t | d | ƒS(   sI  Return dictionary representation of mixing matrix for attribute.

    Parameters
    ----------
    G : graph 
       NetworkX graph object.

    attribute : string 
       Node attribute key.

    nodes: list or iterable (optional)
        Unse nodes in container to build the dict. The default is all nodes.

    normalized : bool (default=False)
       Return counts if False or probabilities if True.

    Examples
    --------
    >>> G=nx.Graph()
    >>> G.add_nodes_from([0,1],color='red')
    >>> G.add_nodes_from([2,3],color='blue')
    >>> G.add_edge(1,3)
    >>> d=nx.attribute_mixing_dict(G,'color')
    >>> print(d['red']['blue'])
    1
    >>> print(d['blue']['red']) # d symmetric for undirected graphs
    1

    Returns
    -------
    d : dictionary
       Counts or joint probability of occurrence of attribute pairs.
    t
   normalized(   R   R	   (   t   Gt	   attributet   nodesR
   t   xy_iter(    (    s`   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/assortativity/mixing.pyR      s    "c         C   sA   t  |  | | ƒ } t | d | ƒ} | r= | | j ƒ  } n  | S(   s«  Return mixing matrix for attribute.

    Parameters
    ----------
    G : graph 
       NetworkX graph object.

    attribute : string 
       Node attribute key.

    nodes: list or iterable (optional)
        Use only nodes in container to build the matrix. The default is 
        all nodes.

    mapping : dictionary, optional        
       Mapping from node attribute to integer index in matrix.  
       If not specified, an arbitrary ordering will be used. 
    
    normalized : bool (default=False)
       Return counts if False or probabilities if True.

    Returns
    -------
    m: numpy array
       Counts or joint probability of occurrence of attribute pairs.
    t   mapping(   R   R    t   sum(   R   R   R   R   R
   t   dt   a(    (    s`   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/assortativity/mixing.pyR   7   s
    t   outt   inc      
   C   s4   t  |  d | d | d | d | ƒ} t | d | ƒS(   s  Return dictionary representation of mixing matrix for degree.

    Parameters
    ----------
    G : graph 
        NetworkX graph object.

    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.

    normalized : bool (default=False)
        Return counts if False or probabilities if True.

    Returns
    -------
    d: dictionary
       Counts or joint probability of occurrence of degree pairs.
    t   xt   yR   t   weightR
   (   R   R	   (   R   R   R   R   R   R
   R   (    (    s`   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/assortativity/mixing.pyR   Z   s    $c      
   C   sÊ   t  |  d | d | d | d | ƒ} t | j ƒ  ƒ } x- | j ƒ  D] \ } }	 | j |	 j ƒ  ƒ qC Wt | ƒ }
 t t t |
 d ƒ t |
 d ƒ ƒ ƒ } t	 | d | ƒ} | rÆ | | j
 ƒ  } n  | S(   sq  Return mixing matrix for attribute.

    Parameters
    ----------
    G : graph 
       NetworkX graph object.

    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).

    nodes: list or iterable (optional)
        Build the matrix using only nodes in container. 
        The default is all nodes.

    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.

    normalized : bool (default=False)
       Return counts if False or probabilities if True.

    Returns
    -------
    m: numpy array
       Counts, or joint probability, of occurrence of node degree.
    R   R   R   R   i   R   (   R   t   sett   keyst   itemst   updatet   maxt   dictt   zipt   rangeR    R   (   R   R   R   R   R   R
   R   t   st   kt   vt   mR   R   (    (    s`   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/assortativity/mixing.pyR   {   s     $)c         C   s¸   t  |  | | ƒ } t | j ƒ  ƒ } x- | j ƒ  D] \ } } | j | j ƒ  ƒ q1 Wt | ƒ } t t t | d ƒ t | d ƒ ƒ ƒ }	 t	 | d |	 ƒ}
 | r´ |
 |
 j
 ƒ  }
 n  |
 S(   s  Return numeric mixing matrix for attribute.

    Parameters
    ----------
    G : graph 
       NetworkX graph object.

    attribute : string 
       Node attribute key.

    nodes: list or iterable (optional)
        Build the matrix only with nodes in container. The default is all nodes.
    
    normalized : bool (default=False)
       Return counts if False or probabilities if True.

    Returns
    -------
    m: numpy array
       Counts, or joint, probability of occurrence of node attribute pairs.
    i   R   (   R   R   R   R   R   R   R   R   R   R    R   (   R   R   R   R
   R   R    R!   R"   R#   R   R   (    (    s`   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/assortativity/mixing.pyR   ¦   s    )c   
      C   sÒ   i  } d } xx |  D]p \ } } | | k r8 i  | | <n  | | k rQ i  | | <n  | | j  | d ƒ } | d | | | <| d 7} q W| rÎ x> | j ƒ  D]- \ } } x | D] }	 | |	 c | :<q­ Wqš Wn  | S(   sŸ  Return a dictionary representation of mixing matrix.

    Parameters
    ----------
    xy : list or container of two-tuples
       Pairs of (x,y) items. 

    attribute : string 
       Node attribute key 

    normalized : bool (default=False)
       Return counts if False or probabilities if True.

    Returns
    -------
    d: dictionary
       Counts or Joint probability of occurrence of values in xy.
    g        i    i   (   t   getR   (
   t   xyR
   R   t   psumR   R   R"   R!   t   jdictt   j(    (    s`   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/assortativity/mixing.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)   t   numpyt   scipy(   t   moduleR)   R+   R,   (    (    s`   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/assortativity/mixing.pyt   setup_moduleï   s    (   t   __doc__t   networkxt   nxt   networkx.utilsR    t'   networkx.algorithms.assortativity.pairsR   R   t   joint
   __author__t   __all__t   Nonet   FalseR   t   TrueR   R   R   R   R	   R.   (    (    (    s`   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/assortativity/mixing.pyt   <module>   s(   	&"	 	*!(