NumPy .npy files and memory-mapped storage interoperability
0 reputation · 12 Feb 2022, 02:12 UTC
0 reputation · 12 Feb 2022, 02:12 UTC
NumPy provides the .npy binary format to persist array metadata and raw data. When dealing with datasets that exceed available system RAM, the mmap_mode parameter in np.load() allows the array to be mapped directly from disk, loading only the required segments into memory.
However, there is a design uncertainty regarding the behavior of memory-mapped views when the underlying filesystem undergoes modifications or when the file is accessed across different NumPy version headers. Ensuring data integrity during the restoration of these large-scale arrays requires a clear understanding of how the header versioning interacts with the memory-map interface.
mmap_mode?.npy file is modified by an external process during the session?34775 reputation · 12 Feb 2022, 07:55 UTC
When using np.load(filename, mmap_mode='r'), NumPy does not load the entire dataset into RAM. Instead, it reads the .npy header to determine the array's shape, dtype, and byte order, then creates a numpy.memmap object that maps the file's data segment directly into the process's virtual address space.
NumPy handles header versioning during the initial call to np.load(). The .npy format is designed for backward compatibility; newer versions of NumPy can typically read files created by older versions. If a file is opened in mmap_mode, the header is parsed fully before the memory map is established.
np.load() will typically raise a ValueError or OSError during the header parsing phase, before the memory map is created.The behavior of a memory-mapped array when the underlying file is modified by an external process depends on the Operating System's page cache and the mmap_mode used:
memmap leverages the OS kernel's page cache, changes made by another process to the file on disk are generally reflected in the memory-mapped view once the OS synchronizes the pages. However, this is not atomic..npy file while it is mapped, the existing NumPy array object will not update its shape or dtype attributes, leading to a misalignment between the array's metadata and the actual bytes on disk. This often results in corrupted data reads or segmentation faults.Bus Error (on Unix) or an access violation (on Windows).To verify the memory-map status of your array, check the .base attribute:
import numpy as np
# Load with memory mapping
data = np.load('large_dataset.npy', mmap_mode='r')
# Verify it is a memmap object and not a standard ndarray
print(type(data.base))
# Expected output: <class 'numpy.memmap'>
Diagnostic Detail Needed: Are you accessing these files across a network filesystem (e.g., NFS, SMB) or a local disk? Network filesystems often have different caching behaviors that can delay or prevent the visibility of external modifications in a memory-mapped view.
Use comments to ask for clarification. Post a solution as an answer.
34,775 reputation · 12 Feb 2022, 05:18 UTC
When a .npy file is loaded with mmap_mode, NumPy not only reads the shape and dtype but also the Fortran order flag stored in the header. The resulting numpy.memmap object presents the data in the file's native layout; if the flag indicates Fortran (column‑major) ordering, the array will be Fortran‑contiguous even though the underlying bytes are still mapped directly from disk. Consequently, algorithms that assume C‑contiguity may see unexpected stride patterns, and calling .copy() or .astype(order='C') is needed to obtain a contiguous view without altering the on‑disk representation.