MCPcopy Create free account
hub / github.com/dask/dask / to_npy_stack

Function to_npy_stack

dask/array/core.py:5870–5919  ·  view source on GitHub ↗

Write dask array to a stack of .npy files This partitions the dask.array along one axis and stores each block along that axis as a single .npy file in the specified directory Examples -------- >>> x = da.ones((5, 10, 10), chunks=(2, 4, 4)) # doctest: +SKIP >>> da.to_npy_st

(dirname, x, axis=0)

Source from the content-addressed store, hash-verified

5868
5869
5870def to_npy_stack(dirname, x, axis=0):
5871 """Write dask array to a stack of .npy files
5872
5873 This partitions the dask.array along one axis and stores each block along
5874 that axis as a single .npy file in the specified directory
5875
5876 Examples
5877 --------
5878 >>> x = da.ones((5, 10, 10), chunks=(2, 4, 4)) # doctest: +SKIP
5879 >>> da.to_npy_stack('data/', x, axis=0) # doctest: +SKIP
5880
5881 The ``.npy`` files store numpy arrays for ``x[0:2], x[2:4], and x[4:5]``
5882 respectively, as is specified by the chunk size along the zeroth axis::
5883
5884 $ tree data/
5885 data/
5886 |-- 0.npy
5887 |-- 1.npy
5888 |-- 2.npy
5889 |-- info
5890
5891 The ``info`` file stores the dtype, chunks, and axis information of the array.
5892 You can load these stacks with the :func:`dask.array.from_npy_stack` function.
5893
5894 >>> y = da.from_npy_stack('data/') # doctest: +SKIP
5895
5896 See Also
5897 --------
5898 from_npy_stack
5899 """
5900
5901 chunks = tuple((c if i == axis else (sum(c),)) for i, c in enumerate(x.chunks))
5902 xx = x.rechunk(chunks)
5903
5904 if not os.path.exists(dirname):
5905 os.mkdir(dirname)
5906
5907 meta = {"chunks": chunks, "dtype": x.dtype, "axis": axis}
5908
5909 with open(os.path.join(dirname, "info"), "wb") as f:
5910 pickle.dump(meta, f)
5911
5912 name = "to-npy-stack-" + str(uuid.uuid1())
5913 dsk = {
5914 (name, i): (np.save, os.path.join(dirname, "%d.npy" % i), key)
5915 for i, key in enumerate(core.flatten(xx.__dask_keys__()))
5916 }
5917
5918 graph = HighLevelGraph.from_collections(name, dsk, dependencies=[xx])
5919 compute_as_if_collection(Array, graph, list(dsk))
5920
5921
5922def from_npy_stack(dirname, mmap_mode="r"):

Callers

nothing calls this directly

Calls 7

sumFunction · 0.90
compute_as_if_collectionFunction · 0.90
flattenMethod · 0.80
from_collectionsMethod · 0.80
rechunkMethod · 0.45
joinMethod · 0.45
__dask_keys__Method · 0.45

Tested by

no test coverage detected