a
    ¬ˆcÍ  ã                   @   s8   d dl Zd dlZddgZedd�Zd	dd„Zdd„ ZdS )
é    NÚsave_npzÚload_npzF)Zallow_pickleTc                 C   s´   i }|j dv r"|j|j|jd� nH|j dkr<|j|jd� n.|j dkrZ|j|j|jd� ntd  |j ¡ƒ‚|j|j  d¡|j	|j
d	� |ržtj| fi |¤Ž ntj| fi |¤Ž d
S )aH   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.toarray()
    array([[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.toarray()
    array([[0, 0, 3],
           [4, 0, 0]], dtype=int64)
    ©ZcscZcsrZbsr)ÚindicesÚindptrÚdia)ÚoffsetsÚcoo)ÚrowÚcolz7Save is not implemented for sparse matrix of format {}.Úascii)ÚformatÚshapeÚdataN)r   Úupdater   r   r   r
   r   ÚNotImplementedErrorÚencoder   r   ÚnpZsavez_compressedZsavez)ÚfileZmatrixÚ
compressedZarrays_dict© r   úZ/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/scipy/sparse/_matrix_io.pyr      s     ,



ýc                 C   sš  t j| fi t¤Ž��p}z|d }W n4 tyV } ztd | ¡ƒ|‚W Y d}~n
d}~0 0 | ¡ }t|tƒst| 	d¡}zt
tjd |¡ƒ}W n4 ty¾ } ztd |¡ƒ|‚W Y d}~n
d}~0 0 |dv rø||d |d	 |d
 f|d d�W  d  ƒ S |dk�r,||d |d f|d d�W  d  ƒ S |dk�rh||d |d |d ff|d d�W  d  ƒ S td |¡ƒ‚W d  ƒ n1 �sŒ0    Y  dS )a°   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
    ------
    OSError
        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.toarray()
    array([[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.toarray()
    array([[0, 0, 3],
           [4, 0, 0]], dtype=int64)
    r   z-The file {} does not contain a sparse matrix.Nr   z	{}_matrixzUnknown matrix format "{}"r   r   r   r   r   )r   r   r   r	   r
   r   z7Load is not implemented for sparse matrix of format {}.)r   ÚloadÚPICKLE_KWARGSÚKeyErrorÚ
ValueErrorr   ÚitemÚ
isinstanceÚstrÚdecodeÚgetattrÚscipyÚsparseÚAttributeErrorr   )r   ZloadedZmatrix_formatÚeÚclsr   r   r   r   K   s*    0&

&0
*
2ÿ)T)	Únumpyr   Zscipy.sparser!   Ú__all__Údictr   r   r   r   r   r   r   Ú<module>   s
   

@