Reading¶
For reading, instantiate a mmnpz.NpzReader, also available under
the alias mmnpz.load().
- class mmnpz.NpzReader(fn, mmap_mode='r', *, cache=True, preload=False)[source]¶
A dictionary-like object with lazy-loading of numpy arrays in the given uncompressed .npz file. Upon construction, creates a memory map of the full .npz file, returning views for the arrays within on request. Serves as a drop-in replacement for
numpy.load().- Parameters:
fn (str or Path) – The zipped archive to open.
mmap_mode (str, optional) – Provided for compatibility with numpy. Only supports ‘r’, indicating that the file is loaded as a readonly memory map.
cache (bool, optional) – Whether to cache array objects in case they are requested again.
preload (bool, optional) – Whether to precreate all array objects upon opening. Enforces caching.
- Raises:
OSError – If the input file does not exist or cannot be read.
zipfile.BadZipFile – If the input file cannot be interpreted as a zip file.
Examples
Store data to disk, and load it again as a memory map:
>>> np.savez("/tmp/123.npz", a=np.array([[1, 2], [3, 4]])) >>> data = mmnpz.NpzReader("/tmp/123.npz") >>> data["a"] memmap([[1, 2], [3, 4]])
- files¶
List of all uncompressed files in the archive with a
.npyextension (listed without the extension). These are supported as dictionary keys.
- mmap¶
The memory map of the full .npz file.
- Type:
- load(name)[source]¶
Creates a view for the specified array, disregarding the cache.
- Parameters:
name (str) – File name in the archive to load, without its
.npyextension.- Returns:
A view into the global memory map with correct shape and dtype.
- Return type:
See also
__getitem__Returns a view for the specified array using the cache.
- get(k[, d]) D[k] if k in D, else d. d defaults to None.¶
- items() a set-like object providing a view on D's items¶
- keys() a set-like object providing a view on D's keys¶
- values() an object providing a view on D's values¶