ó
Ë½÷Xc           @` sö   d  Z  d d l m Z m Z m Z d d l Z d d l m Z d d l	 m
 Z
 m Z d d l m Z m Z d d l m Z m Z d	 d
 d d g Z d „  Z e e d „ ƒ Z e d e d „ ƒ Z e e d e e d d „ ƒ Z e e d „ ƒ Z d S(   s7   
Module for reading and writing matlab (TM) .mat files
i    (   t   divisiont   print_functiont   absolute_importN(   t   string_typesi   (   t   get_matfile_versiont	   docfiller(   t   MatFile4Readert   MatFile4Writer(   t   MatFile5Readert   MatFile5Writert   mat_reader_factoryt   loadmatt   savematt   whosmatc         C` s¹   t  |  t ƒ r„ y t |  d ƒ SWq„ t k
 r€ } | rz |  j d ƒ rz |  d 7}  y t |  d ƒ SWqz t k
 rv qz Xn  ‚  q„ Xn  y |  j d ƒ Wn t k
 r´ t d ƒ ‚ n X|  S(   s1    Open `file_like` and return as file-like object t   rbs   .mati    s/   Reader needs file name or open file-like object(   t
   isinstanceR   t   opent   IOErrort   endswitht   readt   AttributeError(   t	   file_liket	   appendmatt   e(    (    s2   /tmp/pip-build-7oUkmx/scipy/scipy/io/matlab/mio.pyt
   _open_file   s     

c         K` s‚   t  |  | ƒ } t | ƒ \ } } | d k r: t | |  S| d k rS t | |  S| d k rn t d ƒ ‚ n t d | ƒ ‚ d S(   sH  
    Create reader for matlab .mat format files.

    Parameters
    ----------
    %(file_arg)s
    %(append_arg)s
    %(load_args)s
    %(struct_arg)s

    Returns
    -------
    matreader : MatFileReader object
       Initialized instance of MatFileReader class matching the mat file
       type detected in `filename`.
    i    i   i   s+   Please use HDF reader for matlab v7.3 filess   Did not recognize version %sN(   R   R   R   R   t   NotImplementedErrort	   TypeError(   t	   file_nameR   t   kwargst   byte_streamt   mjvt   mnv(    (    s2   /tmp/pip-build-7oUkmx/scipy/scipy/io/matlab/mio.pyR
   (   s    c         K` sx   | j  d d ƒ } t |  | |  } | j | ƒ } | d k	 rO | j | ƒ n | } t |  t ƒ rt | j j ƒ  n  | S(   su
  
    Load MATLAB file.

    Parameters
    ----------
    file_name : str
       Name of the mat file (do not need .mat extension if
       appendmat==True). Can also pass open file-like object.
    mdict : dict, optional
        Dictionary in which to insert matfile variables.
    appendmat : bool, optional
       True to append the .mat extension to the end of the given
       filename, if not already present.
    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).
    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.
    verify_compressed_data_integrity : bool, optional
        Whether the length of compressed sequences in the MATLAB file
        should be checked, to ensure that they are not longer than we expect.
        It is advisable to enable this (the default) because overlong
        compressed sequences in MATLAB files generally indicate that the
        files have experienced some sort of corruption.
    variable_names : None or sequence
        If None (the default) - read all variables in file. Otherwise
        `variable_names` should be a sequence of strings, giving names of the
        matlab variables to read from the file.  The reader will skip any
        variable with a name not in this sequence, possibly saving some read
        processing.

    Returns
    -------
    mat_dict : dict
       dictionary with variable names as keys, and loaded matrices as
       values.

    Notes
    -----
    v4 (Level 1.0), v6 and v7 to 7.2 matfiles are supported.

    You will need an HDF5 python library to read matlab 7.3 format mat
    files.  Because scipy does not supply one, we do not implement the
    HDF5 / 7.3 interface here.

    t   variable_namesN(	   t   popt   NoneR
   t   get_variablest   updateR   R   t
   mat_streamt   close(   R   t   mdictR   R   R    t   MRt   matfile_dict(    (    s2   /tmp/pip-build-7oUkmx/scipy/scipy/io/matlab/mio.pyR   F   s    @t   5t   rowc   
      C` s  t  |  t ƒ } | rJ | r8 |  d d k r8 |  d }  n  t |  d ƒ } n$ t |  d ƒ sh t d ƒ ‚ n  |  } | d k r¡ | r t d ƒ ‚ n  t | | ƒ }	 n? | d k rÔ t | d	 | d
 t d | d | ƒ}	 n t d ƒ ‚ |	 j	 | ƒ | r | j
 ƒ  n  d S(   sZ  
    Save a dictionary of names and arrays into a MATLAB-style .mat file.

    This saves the array objects in the given dictionary to a MATLAB-
    style .mat file.

    Parameters
    ----------
    file_name : str or file-like object
        Name of the .mat file (.mat extension not needed if ``appendmat ==
        True``).
        Can also pass open file_like object.
    mdict : dict
        Dictionary from which to save matfile variables.
    appendmat : bool, optional
        True (the default) to append the .mat extension to the end of the
        given filename, if not already present.
    format : {'5', '4'}, string, optional
        '5' (the default) for MATLAB 5 and up (to 7.2),
        '4' for MATLAB 4 .mat files.
    long_field_names : bool, optional
        False (the default) - maximum field name length in a structure is
        31 characters which is the documented maximum length.
        True - maximum field name length in a structure is 63 characters
        which works for MATLAB 7.6+.
    do_compression : bool, optional
        Whether or not to compress matrices on write.  Default is False.
    oned_as : {'row', 'column'}, optional
        If 'column', write 1-D numpy arrays as column vectors.
        If 'row', write 1-D numpy arrays as row vectors.

    See also
    --------
    mio4.MatFile4Writer
    mio5.MatFile5Writer
    iüÿÿÿs   .matt   wbt   writes4   Writer needs file name or writeable file-like objectt   4s6   Long field names are not available for version 4 filesR*   t   do_compressiont   unicode_stringst   long_field_namest   oned_ass   Format should be '4' or '5'N(   R   R   R   t   hasattrR   t
   ValueErrorR   R	   t   Truet   put_variablesR&   (
   R   R'   R   t   formatR1   R/   R2   t   file_is_stringt   file_streamt   MW(    (    s2   /tmp/pip-build-7oUkmx/scipy/scipy/io/matlab/mio.pyR   ’   s,    +	c         K` s>   t  |  |  } | j ƒ  } t |  t ƒ r: | j j ƒ  n  | S(   sA  
    List variables inside a MATLAB file.

    Parameters
    ----------
    %(file_arg)s
    %(append_arg)s
    %(load_args)s
    %(struct_arg)s

    Returns
    -------
    variables : list of tuples
        A list of tuples, where each tuple holds the matrix name (a string),
        its shape (tuple of ints), and its data class (a string).
        Possible data classes are: int8, uint8, int16, uint16, int32, uint32,
        int64, uint64, single, double, cell, struct, object, char, sparse,
        function, opaque, logical, unknown.

    Notes
    -----
    v4 (Level 1.0), v6 and v7 to 7.2 matfiles are supported.

    You will need an HDF5 python library to read matlab 7.3 format mat
    files.  Because scipy does not supply one, we do not implement the
    HDF5 / 7.3 interface here.

    .. versionadded:: 0.12.0

    (   R
   t   list_variablesR   R   R%   R&   (   R   R   R   t   MLt	   variables(    (    s2   /tmp/pip-build-7oUkmx/scipy/scipy/io/matlab/mio.pyR   Ø   s
     (   t   __doc__t
   __future__R    R   R   t   numpyt   npt   scipy._lib.sixR   t   miobaseR   R   t   mio4R   R   t   mio5R   R	   t   __all__R   R5   R
   R"   R   t   FalseR   R   (    (    (    s2   /tmp/pip-build-7oUkmx/scipy/scipy/io/matlab/mio.pyt   <module>   s(   	K@