Blame numpy/lib/npyio.py

Packit Service a47777
from __future__ import division, absolute_import, print_function
Packit Service a47777
Packit Service a47777
import io
Packit Service a47777
import sys
Packit Service a47777
import os
Packit Service a47777
import re
Packit Service a47777
import itertools
Packit Service a47777
import warnings
Packit Service a47777
import weakref
Packit Service a47777
from operator import itemgetter, index as opindex
Packit Service a47777
Packit Service a47777
import numpy as np
Packit Service a47777
from . import format
Packit Service a47777
from ._datasource import DataSource
Packit Service a47777
from numpy.core.multiarray import packbits, unpackbits
Packit Service a47777
from ._iotools import (
Packit Service a47777
    LineSplitter, NameValidator, StringConverter, ConverterError,
Packit Service a47777
    ConverterLockError, ConversionWarning, _is_string_like,
Packit Service a47777
    has_nested_fields, flatten_dtype, easy_dtype, _decode_line
Packit Service a47777
    )
Packit Service a47777
Packit Service a47777
from numpy.compat import (
Packit Service a47777
    asbytes, asstr, asunicode, asbytes_nested, bytes, basestring, unicode,
Packit Service a47777
    is_pathlib_path
Packit Service a47777
    )
Packit Service a47777
Packit Service a47777
if sys.version_info[0] >= 3:
Packit Service a47777
    import pickle
Packit Service a47777
else:
Packit Service a47777
    import cPickle as pickle
Packit Service a47777
    from future_builtins import map
Packit Service a47777
Packit Service a47777
loads = pickle.loads
Packit Service a47777
Packit Service a47777
__all__ = [
Packit Service a47777
    'savetxt', 'loadtxt', 'genfromtxt', 'ndfromtxt', 'mafromtxt',
Packit Service a47777
    'recfromtxt', 'recfromcsv', 'load', 'loads', 'save', 'savez',
Packit Service a47777
    'savez_compressed', 'packbits', 'unpackbits', 'fromregex', 'DataSource'
Packit Service a47777
    ]
Packit Service a47777
Packit Service a47777
Packit Service a47777
class BagObj(object):
Packit Service a47777
    """
Packit Service a47777
    BagObj(obj)
Packit Service a47777
Packit Service a47777
    Convert attribute look-ups to getitems on the object passed in.
Packit Service a47777
Packit Service a47777
    Parameters
Packit Service a47777
    ----------
Packit Service a47777
    obj : class instance
Packit Service a47777
        Object on which attribute look-up is performed.
Packit Service a47777
Packit Service a47777
    Examples
Packit Service a47777
    --------
Packit Service a47777
    >>> from numpy.lib.npyio import BagObj as BO
Packit Service a47777
    >>> class BagDemo(object):
Packit Service a47777
    ...     def __getitem__(self, key): # An instance of BagObj(BagDemo)
Packit Service a47777
    ...                                 # will call this method when any
Packit Service a47777
    ...                                 # attribute look-up is required
Packit Service a47777
    ...         result = "Doesn't matter what you want, "
Packit Service a47777
    ...         return result + "you're gonna get this"
Packit Service a47777
    ...
Packit Service a47777
    >>> demo_obj = BagDemo()
Packit Service a47777
    >>> bagobj = BO(demo_obj)
Packit Service a47777
    >>> bagobj.hello_there
Packit Service a47777
    "Doesn't matter what you want, you're gonna get this"
Packit Service a47777
    >>> bagobj.I_can_be_anything
Packit Service a47777
    "Doesn't matter what you want, you're gonna get this"
Packit Service a47777
Packit Service a47777
    """
Packit Service a47777
Packit Service a47777
    def __init__(self, obj):
Packit Service a47777
        # Use weakref to make NpzFile objects collectable by refcount
Packit Service a47777
        self._obj = weakref.proxy(obj)
Packit Service a47777
Packit Service a47777
    def __getattribute__(self, key):
Packit Service a47777
        try:
Packit Service a47777
            return object.__getattribute__(self, '_obj')[key]
Packit Service a47777
        except KeyError:
Packit Service a47777
            raise AttributeError(key)
Packit Service a47777
Packit Service a47777
    def __dir__(self):
Packit Service a47777
        """
Packit Service a47777
        Enables dir(bagobj) to list the files in an NpzFile.
Packit Service a47777
Packit Service a47777
        This also enables tab-completion in an interpreter or IPython.
Packit Service a47777
        """
Packit Service a47777
        return object.__getattribute__(self, '_obj').keys()
Packit Service a47777
Packit Service a47777
Packit Service a47777
def zipfile_factory(file, *args, **kwargs):
Packit Service a47777
    """
Packit Service a47777
    Create a ZipFile.
Packit Service a47777
Packit Service a47777
    Allows for Zip64, and the `file` argument can accept file, str, or
Packit Service a47777
    pathlib.Path objects. `args` and `kwargs` are passed to the zipfile.ZipFile
Packit Service a47777
    constructor.
Packit Service a47777
    """
Packit Service a47777
    if is_pathlib_path(file):
Packit Service a47777
        file = str(file)
Packit Service a47777
    import zipfile
Packit Service a47777
    kwargs['allowZip64'] = True
Packit Service a47777
    return zipfile.ZipFile(file, *args, **kwargs)
Packit Service a47777
Packit Service a47777
Packit Service a47777
class NpzFile(object):
Packit Service a47777
    """
Packit Service a47777
    NpzFile(fid)
Packit Service a47777
Packit Service a47777
    A dictionary-like object with lazy-loading of files in the zipped
Packit Service a47777
    archive provided on construction.
Packit Service a47777
Packit Service a47777
    `NpzFile` is used to load files in the NumPy ``.npz`` data archive
Packit Service a47777
    format. It assumes that files in the archive have a ``.npy`` extension,
Packit Service a47777
    other files are ignored.
Packit Service a47777
Packit Service a47777
    The arrays and file strings are lazily loaded on either
Packit Service a47777
    getitem access using ``obj['key']`` or attribute lookup using
Packit Service a47777
    ``obj.f.key``. A list of all files (without ``.npy`` extensions) can
Packit Service a47777
    be obtained with ``obj.files`` and the ZipFile object itself using
Packit Service a47777
    ``obj.zip``.
Packit Service a47777
Packit Service a47777
    Attributes
Packit Service a47777
    ----------
Packit Service a47777
    files : list of str
Packit Service a47777
        List of all files in the archive with a ``.npy`` extension.
Packit Service a47777
    zip : ZipFile instance
Packit Service a47777
        The ZipFile object initialized with the zipped archive.
Packit Service a47777
    f : BagObj instance
Packit Service a47777
        An object on which attribute can be performed as an alternative
Packit Service a47777
        to getitem access on the `NpzFile` instance itself.
Packit Service a47777
    allow_pickle : bool, optional
Packit Service dce907
        Allow loading pickled data. Default: False
Packit Service dce907
Packit Service dce907
        .. versionchanged:: 1.14.3
Packit Service dce907
            Made default False in response to CVE-2019-6446.
Packit Service dce907
Packit Service a47777
    pickle_kwargs : dict, optional
Packit Service a47777
        Additional keyword arguments to pass on to pickle.load.
Packit Service a47777
        These are only useful when loading object arrays saved on
Packit Service a47777
        Python 2 when using Python 3.
Packit Service a47777
Packit Service a47777
    Parameters
Packit Service a47777
    ----------
Packit Service a47777
    fid : file or str
Packit Service a47777
        The zipped archive to open. This is either a file-like object
Packit Service a47777
        or a string containing the path to the archive.
Packit Service a47777
    own_fid : bool, optional
Packit Service a47777
        Whether NpzFile should close the file handle.
Packit Service a47777
        Requires that `fid` is a file-like object.
Packit Service a47777
Packit Service a47777
    Examples
Packit Service a47777
    --------
Packit Service a47777
    >>> from tempfile import TemporaryFile
Packit Service a47777
    >>> outfile = TemporaryFile()
Packit Service a47777
    >>> x = np.arange(10)
Packit Service a47777
    >>> y = np.sin(x)
Packit Service a47777
    >>> np.savez(outfile, x=x, y=y)
Packit Service a47777
    >>> outfile.seek(0)
Packit Service a47777
Packit Service a47777
    >>> npz = np.load(outfile)
Packit Service a47777
    >>> isinstance(npz, np.lib.io.NpzFile)
Packit Service a47777
    True
Packit Service a47777
    >>> npz.files
Packit Service a47777
    ['y', 'x']
Packit Service a47777
    >>> npz['x']  # getitem access
Packit Service a47777
    array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
Packit Service a47777
    >>> npz.f.x  # attribute lookup
Packit Service a47777
    array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
Packit Service a47777
Packit Service a47777
    """
Packit Service a47777
Packit Service dce907
    def __init__(self, fid, own_fid=False, allow_pickle=False,
Packit Service a47777
                 pickle_kwargs=None):
Packit Service a47777
        # Import is postponed to here since zipfile depends on gzip, an
Packit Service a47777
        # optional component of the so-called standard library.
Packit Service a47777
        _zip = zipfile_factory(fid)
Packit Service a47777
        self._files = _zip.namelist()
Packit Service a47777
        self.files = []
Packit Service a47777
        self.allow_pickle = allow_pickle
Packit Service a47777
        self.pickle_kwargs = pickle_kwargs
Packit Service a47777
        for x in self._files:
Packit Service a47777
            if x.endswith('.npy'):
Packit Service a47777
                self.files.append(x[:-4])
Packit Service a47777
            else:
Packit Service a47777
                self.files.append(x)
Packit Service a47777
        self.zip = _zip
Packit Service a47777
        self.f = BagObj(self)
Packit Service a47777
        if own_fid:
Packit Service a47777
            self.fid = fid
Packit Service a47777
        else:
Packit Service a47777
            self.fid = None
Packit Service a47777
Packit Service a47777
    def __enter__(self):
Packit Service a47777
        return self
Packit Service a47777
Packit Service a47777
    def __exit__(self, exc_type, exc_value, traceback):
Packit Service a47777
        self.close()
Packit Service a47777
Packit Service a47777
    def close(self):
Packit Service a47777
        """
Packit Service a47777
        Close the file.
Packit Service a47777
Packit Service a47777
        """
Packit Service a47777
        if self.zip is not None:
Packit Service a47777
            self.zip.close()
Packit Service a47777
            self.zip = None
Packit Service a47777
        if self.fid is not None:
Packit Service a47777
            self.fid.close()
Packit Service a47777
            self.fid = None
Packit Service a47777
        self.f = None  # break reference cycle
Packit Service a47777
Packit Service a47777
    def __del__(self):
Packit Service a47777
        self.close()
Packit Service a47777
Packit Service a47777
    def __getitem__(self, key):
Packit Service a47777
        # FIXME: This seems like it will copy strings around
Packit Service a47777
        #   more than is strictly necessary.  The zipfile
Packit Service a47777
        #   will read the string and then
Packit Service a47777
        #   the format.read_array will copy the string
Packit Service a47777
        #   to another place in memory.
Packit Service a47777
        #   It would be better if the zipfile could read
Packit Service a47777
        #   (or at least uncompress) the data
Packit Service a47777
        #   directly into the array memory.
Packit Service a47777
        member = 0
Packit Service a47777
        if key in self._files:
Packit Service a47777
            member = 1
Packit Service a47777
        elif key in self.files:
Packit Service a47777
            member = 1
Packit Service a47777
            key += '.npy'
Packit Service a47777
        if member:
Packit Service a47777
            bytes = self.zip.open(key)
Packit Service a47777
            magic = bytes.read(len(format.MAGIC_PREFIX))
Packit Service a47777
            bytes.close()
Packit Service a47777
            if magic == format.MAGIC_PREFIX:
Packit Service a47777
                bytes = self.zip.open(key)
Packit Service a47777
                return format.read_array(bytes,
Packit Service a47777
                                         allow_pickle=self.allow_pickle,
Packit Service a47777
                                         pickle_kwargs=self.pickle_kwargs)
Packit Service a47777
            else:
Packit Service a47777
                return self.zip.read(key)
Packit Service a47777
        else:
Packit Service a47777
            raise KeyError("%s is not a file in the archive" % key)
Packit Service a47777
Packit Service a47777
    def __iter__(self):
Packit Service a47777
        return iter(self.files)
Packit Service a47777
Packit Service a47777
    def items(self):
Packit Service a47777
        """
Packit Service a47777
        Return a list of tuples, with each tuple (filename, array in file).
Packit Service a47777
Packit Service a47777
        """
Packit Service a47777
        return [(f, self[f]) for f in self.files]
Packit Service a47777
Packit Service a47777
    def iteritems(self):
Packit Service a47777
        """Generator that returns tuples (filename, array in file)."""
Packit Service a47777
        for f in self.files:
Packit Service a47777
            yield (f, self[f])
Packit Service a47777
Packit Service a47777
    def keys(self):
Packit Service a47777
        """Return files in the archive with a ``.npy`` extension."""
Packit Service a47777
        return self.files
Packit Service a47777
Packit Service a47777
    def iterkeys(self):
Packit Service a47777
        """Return an iterator over the files in the archive."""
Packit Service a47777
        return self.__iter__()
Packit Service a47777
Packit Service a47777
    def __contains__(self, key):
Packit Service a47777
        return self.files.__contains__(key)
Packit Service a47777
Packit Service a47777
Packit Service dce907
def load(file, mmap_mode=None, allow_pickle=False, fix_imports=True,
Packit Service a47777
         encoding='ASCII'):
Packit Service a47777
    """
Packit Service a47777
    Load arrays or pickled objects from ``.npy``, ``.npz`` or pickled files.
Packit Service a47777
Packit Service a47777
    Parameters
Packit Service a47777
    ----------
Packit Service a47777
    file : file-like object, string, or pathlib.Path
Packit Service a47777
        The file to read. File-like objects must support the
Packit Service a47777
        ``seek()`` and ``read()`` methods. Pickled files require that the
Packit Service a47777
        file-like object support the ``readline()`` method as well.
Packit Service a47777
    mmap_mode : {None, 'r+', 'r', 'w+', 'c'}, optional
Packit Service a47777
        If not None, then memory-map the file, using the given mode (see
Packit Service a47777
        `numpy.memmap` for a detailed description of the modes).  A
Packit Service a47777
        memory-mapped array is kept on disk. However, it can be accessed
Packit Service a47777
        and sliced like any ndarray.  Memory mapping is especially useful
Packit Service a47777
        for accessing small fragments of large files without reading the
Packit Service a47777
        entire file into memory.
Packit Service a47777
    allow_pickle : bool, optional
Packit Service a47777
        Allow loading pickled object arrays stored in npy files. Reasons for
Packit Service a47777
        disallowing pickles include security, as loading pickled data can
Packit Service a47777
        execute arbitrary code. If pickles are disallowed, loading object
Packit Service dce907
        arrays will fail. Default: False
Packit Service dce907
Packit Service dce907
        .. versionchanged:: 1.14.3
Packit Service dce907
            Made default False in response to CVE-2019-6446.
Packit Service dce907
Packit Service a47777
    fix_imports : bool, optional
Packit Service a47777
        Only useful when loading Python 2 generated pickled files on Python 3,
Packit Service a47777
        which includes npy/npz files containing object arrays. If `fix_imports`
Packit Service a47777
        is True, pickle will try to map the old Python 2 names to the new names
Packit Service a47777
        used in Python 3.
Packit Service a47777
    encoding : str, optional
Packit Service a47777
        What encoding to use when reading Python 2 strings. Only useful when
Packit Service a47777
        loading Python 2 generated pickled files in Python 3, which includes
Packit Service a47777
        npy/npz files containing object arrays. Values other than 'latin1',
Packit Service a47777
        'ASCII', and 'bytes' are not allowed, as they can corrupt numerical
Packit Service a47777
        data. Default: 'ASCII'
Packit Service a47777
Packit Service a47777
    Returns
Packit Service a47777
    -------
Packit Service a47777
    result : array, tuple, dict, etc.
Packit Service a47777
        Data stored in the file. For ``.npz`` files, the returned instance
Packit Service a47777
        of NpzFile class must be closed to avoid leaking file descriptors.
Packit Service a47777
Packit Service a47777
    Raises
Packit Service a47777
    ------
Packit Service a47777
    IOError
Packit Service a47777
        If the input file does not exist or cannot be read.
Packit Service a47777
    ValueError
Packit Service a47777
        The file contains an object array, but allow_pickle=False given.
Packit Service a47777
Packit Service a47777
    See Also
Packit Service a47777
    --------
Packit Service a47777
    save, savez, savez_compressed, loadtxt
Packit Service a47777
    memmap : Create a memory-map to an array stored in a file on disk.
Packit Service a47777
    lib.format.open_memmap : Create or load a memory-mapped ``.npy`` file.
Packit Service a47777
Packit Service a47777
    Notes
Packit Service a47777
    -----
Packit Service a47777
    - If the file contains pickle data, then whatever object is stored
Packit Service a47777
      in the pickle is returned.
Packit Service a47777
    - If the file is a ``.npy`` file, then a single array is returned.
Packit Service a47777
    - If the file is a ``.npz`` file, then a dictionary-like object is
Packit Service a47777
      returned, containing ``{filename: array}`` key-value pairs, one for
Packit Service a47777
      each file in the archive.
Packit Service a47777
    - If the file is a ``.npz`` file, the returned value supports the
Packit Service a47777
      context manager protocol in a similar fashion to the open function::
Packit Service a47777
Packit Service a47777
        with load('foo.npz') as data:
Packit Service a47777
            a = data['a']
Packit Service a47777
Packit Service a47777
      The underlying file descriptor is closed when exiting the 'with'
Packit Service a47777
      block.
Packit Service a47777
Packit Service a47777
    Examples
Packit Service a47777
    --------
Packit Service a47777
    Store data to disk, and load it again:
Packit Service a47777
Packit Service a47777
    >>> np.save('/tmp/123', np.array([[1, 2, 3], [4, 5, 6]]))
Packit Service a47777
    >>> np.load('/tmp/123.npy')
Packit Service a47777
    array([[1, 2, 3],
Packit Service a47777
           [4, 5, 6]])
Packit Service a47777
Packit Service a47777
    Store compressed data to disk, and load it again:
Packit Service a47777
Packit Service a47777
    >>> a=np.array([[1, 2, 3], [4, 5, 6]])
Packit Service a47777
    >>> b=np.array([1, 2])
Packit Service a47777
    >>> np.savez('/tmp/123.npz', a=a, b=b)
Packit Service a47777
    >>> data = np.load('/tmp/123.npz')
Packit Service a47777
    >>> data['a']
Packit Service a47777
    array([[1, 2, 3],
Packit Service a47777
           [4, 5, 6]])
Packit Service a47777
    >>> data['b']
Packit Service a47777
    array([1, 2])
Packit Service a47777
    >>> data.close()
Packit Service a47777
Packit Service a47777
    Mem-map the stored array, and then access the second row
Packit Service a47777
    directly from disk:
Packit Service a47777
Packit Service a47777
    >>> X = np.load('/tmp/123.npy', mmap_mode='r')
Packit Service a47777
    >>> X[1, :]
Packit Service a47777
    memmap([4, 5, 6])
Packit Service a47777
Packit Service a47777
    """
Packit Service a47777
    own_fid = False
Packit Service a47777
    if isinstance(file, basestring):
Packit Service a47777
        fid = open(file, "rb")
Packit Service a47777
        own_fid = True
Packit Service a47777
    elif is_pathlib_path(file):
Packit Service a47777
        fid = file.open("rb")
Packit Service a47777
        own_fid = True
Packit Service a47777
    else:
Packit Service a47777
        fid = file
Packit Service a47777
Packit Service a47777
    if encoding not in ('ASCII', 'latin1', 'bytes'):
Packit Service a47777
        # The 'encoding' value for pickle also affects what encoding
Packit Service a47777
        # the serialized binary data of NumPy arrays is loaded
Packit Service a47777
        # in. Pickle does not pass on the encoding information to
Packit Service a47777
        # NumPy. The unpickling code in numpy.core.multiarray is
Packit Service a47777
        # written to assume that unicode data appearing where binary
Packit Service a47777
        # should be is in 'latin1'. 'bytes' is also safe, as is 'ASCII'.
Packit Service a47777
        #
Packit Service a47777
        # Other encoding values can corrupt binary data, and we
Packit Service a47777
        # purposefully disallow them. For the same reason, the errors=
Packit Service a47777
        # argument is not exposed, as values other than 'strict'
Packit Service a47777
        # result can similarly silently corrupt numerical data.
Packit Service a47777
        raise ValueError("encoding must be 'ASCII', 'latin1', or 'bytes'")
Packit Service a47777
Packit Service a47777
    if sys.version_info[0] >= 3:
Packit Service a47777
        pickle_kwargs = dict(encoding=encoding, fix_imports=fix_imports)
Packit Service a47777
    else:
Packit Service a47777
        # Nothing to do on Python 2
Packit Service a47777
        pickle_kwargs = {}
Packit Service a47777
Packit Service a47777
    try:
Packit Service a47777
        # Code to distinguish from NumPy binary files and pickles.
Packit Service a47777
        _ZIP_PREFIX = b'PK\x03\x04'
Packit Service a47777
        N = len(format.MAGIC_PREFIX)
Packit Service a47777
        magic = fid.read(N)
Packit Service a47777
        # If the file size is less than N, we need to make sure not
Packit Service a47777
        # to seek past the beginning of the file
Packit Service a47777
        fid.seek(-min(N, len(magic)), 1)  # back-up
Packit Service a47777
        if magic.startswith(_ZIP_PREFIX):
Packit Service a47777
            # zip-file (assume .npz)
Packit Service a47777
            # Transfer file ownership to NpzFile
Packit Service a47777
            tmp = own_fid
Packit Service a47777
            own_fid = False
Packit Service a47777
            return NpzFile(fid, own_fid=tmp, allow_pickle=allow_pickle,
Packit Service a47777
                           pickle_kwargs=pickle_kwargs)
Packit Service a47777
        elif magic == format.MAGIC_PREFIX:
Packit Service a47777
            # .npy file
Packit Service a47777
            if mmap_mode:
Packit Service a47777
                return format.open_memmap(file, mode=mmap_mode)
Packit Service a47777
            else:
Packit Service a47777
                return format.read_array(fid, allow_pickle=allow_pickle,
Packit Service a47777
                                         pickle_kwargs=pickle_kwargs)
Packit Service a47777
        else:
Packit Service a47777
            # Try a pickle
Packit Service a47777
            if not allow_pickle:
Packit Service a47777
                raise ValueError("allow_pickle=False, but file does not contain "
Packit Service a47777
                                 "non-pickled data")
Packit Service a47777
            try:
Packit Service a47777
                return pickle.load(fid, **pickle_kwargs)
Packit Service a47777
            except Exception:
Packit Service a47777
                raise IOError(
Packit Service a47777
                    "Failed to interpret file %s as a pickle" % repr(file))
Packit Service a47777
    finally:
Packit Service a47777
        if own_fid:
Packit Service a47777
            fid.close()
Packit Service a47777
Packit Service a47777
Packit Service a47777
def save(file, arr, allow_pickle=True, fix_imports=True):
Packit Service a47777
    """
Packit Service a47777
    Save an array to a binary file in NumPy ``.npy`` format.
Packit Service a47777
Packit Service a47777
    Parameters
Packit Service a47777
    ----------
Packit Service a47777
    file : file, str, or pathlib.Path
Packit Service a47777
        File or filename to which the data is saved.  If file is a file-object,
Packit Service a47777
        then the filename is unchanged.  If file is a string or Path, a ``.npy``
Packit Service a47777
        extension will be appended to the file name if it does not already
Packit Service a47777
        have one.
Packit Service a47777
    arr : array_like
Packit Service a47777
        Array data to be saved.
Packit Service a47777
    allow_pickle : bool, optional
Packit Service a47777
        Allow saving object arrays using Python pickles. Reasons for disallowing
Packit Service a47777
        pickles include security (loading pickled data can execute arbitrary
Packit Service a47777
        code) and portability (pickled objects may not be loadable on different
Packit Service a47777
        Python installations, for example if the stored objects require libraries
Packit Service a47777
        that are not available, and not all pickled data is compatible between
Packit Service a47777
        Python 2 and Python 3).
Packit Service a47777
        Default: True
Packit Service a47777
    fix_imports : bool, optional
Packit Service a47777
        Only useful in forcing objects in object arrays on Python 3 to be
Packit Service a47777
        pickled in a Python 2 compatible way. If `fix_imports` is True, pickle
Packit Service a47777
        will try to map the new Python 3 names to the old module names used in
Packit Service a47777
        Python 2, so that the pickle data stream is readable with Python 2.
Packit Service a47777
Packit Service a47777
    See Also
Packit Service a47777
    --------
Packit Service a47777
    savez : Save several arrays into a ``.npz`` archive
Packit Service a47777
    savetxt, load
Packit Service a47777
Packit Service a47777
    Notes
Packit Service a47777
    -----
Packit Service a47777
    For a description of the ``.npy`` format, see the module docstring
Packit Service a47777
    of `numpy.lib.format` or the NumPy Enhancement Proposal
Packit Service a47777
    http://docs.scipy.org/doc/numpy/neps/npy-format.html
Packit Service a47777
Packit Service a47777
    Examples
Packit Service a47777
    --------
Packit Service a47777
    >>> from tempfile import TemporaryFile
Packit Service a47777
    >>> outfile = TemporaryFile()
Packit Service a47777
Packit Service a47777
    >>> x = np.arange(10)
Packit Service a47777
    >>> np.save(outfile, x)
Packit Service a47777
Packit Service a47777
    >>> outfile.seek(0) # Only needed here to simulate closing & reopening file
Packit Service a47777
    >>> np.load(outfile)
Packit Service a47777
    array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
Packit Service a47777
Packit Service a47777
    """
Packit Service a47777
    own_fid = False
Packit Service a47777
    if isinstance(file, basestring):
Packit Service a47777
        if not file.endswith('.npy'):
Packit Service a47777
            file = file + '.npy'
Packit Service a47777
        fid = open(file, "wb")
Packit Service a47777
        own_fid = True
Packit Service a47777
    elif is_pathlib_path(file):
Packit Service a47777
        if not file.name.endswith('.npy'):
Packit Service a47777
            file = file.parent / (file.name + '.npy')
Packit Service a47777
        fid = file.open("wb")
Packit Service a47777
        own_fid = True
Packit Service a47777
    else:
Packit Service a47777
        fid = file
Packit Service a47777
Packit Service a47777
    if sys.version_info[0] >= 3:
Packit Service a47777
        pickle_kwargs = dict(fix_imports=fix_imports)
Packit Service a47777
    else:
Packit Service a47777
        # Nothing to do on Python 2
Packit Service a47777
        pickle_kwargs = None
Packit Service a47777
Packit Service a47777
    try:
Packit Service a47777
        arr = np.asanyarray(arr)
Packit Service a47777
        format.write_array(fid, arr, allow_pickle=allow_pickle,
Packit Service a47777
                           pickle_kwargs=pickle_kwargs)
Packit Service a47777
    finally:
Packit Service a47777
        if own_fid:
Packit Service a47777
            fid.close()
Packit Service a47777
Packit Service a47777
Packit Service a47777
def savez(file, *args, **kwds):
Packit Service a47777
    """
Packit Service a47777
    Save several arrays into a single file in uncompressed ``.npz`` format.
Packit Service a47777
Packit Service a47777
    If arguments are passed in with no keywords, the corresponding variable
Packit Service a47777
    names, in the ``.npz`` file, are 'arr_0', 'arr_1', etc. If keyword
Packit Service a47777
    arguments are given, the corresponding variable names, in the ``.npz``
Packit Service a47777
    file will match the keyword names.
Packit Service a47777
Packit Service a47777
    Parameters
Packit Service a47777
    ----------
Packit Service a47777
    file : str or file
Packit Service a47777
        Either the file name (string) or an open file (file-like object)
Packit Service a47777
        where the data will be saved. If file is a string or a Path, the
Packit Service a47777
        ``.npz`` extension will be appended to the file name if it is not
Packit Service a47777
        already there.
Packit Service a47777
    args : Arguments, optional
Packit Service a47777
        Arrays to save to the file. Since it is not possible for Python to
Packit Service a47777
        know the names of the arrays outside `savez`, the arrays will be saved
Packit Service a47777
        with names "arr_0", "arr_1", and so on. These arguments can be any
Packit Service a47777
        expression.
Packit Service a47777
    kwds : Keyword arguments, optional
Packit Service a47777
        Arrays to save to the file. Arrays will be saved in the file with the
Packit Service a47777
        keyword names.
Packit Service a47777
Packit Service a47777
    Returns
Packit Service a47777
    -------
Packit Service a47777
    None
Packit Service a47777
Packit Service a47777
    See Also
Packit Service a47777
    --------
Packit Service a47777
    save : Save a single array to a binary file in NumPy format.
Packit Service a47777
    savetxt : Save an array to a file as plain text.
Packit Service a47777
    savez_compressed : Save several arrays into a compressed ``.npz`` archive
Packit Service a47777
Packit Service a47777
    Notes
Packit Service a47777
    -----
Packit Service a47777
    The ``.npz`` file format is a zipped archive of files named after the
Packit Service a47777
    variables they contain.  The archive is not compressed and each file
Packit Service a47777
    in the archive contains one variable in ``.npy`` format. For a
Packit Service a47777
    description of the ``.npy`` format, see `numpy.lib.format` or the
Packit Service a47777
    NumPy Enhancement Proposal
Packit Service a47777
    http://docs.scipy.org/doc/numpy/neps/npy-format.html
Packit Service a47777
Packit Service a47777
    When opening the saved ``.npz`` file with `load` a `NpzFile` object is
Packit Service a47777
    returned. This is a dictionary-like object which can be queried for
Packit Service a47777
    its list of arrays (with the ``.files`` attribute), and for the arrays
Packit Service a47777
    themselves.
Packit Service a47777
Packit Service a47777
    Examples
Packit Service a47777
    --------
Packit Service a47777
    >>> from tempfile import TemporaryFile
Packit Service a47777
    >>> outfile = TemporaryFile()
Packit Service a47777
    >>> x = np.arange(10)
Packit Service a47777
    >>> y = np.sin(x)
Packit Service a47777
Packit Service a47777
    Using `savez` with \\*args, the arrays are saved with default names.
Packit Service a47777
Packit Service a47777
    >>> np.savez(outfile, x, y)
Packit Service a47777
    >>> outfile.seek(0) # Only needed here to simulate closing & reopening file
Packit Service a47777
    >>> npzfile = np.load(outfile)
Packit Service a47777
    >>> npzfile.files
Packit Service a47777
    ['arr_1', 'arr_0']
Packit Service a47777
    >>> npzfile['arr_0']
Packit Service a47777
    array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
Packit Service a47777
Packit Service a47777
    Using `savez` with \\**kwds, the arrays are saved with the keyword names.
Packit Service a47777
Packit Service a47777
    >>> outfile = TemporaryFile()
Packit Service a47777
    >>> np.savez(outfile, x=x, y=y)
Packit Service a47777
    >>> outfile.seek(0)
Packit Service a47777
    >>> npzfile = np.load(outfile)
Packit Service a47777
    >>> npzfile.files
Packit Service a47777
    ['y', 'x']
Packit Service a47777
    >>> npzfile['x']
Packit Service a47777
    array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
Packit Service a47777
Packit Service a47777
    """
Packit Service a47777
    _savez(file, args, kwds, False)
Packit Service a47777
Packit Service a47777
Packit Service a47777
def savez_compressed(file, *args, **kwds):
Packit Service a47777
    """
Packit Service a47777
    Save several arrays into a single file in compressed ``.npz`` format.
Packit Service a47777
Packit Service a47777
    If keyword arguments are given, then filenames are taken from the keywords.
Packit Service a47777
    If arguments are passed in with no keywords, then stored file names are
Packit Service a47777
    arr_0, arr_1, etc.
Packit Service a47777
Packit Service a47777
    Parameters
Packit Service a47777
    ----------
Packit Service a47777
    file : str or file
Packit Service a47777
        Either the file name (string) or an open file (file-like object)
Packit Service a47777
        where the data will be saved. If file is a string or a Path, the
Packit Service a47777
        ``.npz`` extension will be appended to the file name if it is not
Packit Service a47777
        already there.
Packit Service a47777
    args : Arguments, optional
Packit Service a47777
        Arrays to save to the file. Since it is not possible for Python to
Packit Service a47777
        know the names of the arrays outside `savez`, the arrays will be saved
Packit Service a47777
        with names "arr_0", "arr_1", and so on. These arguments can be any
Packit Service a47777
        expression.
Packit Service a47777
    kwds : Keyword arguments, optional
Packit Service a47777
        Arrays to save to the file. Arrays will be saved in the file with the
Packit Service a47777
        keyword names.
Packit Service a47777
Packit Service a47777
    Returns
Packit Service a47777
    -------
Packit Service a47777
    None
Packit Service a47777
Packit Service a47777
    See Also
Packit Service a47777
    --------
Packit Service a47777
    numpy.save : Save a single array to a binary file in NumPy format.
Packit Service a47777
    numpy.savetxt : Save an array to a file as plain text.
Packit Service a47777
    numpy.savez : Save several arrays into an uncompressed ``.npz`` file format
Packit Service a47777
    numpy.load : Load the files created by savez_compressed.
Packit Service a47777
Packit Service a47777
    Notes
Packit Service a47777
    -----
Packit Service a47777
    The ``.npz`` file format is a zipped archive of files named after the
Packit Service a47777
    variables they contain.  The archive is compressed with
Packit Service a47777
    ``zipfile.ZIP_DEFLATED`` and each file in the archive contains one variable
Packit Service a47777
    in ``.npy`` format. For a description of the ``.npy`` format, see
Packit Service a47777
    `numpy.lib.format` or the NumPy Enhancement Proposal
Packit Service a47777
    http://docs.scipy.org/doc/numpy/neps/npy-format.html
Packit Service a47777
Packit Service a47777
    When opening the saved ``.npz`` file with `load` a `NpzFile` object is
Packit Service a47777
    returned. This is a dictionary-like object which can be queried for
Packit Service a47777
    its list of arrays (with the ``.files`` attribute), and for the arrays
Packit Service a47777
    themselves.
Packit Service a47777
Packit Service a47777
    Examples
Packit Service a47777
    --------
Packit Service a47777
    >>> test_array = np.random.rand(3, 2)
Packit Service a47777
    >>> test_vector = np.random.rand(4)
Packit Service a47777
    >>> np.savez_compressed('/tmp/123', a=test_array, b=test_vector)
Packit Service a47777
    >>> loaded = np.load('/tmp/123.npz')
Packit Service a47777
    >>> print(np.array_equal(test_array, loaded['a']))
Packit Service a47777
    True
Packit Service a47777
    >>> print(np.array_equal(test_vector, loaded['b']))
Packit Service a47777
    True
Packit Service a47777
Packit Service a47777
    """
Packit Service a47777
    _savez(file, args, kwds, True)
Packit Service a47777
Packit Service a47777
Packit Service a47777
def _savez(file, args, kwds, compress, allow_pickle=True, pickle_kwargs=None):
Packit Service a47777
    # Import is postponed to here since zipfile depends on gzip, an optional
Packit Service a47777
    # component of the so-called standard library.
Packit Service a47777
    import zipfile
Packit Service a47777
Packit Service a47777
    if isinstance(file, basestring):
Packit Service a47777
        if not file.endswith('.npz'):
Packit Service a47777
            file = file + '.npz'
Packit Service a47777
    elif is_pathlib_path(file):
Packit Service a47777
        if not file.name.endswith('.npz'):
Packit Service a47777
            file = file.parent / (file.name + '.npz')
Packit Service a47777
Packit Service a47777
    namedict = kwds
Packit Service a47777
    for i, val in enumerate(args):
Packit Service a47777
        key = 'arr_%d' % i
Packit Service a47777
        if key in namedict.keys():
Packit Service a47777
            raise ValueError(
Packit Service a47777
                "Cannot use un-named variables and keyword %s" % key)
Packit Service a47777
        namedict[key] = val
Packit Service a47777
Packit Service a47777
    if compress:
Packit Service a47777
        compression = zipfile.ZIP_DEFLATED
Packit Service a47777
    else:
Packit Service a47777
        compression = zipfile.ZIP_STORED
Packit Service a47777
Packit Service a47777
    zipf = zipfile_factory(file, mode="w", compression=compression)
Packit Service a47777
Packit Service a47777
    if sys.version_info >= (3, 6):
Packit Service a47777
        # Since Python 3.6 it is possible to write directly to a ZIP file.
Packit Service a47777
        for key, val in namedict.items():
Packit Service a47777
            fname = key + '.npy'
Packit Service a47777
            val = np.asanyarray(val)
Packit Service a47777
            force_zip64 = val.nbytes >= 2**30
Packit Service a47777
            with zipf.open(fname, 'w', force_zip64=force_zip64) as fid:
Packit Service a47777
                format.write_array(fid, val,
Packit Service a47777
                                   allow_pickle=allow_pickle,
Packit Service a47777
                                   pickle_kwargs=pickle_kwargs)
Packit Service a47777
    else:
Packit Service a47777
        # Stage arrays in a temporary file on disk, before writing to zip.
Packit Service a47777
Packit Service a47777
        # Import deferred for startup time improvement
Packit Service a47777
        import tempfile
Packit Service a47777
        # Since target file might be big enough to exceed capacity of a global
Packit Service a47777
        # temporary directory, create temp file side-by-side with the target file.
Packit Service a47777
        file_dir, file_prefix = os.path.split(file) if _is_string_like(file) else (None, 'tmp')
Packit Service a47777
        fd, tmpfile = tempfile.mkstemp(prefix=file_prefix, dir=file_dir, suffix='-numpy.npy')
Packit Service a47777
        os.close(fd)
Packit Service a47777
        try:
Packit Service a47777
            for key, val in namedict.items():
Packit Service a47777
                fname = key + '.npy'
Packit Service a47777
                fid = open(tmpfile, 'wb')
Packit Service a47777
                try:
Packit Service a47777
                    format.write_array(fid, np.asanyarray(val),
Packit Service a47777
                                       allow_pickle=allow_pickle,
Packit Service a47777
                                       pickle_kwargs=pickle_kwargs)
Packit Service a47777
                    fid.close()
Packit Service a47777
                    fid = None
Packit Service a47777
                    zipf.write(tmpfile, arcname=fname)
Packit Service a47777
                except IOError as exc:
Packit Service a47777
                    raise IOError("Failed to write to %s: %s" % (tmpfile, exc))
Packit Service a47777
                finally:
Packit Service a47777
                    if fid:
Packit Service a47777
                        fid.close()
Packit Service a47777
        finally:
Packit Service a47777
            os.remove(tmpfile)
Packit Service a47777
Packit Service a47777
    zipf.close()
Packit Service a47777
Packit Service a47777
Packit Service a47777
def _getconv(dtype):
Packit Service a47777
    """ Find the correct dtype converter. Adapted from matplotlib """
Packit Service a47777
Packit Service a47777
    def floatconv(x):
Packit Service a47777
        x.lower()
Packit Service a47777
        if '0x' in x:
Packit Service a47777
            return float.fromhex(x)
Packit Service a47777
        return float(x)
Packit Service a47777
Packit Service a47777
    typ = dtype.type
Packit Service a47777
    if issubclass(typ, np.bool_):
Packit Service a47777
        return lambda x: bool(int(x))
Packit Service a47777
    if issubclass(typ, np.uint64):
Packit Service a47777
        return np.uint64
Packit Service a47777
    if issubclass(typ, np.int64):
Packit Service a47777
        return np.int64
Packit Service a47777
    if issubclass(typ, np.integer):
Packit Service a47777
        return lambda x: int(float(x))
Packit Service a47777
    elif issubclass(typ, np.longdouble):
Packit Service a47777
        return np.longdouble
Packit Service a47777
    elif issubclass(typ, np.floating):
Packit Service a47777
        return floatconv
Packit Service a47777
    elif issubclass(typ, complex):
Packit Service a47777
        return lambda x: complex(asstr(x))
Packit Service a47777
    elif issubclass(typ, np.bytes_):
Packit Service a47777
        return asbytes
Packit Service a47777
    elif issubclass(typ, np.unicode_):
Packit Service a47777
        return asunicode
Packit Service a47777
    else:
Packit Service a47777
        return asstr
Packit Service a47777
Packit Service a47777
# amount of lines loadtxt reads in one chunk, can be overriden for testing
Packit Service a47777
_loadtxt_chunksize = 50000
Packit Service a47777
Packit Service a47777
def loadtxt(fname, dtype=float, comments='#', delimiter=None,
Packit Service a47777
            converters=None, skiprows=0, usecols=None, unpack=False,
Packit Service a47777
            ndmin=0, encoding='bytes'):
Packit Service a47777
    """
Packit Service a47777
    Load data from a text file.
Packit Service a47777
Packit Service a47777
    Each row in the text file must have the same number of values.
Packit Service a47777
Packit Service a47777
    Parameters
Packit Service a47777
    ----------
Packit Service a47777
    fname : file, str, or pathlib.Path
Packit Service a47777
        File, filename, or generator to read.  If the filename extension is
Packit Service a47777
        ``.gz`` or ``.bz2``, the file is first decompressed. Note that
Packit Service a47777
        generators should return byte strings for Python 3k.
Packit Service a47777
    dtype : data-type, optional
Packit Service a47777
        Data-type of the resulting array; default: float.  If this is a
Packit Service a47777
        structured data-type, the resulting array will be 1-dimensional, and
Packit Service a47777
        each row will be interpreted as an element of the array.  In this
Packit Service a47777
        case, the number of columns used must match the number of fields in
Packit Service a47777
        the data-type.
Packit Service a47777
    comments : str or sequence of str, optional
Packit Service a47777
        The characters or list of characters used to indicate the start of a
Packit Service a47777
        comment. For backwards compatibility, byte strings will be decoded as
Packit Service a47777
        'latin1'. The default is '#'.
Packit Service a47777
    delimiter : str, optional
Packit Service a47777
        The string used to separate values. For backwards compatibility, byte
Packit Service a47777
        strings will be decoded as 'latin1'. The default is whitespace.
Packit Service a47777
    converters : dict, optional
Packit Service a47777
        A dictionary mapping column number to a function that will convert
Packit Service a47777
        that column to a float.  E.g., if column 0 is a date string:
Packit Service a47777
        ``converters = {0: datestr2num}``.  Converters can also be used to
Packit Service a47777
        provide a default value for missing data (but see also `genfromtxt`):
Packit Service a47777
        ``converters = {3: lambda s: float(s.strip() or 0)}``.  Default: None.
Packit Service a47777
    skiprows : int, optional
Packit Service a47777
        Skip the first `skiprows` lines; default: 0.
Packit Service a47777
    usecols : int or sequence, optional
Packit Service a47777
        Which columns to read, with 0 being the first. For example,
Packit Service a47777
        usecols = (1,4,5) will extract the 2nd, 5th and 6th columns.
Packit Service a47777
        The default, None, results in all columns being read.
Packit Service a47777
Packit Service a47777
        .. versionchanged:: 1.11.0
Packit Service a47777
            When a single column has to be read it is possible to use
Packit Service a47777
            an integer instead of a tuple. E.g ``usecols = 3`` reads the
Packit Service a47777
            fourth column the same way as `usecols = (3,)`` would.
Packit Service a47777
    unpack : bool, optional
Packit Service a47777
        If True, the returned array is transposed, so that arguments may be
Packit Service a47777
        unpacked using ``x, y, z = loadtxt(...)``.  When used with a structured
Packit Service a47777
        data-type, arrays are returned for each field.  Default is False.
Packit Service a47777
    ndmin : int, optional
Packit Service a47777
        The returned array will have at least `ndmin` dimensions.
Packit Service a47777
        Otherwise mono-dimensional axes will be squeezed.
Packit Service a47777
        Legal values: 0 (default), 1 or 2.
Packit Service a47777
Packit Service a47777
        .. versionadded:: 1.6.0
Packit Service a47777
    encoding : str, optional
Packit Service a47777
        Encoding used to decode the inputfile. Does not apply to input streams.
Packit Service a47777
        The special value 'bytes' enables backward compatibility workarounds
Packit Service a47777
        that ensures you receive byte arrays as results if possible and passes
Packit Service a47777
        latin1 encoded strings to converters. Override this value to receive
Packit Service a47777
        unicode arrays and pass strings as input to converters.  If set to None
Packit Service a47777
        the system default is used. The default value is 'bytes'.
Packit Service a47777
Packit Service a47777
        .. versionadded:: 1.14.0
Packit Service a47777
Packit Service a47777
    Returns
Packit Service a47777
    -------
Packit Service a47777
    out : ndarray
Packit Service a47777
        Data read from the text file.
Packit Service a47777
Packit Service a47777
    See Also
Packit Service a47777
    --------
Packit Service a47777
    load, fromstring, fromregex
Packit Service a47777
    genfromtxt : Load data with missing values handled as specified.
Packit Service a47777
    scipy.io.loadmat : reads MATLAB data files
Packit Service a47777
Packit Service a47777
    Notes
Packit Service a47777
    -----
Packit Service a47777
    This function aims to be a fast reader for simply formatted files.  The
Packit Service a47777
    `genfromtxt` function provides more sophisticated handling of, e.g.,
Packit Service a47777
    lines with missing values.
Packit Service a47777
Packit Service a47777
    .. versionadded:: 1.10.0
Packit Service a47777
Packit Service a47777
    The strings produced by the Python float.hex method can be used as
Packit Service a47777
    input for floats.
Packit Service a47777
Packit Service a47777
    Examples
Packit Service a47777
    --------
Packit Service a47777
    >>> from io import StringIO   # StringIO behaves like a file object
Packit Service a47777
    >>> c = StringIO("0 1\\n2 3")
Packit Service a47777
    >>> np.loadtxt(c)
Packit Service a47777
    array([[ 0.,  1.],
Packit Service a47777
           [ 2.,  3.]])
Packit Service a47777
Packit Service a47777
    >>> d = StringIO("M 21 72\\nF 35 58")
Packit Service a47777
    >>> np.loadtxt(d, dtype={'names': ('gender', 'age', 'weight'),
Packit Service a47777
    ...                      'formats': ('S1', 'i4', 'f4')})
Packit Service a47777
    array([('M', 21, 72.0), ('F', 35, 58.0)],
Packit Service a47777
          dtype=[('gender', '|S1'), ('age', '
Packit Service a47777
Packit Service a47777
    >>> c = StringIO("1,0,2\\n3,0,4")
Packit Service a47777
    >>> x, y = np.loadtxt(c, delimiter=',', usecols=(0, 2), unpack=True)
Packit Service a47777
    >>> x
Packit Service a47777
    array([ 1.,  3.])
Packit Service a47777
    >>> y
Packit Service a47777
    array([ 2.,  4.])
Packit Service a47777
Packit Service a47777
    """
Packit Service a47777
    # Type conversions for Py3 convenience
Packit Service a47777
    if comments is not None:
Packit Service a47777
        if isinstance(comments, (basestring, bytes)):
Packit Service a47777
            comments = [comments]
Packit Service a47777
        comments = [_decode_line(x) for x in comments]
Packit Service a47777
        # Compile regex for comments beforehand
Packit Service a47777
        comments = (re.escape(comment) for comment in comments)
Packit Service a47777
        regex_comments = re.compile('|'.join(comments))
Packit Service a47777
Packit Service a47777
    if delimiter is not None:
Packit Service a47777
        delimiter = _decode_line(delimiter)
Packit Service a47777
Packit Service a47777
    user_converters = converters
Packit Service a47777
Packit Service a47777
    if encoding == 'bytes':
Packit Service a47777
        encoding = None
Packit Service a47777
        byte_converters = True
Packit Service a47777
    else:
Packit Service a47777
        byte_converters = False
Packit Service a47777
Packit Service a47777
    if usecols is not None:
Packit Service a47777
        # Allow usecols to be a single int or a sequence of ints
Packit Service a47777
        try:
Packit Service a47777
            usecols_as_list = list(usecols)
Packit Service a47777
        except TypeError:
Packit Service a47777
            usecols_as_list = [usecols]
Packit Service a47777
        for col_idx in usecols_as_list:
Packit Service a47777
            try:
Packit Service a47777
                opindex(col_idx)
Packit Service a47777
            except TypeError as e:
Packit Service a47777
                e.args = (
Packit Service a47777
                    "usecols must be an int or a sequence of ints but "
Packit Service a47777
                    "it contains at least one element of type %s" %
Packit Service a47777
                    type(col_idx),
Packit Service a47777
                    )
Packit Service a47777
                raise
Packit Service a47777
        # Fall back to existing code
Packit Service a47777
        usecols = usecols_as_list
Packit Service a47777
Packit Service a47777
    fown = False
Packit Service a47777
    try:
Packit Service a47777
        if is_pathlib_path(fname):
Packit Service a47777
            fname = str(fname)
Packit Service a47777
        if _is_string_like(fname):
Packit Service a47777
            fh = np.lib._datasource.open(fname, 'rt', encoding=encoding)
Packit Service a47777
            fencoding = getattr(fh, 'encoding', 'latin1')
Packit Service a47777
            fh = iter(fh)
Packit Service a47777
            fown = True
Packit Service a47777
        else:
Packit Service a47777
            fh = iter(fname)
Packit Service a47777
            fencoding = getattr(fname, 'encoding', 'latin1')
Packit Service a47777
    except TypeError:
Packit Service a47777
        raise ValueError('fname must be a string, file handle, or generator')
Packit Service a47777
Packit Service a47777
    # input may be a python2 io stream
Packit Service a47777
    if encoding is not None:
Packit Service a47777
        fencoding = encoding
Packit Service a47777
    # we must assume local encoding
Packit Service a47777
    # TOOD emit portability warning?
Packit Service a47777
    elif fencoding is None:
Packit Service a47777
        import locale
Packit Service a47777
        fencoding = locale.getpreferredencoding()
Packit Service a47777
Packit Service a47777
    # not to be confused with the flatten_dtype we import...
Packit Service a47777
    def flatten_dtype_internal(dt):
Packit Service a47777
        """Unpack a structured data-type, and produce re-packing info."""
Packit Service a47777
        if dt.names is None:
Packit Service a47777
            # If the dtype is flattened, return.
Packit Service a47777
            # If the dtype has a shape, the dtype occurs
Packit Service a47777
            # in the list more than once.
Packit Service a47777
            shape = dt.shape
Packit Service a47777
            if len(shape) == 0:
Packit Service a47777
                return ([dt.base], None)
Packit Service a47777
            else:
Packit Service a47777
                packing = [(shape[-1], list)]
Packit Service a47777
                if len(shape) > 1:
Packit Service a47777
                    for dim in dt.shape[-2::-1]:
Packit Service a47777
                        packing = [(dim*packing[0][0], packing*dim)]
Packit Service a47777
                return ([dt.base] * int(np.prod(dt.shape)), packing)
Packit Service a47777
        else:
Packit Service a47777
            types = []
Packit Service a47777
            packing = []
Packit Service a47777
            for field in dt.names:
Packit Service a47777
                tp, bytes = dt.fields[field]
Packit Service a47777
                flat_dt, flat_packing = flatten_dtype_internal(tp)
Packit Service a47777
                types.extend(flat_dt)
Packit Service a47777
                # Avoid extra nesting for subarrays
Packit Service a47777
                if tp.ndim > 0:
Packit Service a47777
                    packing.extend(flat_packing)
Packit Service a47777
                else:
Packit Service a47777
                    packing.append((len(flat_dt), flat_packing))
Packit Service a47777
            return (types, packing)
Packit Service a47777
Packit Service a47777
    def pack_items(items, packing):
Packit Service a47777
        """Pack items into nested lists based on re-packing info."""
Packit Service a47777
        if packing is None:
Packit Service a47777
            return items[0]
Packit Service a47777
        elif packing is tuple:
Packit Service a47777
            return tuple(items)
Packit Service a47777
        elif packing is list:
Packit Service a47777
            return list(items)
Packit Service a47777
        else:
Packit Service a47777
            start = 0
Packit Service a47777
            ret = []
Packit Service a47777
            for length, subpacking in packing:
Packit Service a47777
                ret.append(pack_items(items[start:start+length], subpacking))
Packit Service a47777
                start += length
Packit Service a47777
            return tuple(ret)
Packit Service a47777
Packit Service a47777
    def split_line(line):
Packit Service a47777
        """Chop off comments, strip, and split at delimiter. """
Packit Service a47777
        line = _decode_line(line, encoding=encoding)
Packit Service a47777
Packit Service a47777
        if comments is not None:
Packit Service a47777
            line = regex_comments.split(line, maxsplit=1)[0]
Packit Service a47777
        line = line.strip('\r\n')
Packit Service a47777
        if line:
Packit Service a47777
            return line.split(delimiter)
Packit Service a47777
        else:
Packit Service a47777
            return []
Packit Service a47777
Packit Service a47777
    def read_data(chunk_size):
Packit Service a47777
        """Parse each line, including the first.
Packit Service a47777
Packit Service a47777
        The file read, `fh`, is a global defined above.
Packit Service a47777
Packit Service a47777
        Parameters
Packit Service a47777
        ----------
Packit Service a47777
        chunk_size : int
Packit Service a47777
            At most `chunk_size` lines are read at a time, with iteration
Packit Service a47777
            until all lines are read.
Packit Service a47777
Packit Service a47777
        """
Packit Service a47777
        X = []
Packit Service a47777
        for i, line in enumerate(itertools.chain([first_line], fh)):
Packit Service a47777
            vals = split_line(line)
Packit Service a47777
            if len(vals) == 0:
Packit Service a47777
                continue
Packit Service a47777
            if usecols:
Packit Service a47777
                vals = [vals[j] for j in usecols]
Packit Service a47777
            if len(vals) != N:
Packit Service a47777
                line_num = i + skiprows + 1
Packit Service a47777
                raise ValueError("Wrong number of columns at line %d"
Packit Service a47777
                                 % line_num)
Packit Service a47777
Packit Service a47777
            # Convert each value according to its column and store
Packit Service a47777
            items = [conv(val) for (conv, val) in zip(converters, vals)]
Packit Service a47777
Packit Service a47777
            # Then pack it according to the dtype's nesting
Packit Service a47777
            items = pack_items(items, packing)
Packit Service a47777
            X.append(items)
Packit Service a47777
            if len(X) > chunk_size:
Packit Service a47777
                yield X
Packit Service a47777
                X = []
Packit Service a47777
        if X:
Packit Service a47777
            yield X
Packit Service a47777
Packit Service a47777
    try:
Packit Service a47777
        # Make sure we're dealing with a proper dtype
Packit Service a47777
        dtype = np.dtype(dtype)
Packit Service a47777
        defconv = _getconv(dtype)
Packit Service a47777
Packit Service a47777
        # Skip the first `skiprows` lines
Packit Service a47777
        for i in range(skiprows):
Packit Service a47777
            next(fh)
Packit Service a47777
Packit Service a47777
        # Read until we find a line with some values, and use
Packit Service a47777
        # it to estimate the number of columns, N.
Packit Service a47777
        first_vals = None
Packit Service a47777
        try:
Packit Service a47777
            while not first_vals:
Packit Service a47777
                first_line = next(fh)
Packit Service a47777
                first_vals = split_line(first_line)
Packit Service a47777
        except StopIteration:
Packit Service a47777
            # End of lines reached
Packit Service a47777
            first_line = ''
Packit Service a47777
            first_vals = []
Packit Service a47777
            warnings.warn('loadtxt: Empty input file: "%s"' % fname, stacklevel=2)
Packit Service a47777
        N = len(usecols or first_vals)
Packit Service a47777
Packit Service a47777
        dtype_types, packing = flatten_dtype_internal(dtype)
Packit Service a47777
        if len(dtype_types) > 1:
Packit Service a47777
            # We're dealing with a structured array, each field of
Packit Service a47777
            # the dtype matches a column
Packit Service a47777
            converters = [_getconv(dt) for dt in dtype_types]
Packit Service a47777
        else:
Packit Service a47777
            # All fields have the same dtype
Packit Service a47777
            converters = [defconv for i in range(N)]
Packit Service a47777
            if N > 1:
Packit Service a47777
                packing = [(N, tuple)]
Packit Service a47777
Packit Service a47777
        # By preference, use the converters specified by the user
Packit Service a47777
        for i, conv in (user_converters or {}).items():
Packit Service a47777
            if usecols:
Packit Service a47777
                try:
Packit Service a47777
                    i = usecols.index(i)
Packit Service a47777
                except ValueError:
Packit Service a47777
                    # Unused converter specified
Packit Service a47777
                    continue
Packit Service a47777
            if byte_converters:
Packit Service a47777
                # converters may use decode to workaround numpy's old behaviour,
Packit Service a47777
                # so encode the string again before passing to the user converter
Packit Service a47777
                def tobytes_first(x, conv):
Packit Service a47777
                    if type(x) is bytes:
Packit Service a47777
                        return conv(x)
Packit Service a47777
                    return conv(x.encode("latin1"))
Packit Service a47777
                import functools
Packit Service a47777
                converters[i] = functools.partial(tobytes_first, conv=conv)
Packit Service a47777
            else:
Packit Service a47777
                converters[i] = conv
Packit Service a47777
Packit Service a47777
        converters = [conv if conv is not bytes else
Packit Service a47777
                      lambda x: x.encode(fencoding) for conv in converters]
Packit Service a47777
Packit Service a47777
        # read data in chunks and fill it into an array via resize
Packit Service a47777
        # over-allocating and shrinking the array later may be faster but is
Packit Service a47777
        # probably not relevant compared to the cost of actually reading and
Packit Service a47777
        # converting the data
Packit Service a47777
        X = None
Packit Service a47777
        for x in read_data(_loadtxt_chunksize):
Packit Service a47777
            if X is None:
Packit Service a47777
                X = np.array(x, dtype)
Packit Service a47777
            else:
Packit Service a47777
                nshape = list(X.shape)
Packit Service a47777
                pos = nshape[0]
Packit Service a47777
                nshape[0] += len(x)
Packit Service a47777
                X.resize(nshape)
Packit Service a47777
                X[pos:, ...] = x
Packit Service a47777
    finally:
Packit Service a47777
        if fown:
Packit Service a47777
            fh.close()
Packit Service a47777
        # recursive closures have a cyclic reference to themselves, which
Packit Service a47777
        # requires gc to collect (gh-10620). To avoid this problem, for
Packit Service a47777
        # performance and PyPy friendliness, we break the cycle:
Packit Service a47777
        flatten_dtype_internal = None
Packit Service a47777
        pack_items = None
Packit Service a47777
Packit Service a47777
    if X is None:
Packit Service a47777
        X = np.array([], dtype)
Packit Service a47777
Packit Service a47777
    # Multicolumn data are returned with shape (1, N, M), i.e.
Packit Service a47777
    # (1, 1, M) for a single row - remove the singleton dimension there
Packit Service a47777
    if X.ndim == 3 and X.shape[:2] == (1, 1):
Packit Service a47777
        X.shape = (1, -1)
Packit Service a47777
Packit Service a47777
    # Verify that the array has at least dimensions `ndmin`.
Packit Service a47777
    # Check correctness of the values of `ndmin`
Packit Service a47777
    if ndmin not in [0, 1, 2]:
Packit Service a47777
        raise ValueError('Illegal value of ndmin keyword: %s' % ndmin)
Packit Service a47777
    # Tweak the size and shape of the arrays - remove extraneous dimensions
Packit Service a47777
    if X.ndim > ndmin:
Packit Service a47777
        X = np.squeeze(X)
Packit Service a47777
    # and ensure we have the minimum number of dimensions asked for
Packit Service a47777
    # - has to be in this order for the odd case ndmin=1, X.squeeze().ndim=0
Packit Service a47777
    if X.ndim < ndmin:
Packit Service a47777
        if ndmin == 1:
Packit Service a47777
            X = np.atleast_1d(X)
Packit Service a47777
        elif ndmin == 2:
Packit Service a47777
            X = np.atleast_2d(X).T
Packit Service a47777
Packit Service a47777
    if unpack:
Packit Service a47777
        if len(dtype_types) > 1:
Packit Service a47777
            # For structured arrays, return an array for each field.
Packit Service a47777
            return [X[field] for field in dtype.names]
Packit Service a47777
        else:
Packit Service a47777
            return X.T
Packit Service a47777
    else:
Packit Service a47777
        return X
Packit Service a47777
Packit Service a47777
Packit Service a47777
def savetxt(fname, X, fmt='%.18e', delimiter=' ', newline='\n', header='',
Packit Service a47777
            footer='', comments='# ', encoding=None):
Packit Service a47777
    """
Packit Service a47777
    Save an array to a text file.
Packit Service a47777
Packit Service a47777
    Parameters
Packit Service a47777
    ----------
Packit Service a47777
    fname : filename or file handle
Packit Service a47777
        If the filename ends in ``.gz``, the file is automatically saved in
Packit Service a47777
        compressed gzip format.  `loadtxt` understands gzipped files
Packit Service a47777
        transparently.
Packit Service a47777
    X : 1D or 2D array_like
Packit Service a47777
        Data to be saved to a text file.
Packit Service a47777
    fmt : str or sequence of strs, optional
Packit Service a47777
        A single format (%10.5f), a sequence of formats, or a
Packit Service a47777
        multi-format string, e.g. 'Iteration %d -- %10.5f', in which
Packit Service a47777
        case `delimiter` is ignored. For complex `X`, the legal options
Packit Service a47777
        for `fmt` are:
Packit Service a47777
            a) a single specifier, `fmt='%.4e'`, resulting in numbers formatted
Packit Service a47777
               like `' (%s+%sj)' % (fmt, fmt)`
Packit Service a47777
            b) a full string specifying every real and imaginary part, e.g.
Packit Service a47777
               `' %.4e %+.4ej %.4e %+.4ej %.4e %+.4ej'` for 3 columns
Packit Service a47777
            c) a list of specifiers, one per column - in this case, the real
Packit Service a47777
               and imaginary part must have separate specifiers,
Packit Service a47777
               e.g. `['%.3e + %.3ej', '(%.15e%+.15ej)']` for 2 columns
Packit Service a47777
    delimiter : str, optional
Packit Service a47777
        String or character separating columns.
Packit Service a47777
    newline : str, optional
Packit Service a47777
        String or character separating lines.
Packit Service a47777
Packit Service a47777
        .. versionadded:: 1.5.0
Packit Service a47777
    header : str, optional
Packit Service a47777
        String that will be written at the beginning of the file.
Packit Service a47777
Packit Service a47777
        .. versionadded:: 1.7.0
Packit Service a47777
    footer : str, optional
Packit Service a47777
        String that will be written at the end of the file.
Packit Service a47777
Packit Service a47777
        .. versionadded:: 1.7.0
Packit Service a47777
    comments : str, optional
Packit Service a47777
        String that will be prepended to the ``header`` and ``footer`` strings,
Packit Service a47777
        to mark them as comments. Default: '# ',  as expected by e.g.
Packit Service a47777
        ``numpy.loadtxt``.
Packit Service a47777
Packit Service a47777
        .. versionadded:: 1.7.0
Packit Service a47777
    encoding : {None, str}, optional
Packit Service a47777
        Encoding used to encode the outputfile. Does not apply to output
Packit Service a47777
        streams. If the encoding is something other than 'bytes' or 'latin1'
Packit Service a47777
        you will not be able to load the file in NumPy versions < 1.14. Default
Packit Service a47777
        is 'latin1'.
Packit Service a47777
Packit Service a47777
        .. versionadded:: 1.14.0
Packit Service a47777
Packit Service a47777
Packit Service a47777
    See Also
Packit Service a47777
    --------
Packit Service a47777
    save : Save an array to a binary file in NumPy ``.npy`` format
Packit Service a47777
    savez : Save several arrays into an uncompressed ``.npz`` archive
Packit Service a47777
    savez_compressed : Save several arrays into a compressed ``.npz`` archive
Packit Service a47777
Packit Service a47777
    Notes
Packit Service a47777
    -----
Packit Service a47777
    Further explanation of the `fmt` parameter
Packit Service a47777
    (``%[flag]width[.precision]specifier``):
Packit Service a47777
Packit Service a47777
    flags:
Packit Service a47777
        ``-`` : left justify
Packit Service a47777
Packit Service a47777
        ``+`` : Forces to precede result with + or -.
Packit Service a47777
Packit Service a47777
        ``0`` : Left pad the number with zeros instead of space (see width).
Packit Service a47777
Packit Service a47777
    width:
Packit Service a47777
        Minimum number of characters to be printed. The value is not truncated
Packit Service a47777
        if it has more characters.
Packit Service a47777
Packit Service a47777
    precision:
Packit Service a47777
        - For integer specifiers (eg. ``d,i,o,x``), the minimum number of
Packit Service a47777
          digits.
Packit Service a47777
        - For ``e, E`` and ``f`` specifiers, the number of digits to print
Packit Service a47777
          after the decimal point.
Packit Service a47777
        - For ``g`` and ``G``, the maximum number of significant digits.
Packit Service a47777
        - For ``s``, the maximum number of characters.
Packit Service a47777
Packit Service a47777
    specifiers:
Packit Service a47777
        ``c`` : character
Packit Service a47777
Packit Service a47777
        ``d`` or ``i`` : signed decimal integer
Packit Service a47777
Packit Service a47777
        ``e`` or ``E`` : scientific notation with ``e`` or ``E``.
Packit Service a47777
Packit Service a47777
        ``f`` : decimal floating point
Packit Service a47777
Packit Service a47777
        ``g,G`` : use the shorter of ``e,E`` or ``f``
Packit Service a47777
Packit Service a47777
        ``o`` : signed octal
Packit Service a47777
Packit Service a47777
        ``s`` : string of characters
Packit Service a47777
Packit Service a47777
        ``u`` : unsigned decimal integer
Packit Service a47777
Packit Service a47777
        ``x,X`` : unsigned hexadecimal integer
Packit Service a47777
Packit Service a47777
    This explanation of ``fmt`` is not complete, for an exhaustive
Packit Service a47777
    specification see [1]_.
Packit Service a47777
Packit Service a47777
    References
Packit Service a47777
    ----------
Packit Service a47777
    .. [1] `Format Specification Mini-Language
Packit Service a47777
           
Packit Service a47777
           format-specification-mini-language>`_, Python Documentation.
Packit Service a47777
Packit Service a47777
    Examples
Packit Service a47777
    --------
Packit Service a47777
    >>> x = y = z = np.arange(0.0,5.0,1.0)
Packit Service a47777
    >>> np.savetxt('test.out', x, delimiter=',')   # X is an array
Packit Service a47777
    >>> np.savetxt('test.out', (x,y,z))   # x,y,z equal sized 1D arrays
Packit Service a47777
    >>> np.savetxt('test.out', x, fmt='%1.4e')   # use exponential notation
Packit Service a47777
Packit Service a47777
    """
Packit Service a47777
Packit Service a47777
    # Py3 conversions first
Packit Service a47777
    if isinstance(fmt, bytes):
Packit Service a47777
        fmt = asstr(fmt)
Packit Service a47777
    delimiter = asstr(delimiter)
Packit Service a47777
Packit Service a47777
    class WriteWrap(object):
Packit Service a47777
        """Convert to unicode in py2 or to bytes on bytestream inputs.
Packit Service a47777
Packit Service a47777
        """
Packit Service a47777
        def __init__(self, fh, encoding):
Packit Service a47777
            self.fh = fh
Packit Service a47777
            self.encoding = encoding
Packit Service a47777
            self.do_write = self.first_write
Packit Service a47777
Packit Service a47777
        def close(self):
Packit Service a47777
            self.fh.close()
Packit Service a47777
Packit Service a47777
        def write(self, v):
Packit Service a47777
            self.do_write(v)
Packit Service a47777
Packit Service a47777
        def write_bytes(self, v):
Packit Service a47777
            if isinstance(v, bytes):
Packit Service a47777
                self.fh.write(v)
Packit Service a47777
            else:
Packit Service a47777
                self.fh.write(v.encode(self.encoding))
Packit Service a47777
Packit Service a47777
        def write_normal(self, v):
Packit Service a47777
            self.fh.write(asunicode(v))
Packit Service a47777
Packit Service a47777
        def first_write(self, v):
Packit Service a47777
            try:
Packit Service a47777
                self.write_normal(v)
Packit Service a47777
                self.write = self.write_normal
Packit Service a47777
            except TypeError:
Packit Service a47777
                # input is probably a bytestream
Packit Service a47777
                self.write_bytes(v)
Packit Service a47777
                self.write = self.write_bytes
Packit Service a47777
Packit Service a47777
    own_fh = False
Packit Service a47777
    if is_pathlib_path(fname):
Packit Service a47777
        fname = str(fname)
Packit Service a47777
    if _is_string_like(fname):
Packit Service a47777
        # datasource doesn't support creating a new file ...
Packit Service a47777
        open(fname, 'wt').close()
Packit Service a47777
        fh = np.lib._datasource.open(fname, 'wt', encoding=encoding)
Packit Service a47777
        own_fh = True
Packit Service a47777
        # need to convert str to unicode for text io output
Packit Service a47777
        if sys.version_info[0] == 2:
Packit Service a47777
            fh = WriteWrap(fh, encoding or 'latin1')
Packit Service a47777
    elif hasattr(fname, 'write'):
Packit Service a47777
        # wrap to handle byte output streams
Packit Service a47777
        fh = WriteWrap(fname, encoding or 'latin1')
Packit Service a47777
    else:
Packit Service a47777
        raise ValueError('fname must be a string or file handle')
Packit Service a47777
Packit Service a47777
    try:
Packit Service a47777
        X = np.asarray(X)
Packit Service a47777
Packit Service a47777
        # Handle 1-dimensional arrays
Packit Service a47777
        if X.ndim == 0 or X.ndim > 2:
Packit Service a47777
            raise ValueError(
Packit Service a47777
                "Expected 1D or 2D array, got %dD array instead" % X.ndim)
Packit Service a47777
        elif X.ndim == 1:
Packit Service a47777
            # Common case -- 1d array of numbers
Packit Service a47777
            if X.dtype.names is None:
Packit Service a47777
                X = np.atleast_2d(X).T
Packit Service a47777
                ncol = 1
Packit Service a47777
Packit Service a47777
            # Complex dtype -- each field indicates a separate column
Packit Service a47777
            else:
Packit Service a47777
                ncol = len(X.dtype.descr)
Packit Service a47777
        else:
Packit Service a47777
            ncol = X.shape[1]
Packit Service a47777
Packit Service a47777
        iscomplex_X = np.iscomplexobj(X)
Packit Service a47777
        # `fmt` can be a string with multiple insertion points or a
Packit Service a47777
        # list of formats.  E.g. '%10.5f\t%10d' or ('%10.5f', '$10d')
Packit Service a47777
        if type(fmt) in (list, tuple):
Packit Service a47777
            if len(fmt) != ncol:
Packit Service a47777
                raise AttributeError('fmt has wrong shape.  %s' % str(fmt))
Packit Service a47777
            format = asstr(delimiter).join(map(asstr, fmt))
Packit Service a47777
        elif isinstance(fmt, str):
Packit Service a47777
            n_fmt_chars = fmt.count('%')
Packit Service a47777
            error = ValueError('fmt has wrong number of %% formats:  %s' % fmt)
Packit Service a47777
            if n_fmt_chars == 1:
Packit Service a47777
                if iscomplex_X:
Packit Service a47777
                    fmt = [' (%s+%sj)' % (fmt, fmt), ] * ncol
Packit Service a47777
                else:
Packit Service a47777
                    fmt = [fmt, ] * ncol
Packit Service a47777
                format = delimiter.join(fmt)
Packit Service a47777
            elif iscomplex_X and n_fmt_chars != (2 * ncol):
Packit Service a47777
                raise error
Packit Service a47777
            elif ((not iscomplex_X) and n_fmt_chars != ncol):
Packit Service a47777
                raise error
Packit Service a47777
            else:
Packit Service a47777
                format = fmt
Packit Service a47777
        else:
Packit Service a47777
            raise ValueError('invalid fmt: %r' % (fmt,))
Packit Service a47777
Packit Service a47777
        if len(header) > 0:
Packit Service a47777
            header = header.replace('\n', '\n' + comments)
Packit Service a47777
            fh.write(comments + header + newline)
Packit Service a47777
        if iscomplex_X:
Packit Service a47777
            for row in X:
Packit Service a47777
                row2 = []
Packit Service a47777
                for number in row:
Packit Service a47777
                    row2.append(number.real)
Packit Service a47777
                    row2.append(number.imag)
Packit Service a47777
                fh.write(format % tuple(row2) + newline)
Packit Service a47777
        else:
Packit Service a47777
            for row in X:
Packit Service a47777
                try:
Packit Service a47777
                    v = format % tuple(row) + newline
Packit Service a47777
                except TypeError:
Packit Service a47777
                    raise TypeError("Mismatch between array dtype ('%s') and "
Packit Service a47777
                                    "format specifier ('%s')"
Packit Service a47777
                                    % (str(X.dtype), format))
Packit Service a47777
                fh.write(v)
Packit Service a47777
Packit Service a47777
        if len(footer) > 0:
Packit Service a47777
            footer = footer.replace('\n', '\n' + comments)
Packit Service a47777
            fh.write(comments + footer + newline)
Packit Service a47777
    finally:
Packit Service a47777
        if own_fh:
Packit Service a47777
            fh.close()
Packit Service a47777
Packit Service a47777
Packit Service a47777
def fromregex(file, regexp, dtype, encoding=None):
Packit Service a47777
    """
Packit Service a47777
    Construct an array from a text file, using regular expression parsing.
Packit Service a47777
Packit Service a47777
    The returned array is always a structured array, and is constructed from
Packit Service a47777
    all matches of the regular expression in the file. Groups in the regular
Packit Service a47777
    expression are converted to fields of the structured array.
Packit Service a47777
Packit Service a47777
    Parameters
Packit Service a47777
    ----------
Packit Service a47777
    file : str or file
Packit Service a47777
        File name or file object to read.
Packit Service a47777
    regexp : str or regexp
Packit Service a47777
        Regular expression used to parse the file.
Packit Service a47777
        Groups in the regular expression correspond to fields in the dtype.
Packit Service a47777
    dtype : dtype or list of dtypes
Packit Service a47777
        Dtype for the structured array.
Packit Service a47777
    encoding : str, optional
Packit Service a47777
        Encoding used to decode the inputfile. Does not apply to input streams.
Packit Service a47777
Packit Service a47777
        .. versionadded:: 1.14.0
Packit Service a47777
Packit Service a47777
    Returns
Packit Service a47777
    -------
Packit Service a47777
    output : ndarray
Packit Service a47777
        The output array, containing the part of the content of `file` that
Packit Service a47777
        was matched by `regexp`. `output` is always a structured array.
Packit Service a47777
Packit Service a47777
    Raises
Packit Service a47777
    ------
Packit Service a47777
    TypeError
Packit Service a47777
        When `dtype` is not a valid dtype for a structured array.
Packit Service a47777
Packit Service a47777
    See Also
Packit Service a47777
    --------
Packit Service a47777
    fromstring, loadtxt
Packit Service a47777
Packit Service a47777
    Notes
Packit Service a47777
    -----
Packit Service a47777
    Dtypes for structured arrays can be specified in several forms, but all
Packit Service a47777
    forms specify at least the data type and field name. For details see
Packit Service a47777
    `doc.structured_arrays`.
Packit Service a47777
Packit Service a47777
    Examples
Packit Service a47777
    --------
Packit Service a47777
    >>> f = open('test.dat', 'w')
Packit Service a47777
    >>> f.write("1312 foo\\n1534  bar\\n444   qux")
Packit Service a47777
    >>> f.close()
Packit Service a47777
Packit Service a47777
    >>> regexp = r"(\\d+)\\s+(...)"  # match [digits, whitespace, anything]
Packit Service a47777
    >>> output = np.fromregex('test.dat', regexp,
Packit Service a47777
    ...                       [('num', np.int64), ('key', 'S3')])
Packit Service a47777
    >>> output
Packit Service a47777
    array([(1312L, 'foo'), (1534L, 'bar'), (444L, 'qux')],
Packit Service a47777
          dtype=[('num', '
Packit Service a47777
    >>> output['num']
Packit Service a47777
    array([1312, 1534,  444], dtype=int64)
Packit Service a47777
Packit Service a47777
    """
Packit Service a47777
    own_fh = False
Packit Service a47777
    if not hasattr(file, "read"):
Packit Service a47777
        file = np.lib._datasource.open(file, 'rt', encoding=encoding)
Packit Service a47777
        own_fh = True
Packit Service a47777
Packit Service a47777
    try:
Packit Service a47777
        if not isinstance(dtype, np.dtype):
Packit Service a47777
            dtype = np.dtype(dtype)
Packit Service a47777
Packit Service a47777
        content = file.read()
Packit Service a47777
        if isinstance(content, bytes) and not isinstance(regexp, bytes):
Packit Service a47777
            regexp = asbytes(regexp)
Packit Service a47777
        elif not isinstance(content, bytes) and isinstance(regexp, bytes):
Packit Service a47777
            regexp = asstr(regexp)
Packit Service a47777
Packit Service a47777
        if not hasattr(regexp, 'match'):
Packit Service a47777
            regexp = re.compile(regexp)
Packit Service a47777
        seq = regexp.findall(content)
Packit Service a47777
        if seq and not isinstance(seq[0], tuple):
Packit Service a47777
            # Only one group is in the regexp.
Packit Service a47777
            # Create the new array as a single data-type and then
Packit Service a47777
            #   re-interpret as a single-field structured array.
Packit Service a47777
            newdtype = np.dtype(dtype[dtype.names[0]])
Packit Service a47777
            output = np.array(seq, dtype=newdtype)
Packit Service a47777
            output.dtype = dtype
Packit Service a47777
        else:
Packit Service a47777
            output = np.array(seq, dtype=dtype)
Packit Service a47777
Packit Service a47777
        return output
Packit Service a47777
    finally:
Packit Service a47777
        if own_fh:
Packit Service a47777
            file.close()
Packit Service a47777
Packit Service a47777
Packit Service a47777
#####--------------------------------------------------------------------------
Packit Service a47777
#---- --- ASCII functions ---
Packit Service a47777
#####--------------------------------------------------------------------------
Packit Service a47777
Packit Service a47777
Packit Service a47777
def genfromtxt(fname, dtype=float, comments='#', delimiter=None,
Packit Service a47777
               skip_header=0, skip_footer=0, converters=None,
Packit Service a47777
               missing_values=None, filling_values=None, usecols=None,
Packit Service a47777
               names=None, excludelist=None, deletechars=None,
Packit Service a47777
               replace_space='_', autostrip=False, case_sensitive=True,
Packit Service a47777
               defaultfmt="f%i", unpack=None, usemask=False, loose=True,
Packit Service a47777
               invalid_raise=True, max_rows=None, encoding='bytes'):
Packit Service a47777
    """
Packit Service a47777
    Load data from a text file, with missing values handled as specified.
Packit Service a47777
Packit Service a47777
    Each line past the first `skip_header` lines is split at the `delimiter`
Packit Service a47777
    character, and characters following the `comments` character are discarded.
Packit Service a47777
Packit Service a47777
    Parameters
Packit Service a47777
    ----------
Packit Service a47777
    fname : file, str, pathlib.Path, list of str, generator
Packit Service a47777
        File, filename, list, or generator to read.  If the filename
Packit Service a47777
        extension is `.gz` or `.bz2`, the file is first decompressed. Note
Packit Service a47777
        that generators must return byte strings in Python 3k.  The strings
Packit Service a47777
        in a list or produced by a generator are treated as lines.
Packit Service a47777
    dtype : dtype, optional
Packit Service a47777
        Data type of the resulting array.
Packit Service a47777
        If None, the dtypes will be determined by the contents of each
Packit Service a47777
        column, individually.
Packit Service a47777
    comments : str, optional
Packit Service a47777
        The character used to indicate the start of a comment.
Packit Service a47777
        All the characters occurring on a line after a comment are discarded
Packit Service a47777
    delimiter : str, int, or sequence, optional
Packit Service a47777
        The string used to separate values.  By default, any consecutive
Packit Service a47777
        whitespaces act as delimiter.  An integer or sequence of integers
Packit Service a47777
        can also be provided as width(s) of each field.
Packit Service a47777
    skiprows : int, optional
Packit Service a47777
        `skiprows` was removed in numpy 1.10. Please use `skip_header` instead.
Packit Service a47777
    skip_header : int, optional
Packit Service a47777
        The number of lines to skip at the beginning of the file.
Packit Service a47777
    skip_footer : int, optional
Packit Service a47777
        The number of lines to skip at the end of the file.
Packit Service a47777
    converters : variable, optional
Packit Service a47777
        The set of functions that convert the data of a column to a value.
Packit Service a47777
        The converters can also be used to provide a default value
Packit Service a47777
        for missing data: ``converters = {3: lambda s: float(s or 0)}``.
Packit Service a47777
    missing : variable, optional
Packit Service a47777
        `missing` was removed in numpy 1.10. Please use `missing_values`
Packit Service a47777
        instead.
Packit Service a47777
    missing_values : variable, optional
Packit Service a47777
        The set of strings corresponding to missing data.
Packit Service a47777
    filling_values : variable, optional
Packit Service a47777
        The set of values to be used as default when the data are missing.
Packit Service a47777
    usecols : sequence, optional
Packit Service a47777
        Which columns to read, with 0 being the first.  For example,
Packit Service a47777
        ``usecols = (1, 4, 5)`` will extract the 2nd, 5th and 6th columns.
Packit Service a47777
    names : {None, True, str, sequence}, optional
Packit Service a47777
        If `names` is True, the field names are read from the first line after
Packit Service a47777
        the first `skip_header` lines.  This line can optionally be proceeded
Packit Service a47777
        by a comment delimeter. If `names` is a sequence or a single-string of
Packit Service a47777
        comma-separated names, the names will be used to define the field names
Packit Service a47777
        in a structured dtype. If `names` is None, the names of the dtype
Packit Service a47777
        fields will be used, if any.
Packit Service a47777
    excludelist : sequence, optional
Packit Service a47777
        A list of names to exclude. This list is appended to the default list
Packit Service a47777
        ['return','file','print']. Excluded names are appended an underscore:
Packit Service a47777
        for example, `file` would become `file_`.
Packit Service a47777
    deletechars : str, optional
Packit Service a47777
        A string combining invalid characters that must be deleted from the
Packit Service a47777
        names.
Packit Service a47777
    defaultfmt : str, optional
Packit Service a47777
        A format used to define default field names, such as "f%i" or "f_%02i".
Packit Service a47777
    autostrip : bool, optional
Packit Service a47777
        Whether to automatically strip white spaces from the variables.
Packit Service a47777
    replace_space : char, optional
Packit Service a47777
        Character(s) used in replacement of white spaces in the variables
Packit Service a47777
        names. By default, use a '_'.
Packit Service a47777
    case_sensitive : {True, False, 'upper', 'lower'}, optional
Packit Service a47777
        If True, field names are case sensitive.
Packit Service a47777
        If False or 'upper', field names are converted to upper case.
Packit Service a47777
        If 'lower', field names are converted to lower case.
Packit Service a47777
    unpack : bool, optional
Packit Service a47777
        If True, the returned array is transposed, so that arguments may be
Packit Service a47777
        unpacked using ``x, y, z = loadtxt(...)``
Packit Service a47777
    usemask : bool, optional
Packit Service a47777
        If True, return a masked array.
Packit Service a47777
        If False, return a regular array.
Packit Service a47777
    loose : bool, optional
Packit Service a47777
        If True, do not raise errors for invalid values.
Packit Service a47777
    invalid_raise : bool, optional
Packit Service a47777
        If True, an exception is raised if an inconsistency is detected in the
Packit Service a47777
        number of columns.
Packit Service a47777
        If False, a warning is emitted and the offending lines are skipped.
Packit Service a47777
    max_rows : int,  optional
Packit Service a47777
        The maximum number of rows to read. Must not be used with skip_footer
Packit Service a47777
        at the same time.  If given, the value must be at least 1. Default is
Packit Service a47777
        to read the entire file.
Packit Service a47777
Packit Service a47777
        .. versionadded:: 1.10.0
Packit Service a47777
    encoding : str, optional
Packit Service a47777
        Encoding used to decode the inputfile. Does not apply when `fname` is
Packit Service a47777
        a file object.  The special value 'bytes' enables backward compatibility
Packit Service a47777
        workarounds that ensure that you receive byte arrays when possible
Packit Service a47777
        and passes latin1 encoded strings to converters. Override this value to
Packit Service a47777
        receive unicode arrays and pass strings as input to converters.  If set
Packit Service a47777
        to None the system default is used. The default value is 'bytes'.
Packit Service a47777
Packit Service a47777
        .. versionadded:: 1.14.0
Packit Service a47777
Packit Service a47777
    Returns
Packit Service a47777
    -------
Packit Service a47777
    out : ndarray
Packit Service a47777
        Data read from the text file. If `usemask` is True, this is a
Packit Service a47777
        masked array.
Packit Service a47777
Packit Service a47777
    See Also
Packit Service a47777
    --------
Packit Service a47777
    numpy.loadtxt : equivalent function when no data is missing.
Packit Service a47777
Packit Service a47777
    Notes
Packit Service a47777
    -----
Packit Service a47777
    * When spaces are used as delimiters, or when no delimiter has been given
Packit Service a47777
      as input, there should not be any missing data between two fields.
Packit Service a47777
    * When the variables are named (either by a flexible dtype or with `names`,
Packit Service a47777
      there must not be any header in the file (else a ValueError
Packit Service a47777
      exception is raised).
Packit Service a47777
    * Individual values are not stripped of spaces by default.
Packit Service a47777
      When using a custom converter, make sure the function does remove spaces.
Packit Service a47777
Packit Service a47777
    References
Packit Service a47777
    ----------
Packit Service a47777
    .. [1] NumPy User Guide, section `I/O with NumPy
Packit Service a47777
           <http://docs.scipy.org/doc/numpy/user/basics.io.genfromtxt.html>`_.
Packit Service a47777
Packit Service a47777
    Examples
Packit Service a47777
    ---------
Packit Service a47777
    >>> from io import StringIO
Packit Service a47777
    >>> import numpy as np
Packit Service a47777
Packit Service a47777
    Comma delimited file with mixed dtype
Packit Service a47777
Packit Service a47777
    >>> s = StringIO("1,1.3,abcde")
Packit Service a47777
    >>> data = np.genfromtxt(s, dtype=[('myint','i8'),('myfloat','f8'),
Packit Service a47777
    ... ('mystring','S5')], delimiter=",")
Packit Service a47777
    >>> data
Packit Service a47777
    array((1, 1.3, 'abcde'),
Packit Service a47777
          dtype=[('myint', '
Packit Service a47777
Packit Service a47777
    Using dtype = None
Packit Service a47777
Packit Service a47777
    >>> s.seek(0) # needed for StringIO example only
Packit Service a47777
    >>> data = np.genfromtxt(s, dtype=None,
Packit Service a47777
    ... names = ['myint','myfloat','mystring'], delimiter=",")
Packit Service a47777
    >>> data
Packit Service a47777
    array((1, 1.3, 'abcde'),
Packit Service a47777
          dtype=[('myint', '
Packit Service a47777
Packit Service a47777
    Specifying dtype and names
Packit Service a47777
Packit Service a47777
    >>> s.seek(0)
Packit Service a47777
    >>> data = np.genfromtxt(s, dtype="i8,f8,S5",
Packit Service a47777
    ... names=['myint','myfloat','mystring'], delimiter=",")
Packit Service a47777
    >>> data
Packit Service a47777
    array((1, 1.3, 'abcde'),
Packit Service a47777
          dtype=[('myint', '
Packit Service a47777
Packit Service a47777
    An example with fixed-width columns
Packit Service a47777
Packit Service a47777
    >>> s = StringIO("11.3abcde")
Packit Service a47777
    >>> data = np.genfromtxt(s, dtype=None, names=['intvar','fltvar','strvar'],
Packit Service a47777
    ...     delimiter=[1,3,5])
Packit Service a47777
    >>> data
Packit Service a47777
    array((1, 1.3, 'abcde'),
Packit Service a47777
          dtype=[('intvar', '
Packit Service a47777
Packit Service a47777
    """
Packit Service a47777
    if max_rows is not None:
Packit Service a47777
        if skip_footer:
Packit Service a47777
            raise ValueError(
Packit Service a47777
                    "The keywords 'skip_footer' and 'max_rows' can not be "
Packit Service a47777
                    "specified at the same time.")
Packit Service a47777
        if max_rows < 1:
Packit Service a47777
            raise ValueError("'max_rows' must be at least 1.")
Packit Service a47777
Packit Service a47777
    if usemask:
Packit Service a47777
        from numpy.ma import MaskedArray, make_mask_descr
Packit Service a47777
    # Check the input dictionary of converters
Packit Service a47777
    user_converters = converters or {}
Packit Service a47777
    if not isinstance(user_converters, dict):
Packit Service a47777
        raise TypeError(
Packit Service a47777
            "The input argument 'converter' should be a valid dictionary "
Packit Service a47777
            "(got '%s' instead)" % type(user_converters))
Packit Service a47777
Packit Service a47777
    if encoding == 'bytes':
Packit Service a47777
        encoding = None
Packit Service a47777
        byte_converters = True
Packit Service a47777
    else:
Packit Service a47777
        byte_converters = False
Packit Service a47777
Packit Service a47777
    # Initialize the filehandle, the LineSplitter and the NameValidator
Packit Service a47777
    own_fhd = False
Packit Service a47777
    try:
Packit Service a47777
        if is_pathlib_path(fname):
Packit Service a47777
            fname = str(fname)
Packit Service a47777
        if isinstance(fname, basestring):
Packit Service a47777
            fhd = iter(np.lib._datasource.open(fname, 'rt', encoding=encoding))
Packit Service a47777
            own_fhd = True
Packit Service a47777
        else:
Packit Service a47777
            fhd = iter(fname)
Packit Service a47777
    except TypeError:
Packit Service a47777
        raise TypeError(
Packit Service a47777
            "fname must be a string, filehandle, list of strings, "
Packit Service a47777
            "or generator. Got %s instead." % type(fname))
Packit Service a47777
Packit Service a47777
    split_line = LineSplitter(delimiter=delimiter, comments=comments,
Packit Service a47777
                              autostrip=autostrip, encoding=encoding)
Packit Service a47777
    validate_names = NameValidator(excludelist=excludelist,
Packit Service a47777
                                   deletechars=deletechars,
Packit Service a47777
                                   case_sensitive=case_sensitive,
Packit Service a47777
                                   replace_space=replace_space)
Packit Service a47777
Packit Service a47777
    # Skip the first `skip_header` rows
Packit Service a47777
    for i in range(skip_header):
Packit Service a47777
        next(fhd)
Packit Service a47777
Packit Service a47777
    # Keep on until we find the first valid values
Packit Service a47777
    first_values = None
Packit Service a47777
    try:
Packit Service a47777
        while not first_values:
Packit Service a47777
            first_line = _decode_line(next(fhd), encoding)
Packit Service a47777
            if names is True:
Packit Service a47777
                if comments in first_line:
Packit Service a47777
                    first_line = (
Packit Service a47777
                        ''.join(first_line.split(comments)[1:]))
Packit Service a47777
            first_values = split_line(first_line)
Packit Service a47777
    except StopIteration:
Packit Service a47777
        # return an empty array if the datafile is empty
Packit Service a47777
        first_line = ''
Packit Service a47777
        first_values = []
Packit Service a47777
        warnings.warn('genfromtxt: Empty input file: "%s"' % fname, stacklevel=2)
Packit Service a47777
Packit Service a47777
    # Should we take the first values as names ?
Packit Service a47777
    if names is True:
Packit Service a47777
        fval = first_values[0].strip()
Packit Service a47777
        if fval in comments:
Packit Service a47777
            del first_values[0]
Packit Service a47777
Packit Service a47777
    # Check the columns to use: make sure `usecols` is a list
Packit Service a47777
    if usecols is not None:
Packit Service a47777
        try:
Packit Service a47777
            usecols = [_.strip() for _ in usecols.split(",")]
Packit Service a47777
        except AttributeError:
Packit Service a47777
            try:
Packit Service a47777
                usecols = list(usecols)
Packit Service a47777
            except TypeError:
Packit Service a47777
                usecols = [usecols, ]
Packit Service a47777
    nbcols = len(usecols or first_values)
Packit Service a47777
Packit Service a47777
    # Check the names and overwrite the dtype.names if needed
Packit Service a47777
    if names is True:
Packit Service a47777
        names = validate_names([str(_.strip()) for _ in first_values])
Packit Service a47777
        first_line = ''
Packit Service a47777
    elif _is_string_like(names):
Packit Service a47777
        names = validate_names([_.strip() for _ in names.split(',')])
Packit Service a47777
    elif names:
Packit Service a47777
        names = validate_names(names)
Packit Service a47777
    # Get the dtype
Packit Service a47777
    if dtype is not None:
Packit Service a47777
        dtype = easy_dtype(dtype, defaultfmt=defaultfmt, names=names,
Packit Service a47777
                           excludelist=excludelist,
Packit Service a47777
                           deletechars=deletechars,
Packit Service a47777
                           case_sensitive=case_sensitive,
Packit Service a47777
                           replace_space=replace_space)
Packit Service a47777
    # Make sure the names is a list (for 2.5)
Packit Service a47777
    if names is not None:
Packit Service a47777
        names = list(names)
Packit Service a47777
Packit Service a47777
    if usecols:
Packit Service a47777
        for (i, current) in enumerate(usecols):
Packit Service a47777
            # if usecols is a list of names, convert to a list of indices
Packit Service a47777
            if _is_string_like(current):
Packit Service a47777
                usecols[i] = names.index(current)
Packit Service a47777
            elif current < 0:
Packit Service a47777
                usecols[i] = current + len(first_values)
Packit Service a47777
        # If the dtype is not None, make sure we update it
Packit Service a47777
        if (dtype is not None) and (len(dtype) > nbcols):
Packit Service a47777
            descr = dtype.descr
Packit Service a47777
            dtype = np.dtype([descr[_] for _ in usecols])
Packit Service a47777
            names = list(dtype.names)
Packit Service a47777
        # If `names` is not None, update the names
Packit Service a47777
        elif (names is not None) and (len(names) > nbcols):
Packit Service a47777
            names = [names[_] for _ in usecols]
Packit Service a47777
    elif (names is not None) and (dtype is not None):
Packit Service a47777
        names = list(dtype.names)
Packit Service a47777
Packit Service a47777
    # Process the missing values ...............................
Packit Service a47777
    # Rename missing_values for convenience
Packit Service a47777
    user_missing_values = missing_values or ()
Packit Service a47777
    if isinstance(user_missing_values, bytes):
Packit Service a47777
        user_missing_values = user_missing_values.decode('latin1')
Packit Service a47777
Packit Service a47777
    # Define the list of missing_values (one column: one list)
Packit Service a47777
    missing_values = [list(['']) for _ in range(nbcols)]
Packit Service a47777
Packit Service a47777
    # We have a dictionary: process it field by field
Packit Service a47777
    if isinstance(user_missing_values, dict):
Packit Service a47777
        # Loop on the items
Packit Service a47777
        for (key, val) in user_missing_values.items():
Packit Service a47777
            # Is the key a string ?
Packit Service a47777
            if _is_string_like(key):
Packit Service a47777
                try:
Packit Service a47777
                    # Transform it into an integer
Packit Service a47777
                    key = names.index(key)
Packit Service a47777
                except ValueError:
Packit Service a47777
                    # We couldn't find it: the name must have been dropped
Packit Service a47777
                    continue
Packit Service a47777
            # Redefine the key as needed if it's a column number
Packit Service a47777
            if usecols:
Packit Service a47777
                try:
Packit Service a47777
                    key = usecols.index(key)
Packit Service a47777
                except ValueError:
Packit Service a47777
                    pass
Packit Service a47777
            # Transform the value as a list of string
Packit Service a47777
            if isinstance(val, (list, tuple)):
Packit Service a47777
                val = [str(_) for _ in val]
Packit Service a47777
            else:
Packit Service a47777
                val = [str(val), ]
Packit Service a47777
            # Add the value(s) to the current list of missing
Packit Service a47777
            if key is None:
Packit Service a47777
                # None acts as default
Packit Service a47777
                for miss in missing_values:
Packit Service a47777
                    miss.extend(val)
Packit Service a47777
            else:
Packit Service a47777
                missing_values[key].extend(val)
Packit Service a47777
    # We have a sequence : each item matches a column
Packit Service a47777
    elif isinstance(user_missing_values, (list, tuple)):
Packit Service a47777
        for (value, entry) in zip(user_missing_values, missing_values):
Packit Service a47777
            value = str(value)
Packit Service a47777
            if value not in entry:
Packit Service a47777
                entry.append(value)
Packit Service a47777
    # We have a string : apply it to all entries
Packit Service a47777
    elif isinstance(user_missing_values, basestring):
Packit Service a47777
        user_value = user_missing_values.split(",")
Packit Service a47777
        for entry in missing_values:
Packit Service a47777
            entry.extend(user_value)
Packit Service a47777
    # We have something else: apply it to all entries
Packit Service a47777
    else:
Packit Service a47777
        for entry in missing_values:
Packit Service a47777
            entry.extend([str(user_missing_values)])
Packit Service a47777
Packit Service a47777
    # Process the filling_values ...............................
Packit Service a47777
    # Rename the input for convenience
Packit Service a47777
    user_filling_values = filling_values
Packit Service a47777
    if user_filling_values is None:
Packit Service a47777
        user_filling_values = []
Packit Service a47777
    # Define the default
Packit Service a47777
    filling_values = [None] * nbcols
Packit Service a47777
    # We have a dictionary : update each entry individually
Packit Service a47777
    if isinstance(user_filling_values, dict):
Packit Service a47777
        for (key, val) in user_filling_values.items():
Packit Service a47777
            if _is_string_like(key):
Packit Service a47777
                try:
Packit Service a47777
                    # Transform it into an integer
Packit Service a47777
                    key = names.index(key)
Packit Service a47777
                except ValueError:
Packit Service a47777
                    # We couldn't find it: the name must have been dropped,
Packit Service a47777
                    continue
Packit Service a47777
            # Redefine the key if it's a column number and usecols is defined
Packit Service a47777
            if usecols:
Packit Service a47777
                try:
Packit Service a47777
                    key = usecols.index(key)
Packit Service a47777
                except ValueError:
Packit Service a47777
                    pass
Packit Service a47777
            # Add the value to the list
Packit Service a47777
            filling_values[key] = val
Packit Service a47777
    # We have a sequence : update on a one-to-one basis
Packit Service a47777
    elif isinstance(user_filling_values, (list, tuple)):
Packit Service a47777
        n = len(user_filling_values)
Packit Service a47777
        if (n <= nbcols):
Packit Service a47777
            filling_values[:n] = user_filling_values
Packit Service a47777
        else:
Packit Service a47777
            filling_values = user_filling_values[:nbcols]
Packit Service a47777
    # We have something else : use it for all entries
Packit Service a47777
    else:
Packit Service a47777
        filling_values = [user_filling_values] * nbcols
Packit Service a47777
Packit Service a47777
    # Initialize the converters ................................
Packit Service a47777
    if dtype is None:
Packit Service a47777
        # Note: we can't use a [...]*nbcols, as we would have 3 times the same
Packit Service a47777
        # ... converter, instead of 3 different converters.
Packit Service a47777
        converters = [StringConverter(None, missing_values=miss, default=fill)
Packit Service a47777
                      for (miss, fill) in zip(missing_values, filling_values)]
Packit Service a47777
    else:
Packit Service a47777
        dtype_flat = flatten_dtype(dtype, flatten_base=True)
Packit Service a47777
        # Initialize the converters
Packit Service a47777
        if len(dtype_flat) > 1:
Packit Service a47777
            # Flexible type : get a converter from each dtype
Packit Service a47777
            zipit = zip(dtype_flat, missing_values, filling_values)
Packit Service a47777
            converters = [StringConverter(dt, locked=True,
Packit Service a47777
                                          missing_values=miss, default=fill)
Packit Service a47777
                          for (dt, miss, fill) in zipit]
Packit Service a47777
        else:
Packit Service a47777
            # Set to a default converter (but w/ different missing values)
Packit Service a47777
            zipit = zip(missing_values, filling_values)
Packit Service a47777
            converters = [StringConverter(dtype, locked=True,
Packit Service a47777
                                          missing_values=miss, default=fill)
Packit Service a47777
                          for (miss, fill) in zipit]
Packit Service a47777
    # Update the converters to use the user-defined ones
Packit Service a47777
    uc_update = []
Packit Service a47777
    for (j, conv) in user_converters.items():
Packit Service a47777
        # If the converter is specified by column names, use the index instead
Packit Service a47777
        if _is_string_like(j):
Packit Service a47777
            try:
Packit Service a47777
                j = names.index(j)
Packit Service a47777
                i = j
Packit Service a47777
            except ValueError:
Packit Service a47777
                continue
Packit Service a47777
        elif usecols:
Packit Service a47777
            try:
Packit Service a47777
                i = usecols.index(j)
Packit Service a47777
            except ValueError:
Packit Service a47777
                # Unused converter specified
Packit Service a47777
                continue
Packit Service a47777
        else:
Packit Service a47777
            i = j
Packit Service a47777
        # Find the value to test - first_line is not filtered by usecols:
Packit Service a47777
        if len(first_line):
Packit Service a47777
            testing_value = first_values[j]
Packit Service a47777
        else:
Packit Service a47777
            testing_value = None
Packit Service a47777
        if conv is bytes:
Packit Service a47777
            user_conv = asbytes
Packit Service a47777
        elif byte_converters:
Packit Service a47777
            # converters may use decode to workaround numpy's old behaviour,
Packit Service a47777
            # so encode the string again before passing to the user converter
Packit Service a47777
            def tobytes_first(x, conv):
Packit Service a47777
                if type(x) is bytes:
Packit Service a47777
                    return conv(x)
Packit Service a47777
                return conv(x.encode("latin1"))
Packit Service a47777
            import functools
Packit Service a47777
            user_conv = functools.partial(tobytes_first, conv=conv)
Packit Service a47777
        else:
Packit Service a47777
            user_conv = conv
Packit Service a47777
        converters[i].update(user_conv, locked=True,
Packit Service a47777
                             testing_value=testing_value,
Packit Service a47777
                             default=filling_values[i],
Packit Service a47777
                             missing_values=missing_values[i],)
Packit Service a47777
        uc_update.append((i, user_conv))
Packit Service a47777
    # Make sure we have the corrected keys in user_converters...
Packit Service a47777
    user_converters.update(uc_update)
Packit Service a47777
Packit Service a47777
    # Fixme: possible error as following variable never used.
Packit Service a47777
    # miss_chars = [_.missing_values for _ in converters]
Packit Service a47777
Packit Service a47777
    # Initialize the output lists ...
Packit Service a47777
    # ... rows
Packit Service a47777
    rows = []
Packit Service a47777
    append_to_rows = rows.append
Packit Service a47777
    # ... masks
Packit Service a47777
    if usemask:
Packit Service a47777
        masks = []
Packit Service a47777
        append_to_masks = masks.append
Packit Service a47777
    # ... invalid
Packit Service a47777
    invalid = []
Packit Service a47777
    append_to_invalid = invalid.append
Packit Service a47777
Packit Service a47777
    # Parse each line
Packit Service a47777
    for (i, line) in enumerate(itertools.chain([first_line, ], fhd)):
Packit Service a47777
        values = split_line(line)
Packit Service a47777
        nbvalues = len(values)
Packit Service a47777
        # Skip an empty line
Packit Service a47777
        if nbvalues == 0:
Packit Service a47777
            continue
Packit Service a47777
        if usecols:
Packit Service a47777
            # Select only the columns we need
Packit Service a47777
            try:
Packit Service a47777
                values = [values[_] for _ in usecols]
Packit Service a47777
            except IndexError:
Packit Service a47777
                append_to_invalid((i + skip_header + 1, nbvalues))
Packit Service a47777
                continue
Packit Service a47777
        elif nbvalues != nbcols:
Packit Service a47777
            append_to_invalid((i + skip_header + 1, nbvalues))
Packit Service a47777
            continue
Packit Service a47777
        # Store the values
Packit Service a47777
        append_to_rows(tuple(values))
Packit Service a47777
        if usemask:
Packit Service a47777
            append_to_masks(tuple([v.strip() in m
Packit Service a47777
                                   for (v, m) in zip(values,
Packit Service a47777
                                                     missing_values)]))
Packit Service a47777
        if len(rows) == max_rows:
Packit Service a47777
            break
Packit Service a47777
Packit Service a47777
    if own_fhd:
Packit Service a47777
        fhd.close()
Packit Service a47777
Packit Service a47777
    # Upgrade the converters (if needed)
Packit Service a47777
    if dtype is None:
Packit Service a47777
        for (i, converter) in enumerate(converters):
Packit Service a47777
            current_column = [itemgetter(i)(_m) for _m in rows]
Packit Service a47777
            try:
Packit Service a47777
                converter.iterupgrade(current_column)
Packit Service a47777
            except ConverterLockError:
Packit Service a47777
                errmsg = "Converter #%i is locked and cannot be upgraded: " % i
Packit Service a47777
                current_column = map(itemgetter(i), rows)
Packit Service a47777
                for (j, value) in enumerate(current_column):
Packit Service a47777
                    try:
Packit Service a47777
                        converter.upgrade(value)
Packit Service a47777
                    except (ConverterError, ValueError):
Packit Service a47777
                        errmsg += "(occurred line #%i for value '%s')"
Packit Service a47777
                        errmsg %= (j + 1 + skip_header, value)
Packit Service a47777
                        raise ConverterError(errmsg)
Packit Service a47777
Packit Service a47777
    # Check that we don't have invalid values
Packit Service a47777
    nbinvalid = len(invalid)
Packit Service a47777
    if nbinvalid > 0:
Packit Service a47777
        nbrows = len(rows) + nbinvalid - skip_footer
Packit Service a47777
        # Construct the error message
Packit Service a47777
        template = "    Line #%%i (got %%i columns instead of %i)" % nbcols
Packit Service a47777
        if skip_footer > 0:
Packit Service a47777
            nbinvalid_skipped = len([_ for _ in invalid
Packit Service a47777
                                     if _[0] > nbrows + skip_header])
Packit Service a47777
            invalid = invalid[:nbinvalid - nbinvalid_skipped]
Packit Service a47777
            skip_footer -= nbinvalid_skipped
Packit Service a47777
#
Packit Service a47777
#            nbrows -= skip_footer
Packit Service a47777
#            errmsg = [template % (i, nb)
Packit Service a47777
#                      for (i, nb) in invalid if i < nbrows]
Packit Service a47777
#        else:
Packit Service a47777
        errmsg = [template % (i, nb)
Packit Service a47777
                  for (i, nb) in invalid]
Packit Service a47777
        if len(errmsg):
Packit Service a47777
            errmsg.insert(0, "Some errors were detected !")
Packit Service a47777
            errmsg = "\n".join(errmsg)
Packit Service a47777
            # Raise an exception ?
Packit Service a47777
            if invalid_raise:
Packit Service a47777
                raise ValueError(errmsg)
Packit Service a47777
            # Issue a warning ?
Packit Service a47777
            else:
Packit Service a47777
                warnings.warn(errmsg, ConversionWarning, stacklevel=2)
Packit Service a47777
Packit Service a47777
    # Strip the last skip_footer data
Packit Service a47777
    if skip_footer > 0:
Packit Service a47777
        rows = rows[:-skip_footer]
Packit Service a47777
        if usemask:
Packit Service a47777
            masks = masks[:-skip_footer]
Packit Service a47777
Packit Service a47777
    # Convert each value according to the converter:
Packit Service a47777
    # We want to modify the list in place to avoid creating a new one...
Packit Service a47777
    if loose:
Packit Service a47777
        rows = list(
Packit Service a47777
            zip(*[[conv._loose_call(_r) for _r in map(itemgetter(i), rows)]
Packit Service a47777
                  for (i, conv) in enumerate(converters)]))
Packit Service a47777
    else:
Packit Service a47777
        rows = list(
Packit Service a47777
            zip(*[[conv._strict_call(_r) for _r in map(itemgetter(i), rows)]
Packit Service a47777
                  for (i, conv) in enumerate(converters)]))
Packit Service a47777
Packit Service a47777
    # Reset the dtype
Packit Service a47777
    data = rows
Packit Service a47777
    if dtype is None:
Packit Service a47777
        # Get the dtypes from the types of the converters
Packit Service a47777
        column_types = [conv.type for conv in converters]
Packit Service a47777
        # Find the columns with strings...
Packit Service a47777
        strcolidx = [i for (i, v) in enumerate(column_types)
Packit Service a47777
                     if v == np.unicode_]
Packit Service a47777
Packit Service a47777
        if byte_converters and strcolidx:
Packit Service a47777
            # convert strings back to bytes for backward compatibility
Packit Service a47777
            warnings.warn(
Packit Service a47777
                "Reading unicode strings without specifying the encoding "
Packit Service a47777
                "argument is deprecated. Set the encoding, use None for the "
Packit Service a47777
                "system default.",
Packit Service a47777
                np.VisibleDeprecationWarning, stacklevel=2)
Packit Service a47777
            def encode_unicode_cols(row_tup):
Packit Service a47777
                row = list(row_tup)
Packit Service a47777
                for i in strcolidx:
Packit Service a47777
                    row[i] = row[i].encode('latin1')
Packit Service a47777
                return tuple(row)
Packit Service a47777
Packit Service a47777
            try:
Packit Service a47777
                data = [encode_unicode_cols(r) for r in data]
Packit Service a47777
            except UnicodeEncodeError:
Packit Service a47777
                pass
Packit Service a47777
            else:
Packit Service a47777
                for i in strcolidx:
Packit Service a47777
                    column_types[i] = np.bytes_
Packit Service a47777
Packit Service a47777
        # Update string types to be the right length
Packit Service a47777
        sized_column_types = column_types[:]
Packit Service a47777
        for i, col_type in enumerate(column_types):
Packit Service a47777
            if np.issubdtype(col_type, np.character):
Packit Service a47777
                n_chars = max(len(row[i]) for row in data)
Packit Service a47777
                sized_column_types[i] = (col_type, n_chars)
Packit Service a47777
Packit Service a47777
        if names is None:
Packit Service a47777
            # If the dtype is uniform (before sizing strings)
Packit Service a47777
            base = set([
Packit Service a47777
                c_type
Packit Service a47777
                for c, c_type in zip(converters, column_types)
Packit Service a47777
                if c._checked])
Packit Service a47777
            if len(base) == 1:
Packit Service a47777
                uniform_type, = base
Packit Service a47777
                (ddtype, mdtype) = (uniform_type, bool)
Packit Service a47777
            else:
Packit Service a47777
                ddtype = [(defaultfmt % i, dt)
Packit Service a47777
                          for (i, dt) in enumerate(sized_column_types)]
Packit Service a47777
                if usemask:
Packit Service a47777
                    mdtype = [(defaultfmt % i, bool)
Packit Service a47777
                              for (i, dt) in enumerate(sized_column_types)]
Packit Service a47777
        else:
Packit Service a47777
            ddtype = list(zip(names, sized_column_types))
Packit Service a47777
            mdtype = list(zip(names, [bool] * len(sized_column_types)))
Packit Service a47777
        output = np.array(data, dtype=ddtype)
Packit Service a47777
        if usemask:
Packit Service a47777
            outputmask = np.array(masks, dtype=mdtype)
Packit Service a47777
    else:
Packit Service a47777
        # Overwrite the initial dtype names if needed
Packit Service a47777
        if names and dtype.names:
Packit Service a47777
            dtype.names = names
Packit Service a47777
        # Case 1. We have a structured type
Packit Service a47777
        if len(dtype_flat) > 1:
Packit Service a47777
            # Nested dtype, eg [('a', int), ('b', [('b0', int), ('b1', 'f4')])]
Packit Service a47777
            # First, create the array using a flattened dtype:
Packit Service a47777
            # [('a', int), ('b1', int), ('b2', float)]
Packit Service a47777
            # Then, view the array using the specified dtype.
Packit Service a47777
            if 'O' in (_.char for _ in dtype_flat):
Packit Service a47777
                if has_nested_fields(dtype):
Packit Service a47777
                    raise NotImplementedError(
Packit Service a47777
                        "Nested fields involving objects are not supported...")
Packit Service a47777
                else:
Packit Service a47777
                    output = np.array(data, dtype=dtype)
Packit Service a47777
            else:
Packit Service a47777
                rows = np.array(data, dtype=[('', _) for _ in dtype_flat])
Packit Service a47777
                output = rows.view(dtype)
Packit Service a47777
            # Now, process the rowmasks the same way
Packit Service a47777
            if usemask:
Packit Service a47777
                rowmasks = np.array(
Packit Service a47777
                    masks, dtype=np.dtype([('', bool) for t in dtype_flat]))
Packit Service a47777
                # Construct the new dtype
Packit Service a47777
                mdtype = make_mask_descr(dtype)
Packit Service a47777
                outputmask = rowmasks.view(mdtype)
Packit Service a47777
        # Case #2. We have a basic dtype
Packit Service a47777
        else:
Packit Service a47777
            # We used some user-defined converters
Packit Service a47777
            if user_converters:
Packit Service a47777
                ishomogeneous = True
Packit Service a47777
                descr = []
Packit Service a47777
                for i, ttype in enumerate([conv.type for conv in converters]):
Packit Service a47777
                    # Keep the dtype of the current converter
Packit Service a47777
                    if i in user_converters:
Packit Service a47777
                        ishomogeneous &= (ttype == dtype.type)
Packit Service a47777
                        if np.issubdtype(ttype, np.character):
Packit Service a47777
                            ttype = (ttype, max(len(row[i]) for row in data))
Packit Service a47777
                        descr.append(('', ttype))
Packit Service a47777
                    else:
Packit Service a47777
                        descr.append(('', dtype))
Packit Service a47777
                # So we changed the dtype ?
Packit Service a47777
                if not ishomogeneous:
Packit Service a47777
                    # We have more than one field
Packit Service a47777
                    if len(descr) > 1:
Packit Service a47777
                        dtype = np.dtype(descr)
Packit Service a47777
                    # We have only one field: drop the name if not needed.
Packit Service a47777
                    else:
Packit Service a47777
                        dtype = np.dtype(ttype)
Packit Service a47777
            #
Packit Service a47777
            output = np.array(data, dtype)
Packit Service a47777
            if usemask:
Packit Service a47777
                if dtype.names:
Packit Service a47777
                    mdtype = [(_, bool) for _ in dtype.names]
Packit Service a47777
                else:
Packit Service a47777
                    mdtype = bool
Packit Service a47777
                outputmask = np.array(masks, dtype=mdtype)
Packit Service a47777
    # Try to take care of the missing data we missed
Packit Service a47777
    names = output.dtype.names
Packit Service a47777
    if usemask and names:
Packit Service a47777
        for (name, conv) in zip(names, converters):
Packit Service a47777
            missing_values = [conv(_) for _ in conv.missing_values
Packit Service a47777
                              if _ != '']
Packit Service a47777
            for mval in missing_values:
Packit Service a47777
                outputmask[name] |= (output[name] == mval)
Packit Service a47777
    # Construct the final array
Packit Service a47777
    if usemask:
Packit Service a47777
        output = output.view(MaskedArray)
Packit Service a47777
        output._mask = outputmask
Packit Service a47777
    if unpack:
Packit Service a47777
        return output.squeeze().T
Packit Service a47777
    return output.squeeze()
Packit Service a47777
Packit Service a47777
Packit Service a47777
def ndfromtxt(fname, **kwargs):
Packit Service a47777
    """
Packit Service a47777
    Load ASCII data stored in a file and return it as a single array.
Packit Service a47777
Packit Service a47777
    Parameters
Packit Service a47777
    ----------
Packit Service a47777
    fname, kwargs : For a description of input parameters, see `genfromtxt`.
Packit Service a47777
Packit Service a47777
    See Also
Packit Service a47777
    --------
Packit Service a47777
    numpy.genfromtxt : generic function.
Packit Service a47777
Packit Service a47777
    """
Packit Service a47777
    kwargs['usemask'] = False
Packit Service a47777
    return genfromtxt(fname, **kwargs)
Packit Service a47777
Packit Service a47777
Packit Service a47777
def mafromtxt(fname, **kwargs):
Packit Service a47777
    """
Packit Service a47777
    Load ASCII data stored in a text file and return a masked array.
Packit Service a47777
Packit Service a47777
    Parameters
Packit Service a47777
    ----------
Packit Service a47777
    fname, kwargs : For a description of input parameters, see `genfromtxt`.
Packit Service a47777
Packit Service a47777
    See Also
Packit Service a47777
    --------
Packit Service a47777
    numpy.genfromtxt : generic function to load ASCII data.
Packit Service a47777
Packit Service a47777
    """
Packit Service a47777
    kwargs['usemask'] = True
Packit Service a47777
    return genfromtxt(fname, **kwargs)
Packit Service a47777
Packit Service a47777
Packit Service a47777
def recfromtxt(fname, **kwargs):
Packit Service a47777
    """
Packit Service a47777
    Load ASCII data from a file and return it in a record array.
Packit Service a47777
Packit Service a47777
    If ``usemask=False`` a standard `recarray` is returned,
Packit Service a47777
    if ``usemask=True`` a MaskedRecords array is returned.
Packit Service a47777
Packit Service a47777
    Parameters
Packit Service a47777
    ----------
Packit Service a47777
    fname, kwargs : For a description of input parameters, see `genfromtxt`.
Packit Service a47777
Packit Service a47777
    See Also
Packit Service a47777
    --------
Packit Service a47777
    numpy.genfromtxt : generic function
Packit Service a47777
Packit Service a47777
    Notes
Packit Service a47777
    -----
Packit Service a47777
    By default, `dtype` is None, which means that the data-type of the output
Packit Service a47777
    array will be determined from the data.
Packit Service a47777
Packit Service a47777
    """
Packit Service a47777
    kwargs.setdefault("dtype", None)
Packit Service a47777
    usemask = kwargs.get('usemask', False)
Packit Service a47777
    output = genfromtxt(fname, **kwargs)
Packit Service a47777
    if usemask:
Packit Service a47777
        from numpy.ma.mrecords import MaskedRecords
Packit Service a47777
        output = output.view(MaskedRecords)
Packit Service a47777
    else:
Packit Service a47777
        output = output.view(np.recarray)
Packit Service a47777
    return output
Packit Service a47777
Packit Service a47777
Packit Service a47777
def recfromcsv(fname, **kwargs):
Packit Service a47777
    """
Packit Service a47777
    Load ASCII data stored in a comma-separated file.
Packit Service a47777
Packit Service a47777
    The returned array is a record array (if ``usemask=False``, see
Packit Service a47777
    `recarray`) or a masked record array (if ``usemask=True``,
Packit Service a47777
    see `ma.mrecords.MaskedRecords`).
Packit Service a47777
Packit Service a47777
    Parameters
Packit Service a47777
    ----------
Packit Service a47777
    fname, kwargs : For a description of input parameters, see `genfromtxt`.
Packit Service a47777
Packit Service a47777
    See Also
Packit Service a47777
    --------
Packit Service a47777
    numpy.genfromtxt : generic function to load ASCII data.
Packit Service a47777
Packit Service a47777
    Notes
Packit Service a47777
    -----
Packit Service a47777
    By default, `dtype` is None, which means that the data-type of the output
Packit Service a47777
    array will be determined from the data.
Packit Service a47777
Packit Service a47777
    """
Packit Service a47777
    # Set default kwargs for genfromtxt as relevant to csv import.
Packit Service a47777
    kwargs.setdefault("case_sensitive", "lower")
Packit Service a47777
    kwargs.setdefault("names", True)
Packit Service a47777
    kwargs.setdefault("delimiter", ",")
Packit Service a47777
    kwargs.setdefault("dtype", None)
Packit Service a47777
    output = genfromtxt(fname, **kwargs)
Packit Service a47777
Packit Service a47777
    usemask = kwargs.get("usemask", False)
Packit Service a47777
    if usemask:
Packit Service a47777
        from numpy.ma.mrecords import MaskedRecords
Packit Service a47777
        output = output.view(MaskedRecords)
Packit Service a47777
    else:
Packit Service a47777
        output = output.view(np.recarray)
Packit Service a47777
    return output