B
    &]\O                 @   s  d Z 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
mZ ddlZddlmZ ddlmZmZmZmZmZmZmZ ddlmZmZ dd	lmZ ejd
kZdZdZ dZ!dZ"dZ#dZ$ede de!de"de#de$dddddddgddiZ%eeeee e e!e"e#e$e$dZ&dZ'dZ(dZ)dddd d!d"Z*e'd#e(d$e)d%iZ+G d&d' d'e,Z-G d(d) d)e,Z.G d*d+ d+eZ/d3d-d.Z0G d/d0 d0e,Z1G d1d2 d2e,Z2dS )4z1 Classes for read / write of matlab (TM) 4 files
    )divisionprint_functionabsolute_importN)asbytesasstr)string_types   )MatFileReader	docfillermatdims
read_dtypeconvert_dtypesarr_to_charsarr_dtype_number)squeeze_elementchars_to_strings)reducelittle            f8f4i4i2u2u1header)moptr   )mrowsr   )ncolsr   )imagfr   )namlenr   U1)r   Zc32Zc24Zc16r   Zc8r   r   r   r   S1<>zVAX D-floatzVAX G-floatZCray)r   r   r   r   r   Zdoublecharsparsec               @   s   e Zd ZdZdZdd ZdS )
VarHeader4Fc             C   s"   || _ || _|| _|| _|| _d S )N)namedtypemclassdims
is_complex)selfr+   r,   r-   r.   r/    r1   3lib/python3.7/site-packages/scipy/io/matlab/mio4.py__init__U   s
    zVarHeader4.__init__N)__name__
__module____qualname__Z
is_logicalZ	is_globalr3   r1   r1   r1   r2   r*   P   s   r*   c               @   sT   e Zd ZdZdd Zdd ZdddZdd	d
Zdd Zdd Z	dd Z
dd ZdS )
VarReader4z" Class to read matlab 4 variables c             C   s*   || _ |j| _|j| _|j| _|j| _d S )N)file_reader
mat_streamdtypeschars_as_strings
squeeze_me)r0   r8   r1   r1   r2   r3   e   s
    zVarReader4.__init__c             C   s   t | j| jd }| jt|d d}|d dk sD|d dkrLtdt|d d\}}|d	krzt	d
t
|  t t|d\}}|dkrtdt|d\}}|}|d |d f}|d dk}	| j| }
t||
|||	S )z% Read and return header for variable r   r#       r   r   i  z.Mat 4 mopt wrong format, byteswapping problem?i  )r   r   zBWe do not support byte ordering '%s'; returned data may be corruptd   z,O in MOPT integer should be 0, wrong format?
   r    r!   r"   r   )r   r9   r:   readintstrip
ValueErrordivmodwarningswarnorder_codesUserWarningr*   )r0   datar+   MrestOPTr.   r/   r,   r1   r1   r2   read_headerl   s.    
zVarReader4.read_headerTc             C   st   |j }|tkr| |}nD|tkr@| |}|r^| jr^t|}n|tkrR| |S t	d| |rp| j
rpt|S |S )NzNo reader for class code %s)r-   mxFULL_CLASSread_full_arraymxCHAR_CLASSread_char_arrayr;   r   mxSPARSE_CLASSread_sparse_array	TypeErrorr<   r   )r0   hdrprocessr-   arrr1   r1   r2   array_from_header   s    




zVarReader4.array_from_headerc       	      C   st   |j }|j}|j}x|D ]}||9 }qW | jt|}t||krRtd|j t	j
|||dd}|rp| }|S )a   Mat4 read using header `hdr` dtype and dims

        Parameters
        ----------
        hdr : object
           object with attributes ``dtype``, ``dims``.  dtype is assumed to be
           the correct endianness
        copy : bool, optional
           copies array before return if True (default True)
           (buffer is usually read only)

        Returns
        -------
        arr : ndarray
            of dtype givem by `hdr` ``dtype`` and shape givem by `hdr` ``dims``
        zNot enough bytes to read matrix '%s'; is this a badly-formed file? Consider listing matrices with `whosmat` and loading named matrices with `variable_names` kwarg to `loadmat`F)shaper,   bufferorder)r,   r.   itemsizer9   r@   rA   lenrC   r+   npndarraycopy)	r0   rW   rc   dtr.   Z	num_bytesdr]   rY   r1   r1   r2   read_sub_array   s     

zVarReader4.read_sub_arrayc             C   s8   |j r.| j|dd}| j|dd}||d  S | |S )aM   Full (rather than sparse) matrix getter

        Read matrix (array) can be real or complex

        Parameters
        ----------
        hdr : ``VarHeader4`` instance

        Returns
        -------
        arr : ndarray
            complex array if ``hdr.is_complex`` is True, otherwise a real
            numeric array
        F)rc   y              ?)r/   rf   )r0   rW   resZres_jr1   r1   r2   rQ      s
    zVarReader4.read_full_arrayc             C   sB   |  |tj}| d}tj|jtdt	|d
 S )z latin-1 text matrix (char matrix) reader

        Parameters
        ----------
        hdr : ``VarHeader4`` instance

        Returns
        -------
        arr : ndarray
            with dtype 'U1', shape given by `hdr` ``dims``
        zlatin-1r$   )r\   r,   r]   )rf   astypera   Zuint8tostringdecoderb   r.   r,   Zarrayrc   )r0   rW   rY   Sr1   r1   r2   rS      s
    zVarReader4.read_char_arrayc             C   s   |  |}|ddddf }t|d t|d f}tj|dddf dd}tj|dddf dd}|d8 }|d8 }|jd d	krtj|ddd
f dd}n,tj|ddd
f dd}|ddd	f |_tj|||ff|S )a   Read and return sparse matrix type

        Parameters
        ----------
        hdr : ``VarHeader4`` instance

        Returns
        -------
        arr : ``scipy.sparse.coo_matrix``
            with dtype ``float`` and shape read from the sparse matrix data

        Notes
        -----
        MATLAB 4 real sparse arrays are saved in a N+1 by 3 array format, where
        N is the number of non-zero values.  Column 1 values [0:N] are the
        (1-based) row indices of the each non-zero value, column 2 [0:N] are the
        column indices, column 3 [0:N] are the (real) values.  The last values
        [-1,0:2] of the rows, column indices are shape[0] and shape[1]
        respectively of the output matrix. The last value for the values column
        is a padding 0. mrows and ncols values from the header give the shape of
        the stored matrix, here [N+1, 3].  Complex data is saved as a 4 column
        matrix, where the fourth column contains the imaginary component; the
        last value is again 0.  Complex sparse data do *not* have the header
        ``imagf`` field set to True; the fact that the data are complex is only
        detectable because there are 4 storage columns
        N)rl   r   )rl   r   r   Zintc)r,   r   r   r   floatcomplex)	rf   rA   ra   Zascontiguousarrayr\   imagscipyr)   Z
coo_matrix)r0   rW   rg   Ztmpr.   IJVr1   r1   r2   rU      s    
zVarReader4.read_sparse_arrayc             C   s2  |j }|tkr ttt|j}n|tkrLttt|j}| jrJ|dd }n|tkr|j	}|j}t
|dkr|d dkr|d dksdS | j|j|d d  d tjd|| j|jd}| j|j|d d  d tjd|| j|jd}t|t|f}ntd	| | jr.td
d |D }|S )zwRead the shape of the array described by the header.
        The file position after this call is unspecified.
        Nrl   r   r   r   r1   )r   )r\   r,   r]   zNo reader for class code %sc             S   s   g | ]}|d kr|qS )r   r1   ).0xr1   r1   r2   
<listcomp>,  s    z0VarReader4.shape_from_header.<locals>.<listcomp>)r-   rP   tuplemaprA   r.   rR   r;   rT   r,   r`   r9   seekr_   ra   rb   r@   rV   r<   )r0   rW   r-   r\   rd   r.   ZrowsZcolsr1   r1   r2   shape_from_header  s.    
$zVarReader4.shape_from_headerN)T)T)r4   r5   r6   __doc__r3   rO   rZ   rf   rQ   rS   rU   rz   r1   r1   r1   r2   r7   b   s   

$*r7   c                   sX   e Zd ZdZe fddZdd Zdd Zdd	 ZdddZ	dddZ
dd Z  ZS )MatFile4Readerz Reader for Mat4 files c                s"   t t| j|f|| d| _dS )zR Initialize matlab 4 file reader

    %(matstream_arg)s
    %(load_args)s
        N)superr|   r3   _matrix_reader)r0   r9   argskwargs)	__class__r1   r2   r3   2  s    zMatFile4Reader.__init__c             C   s^   | j d t| j td}| j d |dkr6dS |dk sF|dkrRtrNdpPdS trZdp\dS )Nr   r   r&   i  r'   )r9   ry   r   ra   r,   SYS_LITTLE_ENDIAN)r0   r   r1   r1   r2   guess_byte_order<  s    zMatFile4Reader.guess_byte_orderc             C   s   t t| j| _t| | _dS )za Run when beginning read of variables

        Sets up readers from parameters in `self`
        N)r   mdtypes_templateZ
byte_orderr:   r7   r~   )r0   r1   r1   r2   initialize_readH  s    zMatFile4Reader.initialize_readc             C   sV   | j  }tdd |jd}|jj| }|jr@|jtks@|d9 }| j	
 | }||fS )ab   Read and return header, next position

        Parameters
        ----------
        None

        Returns
        -------
        header : object
           object that can be passed to self.read_var_array, and that
           has attributes ``name`` and ``is_global``
        next_position : int
           position in stream of next variable
        c             S   s   | | S )Nr1   )ru   yr1   r1   r2   <lambda>`  s    z0MatFile4Reader.read_var_header.<locals>.<lambda>r   r   )r~   rO   r   r.   r,   r_   r/   r-   rT   r9   tell)r0   rW   nZremaining_bytesnext_positionr1   r1   r2   read_var_headerP  s    
zMatFile4Reader.read_var_headerTc             C   s   | j ||S )a   Read array, given `header`

        Parameters
        ----------
        header : header object
           object with fields defining variable header
        process : {True, False}, optional
           If True, apply recursive post-processing during loading of array.

        Returns
        -------
        arr : array
           array with post-processing applied or not according to
           `process`.
        )r~   rZ   )r0   r   rX   r1   r1   r2   read_var_arrayg  s    zMatFile4Reader.read_var_arrayNc             C   s   t |tr|g}n|dk	r"t|}| jd |   i }xz|  s|  \}}t|j	}|dk	rx||krx| j| q<| 
|||< | j| |dk	r<|| t|dkr<P q<W |S )a,   get variables from stream as dictionary

        Parameters
        ----------
        variable_names : None or str or sequence of str, optional
            variable name, or sequence of variable names to get from Mat file /
            file stream.  If None, then get all variables in file
        Nr   )
isinstancer   listr9   ry   r   end_of_streamr   r   r+   r   remover`   )r0   Zvariable_namesmdictrW   r   r+   r1   r1   r2   get_variablesy  s(    	



zMatFile4Reader.get_variablesc             C   sv   | j d |   g }xX|  sp|  \}}t|j}| j|}t	
|jd}||||f | j | qW |S )z list variables from stream r   unknown)r9   ry   r   r   r   r   r+   r~   rz   mclass_infogetr-   append)r0   varsrW   r   r+   r\   infor1   r1   r2   list_variables  s    

zMatFile4Reader.list_variables)T)N)r4   r5   r6   r{   r
   r3   r   r   r   r   r   r   __classcell__r1   r1   )r   r2   r|   0  s   


r|   rowc             C   s(   t | |}t|dkrtd| |S )a   Make ``arr`` exactly two dimensional

    If `arr` has more than 2 dimensions, raise a ValueError

    Parameters
    ----------
    arr : array
    oned_as : {'row', 'column'}, optional
       Whether to reshape 1D vectors as row vectors or column vectors.
       See documentation for ``matdims`` for more detail

    Returns
    -------
    arr2d : array
       2D version of the array
    r   z=Matlab 4 files cannot save arrays with more than 2 dimensions)r   r`   rC   Zreshape)rY   oned_asr.   r1   r1   r2   	arr_to_2d  s    
r   c               @   sT   e Zd Zdd Zdd Zdd Zeedfdd	Zd
d Z	dd Z
dd Zdd ZdS )
VarWriter4c             C   s   |j | _ |j| _d S )N)file_streamr   )r0   Zfile_writerr1   r1   r2   r3     s    zVarWriter4.__init__c             C   s   | j |jdd d S )Nr[   )r^   )r   writeri   )r0   rY   r1   r1   r2   write_bytes  s    zVarWriter4.write_bytesc             C   s   | j | d S )N)r   r   )r0   sr1   r1   r2   write_string  s    zVarWriter4.write_stringr   c       	      C   s   t dtd }t }d}|d |d  |d  | |d< |d |d< |d	 |d
< ||d< t|d	 |d< | | | t|d  dS )a(   Write header for given data options

        Parameters
        ----------
        name : str
            name of variable
        shape : sequence
           Shape of array as it will be read in matlab
        P : int, optional
            code for mat4 data type, one of ``miDOUBLE, miSINGLE, miINT32,
            miINT16, miUINT16, miUINT8``
        T : int, optional
            code for mat4 matrix class, one of ``mxFULL_CLASS, mxCHAR_CLASS,
            mxSPARSE_CLASS``
        imagf : int, optional
            flag indicating complex
        r1   r   r   i  r>   r?   r   r    r   r!   r"   r#    N)ra   emptyr   r   r`   r   r   r   )	r0   r+   r\   rM   rN   r"   r   rJ   rL   r1   r1   r2   write_header  s    

zVarWriter4.write_headerc             C   s   t j|r| || dS t|}|j}|jsB||	d}|j
}|tjkr\tdn4|tjkrptdn |tjtjfkr| || dS | || dS )z Write matrix `arr`, with name `name`

        Parameters
        ----------
        arr : array_like
           array to write
        name : str
           name in matlab workspace
        N=z!Cannot save object arrays in Mat4zCannot save void type arrays)rp   r)   Zissparsewrite_sparsera   Zasarrayr,   Zisnativerh   ZnewbyteordertypeZobject_rV   ZvoidZunicode_Zstring_
write_charwrite_numeric)r0   rY   r+   rd   Zdttr1   r1   r2   r     s     




zVarWriter4.writec             C   s   t || j}|jjdk}yt|jjdd   }W n2 tk
rb   |rP|d}n
|d}t}Y nX | j	||j
|t|d |r| |j | |j n
| | d S )Ncr   Zc128r   )rM   rN   r"   )r   r   r,   kindnp_to_mtypesstrKeyErrorrh   miDOUBLEr   r\   rP   r   realro   )r0   rY   r+   r"   rM   r1   r1   r2   r   	  s$    

zVarWriter4.write_numericc             C   s   t |}t|| j}|j}| j||ttd |jjdkrvt	
|}t	jdt|||d}| d}t	j|d|d}| | d S )N)rM   rN   Ur1   )r\   r,   r]   zlatin-1r%   )r   r   r   r\   r   miUINT8rR   r,   r   ra   productrb   r   itemencoder   )r0   rY   r+   r.   Zn_charsZst_arrstr1   r1   r2   r     s     
zVarWriter4.write_charc             C   s   |  }|jjdk}tj|jd d| fdd}|j|dddf< |j|dddf< |dddd	f  d7  < |r|jj	|ddd	f< |jj
|dddf< n|j|ddd	f< |j|ddd	f< | j||jttd
 | | dS )zX Sparse matrices are 2D

        See docstring for VarReader4.read_sparse_array
        r   r   r   r   )r,   Nrl   r   r   )rM   rN   )Ztocoor,   r   ra   ZzerosZnnzr   colrI   r   ro   r\   r   r   rT   r   )r0   rY   r+   Ar"   Zijvr1   r1   r2   r   2  s"    zVarWriter4.write_sparseN)r4   r5   r6   r3   r   r   r   rP   r   r   r   r   r   r1   r1   r1   r2   r     s    r   c               @   s$   e Zd ZdZdddZdddZdS )	MatFile4Writerz) Class for writing matlab 4 format files Nc             C   s"   || _ |d krd}|| _d | _d S )Nr   )r   r   _matrix_writer)r0   r   r   r1   r1   r2   r3   M  s
    zMatFile4Writer.__init__c             C   s2   t | | _x"| D ]\}}| j|| qW dS )a   Write variables in `mdict` to stream

        Parameters
        ----------
        mdict : mapping
           mapping with method ``items`` return name, contents pairs
           where ``name`` which will appeak in the matlab workspace in
           file load, and ``contents`` is something writeable to a
           matlab file, such as a numpy array.
        write_header : {None, True, False}
           If True, then write the matlab file header before writing the
           variables.  If None (the default) then write the file header
           if we are at position 0 in the stream.  By setting False
           here, and setting the stream position to the end of the file,
           you can append variables to a matlab file
        N)r   r   itemsr   )r0   r   r   r+   varr1   r1   r2   put_variablesT  s    
zMatFile4Writer.put_variables)N)N)r4   r5   r6   r{   r3   r   r1   r1   r1   r2   r   K  s   
r   )r   )3r{   Z
__future__r   r   r   sysrE   Znumpyra   Znumpy.compatr   r   Zscipy.sparserp   Zscipy._lib.sixr   Zmiobaser	   r
   r   r   r   r   r   Z	mio_utilsr   r   	functoolsr   	byteorderr   r   ZmiSINGLEZmiINT32ZmiINT16ZmiUINT16r   r   r   rP   rR   rT   rG   r   objectr*   r7   r|   r   r   r   r1   r1   r1   r2   <module>   sv   $
 Oy
 