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

Function to_npy_stack

dask/array/core.py:6070–6119  ·  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

6068
6069
6070def to_npy_stack(dirname, x, axis=0):
6071 """Write dask array to a stack of .npy files
6072
6073 This partitions the dask.array along one axis and stores each block along
6074 that axis as a single .npy file in the specified directory
6075
6076 Examples
6077 --------
6078 >>> x = da.ones((5, 10, 10), chunks=(2, 4, 4)) # doctest: +SKIP
6079 >>> da.to_npy_stack('data/', x, axis=0) # doctest: +SKIP
6080
6081 The ``.npy`` files store numpy arrays for ``x[0:2], x[2:4], and x[4:5]``
6082 respectively, as is specified by the chunk size along the zeroth axis::
6083
6084 $ tree data/
6085 data/
6086 |-- 0.npy
6087 |-- 1.npy
6088 |-- 2.npy
6089 |-- info
6090
6091 The ``info`` file stores the dtype, chunks, and axis information of the array.
6092 You can load these stacks with the :func:`dask.array.from_npy_stack` function.
6093
6094 >>> y = da.from_npy_stack('data/') # doctest: +SKIP
6095
6096 See Also
6097 --------
6098 from_npy_stack
6099 """
6100
6101 chunks = tuple((c if i == axis else (sum(c),)) for i, c in enumerate(x.chunks))
6102 xx = x.rechunk(chunks)
6103
6104 if not os.path.exists(dirname):
6105 os.mkdir(dirname)
6106
6107 meta = {"chunks": chunks, "dtype": x.dtype, "axis": axis}
6108
6109 with open(os.path.join(dirname, "info"), "wb") as f:
6110 pickle.dump(meta, f)
6111
6112 name = f"to-npy-stack-{uuid.uuid1()}"
6113 dsk = {
6114 (name, i): (np.save, os.path.join(dirname, f"{i}.npy"), key)
6115 for i, key in enumerate(core.flatten(xx.__dask_keys__()))
6116 }
6117
6118 graph = HighLevelGraph.from_collections(name, dsk, dependencies=[xx])
6119 compute_as_if_collection(Array, graph, list(dsk))
6120
6121
6122def 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