ó
î&]\c           @` s¥   d  d l  m Z m Z m Z d  d l Z d  d l Z d  d l Z d  d l	 m
 Z
 d d g Z e
 e j ƒ d k rƒ e d e ƒ Z n	 e ƒ  Z e d „ Z d	 „  Z d S(
   i    (   t   divisiont   print_functiont   absolute_importN(   t   NumpyVersiont   save_npzt   load_npzs   1.10.0t   allow_picklec         C` sú   i  } | j  d k r4 | j d | j d | j ƒ nk | j  d k rY | j d | j ƒ nF | j  d k r‡ | j d	 | j d
 | j ƒ n t d j  | j  ƒ ƒ ‚ | j d | j  j d ƒ d | j	 d | j
 ƒ | ræ t j |  |  n t j |  |  d S(   sL   Save a sparse matrix to a file using ``.npz`` format.

    Parameters
    ----------
    file : str or file-like object
        Either the file name (string) or an open file (file-like object)
        where the data will be saved. If file is a string, the ``.npz``
        extension will be appended to the file name if it is not already
        there.
    matrix: spmatrix (format: ``csc``, ``csr``, ``bsr``, ``dia`` or coo``)
        The sparse matrix to save.
    compressed : bool, optional
        Allow compressing the file. Default: True

    See Also
    --------
    scipy.sparse.load_npz: Load a sparse matrix from a file using ``.npz`` format.
    numpy.savez: Save several arrays into a ``.npz`` archive.
    numpy.savez_compressed : Save several arrays into a compressed ``.npz`` archive.

    Examples
    --------
    Store sparse matrix to disk, and load it again:

    >>> import scipy.sparse
    >>> sparse_matrix = scipy.sparse.csc_matrix(np.array([[0, 0, 3], [4, 0, 0]]))
    >>> sparse_matrix
    <2x3 sparse matrix of type '<class 'numpy.int64'>'
       with 2 stored elements in Compressed Sparse Column format>
    >>> sparse_matrix.todense()
    matrix([[0, 0, 3],
            [4, 0, 0]], dtype=int64)

    >>> scipy.sparse.save_npz('/tmp/sparse_matrix.npz', sparse_matrix)
    >>> sparse_matrix = scipy.sparse.load_npz('/tmp/sparse_matrix.npz')

    >>> sparse_matrix
    <2x3 sparse matrix of type '<class 'numpy.int64'>'
       with 2 stored elements in Compressed Sparse Column format>
    >>> sparse_matrix.todense()
    matrix([[0, 0, 3],
            [4, 0, 0]], dtype=int64)
    t   csct   csrt   bsrt   indicest   indptrt   diat   offsetst   coot   rowt   cols7   Save is not implemented for sparse matrix of format {}.t   formatt   asciit   shapet   dataN(   R   R   R	   (   R   t   updateR
   R   R   R   R   t   NotImplementedErrort   encodeR   R   t   npt   savez_compressedt   savez(   t   filet   matrixt
   compressedt   arrays_dict(    (    s6   lib/python2.7/site-packages/scipy/sparse/_matrix_io.pyR      s    ,		
c      	   C` s  t  j |  t  w} y | d } Wn& t k
 rK t d j |  ƒ ƒ ‚ n X| j ƒ  } t j d d k r t	 | t
 ƒ r | j d ƒ } n  y t t j d j | ƒ ƒ } Wn& t k
 rÔ t d j | ƒ ƒ ‚ n X| d k r
| | d | d | d f d | d ƒS| d k r8| | d | d f d | d ƒS| d k rp| | d | d | d f f d | d ƒSt d j | ƒ ƒ ‚ Wd QXd S(   s´   Load a sparse matrix from a file using ``.npz`` format.

    Parameters
    ----------
    file : str or file-like object
        Either the file name (string) or an open file (file-like object)
        where the data will be loaded.

    Returns
    -------
    result : csc_matrix, csr_matrix, bsr_matrix, dia_matrix or coo_matrix
        A sparse matrix containing the loaded data.

    Raises
    ------
    IOError
        If the input file does not exist or cannot be read.

    See Also
    --------
    scipy.sparse.save_npz: Save a sparse matrix to a file using ``.npz`` format.
    numpy.load: Load several arrays from a ``.npz`` archive.

    Examples
    --------
    Store sparse matrix to disk, and load it again:

    >>> import scipy.sparse
    >>> sparse_matrix = scipy.sparse.csc_matrix(np.array([[0, 0, 3], [4, 0, 0]]))
    >>> sparse_matrix
    <2x3 sparse matrix of type '<class 'numpy.int64'>'
       with 2 stored elements in Compressed Sparse Column format>
    >>> sparse_matrix.todense()
    matrix([[0, 0, 3],
            [4, 0, 0]], dtype=int64)

    >>> scipy.sparse.save_npz('/tmp/sparse_matrix.npz', sparse_matrix)
    >>> sparse_matrix = scipy.sparse.load_npz('/tmp/sparse_matrix.npz')

    >>> sparse_matrix
    <2x3 sparse matrix of type '<class 'numpy.int64'>'
        with 2 stored elements in Compressed Sparse Column format>
    >>> sparse_matrix.todense()
    matrix([[0, 0, 3],
            [4, 0, 0]], dtype=int64)
    R   s-   The file {} does not contain a sparse matrix.i    i   R   s	   {}_matrixs   Unknown matrix format "{}"R   R   R	   R   R
   R   R   R   R   R   R   R   s7   Load is not implemented for sparse matrix of format {}.N(   R   R   R	   (   R   t   loadt   PICKLE_KWARGSt   KeyErrort
   ValueErrorR   t   itemt   syst   version_infot
   isinstancet   strt   decodet   getattrt   scipyt   sparset   AttributeErrorR   (   R   t   loadedt   matrix_formatt   cls(    (    s6   lib/python2.7/site-packages/scipy/sparse/_matrix_io.pyR   S   s(    0#)",	(   t
   __future__R    R   R   R$   t   numpyR   t   scipy.sparseR*   t   scipy._lib._versionR   t   __all__t   __version__t   dictt   FalseR    t   TrueR   R   (    (    (    s6   lib/python2.7/site-packages/scipy/sparse/_matrix_io.pyt   <module>   s   	@