3
PdN/              
   @   s   d Z ddlZddlZddlZddlmZ ddlmZ	 G dd de
ZG dd	 d	e
ZG d
d deZdddddddddd	ZejeZdd Zdd Zdd Zd'ddZG dd  d eZG d!d" d"eZd#d$ Zd%d& ZdS )(zf
Base classes for MATLAB file stream reading.

MATLAB is a registered trademark of the Mathworks inc.
    N)doccer   )byteordercodesc               @   s   e Zd ZdS )MatReadErrorN)__name__
__module____qualname__ r	   r	   K/var/www/html/virt/lib64/python3.6/site-packages/scipy/io/matlab/miobase.pyr      s   r   c               @   s   e Zd ZdS )MatWriteErrorN)r   r   r   r	   r	   r	   r
   r      s   r   c               @   s   e Zd ZdS )MatReadWarningN)r   r   r   r	   r	   r	   r
   r      s   r   zfile_name : str
   Name of the mat file (do not need .mat extension if
   appendmat==True) Can also pass open file-like object.z|appendmat : bool, optional
   True to append the .mat extension to the end of the given
   filename, if not already present.a  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).ab  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.zAmat_stream : file-like
   Object with file API, open for reading.a  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.6z[do_compression : bool, optional
   Whether to compress matrices on write. Default is False.zoned_as : {'row', 'column'}, optional
   If 'column', write 1-D NumPy arrays as column vectors.
   If 'row', write 1D NumPy arrays as row vectors.zbunicode_strings : bool, optional
   If True, write strings as Unicode, else MATLAB usual encoding.)	Zfile_argZ
append_argZ	load_argsZ
struct_argZmatstream_argZlong_fieldsZdo_compressiononed_asZunicode_stringsc             C   s2   | j  }x$|D ]}tj|| j|||< qW |S )a   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)``

    )copynpdtypeZnewbyteorder)Zdtype_templateZ
order_codedtypeskr	   r	   r
   convert_dtypes   s    
r   c             C   s"   |j }tjf || j|dd}|S )af  
    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.

    F)shaper   bufferorder)itemsizer   ndarrayread)
mat_streamZa_dtype	num_bytesarrr	   r	   r
   
read_dtype   s    r   c             C   s   | j d | jd}t|dkr(tdtjdtj|d}d|krP| j d dS | j d | jd}| j d t|d dk}t|| }t|d|  }||f}|dkr|S td	| d
S )a  
    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
    r      zMat file appears to be empty)r   r   r   |         Ir   z%Unknown mat file type, version %s, %sN)r   )r   r   I   )r   r!   )	seekr   lenr   r   r   Zuint8int
ValueError)fileobjZ
mopt_bytesZ	mopt_intsZtst_strZmaj_indZmaj_valZmin_valretr	   r	   r
   get_matfile_version   s$    "





r*   columnc             C   st   | j }|f krdS tjtj|dkr8dtj| jdg S t|dkrp|dkrT|d	 S |dkrdd
| S t	d| |S )a  
    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])) # 1-D array, 1 element
    (1, 1)
    >>> matdims(np.array([1,2])) # 1-D array, 2 elements
    (2, 1)
    >>> matdims(np.array([[2],[3]])) # 2-D array, column vector
    (2, 1)
    >>> matdims(np.array([[2,3]])) # 2-D array, row vector
    (1, 2)
    >>> matdims(np.array([[[2,3]]])) # 3-D array, rowish vector
    (1, 1, 2)
    >>> matdims(np.array([])) # empty 1-D array
    (0, 0)
    >>> matdims(np.array([[]])) # empty 2-D array
    (0, 0)
    >>> matdims(np.array([[[]]])) # empty 3-D array
    (0, 0, 0)

    Optional argument flips 1-D shape behavior.

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

    The argument has to make sense though

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

    r   r   r!   r+   rowz1-D option "%s" is strange)r   r   )r   )r   )r   )
r   	functoolsreduceoperatormulr   maxndimr%   r'   )r   r   r   r	   r	   r
   matdims   s    =r3   c               @   s(   e Zd ZdZdd Zdd Zdd ZdS )	MatVarReaderz; Abstract class defining required interface for var readersc             C   s   d S )Nr	   )selfZfile_readerr	   r	   r
   __init__9  s    zMatVarReader.__init__c             C   s   dS )z Returns header Nr	   )r5   r	   r	   r
   read_header<  s    zMatVarReader.read_headerc             C   s   dS )z Reads array given header Nr	   )r5   headerr	   r	   r
   array_from_header@  s    zMatVarReader.array_from_headerN)r   r   r   __doc__r6   r7   r9   r	   r	   r	   r
   r4   7  s   r4   c            	   @   s6   e Zd ZdZedddZdd Zd	d
 Zdd ZdS )MatFileReadera   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
    NFTc
       
      C   sp   || _ i | _|s| j }n
tj|}|| _|| _|r>| j  n|| _|| _	|| _
|| _|	| _|	rld| _d| _dS )z
        Initializer for mat file reader

        mat_stream : file-like
            object with file API, open for reading
    %(load_args)s
        TFN)r   r   guess_byte_orderbocZto_numpy_code
byte_orderstruct_as_recordset_matlab_compatible
squeeze_mechars_as_strings	mat_dtype verify_compressed_data_integritysimplify_cells)
r5   r   r>   rC   rA   rB   Zmatlab_compatibler?   rD   rE   r	   r	   r
   r6   O  s"    


zMatFileReader.__init__c             C   s   d| _ d| _d| _dS )z4 Sets options to return arrays as MATLAB loads them TFN)rC   rA   rB   )r5   r	   r	   r
   r@   u  s    z#MatFileReader.set_matlab_compatiblec             C   s   t jS )z9 As we do not know what file type we have, assume native )r=   Znative_code)r5   r	   r	   r
   r<   {  s    zMatFileReader.guess_byte_orderc             C   s2   | j jd}| j j }| j j|d  t|dkS )Nr   r   )r   r   tellr$   r%   )r5   bZcurposr	   r	   r
   end_of_stream  s    
zMatFileReader.end_of_stream)NFFTFTTF)	r   r   r   r:   	docfillerr6   r@   r<   rH   r	   r	   r	   r
   r;   E  s          r;   c             C   s   t j| jjdd t| S )z3 Return dtype for given number of items per elementNr!   )r   r   str)r   numr	   r	   r
   arr_dtype_number  s    rL   c             C   st   t | j}|sdg}|jt| jjdd  tj|t| d| d} | dkg}tj	|s\| S | j
 } d| t|< | S )z$ Convert string array to char array r   r!   N)r   r   r     )listr   appendr&   r   rJ   r   r   rL   anyr   tuple)r   ZdimsZemptiesr	   r	   r
   arr_to_chars  s    


rS   )r+   )r:   r/   r-   Znumpyr   Z
scipy._libr   rM   r   r=   	Exceptionr   r   UserWarningr   Zdoc_dictZfilldocrI   r   r   r*   r3   objectr4   r;   rL   rS   r	   r	   r	   r
   <module>   s4   
/9
MA