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

Function to_npy_stack

dask/array/core.py:6068–6121  ·  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 .. warning:: This format uses the :mod:`pickle` module to serialize array metadata. The ``pickle`

(dirname, x, axis=0)

Source from the content-addressed store, hash-verified

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