ó
‡ˆ\c           @   s  d  Z  d d l Z d d l 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
 d „  Z e r… d	 „  Z n e Z e e d ƒ ƒ Z d Z d „  Z d d „ Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e
 f d „  ƒ  YZ d „  Z d S(   s%   Numpy pickle compatibility functions.iÿÿÿÿN(   t   BytesIOi   (   t   PY3_OR_LATER(   t   _ZFILE_PREFIX(   t	   Unpicklerc         C   s   d j  |  ƒ S(   s(   Convert an int to an hexadecimal string.s   {:#x}(   t   format(   t   an_int(    (    sK   lib/python2.7/site-packages/sklearn/externals/joblib/numpy_pickle_compat.pyt   hex_str   s    c         C   s    t  |  t ƒ r |  S|  j d ƒ S(   Nt   latin1(   t
   isinstancet   bytest   encode(   t   s(    (    sK   lib/python2.7/site-packages/sklearn/externals/joblib/numpy_pickle_compat.pyt   asbytes   s    i   i@   i   c         C   s·   |  j  d ƒ t t ƒ t } |  j | ƒ } | t t ƒ } t | d ƒ } |  j d ƒ } | d k rv |  j  | ƒ n  t j |  j ƒ  d | ƒ } t | ƒ | k s³ t d |  ƒ ‚ | S(   sæ   Read the z-file and return the content as a string.

    Z-files are raw data compressed with zlib used internally by joblib
    for persistence. Backward compatibility is not guaranteed. Do not
    use for external purposes.
    i    i   i   t    i   sI   Incorrect data length while decompressing %s.The file could be corrupted.(	   t   seekt   lenR   t   _MAX_LENt   readt   intt   zlibt
   decompresst   AssertionError(   t   file_handlet   header_lengtht   lengtht	   next_bytet   data(    (    sK   lib/python2.7/site-packages/sklearn/externals/joblib/numpy_pickle_compat.pyt
   read_zfile   s    
c         C   s^   |  j  t ƒ t t | ƒ ƒ } |  j  t | j t ƒ ƒ ƒ |  j  t j t | ƒ | ƒ ƒ d S(   sà   Write the data in the given file as a Z-file.

    Z-files are raw data compressed with zlib used internally by joblib
    for persistence. Backward compatibility is not guarantied. Do not
    use for external purposes.
    N(	   t   writeR   R   R   R   t   ljustR   R   t   compress(   R   R   R   R   (    (    sK   lib/python2.7/site-packages/sklearn/externals/joblib/numpy_pickle_compat.pyt   write_zfile>   s    t   NDArrayWrapperc           B   s#   e  Z d  Z e d „ Z d „  Z RS(   s¹   An object to be persisted instead of numpy arrays.

    The only thing this object does, is to carry the filename in which
    the array has been persisted, and the array subclass.
    c         C   s   | |  _  | |  _ | |  _ d S(   s4   Constructor. Store the useful information for later.N(   t   filenamet   subclasst
   allow_mmap(   t   selfR!   R"   R#   (    (    sK   lib/python2.7/site-packages/sklearn/externals/joblib/numpy_pickle_compat.pyt   __init__V   s    		c         C   sÄ   t  j j | j |  j ƒ } t |  d t ƒ } | s9 i  n i | j d 6} | j j	 | |  } t
 | d ƒ r¼ |  j | j j | j j f k r¼ | j j j j |  j d d ƒ } | j | ƒ S| Sd S(   s   Reconstruct the array.R#   t	   mmap_modet   __array_prepare__i    t   bN(   i    (   t   ost   patht   joint   _dirnameR!   t   getattrt   TrueR&   t   npt   loadt   hasattrR"   t   ndarrayt   memmapt   coret
   multiarrayt   _reconstructR'   (   R$   t	   unpicklerR!   R#   t   memmap_kwargst   arrayt	   new_array(    (    sK   lib/python2.7/site-packages/sklearn/externals/joblib/numpy_pickle_compat.pyR   \   s    (   t   __name__t
   __module__t   __doc__R.   R%   R   (    (    (    sK   lib/python2.7/site-packages/sklearn/externals/joblib/numpy_pickle_compat.pyR    O   s   t   ZNDArrayWrapperc           B   s    e  Z d  Z d „  Z d „  Z RS(   sU  An object to be persisted instead of numpy arrays.

    This object store the Zfile filename in which
    the data array has been persisted, and the meta information to
    retrieve it.
    The reason that we store the raw buffer data of the array and
    the meta information, rather than array representation routine
    (tostring) is that it enables us to use completely the strided
    model to avoid memory copies (a and a.T store as fast). In
    addition saving the heavy information separately can avoid
    creating large temporary buffers when unpickling data with
    large arrays.
    c         C   s   | |  _  | |  _ | |  _ d S(   s4   Constructor. Store the useful information for later.N(   R!   t   statet	   init_args(   R$   R!   R@   R?   (    (    sK   lib/python2.7/site-packages/sklearn/externals/joblib/numpy_pickle_compat.pyR%   ‚   s    		c         C   s{   t  j j | j |  j ƒ } | j j j j |  j	 Œ  } t
 | d ƒ  } t | ƒ } Wd QX|  j | f } | j | ƒ | S(   s?   Reconstruct the array from the meta-information and the z-file.t   rbN(   R)   R*   R+   R,   R!   R/   R4   R5   R6   R@   t   openR   R?   t   __setstate__(   R$   R7   R!   R9   t   fR   R?   (    (    sK   lib/python2.7/site-packages/sklearn/externals/joblib/numpy_pickle_compat.pyR   ˆ   s    (   R;   R<   R=   R%   R   (    (    (    sK   lib/python2.7/site-packages/sklearn/externals/joblib/numpy_pickle_compat.pyR>   s   s   	t   ZipNumpyUnpicklerc           B   sb   e  Z d  Z e j j ƒ  Z d d „ Z d „  Z d „  Z	 e
 rS e	 e e j d <n e	 e e j <RS(   s:   A subclass of the Unpickler to unpickle our numpy pickles.c         C   s   t  j j | ƒ |  _ t  j j | ƒ |  _ | |  _ |  j | ƒ |  _ t	 j
 |  |  j ƒ y d d l } Wn t k
 r d } n X| |  _ d S(   s   Constructor.iÿÿÿÿN(   R)   R*   t   basenamet	   _filenamet   dirnameR,   R&   t   _open_pickleR   R   R%   t   numpyt   ImportErrort   NoneR/   (   R$   R!   R   R&   R/   (    (    sK   lib/python2.7/site-packages/sklearn/externals/joblib/numpy_pickle_compat.pyR%   š   s    	
c         C   s   t  t | ƒ ƒ S(   N(   R    R   (   R$   R   (    (    sK   lib/python2.7/site-packages/sklearn/externals/joblib/numpy_pickle_compat.pyRI   §   s    c         C   sv   t  j |  ƒ t |  j d t ƒ rr |  j d k rA t d ƒ ‚ n  |  j j ƒ  } | j	 |  ƒ } |  j j
 | ƒ n  d S(   sâ   Set the state of a newly created object.

        We capture it to replace our place-holder objects,
        NDArrayWrapper, by the array we are interested in. We
        replace them directly in the stack of pickler.
        iÿÿÿÿs@   Trying to unpickle an ndarray, but numpy didn't import correctlyN(   R   t
   load_buildR   t   stackR    R/   RL   RK   t   popR   t   append(   R$   t   nd_array_wrapperR9   (    (    sK   lib/python2.7/site-packages/sklearn/externals/joblib/numpy_pickle_compat.pyRM   ª   s    i    N(   R;   R<   R=   R   t   dispatcht   copyRL   R%   RI   RM   R   t   picklet   BUILD(    (    (    sK   lib/python2.7/site-packages/sklearn/externals/joblib/numpy_pickle_compat.pyRE   •   s   		c      
   C   s£   t  |  d ƒ Ž } t |  d | ƒ} zN y | j ƒ  } Wn7 t k
 rp } t rq t d ƒ } | | _ | ‚ qq n XWd t | d ƒ r” | j j	 ƒ  n  X| SWd QXd S(   s'  Reconstruct a Python object from a file persisted with joblib.dump.

    This function ensures the compatibility with joblib old persistence format
    (<= 0.9.3).

    Parameters
    -----------
    filename: string
        The name of the file from which to load the object

    Returns
    -------
    result: any Python object
        The object stored in the file.

    See Also
    --------
    joblib.dump : function to save an object

    Notes
    -----

    This function can load numpy array files saved separately during the
    dump.
    RA   R   sy   You may be trying to read with python 3 a joblib pickle generated with python 2. This feature is not supported by joblib.N(
   RB   RE   R0   t   UnicodeDecodeErrorR   t
   ValueErrort	   __cause__R1   R   t   close(   R!   R   R7   t   objt   exct   new_exc(    (    sK   lib/python2.7/site-packages/sklearn/externals/joblib/numpy_pickle_compat.pyt   load_compatibilityÁ   s     		l            i   (   R=   RT   R)   R   t   ioR    t   _compatR   t   numpy_pickle_utilsR   R   R   R   t   strR   R   t   _CHUNK_SIZER   R   t   objectR    R>   RE   R]   (    (    (    sK   lib/python2.7/site-packages/sklearn/externals/joblib/numpy_pickle_compat.pyt   <module>   s&   		!$",