ó
àÆ÷Xc           @` s¯   d  d l  m Z m Z m Z d  d l m Z d  d l Z d  d l m Z d Z	 d g Z
 d Z d Z d Z d e f d	 „  ƒ  YZ d
 e f d „  ƒ  YZ d e f d „  ƒ  YZ d S(   i    (   t   absolute_importt   print_functiont   division(   t   ModeN(   t
   hex_digests   Ian Goodfellows   3-clause BSDs   goodfeli@irot   MismatchErrorc           B` s   e  Z d  Z RS(   so   
    Raised by Record.handle_line when the
    current execution doesn't match the replay
    of a record.
    (   t   __name__t
   __module__t   __doc__(    (    (    s3   /tmp/pip-build-X4mzal/theano/theano/tests/record.pyR      s   t   Recordc           B` s)   e  Z d  Z d d e d „ Z d „  Z RS(   sû  
    Records a sequence of strings (from a string buffer). These can then be
    compared to another sequence of strings, and if the two sequences don't
    match a mismatch exception is raised.

    Example:
       # Create a Record object and store 'hello world' inside it
       output = cStringIO.StringIO()
       recorder = Record(file_object=output, replay=False)
       recorder.handle_line('hello world 
')

       # Store the previous output
       output_value = output.getvalue()
       output = cStringIO.StringIO(output_value)

       # Create another Record object, now in playback mode, and set
       # it to the previous sequence of strings
       playback_checker = Record(file_object=output,  replay=True)

       # Check if it matches the previous one
       playback_checker.handle_line('hello world 
')

       # Now check if it the next item matches something else. This will
       # throw an exception because there is no next item
       playback_checker.handle_line('hello new world 
')
    c         C` s   | d k	 s | d k	 s t ‚ | rE | d k rE t | d ƒ |  _ n1 | rm | d k rm t | d ƒ |  _ n	 | |  _ |  j j t ƒ  ƒ d S(   s>  
        Initializes Record object to use file on disc and whether it is in
        replay mode or not.

        Parameters
        ----------
        file_object : StringIO
            The input string buffer.
        file_path : string, optional
            File to save Record to.
        replay : bool, optional
            Determines whether or not the object is in playback mode. If not
            in playback mode, the content of record will be written to the
            file. If in playback mode, the content of file is loaded into the
            record.
        t   rt   wN(   t   Nonet   AssertionErrort   opent   ft   __dict__t   updatet   locals(   t   selft   file_objectt	   file_patht   replay(    (    s3   /tmp/pip-build-X4mzal/theano/theano/tests/record.pyt   __init__1   s    	c         C` s   | j  d ƒ s t ‚ | d  j d ƒ d k s4 t ‚ |  j rì |  j j ƒ  } | | k rü d } | d 7} t | ƒ d k r’ | | d d !d 7} n
 | | 7} | d	 7} t | ƒ d k rÐ | | d d !d 7} n
 | | 7} t | ƒ ‚ qü n |  j j | ƒ d
 S(   so  
        If not in playback mode, it records a new string. If in playback mode,
        it compares the current string to the next element in the sequence.
        If these are identical the element is removed and otherwise a mismatch
        exception is raised.

        Parameters
        ----------
        line : string
            The string to record.
        s   
iþÿÿÿiÿÿÿÿs   Replay detected mismatch.
s    I wanted to write:
id   i    s   ...s   
when previous job wrote:
N(	   t   endswithR   t   findR   R   t   readlinet   lenR   t   write(   R   t   linet   old_linet   msg(    (    s3   /tmp/pip-build-X4mzal/theano/theano/tests/record.pyt   handle_lineN   s     	



N(   R   R   R   R   t   FalseR   R    (    (    (    s3   /tmp/pip-build-X4mzal/theano/theano/tests/record.pyR	      s   t
   RecordModec           B` s#   e  Z d  Z d „  Z d d „ Z RS(   s¼  
    Records all computations done with a function in a file at output_path.
    Writes into the file the index of each apply node and md5 digests of the
    numpy ndarrays it receives as inputs and produces as output.

    Example:
       # We use RecordMode to test that the computation of a function is
       identical. Create a Record object and use it to initialize a
       RecordMode object.
       output = cStringIO.StringIO()
       record = Record(file_object=output, replay=False)
       record_mode = RecordMode(record)

       # Then compile and call the function you wish to test, which uses
       # Apply nodes with record_mode as first parameter to record all the
       # computations to file. For example, call a Theano function with the
       # RecordMode object.
       x = theano.tensor.dscalar()
       f = theano.function([x], 2*x, mode=record_mode)
       print f(4)

       # Create another RecordMode object and initialize it with the previous
       # record.
       output = cStringIO.StringIO(output.getvalue())
       playback = Record(file_object=output, replay=True)
       playback_mode = RecordMode(playback)

       # Compile and call the function to test again with record_mode as
       # first parameter. An exception will be thrown if the recorded
       # computations are not identical between the two runs.
       x = theano.tensor.dscalar()
       f = theano.function([x], 2*x, mode=playback_mode)
       print f(4)

    c         C` s   | |  _  t g  ƒ |  _ d S(   s©   
        Configure object to use an existing Record object.

        Parameters
        ----------
        record : Record
            The Record object to use.
        N(   t   recordt   sett   known_fgraphs(   R   R#   (    (    s3   /tmp/pip-build-X4mzal/theano/theano/tests/record.pyt
   set_record•   s    
	c         ` sÆ   | d k r t |   } n t | j ƒ  ƒ d k s9 t ‚ ˆ j | ƒ ‡ f d †  ‰  ‡  ‡ f d †  } t j j j	 d t
 t j j ƒ ƒ } t j j | g | g ƒ } t t ˆ ƒ j | d d ƒd S(   s  
        Takes either a Record object or the keyword arguments to make one.

        Parameters
        ----------
        record : Record
            The existing Record object to use.
        kwargs : pointer?
            Keyword arguments to construct new object.
        i    c         ` s  y ˆ  j  j |  ƒ Wnò t k
 r} t d ƒ t | ƒ t d t | ƒ d ƒ t d t | ƒ ƒ t d ƒ x' | j D] } t t j j | ƒ ƒ qy Wt d ƒ x? | j	 D]4 } t
 | t ƒ sÈ t ‚ | \ } t t | ƒ ƒ q­ Wt d | j j ƒ t d ƒ ‚ n Xd	 S(
   sµ  
            Records new node computation.

            Parameters
            ----------
            line : string
                Line to record. For example, the function name or node name.
            i : integer
                Node number in the toposort order.
            node : Apply,
                The Apply node which created the entry.
            fn : Function,
                Function related to Apply node.
            s   Got this MismatchError:s   while processing node i=t   :s
   str(node):s   Symbolic inputs: s   str(output) of outputs: s   function name: s&   Non-determinism detected by WrapLinkerN(   R#   R    R   t   printt   strt   inputst   theanot   printingt   min_informative_strt   outputst
   isinstancet   listR   t   fgrapht   name(   R   t   it   nodet   fnt   et   elem(   R   (    s3   /tmp/pip-build-X4mzal/theano/theano/tests/record.pyR    µ   s"    



	c         ` s  | j  } | j d k r' t d ƒ ‚ n  | ˆ j k r¿ t g  ˆ j D] } | j | j k ^ qC ƒ sk t ‚ ˆ j j | ƒ t | j	 ƒ } d | j d t
 | ƒ d } ˆ  | |  | | ƒ n  d | j d } ˆ  | |  | | ƒ d t
 |  ƒ d t
 | ƒ d } ˆ  | |  | | ƒ t g  | j D]' } t | t ƒ oIt | ƒ d	 k ^ q%ƒ s[t ‚ d
 „  } d j g  | j D] } | | ƒ ^ qtƒ }	 d |	 d } ˆ  | |  | | ƒ | ƒ  d j g  | j D] } | | ƒ ^ qÇƒ }
 d |
 d } ˆ  | |  | | ƒ d S(   sX   
            Function called by Apply nodes at the end of each computation?
            s   Un-named functions are not allowed with RecordMode, because they make it impossible to tell if the same function is running during the playback.s	   Function s    has s    apply nodes.
s   Function name: s   
s   Node R'   i   c         S` s   |  d }  t  |  ƒ S(   Ni    (   R   (   t   x(    (    s3   /tmp/pip-build-X4mzal/theano/theano/tests/record.pyt   digestñ   s    
t    s   Inputs: s	   Outputs: N(   R1   R2   R   t
   ValueErrorR%   t   anyR   t   addR   t   apply_nodesR)   t   allR*   R/   R0   t   joinR.   (   R3   R4   R5   R1   R7   t   num_appR   R8   R9   t   inputs_digestt   outputs_digest(   R    R   (    s3   /tmp/pip-build-X4mzal/theano/theano/tests/record.pyt   callbackÖ   s.    	/"C	++t	   use_cloopt	   optimizert   fast_runN(   R   R	   R   t   keysR   R&   R+   t   goft   vmt	   VM_Linkert   boolt   configt   cxxt   WrapLinkerManyt   superR"   R   (   R   R#   t   kwargsRD   t   linkert   wrap_linker(    (   R    R   s3   /tmp/pip-build-X4mzal/theano/theano/tests/record.pyR   ¢   s    !'$N(   R   R   R   R&   R   R   (    (    (    s3   /tmp/pip-build-X4mzal/theano/theano/tests/record.pyR"   p   s   #	(   t
   __future__R    R   R   t   theano.compileR   R+   t   theano.printingR   t   __authors__t   __credits__t   __license__t   __maintainer__t	   __email__t	   ExceptionR   t   objectR	   R"   (    (    (    s3   /tmp/pip-build-X4mzal/theano/theano/tests/record.pyt   <module>   s   	[