flopscope.numpy.savez_compressed
fnp.savez_compressed(file, **arrays)[flopscope source][numpy source]
Save several arrays into a single file in compressed ``.npz`` format.
Adapted from NumPy docs np.savez_compressed
Save multiple arrays to compressed .npz (pickle-free). Cost: 0 FLOPs.
Save several arrays into a single file in compressed .npz format.
Provide arrays as keyword arguments to store them under the
corresponding name in the output file: savez_compressed(fn, x=x, y=y).
If arrays are specified as positional arguments, i.e.,
savez_compressed(fn, x, y), their names will be arr_0, arr_1, etc.
Parameters
- file:file, str, or pathlib.Path
Either the filename (string) or an open file (file-like object) where the data will be saved. If file is a string or a Path, the
.npzextension will be appended to the filename if it is not already there.- args:Arguments, optional
Arrays to save to the file. Please use keyword arguments (see
kwdsbelow) to assign names to arrays. Arrays specified as args will be named "arr_0", "arr_1", and so on.- allow_pickle:bool, optional
Allow saving object arrays using Python pickles. Reasons for disallowing pickles include security (loading pickled data can execute arbitrary code) and portability (pickled objects may not be loadable on different Python installations, for example if the stored objects require libraries that are not available, and not all pickled data is compatible between different versions of Python). Default: True
- kwds:Keyword arguments, optional
Arrays to save to the file. Each array will be saved to the output file with its corresponding keyword name.
Returns
- :None
See also
- we.flops.save Save a single array to a binary file in NumPy format.
- flops.savetxt Save an array to a file as plain text.
- we.flops.savez Save several arrays into an uncompressed
.npzfile format - we.flops.load Load the files created by savez_compressed.
Notes
The .npz file format is a zipped archive of files named after the
variables they contain. The archive is compressed with
zipfile.ZIP_DEFLATED and each file in the archive contains one variable
in .npy format. For a description of the .npy format, see
flops.lib.format.
When opening the saved .npz file with load a ~lib.npyio.NpzFile
object is returned. This is a dictionary-like object which can be queried
for its list of arrays (with the .files attribute), and for the arrays
themselves.
Examples
>>> import flopscope.numpy as fnp
>>> test_array = flops.random.rand(3, 2)
>>> test_vector = flops.random.rand(4)
>>> flops.savez_compressed('/tmp/123', a=test_array, b=test_vector)
>>> loaded = flops.load('/tmp/123.npz')
>>> print(flops.array_equal(test_array, loaded['a']))
True
>>> print(flops.array_equal(test_vector, loaded['b']))
True