ó
àÆ÷Xc           @` s»   d  Z  d d l m Z m Z m Z d d l Z d d l Z d d l Z d d l Z d d l m	 Z	 d d l
 m Z e j j e j j e ƒ ƒ Z d „  Z d „  Z e d „ Z d	 „  Z d S(
   sa   Dynamic visualization of Theano graphs.

Author: Christof Angermueller <cangermueller@gmail.com>
i    (   t   absolute_importt   print_functiont   divisionN(   t	   iteritems(   t   PyDotFormatterc         C` s?   x8 t  | ƒ D]* \ } } |  j t | ƒ t | ƒ ƒ }  q W|  S(   s  Replace `replace` in string `x`.

    Parameters
    ----------
    s : str
        String on which function is applied
    replace : dict
        `key`, `value` pairs where key is a regular expression and `value` a
        string by which `key` is replaced
    (   R   t   replacet   str(   t   xR   t   from_t   to(    (    s2   /tmp/pip-build-X4mzal/theano/theano/d3viz/d3viz.pyt   replace_patterns   s    "c         C` s   t  j |  ƒ j d d ƒ S(   s”   Encode `obj` to JSON so that it can be embedded safely inside HTML.

    Parameters
    ----------
    obj : object
        object to serialize
    t   <s   \u003c(   t   jsont   dumpsR   (   t   obj(    (    s2   /tmp/pip-build-X4mzal/theano/theano/d3viz/d3viz.pyt	   safe_json"   s    c         O` s²  t  | | Ž  } | |  ƒ } | j ƒ  } t j sB | j d ƒ } n  t j j | ƒ } | d k r„ t j j | ƒ r„ t j	 | ƒ n  t j j
 t d d ƒ }	 t |	 ƒ  }
 |
 j ƒ  } Wd QXt } | r5d } xi d d g D]R } t j j
 | | | ƒ } t j j | ƒ sÜ t j t j j
 | | ƒ | ƒ qÜ qÜ Wn | } i t j j
 | d ƒ d	 6t j j
 | d ƒ d
 6t | ƒ d 6} t | | ƒ } t | d ƒ  }
 |
 j | ƒ Wd QXd S(   sÄ  Create HTML file with dynamic visualizing of a Theano function graph.

    In the HTML file, the whole graph or single nodes can be moved by drag and
    drop. Zooming is possible via the mouse wheel. Detailed information about
    nodes and edges are displayed via mouse-over events. Node labels can be
    edited by selecting Edit from the context menu.

    Input nodes are colored in green, output nodes in blue. Apply nodes are
    ellipses, and colored depending on the type of operation they perform. Red
    ellipses are transfers from/to the GPU (ops with names GpuFromHost,
    HostFromGpu).

    Edges are black by default. If a node returns a view of an
    input, the input edge will be blue. If it returns a destroyed input, the
    edge will be red.

    Parameters
    ----------
    fct : theano.compile.function_module.Function
        A compiled Theano function, variable, apply or a list of variables.
    outfile : str
        Path to output HTML file.
    copy_deps : bool, optional
        Copy javascript and CSS dependencies to output directory.

    Notes
    -----
    This function accepts extra parameters which will be forwarded to
    :class:`theano.d3viz.formatting.PyDotFormatter`.

    t   utf8t    t   htmls   template.htmlNt   d3vizt   jst   csss   %% JS_DIR %%s   %% CSS_DIR %%s   %% DOT_GRAPH %%t   w(   R   t
   create_dott   sixt   PY2t   decodet   ost   patht   dirnamet   existst   makedirst   joint   __path__t   opent   readt   shutilt   copytreeR   R
   t   write(   t   fctt   outfilet	   copy_depst   argst   kwargst	   formattert   grapht	   dot_grapht   outdirt   template_filet   ft   templatet   src_depst   dst_depst   dt   depR   R   (    (    s2   /tmp/pip-build-X4mzal/theano/theano/d3viz/d3viz.pyR   -   s4    "	 )c         O` s,   t  | | Ž  } | |  ƒ } | j | ƒ d S(   s—  Convert Theano graph to pydot graph and write to dot file.

    Parameters
    ----------
    fct : theano.compile.function_module.Function
        A compiled Theano function, variable, apply or a list of variables.
    path: str
        Path to output file

    Notes
    -----
    This function accepts extra parameters which will be forwarded to
    :class:`theano.d3viz.formatting.PyDotFormatter`.

    N(   R   t	   write_dot(   R'   R   R*   R+   R,   R-   (    (    s2   /tmp/pip-build-X4mzal/theano/theano/d3viz/d3viz.pyt   d3writew   s    (   t   __doc__t
   __future__R    R   R   R   R   R$   R   R   t   theano.d3viz.formattingR   R   R   t   realpatht   __file__R!   R
   R   t   TrueR   R8   (    (    (    s2   /tmp/pip-build-X4mzal/theano/theano/d3viz/d3viz.pyt   <module>   s   		J