bundles / numpy 2.4.3 / numpy / memmap
class
numpy:memmap
source: /numpy/__init__.py
Signature
class memmap ( filename , dtype = <class 'numpy.uint8'> , mode = r+ , offset = 0 , shape = None , order = C ) Summary
Create a memory-map to an array stored in a binary file on disk.
Extended Summary
Memory-mapped files are used for accessing small segments of large files on disk, without reading the entire file into memory. NumPy's memmap's are array-like objects. This differs from Python's mmap module, which uses file-like objects.
This subclass of ndarray has some unpleasant interactions with some operations, because it doesn't quite fit properly as a subclass. An alternative to using this subclass is to create the mmap object yourself, then create an ndarray with ndarray.__new__ directly, passing the object created in its 'buffer=' parameter.
This class may at some point be turned into a factory function which returns a view into an mmap buffer.
Flush the memmap instance to write the changes to the file. Currently there is no API to close the underlying mmap. It is tricky to ensure the resource is actually closed, since it may be shared between different memmap instances.
Parameters
filename: str, file-like object, or pathlib.Path instanceThe file name or file object to be used as the array data buffer.
dtype: data-type, optionalThe data-type used to interpret the file contents. Default is
uint8.mode: {'r+', 'r', 'w+', 'c'}, optionalThe file is opened in this mode:
+------+-------------------------------------------------------------+ | 'r' | Open existing file for reading only. | +------+-------------------------------------------------------------+ | 'r+' | Open existing file for reading and writing. | +------+-------------------------------------------------------------+ | 'w+' | Create or overwrite existing file for reading and writing. | | | If ``mode == 'w+'`` then `shape` must also be specified. | +------+-------------------------------------------------------------+ | 'c' | Copy-on-write: assignments affect data in memory, but | | | changes are not saved to disk. The file on disk is | | | read-only. | +------+-------------------------------------------------------------+
Default is 'r+'.
offset: int, optionalIn the file, array data starts at this offset. Since
offsetis measured in bytes, it should normally be a multiple of the byte-size ofdtype. Whenmode != 'r', even positive offsets beyond end of file are valid; The file will be extended to accommodate the additional data. By default,memmapwill start at the beginning of the file, even iffilenameis a file pointerfpandfp.tell() != 0.shape: int or sequence of ints, optionalThe desired shape of the array. If
mode == 'r'and the number of remaining bytes afteroffsetis not a multiple of the byte-size ofdtype, you must specifyshape. By default, the returned array will be 1-D with the number of elements determined by file size and data-type.order: {'C', 'F'}, optionalSpecify the order of the ndarray memory layout:
row-major, C-style orcolumn-major, Fortran-style. This only has an effect if the shape is greater than 1-D. The default order is 'C'.
Attributes
filename: str or pathlib.Path instancePath to the mapped file.
offset: intOffset position in the file.
mode: strFile mode.
Methods
flushFlush any changes in memory to file on disk. When you delete a memmap object, flush is called first to write changes to disk.
Notes
The memmap object can be used anywhere an ndarray is accepted. Given a memmap fp, isinstance(fp, numpy.ndarray) returns True.
Memory-mapped files cannot be larger than 2GB on 32-bit systems.
When a memmap causes a file to be created or extended beyond its current size in the filesystem, the contents of the new part are unspecified. On systems with POSIX filesystem semantics, the extended part will be filled with zero bytes.
Examples
import numpy as np data = np.arange(12, dtype='float32') data.resize((3,4))✓
from tempfile import mkdtemp import os.path as path filename = path.join(mkdtemp(), 'newfile.dat')✓
fp = np.memmap(filename, dtype='float32', mode='w+', shape=(3,4)) fp✓
fp[:] = data[:]
✓fp
✗fp.filename == path.abspath(filename)
✓fp.flush()
✓newfp = np.memmap(filename, dtype='float32', mode='r', shape=(3,4))
✓newfp
✗fpr = np.memmap(filename, dtype='float32', mode='r', shape=(3,4)) fpr.flags.writeable✓
fpc = np.memmap(filename, dtype='float32', mode='c', shape=(3,4)) fpc.flags.writeable✓
fpc
✗fpc[0,:] = 0
✓fpc
✗fpr
✗fpo = np.memmap(filename, dtype='float32', mode='r', offset=16)
✓fpo
✗See also
- lib.format.open_memmap
Create or load a memory-mapped
.npyfile.
Aliases
-
numpy.memmap