Your IP : 216.73.216.61


Current Path : /opt/alt/python35/lib/python3.5/site-packages/joblib/__pycache__/
Upload File :
Current File : //opt/alt/python35/lib/python3.5/site-packages/joblib/__pycache__/numpy_pickle.cpython-35.pyc



��X�Z�
@s�dZddlZddlZddlZddlZyddlmZWnek
rddZYnXddlm	Z	ddlm
Z
ddlmZmZddlm
Z
mZdd	lmZmZdd
lmZddlmZddlmZdd
lmZmZGdd�de�ZGdd�de�ZGdd�de�Zddddd�Zdddd�Zddd�ZdS)zFUtilities for fast persistence of big data, with optional compression.�N)�Path�)�_COMPRESSORS)�BinaryZlibFile)�	Unpickler�Pickler)�_read_fileobject�_write_fileobject)�_read_bytes�BUFFER_SIZE)�load_compatibility)�NDArrayWrapper)�ZNDArrayWrapper)�_basestring�PY3_OR_LATERc@sUeZdZdZddd�Zdd�Zdd�Zd	d
�Zdd�Zd
S)�NumpyArrayWrapperapAn object to be persisted instead of numpy arrays.

    This object is used to hack into the pickle machinery and read numpy
    array data from our custom persistence format.
    More precisely, this object is used for:
    * carrying the information of the persisted array: subclass, shape, order,
    dtype. Those ndarray metadata are used to correctly reconstruct the array
    with low level numpy functions.
    * determining if memmap is allowed on the array.
    * reading the array bytes from a file.
    * reading the array using memorymap from a file.
    * writing the array bytes to a file.

    Attributes
    ----------
    subclass: numpy.ndarray subclass
        Determine the subclass of the wrapped array.
    shape: numpy.ndarray shape
        Determine the shape of the wrapped array.
    order: {'C', 'F'}
        Determine the order of wrapped array data. 'C' is for C order, 'F' is
        for fortran order.
    dtype: numpy.ndarray dtype
        Determine the data type of the wrapped array.
    allow_mmap: bool
        Determine if memory mapping is allowed on the wrapped array.
        Default: False.
    FcCs1||_||_||_||_||_dS)z4Constructor. Store the useful information for later.N)�subclass�shape�order�dtype�
allow_mmap)�selfrrrrr�r�D/opt/alt/python35/lib/python3.5/site-packages/joblib/numpy_pickle.py�__init__@s
				zNumpyArrayWrapper.__init__cCs�td|jd�}|jjr>tj||jdd�nTxQ|jj|dddd	gd
|d|j	�D]}|jj
|jd��qoWd
S)z�Write array bytes to pickler file handle.

        This function is an adaptation of the numpy write_array function
        available in version 1.10.1 in numpy/lib/format.py.
        �i�r�protocol�flags�
external_loop�buffered�zerosize_ok�
buffersizer�CNii)�max�itemsizer�	hasobject�pickle�dump�file_handle�np�nditerr�write�tostring)r�array�picklerr"�chunkrrr�write_arrayHs	zNumpyArrayWrapper.write_arrayc	Cs�t|j�dkrd}n|jjj|j�}|jjrWtj|j	�}nJt
r�|jjj|j	�r�|jj
|j	d|jd|�}n�ttt|jj�}|jj|d|j�}x�td||�D]v}t|||�}t||jj�}t|j	|d�}|jj|d|jd|�||||�<~q�W|jdkr�|jddd�|_|j�}n|j|_|S)	z�Read array from unpickler file handle.

        This function is an adaptation of the numpy read_array function
        available in version 1.10.1 in numpy/lib/format.py.
        rrr�countz
array data�FN���)�lenrr*�multiply�reducerr&r'�loadr)r�compat�	isfileobj�fromfiler�minr%�empty�range�intr
�
frombufferr�	transpose)	r�	unpicklerr2r.�max_read_count�i�
read_count�	read_size�datarrr�
read_array^s2				zNumpyArrayWrapper.read_arraycCs�|jj�}|jdkr'd|_|jj|jd|jd|jd|jd|jd|�}|jj	||j
�|S)z!Read an array using numpy memmap.zw+zr+rrr�mode�offset)r)�tell�	mmap_moder*�memmap�filenamerrr�seek�nbytes)rrBrJ�marrayrrr�	read_mmap�s						zNumpyArrayWrapper.read_mmapcCs�|jdk	r*|jr*|j|�}n|j|�}t|d�r�|j|jj|jjfkr�|jj	j
j|jdd�}|j|�S|SdS)a
Read the array corresponding to this wrapper.

        Use the unpickler to get all information to correctly read the array.

        Parameters
        ----------
        unpickler: NumpyUnpickler

        Returns
        -------
        array: numpy.ndarray

        N�__array_prepare__r�b)r)
rLrrRrH�hasattrrr*�ndarrayrM�core�
multiarray�_reconstructrS)rrBr.�	new_arrayrrr�read�s
zNumpyArrayWrapper.readN)	�__name__�
__module__�__qualname__�__doc__rr1rHrRr[rrrrr"s6rc@sLeZdZdZejj�Zddd�Zdd�Zdd�Z	dS)	�NumpyPicklera�A pickler to persist big data efficiently.

    The main features of this object are:
    * persistence of numpy arrays in a single file.
    * optional compression with a special care on avoiding memory copies.

    Attributes
    ----------
    fp: file
        File object handle used for serializing the input object.
    protocol: int
        Pickle protocol used. Default is pickle.DEFAULT_PROTOCOL under
        python 3, pickle.HIGHEST_PROTOCOL otherwise.
    NcCs�||_t|jt�|_|dkrBtr9tjntj}tj	||jd|�yddl
}Wntk
r�d}YnX||_dS)Nrr)
r)�
isinstancerr rr'�DEFAULT_PROTOCOL�HIGHEST_PROTOCOLrr�numpy�ImportErrorr*)r�fprr*rrrr�s		
zNumpyPickler.__init__cCsg|jjr|jjrdnd}|jo9|jj}tt|�|j||jd|�}|S)z<Create and returns a numpy array wrapper from a numpy array.r3r#r)	r�f_contiguous�c_contiguousr rr&r�typer)rr.rr�wrapperrrr�_create_array_wrapper�s	z"NumpyPickler._create_array_wrappercCs�|jdk	r�t|�|jj|jj|jjfkr�t|�|jjkrf|jj|�}|j|�}tj||�|j	dkr�|j
jdd�|j||�dStj||�S)a�Subclass the Pickler `save` method.

        This is a total abuse of the Pickler class in order to use the numpy
        persistence function `save` instead of the default pickle
        implementation. The numpy array is replaced by a custom wrapper in the
        pickle persistence stack and the serialized array is written right
        after in the file. Warning: the file produced does not follow the
        pickle format. As such it can not be read with `pickle.load`.
        N��forceT)
r*rirV�matrixrM�
asanyarrayrkr�save�proto�framer�commit_framer1)r�objrjrrrrp�s
!	zNumpyPickler.save)
r\r]r^r_r�dispatch�copyrrkrprrrrr`�s
r`c@sgeZdZdZejj�Zddd�Zdd�Ze	rVeee
jd<n
eee
j<dS)�NumpyUnpicklera�A subclass of the Unpickler to unpickle our numpy pickles.

    Attributes
    ----------
    mmap_mode: str
        The memorymap mode to use for reading numpy arrays.
    file_handle: file_like
        File object to unpickle from.
    filename: str
        Name of the file to unpickle from. It should correspond to file_handle.
        This parameter is required when using mmap_mode.
    np: module
        Reference to numpy module if numpy is installed else None.

    NcCs�tjj|�|_||_||_||_d|_tj	||j�yddl
}Wntk
rvd}YnX||_dS)NFr)
�os�path�dirname�_dirnamerLr)rN�compat_moderrrdrer*)rrNr)rLr*rrrr.s				
zNumpyUnpickler.__init__cCs�tj|�t|jdttf�r�|jdkrDtd��|jj�}t|t�rkd|_	|jj
|j|��dS)aOCalled to set the state of a newly created object.

        We capture it to replace our place-holder objects, NDArrayWrapper or
        NumpyArrayWrapper, by the array we are interested in. We
        replace them directly in the stack of pickler.
        NDArrayWrapper is used for backward compatibility with joblib <= 0.9.
        rNz@Trying to unpickle an ndarray, but numpy didn't import correctlyTr4)r�
load_buildra�stackr
rr*re�popr|�appendr[)r�
array_wrapperrrrr}?s
	zNumpyUnpickler.load_buildr)r\r]r^r_rrurvrr}rr'�BUILDrrrrrwsrwc
Cstdk	r't|t�r't|�}t|t�}t|d�}d}|dkr`d}nKt|t�r�t|�dkr�tdj|���|\}}n|}|dk	r�|t	d	�kr�td
j|t
t	d	�����|tkrtdj|t���|r;|r;td|t|�f��|r�t|t�r�|j
d
�rid}nf|j
d�r�d}nN|j
d�r�d}n6|j
d�r�d}n|j
d�r�d}nd}|tkr�|dkr�d}tr)|dkr)tdj|tjdtjd���|dk	rTtjdj|�tdd�|dkr�t|d||f�� }	t|	d|�j|�WdQRXnT|r�t|d�� }	t|	d|�j|�WdQRXnt|d|�j|�|r�dS|gS)a�Persist an arbitrary Python object into one file.

    Parameters
    -----------
    value: any Python object
        The object to store to disk.
    filename: str or pathlib.Path
        The path of the file in which it is to be stored. The compression
        method corresponding to one of the supported filename extensions ('.z',
        '.gz', '.bz2', '.xz' or '.lzma') will be used automatically.
    compress: int from 0 to 9 or bool or 2-tuple, optional
        Optional compression level for the data. 0 or False is no compression.
        Higher value means more compression, but also slower read and
        write times. Using a value of 3 is often a good compromise.
        See the notes for more details.
        If compress is True, the compression level used is 3.
        If compress is a 2-tuple, the first element must correspond to a string
        between supported compressors (e.g 'zlib', 'gzip', 'bz2', 'lzma'
        'xz'), the second element must be an integer from 0 to 9, corresponding
        to the compression level.
    protocol: positive int
        Pickle protocol, see pickle.dump documentation for more details.
    cache_size: positive int, optional
        This option is deprecated in 0.10 and has no effect.

    Returns
    -------
    filenames: list of strings
        The list of file names in which the data is stored. If
        compress is false, each array is stored in a different file.

    See Also
    --------
    joblib.load : corresponding loader

    Notes
    -----
    Memmapping on load cannot be used for compressed files. Thus
    using compression can significantly slow down loading. In
    addition, compressed files take extra extra memory during
    dump and load.

    Nr,�zlibT�rzlCompress argument tuple should contain exactly 2 elements: (compress method, compress level), you passed {0}F�
z?Non valid compress level given: "{0}". Possible values are {1}.zCNon valid compression method given: "{0}". Possible values are {1}.zSSecond argument should be a filename or a file-like object, %s (type %s) was given.z.zz.gz�gzipz.bz2�bz2z.lzma�lzmaz.xz�xzrzR{0} compression is only available for python version >= 3.3. You are using {1}.{2}rzzPlease do not set 'cache_size' in joblib.dump, this parameter has no effect and will be removed. You used 'cache_size={0}'�
stacklevel�compressr�wb)r�r�)rra�strrrU�tupler5�
ValueError�formatr>�listrri�endswithr�NotImplementedError�sys�version_info�warnings�warn�DeprecationWarningr	r`r(�open)
�valuerNr�r�
cache_size�is_filename�
is_fileobj�compress_method�compress_level�frrrr(`sx-							

##r(�cCs�t||d|�}d}y3|j�}|jrMtjd|tdd�WnItk
r�}z)tr�td�}||_	|��WYdd}~XnX|S)zInternal unpickling function.rLNzjThe file '%s' has been generated with a joblib version less than 0.10. Please regenerate this pickle file.r�r�zyYou may be trying to read with python 3 a joblib pickle generated with python 2. This feature is not supported by joblib.)
rwr8r|r�r�r��UnicodeDecodeErrorrr��	__cause__)�fobjrNrLrBrt�exc�new_excrrr�	_unpickle�s 				r�cCs�tdk	r't|t�r't|�}t|d�rpt|d�rpt|d|��}t|�}WdQRXn`t|d��N}t|||��2}t|t�r�t|�St|||�}WdQRXWdQRX|S)aReconstruct a Python object from a file persisted with joblib.dump.

    Parameters
    -----------
    filename: str or pathlib.Path
        The path of the file from which to load the object
    mmap_mode: {None, 'r+', 'r', 'w+', 'c'}, optional
        If not None, the arrays are memory-mapped from the disk. This
        mode has no effect for compressed files. Note that in this
        case the reconstructed object might not longer match exactly
        the originally pickled 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. If the mmap_mode argument is given, it is passed to np.load and
    arrays are loaded as memmaps. As a consequence, the reconstructed
    object might not match the original pickled object. Note that if the
    file was saved with compression, the arrays cannot be memmaped.
    Nr[rOr��rb)	rrar�rUrr�r�rr)rNrLr�rtr�rrrr8s
 r8)r_r'rxr�r��pathlibrre�numpy_pickle_utilsrrrrrr	r
r�numpy_pickle_compatrr
r�_compatrr�objectrr`rwr(r�r8rrrr�<module>s.
�SE�!