spdl.io.load_npy¶
- load_npy(data: bytes | bytearray, *, copy: bool = False) ndarray[tuple[int, ...], dtype[_ScalarType_co]][source]¶
Load NumPy NDArray from bytes.
This function loads NumPy NDArray from memory. It is equivalent to
numpy.load(io.BytesIO(data)), but it is more efficient.Note
This function does not support
objectdtype, and Fortran order.Example
>>> ref = np.arange(20) >>> buffer = BytesIO() >>> np.save(buffer, ref) >>> buffer.seek(0) >>> data = buffer.getvalue() >>> restore = spdl.io.load_npy(data) >>> assert np.array_equal(restore, ref)
- Parameters:
data – The data generated by
numpy.save()function. (Note that it is different fromnumpy.ndarray.tobytes(), which does not contain shape and dtype.)copy – Whether to copy data. Default: no copy.
- Returns:
The restored array data.
See also
numpy.lib.format: The detail of NPY serialization is found._read_array_header: The function called by
numpy.load()to parse the header._read_array: The function called by
numpy.load()when loading the data region of NPY file.
Note
There is a branch in _read_array function where the execution can call some faster implementation. Howver,
BytesIOdoes not meet the condition. (isfileobj()function returnsFalseforio.BytesIO. [source]) Even if the execution takes the fasternumpy.fromfile()path, it creates a new array.