A robot telemetry recorder can write a short binary header followed by densely packed samples. NumPy memmap lets a review tool read part of a large file as an array. The reader still needs the file format. An incorrect byte offset can pull header bytes into the first sample, and an incorrect byte order can produce plausible numbers in a correctly shaped array.

Start with a file whose bytes you control

This fixture uses four header bytes followed by two frames of two signed 16-bit samples. Python struct writes the payload with its little-endian prefix. The reader uses offset=4 to start just after the header and dtype="<i2" to interpret two-byte signed integers in little-endian order. The mode is read-only. The two channels are interleaved by frame, so each row represents one frame.

import struct

from pathlib import Path

from tempfile import TemporaryDirectory

import numpy as np

with TemporaryDirectory() as folder:

path = Path(folder) / "run.bin"

path.write_bytes(

b"LOG1" + struct.pack("<4h", 1, 256, -2, 1024)

)

data = np.memmap(

path, mode="r", offset=4, dtype="<i2", shape=(2, 2)

)

np.testing.assert_array_equal(data, [[1, 256], [-2, 1024]])

del data

With the expected byte order, the first frame is [1, 256] and the second is [-2, 1024]. Changing the reader to big-endian yields [256, 1] and [-257, 4]. The shape remains two by two. Including both positive and negative samples and values that differ across the two byte positions makes this a stronger test than an all-zero payload.

Validate the layout before mapping production files

For a fixed-size format, check the magic bytes, version, and expected file length from the header and payload dimensions. Keep the sample type, byte order, channel order, and record width in the format specification. If a format permits trailing checksums or record padding, account for those bytes explicitly. This simple two-dimensional mapping applies to frames whose sample bytes are contiguous. A header that changes length between versions needs a version-specific payload offset.

NumPy measures the memory-map offset in bytes and uses the supplied dtype to interpret the file contents. Python's struct reference describes how its format prefix chooses byte order and standard field sizes. Together these APIs let a short fixture verify both the producer's bytes and the reader's interpretation.

Sources

https://numpy.org/doc/stable/reference/generated/numpy.memmap.html

https://docs.python.org/3/library/struct.html