σ
Λ½χXc           @` s  d  Z  d d l m Z m Z m Z d d l Z d d l Z d d l m Z d d l	 Z
 e j d d k rr e Z n e Z d d l m Z d d l m Z d	 e f d
     YZ d e f d     YZ d e f d     YZ i	 d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d  6Z e j e  Z d!   Z d"   Z d#   Z d$ d%  Z d& e  f d'     YZ! d( e  f d)     YZ" d*   Z# d+   Z$ d S(,   sf   
Base classes for MATLAB file stream reading.

MATLAB is a registered trademark of the Mathworks inc.
i    (   t   divisiont   print_functiont   absolute_importN(   t   reducei   (   t   docceri   (   t   byteordercodest   MatReadErrorc           B` s   e  Z RS(    (   t   __name__t
   __module__(    (    (    s6   /tmp/pip-build-7oUkmx/scipy/scipy/io/matlab/miobase.pyR      s   t   MatWriteErrorc           B` s   e  Z RS(    (   R   R   (    (    (    s6   /tmp/pip-build-7oUkmx/scipy/scipy/io/matlab/miobase.pyR	      s   t   MatReadWarningc           B` s   e  Z RS(    (   R   R   (    (    (    s6   /tmp/pip-build-7oUkmx/scipy/scipy/io/matlab/miobase.pyR
   #   s   s   file_name : str
   Name of the mat file (do not need .mat extension if
   appendmat==True) Can also pass open file-like object.t   file_args|   appendmat : bool, optional
   True to append the .mat extension to the end of the given
   filename, if not already present.t
   append_args  byte_order : str or None, optional
   None by default, implying byte order guessed from mat
   file. Otherwise can be one of ('native', '=', 'little', '<',
   'BIG', '>').
mat_dtype : bool, optional
   If True, return arrays in same dtype as would be loaded into
   MATLAB (instead of the dtype with which they are saved).
squeeze_me : bool, optional
   Whether to squeeze unit matrix dimensions or not.
chars_as_strings : bool, optional
   Whether to convert char arrays to string arrays.
matlab_compatible : bool, optional
   Returns matrices as would be loaded by MATLAB (implies
   squeeze_me=False, chars_as_strings=False, mat_dtype=True,
   struct_as_record=True).t	   load_argssd  struct_as_record : bool, optional
   Whether to load MATLAB structs as numpy record arrays, or as
   old-style numpy arrays with dtype=object.  Setting this flag to
   False replicates the behavior of scipy version 0.7.x (returning
   numpy object arrays).  The default setting is True, because it
   allows easier round-trip load and save of MATLAB files.t
   struct_argsA   mat_stream : file-like
   Object with file API, open for reading.t   matstream_args  long_field_names : bool, optional
   * False - maximum field name length in a structure is 31 characters
     which is the documented maximum length. This is the default.
   * True - maximum field name length in a structure is 63 characters
     which works for MATLAB 7.6t   long_fieldss[   do_compression : bool, optional
   Whether to compress matrices on write. Default is False.t   do_compressions   oned_as : {'row', 'column'}, optional
   If 'column', write 1-D numpy arrays as column vectors.
   If 'row', write 1D numpy arrays as row vectors.t   oned_assb   unicode_strings : bool, optional
   If True, write strings as Unicode, else MATLAB usual encoding.t   unicode_stringsc         C` sA   |  j    } x. | D]& } t j | |  j |  | | <q W| S(   s   Convert dtypes in mapping to given order

    Parameters
    ----------
    dtype_template : mapping
       mapping with values returning numpy dtype from ``np.dtype(val)``
    order_code : str
       an order code suitable for using in ``dtype.newbyteorder()``

    Returns
    -------
    dtypes : mapping
       mapping where values have been replaced by
       ``np.dtype(val).newbyteorder(order_code)``

    (   t   copyt   npt   dtypet   newbyteorder(   t   dtype_templatet
   order_codet   dtypest   k(    (    s6   /tmp/pip-build-7oUkmx/scipy/scipy/io/matlab/miobase.pyt   convert_dtypes   s    $c      	   C` s:   | j  } t j d d d | d |  j |  d d  } | S(   sg  
    Generic get of byte stream data of known type

    Parameters
    ----------
    mat_stream : file_like object
        MATLAB (tm) mat file stream
    a_dtype : dtype
        dtype of array to read.  `a_dtype` is assumed to be correct
        endianness.

    Returns
    -------
    arr : ndarray
        Array of dtype `a_dtype` read from stream.

    t   shapeR   t   buffert   ordert   F(    (   t   itemsizeR   t   ndarrayt   read(   t
   mat_streamt   a_dtypet	   num_bytest   arr(    (    s6   /tmp/pip-build-7oUkmx/scipy/scipy/io/matlab/miobase.pyt
   read_dtype‘   s    		c         C` s  |  j  d  |  j d  } t |  d k r= t d   n  t j d d d t j d |  } d | k r{ |  j  d  d S|  j  d  |  j d  } |  j  d  t | d d k  } t | |  } t | d
 |  } | | f } | d k rϊ | St	 d |   d S(   s·  
    Return major, minor tuple depending on apparent mat file type

    Where:

     #. 0,x -> version 4 format mat files
     #. 1,x -> version 5 format mat files
     #. 2,x -> version 7.3 format mat files (HDF format)

    Parameters
    ----------
    fileobj : file_like
        object implementing seek() and read()

    Returns
    -------
    major_version : {0, 1, 2}
        major MATLAB File format version
    minor_version : int
        minor MATLAB file format version

    Raises
    ------
    MatReadError
        If the file is empty.
    ValueError
        The matfile version is unknown.

    Notes
    -----
    Has the side effect of setting the file read pointer to 0
    i    i   s   Mat file appears to be emptyR   R   R   i|   i   t   Ii   s%   Unknown mat file type, version %s, %sN(   i   (   i    i    R)   (   i   i   (
   t   seekR#   t   lenR   R   R"   t   uint8t   intt   byteordt
   ValueError(   t   fileobjt
   mopt_bytest	   mopt_intst   tst_strt   maj_indt   maj_valt   min_valt   ret(    (    s6   /tmp/pip-build-7oUkmx/scipy/scipy/io/matlab/miobase.pyt   get_matfile_version»   s$    "!t   columnc         C` s   |  j  } | d k r d St t j |  d k rK d	 t j |  j d g  St |  d k r | d k rq | d
 S| d k r d | St d |   n  | S(   s  
    Determine equivalent MATLAB dimensions for given array

    Parameters
    ----------
    arr : ndarray
        Input array
    oned_as : {'column', 'row'}, optional
        Whether 1-D arrays are returned as MATLAB row or column matrices.
        Default is 'column'.

    Returns
    -------
    dims : tuple
        Shape tuple, in the form MATLAB expects it.

    Notes
    -----
    We had to decide what shape a 1 dimensional array would be by
    default.  ``np.atleast_2d`` thinks it is a row vector.  The
    default for a vector in MATLAB (e.g. ``>> 1:12``) is a row vector.

    Versions of scipy up to and including 0.11 resulted (accidentally)
    in 1-D arrays being read as column vectors.  For the moment, we
    maintain the same tradition here.

    Examples
    --------
    >>> matdims(np.array(1)) # numpy scalar
    (1, 1)
    >>> matdims(np.array([1])) # 1d array, 1 element
    (1, 1)
    >>> matdims(np.array([1,2])) # 1d array, 2 elements
    (2, 1)
    >>> matdims(np.array([[2],[3]])) # 2d array, column vector
    (2, 1)
    >>> matdims(np.array([[2,3]])) # 2d array, row vector
    (1, 2)
    >>> matdims(np.array([[[2,3]]])) # 3d array, rowish vector
    (1, 1, 2)
    >>> matdims(np.array([])) # empty 1d array
    (0, 0)
    >>> matdims(np.array([[]])) # empty 2d
    (0, 0)
    >>> matdims(np.array([[[]]])) # empty 3d
    (0, 0, 0)

    Optional argument flips 1-D shape behavior.

    >>> matdims(np.array([1,2]), 'row') # 1d array, 2 elements
    (1, 2)

    The argument has to make sense though

    >>> matdims(np.array([1,2]), 'bizarre')
    Traceback (most recent call last):
       ...
    ValueError: 1D option "bizarre" is strange

    i   i    i   R9   t   rows   1D option "%s" is strange(    (   i   i   (   i    (   i   (   i   (	   R   R   t   operatort   mulR   t   maxt   ndimR+   R/   (   R'   R   R   (    (    s6   /tmp/pip-build-7oUkmx/scipy/scipy/io/matlab/miobase.pyt   matdimsτ   s    =	t   MatVarReaderc           B` s)   e  Z d  Z d   Z d   Z d   Z RS(   s;    Abstract class defining required interface for var readersc         C` s   d  S(   N(    (   t   selft   file_reader(    (    s6   /tmp/pip-build-7oUkmx/scipy/scipy/io/matlab/miobase.pyt   __init__C  s    c         C` s   d S(   s    Returns header N(    (   RA   (    (    s6   /tmp/pip-build-7oUkmx/scipy/scipy/io/matlab/miobase.pyt   read_headerF  s    c         C` s   d S(   s    Reads array given header N(    (   RA   t   header(    (    s6   /tmp/pip-build-7oUkmx/scipy/scipy/io/matlab/miobase.pyt   array_from_headerJ  s    (   R   R   t   __doc__RC   RD   RF   (    (    (    s6   /tmp/pip-build-7oUkmx/scipy/scipy/io/matlab/miobase.pyR@   A  s   		t   MatFileReaderc        	   B` sM   e  Z d  Z e d e e e e e e d   Z d   Z d   Z	 d   Z
 RS(   s   Base object for reading mat files

    To make this class functional, you will need to override the
    following methods:

    matrix_getter_factory   - gives object to fetch next matrix from stream
    guess_byte_order        - guesses file byte order from file
    c	   	      C` s   | |  _  i  |  _ | s' |  j   } n t j |  } | |  _ | |  _ | r[ |  j   n | |  _ | |  _	 | |  _
 | |  _ d S(   s   
        Initializer for mat file reader

        mat_stream : file-like
            object with file API, open for reading
    %(load_args)s
        N(   R$   R   t   guess_byte_ordert   boct   to_numpy_codet
   byte_ordert   struct_as_recordt   set_matlab_compatiblet
   squeeze_met   chars_as_stringst	   mat_dtypet    verify_compressed_data_integrity(	   RA   R$   RL   RQ   RO   RP   t   matlab_compatibleRM   RR   (    (    s6   /tmp/pip-build-7oUkmx/scipy/scipy/io/matlab/miobase.pyRC   Y  s    							c         C` s   t  |  _ t |  _ t |  _ d S(   s4    Sets options to return arrays as MATLAB loads them N(   t   TrueRQ   t   FalseRO   RP   (   RA   (    (    s6   /tmp/pip-build-7oUkmx/scipy/scipy/io/matlab/miobase.pyRN   {  s    		c         C` s   t  j S(   s9    As we do not know what file type we have, assume native (   RJ   t   native_code(   RA   (    (    s6   /tmp/pip-build-7oUkmx/scipy/scipy/io/matlab/miobase.pyRI     s    c         C` sE   |  j  j d  } |  j  j   } |  j  j | d  t |  d k S(   Ni   i    (   R$   R#   t   tellR*   R+   (   RA   t   bt   curpos(    (    s6   /tmp/pip-build-7oUkmx/scipy/scipy/io/matlab/miobase.pyt   end_of_stream  s    N(   R   R   RG   t	   docfillert   NoneRU   RT   RC   RN   RI   RZ   (    (    (    s6   /tmp/pip-build-7oUkmx/scipy/scipy/io/matlab/miobase.pyRH   O  s   		c         C` s!   t  j |  j j d  t |   S(   s3    Return dtype for given number of items per elementi   (   R   R   t   str(   R'   t   num(    (    s6   /tmp/pip-build-7oUkmx/scipy/scipy/io/matlab/miobase.pyt   arr_dtype_number  s    c         C` s‘   t  |  j  } | s! d g } n  | j t |  j j d   t j d | d t |  d  d |   }  |  d k g } t j	 |  s |  S|  j
   }  d |  | <|  S(   s$    Convert string array to char array i   i   R   R   R   t    t    (   t   listR   t   appendR-   R   R]   R   R"   R_   t   anyR   (   R'   t   dimst   empties(    (    s6   /tmp/pip-build-7oUkmx/scipy/scipy/io/matlab/miobase.pyt   arr_to_chars  s    	
(%   RG   t
   __future__R    R   R   t   sysR;   t   scipy._lib.sixR   t   numpyR   t   version_infoR-   R.   t   ordt
   scipy.miscR   R`   R   RJ   t	   ExceptionR   R	   t   UserWarningR
   t   doc_dictt   filldocR[   R   R(   R8   R?   t   objectR@   RH   R_   Rg   (    (    (    s6   /tmp/pip-build-7oUkmx/scipy/scipy/io/matlab/miobase.pyt   <module>   s@   	
/			9M=	