def pre_order(self, func=(lambda x: x.id)):

<EOL>n = self.count<EOL>curNode = [None] * (<NUM_LIT:2> * n)<EOL>lvisited = set()<EOL>rvisited = set()<EOL>curNode[<NUM_LIT:0>] = self<EOL>k = <NUM_LIT:0><EOL>preorder = []<EOL>while k >= <NUM_LIT:0>:<EOL><INDENT>nd = curNode[k]<EOL>ndid = nd.id<EOL>if nd.is_leaf():<EOL><INDENT>preorder.append(func(nd))<EOL>k = k - <NUM_LIT:1><EOL><DEDENT>else:<EOL><INDENT>if ndid not in lvisited:<EOL><INDENT>curNode[k + <NUM_LIT:1>] = nd.left<EOL>lvisited.add(ndid)<EOL>k = k + <NUM_LIT:1><EOL><DEDENT>elif ndid not in rvisited:<EOL><INDENT>curNode[k + <NUM_LIT:1>] = nd.right<EOL>rvisited.add(ndid)<EOL>k = k + <NUM_LIT:1><EOL><DEDENT>else:<EOL><INDENT>k = k - <NUM_LIT:1><EOL><DEDENT><DEDENT><DEDENT>return preorder<EOL>

Performs pre-order traversal without recursive function calls.

When a leaf node is first encountered, ``func`` is called with
the leaf node as its argument, and its result is appended to
the list.

For example, the statement::

   ids = root.pre_order(lambda x: x.id)

returns a list of the node ids corresponding to the leaf nodes
of the tree as they appear from left to right.

Parameters
----------
func : function
    Applied to each leaf ClusterNode object in the pre-order traversal.
    Given the i'th leaf node in the pre-ordeR traversal ``n[i]``, the
    result of func(n[i]) is stored in L[i]. If not provided, the index
    of the original observation to which the node corresponds is used.

Returns
-------
L : list
    The pre-order traversal.


@classmethod<EOL><INDENT>def from_number(cls, n, min=None):<DEDENT>

width = number_digits(n) + <NUM_LIT:1><EOL>if n < <NUM_LIT:0>:<EOL><INDENT>width += <NUM_LIT:1><EOL><DEDENT>repeat = <NUM_LIT> // width<EOL>return cls(width, min, repeat=repeat)<EOL>

Given an integer, returns a "reasonable" IntFormat instance to represent
        any number between 0 and n if n > 0, -n and n if n < 0

        Parameters
        ----------
        n : int
            max number one wants to be able to represent
        min : int
            minimum number of characters to use for the format

        Returns
        -------
        res : IntFormat
            IntFormat instance with reasonable (see Notes) computed width

        Notes
        -----
        Reasonable should be understood as the minimal string length necessary
        without losing precision. For example, IntFormat.from_number(1) will
        return an IntFormat instance of width 2, so that any 0 and 1 may be
        represented as 1-character strings without loss of information.


<EOL>finfo = np.finfo(n.dtype)<EOL>n_prec = finfo.precision + <NUM_LIT:1><EOL>n_exp = number_digits(np.max(np.abs([finfo.maxexp, finfo.minexp])))<EOL>width = <NUM_LIT:1> + <NUM_LIT:1> + n_prec + <NUM_LIT:1> + n_exp + <NUM_LIT:1><EOL>if n < <NUM_LIT:0>:<EOL><INDENT>width += <NUM_LIT:1><EOL><DEDENT>repeat = int(np.floor(<NUM_LIT> / width))<EOL>return cls(width, n_prec, min, repeat=repeat)<EOL>

Given a float number, returns a "reasonable" ExpFormat instance to
        represent any number between -n and n.

        Parameters
        ----------
        n : float
            max number one wants to be able to represent
        min : int
            minimum number of characters to use for the format

        Returns
        -------
        res : ExpFormat
            ExpFormat instance with reasonable (see Notes) computed width

        Notes
        -----
        Reasonable should be understood as the minimal string length necessary
        to avoid losing precision.


def __init__(self, width, significand, min=None, repeat=None):

self.width = width<EOL>self.significand = significand<EOL>self.repeat = repeat<EOL>self.min = min<EOL>

\
        Parameters
        ----------
        width : int
            number of characters taken by the string (includes space).


return (fmt.repeat * fmt.width + <NUM_LIT:1>) * (nlines - <NUM_LIT:1>)<EOL>

Return the number of bytes to read to get every full lines for the
    given parsed fortran format.


def _get_matrix(fid):<EOL><INDENT>hb = HBFile(fid)<EOL>return hb.read_matrix()<EOL><DEDENT>if isinstance(file, string_types):<EOL><INDENT>fid = open(file)<EOL>try:<EOL><INDENT>return _get_matrix(fid)<EOL><DEDENT>finally:<EOL><INDENT>fid.close()<EOL><DEDENT><DEDENT>else:<EOL><INDENT>return _get_matrix(file)<EOL><DEDENT>

Read HB-format file.

    Parameters
    ----------
    file : str-like or file-like
        If a string-like object, file is the name of the file to read. If a
        file-like object, the data are read from it.

    Returns
    -------
    data : scipy.sparse.csc_matrix instance
        The data read from the HB file as a sparse matrix.

    Notes
    -----
    At the moment not the full Harwell-Boeing format is supported. Supported
    features are:

        - assembled, non-symmetric, real matrices
        - integer for pointer/indices
        - exponential format for float values, and int format


if hb_info is None:<EOL><INDENT>hb_info = HBInfo.from_data(m)<EOL><DEDENT>def _set_matrix(fid):<EOL><INDENT>hb = HBFile(fid, hb_info)<EOL>return hb.write_matrix(m)<EOL><DEDENT>if isinstance(file, string_types):<EOL><INDENT>fid = open(file, "<STR_LIT:w>")<EOL>try:<EOL><INDENT>return _set_matrix(fid)<EOL><DEDENT>finally:<EOL><INDENT>fid.close()<EOL><DEDENT><DEDENT>else:<EOL><INDENT>return _set_matrix(file)<EOL><DEDENT>

Write HB-format file.

    Parameters
    ----------
    file : str-like or file-like
        if a string-like object, file is the name of the file to read. If a
        file-like object, the data are read from it.
    m : sparse-matrix
        the sparse matrix to write
    hb_info : HBInfo
        contains the meta-data for write

    Returns
    -------
    None

    Notes
    -----
    At the moment not the full Harwell-Boeing format is supported. Supported
    features are:

        - assembled, non-symmetric, real matrices
        - integer for pointer/indices
        - exponential format for float values, and int format


@classmethod<EOL><INDENT>def from_data(cls, m, title="<STR_LIT>", key="<STR_LIT:0>", mxtype=None, fmt=None):<DEDENT>

pointer = m.indptr<EOL>indices = m.indices<EOL>values = m.data<EOL>nrows, ncols = m.shape<EOL>nnon_zeros = m.nnz<EOL>if fmt is None:<EOL><INDENT>pointer_fmt = IntFormat.from_number(np.max(pointer+<NUM_LIT:1>))<EOL>indices_fmt = IntFormat.from_number(np.max(indices+<NUM_LIT:1>))<EOL>if values.dtype.kind in np.typecodes["<STR_LIT>"]:<EOL><INDENT>values_fmt = ExpFormat.from_number(-np.max(np.abs(values)))<EOL><DEDENT>elif values.dtype.kind in np.typecodes["<STR_LIT>"]:<EOL><INDENT>values_fmt = IntFormat.from_number(-np.max(np.abs(values)))<EOL><DEDENT>else:<EOL><INDENT>raise NotImplementedError("<STR_LIT>" % values.dtype.kind)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>raise NotImplementedError("<STR_LIT>")<EOL><DEDENT>if mxtype is None:<EOL><INDENT>if not np.isrealobj(values):<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if values.dtype.kind in np.typecodes["<STR_LIT>"]:<EOL><INDENT>tp = "<STR_LIT>"<EOL><DEDENT>elif values.dtype.kind in np.typecodes["<STR_LIT>"]:<EOL><INDENT>tp = "<STR_LIT>"<EOL><DEDENT>else:<EOL><INDENT>raise NotImplementedError("<STR_LIT>"<EOL>% values.dtype)<EOL><DEDENT>mxtype = HBMatrixType(tp, "<STR_LIT>", "<STR_LIT>")<EOL><DEDENT>else:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>def _nlines(fmt, size):<EOL><INDENT>nlines = size // fmt.repeat<EOL>if nlines * fmt.repeat != size:<EOL><INDENT>nlines += <NUM_LIT:1><EOL><DEDENT>return nlines<EOL><DEDENT>pointer_nlines = _nlines(pointer_fmt, pointer.size)<EOL>indices_nlines = _nlines(indices_fmt, indices.size)<EOL>values_nlines = _nlines(values_fmt, values.size)<EOL>total_nlines = pointer_nlines + indices_nlines + values_nlines<EOL>return cls(title, key,<EOL>total_nlines, pointer_nlines, indices_nlines, values_nlines,<EOL>mxtype, nrows, ncols, nnon_zeros,<EOL>pointer_fmt.fortran_format, indices_fmt.fortran_format,<EOL>values_fmt.fortran_format)<EOL>

Create a HBInfo instance from an existing sparse matrix.

        Parameters
        ----------
        m : sparse matrix
            the HBInfo instance will derive its parameters from m
        title : str
            Title to put in the HB header
        key : str
            Key
        mxtype : HBMatrixType
            type of the input matrix
        fmt : dict
            not implemented

        Returns
        -------
        hb_info : HBInfo instance


<EOL>line = fid.readline().strip("<STR_LIT:\n>")<EOL>if not len(line) > <NUM_LIT>:<EOL><INDENT>raise ValueError("<STR_LIT>"<EOL>"<STR_LIT>" % line)<EOL><DEDENT>title = line[:<NUM_LIT>]<EOL>key = line[<NUM_LIT>:]<EOL>line = fid.readline().strip("<STR_LIT:\n>")<EOL>if not len(line.rstrip()) >= <NUM_LIT>:<EOL><INDENT>raise ValueError("<STR_LIT>"<EOL>"<STR_LIT>" % line)<EOL><DEDENT>total_nlines = _expect_int(line[:<NUM_LIT>])<EOL>pointer_nlines = _expect_int(line[<NUM_LIT>:<NUM_LIT>])<EOL>indices_nlines = _expect_int(line[<NUM_LIT>:<NUM_LIT>])<EOL>values_nlines = _expect_int(line[<NUM_LIT>:<NUM_LIT>])<EOL>rhs_nlines = line[<NUM_LIT>:<NUM_LIT>].strip()<EOL>if rhs_nlines == '<STR_LIT>':<EOL><INDENT>rhs_nlines = <NUM_LIT:0><EOL><DEDENT>else:<EOL><INDENT>rhs_nlines = _expect_int(rhs_nlines)<EOL><DEDENT>if not rhs_nlines == <NUM_LIT:0>:<EOL><INDENT>raise ValueError("<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT>line = fid.readline().strip("<STR_LIT:\n>")<EOL>if not len(line) >= <NUM_LIT>:<EOL><INDENT>raise ValueError("<STR_LIT>"<EOL>"<STR_LIT:%s>" % line)<EOL><DEDENT>mxtype_s = line[:<NUM_LIT:3>].upper()<EOL>if not len(mxtype_s) == <NUM_LIT:3>:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>mxtype = HBMatrixType.from_fortran(mxtype_s)<EOL>if mxtype.value_type not in ["<STR_LIT>", "<STR_LIT>"]:<EOL><INDENT>raise ValueError("<STR_LIT>"<EOL>"<STR_LIT>" % mxtype)<EOL><DEDENT>if not mxtype.structure == "<STR_LIT>":<EOL><INDENT>raise ValueError("<STR_LIT>"<EOL>"<STR_LIT>" % mxtype)<EOL><DEDENT>if not mxtype.storage == "<STR_LIT>":<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if not line[<NUM_LIT:3>:<NUM_LIT>] == "<STR_LIT:U+0020>" * <NUM_LIT:11>:<EOL><INDENT>raise ValueError("<STR_LIT>" % line)<EOL><DEDENT>nrows = _expect_int(line[<NUM_LIT>:<NUM_LIT>])<EOL>ncols = _expect_int(line[<NUM_LIT>:<NUM_LIT>])<EOL>nnon_zeros = _expect_int(line[<NUM_LIT>:<NUM_LIT>])<EOL>nelementals = _expect_int(line[<NUM_LIT>:<NUM_LIT>])<EOL>if not nelementals == <NUM_LIT:0>:<EOL><INDENT>raise ValueError("<STR_LIT>"<EOL>% nelementals)<EOL><DEDENT>line = fid.readline().strip("<STR_LIT:\n>")<EOL>ct = line.split()<EOL>if not len(ct) == <NUM_LIT:3>:<EOL><INDENT>raise ValueError("<STR_LIT>" % ct)<EOL><DEDENT>return cls(title, key,<EOL>total_nlines, pointer_nlines, indices_nlines, values_nlines,<EOL>mxtype, nrows, ncols, nnon_zeros,<EOL>ct[<NUM_LIT:0>], ct[<NUM_LIT:1>], ct[<NUM_LIT:2>],<EOL>rhs_nlines, nelementals)<EOL>

Create a HBInfo instance from a file object containg a matrix in the
        HB format.

        Parameters
        ----------
        fid : file-like matrix
            File or file-like object containing a matrix in the HB format.

        Returns
        -------
        hb_info : HBInfo instance


def __init__(self, title, key,<EOL>total_nlines, pointer_nlines, indices_nlines, values_nlines,<EOL>mxtype, nrows, ncols, nnon_zeros,<EOL>pointer_format_str, indices_format_str, values_format_str,<EOL>right_hand_sides_nlines=<NUM_LIT:0>, nelementals=<NUM_LIT:0>):

self.title = title<EOL>self.key = key<EOL>if title is None:<EOL><INDENT>title = "<STR_LIT>"<EOL><DEDENT>if len(title) > <NUM_LIT>:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if key is None:<EOL><INDENT>key = "<STR_LIT>"<EOL><DEDENT>if len(key) > <NUM_LIT:8>:<EOL><INDENT>warnings.warn("<STR_LIT>" % key, LineOverflow)<EOL><DEDENT>self.total_nlines = total_nlines<EOL>self.pointer_nlines = pointer_nlines<EOL>self.indices_nlines = indices_nlines<EOL>self.values_nlines = values_nlines<EOL>parser = FortranFormatParser()<EOL>pointer_format = parser.parse(pointer_format_str)<EOL>if not isinstance(pointer_format, IntFormat):<EOL><INDENT>raise ValueError("<STR_LIT>"<EOL>% pointer_format)<EOL><DEDENT>indices_format = parser.parse(indices_format_str)<EOL>if not isinstance(indices_format, IntFormat):<EOL><INDENT>raise ValueError("<STR_LIT>" %<EOL>indices_format)<EOL><DEDENT>values_format = parser.parse(values_format_str)<EOL>if isinstance(values_format, ExpFormat):<EOL><INDENT>if mxtype.value_type not in ["<STR_LIT>", "<STR_LIT>"]:<EOL><INDENT>raise ValueError("<STR_LIT>"<EOL>"<STR_LIT>" % (mxtype, values_format))<EOL><DEDENT>values_dtype = np.float64<EOL><DEDENT>elif isinstance(values_format, IntFormat):<EOL><INDENT>if mxtype.value_type not in ["<STR_LIT>"]:<EOL><INDENT>raise ValueError("<STR_LIT>"<EOL>"<STR_LIT>" % (mxtype, values_format))<EOL><DEDENT>values_dtype = np.int<EOL><DEDENT>else:<EOL><INDENT>raise ValueError("<STR_LIT>" % (values_format,))<EOL><DEDENT>self.pointer_format = pointer_format<EOL>self.indices_format = indices_format<EOL>self.values_format = values_format<EOL>self.pointer_dtype = np.int32<EOL>self.indices_dtype = np.int32<EOL>self.values_dtype = values_dtype<EOL>self.pointer_nlines = pointer_nlines<EOL>self.pointer_nbytes_full = _nbytes_full(pointer_format, pointer_nlines)<EOL>self.indices_nlines = indices_nlines<EOL>self.indices_nbytes_full = _nbytes_full(indices_format, indices_nlines)<EOL>self.values_nlines = values_nlines<EOL>self.values_nbytes_full = _nbytes_full(values_format, values_nlines)<EOL>self.nrows = nrows<EOL>self.ncols = ncols<EOL>self.nnon_zeros = nnon_zeros<EOL>self.nelementals = nelementals<EOL>self.mxtype = mxtype<EOL>

Do not use this directly, but the class ctrs (from_* functions).


header = [self.title.ljust(<NUM_LIT>) + self.key.ljust(<NUM_LIT:8>)]<EOL>header.append("<STR_LIT>" %<EOL>(self.total_nlines, self.pointer_nlines,<EOL>self.indices_nlines, self.values_nlines))<EOL>header.append("<STR_LIT>" %<EOL>(self.mxtype.fortran_format.ljust(<NUM_LIT>), self.nrows,<EOL>self.ncols, self.nnon_zeros, <NUM_LIT:0>))<EOL>pffmt = self.pointer_format.fortran_format<EOL>iffmt = self.indices_format.fortran_format<EOL>vffmt = self.values_format.fortran_format<EOL>header.append("<STR_LIT>" %<EOL>(pffmt.ljust(<NUM_LIT:16>), iffmt.ljust(<NUM_LIT:16>), vffmt.ljust(<NUM_LIT:20>)))<EOL>return "<STR_LIT:\n>".join(header)<EOL>

Gives the header corresponding to this instance as a string.


self._fid = file<EOL>if hb_info is None:<EOL><INDENT>self._hb_info = HBInfo.from_file(file)<EOL><DEDENT>else:<EOL><INDENT>self._hb_info = hb_info<EOL><DEDENT>

Create a HBFile instance.

        Parameters
        ----------
        file : file-object
            StringIO work as well
        hb_info : HBInfo
            Should be given as an argument for writing, in which case the file
            should be writable.


codecs = {}<EOL>postfix = byte_order == '<STR_LIT:<>' and '<STR_LIT>' or '<STR_LIT>'<EOL>for k, v in template.items():<EOL><INDENT>codec = v['<STR_LIT>']<EOL>try:<EOL><INDENT>"<STR_LIT:U+0020>".encode(codec)<EOL><DEDENT>except LookupError:<EOL><INDENT>codecs[k] = None<EOL>continue<EOL><DEDENT>if v['<STR_LIT:width>'] > <NUM_LIT:1>:<EOL><INDENT>codec += postfix<EOL><DEDENT>codecs[k] = codec<EOL><DEDENT>return codecs.copy()<EOL>

Convert codec template mapping to byte order

    Set codecs not on this system to None

    Parameters
    ----------
    template : mapping
       key, value are respectively codec name, and root name for codec
       (without byte order suffix)
    byte_order : {'<', '>'}
       code for little or big endian

    Returns
    -------
    codecs : dict
       key, value are name, codec (as in .encode(codec))


arr = np.array(*args, **kwargs)<EOL>arr.shape = matdims(arr)<EOL>return arr<EOL>

Convenience function to return matlab-compatible 2D array.


type1 = type(var1)<EOL>type2 = type(var2)<EOL>if type1 is type2:<EOL><INDENT>return True<EOL><DEDENT>if type1 is np.ndarray and var1.shape == ():<EOL><INDENT>return type(var1.item()) is type2<EOL><DEDENT>if type2 is np.ndarray and var2.shape == ():<EOL><INDENT>return type(var2.item()) is type1<EOL><DEDENT>return False<EOL>

Check if types are same or compatible.

    0-D numpy scalars are compatible with bare python scalars.


def _check_level(label, expected, actual):

if SP.issparse(expected):  <EOL><INDENT>assert_(SP.issparse(actual))<EOL>assert_array_almost_equal(actual.todense(),<EOL>expected.todense(),<EOL>err_msg=label,<EOL>decimal=<NUM_LIT:5>)<EOL>return<EOL><DEDENT>assert_(types_compatible(expected, actual),<EOL>"<STR_LIT>" %<EOL>(type(expected), type(actual), label))<EOL>if not isinstance(expected,<EOL>(np.void, np.ndarray, MatlabObject)):<EOL><INDENT>assert_equal(expected, actual)<EOL>return<EOL><DEDENT>assert_(expected.shape == actual.shape,<EOL>msg='<STR_LIT>' % (expected.shape,<EOL>actual.shape,<EOL>label))<EOL>ex_dtype = expected.dtype<EOL>if ex_dtype.hasobject:  <EOL><INDENT>if isinstance(expected, MatlabObject):<EOL><INDENT>assert_equal(expected.classname, actual.classname)<EOL><DEDENT>for i, ev in enumerate(expected):<EOL><INDENT>level_label = "<STR_LIT>" % (label, i)<EOL>_check_level(level_label, ev, actual[i])<EOL><DEDENT>return<EOL><DEDENT>if ex_dtype.fields:  <EOL><INDENT>for fn in ex_dtype.fields:<EOL><INDENT>level_label = "<STR_LIT>" % (label, fn)<EOL>_check_level(level_label,<EOL>expected[fn], actual[fn])<EOL><DEDENT>return<EOL><DEDENT>if ex_dtype.type in (text_type,  <EOL>np.unicode_,<EOL>np.bool_):<EOL><INDENT>assert_equal(actual, expected, err_msg=label)<EOL>return<EOL><DEDENT>assert_array_almost_equal(actual, expected, err_msg=label, decimal=<NUM_LIT:5>)<EOL>

Check one level of a potentially nested array


def _make_tag(base_dt, val, mdtype, sde=False):

base_dt = np.dtype(base_dt)<EOL>bo = boc.to_numpy_code(base_dt.byteorder)<EOL>byte_count = base_dt.itemsize<EOL>if not sde:<EOL><INDENT>udt = bo + '<STR_LIT>'<EOL>padding = <NUM_LIT:8> - (byte_count % <NUM_LIT:8>)<EOL>all_dt = [('<STR_LIT>', udt),<EOL>('<STR_LIT>', udt),<EOL>('<STR_LIT>', base_dt)]<EOL>if padding:<EOL><INDENT>all_dt.append(('<STR_LIT>', '<STR_LIT>', padding))<EOL><DEDENT><DEDENT>else:  <EOL><INDENT>udt = bo + '<STR_LIT>'<EOL>padding = <NUM_LIT:4>-byte_count<EOL>if bo == '<STR_LIT:<>':  <EOL><INDENT>all_dt = [('<STR_LIT>', udt),<EOL>('<STR_LIT>', udt),<EOL>('<STR_LIT>', base_dt)]<EOL><DEDENT>else:  <EOL><INDENT>all_dt = [('<STR_LIT>', udt),<EOL>('<STR_LIT>', udt),<EOL>('<STR_LIT>', base_dt)]<EOL><DEDENT>if padding:<EOL><INDENT>all_dt.append(('<STR_LIT>', '<STR_LIT>', padding))<EOL><DEDENT><DEDENT>tag = np.zeros((<NUM_LIT:1>,), dtype=all_dt)<EOL>tag['<STR_LIT>'] = mdtype<EOL>tag['<STR_LIT>'] = byte_count<EOL>tag['<STR_LIT>'] = val<EOL>return tag<EOL>


if isinstance(file_like, string_types):<EOL><INDENT>try:<EOL><INDENT>return open(file_like, '<STR_LIT:rb>')<EOL><DEDENT>except IOError as e:<EOL><INDENT>if appendmat and not file_like.endswith('<STR_LIT>'):<EOL><INDENT>file_like += '<STR_LIT>'<EOL>try:<EOL><INDENT>return open(file_like, '<STR_LIT:rb>')<EOL><DEDENT>except IOError:<EOL><INDENT>pass  <EOL><DEDENT><DEDENT>raise<EOL><DEDENT><DEDENT>try:<EOL><INDENT>file_like.read(<NUM_LIT:0>)<EOL><DEDENT>except AttributeError:<EOL><INDENT>raise IOError('<STR_LIT>')<EOL><DEDENT>return file_like<EOL>

Open `file_like` and return as file-like object


@docfiller<EOL>def mat_reader_factory(file_name, appendmat=True, **kwargs):

byte_stream = _open_file(file_name, appendmat)<EOL>mjv, mnv = get_matfile_version(byte_stream)<EOL>if mjv == <NUM_LIT:0>:<EOL><INDENT>return MatFile4Reader(byte_stream, **kwargs)<EOL><DEDENT>elif mjv == <NUM_LIT:1>:<EOL><INDENT>return MatFile5Reader(byte_stream, **kwargs)<EOL><DEDENT>elif mjv == <NUM_LIT:2>:<EOL><INDENT>raise NotImplementedError('<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>raise TypeError('<STR_LIT>' % mjv)<EOL><DEDENT>

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`.


@docfiller<EOL>def loadmat(file_name, mdict=None, appendmat=True, **kwargs):

variable_names = kwargs.pop('<STR_LIT>', None)<EOL>MR = mat_reader_factory(file_name, appendmat, **kwargs)<EOL>matfile_dict = MR.get_variables(variable_names)<EOL>if mdict is not None:<EOL><INDENT>mdict.update(matfile_dict)<EOL><DEDENT>else:<EOL><INDENT>mdict = matfile_dict<EOL><DEDENT>if isinstance(file_name, string_types):<EOL><INDENT>MR.mat_stream.close()<EOL><DEDENT>return mdict<EOL>

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.
m_dict : 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.


file_is_string = isinstance(file_name, string_types)<EOL>if file_is_string:<EOL><INDENT>if appendmat and file_name[-<NUM_LIT:4>:] != "<STR_LIT>":<EOL><INDENT>file_name = file_name + "<STR_LIT>"<EOL><DEDENT>file_stream = open(file_name, '<STR_LIT:wb>')<EOL><DEDENT>else:<EOL><INDENT>if not hasattr(file_name, '<STR_LIT>'):<EOL><INDENT>raise IOError('<STR_LIT>'<EOL>'<STR_LIT>')<EOL><DEDENT>file_stream = file_name<EOL><DEDENT>if format == '<STR_LIT:4>':<EOL><INDENT>if long_field_names:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>MW = MatFile4Writer(file_stream, oned_as)<EOL><DEDENT>elif format == '<STR_LIT:5>':<EOL><INDENT>MW = MatFile5Writer(file_stream,<EOL>do_compression=do_compression,<EOL>unicode_strings=True,<EOL>long_field_names=long_field_names,<EOL>oned_as=oned_as)<EOL><DEDENT>else:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>MW.put_variables(mdict)<EOL>if file_is_string:<EOL><INDENT>file_stream.close()<EOL><DEDENT>

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


@docfiller<EOL>def whosmat(file_name, appendmat=True, **kwargs):

ML = mat_reader_factory(file_name, **kwargs)<EOL>variables = ML.list_variables()<EOL>if isinstance(file_name, string_types):<EOL><INDENT>ML.mat_stream.close()<EOL><DEDENT>return variables<EOL>

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


code = textwrap.dedent(code)<EOL>process = subprocess.Popen([sys.executable, '<STR_LIT:-c>', code])<EOL>peak_memusage = -<NUM_LIT:1><EOL>start = time.time()<EOL>while True:<EOL><INDENT>ret = process.poll()<EOL>if ret is not None:<EOL><INDENT>break<EOL><DEDENT>with open('<STR_LIT>' % process.pid, '<STR_LIT:r>') as f:<EOL><INDENT>procdata = f.read()<EOL><DEDENT>m = re.search('<STR_LIT>', procdata, re.S | re.I)<EOL>if m is not None:<EOL><INDENT>memusage = float(m.group(<NUM_LIT:1>)) * <NUM_LIT><EOL>peak_memusage = max(memusage, peak_memusage)<EOL><DEDENT>time.sleep(<NUM_LIT>)<EOL><DEDENT>process.wait()<EOL>duration = time.time() - start<EOL>if process.returncode != <NUM_LIT:0>:<EOL><INDENT>raise AssertionError("<STR_LIT>" % code)<EOL><DEDENT>return duration, peak_memusage<EOL>

Run code in a new Python process, and monitor peak memory usage.

Returns
-------
duration : float
    Duration in seconds (including Python startup time)
peak_memusage : float
    Peak memory usage (rough estimate only) in bytes


info = {}<EOL>with open('<STR_LIT>', '<STR_LIT:r>') as f:<EOL><INDENT>for line in f:<EOL><INDENT>p = line.split()<EOL>info[p[<NUM_LIT:0>].strip('<STR_LIT::>').lower()] = float(p[<NUM_LIT:1>]) * <NUM_LIT><EOL><DEDENT><DEDENT>return info<EOL>


import resource<EOL>cur_limit = resource.getrlimit(resource.RLIMIT_AS)<EOL>if cur_limit[<NUM_LIT:0>] > <NUM_LIT:0>:<EOL><INDENT>max_mem = min(max_mem, cur_limit[<NUM_LIT:0>])<EOL><DEDENT>resource.setrlimit(resource.RLIMIT_AS, (max_mem, cur_limit[<NUM_LIT:1>]))<EOL>

Set rlimit to 80% of total system memory, to avoid grinding halt
because of swapping.


code = code.lower()<EOL>if code is None:<EOL><INDENT>return native_code<EOL><DEDENT>if code in aliases['<STR_LIT>']:<EOL><INDENT>return '<STR_LIT:<>'<EOL><DEDENT>elif code in aliases['<STR_LIT>']:<EOL><INDENT>return '<STR_LIT:>>'<EOL><DEDENT>elif code in aliases['<STR_LIT>']:<EOL><INDENT>return native_code<EOL><DEDENT>elif code in aliases['<STR_LIT>']:<EOL><INDENT>return swapped_code<EOL><DEDENT>else:<EOL><INDENT>raise ValueError(<EOL>'<STR_LIT>' % code)<EOL><DEDENT>

Convert various order codings to numpy format.

Parameters
----------
code : str
    The code to convert. It is converted to lower case before parsing.
    Legal values are:
    'little', 'big', 'l', 'b', 'le', 'be', '<', '>', 'native', '=',
    'swapped', 's'.

Returns
-------
out_code : {'<', '>'}
    Here '<' is the numpy dtype code for little endian,
    and '>' is the code for big endian.

Examples
--------
>>> import sys
>>> sys_is_le == (sys.byteorder == 'little')
True
>>> to_numpy_code('big')
'>'
>>> to_numpy_code('little')
'<'
>>> nc = to_numpy_code('native')
>>> nc == '<' if sys_is_le else nc == '>'
True
>>> sc = to_numpy_code('swapped')
>>> sc == '>' if sys_is_le else sc == '<'
True


dims = matdims(arr, oned_as)<EOL>if len(dims) > <NUM_LIT:2>:<EOL><INDENT>raise ValueError('<STR_LIT>'<EOL>'<STR_LIT>')<EOL><DEDENT>return arr.reshape(dims)<EOL>

Make ``arr`` exactly two dimensional

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

    Parameters
    ----------
    arr : array
    oned_as : {'row', 'column'}
       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


data = read_dtype(self.mat_stream, self.dtypes['<STR_LIT>'])<EOL>name = self.mat_stream.read(int(data['<STR_LIT>'])).strip(b'<STR_LIT:\x00>')<EOL>if data['<STR_LIT>'] < <NUM_LIT:0> or data['<STR_LIT>'] > <NUM_LIT>:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>M, rest = divmod(data['<STR_LIT>'], <NUM_LIT:1000>)  <EOL>if M not in (<NUM_LIT:0>, <NUM_LIT:1>):<EOL><INDENT>warnings.warn("<STR_LIT>"<EOL>"<STR_LIT>" % order_codes[M],<EOL>UserWarning)<EOL><DEDENT>O, rest = divmod(rest, <NUM_LIT:100>)  <EOL>if O != <NUM_LIT:0>:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>P, rest = divmod(rest, <NUM_LIT:10>)  <EOL>T = rest  <EOL>dims = (data['<STR_LIT>'], data['<STR_LIT>'])<EOL>is_complex = data['<STR_LIT>'] == <NUM_LIT:1><EOL>dtype = self.dtypes[P]<EOL>return VarHeader4(<EOL>name,<EOL>dtype,<EOL>T,<EOL>dims,<EOL>is_complex)<EOL>


def read_sub_array(self, hdr, copy=True):

dt = hdr.dtype<EOL>dims = hdr.dims<EOL>num_bytes = dt.itemsize<EOL>for d in dims:<EOL><INDENT>num_bytes *= d<EOL><DEDENT>buffer = self.mat_stream.read(int(num_bytes))<EOL>if len(buffer) != num_bytes:<EOL><INDENT>raise ValueError("<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>" % hdr.name)<EOL><DEDENT>arr = np.ndarray(shape=dims,<EOL>dtype=dt,<EOL>buffer=buffer,<EOL>order='<STR_LIT:F>')<EOL>if copy:<EOL><INDENT>arr = arr.copy()<EOL><DEDENT>return arr<EOL>

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``


if hdr.is_complex:<EOL><INDENT>res = self.read_sub_array(hdr, copy=False)<EOL>res_j = self.read_sub_array(hdr, copy=False)<EOL>return res + (res_j * <NUM_LIT>)<EOL><DEDENT>return self.read_sub_array(hdr)<EOL>

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


arr = self.read_sub_array(hdr).astype(np.uint8)<EOL>S = arr.tostring().decode('<STR_LIT>')<EOL>return np.ndarray(shape=hdr.dims,<EOL>dtype=np.dtype('<STR_LIT>'),<EOL>buffer=np.array(S)).copy()<EOL>

latin-1 text matrix (char matrix) reader

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

        Returns
        -------
        arr : ndarray
            with dtype 'U1', shape given by `hdr` ``dims``


res = self.read_sub_array(hdr)<EOL>tmp = res[:-<NUM_LIT:1>,:]<EOL>dims = res[-<NUM_LIT:1>,<NUM_LIT:0>:<NUM_LIT:2>]<EOL>I = np.ascontiguousarray(tmp[:,<NUM_LIT:0>],dtype='<STR_LIT>')  <EOL>J = np.ascontiguousarray(tmp[:,<NUM_LIT:1>],dtype='<STR_LIT>')<EOL>I -= <NUM_LIT:1>  <EOL>J -= <NUM_LIT:1><EOL>if res.shape[<NUM_LIT:1>] == <NUM_LIT:3>:<EOL><INDENT>V = np.ascontiguousarray(tmp[:,<NUM_LIT:2>],dtype='<STR_LIT:float>')<EOL><DEDENT>else:<EOL><INDENT>V = np.ascontiguousarray(tmp[:,<NUM_LIT:2>],dtype='<STR_LIT>')<EOL>V.imag = tmp[:,<NUM_LIT:3>]<EOL><DEDENT>return scipy.sparse.coo_matrix((V,(I,J)), dims)<EOL>

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


mclass = hdr.mclass<EOL>if mclass == mxFULL_CLASS:<EOL><INDENT>shape = tuple(map(int, hdr.dims))<EOL><DEDENT>elif mclass == mxCHAR_CLASS:<EOL><INDENT>shape = tuple(map(int, hdr.dims))<EOL>if self.chars_as_strings:<EOL><INDENT>shape = shape[:-<NUM_LIT:1>]<EOL><DEDENT><DEDENT>elif mclass == mxSPARSE_CLASS:<EOL><INDENT>dt = hdr.dtype<EOL>dims = hdr.dims<EOL>if not (len(dims) == <NUM_LIT:2> and dims[<NUM_LIT:0>] >= <NUM_LIT:1> and dims[<NUM_LIT:1>] >= <NUM_LIT:1>):<EOL><INDENT>return ()<EOL><DEDENT>self.mat_stream.seek(dt.itemsize * (dims[<NUM_LIT:0>] - <NUM_LIT:1>), <NUM_LIT:1>)<EOL>rows = np.ndarray(shape=(<NUM_LIT:1>,), dtype=dt,<EOL>buffer=self.mat_stream.read(dt.itemsize))<EOL>self.mat_stream.seek(dt.itemsize * (dims[<NUM_LIT:0>] - <NUM_LIT:1>), <NUM_LIT:1>)<EOL>cols = np.ndarray(shape=(<NUM_LIT:1>,), dtype=dt,<EOL>buffer=self.mat_stream.read(dt.itemsize))<EOL>shape = (int(rows), int(cols))<EOL><DEDENT>else:<EOL><INDENT>raise TypeError('<STR_LIT>' % mclass)<EOL><DEDENT>if self.squeeze_me:<EOL><INDENT>shape = tuple([x for x in shape if x != <NUM_LIT:1>])<EOL><DEDENT>return shape<EOL>

Read the shape of the array described by the header.
        The file position after this call is unspecified.


@docfiller<EOL><INDENT>def __init__(self, mat_stream, *args, **kwargs):<DEDENT>

super(MatFile4Reader, self).__init__(mat_stream, *args, **kwargs)<EOL>self._matrix_reader = None<EOL>

Initialize matlab 4 file reader

    %(matstream_arg)s
    %(load_args)s


self.dtypes = convert_dtypes(mdtypes_template, self.byte_order)<EOL>self._matrix_reader = VarReader4(self)<EOL>

Run when beginning read of variables

        Sets up readers from parameters in `self`


hdr = self._matrix_reader.read_header()<EOL>n = reduce(lambda x, y: x*y, hdr.dims, <NUM_LIT:1>)  <EOL>remaining_bytes = hdr.dtype.itemsize * n<EOL>if hdr.is_complex and not hdr.mclass == mxSPARSE_CLASS:<EOL><INDENT>remaining_bytes *= <NUM_LIT:2><EOL><DEDENT>next_position = self.mat_stream.tell() + remaining_bytes<EOL>return hdr, next_position<EOL>

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


def read_var_array(self, header, process=True):

return self._matrix_reader.array_from_header(header, process)<EOL>

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`.


if isinstance(variable_names, string_types):<EOL><INDENT>variable_names = [variable_names]<EOL><DEDENT>elif variable_names is not None:<EOL><INDENT>variable_names = list(variable_names)<EOL><DEDENT>self.mat_stream.seek(<NUM_LIT:0>)<EOL>self.initialize_read()<EOL>mdict = {}<EOL>while not self.end_of_stream():<EOL><INDENT>hdr, next_position = self.read_var_header()<EOL>name = asstr(hdr.name)<EOL>if variable_names and name not in variable_names:<EOL><INDENT>self.mat_stream.seek(next_position)<EOL>continue<EOL><DEDENT>mdict[name] = self.read_var_array(hdr)<EOL>self.mat_stream.seek(next_position)<EOL>if variable_names:<EOL><INDENT>variable_names.remove(name)<EOL>if len(variable_names) == <NUM_LIT:0>:<EOL><INDENT>break<EOL><DEDENT><DEDENT><DEDENT>return mdict<EOL>

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


self.mat_stream.seek(<NUM_LIT:0>)<EOL>self.initialize_read()<EOL>vars = []<EOL>while not self.end_of_stream():<EOL><INDENT>hdr, next_position = self.read_var_header()<EOL>name = asstr(hdr.name)<EOL>shape = self._matrix_reader.shape_from_header(hdr)<EOL>info = mclass_info.get(hdr.mclass, '<STR_LIT>')<EOL>vars.append((name, shape, info))<EOL>self.mat_stream.seek(next_position)<EOL><DEDENT>return vars<EOL>


def write_header(self, name, shape, P=miDOUBLE, T=mxFULL_CLASS, imagf=<NUM_LIT:0>):

header = np.empty((), mdtypes_template['<STR_LIT>'])<EOL>M = not SYS_LITTLE_ENDIAN<EOL>O = <NUM_LIT:0><EOL>header['<STR_LIT>'] = (M * <NUM_LIT:1000> +<EOL>O * <NUM_LIT:100> +<EOL>P * <NUM_LIT:10> +<EOL>T)<EOL>header['<STR_LIT>'] = shape[<NUM_LIT:0>]<EOL>header['<STR_LIT>'] = shape[<NUM_LIT:1>]<EOL>header['<STR_LIT>'] = imagf<EOL>header['<STR_LIT>'] = len(name) + <NUM_LIT:1><EOL>self.write_bytes(header)<EOL>self.write_string(asbytes(name + '<STR_LIT>'))<EOL>

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


<EOL>if scipy.sparse.issparse(arr):<EOL><INDENT>self.write_sparse(arr, name)<EOL>return<EOL><DEDENT>arr = np.asarray(arr)<EOL>dt = arr.dtype<EOL>if not dt.isnative:<EOL><INDENT>arr = arr.astype(dt.newbyteorder('<STR_LIT:=>'))<EOL><DEDENT>dtt = dt.type<EOL>if dtt is np.object_:<EOL><INDENT>raise TypeError('<STR_LIT>')<EOL><DEDENT>elif dtt is np.void:<EOL><INDENT>raise TypeError('<STR_LIT>')<EOL><DEDENT>elif dtt in (np.unicode_, np.string_):<EOL><INDENT>self.write_char(arr, name)<EOL>return<EOL><DEDENT>self.write_numeric(arr, name)<EOL>

Write matrix `arr`, with name `name`

        Parameters
        ----------
        arr : array-like
           array to write
        name : str
           name in matlab workspace


A = arr.tocoo()  <EOL>imagf = A.dtype.kind == '<STR_LIT:c>'<EOL>ijv = np.zeros((A.nnz + <NUM_LIT:1>, <NUM_LIT:3>+imagf), dtype='<STR_LIT>')<EOL>ijv[:-<NUM_LIT:1>,<NUM_LIT:0>] = A.row<EOL>ijv[:-<NUM_LIT:1>,<NUM_LIT:1>] = A.col<EOL>ijv[:-<NUM_LIT:1>,<NUM_LIT:0>:<NUM_LIT:2>] += <NUM_LIT:1>  <EOL>if imagf:<EOL><INDENT>ijv[:-<NUM_LIT:1>,<NUM_LIT:2>] = A.data.real<EOL>ijv[:-<NUM_LIT:1>,<NUM_LIT:3>] = A.data.imag<EOL><DEDENT>else:<EOL><INDENT>ijv[:-<NUM_LIT:1>,<NUM_LIT:2>] = A.data<EOL><DEDENT>ijv[-<NUM_LIT:1>,<NUM_LIT:0>:<NUM_LIT:2>] = A.shape<EOL>self.write_header(<EOL>name,<EOL>ijv.shape,<EOL>P=miDOUBLE,<EOL>T=mxSPARSE_CLASS)<EOL>self.write_bytes(ijv)<EOL>

Sparse matrices are 2D

        See docstring for VarReader4.read_sparse_array


def put_variables(self, mdict, write_header=None):

<EOL>self._matrix_writer = VarWriter4(self)<EOL>for name, var in mdict.items():<EOL><INDENT>self._matrix_writer.write(var, name)<EOL><DEDENT>

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


dtypes = dtype_template.copy()<EOL>for k in dtypes:<EOL><INDENT>dtypes[k] = np.dtype(dtypes[k]).newbyteorder(order_code)<EOL><DEDENT>return dtypes<EOL>

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)``


num_bytes = a_dtype.itemsize<EOL>arr = np.ndarray(shape=(),<EOL>dtype=a_dtype,<EOL>buffer=mat_stream.read(num_bytes),<EOL>order='<STR_LIT:F>')<EOL>return arr<EOL>

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.


<EOL>fileobj.seek(<NUM_LIT:0>)<EOL>mopt_bytes = np.ndarray(shape=(<NUM_LIT:4>,),<EOL>dtype=np.uint8,<EOL>buffer = fileobj.read(<NUM_LIT:4>))<EOL>if <NUM_LIT:0> in mopt_bytes:<EOL><INDENT>fileobj.seek(<NUM_LIT:0>)<EOL>return (<NUM_LIT:0>,<NUM_LIT:0>)<EOL><DEDENT>fileobj.seek(<NUM_LIT>)<EOL>tst_str = fileobj.read(<NUM_LIT:4>)<EOL>fileobj.seek(<NUM_LIT:0>)<EOL>maj_ind = int(tst_str[<NUM_LIT:2>] == b'<STR_LIT:I>'[<NUM_LIT:0>])<EOL>maj_val = byteord(tst_str[maj_ind])<EOL>min_val = byteord(tst_str[<NUM_LIT:1>-maj_ind])<EOL>ret = (maj_val, min_val)<EOL>if maj_val in (<NUM_LIT:1>, <NUM_LIT:2>):<EOL><INDENT>return ret<EOL><DEDENT>else:<EOL><INDENT>raise ValueError('<STR_LIT>'<EOL>% ret)<EOL><DEDENT>

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

Notes
-----
Has the side effect of setting the file read pointer to 0


shape = arr.shape<EOL>if shape == ():  <EOL><INDENT>return (<NUM_LIT:1>,<NUM_LIT:1>)<EOL><DEDENT>if reduce(operator.mul, shape) == <NUM_LIT:0>:  <EOL><INDENT>return (<NUM_LIT:0>,) * np.max([arr.ndim, <NUM_LIT:2>])<EOL><DEDENT>if len(shape) == <NUM_LIT:1>:  <EOL><INDENT>if oned_as == '<STR_LIT>':<EOL><INDENT>return shape + (<NUM_LIT:1>,)<EOL><DEDENT>elif oned_as == '<STR_LIT>':<EOL><INDENT>return (<NUM_LIT:1>,) + shape<EOL><DEDENT>else:<EOL><INDENT>raise ValueError('<STR_LIT>'<EOL>% oned_as)<EOL><DEDENT><DEDENT>return shape<EOL>

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


return np.dtype(arr.dtype.str[:<NUM_LIT:2>] + str(num))<EOL>

Return dtype for given number of items per element


dims = list(arr.shape)<EOL>if not dims:<EOL><INDENT>dims = [<NUM_LIT:1>]<EOL><DEDENT>dims.append(int(arr.dtype.str[<NUM_LIT:2>:]))<EOL>arr = np.ndarray(shape=dims,<EOL>dtype=arr_dtype_number(arr, <NUM_LIT:1>),<EOL>buffer=arr)<EOL>empties = [arr == '<STR_LIT>']<EOL>if not np.any(empties):<EOL><INDENT>return arr<EOL><DEDENT>arr = arr.copy()<EOL>arr[empties] = '<STR_LIT:U+0020>'<EOL>return arr<EOL>


pass<EOL>


<EOL>self.mat_stream = mat_stream<EOL>self.dtypes = {}<EOL>if not byte_order:<EOL><INDENT>byte_order = self.guess_byte_order()<EOL><DEDENT>else:<EOL><INDENT>byte_order = boc.to_numpy_code(byte_order)<EOL><DEDENT>self.byte_order = byte_order<EOL>self.struct_as_record = struct_as_record<EOL>if matlab_compatible:<EOL><INDENT>self.set_matlab_compatible()<EOL><DEDENT>else:<EOL><INDENT>self.squeeze_me = squeeze_me<EOL>self.chars_as_strings = chars_as_strings<EOL>self.mat_dtype = mat_dtype<EOL><DEDENT>self.verify_compressed_data_integrity = verify_compressed_data_integrity<EOL>

Initializer for mat file reader

    mat_stream : file-like
        object with file API, open for reading
%(load_args)s


self.mat_dtype = True<EOL>self.squeeze_me = False<EOL>self.chars_as_strings = False<EOL>

Sets options to return arrays as MATLAB loads them


return boc.native_code<EOL>

As we do not know what file type we have, assume native


rdr = MatFile5Reader(file_obj)<EOL>file_obj.seek(<NUM_LIT:0>)<EOL>hdr_len = MDTYPES[native_code]['<STR_LIT>']['<STR_LIT>'].itemsize<EOL>raw_hdr = file_obj.read(hdr_len)<EOL>file_obj.seek(<NUM_LIT:0>)<EOL>rdr.initialize_read()<EOL>mdict = rdr.read_file_header()<EOL>next_position = file_obj.tell()<EOL>named_mats = []<EOL>while not rdr.end_of_stream():<EOL><INDENT>start_position = next_position<EOL>hdr, next_position = rdr.read_var_header()<EOL>name = asstr(hdr.name)<EOL>file_obj.seek(start_position)<EOL>byte_count = next_position - start_position<EOL>var_str = file_obj.read(byte_count)<EOL>out_obj = BytesIO()<EOL>out_obj.write(raw_hdr)<EOL>out_obj.write(var_str)<EOL>out_obj.seek(<NUM_LIT:0>)<EOL>named_mats.append((name, out_obj))<EOL><DEDENT>return named_mats<EOL>

Pull variables out of mat 5 file as a sequence of mat file objects

    This can be useful with a difficult mat file, containing unreadable
    variables.  This routine pulls the variables out in raw form and puts them,
    unread, back into a file stream for saving or reading.  Another use is the
    pathological case where there is more than one variable of the same name in
    the file; this routine returns the duplicates, whereas the standard reader
    will overwrite duplicates in the returned dictionary.

    The file pointer in `file_obj` will be undefined.  File pointers for the
    returned file-like objects are set at 0.

    Parameters
    ----------
    file_obj : file-like
        file object containing mat file

    Returns
    -------
    named_mats : list
        list contains tuples of (name, BytesIO) where BytesIO is a file-like
        object containing mat file contents as for a single variable.  The
        BytesIO contains a string with the original header and a single var. If
        ``var_file_obj`` is an individual BytesIO instance, then save as a mat
        file with something like ``open('test.mat',
        'wb').write(var_file_obj.read())``

    Examples
    --------
    >>> import scipy.io

    BytesIO is from the ``io`` module in python 3, and is ``cStringIO`` for
    python < 3.

    >>> mat_fileobj = BytesIO()
    >>> scipy.io.savemat(mat_fileobj, {'b': np.arange(10), 'a': 'a string'})
    >>> varmats = varmats_from_mat(mat_fileobj)
    >>> sorted([name for name, str_obj in varmats])
    ['a', 'b']


if isinstance(source, np.ndarray):<EOL><INDENT>return source<EOL><DEDENT>if source is None:<EOL><INDENT>return None<EOL><DEDENT>is_mapping = (hasattr(source, '<STR_LIT>') and hasattr(source, '<STR_LIT>') and<EOL>hasattr(source, '<STR_LIT>'))<EOL>if not is_mapping and hasattr(source, '<STR_LIT>'):<EOL><INDENT>source = dict((key, value) for key, value in source.__dict__.items()<EOL>if not key.startswith('<STR_LIT:_>'))<EOL>is_mapping = True<EOL><DEDENT>if is_mapping:<EOL><INDENT>dtype = []<EOL>values = []<EOL>for field, value in source.items():<EOL><INDENT>if (isinstance(field, string_types) and<EOL>field[<NUM_LIT:0>] not in '<STR_LIT>'):<EOL><INDENT>dtype.append((field,object))<EOL>values.append(value)<EOL><DEDENT><DEDENT>if dtype:<EOL><INDENT>return np.array([tuple(values)],dtype)<EOL><DEDENT>else:<EOL><INDENT>return None<EOL><DEDENT><DEDENT>narr = np.asanyarray(source)<EOL>if narr.dtype.type in (np.object, np.object_) andnarr.shape == () and narr == source:<EOL><INDENT>return None<EOL><DEDENT>return narr<EOL>

Convert input object ``source`` to something we can write

    Parameters
    ----------
    source : object

    Returns
    -------
    arr : ndarray

    Examples
    --------
    >>> to_writeable(np.array([1])) # pass through ndarrays
    array([1])
    >>> expected = np.array([(1, 2)], dtype=[('a', '|O8'), ('b', '|O8')])
    >>> np.all(to_writeable({'a':1,'b':2}) == expected)
    True
    >>> np.all(to_writeable({'a':1,'b':2, '_c':3}) == expected)
    True
    >>> np.all(to_writeable({'a':1,'b':2, 100:3}) == expected)
    True
    >>> np.all(to_writeable({'a':1,'b':2, '99':3}) == expected)
    True
    >>> class klass(object): pass
    >>> c = klass
    >>> c.a = 1
    >>> c.b = 2
    >>> np.all(to_writeable({'a':1,'b':2}) == expected)
    True
    >>> to_writeable([])
    array([], dtype=float64)
    >>> to_writeable(())
    array([], dtype=float64)
    >>> to_writeable(None)

    >>> to_writeable('a string').dtype.type == np.str_
    True
    >>> to_writeable(1)
    array(1)
    >>> to_writeable([1])
    array([1])
    >>> to_writeable([1])
    array([1])
    >>> to_writeable(object()) # not convertable

    dict keys with legal characters are convertible

    >>> to_writeable({'a':1})['a']
    array([1], dtype=object)

    but not with illegal characters

    >>> to_writeable({'1':1}) is None
    True
    >>> to_writeable({'_a':1}) is None
    True


super(MatFile5Reader, self).__init__(<EOL>mat_stream,<EOL>byte_order,<EOL>mat_dtype,<EOL>squeeze_me,<EOL>chars_as_strings,<EOL>matlab_compatible,<EOL>struct_as_record,<EOL>verify_compressed_data_integrity<EOL>)<EOL>if not uint16_codec:<EOL><INDENT>uint16_codec = sys.getdefaultencoding()<EOL><DEDENT>self.uint16_codec = uint16_codec<EOL>self._file_reader = None<EOL>self._matrix_reader = None<EOL>

Initializer for matlab 5 file format reader

    %(matstream_arg)s
    %(load_args)s
    %(struct_arg)s
    uint16_codec : {None, string}
        Set codec to use for uint16 char arrays (e.g. 'utf-8').
        Use system default codec if None


self.mat_stream.seek(<NUM_LIT>)<EOL>mi = self.mat_stream.read(<NUM_LIT:2>)<EOL>self.mat_stream.seek(<NUM_LIT:0>)<EOL>return mi == b'<STR_LIT>' and '<STR_LIT:<>' or '<STR_LIT:>>'<EOL>

Guess byte order.
        Sets stream pointer to 0


hdict = {}<EOL>hdr_dtype = MDTYPES[self.byte_order]['<STR_LIT>']['<STR_LIT>']<EOL>hdr = read_dtype(self.mat_stream, hdr_dtype)<EOL>hdict['<STR_LIT>'] = hdr['<STR_LIT:description>'].item().strip(b'<STR_LIT>')<EOL>v_major = hdr['<STR_LIT:version>'] >> <NUM_LIT:8><EOL>v_minor = hdr['<STR_LIT:version>'] & <NUM_LIT><EOL>hdict['<STR_LIT>'] = '<STR_LIT>' % (v_major, v_minor)<EOL>return hdict<EOL>


<EOL>self._file_reader = VarReader5(self)<EOL>self._matrix_reader = VarReader5(self)<EOL>

Run when beginning read of variables

        Sets up readers from parameters in `self`


mdtype, byte_count = self._file_reader.read_full_tag()<EOL>if not byte_count > <NUM_LIT:0>:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>next_pos = self.mat_stream.tell() + byte_count<EOL>if mdtype == miCOMPRESSED:<EOL><INDENT>stream = ZlibInputStream(self.mat_stream, byte_count)<EOL>self._matrix_reader.set_stream(stream)<EOL>check_stream_limit = self.verify_compressed_data_integrity<EOL>mdtype, byte_count = self._matrix_reader.read_full_tag()<EOL><DEDENT>else:<EOL><INDENT>check_stream_limit = False<EOL>self._matrix_reader.set_stream(self.mat_stream)<EOL><DEDENT>if not mdtype == miMATRIX:<EOL><INDENT>raise TypeError('<STR_LIT>' % mdtype)<EOL><DEDENT>header = self._matrix_reader.read_header(check_stream_limit)<EOL>return header, next_pos<EOL>

Read header, return header, next position

        Header has to define at least .name and .is_global

        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


return self._matrix_reader.array_from_header(header, process)<EOL>

Read array, given `header`

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

        Returns
        -------
        arr : array
           array with post-processing applied or not according to
           `process`.


if isinstance(variable_names, string_types):<EOL><INDENT>variable_names = [variable_names]<EOL><DEDENT>elif variable_names is not None:<EOL><INDENT>variable_names = list(variable_names)<EOL><DEDENT>self.mat_stream.seek(<NUM_LIT:0>)<EOL>self.initialize_read()<EOL>mdict = self.read_file_header()<EOL>mdict['<STR_LIT>'] = []<EOL>while not self.end_of_stream():<EOL><INDENT>hdr, next_position = self.read_var_header()<EOL>name = asstr(hdr.name)<EOL>if name in mdict:<EOL><INDENT>warnings.warn('<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>' % name,<EOL>MatReadWarning, stacklevel=<NUM_LIT:2>)<EOL><DEDENT>if name == '<STR_LIT>':<EOL><INDENT>name = '<STR_LIT>'<EOL>process = False<EOL><DEDENT>else:<EOL><INDENT>process = True<EOL><DEDENT>if variable_names and name not in variable_names:<EOL><INDENT>self.mat_stream.seek(next_position)<EOL>continue<EOL><DEDENT>try:<EOL><INDENT>res = self.read_var_array(hdr, process)<EOL><DEDENT>except MatReadError as err:<EOL><INDENT>warnings.warn(<EOL>'<STR_LIT>' %<EOL>(name, err),<EOL>Warning, stacklevel=<NUM_LIT:2>)<EOL>res = "<STR_LIT>" % err<EOL><DEDENT>self.mat_stream.seek(next_position)<EOL>mdict[name] = res<EOL>if hdr.is_global:<EOL><INDENT>mdict['<STR_LIT>'].append(name)<EOL><DEDENT>if variable_names:<EOL><INDENT>variable_names.remove(name)<EOL>if len(variable_names) == <NUM_LIT:0>:<EOL><INDENT>break<EOL><DEDENT><DEDENT><DEDENT>return mdict<EOL>

get variables from stream as dictionary

        variable_names   - optional list of variable names to get

        If variable_names is None, then get all variables in file


self.mat_stream.seek(<NUM_LIT:0>)<EOL>self.initialize_read()<EOL>self.read_file_header()<EOL>vars = []<EOL>while not self.end_of_stream():<EOL><INDENT>hdr, next_position = self.read_var_header()<EOL>name = asstr(hdr.name)<EOL>if name == '<STR_LIT>':<EOL><INDENT>name = '<STR_LIT>'<EOL><DEDENT>shape = self._matrix_reader.shape_from_header(hdr)<EOL>if hdr.is_logical:<EOL><INDENT>info = '<STR_LIT>'<EOL><DEDENT>else:<EOL><INDENT>info = mclass_info.get(hdr.mclass, '<STR_LIT>')<EOL><DEDENT>vars.append((name, shape, info))<EOL>self.mat_stream.seek(next_position)<EOL><DEDENT>return vars<EOL>


def write_element(self, arr, mdtype=None):

if mdtype is None:<EOL><INDENT>mdtype = NP_TO_MTYPES[arr.dtype.str[<NUM_LIT:1>:]]<EOL><DEDENT>if arr.dtype.byteorder == swapped_code:<EOL><INDENT>arr = arr.byteswap().newbyteorder()<EOL><DEDENT>byte_count = arr.size*arr.itemsize<EOL>if byte_count <= <NUM_LIT:4>:<EOL><INDENT>self.write_smalldata_element(arr, mdtype, byte_count)<EOL><DEDENT>else:<EOL><INDENT>self.write_regular_element(arr, mdtype, byte_count)<EOL><DEDENT>


<EOL>name = self._var_name<EOL>is_global = self._var_is_global<EOL>self._mat_tag_pos = self.file_stream.tell()<EOL>self.write_bytes(self.mat_tag)<EOL>af = np.zeros((), NDT_ARRAY_FLAGS)<EOL>af['<STR_LIT>'] = miUINT32<EOL>af['<STR_LIT>'] = <NUM_LIT:8><EOL>flags = is_complex << <NUM_LIT:3> | is_global << <NUM_LIT:2> | is_logical << <NUM_LIT:1><EOL>af['<STR_LIT>'] = mclass | flags << <NUM_LIT:8><EOL>af['<STR_LIT>'] = nzmax<EOL>self.write_bytes(af)<EOL>self.write_element(np.array(shape, dtype='<STR_LIT>'))<EOL>name = np.asarray(name)<EOL>if name == '<STR_LIT>':  <EOL><INDENT>self.write_smalldata_element(name, miINT8, <NUM_LIT:0>)<EOL><DEDENT>else:<EOL><INDENT>self.write_element(name, miINT8)<EOL><DEDENT>self._var_name = '<STR_LIT>'<EOL>self._var_is_global = False<EOL>

Write header for given data options
        shape : sequence
           array shape
        mclass      - mat5 matrix class
        is_complex  - True if matrix is complex
        is_logical  - True if matrix is logical
        nzmax        - max non zero elements for sparse arrays

        We get the name and the global flag from the object, and reset
        them to defaults after we've used them


def write_top(self, arr, name, is_global):

<EOL>self._var_is_global = is_global<EOL>self._var_name = name<EOL>self.write(arr)<EOL>

Write variable at top level of mat file

        Parameters
        ----------
        arr : array-like
            array-like object to create writer for
        name : str, optional
            name as it will appear in matlab workspace
            default is empty string
        is_global : {False, True}, optional
            whether variable will be global on load into matlab


<EOL>mat_tag_pos = self.file_stream.tell()<EOL>if scipy.sparse.issparse(arr):<EOL><INDENT>self.write_sparse(arr)<EOL>self.update_matrix_tag(mat_tag_pos)<EOL>return<EOL><DEDENT>narr = to_writeable(arr)<EOL>if narr is None:<EOL><INDENT>raise TypeError('<STR_LIT>'<EOL>% (arr, type(arr)))<EOL><DEDENT>if isinstance(narr, MatlabObject):<EOL><INDENT>self.write_object(narr)<EOL><DEDENT>elif isinstance(narr, MatlabFunction):<EOL><INDENT>raise MatWriteError('<STR_LIT>')<EOL><DEDENT>elif narr.dtype.fields:  <EOL><INDENT>self.write_struct(narr)<EOL><DEDENT>elif narr.dtype.hasobject:  <EOL><INDENT>self.write_cells(narr)<EOL><DEDENT>elif narr.dtype.kind in ('<STR_LIT>', '<STR_LIT:S>'):<EOL><INDENT>if self.unicode_strings:<EOL><INDENT>codec = '<STR_LIT>'<EOL><DEDENT>else:<EOL><INDENT>codec = '<STR_LIT:ascii>'<EOL><DEDENT>self.write_char(narr, codec)<EOL><DEDENT>else:<EOL><INDENT>self.write_numeric(narr)<EOL><DEDENT>self.update_matrix_tag(mat_tag_pos)<EOL>

Write `arr` to stream at top and sub levels

        Parameters
        ----------
        arr : array-like
            array-like object to create writer for


def write_char(self, arr, codec='<STR_LIT:ascii>'):

if arr.size == <NUM_LIT:0> or np.all(arr == '<STR_LIT>'):<EOL><INDENT>shape = (<NUM_LIT:0>,) * np.max([arr.ndim, <NUM_LIT:2>])<EOL>self.write_header(shape, mxCHAR_CLASS)<EOL>self.write_smalldata_element(arr, miUTF8, <NUM_LIT:0>)<EOL>return<EOL><DEDENT>arr = arr_to_chars(arr)<EOL>shape = arr.shape<EOL>self.write_header(shape, mxCHAR_CLASS)<EOL>if arr.dtype.kind == '<STR_LIT>' and arr.size:<EOL><INDENT>n_chars = np.product(shape)<EOL>st_arr = np.ndarray(shape=(),<EOL>dtype=arr_dtype_number(arr, n_chars),<EOL>buffer=arr.T.copy())  <EOL>st = st_arr.item().encode(codec)<EOL>arr = np.ndarray(shape=(len(st),),<EOL>dtype='<STR_LIT>',<EOL>buffer=st)<EOL><DEDENT>self.write_element(arr, mdtype=miUTF8)<EOL>

Write string array `arr` with given `codec`


A = arr.tocsc()  <EOL>A.sort_indices()     <EOL>is_complex = (A.dtype.kind == '<STR_LIT:c>')<EOL>is_logical = (A.dtype.kind == '<STR_LIT:b>')<EOL>nz = A.nnz<EOL>self.write_header(matdims(arr, self.oned_as),<EOL>mxSPARSE_CLASS,<EOL>is_complex=is_complex,<EOL>is_logical=is_logical,<EOL>nzmax=nz)<EOL>self.write_element(A.indices.astype('<STR_LIT>'))<EOL>self.write_element(A.indptr.astype('<STR_LIT>'))<EOL>self.write_element(A.data.real)<EOL>if is_complex:<EOL><INDENT>self.write_element(A.data.imag)<EOL><DEDENT>


self.write_header(matdims(arr, self.oned_as),<EOL>mxOBJECT_CLASS)<EOL>self.write_element(np.array(arr.classname, dtype='<STR_LIT:S>'),<EOL>mdtype=miINT8)<EOL>self._write_items(arr)<EOL>

Same as writing structs, except different mx class, and extra
        classname element after header


self.file_stream = file_stream<EOL>self.do_compression = do_compression<EOL>self.unicode_strings = unicode_strings<EOL>if global_vars:<EOL><INDENT>self.global_vars = global_vars<EOL><DEDENT>else:<EOL><INDENT>self.global_vars = []<EOL><DEDENT>self.long_field_names = long_field_names<EOL>self.oned_as = oned_as<EOL>self._matrix_writer = None<EOL>

Initialize writer for matlab 5 format files

        Parameters
        ----------
        %(do_compression)s
        %(unicode_strings)s
        global_vars : None or sequence of strings, optional
            Names of variables to be marked as global for matlab
        %(long_fields)s
        %(oned_as)s


<EOL>if write_header is None:<EOL><INDENT>write_header = self.file_stream.tell() == <NUM_LIT:0><EOL><DEDENT>if write_header:<EOL><INDENT>self.write_file_header()<EOL><DEDENT>self._matrix_writer = VarWriter5(self)<EOL>for name, var in mdict.items():<EOL><INDENT>if name[<NUM_LIT:0>] == '<STR_LIT:_>':<EOL><INDENT>continue<EOL><DEDENT>is_global = name in self.global_vars<EOL>if self.do_compression:<EOL><INDENT>stream = BytesIO()<EOL>self._matrix_writer.file_stream = stream<EOL>self._matrix_writer.write_top(var, asbytes(name), is_global)<EOL>out_str = zlib.compress(stream.getvalue())<EOL>tag = np.empty((), NDT_TAG_FULL)<EOL>tag['<STR_LIT>'] = miCOMPRESSED<EOL>tag['<STR_LIT>'] = len(out_str)<EOL>self.file_stream.write(tag.tostring())<EOL>self.file_stream.write(out_str)<EOL><DEDENT>else:  <EOL><INDENT>self._matrix_writer.write_top(var, asbytes(name), is_global)<EOL><DEDENT><DEDENT>

Write variables in `mdict` to stream

        Parameters
        ----------
        mdict : mapping
           mapping with method ``items`` returns name, contents pairs where
           ``name`` which will appear 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


assert_equal(ncfileobj.history, b'<STR_LIT>')<EOL>time = ncfileobj.variables['<STR_LIT:time>']<EOL>assert_equal(time.units, b'<STR_LIT>')<EOL>assert_equal(time.shape, (N_EG_ELS,))<EOL>assert_equal(time[-<NUM_LIT:1>], N_EG_ELS-<NUM_LIT:1>)<EOL>


array = np.empty(len(args), dtype=np.object)<EOL>for i in range(len(args)):<EOL><INDENT>array[i] = args[i]<EOL><DEDENT>return array<EOL>


assert_equal(a, b)<EOL>if type(b) is np.str:<EOL><INDENT>assert_equal(type(a), type(b))<EOL><DEDENT>else:<EOL><INDENT>assert_equal(np.asarray(a).dtype.type, np.asarray(b).dtype.type)<EOL><DEDENT>

Assert whether value AND type are the same


assert_array_equal(a, b)<EOL>assert_equal(a.dtype.type, b.dtype.type)<EOL>

Assert whether values AND type are the same


if hasattr(filename,'<STR_LIT>'):<EOL><INDENT>fid = filename<EOL>mmap = False<EOL><DEDENT>else:<EOL><INDENT>fid = open(filename, '<STR_LIT:rb>')<EOL><DEDENT>try:<EOL><INDENT>fsize = _read_riff_chunk(fid)<EOL>noc = <NUM_LIT:1><EOL>bits = <NUM_LIT:8><EOL>comp = WAVE_FORMAT_PCM<EOL>while (fid.tell() < fsize):<EOL><INDENT>chunk_id = fid.read(<NUM_LIT:4>)<EOL>if chunk_id == b'<STR_LIT>':<EOL><INDENT>size, comp, noc, rate, sbytes, ba, bits = _read_fmt_chunk(fid)<EOL><DEDENT>elif chunk_id == b'<STR_LIT>':<EOL><INDENT>_skip_unknown_chunk(fid)<EOL><DEDENT>elif chunk_id == b'<STR_LIT:data>':<EOL><INDENT>data = _read_data_chunk(fid, comp, noc, bits, mmap=mmap)<EOL><DEDENT>elif chunk_id == b'<STR_LIT>':<EOL><INDENT>_skip_unknown_chunk(fid)<EOL><DEDENT>else:<EOL><INDENT>warnings.warn("<STR_LIT>",<EOL>WavFileWarning)<EOL>_skip_unknown_chunk(fid)<EOL><DEDENT><DEDENT><DEDENT>finally:<EOL><INDENT>if not hasattr(filename,'<STR_LIT>'):<EOL><INDENT>fid.close()<EOL><DEDENT>else:<EOL><INDENT>fid.seek(<NUM_LIT:0>)<EOL><DEDENT><DEDENT>return rate, data<EOL>

Return the sample rate (in samples/sec) and data from a WAV file

Parameters
----------
filename : string or open file handle
    Input wav file.
mmap : bool, optional
    Whether to read data as memory mapped.
    Only to be used on real files (Default: False)

    .. versionadded:: 0.12.0

Returns
-------
rate : int
    Sample rate of wav file
data : numpy array
    Data read from wav file

Notes
-----

* The file can be an open file or a filename.

* The returned sample rate is a Python integer
* The data is returned as a numpy array with a
  data-type determined from the file.


if hasattr(filename,'<STR_LIT>'):<EOL><INDENT>fid = filename<EOL><DEDENT>else:<EOL><INDENT>fid = open(filename, '<STR_LIT:wb>')<EOL><DEDENT>try:<EOL><INDENT>dkind = data.dtype.kind<EOL>if not (dkind == '<STR_LIT:i>' or dkind == '<STR_LIT:f>' or (dkind == '<STR_LIT:u>' and data.dtype.itemsize == <NUM_LIT:1>)):<EOL><INDENT>raise ValueError("<STR_LIT>" % data.dtype)<EOL><DEDENT>fid.write(b'<STR_LIT>')<EOL>fid.write(b'<STR_LIT>')<EOL>fid.write(b'<STR_LIT>')<EOL>fid.write(b'<STR_LIT>')<EOL>if dkind == '<STR_LIT:f>':<EOL><INDENT>comp = <NUM_LIT:3><EOL><DEDENT>else:<EOL><INDENT>comp = <NUM_LIT:1><EOL><DEDENT>if data.ndim == <NUM_LIT:1>:<EOL><INDENT>noc = <NUM_LIT:1><EOL><DEDENT>else:<EOL><INDENT>noc = data.shape[<NUM_LIT:1>]<EOL><DEDENT>bits = data.dtype.itemsize * <NUM_LIT:8><EOL>sbytes = rate*(bits // <NUM_LIT:8>)*noc<EOL>ba = noc * (bits // <NUM_LIT:8>)<EOL>fid.write(struct.pack('<STR_LIT>', <NUM_LIT:16>, comp, noc, rate, sbytes, ba, bits))<EOL>fid.write(b'<STR_LIT:data>')<EOL>fid.write(struct.pack('<STR_LIT>', data.nbytes))<EOL>if data.dtype.byteorder == '<STR_LIT:>>' or (data.dtype.byteorder == '<STR_LIT:=>' and sys.byteorder == '<STR_LIT>'):<EOL><INDENT>data = data.byteswap()<EOL><DEDENT>_array_tofile(fid, data)<EOL>size = fid.tell()<EOL>fid.seek(<NUM_LIT:4>)<EOL>fid.write(struct.pack('<STR_LIT>', size-<NUM_LIT:8>))<EOL><DEDENT>finally:<EOL><INDENT>if not hasattr(filename,'<STR_LIT>'):<EOL><INDENT>fid.close()<EOL><DEDENT>else:<EOL><INDENT>fid.seek(<NUM_LIT:0>)<EOL><DEDENT><DEDENT>

Write a numpy array as a WAV file

Parameters
----------
filename : string or open file handle
    Output wav file
rate : int
    The sample rate (in samples/sec).
data : ndarray
    A 1-D or 2-D numpy array of either integer or float data-type.

Notes
-----
* The file can be an open file or a filename.

* Writes a simple uncompressed WAV file.
* The bits-per-sample will be determined by the data-type.
* To write multiple-channels, use a 2-D array of shape
  (Nsamples, Nchannels).


s = np.array(s, order='<STR_LIT:F>')<EOL>np.array([s.nbytes],dtype=self._header_dtype).tofile(self._fp)<EOL>s.tofile(self._fp)<EOL>np.array([s.nbytes],dtype=self._header_dtype).tofile(self._fp)<EOL>

Write a record (including sizes) to the file.

Parameters
----------
s : array_like
   The data to write.


if dtype is None:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>dtype = np.dtype(dtype)<EOL>firstSize = self._read_size()<EOL>if firstSize % dtype.itemsize != <NUM_LIT:0>:<EOL><INDENT>raise ValueError('<STR_LIT>'.format(firstSize, dtype.itemsize))<EOL><DEDENT>data = np.fromfile(self._fp, dtype=dtype, count=firstSize//dtype.itemsize)<EOL>secondSize = self._read_size()<EOL>if firstSize != secondSize:<EOL><INDENT>raise IOError('<STR_LIT>')<EOL><DEDENT>return data<EOL>

Reads a record of a given type from the file.

Parameters
----------
dtype : data-type
    Data type specifying the size and endiness of the data.

Returns
-------
data : ndarray
    A one-dimensional array object.

Notes
-----
If the record contains a multi-dimensional array, calling reshape or
resize will restructure the array to the correct size.
Since Fortran multidimensional arrays are stored in column-major format,
this may have some non-intuitive consequences. If the variable was
declared as 'INTEGER var(5,4)', for example, var could be read with
'read_record(dtype=np.integer).reshape( (4,5) )' since Python uses
row-major ordering of indices.

One can transpose to obtain the indices in the same order as in Fortran.

For records that contain several variables or mixed types (as opposed
to single scalar or array types), it is possible to specify a dtype
with mixed types:

>>> record = f.read_record([('a', '<f4'), ('b', '<i4')])
>>> record['a']  # access the variable 'a'
5.6

and if any of the variables are arrays, the shape can be specified as
the third item in the relevant tuple:

>>> record = f.read_record([('a', '<f4'), ('b', '<i4', (3,3))])

Numpy also supports a short syntax for this kind of type:

>>> record = f.read_record('<f4,(3,3)<i4')
>>> record['f0']  # variables are called f0, f1, ...
5.6

See Also
--------
read_reals
read_ints


return self.read_record(dtype=dtype)<EOL>

Reads a record of a given type from the file, defaulting to an integer
type (INTEGER*4 in Fortran)

Parameters
----------
dtype : data-type
    Data type specifying the size and endiness of the data.

Returns
-------
data : ndarray
    A one-dimensional array object.

See Also
--------
read_reals
read_record


Reads a record of a given type from the file, defaulting to a floating
point number (real*8 in Fortran)

Parameters
----------
dtype : data-type
    Data type specifying the size and endiness of the data.

Returns
-------
data : ndarray
    A one-dimensional array object.

See Also
--------
read_ints
read_record


Closes the file. It is unsupported to call any other methods off this
object after closing it. Note that this class supports the 'with'
statement in modern versions of Python, to call this automatically


return MMFile.info(source)<EOL>

Queries the contents of the Matrix Market file 'filename' to
extract size and storage information.

Parameters
----------

source : file
    Matrix Market filename (extension .mtx) or open file object

Returns
-------

rows,cols : int
   Number of matrix rows and columns
entries : int
    Number of non-zero entries of a sparse matrix
    or rows*cols for a dense matrix
format : str
    Either 'coordinate' or 'array'.
field : str
    Either 'real', 'complex', 'pattern', or 'integer'.
symm : str
    Either 'general', 'symmetric', 'skew-symmetric', or 'hermitian'.


return MMFile().read(source)<EOL>

Reads the contents of a Matrix Market file 'filename' into a matrix.

Parameters
----------

source : file
    Matrix Market filename (extensions .mtx, .mtz.gz)
    or open file object.

Returns
-------
a:
    Sparse or full matrix


def mmwrite(target, a, comment='<STR_LIT>', field=None, precision=None):

MMFile().write(target, a, comment, field, precision)<EOL>

Writes the sparse or dense array `a` to a Matrix Market formatted file.

Parameters
----------
target : file
    Matrix Market filename (extension .mtx) or open file object
a : array like
    Sparse or dense 2D array
comment : str, optional
    comments to be prepended to the Matrix Market file
field : None or str, optional
    Either 'real', 'complex', 'pattern', or 'integer'.
precision : None or int, optional
    Number of digits to display for real or complex values.


if sys.version_info[<NUM_LIT:0>] < <NUM_LIT:3>:<EOL><INDENT>return True<EOL><DEDENT>bad_cls = []<EOL>try:<EOL><INDENT>import gzip<EOL>bad_cls.append(gzip.GzipFile)<EOL><DEDENT>except ImportError:<EOL><INDENT>pass<EOL><DEDENT>try:<EOL><INDENT>import bz2<EOL>bad_cls.append(bz2.BZ2File)<EOL><DEDENT>except ImportError:<EOL><INDENT>pass<EOL><DEDENT>bad_cls = tuple(bad_cls)<EOL>return not isinstance(stream, bad_cls)<EOL>

Check whether stream is compatible with numpy.fromfile.

Passing a gzipped file to fromfile/fromstring doesn't work
with Python3


close_it = False<EOL>if isinstance(filespec, string_types):<EOL><INDENT>close_it = True<EOL>if mode[<NUM_LIT:0>] == '<STR_LIT:r>':<EOL><INDENT>if not os.path.isfile(filespec):<EOL><INDENT>if os.path.isfile(filespec+'<STR_LIT>'):<EOL><INDENT>filespec = filespec + '<STR_LIT>'<EOL><DEDENT>elif os.path.isfile(filespec+'<STR_LIT>'):<EOL><INDENT>filespec = filespec + '<STR_LIT>'<EOL><DEDENT>elif os.path.isfile(filespec+'<STR_LIT>'):<EOL><INDENT>filespec = filespec + '<STR_LIT>'<EOL><DEDENT><DEDENT>if filespec.endswith('<STR_LIT>'):<EOL><INDENT>import gzip<EOL>stream = gzip.open(filespec, mode)<EOL><DEDENT>elif filespec.endswith('<STR_LIT>'):<EOL><INDENT>import bz2<EOL>stream = bz2.BZ2File(filespec, '<STR_LIT:rb>')<EOL><DEDENT>else:<EOL><INDENT>stream = open(filespec, mode)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if filespec[-<NUM_LIT:4>:] != '<STR_LIT>':<EOL><INDENT>filespec = filespec + '<STR_LIT>'<EOL><DEDENT>stream = open(filespec, mode)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>stream = filespec<EOL><DEDENT>return stream, close_it<EOL>

Return an open file stream for reading based on source.  If source is
a file name, open it (after trying to find it with mtx and gzipped mtx
extensions).  Otherwise, just return source.


attrs = self.__class__.__slots__<EOL>public_attrs = [attr[<NUM_LIT:1>:] for attr in attrs]<EOL>invalid_keys = set(kwargs.keys()) - set(public_attrs)<EOL>if invalid_keys:<EOL><INDENT>raise ValueError('<STR_LIT>' %<EOL>(tuple(invalid_keys), public_attrs))<EOL><DEDENT>for attr in attrs:<EOL><INDENT>setattr(self, attr, kwargs.get(attr[<NUM_LIT:1>:], None))<EOL><DEDENT>

Initialize each attributes with the corresponding keyword arg value
or a default of None


uattribute = attrtype.lower().strip()<EOL>if uattribute[<NUM_LIT:0>] == '<STR_LIT:{>':<EOL><INDENT>return '<STR_LIT>'<EOL><DEDENT>elif uattribute[:len('<STR_LIT>')] == '<STR_LIT>':<EOL><INDENT>return '<STR_LIT>'<EOL><DEDENT>elif uattribute[:len('<STR_LIT>')] == '<STR_LIT>':<EOL><INDENT>return '<STR_LIT>'<EOL><DEDENT>elif uattribute[:len('<STR_LIT>')] == '<STR_LIT>':<EOL><INDENT>return '<STR_LIT>'<EOL><DEDENT>elif uattribute[:len('<STR_LIT:string>')] == '<STR_LIT:string>':<EOL><INDENT>return '<STR_LIT:string>'<EOL><DEDENT>elif uattribute[:len('<STR_LIT>')] == '<STR_LIT>':<EOL><INDENT>return '<STR_LIT>'<EOL><DEDENT>elif uattribute[:len('<STR_LIT:date>')] == '<STR_LIT:date>':<EOL><INDENT>return '<STR_LIT:date>'<EOL><DEDENT>else:<EOL><INDENT>raise ParseArffError("<STR_LIT>" % uattribute)<EOL><DEDENT>

Given an arff attribute value (meta data), returns its type.

    Expect the value to be a name.


return attribute.split('<STR_LIT:U+002C>')<EOL>

If attribute is nominal, returns a list of the values


data = [next(ofile)]<EOL>if data[<NUM_LIT:0>].strip()[<NUM_LIT:0>] == '<STR_LIT:{>':<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>data.extend([i for i in ofile])<EOL>return data<EOL>

Read each line of the iterable and put it in a list.


data = [next(ofile)]<EOL>loc = <NUM_LIT:1><EOL>if data[<NUM_LIT:0>].strip()[<NUM_LIT:0>] == '<STR_LIT:{>':<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>for i in ofile:<EOL><INDENT>loc += <NUM_LIT:1><EOL><DEDENT>return loc<EOL>

Read the whole file to get number of data attributes.


nomtp = get_nom_val(atrv)<EOL>return max(len(i) for i in nomtp)<EOL>

Given a string containing a nominal type definition, returns the
    string len of the biggest component.

    A nominal type is defined as seomthing framed between brace ({}).

    Parameters
    ----------
    atrv : str
       Nominal type definition

    Returns
    -------
    slen : int
       length of longest component

    Examples
    --------
    maxnomlen("{floup, bouga, fl, ratata}") returns 6 (the size of
    ratata, the longest nominal value).

    >>> maxnomlen("{floup, bouga, fl, ratata}")
    6


r_nominal = re.compile('<STR_LIT>')<EOL>m = r_nominal.match(atrv)<EOL>if m:<EOL><INDENT>return tuple(i.strip() for i in m.group(<NUM_LIT:1>).split('<STR_LIT:U+002C>'))<EOL><DEDENT>else:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>

Given a string containing a nominal type, returns a tuple of the
    possible values.

    A nominal type is defined as something framed between braces ({}).

    Parameters
    ----------
    atrv : str
       Nominal type definition

    Returns
    -------
    poss_vals : tuple
       possible values

    Examples
    --------
    >>> get_nom_val("{floup, bouga, fl, ratata}")
    ('floup', 'bouga', 'fl', 'ratata')


return itertools.dropwhile(lambda x: not r_datameta.match(x), ofile)<EOL>

Skip header.

    the first next() call of the returned iterator will be the @data line


sattr = attribute.strip()<EOL>mattr = r_attribute.match(sattr)<EOL>if mattr:<EOL><INDENT>atrv = mattr.group(<NUM_LIT:1>)<EOL>if r_comattrval.match(atrv):<EOL><INDENT>name, type = tokenize_single_comma(atrv)<EOL>next_item = next(iterable)<EOL><DEDENT>elif r_wcomattrval.match(atrv):<EOL><INDENT>name, type = tokenize_single_wcomma(atrv)<EOL>next_item = next(iterable)<EOL><DEDENT>else:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT><DEDENT>else:<EOL><INDENT>raise ValueError("<STR_LIT>" % sattr)<EOL><DEDENT>if type == '<STR_LIT>':<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>return name, type, next_item<EOL>

Parse a raw string in header (eg starts by @attribute).

    Given a raw string attribute, try to get the name and type of the
    attribute. Constraints:

    * The first line must start with @attribute (case insensitive, and
      space like characters before @attribute are allowed)
    * Works also if the attribute is spread on multilines.
    * Works if empty lines or comments are in between

    Parameters
    ----------
    attribute : str
       the attribute string.

    Returns
    -------
    name : str
       name of the attribute
    value : str
       value of the attribute
    next : str
       next line to be parsed

    Examples
    --------
    If attribute is a string defined in python as r"floupi real", will
    return floupi as name, and real as value.

    >>> iterable = iter([0] * 10) # dummy iterator
    >>> tokenize_attribute(iterable, r"@attribute floupi real")
    ('floupi', 'real', 0)

    If attribute is r"'floupi 2' real", will return 'floupi 2' as name,
    and real as value.

    >>> tokenize_attribute(iterable, r"  @attribute 'floupi 2' real   ")
    ('floupi 2', 'real', 0)


i = next(ofile)<EOL>while r_comment.match(i):<EOL><INDENT>i = next(ofile)<EOL><DEDENT>relation = None<EOL>attributes = []<EOL>while not r_datameta.match(i):<EOL><INDENT>m = r_headerline.match(i)<EOL>if m:<EOL><INDENT>isattr = r_attribute.match(i)<EOL>if isattr:<EOL><INDENT>name, type, i = tokenize_attribute(ofile, i)<EOL>attributes.append((name, type))<EOL><DEDENT>else:<EOL><INDENT>isrel = r_relation.match(i)<EOL>if isrel:<EOL><INDENT>relation = isrel.group(<NUM_LIT:1>)<EOL><DEDENT>else:<EOL><INDENT>raise ValueError("<STR_LIT>" % i)<EOL><DEDENT>i = next(ofile)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>i = next(ofile)<EOL><DEDENT><DEDENT>return relation, attributes<EOL>


if '<STR_LIT:?>' in x:<EOL><INDENT>return np.nan<EOL><DEDENT>else:<EOL><INDENT>return np.float(x)<EOL><DEDENT>

given a string x, convert it to a float. If the stripped string is a ?,
    return a Nan (missing value).

    Parameters
    ----------
    x : str
       string to convert

    Returns
    -------
    f : float
       where float can be nan

    Examples
    --------
    >>> safe_float('1')
    1.0
    >>> safe_float('1\\n')
    1.0
    >>> safe_float('?\\n')
    nan


if '<STR_LIT:U+002C>' in line:<EOL><INDENT>return '<STR_LIT:U+002C>'<EOL><DEDENT>if '<STR_LIT:U+0020>' in line:<EOL><INDENT>return '<STR_LIT:U+0020>'<EOL><DEDENT>raise ValueError("<STR_LIT>" + line)<EOL>

Given a string representing a line of data, check whether the
    delimiter is ',' or space.

    Parameters
    ----------
    line : str
       line of data

    Returns
    -------
    delim : {',', ' '}

    Examples
    --------
    >>> get_delim(',')
    ','
    >>> get_delim(' ')
    ' '
    >>> get_delim(', ')
    ','
    >>> get_delim('x')
    Traceback (most recent call last):
       ...
    ValueError: delimiter not understood: x


if hasattr(f, '<STR_LIT>'):<EOL><INDENT>ofile = f<EOL><DEDENT>else:<EOL><INDENT>ofile = open(f, '<STR_LIT>')<EOL><DEDENT>try:<EOL><INDENT>return _loadarff(ofile)<EOL><DEDENT>finally:<EOL><INDENT>if ofile is not f:  <EOL><INDENT>ofile.close()<EOL><DEDENT><DEDENT>

Read an arff file.

The data is returned as a record array, which can be accessed much like
a dictionary of numpy arrays.  For example, if one of the attributes is
called 'pressure', then its first 10 data points can be accessed from the
``data`` record array like so: ``data['pressure'][0:10]``

Parameters
----------
f : file-like or str
   File-like object to read from, or filename to open.

Returns
-------
data : record array
   The data of the arff file, accessible by attribute names.
meta : `MetaData`
   Contains information about the arff file such as name and
   type of attributes, the relation (name of the dataset), etc...

Raises
------
`ParseArffError`
    This is raised if the given file is not ARFF-formatted.
NotImplementedError
    The ARFF file has an attribute which is not supported yet.

Notes
-----

This function should be able to read most arff files. Not
implemented functionality include:

* date type attributes
* string type attributes

It can read files with numeric and nominal attributes.  It cannot read
files with sparse data ({} in the file).  However, this function can
read files with missing data (? in the file), representing the data
points as NaNs.


return self._attrnames<EOL>


attr_types = [self._attributes[name][<NUM_LIT:0>] for name in self._attrnames]<EOL>return attr_types<EOL>


def __init__(self, filename, mode='<STR_LIT:r>', mmap=None, version=<NUM_LIT:1>):

if mode not in '<STR_LIT>':<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if hasattr(filename, '<STR_LIT>'):  <EOL><INDENT>self.fp = filename<EOL>self.filename = '<STR_LIT:None>'<EOL>if mmap is None:<EOL><INDENT>mmap = False<EOL><DEDENT>elif mmap and not hasattr(filename, '<STR_LIT>'):<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT><DEDENT>else:  <EOL><INDENT>self.filename = filename<EOL>omode = '<STR_LIT>' if mode == '<STR_LIT:a>' else mode<EOL>self.fp = open(self.filename, '<STR_LIT>' % omode)<EOL>if mmap is None:<EOL><INDENT>mmap = True<EOL><DEDENT><DEDENT>if mode != '<STR_LIT:r>':<EOL><INDENT>mmap = False<EOL><DEDENT>self.use_mmap = mmap<EOL>self.mode = mode<EOL>self.version_byte = version<EOL>self.dimensions = {}<EOL>self.variables = {}<EOL>self._dims = []<EOL>self._recs = <NUM_LIT:0><EOL>self._recsize = <NUM_LIT:0><EOL>self._mm = None<EOL>self._mm_buf = None<EOL>if self.use_mmap:<EOL><INDENT>self._mm = mm.mmap(self.fp.fileno(), <NUM_LIT:0>, access=mm.ACCESS_READ)<EOL>self._mm_buf = np.frombuffer(self._mm, dtype=np.int8)<EOL><DEDENT>self._attributes = {}<EOL>if mode in '<STR_LIT>':<EOL><INDENT>self._read()<EOL><DEDENT>

Initialize netcdf_file from fileobj (str or file-like).


if not self.fp.closed:<EOL><INDENT>try:<EOL><INDENT>self.flush()<EOL><DEDENT>finally:<EOL><INDENT>self.variables = {}<EOL>if self._mm_buf is not None:<EOL><INDENT>ref = weakref.ref(self._mm_buf)<EOL>self._mm_buf = None<EOL>if ref() is None:<EOL><INDENT>self._mm.close()<EOL><DEDENT>else:<EOL><INDENT>warnings.warn((<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>), category=RuntimeWarning)<EOL><DEDENT><DEDENT>self._mm = None<EOL>self.fp.close()<EOL><DEDENT><DEDENT>


def createDimension(self, name, length):

self.dimensions[name] = length<EOL>self._dims.append(name)<EOL>

Adds a dimension to the Dimension section of the NetCDF data structure.

Note that this function merely adds a new dimension that the variables can
reference.  The values for the dimension, if desired, should be added as
a variable using `createVariable`, referring to this dimension.

Parameters
----------
name : str
    Name of the dimension (Eg, 'lat' or 'time').
length : int
    Length of the dimension.

See Also
--------
createVariable


def createVariable(self, name, type, dimensions):

shape = tuple([self.dimensions[dim] for dim in dimensions])<EOL>shape_ = tuple([dim or <NUM_LIT:0> for dim in shape])  <EOL>type = dtype(type)<EOL>typecode, size = type.char, type.itemsize<EOL>if (typecode, size) not in REVERSE:<EOL><INDENT>raise ValueError("<STR_LIT>" % type)<EOL><DEDENT>data = empty(shape_, dtype=type.newbyteorder("<STR_LIT:B>"))  <EOL>self.variables[name] = netcdf_variable(data, typecode, size, shape, dimensions)<EOL>return self.variables[name]<EOL>

Create an empty variable for the `netcdf_file` object, specifying its data
type and the dimensions it uses.

Parameters
----------
name : str
    Name of the new variable.
type : dtype or str
    Data type of the variable.
dimensions : sequence of str
    List of the dimension names used by the variable, in the desired order.

Returns
-------
variable : netcdf_variable
    The newly created ``netcdf_variable`` object.
    This object has also been added to the `netcdf_file` object as well.

See Also
--------
createDimension

Notes
-----
Any dimensions to be used by the variable should already exist in the
NetCDF data structure or should be created by `createDimension` prior to
creating the NetCDF variable.


if hasattr(self, '<STR_LIT>') and self.mode in '<STR_LIT>':<EOL><INDENT>self._write()<EOL><DEDENT>

Perform a sync-to-disk flush if the `netcdf_file` object is in write mode.

See Also
--------
sync : Identical function


return bool(self.data.shape) and not self._shape[<NUM_LIT:0>]<EOL>

Returns whether the variable has a record dimension or not.

        A record dimension is a dimension along which additional data could be
        easily appended in the netcdf data structure without much rewriting of
        the data file. This attribute is a read-only property of the
        `netcdf_variable`.


return self.data.shape<EOL>

Returns the shape tuple of the data variable.

        This is a read-only attribute and can not be modified in the
        same manner of other numpy arrays.


return self.data.item()<EOL>

Retrieve a scalar value from a `netcdf_variable` of length one.

Raises
------
ValueError
    If the netcdf variable is an array of length greater than one,
    this exception will be raised.


if not self.data.flags.writeable:<EOL><INDENT>raise RuntimeError("<STR_LIT>")<EOL><DEDENT>self.data.itemset(value)<EOL>

Assign a scalar value to a `netcdf_variable` of length one.

Parameters
----------
value : scalar
    Scalar value (of compatible type) to assign to a length-one netcdf
    variable. This value will be written to file.

Raises
------
ValueError
    If the input is not a scalar, or if the destination is not a length-one
    netcdf variable.


return self._typecode<EOL>

Return the typecode of the variable.

Returns
-------
typecode : char
    The character typecode of the variable (eg, 'i' for int).


return self._size<EOL>

Return the itemsize of the variable.

Returns
-------
itemsize : int
    The element size of the variable (eg, 8 for float64).


pos = f.tell()<EOL>if pos % <NUM_LIT:4> != <NUM_LIT:0>:<EOL><INDENT>f.seek(pos + <NUM_LIT:4> - pos % <NUM_LIT:4>)<EOL><DEDENT>return<EOL>

Align to the next 32-bit position in a file


f.read(n)<EOL>return<EOL>


return f.read(n)<EOL>


return np.uint8(struct.unpack('<STR_LIT>', f.read(<NUM_LIT:4>)[:<NUM_LIT:1>])[<NUM_LIT:0>])<EOL>


return np.int32(struct.unpack('<STR_LIT>', f.read(<NUM_LIT:4>))[<NUM_LIT:0>])<EOL>


return np.int16(struct.unpack('<STR_LIT>', f.read(<NUM_LIT:4>)[<NUM_LIT:2>:<NUM_LIT:4>])[<NUM_LIT:0>])<EOL>


return np.int64(struct.unpack('<STR_LIT>', f.read(<NUM_LIT:8>))[<NUM_LIT:0>])<EOL>


return np.uint16(struct.unpack('<STR_LIT>', f.read(<NUM_LIT:4>)[<NUM_LIT:2>:<NUM_LIT:4>])[<NUM_LIT:0>])<EOL>


return np.uint32(struct.unpack('<STR_LIT>', f.read(<NUM_LIT:4>))[<NUM_LIT:0>])<EOL>


return np.uint64(struct.unpack('<STR_LIT>', f.read(<NUM_LIT:8>))[<NUM_LIT:0>])<EOL>


return np.float32(struct.unpack('<STR_LIT>', f.read(<NUM_LIT:4>))[<NUM_LIT:0>])<EOL>


return np.float64(struct.unpack('<STR_LIT>', f.read(<NUM_LIT:8>))[<NUM_LIT:0>])<EOL>


length = _read_long(f)<EOL>if length > <NUM_LIT:0>:<EOL><INDENT>chars = _read_bytes(f, length)<EOL>_align_32(f)<EOL>chars = asstr(chars)<EOL><DEDENT>else:<EOL><INDENT>chars = '<STR_LIT>'<EOL><DEDENT>return chars<EOL>


length = _read_long(f)<EOL>if length > <NUM_LIT:0>:<EOL><INDENT>length = _read_long(f)<EOL>string_data = _read_bytes(f, length)<EOL>_align_32(f)<EOL><DEDENT>else:<EOL><INDENT>string_data = '<STR_LIT>'<EOL><DEDENT>return string_data<EOL>

Read a data string (length is specified twice)


if dtype == <NUM_LIT:1>:<EOL><INDENT>if _read_int32(f) != <NUM_LIT:1>:<EOL><INDENT>raise Exception("<STR_LIT>")<EOL><DEDENT>return _read_byte(f)<EOL><DEDENT>elif dtype == <NUM_LIT:2>:<EOL><INDENT>return _read_int16(f)<EOL><DEDENT>elif dtype == <NUM_LIT:3>:<EOL><INDENT>return _read_int32(f)<EOL><DEDENT>elif dtype == <NUM_LIT:4>:<EOL><INDENT>return _read_float32(f)<EOL><DEDENT>elif dtype == <NUM_LIT:5>:<EOL><INDENT>return _read_float64(f)<EOL><DEDENT>elif dtype == <NUM_LIT:6>:<EOL><INDENT>real = _read_float32(f)<EOL>imag = _read_float32(f)<EOL>return np.complex64(real + imag * <NUM_LIT>)<EOL><DEDENT>elif dtype == <NUM_LIT:7>:<EOL><INDENT>return _read_string_data(f)<EOL><DEDENT>elif dtype == <NUM_LIT:8>:<EOL><INDENT>raise Exception("<STR_LIT>")<EOL><DEDENT>elif dtype == <NUM_LIT:9>:<EOL><INDENT>real = _read_float64(f)<EOL>imag = _read_float64(f)<EOL>return np.complex128(real + imag * <NUM_LIT>)<EOL><DEDENT>elif dtype == <NUM_LIT:10>:<EOL><INDENT>return Pointer(_read_int32(f))<EOL><DEDENT>elif dtype == <NUM_LIT:11>:<EOL><INDENT>return ObjectPointer(_read_int32(f))<EOL><DEDENT>elif dtype == <NUM_LIT:12>:<EOL><INDENT>return _read_uint16(f)<EOL><DEDENT>elif dtype == <NUM_LIT>:<EOL><INDENT>return _read_uint32(f)<EOL><DEDENT>elif dtype == <NUM_LIT>:<EOL><INDENT>return _read_int64(f)<EOL><DEDENT>elif dtype == <NUM_LIT:15>:<EOL><INDENT>return _read_uint64(f)<EOL><DEDENT>else:<EOL><INDENT>raise Exception("<STR_LIT>" % dtype)<EOL><DEDENT>

Read a variable with a specified data type


def _read_structure(f, array_desc, struct_desc):

nrows = array_desc['<STR_LIT>']<EOL>columns = struct_desc['<STR_LIT>']<EOL>dtype = []<EOL>for col in columns:<EOL><INDENT>if col['<STR_LIT>'] or col['<STR_LIT>']:<EOL><INDENT>dtype.append(((col['<STR_LIT:name>'].lower(), col['<STR_LIT:name>']), np.object_))<EOL><DEDENT>else:<EOL><INDENT>if col['<STR_LIT>'] in DTYPE_DICT:<EOL><INDENT>dtype.append(((col['<STR_LIT:name>'].lower(), col['<STR_LIT:name>']),<EOL>DTYPE_DICT[col['<STR_LIT>']]))<EOL><DEDENT>else:<EOL><INDENT>raise Exception("<STR_LIT>" %<EOL>col['<STR_LIT>'])<EOL><DEDENT><DEDENT><DEDENT>structure = np.recarray((nrows, ), dtype=dtype)<EOL>for i in range(nrows):<EOL><INDENT>for col in columns:<EOL><INDENT>dtype = col['<STR_LIT>']<EOL>if col['<STR_LIT>']:<EOL><INDENT>structure[col['<STR_LIT:name>']][i] = _read_structure(f,<EOL>struct_desc['<STR_LIT>'][col['<STR_LIT:name>']],<EOL>struct_desc['<STR_LIT>'][col['<STR_LIT:name>']])<EOL><DEDENT>elif col['<STR_LIT>']:<EOL><INDENT>structure[col['<STR_LIT:name>']][i] = _read_array(f, dtype,<EOL>struct_desc['<STR_LIT>'][col['<STR_LIT:name>']])<EOL><DEDENT>else:<EOL><INDENT>structure[col['<STR_LIT:name>']][i] = _read_data(f, dtype)<EOL><DEDENT><DEDENT><DEDENT>if array_desc['<STR_LIT>'] > <NUM_LIT:1>:<EOL><INDENT>dims = array_desc['<STR_LIT>'][:int(array_desc['<STR_LIT>'])]<EOL>dims.reverse()<EOL>structure = structure.reshape(dims)<EOL><DEDENT>return structure<EOL>

Read a structure, with the array and structure descriptors given as
`array_desc` and `structure_desc` respectively.


def _read_array(f, typecode, array_desc):

if typecode in [<NUM_LIT:1>, <NUM_LIT:3>, <NUM_LIT:4>, <NUM_LIT:5>, <NUM_LIT:6>, <NUM_LIT:9>, <NUM_LIT>, <NUM_LIT>, <NUM_LIT:15>]:<EOL><INDENT>if typecode == <NUM_LIT:1>:<EOL><INDENT>nbytes = _read_int32(f)<EOL>if nbytes != array_desc['<STR_LIT>']:<EOL><INDENT>raise Exception("<STR_LIT>")<EOL><DEDENT><DEDENT>array = np.fromstring(f.read(array_desc['<STR_LIT>']),<EOL>dtype=DTYPE_DICT[typecode])<EOL><DEDENT>elif typecode in [<NUM_LIT:2>, <NUM_LIT:12>]:<EOL><INDENT>array = np.fromstring(f.read(array_desc['<STR_LIT>']*<NUM_LIT:2>),<EOL>dtype=DTYPE_DICT[typecode])[<NUM_LIT:1>::<NUM_LIT:2>]<EOL><DEDENT>else:<EOL><INDENT>array = []<EOL>for i in range(array_desc['<STR_LIT>']):<EOL><INDENT>dtype = typecode<EOL>data = _read_data(f, dtype)<EOL>array.append(data)<EOL><DEDENT>array = np.array(array, dtype=np.object_)<EOL><DEDENT>if array_desc['<STR_LIT>'] > <NUM_LIT:1>:<EOL><INDENT>dims = array_desc['<STR_LIT>'][:int(array_desc['<STR_LIT>'])]<EOL>dims.reverse()<EOL>array = array.reshape(dims)<EOL><DEDENT>_align_32(f)<EOL>return array<EOL>

Read an array of type `typecode`, with the array descriptor given as
`array_desc`.


record = {}<EOL>record['<STR_LIT>'] = _read_long(f)<EOL>nextrec = _read_uint32(f)<EOL>nextrec += _read_uint32(f) * <NUM_LIT:2>**<NUM_LIT:32><EOL>_skip_bytes(f, <NUM_LIT:4>)<EOL>if not record['<STR_LIT>'] in RECTYPE_DICT:<EOL><INDENT>raise Exception("<STR_LIT>" % record['<STR_LIT>'])<EOL><DEDENT>record['<STR_LIT>'] = RECTYPE_DICT[record['<STR_LIT>']]<EOL>if record['<STR_LIT>'] in ["<STR_LIT>", "<STR_LIT>"]:<EOL><INDENT>if record['<STR_LIT>'] == "<STR_LIT>":<EOL><INDENT>record['<STR_LIT>'] = _read_string(f)<EOL><DEDENT>else:<EOL><INDENT>record['<STR_LIT>'] = _read_long(f)<EOL>_skip_bytes(f, <NUM_LIT:4>)<EOL><DEDENT>rectypedesc = _read_typedesc(f)<EOL>if rectypedesc['<STR_LIT>'] == <NUM_LIT:0>:<EOL><INDENT>if nextrec == f.tell():<EOL><INDENT>record['<STR_LIT:data>'] = None  <EOL><DEDENT>else:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT><DEDENT>else:<EOL><INDENT>varstart = _read_long(f)<EOL>if varstart != <NUM_LIT:7>:<EOL><INDENT>raise Exception("<STR_LIT>")<EOL><DEDENT>if rectypedesc['<STR_LIT>']:<EOL><INDENT>record['<STR_LIT:data>'] = _read_structure(f, rectypedesc['<STR_LIT>'],<EOL>rectypedesc['<STR_LIT>'])<EOL><DEDENT>elif rectypedesc['<STR_LIT>']:<EOL><INDENT>record['<STR_LIT:data>'] = _read_array(f, rectypedesc['<STR_LIT>'],<EOL>rectypedesc['<STR_LIT>'])<EOL><DEDENT>else:<EOL><INDENT>dtype = rectypedesc['<STR_LIT>']<EOL>record['<STR_LIT:data>'] = _read_data(f, dtype)<EOL><DEDENT><DEDENT><DEDENT>elif record['<STR_LIT>'] == "<STR_LIT>":<EOL><INDENT>_skip_bytes(f, <NUM_LIT:4>*<NUM_LIT>)<EOL>record['<STR_LIT:date>'] = _read_string(f)<EOL>record['<STR_LIT:user>'] = _read_string(f)<EOL>record['<STR_LIT:host>'] = _read_string(f)<EOL><DEDENT>elif record['<STR_LIT>'] == "<STR_LIT>":<EOL><INDENT>record['<STR_LIT>'] = _read_long(f)<EOL>record['<STR_LIT>'] = _read_string(f)<EOL>record['<STR_LIT>'] = _read_string(f)<EOL>record['<STR_LIT>'] = _read_string(f)<EOL><DEDENT>elif record['<STR_LIT>'] == "<STR_LIT>":<EOL><INDENT>record['<STR_LIT>'] = _read_string(f)<EOL>record['<STR_LIT:title>'] = _read_string(f)<EOL>record['<STR_LIT>'] = _read_string(f)<EOL><DEDENT>elif record['<STR_LIT>'] == "<STR_LIT>":<EOL><INDENT>record['<STR_LIT>'] = _read_string(f)<EOL><DEDENT>elif record['<STR_LIT>'] == "<STR_LIT>":<EOL><INDENT>record['<STR_LIT:description>'] = _read_string_data(f)<EOL><DEDENT>elif record['<STR_LIT>'] == "<STR_LIT>":<EOL><INDENT>record['<STR_LIT>'] = _read_long(f)<EOL>record['<STR_LIT>'] = []<EOL>for i in range(record['<STR_LIT>']):<EOL><INDENT>record['<STR_LIT>'].append(_read_long(f))<EOL><DEDENT><DEDENT>elif record['<STR_LIT>'] == "<STR_LIT>":<EOL><INDENT>record['<STR_LIT>'] = _read_long(f)<EOL>record['<STR_LIT:name>'] = _read_string(f)<EOL>record['<STR_LIT>'] = []<EOL>for i in range(record['<STR_LIT>']):<EOL><INDENT>record['<STR_LIT>'].append(_read_string(f))<EOL><DEDENT><DEDENT>elif record['<STR_LIT>'] == "<STR_LIT>":<EOL><INDENT>record['<STR_LIT:end>'] = True<EOL><DEDENT>elif record['<STR_LIT>'] == "<STR_LIT>":<EOL><INDENT>warnings.warn("<STR_LIT>")<EOL><DEDENT>elif record['<STR_LIT>'] == "<STR_LIT>":<EOL><INDENT>warnings.warn("<STR_LIT>")<EOL><DEDENT>else:<EOL><INDENT>raise Exception("<STR_LIT>" %<EOL>record['<STR_LIT>'])<EOL><DEDENT>f.seek(nextrec)<EOL>return record<EOL>


typedesc = {}<EOL>typedesc['<STR_LIT>'] = _read_long(f)<EOL>typedesc['<STR_LIT>'] = _read_long(f)<EOL>if typedesc['<STR_LIT>'] & <NUM_LIT:2> == <NUM_LIT:2>:<EOL><INDENT>raise Exception("<STR_LIT>")<EOL><DEDENT>typedesc['<STR_LIT>'] = typedesc['<STR_LIT>'] & <NUM_LIT:4> == <NUM_LIT:4><EOL>typedesc['<STR_LIT>'] = typedesc['<STR_LIT>'] & <NUM_LIT:32> == <NUM_LIT:32><EOL>if typedesc['<STR_LIT>']:<EOL><INDENT>typedesc['<STR_LIT>'] = _read_arraydesc(f)<EOL>typedesc['<STR_LIT>'] = _read_structdesc(f)<EOL><DEDENT>elif typedesc['<STR_LIT>']:<EOL><INDENT>typedesc['<STR_LIT>'] = _read_arraydesc(f)<EOL><DEDENT>return typedesc<EOL>