Which indexing operations on numpy.memmap
arrays return an in-memory copy vs a view that is still backed by a file? The documentation doesn’t explain which indexing operations are “safe”.
np.memmap
objects have the same copy vs view behavior as other np.ndarray
, as documented here.
In particular, slices using slice objects return views, whereas “advanced indexing” using other arrays returns copies. One weird quirk is that if you use advanced indexing to assign contents, it does work.
import numpy as np
memmap = np.memmap(
"/tmp/test.npy", dtype="float32", mode="w+", shape=(10, 10)
)
# operations which create a view of the memmap
print(f"Regular slicing: {type(memmap[:5])}") # numpy.memmap
print(f"Slice with slice object: {type(memmap[np.s_[2::2]])}") # numpy.memmap
# operations which create an in-memory copy
print(f"Slice with np array: {type(memmap[np.arange(5)])}") # numpy.ndarray
print(f"Type when we slice with list: {type(memmap[[0]])}") # numpy.ndarray
# writing to a copy does not affect the original memmap
ndarray = memmap[np.arange(5)]
ndarray[:] = 1
assert np.allclose(memmap[:5], 0)
# quirk: writing using a list works if we don't bind to a new variable
memmap[[0, 1, 2, 3, 4]] = 1
assert np.allclose(memmap[:5], 1)