API reference¶
Package for writing continuously NpyFiles.
This package contains an implementation to write continuously to a .npy
file. It is useful if single slices of an array are available sequentially,
i.e. from a live camera that delivers images over time, or do not fit into the
memory.
The Numpy file format is a simple binary format with a header followed by the actual data.
-
class
npyfile.NpyFile(filename)¶ A file-like object to continuously write data slices into a
.npyNumPy file.Warning
The
shape,dtype& memory view should not change for a file.Examples
TemporaryDirectory for demonstration purpose. You should use a real file:
>>> import pathlib >>> import tempfile >>> tmp_dir = tempfile.TemporaryDirectory() >>> outfile = pathlib.Path(tmp_dir.name) / 'images.npy'
Generate stack of 10 random images (could also arrive from a live cam):
>>> images = np.random.randint(low=0, high=255, size=(10,640,480,3))
Write the data image by image to the file:
>>> with NpyFile(outfile) as file: ... for img in images: ... file.write(img)
Show equivalence between the original & stored data.
>>> np.testing.assert_array_equal(images, np.load(outfile))
Clean-up TemporaryDirectory.
>>> tmp_dir.cleanup()
References
-
close()¶ Close the file.
- Return type
None
-
write(data)¶ Write a data slice into the file.
- Parameters
data (
ndarray) – Slice to write.- Return type
None
-