ó
ú£Õ\c           @   sî   d  Z  d d l Z d d l Z d d l Z d d l Z d 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.to_agraph(G)
>>> H=nx.from_agraph(A)

See Also
--------
Pygraphviz: http://networkx.lanl.gov/pygraphviz
iÿÿÿÿNs   Aric Hagberg (hagberg@lanl.gov)t   from_agrapht	   to_agrapht	   write_dott   read_dott   graphviz_layoutt   pygraphviz_layoutt   view_pygraphvizc   	      C   s÷  | d
 k rl |  j ƒ  rB |  j ƒ  r3 t j ƒ  } qi t j ƒ  } ql |  j ƒ  r] t j ƒ  } ql t j ƒ  } n  t j d | ƒ } d | _	 |  j	 d
 k	 r¥ |  j	 | _	 n  xI |  j
 ƒ  D]; } t d „  | j j ƒ  Dƒ ƒ } | j t | ƒ |  q² Wx½ |  j ƒ  D]¯ } t | d ƒ t | d ƒ } } t | j ƒ } t d „  | j ƒ  Dƒ ƒ } | j ƒ  s‘| j	 d
 k	 r{| 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 class instance
      The output is created using the given graph class instance

    Examples
    --------
    >>> K5=nx.complete_graph(5)
    >>> A=nx.to_agraph(K5)
    >>> G=nx.from_agraph(A)
    >>> G=nx.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    t    c         s   s'   |  ] \ } } t  | ƒ | f Vq d  S(   N(   t   str(   t   .0t   kt   v(    (    sR   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/drawing/nx_agraph.pys	   <genexpr>W   s    i   c         s   s'   |  ] \ } } t  | ƒ | f Vq d  S(   N(   R   (   R	   R
   R   (    (    sR   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/drawing/nx_agraph.pys	   <genexpr>^   s    t   keyt   grapht   nodet   edgeN(   t   Nonet   is_directedt	   is_strictt   nxt   DiGrapht   MultiDiGrapht   Grapht
   MultiGrapht   empty_grapht   namet   nodest   dictt   attrt   itemst   add_nodeR   t   edgest   is_multigrapht   add_edget
   graph_attrR   t	   node_attrt	   edge_attr(	   t   At   create_usingt   Nt   nt   str_attrt   et   uR   R   (    (    sR   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/drawing/nx_agraph.pyR    #   s8     	! c         C   sä  y d d l  } Wn# t k
 r5 t d d d ƒ ‚ n X|  j ƒ  } |  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  ƒ ƒ x0 |  j d t ƒ D] \ } } | j | |  qò W|  j ƒ  r‹x¿ |  j d t d t ƒ D]M \ } } }	 }
 t d „  |
 j ƒ  Dƒ ƒ } | j | | d t |	 ƒ | q7WnU xR |  j d t ƒ D]> \ } } }
 t d „  |
 j ƒ  Dƒ ƒ } | j | | |  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.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://networkx.lanl.gov/pygraphviz s   (not available for Python3)i    R   t   strictt   directedR   R   R   t   datat   keysc         s   s'   |  ] \ } } | t  | ƒ f Vq d  S(   N(   R   (   R	   R
   R   (    (    sR   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/drawing/nx_agraph.pys	   <genexpr>˜   s    R   c         s   s'   |  ] \ } } | t  | ƒ f Vq d  S(   N(   R   (   R	   R
   R   (    (    sR   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/drawing/nx_agraph.pys	   <genexpr>œ   s    (   t
   pygraphvizt   ImportErrorR   t   number_of_selfloopsR    t   AGraphR   R"   t   updateR   t   getR#   R$   R   t   TrueR   t
   edges_iterR   R   R!   R   (   R'   R0   R-   R,   R%   R(   t   nodedataR+   R   R   t   edgedatat   str_edgedata(    (    sR   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/drawing/nx_agraph.pyR   m   s,    !+&"c         C   s]   y d d l  } Wn# t k
 r5 t d 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://networkx.lanl.gov/pygraphviz s   (not available for Python3)(   R0   R1   R   t   writet   clear(   t   Gt   pathR0   R%   (    (    sR   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/drawing/nx_agraph.pyR   ¢   s    

c         C   sR   y d d l  } Wn# t k
 r5 t d 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://networkx.lanl.gov/pygraphviz s   (not available for Python3)t   file(   R0   R1   R3   R    (   R>   R0   R%   (    (    sR   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/drawing/nx_agraph.pyR   ·   s    t   neatoR   c         C   s   t  |  d | d | d | ƒS(   sc  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.graphviz_layout(G)
    >>> pos=nx.graphviz_layout(G,prog='dot')

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

    t   progt   roott   args(   R   (   R=   RA   RB   RC   (    (    sR   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/drawing/nx_agraph.pyR   É   s    c         C   sý   y d d l  } Wn# t k
 r5 t d d d ƒ ‚ n X| d k	 rS | 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.graphviz_layout(G)
    >>> pos=nx.graphviz_layout(G,prog='dot')

    iÿÿÿÿNs   requires pygraphviz s$   http://networkx.lanl.gov/pygraphviz s   (not available for Python3)s	   -Groot=%sRA   RC   t   post   ,s   no position for nodeg        (   g        g        (	   R0   R1   R   R   t   layoutt   NodeR   t   splitt   float(   R=   RA   RB   RC   R0   R%   t   node_posR(   R   t   xxt   yy(    (    sR   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/drawing/nx_agraph.pyR   ç   s(     i   t   wt   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=   (    sR   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/drawing/nx_agraph.pyt   update_attrsV  s
    c            s=   x | D] } ˆ  j  |  | =q Wˆ  j  |  s9 ˆ  j  |  =n  d  S(   N(   R   (   RY   R[   R   (   R=   (    sR   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/drawing/nx_agraph.pyt   clean_attrs^  s    t   __call__c            s    d j  d t |  ˆ  ƒ d g ƒ S(   NR   s     (   t   joinR   (   R.   (   t	   edgelabel(    sR   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/drawing/nx_agraph.pyt   funcu  s    R/   R.   t   labelt   pngs   _%s.%ss   .%st   suffixt   deleteR>   RA   RC   (   t   lenR   t   NetworkXExceptionR0   R   R   R   t   hasattrR    R7   R6   t   get_edgeR   R   t   tempfilet   NamedTemporaryFilet   Falset   display_pygraphvizR   (   R=   R`   RA   RC   Rd   R>   R0   RZ   R   t
   edge_attrst
   node_attrst   graph_attrsR\   R]   R%   Ra   R+   R   R   R.   R   t   ext(    (   R=   R`   sR   /Users/dxp/prism/prism-games/prism-examples/smgs/car/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   formatRA   RC   t   filename(    (    sR   /Users/dxp/prism/prism-games/prism-examples/smgs/car/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   (    (    sR   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/drawing/nx_agraph.pyt   setup_moduleº  s
    (   t   __doc__Rr   t   sysRj   t   networkxR   t
   __author__t   __all__R   R    R   R   R   R   R   Rw   t	   open_fileR   Rm   R~   (    (    (    sR   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/drawing/nx_agraph.pyt   <module>   s*   	J	5		-	~&