ó
àÆ÷Xc           @` s?  d  Z  d d l m Z m Z m Z d d l Z d d l Z d d l m	 Z	 d d l
 m Z m Z d d l Z d d l m Z d d l m Z d d l m Z d d	 l m Z m Z y d d
 l m Z Wn e k
 rÙ n Xd e f d „  ƒ  YZ d d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z  d S(   si   Functions for formatting Theano compute graphs.

Author: Christof Angermueller <cangermueller@gmail.com>
i    (   t   absolute_importt   print_functiont   divisionN(   t   reduce(   t	   iteritemst
   itervalues(   t   gof(   t   Function(   t   builders(   t   pydot_importedt   pydot_imported_msg(   t   pdt   PyDotFormatterc           B` s8   e  Z d  Z e d „ Z d „  Z d „  Z d d „ Z RS(   st  Create `pydot` graph object from Theano function.

    Parameters
    ----------
    compact : bool
        if True, will remove intermediate variables without name.

    Attributes
    ----------
    node_colors : dict
        Color table of node types.
    apply_colors : dict
        Color table of apply nodes.
    shapes : dict
        Shape table of node types.
    c         C` sº   t  s t d t ƒ ‚ n  | |  _ i d d 6d d 6d d 6d d	 6d
 d 6|  _ i d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6|  _ i d d 6d d	 6d d 6|  _ d |  _ d S(    s    Construct PyDotFormatter object.s   Failed to import pydot. t	   limegreent   inputt   SpringGreent   constant_inputt   YellowGreent   shared_inputt
   dodgerbluet   outputt	   lightgreyt   unusedt   redt   GpuFromHostt   HostFromGput   yellowt   Scant   cyant   Shapet   magentat   IfElses   #FFAABBt   Elemwises   #FFAAFFt	   Subtensors   #FFAA22t   Alloct   boxt   ellipset   applyt   nN(   R	   t   ImportErrorR
   t   compactt   node_colorst   apply_colorst   shapest   _PyDotFormatter__node_prefix(   t   selfR(   (    (    s7   /tmp/pip-build-X4mzal/theano/theano/d3viz/formatting.pyt   __init__)   s*    	


c         C` sF   | |  j  k s t ‚ d |  j t |  j  ƒ d f } | |  j  | <| S(   s  Add new node to node list and return unique id.

        Parameters
        ----------
        node : Theano graph node
            Apply node, tensor variable, or shared variable in compute graph.

        Returns
        -------
        str
            Unique node id.
        s   %s%di   (   t   _PyDotFormatter__nodest   AssertionErrorR,   t   len(   R-   t   nodet   _id(    (    s7   /tmp/pip-build-X4mzal/theano/theano/d3viz/formatting.pyt
   __add_nodeB   s     c         C` s+   | |  j  k r |  j  | S|  j | ƒ Sd S(   sþ   Return unique node id.

        Parameters
        ----------
        node : Theano graph node
            Apply node, tensor variable, or shared variable in compute graph.

        Returns
        -------
        str
            Unique node id.
        N(   R/   t   _PyDotFormatter__add_node(   R-   R2   (    (    s7   /tmp/pip-build-X4mzal/theano/theano/d3viz/formatting.pyt	   __node_idT   s    c      	   C` sy  | d# k r t j ƒ  } n  i  |  _ d# } t | t ƒ ro t | d d# ƒ } | j j j	 } | j j j
 ƒ  } nÖ t | t j ƒ r™ | j	 } | j
 ƒ  } n¬ t | t j ƒ r· | g } n t | t j ƒ rÕ | j	 } n  t | t t f ƒ sð t ‚ t d „  | Dƒ ƒ st ‚ t j d t j j | ƒ d | ƒ } | j	 } | j
 ƒ  } t | ƒ } x!| D]} i  } |  j | ƒ } | | d <t | ƒ | d <t | | ƒ | d <d | d <| d | d	 <|  j d | d
 <d# }	 x; t |  j ƒ D]* \ }
 } |
 | j j j k rß| }	 qßqßW|	 r4d | d <|	 | d <d | d <n  t | ƒ } | j | ƒ xt  | j ƒ D]\ } } |  j | j! r| j! n | ƒ } | j! d# k rmi | d 6t" | ƒ d 6d d 6} t | t j# ƒ rÙd | d <n% t | t$ j% j& j' ƒ rþd | d <n  t( | j) ƒ | d <t* | ƒ | d <d | d <|  j+ | d | d <|  j d | d
 <t | ƒ } | j | ƒ n  i  } t, | j d ƒ rÀ| t- t j. t/ | j j0 ƒ g  ƒ k rÀ|  j+ d | d <nF t, | j d ƒ r| t- t j. t/ | j j1 ƒ g  ƒ k rd | d <n  | d } t2 | j ƒ d k r<t3 | ƒ d | } n  t j4 | | d | | } | j5 | ƒ q]WxUt  | j	 ƒ D]D\ } } |  j | ƒ } | | k s´t2 | j6 ƒ d k rƒi | d 6t" | ƒ d 6d d 6t( | j) ƒ d 6t* | ƒ d 6d d 6} t2 | j6 ƒ d k r"|  j+ d | d <n |  j+ d | d <|  j d | d
 <t | ƒ } | j | ƒ | j5 t j4 | | d | d ƒƒ qx| j7 s–|  j8 rx| j5 t j4 | | d | d ƒƒ qxqxWt | j t9 j: ƒ rXt j; | ƒ } t< ƒ  } | | _= | j j> | d# d# d ƒ | | j j? | ƒ | j@ | ƒ | jA ƒ  | jB ƒ  d <d  „  } g  | j D] } |  j | ƒ ^ qX} g  | j jC D] } | j | ƒ ^ q€} t2 | ƒ t2 | ƒ k s¹t ‚ | tD | | ƒ ƒ } | | jB ƒ  d! <g  | j	 D] } |  j | ƒ ^ qè} g  | j jE D] } | j | ƒ ^ q} t2 | ƒ t2 | ƒ k sIt ‚ | tD | | ƒ ƒ } | | jB ƒ  d" <qXqXW| S($   s¢  Create pydot graph from function.

        Parameters
        ----------
        fct : theano.compile.function_module.Function
            A compiled Theano function, variable, apply or a list of variables.
        graph: pydot.Dot
            `pydot` graph to which nodes are added. Creates new one if
            undefined.

        Returns
        -------
        pydot.Dot
            Pydot graph of `fct`
        t   profilec         s` s!   |  ] } t  | t j ƒ Vq d  S(   N(   t
   isinstanceR   t   Variable(   t   .0t   v(    (    s7   /tmp/pip-build-X4mzal/theano/theano/d3viz/formatting.pys	   <genexpr>‰   s    t   inputst   outputst   namet   labelR%   t	   node_typet   apply_opt   shapet   filledt   stylet	   fillcolort   coloredt   typeR   R   R   t   dtypet   tagt   view_mapR   t   colort   destroy_mapR   i   t    i    R   t   pyt   subgc         S` s#   t  g  |  D] } t | ƒ ^ q
 ƒ S(   N(   t   strt   list(   t   mt   x(    (    s7   /tmp/pip-build-X4mzal/theano/theano/d3viz/formatting.pyt
   format_mapñ   s    t   subg_map_inputst   subg_map_outputsN(F   t   NoneR   t   DotR/   R8   R   t   getattrt   makert   fgraphR=   t   toposortR   t   FunctionGraphR9   t   ApplyRQ   t   tupleR0   t   allt   graphR<   t   _PyDotFormatter__node_idt   apply_labelt   apply_profileR+   R   R*   t   opt	   __class__t   __name__t   dict_to_pdnodet   add_nodet	   enumeratet   ownert	   var_labelt   Constantt   theanot   tensort	   sharedvart   TensorSharedVariablet   type_to_strRG   t   var_tagR)   t   hasattrR   t   __add__R   RJ   RL   R1   RP   t   Edget   add_edget   clientsR>   R(   R   t   OpFromGrapht   ClusterR   R,   t   prepare_nodet   fnt   add_subgrapht   get_namet   get_attributest   local_inputst   zipt   local_outputs(   R-   t   fctRa   R7   R=   t   topoR2   t   nparamsRb   t	   use_colort   opNameRK   t   pd_nodet   idt   vart   var_idt   vparamst   pd_vart   edge_paramst
   edge_labelt   pdedget   subgrapht   gfRT   RS   t
   ext_inputst
   int_inputst   ht   ext_outputst   int_outputs(    (    s7   /tmp/pip-build-X4mzal/theano/theano/d3viz/formatting.pyt   __call__f   sæ    				



!



	!

			%%%(N(	   Rg   t
   __module__t   __doc__t   TrueR.   R5   Rb   RW   R˜   (    (    (    s7   /tmp/pip-build-X4mzal/theano/theano/d3viz/formatting.pyR      s
   		i   c         C` sÝ   |  j  d k	 r |  j  St |  t j ƒ rÌ t j |  j ƒ } t } | j	 d k rj t
 } t j | g ƒ } n  t j | d | ƒ} d | k r¡ | | j d ƒ  } n  | rÈ | j d d ƒ j d d ƒ } n  | St |  j ƒ Sd S(   s   Return label of variable node.i    t	   precisions   
t   [t    t   ]N(   R>   RW   R8   R   Rm   t   npt   asarrayt   datat   Falset   ndimR›   t   arrayt   array2stringt   indext   replaceRr   RG   (   RŠ   Rœ   R•   t   is_constt   dstr(    (    s7   /tmp/pip-build-X4mzal/theano/theano/d3viz/formatting.pyRl     s    !c         C` sð   |  j  } t | d ƒ rè t | j ƒ rè t | j d ƒ d k rè t | j d d t t f ƒ r€ | j d d \ } } } } n | j d \ } } } } t j j	 | ƒ } | j
 d d ƒ } | j
 d d ƒ } | j ƒ  } | | | g Sd Sd S(	   s%   Parse tag attribute of variable node.t   tracei    i   iÿÿÿÿt   <Rž   t   >N(   RI   Rt   R1   R«   R8   R_   RQ   t   ost   patht   basenameR¨   t   encodeRW   (   RŠ   RI   R¯   t   linet   _t   src(    (    s7   /tmp/pip-build-X4mzal/theano/theano/d3viz/formatting.pyRs     s    	7  c         C` s   |  j  j j S(   s   Return label of apply node.(   Re   Rf   Rg   (   R2   (    (    s7   /tmp/pip-build-X4mzal/theano/theano/d3viz/formatting.pyRc   +  s    c         C` sB   | s | j  d k r d S| j j |  d ƒ } | j  } | | g S(   s"   Return apply profiling informaton.i    N(   t   fct_call_timeRW   t
   apply_timet   get(   R2   R7   t   timet	   call_time(    (    s7   /tmp/pip-build-X4mzal/theano/theano/d3viz/formatting.pyRd   0  s
    	c         C` sa   i d d 6d t  f 6d t  t f 6d t t  f 6d t  t  f 6} |  | k rW | |  } n d } | S(   s.   Return string representation of broadcastable.t   scalart   vectort   colt   rowt   matrixRž   (    (   R£   R›   (   t   bt   named_broadcastablet   bcast(    (    s7   /tmp/pip-build-X4mzal/theano/theano/d3viz/formatting.pyt   broadcastable_to_str9  s    

c         C` sZ   i d d 6d d 6d d 6d d 6d	 d
 6d d 6d d 6d d 6} |  | k rR | |  Sd Sd S(   s+   Return character that represents data type.t   ct	   complex64t   zt
   complex128t   ft   float32t   dt   float64R¿   t   int8t   wt   int16t   it   int32t   lt   int64t   XN(    (   RH   t
   dtype_char(    (    s7   /tmp/pip-build-X4mzal/theano/theano/d3viz/formatting.pyt   dtype_to_charG  s    
c         C` s]   t  |  d ƒ s t |  ƒ St |  j ƒ } | d k rF t |  j ƒ } n t |  j ƒ | } | S(   s   Return str of variable type.t   broadcastableRž   (   Rt   RP   RÂ   RÕ   RH   RÔ   (   t   tt   s(    (    s7   /tmp/pip-build-X4mzal/theano/theano/d3viz/formatting.pyRr   X  s    
c         C` s¶   t  ƒ  } x— t |  ƒ D]‰ \ } } | d k	 r t | t ƒ rh d j g  | D] } t | ƒ ^ qJ ƒ } n t | ƒ } t | ƒ } | j d d ƒ } | | | <q q Wt j	 |   } | S(   s   Create pydot node from dict.s   	t   "s   'N(
   t   dictR   RW   R8   RQ   t   joinRP   R¨   R   t   Node(   RÉ   t   et   kR;   RS   t   pynode(    (    s7   /tmp/pip-build-X4mzal/theano/theano/d3viz/formatting.pyRh   d  s    	+(!   Rš   t
   __future__R    R   R   t   numpyR    R®   t	   functoolsR   t   sixR   R   Rn   R   t   theano.compileR   R   t   theano.printingR	   R
   R   R'   t   objectR   Rl   Rs   Rc   Rd   RÂ   RÔ   Rr   Rh   (    (    (    s7   /tmp/pip-build-X4mzal/theano/theano/d3viz/formatting.pyt   <module>   s.   ï							