NPYInputOutput#
- class pybrops.core.io.NPYInputOutput.NPYInputOutput[source]#
Bases:
objectAbstract class for defining
.npyfile input/output functionality.This abstract class defines two functions with the following purposes:
to_npy- write an object to a.npyfile.from_npy- load an object from a.npyfile.
Methods
Read an object from a
.npyfile(s).Write an object to a
.npyfile.- abstract classmethod from_npy(filename, fieldname, mmap_mode, allow_pickle, fix_imports, encoding, max_header_size, **kwargs)[source]#
Read an object from a
.npyfile(s).- Parameters:
filename (str, Sequence of str) –
.npyfile name(s) from which to load field(s).fieldname (str, Sequence of str) – Name(s) of field(s) within the object for which to load.
mmap_mode (str, None, default = None) – Options are
{None, "r+", "r", "w+", "c"}If notNone, then memory-map the file, using the given mode (seenumpy.memmapfor a detailed description of the modes). A memory-mapped array is kept on disk. However, it can be accessed and sliced like anyndarray. Memory mapping is especially useful for accessing small fragments of large files without reading the entire file into memory. Default:Noneallow_pickle (bool, default = False) – Allow loading pickled object arrays stored in
.npyfiles. Reasons for disallowing pickles include security, as loading pickled data can execute arbitrary code. If pickles are disallowed, loading object arrays will fail. Default:Falsefix_imports (bool, default = True) – Only useful when loading Python 2 generated pickled files on Python 3, which includes
.npy/.npzfiles containing object arrays. Iffix_importsisTrue, pickle will try to map the old Python 2 names to the new names used in Python 3. Default:Trueencoding (str, default = "ASCII") – What encoding to use when reading Python 2 strings. Only useful when loading Python 2 generated pickled files in Python 3, which includes
.npy/.npzfiles containing object arrays. Values other than"latin1","ASCII", and"bytes"are not allowed, as they can corrupt numerical data. Default:"ASCII"max_header_size (int, optional) – Maximum allowed size of the header. Large headers may not be safe to load securely and thus require explicitly passing a larger value. See
ast.literal_evalfor details. This option is ignored whenallow_pickleis passed. In that case the file is by definition trusted and the limit is unnecessary. Default:10000kwargs (dict) – Additional keyword arguments to use for dictating importing from a
.npyfile(s).
- Returns:
out – An object read from a
.npyfile(s).- Return type:
- abstract to_npy(filename, fieldname, allow_pickle, fix_imports, **kwargs)[source]#
Write an object to a
.npyfile.- Parameters:
filename (str, Sequence of str) –
.npyfile name(s) to which to write field(s).fieldname (str, Sequence of str) – Name(s) of field(s) within the object for which to save.
allow_pickle (bool, default = True) – Allow saving object arrays using Python pickles. Reasons for disallowing pickles include security (loading pickled data can execute arbitrary code) and portability (pickled objects may not be loadable on different Python installations, for example if the stored objects require libraries that are not available, and not all pickled data is compatible between Python 2 and Python 3). Default:
Truefix_imports (bool, default = True) – Only useful in forcing objects in object arrays on Python 3 to be pickled in a Python 2 compatible way. If fix_imports is
True, pickle will try to map the new Python 3 names to the old module names used in Python 2, so that the pickle data stream is readable with Python 2. Default:Truekwargs (dict) – Additional keyword arguments to use for dictating export to a
.npyfile.
- Return type:
None