numpy.memmap[source]¶Create a memory-map to an array stored in abinary file on disk.
Memory-mapped files are used for accessing small segments of large fileson disk, without reading the entire file into memory. NumPy’smemmap’s are array-like objects. This differs from Python’smmapmodule, which uses file-like objects.
This subclass of ndarray has some unpleasant interactions withsome operations, because it doesn’t quite fit properly as a subclass.An alternative to using this subclass is to create themmapobject 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 functionwhich returns a view into an mmap buffer.
Delete the memmap instance to close the memmap file.
| Parameters: |
|
|---|
See also
lib.format.open_memmap.npy file.Notes
The memmap object can be used anywhere an ndarray is accepted.Given a memmapfp,isinstance(fp,numpy.ndarray) returnsTrue.
Memory-mapped files cannot be larger than 2GB on 32-bit systems.
When a memmap causes a file to be created or extended beyond itscurrent size in the filesystem, the contents of the new part areunspecified. On systems with POSIX filesystem semantics, the extendedpart will be filled with zero bytes.
Examples
>>>data=np.arange(12,dtype='float32')>>>data.resize((3,4))
This example uses a temporary file so that doctest doesn’t writefiles to your directory. You would use a ‘normal’ filename.
>>>fromtempfileimportmkdtemp>>>importos.pathaspath>>>filename=path.join(mkdtemp(),'newfile.dat')
Create a memmap with dtype and shape that matches our data:
>>>fp=np.memmap(filename,dtype='float32',mode='w+',shape=(3,4))>>>fpmemmap([[ 0., 0., 0., 0.], [ 0., 0., 0., 0.], [ 0., 0., 0., 0.]], dtype=float32)
Write data to memmap array:
>>>fp[:]=data[:]>>>fpmemmap([[ 0., 1., 2., 3.], [ 4., 5., 6., 7.], [ 8., 9., 10., 11.]], dtype=float32)
>>>fp.filename==path.abspath(filename)True
Deletion flushes memory changes to disk before removing the object:
>>>delfp
Load the memmap and verify data was stored:
>>>newfp=np.memmap(filename,dtype='float32',mode='r',shape=(3,4))>>>newfpmemmap([[ 0., 1., 2., 3.], [ 4., 5., 6., 7.], [ 8., 9., 10., 11.]], dtype=float32)
Read-only memmap:
>>>fpr=np.memmap(filename,dtype='float32',mode='r',shape=(3,4))>>>fpr.flags.writeableFalse
Copy-on-write memmap:
>>>fpc=np.memmap(filename,dtype='float32',mode='c',shape=(3,4))>>>fpc.flags.writeableTrue
It’s possible to assign to copy-on-write array, but values are onlywritten into the memory copy of the array, and not written to disk:
>>>fpcmemmap([[ 0., 1., 2., 3.], [ 4., 5., 6., 7.], [ 8., 9., 10., 11.]], dtype=float32)>>>fpc[0,:]=0>>>fpcmemmap([[ 0., 0., 0., 0.], [ 4., 5., 6., 7.], [ 8., 9., 10., 11.]], dtype=float32)
File on disk is unchanged:
>>>fprmemmap([[ 0., 1., 2., 3.], [ 4., 5., 6., 7.], [ 8., 9., 10., 11.]], dtype=float32)
Offset into a memmap:
>>>fpo=np.memmap(filename,dtype='float32',mode='r',offset=16)>>>fpomemmap([ 4., 5., 6., 7., 8., 9., 10., 11.], dtype=float32)
| Attributes: |
|
|---|
Methods
flush() | Write any changes in the array to the file on disk. |