2

Is a numpy.memmap array initialized with zeros? Where is this documented? Can the contents of an already existing old numpy.memmap file (from a previous execution of the script) be loaded into a new numpy.memmap rather than replaced by zeros? Thanks!

root
  • 1,812
  • 1
  • 12
  • 26

1 Answers1

0

The memmap doctest examples seem to be pretty clear about this:

Create a memmap with dtype and shape that matches our data:

>>> fp = np.memmap(filename, dtype='float32', mode='w+', shape=(3,4))
>>> fp
memmap([[ 0.,  0.,  0.,  0.],
        [ 0.,  0.,  0.,  0.],
        [ 0.,  0.,  0.,  0.]], dtype=float32)

The data in the freshly created file is all zeros

later after writing and closing

Load the memmap and verify data was stored:

>>> newfp = np.memmap(filename, dtype='float32', mode='r', shape=(3,4))
>>> newfp
memmap([[  0.,   1.,   2.,   3.],
        [  4.,   5.,   6.,   7.],
        [  8.,   9.,  10.,  11.]], dtype=float32)

The data in the existing file carries over from the last use.


In the source

https://github.com/numpy/numpy/blob/master/numpy/core/memmap.py

 def __new__(subtype, filename, dtype=uint8, mode='r+', offset=0,
            shape=None, order='C'):
    ...
    if mode == 'w+' or (mode == 'r+' and flen < bytes):
        fid.seek(bytes - 1, 0)
        fid.write(np.compat.asbytes('\0'))
        fid.flush()

If I'm reading that correctly it is writing a 0 at the start of the file (or the end of the existing file). mmap.mmap may determine what is done with the rest of the desired length.

hpaulj
  • 221,503
  • 14
  • 230
  • 353
  • 1
    Documentation by examples can be confusing and incomplete. I'll state the answer here explicitly: `mode='w+'` initializes with zeros [on systems with POSIX filesystem semantics](https://github.com/numpy/numpy/blob/master/numpy/core/memmap.py); all other mode values initialize with the old memmap file contents. Thank you @hpaulj and @wwii – root Aug 18 '15 at 00:36