ó
 ,µ[c           @   sÜ   d  Z  d d l Z d d l Z d d l Z d d d d d d d	 g Z d d
 „ Z d „  Z d „  Z	 d „  Z
 d d d d „ Z d d d d „ Z e j j d d ƒ d d d d d d „ ƒ Z d d d d „ Z d „  Z d S(   s
  
***************
Graphviz AGraph
***************

Interface to pygraphviz AGraph class.

Examples
--------
>>> G = nx.complete_graph(5)
>>> A = nx.nx_agraph.to_agraph(G)
>>> H = nx.nx_agraph.from_agraph(A)

See Also
--------
Pygraphviz: http://pygraphviz.github.io/
iÿÿÿÿNt   from_agrapht	   to_agrapht	   write_dott   read_dott   graphviz_layoutt   pygraphviz_layoutt   view_pygraphvizc   	      C   sé  | d	 k r` |  j ƒ  r< |  j ƒ  r0 t j } q] t j } q` |  j ƒ  rT t j } q` t j } n  t j d | ƒ } |  j	 d	 k	 r |  j	 | _	 n  | j
 j |  j ƒ xC |  j ƒ  D]5 } d „  | j j ƒ  Dƒ } | j t | ƒ |  q° Wx· |  j ƒ  D]© } t | d ƒ t | d ƒ } } t | j ƒ } d „  | j ƒ  Dƒ } | j ƒ  sƒ| j	 d	 k	 rm| j	 | d <n  | j | | |  qö | j | | d | j	 | qö Wt |  j ƒ | j
 d <t |  j ƒ | j
 d <t |  j ƒ | j
 d <| S(
   s€  Return a NetworkX Graph or DiGraph from a PyGraphviz graph.

    Parameters
    ----------
    A : PyGraphviz AGraph
      A graph created with PyGraphviz

    create_using : NetworkX graph constructor, optional (default=nx.Graph)
       Graph type to create. If graph instance, then cleared before populated.

    Examples
    --------
    >>> K5 = nx.complete_graph(5)
    >>> A = nx.nx_agraph.to_agraph(K5)
    >>> G = nx.nx_agraph.from_agraph(A)

    Notes
    -----
    The Graph G will have a dictionary G.graph_attr containing
    the default graphviz attributes for graphs, nodes and edges.

    Default node attributes will be in the dictionary G.node_attr
    which is keyed by node.

    Edge attributes will be returned as edge data in G.  With
    edge_attr=False the edge data will be the Graphviz edge weight
    attribute or the value 1 if no edge weight attribute is found.

    i    c         S   s%   i  |  ] \ } } | t  | ƒ “ q S(    (   t   str(   t   .0t   kt   v(    (    s9   lib/python2.7/site-packages/networkx/drawing/nx_agraph.pys
   <dictcomp>Y   s   	 i   c         S   s%   i  |  ] \ } } | t  | ƒ “ q S(    (   R   (   R   R	   R
   (    (    s9   lib/python2.7/site-packages/networkx/drawing/nx_agraph.pys
   <dictcomp>`   s   	 t   keyt   grapht   nodet   edgeN(   t   Nonet   is_directedt	   is_strictt   nxt   DiGrapht   MultiDiGrapht   Grapht
   MultiGrapht   empty_grapht   nameR   t   updatet
   graph_attrt   nodest   attrt   itemst   add_nodeR   t   edgest   dictt   is_multigrapht   add_edget	   node_attrt	   edge_attr(	   t   At   create_usingt   Nt   nt   str_attrt   et   uR
   R   (    (    s9   lib/python2.7/site-packages/networkx/drawing/nx_agraph.pyR    %   s8    ! c         C   s’  y d d l  } Wn  t k
 r2 t d d ƒ ‚ n X|  j ƒ  } t j |  ƒ d k o^ |  j ƒ  } | j d |  j d | d | ƒ } | j j	 |  j
 j d	 i  ƒ ƒ | j j	 |  j
 j d
 i  ƒ ƒ | j j	 |  j
 j d i  ƒ ƒ | j j	 d „  |  j
 j ƒ  Dƒ ƒ xk |  j d t ƒ D]W \ } } | j | ƒ | d k	 r| j | ƒ } | j j	 d „  | j ƒ  Dƒ ƒ qqW|  j ƒ  rx|  j d t d t ƒ D]u \ } }	 }
 } d „  | j ƒ  Dƒ } | j | |	 d t |
 ƒ ƒ| d k	 r•| j | |	 ƒ } | j j	 | ƒ q•q•Wn} xz |  j d t ƒ D]f \ } }	 } d „  | j ƒ  Dƒ } | j | |	 ƒ | d k	 r$| j | |	 ƒ } | j j	 | ƒ q$q$W| S(   sÂ  Return a pygraphviz graph from a NetworkX graph N.

    Parameters
    ----------
    N : NetworkX graph
      A graph created with NetworkX

    Examples
    --------
    >>> K5 = nx.complete_graph(5)
    >>> A = nx.nx_agraph.to_agraph(K5)

    Notes
    -----
    If N has an dict N.graph_attr an attempt will be made first
    to copy properties attached to the graph (see from_agraph)
    and then updated with the calling arguments if any.

    iÿÿÿÿNs   requires pygraphviz s   http://pygraphviz.github.io/i    R   t   strictt   directedR   R   R   c         s   s-   |  ]# \ } } | d k r | | f Vq d S(   R   R   R   N(   R   R   R   (    (   R   R	   R
   (    (    s9   lib/python2.7/site-packages/networkx/drawing/nx_agraph.pys	   <genexpr>’   s    	t   datac         S   s%   i  |  ] \ } } t  | ƒ | “ q S(    (   R   (   R   R	   R
   (    (    s9   lib/python2.7/site-packages/networkx/drawing/nx_agraph.pys
   <dictcomp>š   s   	 t   keysc         S   s1   i  |  ]' \ } } | d  k r t  | ƒ | “ q S(   R   (   R   (   R   R	   R
   (    (    s9   lib/python2.7/site-packages/networkx/drawing/nx_agraph.pys
   <dictcomp>Ÿ   s   	 	R   c         S   s%   i  |  ] \ } } t  | ƒ | “ q S(    (   R   (   R   R	   R
   (    (    s9   lib/python2.7/site-packages/networkx/drawing/nx_agraph.pys
   <dictcomp>¨   s   	 (   t
   pygraphvizt   ImportErrorR   R   t   number_of_selfloopsR!   t   AGraphR   R   R   R   t   getR#   R$   R   R   t   TrueR   R   t   get_nodeR   R   R"   R   t   get_edge(   R'   R0   R-   R,   R%   R(   t   nodedatat   aR+   R
   R   t   edgedatat   str_edgedata(    (    s9   lib/python2.7/site-packages/networkx/drawing/nx_agraph.pyR   p   s>    "!#'+"c         C   sZ   y d d l  } Wn  t k
 r2 t d d ƒ ‚ n Xt |  ƒ } | j | ƒ | j ƒ  d S(   s¼   Write NetworkX graph G to Graphviz dot format on path.

    Parameters
    ----------
    G : graph
       A networkx graph
    path : filename
       Filename or file handle to write
    iÿÿÿÿNs   requires pygraphviz s   http://pygraphviz.github.io/(   R0   R1   R   t   writet   clear(   t   Gt   pathR0   R%   (    (    s9   lib/python2.7/site-packages/networkx/drawing/nx_agraph.pyR   ±   s    

c         C   sO   y d d l  } Wn  t k
 r2 t d d ƒ ‚ n X| j d |  ƒ } t | ƒ S(   s—   Return a NetworkX graph from a dot file on path.

    Parameters
    ----------
    path : file or string
       File name or file handle to read.
    iÿÿÿÿNs   read_dot() requires pygraphviz s   http://pygraphviz.github.io/t   file(   R0   R1   R3   R    (   R?   R0   R%   (    (    s9   lib/python2.7/site-packages/networkx/drawing/nx_agraph.pyR   Æ   s    t   neatot    c         C   s   t  |  d | d | d | ƒS(   s  Create node positions for G using Graphviz.

    Parameters
    ----------
    G : NetworkX graph
      A graph created with NetworkX
    prog : string
      Name of Graphviz layout program
    root : string, optional
      Root node for twopi layout
    args : string, optional
      Extra arguments to Graphviz layout program

    Returns : dictionary
      Dictionary of x, y, positions keyed by node.

    Examples
    --------
    >>> G = nx.petersen_graph()
    >>> pos = nx.nx_agraph.graphviz_layout(G)
    >>> pos = nx.nx_agraph.graphviz_layout(G, prog='dot')

    Notes
    -----
    This is a wrapper for pygraphviz_layout.

    t   progt   roott   args(   R   (   R>   RC   RD   RE   (    (    s9   lib/python2.7/site-packages/networkx/drawing/nx_agraph.pyR   ×   s    c         C   sú   y d d l  } Wn  t k
 r2 t d d ƒ ‚ n X| d k	 rP | d | 7} n  t |  ƒ } | j d | d | ƒ i  } x{ |  D]s } | j | | ƒ } y< | j d j d	 ƒ \ }	 }
 t |	 ƒ t |
 ƒ f | | <Wq d
 | f GHd | | <q Xq W| S(   s  Create node positions for G using Graphviz.

    Parameters
    ----------
    G : NetworkX graph
      A graph created with NetworkX
    prog : string
      Name of Graphviz layout program
    root : string, optional
      Root node for twopi layout
    args : string, optional
      Extra arguments to Graphviz layout program

    Returns : dictionary
      Dictionary of x, y, positions keyed by node.

    Examples
    --------
    >>> G = nx.petersen_graph()
    >>> pos = nx.nx_agraph.graphviz_layout(G)
    >>> pos = nx.nx_agraph.graphviz_layout(G, prog='dot')

    Notes
    -----
    If you use complex node objects, they may have the same string
    representation and GraphViz could treat them as the same node.
    The layout may assign both nodes a single location. See Issue #1568
    If this occurs in your case, consider relabeling the nodes just
    for the layout computation using something similar to:

        H = nx.convert_node_labels_to_integers(G, label_attribute='node_label')
        H_layout = nx.nx_agraph.pygraphviz_layout(G, prog='dot')
        G_layout = {H.nodes[n]['node_label']: p for n, p in H_layout.items()}

    iÿÿÿÿNs   requires pygraphviz s   http://pygraphviz.github.io/s	   -Groot=%sRC   RE   t   post   ,s   no position for nodeg        (   g        g        (	   R0   R1   R   R   t   layoutt   NodeR   t   splitt   float(   R>   RC   RD   RE   R0   R%   t   node_posR(   R   t   xxt   yy(    (    s9   lib/python2.7/site-packages/networkx/drawing/nx_agraph.pyR   ö   s&    $ i   s   w+bt   dotc            s’  t  ˆ  ƒ s t j d ƒ ‚ n  d d l } d d d g } x- | D]% } | ˆ  j k r@ i  ˆ  j | <q@ q@ Wi d d 6}	 i d	 d
 6d d 6d d 6d d 6d d 6}
 i  } ‡  f d †  } ‡  f d †  } | d |	 ƒ | d |
 ƒ | d | ƒ t ˆ  ƒ } | d |	 ƒ | d |
 ƒ | d | ƒ ˆ d k	 rt ˆ d ƒ sJ‡ f d †  } n ˆ } ˆ  j ƒ  rÂx´ ˆ  j	 d t
 d t
 ƒ D]F \ } } } } | j | | t | ƒ ƒ } t | | ƒ ƒ | j d <quWqxQ ˆ  j	 d t
 ƒ D]: \ } } } | j | | ƒ } t | | ƒ ƒ | j d <qÕWn  | d k rid } | rAd | | f } n d | f } t j d | d t ƒ } n  t | d | d | d  | ƒ| j | f S(!   sk  Views the graph G using the specified layout algorithm.

    Parameters
    ----------
    G : NetworkX graph
        The machine to draw.
    edgelabel : str, callable, None
        If a string, then it specifes the edge attribute to be displayed
        on the edge labels. If a callable, then it is called for each
        edge and it should return the string to be displayed on the edges.
        The function signature of `edgelabel` should be edgelabel(data),
        where `data` is the edge attribute dictionary.
    prog : string
        Name of Graphviz layout program.
    args : str
        Additional arguments to pass to the Graphviz layout program.
    suffix : str
        If `filename` is None, we save to a temporary file.  The value of
        `suffix` will appear at the tail end of the temporary filename.
    path : str, None
        The filename used to save the image.  If None, save to a temporary
        file.  File formats are the same as those from pygraphviz.agraph.draw.

    Returns
    -------
    path : str
        The filename of the generated image.
    A : PyGraphviz graph
        The PyGraphviz graph instance used to generate the image.

    Notes
    -----
    If this function is called in succession too quickly, sometimes the
    image is not displayed. So you might consider time.sleep(.5) between
    calls if you experience problems.

    s   An empty graph cannot be drawn.iÿÿÿÿNR   R   R   t   10t   fontsizet   filledt   styles	   #0000FF40t	   fillcolors   0.75t   heightt   widtht   circlet   shapec            s[   g  } xN | j  ƒ  D]@ \ } } | ˆ  j |  k r | ˆ  j |  | <| j | ƒ q q Wd  S(   N(   R   R   t   append(   t   whicht   attrst   addedR	   R
   (   R>   (    s9   lib/python2.7/site-packages/networkx/drawing/nx_agraph.pyt   update_attrsq  s
    c            s=   x | D] } ˆ  j  |  | =q Wˆ  j  |  s9 ˆ  j  |  =n  d  S(   N(   R   (   RZ   R\   R   (   R>   (    s9   lib/python2.7/site-packages/networkx/drawing/nx_agraph.pyt   clean_attrsy  s    t   __call__c            s    d j  d t |  ˆ  ƒ d g ƒ S(   NRB   s     (   t   joinR   (   R.   (   t	   edgelabel(    s9   lib/python2.7/site-packages/networkx/drawing/nx_agraph.pyt   func  s    R/   R.   t   labelt   pngs   _%s.%ss   .%st   suffixt   deleteR?   RC   RE   (   t   lenR   t   NetworkXExceptionR0   R   R   R   t   hasattrR!   R   R5   R7   R   R   t   tempfilet   NamedTemporaryFilet   Falset   display_pygraphvizR   (   R>   Ra   RC   RE   Re   R?   R0   R[   R   t
   edge_attrst
   node_attrst   graph_attrsR]   R^   R%   Rb   R+   R
   R   R.   R   t   ext(    (   R>   Ra   s9   lib/python2.7/site-packages/networkx/drawing/nx_agraph.pyR   /  sT    (	

+ " c         C   s{   | d k r8 | j } t j j | ƒ d j ƒ  d } n  | sG d } n  |  j | | | | ƒ | j ƒ  t j	 j
 | ƒ d S(   sý  Internal function to display a graph in OS dependent manner.

    Parameters
    ----------
    graph : PyGraphviz graph
        A PyGraphviz AGraph instance.
    path :  file object
        An already opened file object that will be closed.
    format : str, None
        An attempt is made to guess the output format based on the extension
        of the filename. If that fails, the value of `format` is used.
    prog : string
        Name of Graphviz layout program.
    args : str
        Additional arguments to pass to the Graphviz layout program.

    Notes
    -----
    If this function is called in succession too quickly, sometimes the
    image is not displayed. So you might consider time.sleep(.5) between
    calls if you experience problems.

    i   N(   R   R   t   osR?   t   splitextt   lowert   drawt   closeR   t   utilst   default_opener(   R   R?   t   formatRC   RE   t   filename(    (    s9   lib/python2.7/site-packages/networkx/drawing/nx_agraph.pyRm   °  s    	#	
c         C   s:   d d l  m } y d d  l } Wn | d ƒ ‚ n Xd  S(   Niÿÿÿÿ(   t   SkipTests   pygraphviz not available(   t   noseR{   R0   (   t   moduleR{   R0   (    (    s9   lib/python2.7/site-packages/networkx/drawing/nx_agraph.pyt   setup_module×  s
    (   t   __doc__Rr   Rj   t   networkxR   t   __all__R   R    R   R   R   R   R   Rw   t	   open_fileR   Rm   R~   (    (    (    s9   lib/python2.7/site-packages/networkx/drawing/nx_agraph.pyt   <module>   s&   	K	A		9	'