
˽Xc           @` sy  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$ i d e 6d e  6d e! 6d e" 6d e# 6d e$ 6d5 d6 d7 d8 d9 g d 6d d 6Z% i e d 6e d 6e d 6e d 6e  d 6e  d 6e! d 6e" d 6e# d 6e$ d 6e$ d  6Z& d Z' d Z( d Z) i d! d 6d" d 6d# d 6d$ d 6d% d 6Z* i d& e' 6d' e( 6d( e) 6Z+ d) e, f d*     YZ- d+ e, f d,     YZ. d- e f d.     YZ/ d/ d0  Z0 d1 e, f d2     YZ1 d3 e, f d4     YZ2 d S(:   s1    Classes for read / write of matlab (TM) 4 files
i    (   t   divisiont   print_functiont   absolute_importN(   t   asbytest   asstr(   t   string_typesi   (   t   MatFileReadert	   docfillert   matdimst
   read_dtypet   convert_dtypest   arr_to_charst   arr_dtype_number(   t   squeeze_elementt   chars_to_strings(   t   reducet   littlei   i   i   i   t   f8t   f4t   i4t   i2t   u2t   u1t   moptt   mrowst   ncolst   imagft   namlent   headert   U1t   c32t   c24t   c16t   c8t   S1t   <t   >s   VAX D-floats   VAX G-floatt   Crayt   doublet   chart   sparset
   VarHeader4c           B` s   e  Z e Z e Z d    Z RS(   c         C` s1   | |  _  | |  _ | |  _ | |  _ | |  _ d  S(   N(   t   namet   dtypet   mclasst   dimst
   is_complex(   t   selfR*   R+   R,   R-   R.   (    (    s3   /tmp/pip-build-7oUkmx/scipy/scipy/io/matlab/mio4.pyt   __init__U   s
    				(   t   __name__t
   __module__t   Falset
   is_logicalt	   is_globalR0   (    (    (    s3   /tmp/pip-build-7oUkmx/scipy/scipy/io/matlab/mio4.pyR)   P   s   t
   VarReader4c           B` s\   e  Z d  Z d   Z d   Z e d  Z e d  Z d   Z d   Z	 d   Z
 d   Z RS(	   s"    Class to read matlab 4 variables c         C` s=   | |  _  | j |  _ | j |  _ | j |  _ | j |  _ d  S(   N(   t   file_readert
   mat_streamt   dtypest   chars_as_stringst
   squeeze_me(   R/   R7   (    (    s3   /tmp/pip-build-7oUkmx/scipy/scipy/io/matlab/mio4.pyR0   e   s
    	c         C` s?  t  |  j |  j d  } |  j j t | d   j d  } | d d k  s^ | d d k rm t d   n  t | d d  \ } } | d k r t j	 d
 t
 | t  n  t | d  \ } } | d k r t d   n  t | d  \ } } | } | d | d f } | d d	 k }	 |  j | }
 t | |
 | | |	  S(   s%    Read and return header for variable R   R   t    R   i    i  s.   Mat 4 mopt wrong format, byteswapping problem?i  i   sB   We do not support byte ordering '%s'; returned data may be corruptid   s,   O in MOPT integer should be 0, wrong format?i
   R   R   R   (   i    i   (   R	   R8   R9   t   readt   intt   stript
   ValueErrort   divmodt   warningst   warnt   order_codest   UserWarningR)   (   R/   t   dataR*   t   Mt   restt   Ot   Pt   TR-   R.   R+   (    (    s3   /tmp/pip-build-7oUkmx/scipy/scipy/io/matlab/mio4.pyt   read_headerl   s.    % 	
c         C` s   | j  } | t k r' |  j |  } ne | t k rc |  j |  } | r |  j r t |  } q n) | t k r| |  j |  St	 d |   | r |  j
 r t |  S| S(   Ns   No reader for class code %s(   R,   t   mxFULL_CLASSt   read_full_arrayt   mxCHAR_CLASSt   read_char_arrayR:   R   t   mxSPARSE_CLASSt   read_sparse_arrayt	   TypeErrorR;   R   (   R/   t   hdrt   processR,   t   arr(    (    s3   /tmp/pip-build-7oUkmx/scipy/scipy/io/matlab/mio4.pyt   array_from_header   s    	
c   	   	   C` s   | j  } | j } | j } x | D] } | | 9} q" W|  j j t |   } t |  | k rv t d | j   n  t	 j
 d | d | d | d d  } | r | j   } n  | S(   s   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``
        s   Not 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`t   shapeR+   t   buffert   ordert   F(   R+   R-   t   itemsizeR8   R=   R>   t   lenR@   R*   t   npt   ndarrayt   copy(	   R/   RT   R`   t   dtR-   t	   num_bytest   dRY   RV   (    (    s3   /tmp/pip-build-7oUkmx/scipy/scipy/io/matlab/mio4.pyt   read_sub_array   s     				c         C` sL   | j  r? |  j | d t } |  j | d t } | | d S|  j |  S(   sM   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
        R`   y              ?(   R.   Rd   R3   (   R/   RT   t   rest   res_j(    (    s3   /tmp/pip-build-7oUkmx/scipy/scipy/io/matlab/mio4.pyRN      s
    	c         C` sg   |  j  |  j t j  } | j   j d  } t j d | j d t j d  d t j	 |   j
   S(   s    latin-1 text matrix (char matrix) reader

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

        Returns
        -------
        arr : ndarray
            with dtype 'U1', shape given by `hdr` ``dims``
        s   latin-1RX   R+   R   RY   (   Rd   t   astypeR^   t   uint8t   tostringt   decodeR_   R-   R+   t   arrayR`   (   R/   RT   RV   t   S(    (    s3   /tmp/pip-build-7oUkmx/scipy/scipy/io/matlab/mio4.pyRP      s
    c         C` s7  |  j  |  } | d d  d d  f } | d d 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 r t j | d d  d f d d	 } n> t j | d d  d f d d
 } | d d  d f | _ t j j | | | f f |  S(   s   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
        Nii    i   R+   t   intci   i   t   floatt   complex(   Rd   R^   t   ascontiguousarrayRX   t   imagt   scipyR(   t
   coo_matrix(   R/   RT   Re   t   tmpR-   t   It   Jt   V(    (    s3   /tmp/pip-build-7oUkmx/scipy/scipy/io/matlab/mio4.pyRR      s    %%

(%c   	   
   C` s  | j  } | t k r0 t t t | j   } nZ| t k rm t t t | j   } |  j r| d  } qn| t k rz| j	 } | j } t
 |  d k o | d d k o | d d k s d	 S|  j j | j | d d d  t j d d
 d | d |  j j | j   } |  j j | j | d d d  t j d d d | d |  j j | j   } t |  t |  f } n t d |   |  j rt g  | D] } | d k r| ^ q } n  | S(   sw   Read the shape of the array described by the header.
        The file position after this call is unspecified.
        ii   i    i   RX   R+   RY   s   No reader for class code %s(    (   i   (   i   (   R,   RM   t   tuplet   mapR>   R-   RO   R:   RQ   R+   R]   R8   t   seekR\   R^   R_   R=   RS   R;   (	   R/   RT   R,   RX   Ra   R-   t   rowst   colst   x(    (    s3   /tmp/pip-build-7oUkmx/scipy/scipy/io/matlab/mio4.pyt   shape_from_header  s.    				2""	.(   R1   R2   t   __doc__R0   RL   t   TrueRW   Rd   RN   RP   RR   R~   (    (    (    s3   /tmp/pip-build-7oUkmx/scipy/scipy/io/matlab/mio4.pyR6   b   s   		$			)t   MatFile4Readerc           B` sY   e  Z d  Z e d    Z d   Z d   Z d   Z e d  Z	 d d  Z d   Z RS(	   s    Reader for Mat4 files c         O` s)   t  t |   j | | |  d |  _ d S(   sR    Initialize matlab 4 file reader

    %(matstream_arg)s
    %(load_args)s
        N(   t   superR   R0   t   Nonet   _matrix_reader(   R/   R8   t   argst   kwargs(    (    s3   /tmp/pip-build-7oUkmx/scipy/scipy/io/matlab/mio4.pyR0   1  s    c         C` s   |  j  j d  t |  j  t j d   } |  j  j d  | d k rK d S| d k  sc | d k rs t ro d pr d St r d p d S(   Ni    R   R#   i  R$   (   R8   Rz   R	   R^   R+   t   SYS_LITTLE_ENDIAN(   R/   R   (    (    s3   /tmp/pip-build-7oUkmx/scipy/scipy/io/matlab/mio4.pyt   guess_byte_order;  s    c         C` s(   t  t |  j  |  _ t |   |  _ d S(   sa    Run when beginning read of variables

        Sets up readers from parameters in `self`
        N(   R
   t   mdtypes_templatet
   byte_orderR9   R6   R   (   R/   (    (    s3   /tmp/pip-build-7oUkmx/scipy/scipy/io/matlab/mio4.pyt   initialize_readG  s    c         C` sz   |  j  j   } t d   | j d  } | j j | } | j r] | j t k r] | d 9} n  |  j	 j
   | } | | f S(   sb   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(   N(    (   R}   t   y(    (    s3   /tmp/pip-build-7oUkmx/scipy/scipy/io/matlab/mio4.pyt   <lambda>_  s    i   i   (   R   RL   R   R-   R+   R\   R.   R,   RQ   R8   t   tell(   R/   RT   t   nt   remaining_bytest   next_position(    (    s3   /tmp/pip-build-7oUkmx/scipy/scipy/io/matlab/mio4.pyt   read_var_headerO  s    c         C` s   |  j  j | |  S(   s   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   RW   (   R/   R   RU   (    (    s3   /tmp/pip-build-7oUkmx/scipy/scipy/io/matlab/mio4.pyt   read_var_arrayf  s    c         C` s  t  | t  r | g } n | d k	 r6 t |  } n  |  j j d  |  j   i  } x |  j   s|  j   \ } } t	 | j
  } | d k	 r | | k r |  j j |  qY n  |  j |  | | <|  j j |  | d k	 rY | j |  t |  d k r	Pq	qY qY W| S(   s,   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
        i    N(   t
   isinstanceR   R   t   listR8   Rz   R   t   end_of_streamR   R   R*   R   t   removeR]   (   R/   t   variable_namest   mdictRT   R   R*   (    (    s3   /tmp/pip-build-7oUkmx/scipy/scipy/io/matlab/mio4.pyt   get_variablesx  s(    	
c         C` s   |  j  j d  |  j   g  } x~ |  j   s |  j   \ } } t | j  } |  j j |  } t	 j
 | j d  } | j | | | f  |  j  j |  q# W| S(   s    list variables from stream i    t   unknown(   R8   Rz   R   R   R   R   R*   R   R~   t   mclass_infot   getR,   t   append(   R/   t   varsRT   R   R*   RX   t   info(    (    s3   /tmp/pip-build-7oUkmx/scipy/scipy/io/matlab/mio4.pyt   list_variables  s    
N(   R1   R2   R   R   R0   R   R   R   R   R   R   R   R   (    (    (    s3   /tmp/pip-build-7oUkmx/scipy/scipy/io/matlab/mio4.pyR   /  s   
			t   rowc         C` s=   t  |  |  } t |  d k r0 t d   n  |  j |  S(   s   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
    i   s=   Matlab 4 files cannot save arrays with more than 2 dimensions(   R   R]   R@   t   reshape(   RV   t   oned_asR-   (    (    s3   /tmp/pip-build-7oUkmx/scipy/scipy/io/matlab/mio4.pyt	   arr_to_2d  s    t
   VarWriter4c           B` sY   e  Z d    Z d   Z d   Z e e d d  Z d   Z d   Z	 d   Z
 d   Z RS(	   c         C` s   | j  |  _  | j |  _ d  S(   N(   t   file_streamR   (   R/   t   file_writer(    (    s3   /tmp/pip-build-7oUkmx/scipy/scipy/io/matlab/mio4.pyR0     s    c         C` s    |  j  j | j d d   d  S(   NRZ   R[   (   R   t   writeRi   (   R/   RV   (    (    s3   /tmp/pip-build-7oUkmx/scipy/scipy/io/matlab/mio4.pyt   write_bytes  s    c         C` s   |  j  j |  d  S(   N(   R   R   (   R/   t   s(    (    s3   /tmp/pip-build-7oUkmx/scipy/scipy/io/matlab/mio4.pyt   write_string  s    i    c   	      C` s   t  j d t d  } t } d } | d | d | d | | d <| d | d <| d | d	 <| | d
 <t |  d | d <|  j |  |  j t | d   d S(   s(   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
        R   i    i  id   i
   R   R   i   R   R   R   R<   N(    (   R^   t   emptyR   R   R]   R   R   R   (	   R/   R*   RX   RJ   RK   R   R   RG   RI   (    (    s3   /tmp/pip-build-7oUkmx/scipy/scipy/io/matlab/mio4.pyt   write_header  s    
c         C` s   t  j j |  r& |  j | |  d St j |  } | j } | j sb | j | j	 d   } n  | j
 } | t j k r t d   nJ | t j k r t d   n, | t j t j f k r |  j | |  d S|  j | |  d S(   s    Write matrix `arr`, with name `name`

        Parameters
        ----------
        arr : array_like
           array to write
        name : str
           name in matlab workspace
        Nt   =s!   Cannot save object arrays in Mat4s   Cannot save void type arrays(   Rr   R(   t   issparset   write_sparseR^   t   asarrayR+   t   isnativeRg   t   newbyteordert   typet   object_RS   t   voidt   unicode_t   string_t
   write_chart   write_numeric(   R/   RV   R*   Ra   t   dtt(    (    s3   /tmp/pip-build-7oUkmx/scipy/scipy/io/matlab/mio4.pyR     s     			c      	   C` s   t  | |  j  } | j j d k } y t | j j d } Wn> t k
 r| | rd | j d  } n | j d  } t } n X|  j	 | | j
 d | d t d | | r |  j | j  |  j | j  n |  j |  d  S(   Nt   ci   t   c128R   RJ   RK   R   (   R   R   R+   t   kindt   np_to_mtypest   strt   KeyErrorRg   t   miDOUBLER   RX   RM   R   t   realRq   (   R/   RV   R*   R   RJ   (    (    s3   /tmp/pip-build-7oUkmx/scipy/scipy/io/matlab/mio4.pyR     s$    
		c         C` s   t  |  } t | |  j  } | j } |  j | | d t d t | j j d k r t	 j
 |  } t	 j d d	 d t | |  d |  } | j   j d  } t	 j d | d d d |  } n  |  j |  d  S(
   NRJ   RK   t   URX   R+   RY   s   latin-1R"   (    (   R   R   R   RX   R   t   miUINT8RO   R+   R   R^   t   productR_   R   t   itemt   encodeR   (   R/   RV   R*   R-   t   n_charst   st_arrt   st(    (    s3   /tmp/pip-build-7oUkmx/scipy/scipy/io/matlab/mio4.pyR     s     		!c         C` s;  | j    } | 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 c 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 d
 t d t |  j |  d S(   sX    Sparse matrices are 2D

        See docstring for VarReader4.read_sparse_array
        R   i   i   R+   R   Nii    i   RJ   RK   (   t   tocooR+   R   R^   t   zerost   nnzR   t   colRF   R   Rq   RX   R   R   RQ   R   (   R/   RV   R*   t   AR   t   ijv(    (    s3   /tmp/pip-build-7oUkmx/scipy/scipy/io/matlab/mio4.pyR   1  s"    &"	(   R1   R2   R0   R   R   R   RM   R   R   R   R   R   (    (    (    s3   /tmp/pip-build-7oUkmx/scipy/scipy/io/matlab/mio4.pyR     s   			 			t   MatFile4Writerc           B` s&   e  Z d  Z d d  Z d d  Z RS(   s)    Class for writing matlab 4 format files c         C` s4   | |  _  | d  k r d } n  | |  _ d  |  _ d  S(   NR   (   R   R   R   t   _matrix_writer(   R/   R   R   (    (    s3   /tmp/pip-build-7oUkmx/scipy/scipy/io/matlab/mio4.pyR0   L  s
    			c         C` sC   t  |   |  _ x- | j   D] \ } } |  j j | |  q Wd S(   s   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   t   itemsR   (   R/   R   R   R*   t   var(    (    s3   /tmp/pip-build-7oUkmx/scipy/scipy/io/matlab/mio4.pyt   put_variablesS  s    N(   R1   R2   R   R   R0   R   (    (    (    s3   /tmp/pip-build-7oUkmx/scipy/scipy/io/matlab/mio4.pyR   J  s   (   s   mopts   i4(   s   mrowss   i4(   s   ncolss   i4(   s   imagfs   i4(   s   namlens   i4(3   R   t
   __future__R    R   R   t   sysRB   t   numpyR^   t   numpy.compatR   R   t   scipy.sparseRr   t   scipy._lib.sixR   t   miobaseR   R   R   R	   R
   R   R   t	   mio_utilsR   R   t	   functoolsR   t	   byteorderR   R   t   miSINGLEt   miINT32t   miINT16t   miUINT16R   R   R   RM   RO   RQ   RD   R   t   objectR)   R6   R   R   R   R   (    (    (    s3   /tmp/pip-build-7oUkmx/scipy/scipy/io/matlab/mio4.pyt   <module>   sz   4




y