API¶
Dask APIs generally follow from upstream APIs:
- Arrays follows NumPy
- DataFrames follows Pandas
- Bag follows map/filter/groupby/reduce common in Spark and Python iterators
- Dask-ML follows the Scikit-Learn and others
- Delayed wraps general Python code
- Futures follows concurrent.futures from the standard library for real-time computation.
Additionally, Dask has its own functions to start computations, persist data in memory, check progress, and so forth that complement the APIs above. These more general Dask functions are described below:
compute (*args, **kwargs) |
Compute several dask collections at once. |
is_dask_collection (x) |
Returns True if x is a dask collection |
optimize (*args, **kwargs) |
Optimize several dask collections at once. |
persist (*args, **kwargs) |
Persist multiple Dask collections into memory |
visualize (*args, **kwargs) |
Visualize several dask graphs at once. |
These functions work with any scheduler. More advanced operations are
available when using the newer scheduler and starting a
dask.distributed.Client
(which, despite its name, runs nicely on a
single machine). This API provides the ability to submit, cancel, and track
work asynchronously, and includes many functions for complex inter-task
workflows. These are not necessary for normal operation, but can be useful for
real-time or advanced operation.
This more advanced API is available in the Dask distributed documentation
-
dask.
compute
(*args, **kwargs)¶ Compute several dask collections at once.
Parameters: args : object
Any number of objects. If it is a dask object, it’s computed and the result is returned. By default, python builtin collections are also traversed to look for dask objects (for more information see the
traverse
keyword). Non-dask arguments are passed through unchanged.traverse : bool, optional
By default dask traverses builtin python collections looking for dask objects passed to
compute
. For large collections this can be expensive. If none of the arguments contain any dask objects, settraverse=False
to avoid doing this traversal.scheduler : string, optional
Which scheduler to use like “threads”, “synchronous” or “processes”. If not provided, the default is to check the global settings first, and then fall back to the collection defaults.
optimize_graph : bool, optional
If True [default], the optimizations for each collection are applied before computation. Otherwise the graph is run as is. This can be useful for debugging.
kwargs
Extra keywords to forward to the scheduler function.
Examples
>>> import dask.array as da >>> a = da.arange(10, chunks=2).sum() >>> b = da.arange(10, chunks=2).mean() >>> compute(a, b) (45, 4.5)
By default, dask objects inside python collections will also be computed:
>>> compute({'a': a, 'b': b, 'c': 1}) # doctest: +SKIP ({'a': 45, 'b': 4.5, 'c': 1},)
-
dask.
is_dask_collection
(x)¶ Returns
True
ifx
is a dask collection
-
dask.
optimize
(*args, **kwargs)¶ Optimize several dask collections at once.
Returns equivalent dask collections that all share the same merged and optimized underlying graph. This can be useful if converting multiple collections to delayed objects, or to manually apply the optimizations at strategic points.
Note that in most cases you shouldn’t need to call this method directly.
Parameters: *args : objects
Any number of objects. If a dask object, its graph is optimized and merged with all those of all other dask objects before returning an equivalent dask collection. Non-dask arguments are passed through unchanged.
traverse : bool, optional
By default dask traverses builtin python collections looking for dask objects passed to
optimize
. For large collections this can be expensive. If none of the arguments contain any dask objects, settraverse=False
to avoid doing this traversal.optimizations : list of callables, optional
Additional optimization passes to perform.
**kwargs
Extra keyword arguments to forward to the optimization passes.
Examples
>>> import dask.array as da >>> a = da.arange(10, chunks=2).sum() >>> b = da.arange(10, chunks=2).mean() >>> a2, b2 = optimize(a, b)
>>> a2.compute() == a.compute() True >>> b2.compute() == b.compute() True
-
dask.
persist
(*args, **kwargs)¶ Persist multiple Dask collections into memory
This turns lazy Dask collections into Dask collections with the same metadata, but now with their results fully computed or actively computing in the background.
For example a lazy dask.array built up from many lazy calls will now be a dask.array of the same shape, dtype, chunks, etc., but now with all of those previously lazy tasks either computed in memory as many small
numpy.array
(in the single-machine case) or asynchronously running in the background on a cluster (in the distributed case).This function operates differently if a
dask.distributed.Client
exists and is connected to a distributed scheduler. In this case this function will return as soon as the task graph has been submitted to the cluster, but before the computations have completed. Computations will continue asynchronously in the background. When using this function with the single machine scheduler it blocks until the computations have finished.When using Dask on a single machine you should ensure that the dataset fits entirely within memory.
Parameters: *args: Dask collections
scheduler : string, optional
Which scheduler to use like “threads”, “synchronous” or “processes”. If not provided, the default is to check the global settings first, and then fall back to the collection defaults.
traverse : bool, optional
By default dask traverses builtin python collections looking for dask objects passed to
persist
. For large collections this can be expensive. If none of the arguments contain any dask objects, settraverse=False
to avoid doing this traversal.optimize_graph : bool, optional
If True [default], the graph is optimized before computation. Otherwise the graph is run as is. This can be useful for debugging.
**kwargs
Extra keywords to forward to the scheduler function.
Returns: New dask collections backed by in-memory data
Examples
>>> df = dd.read_csv('/path/to/*.csv') # doctest: +SKIP >>> df = df[df.name == 'Alice'] # doctest: +SKIP >>> df['in-debt'] = df.balance < 0 # doctest: +SKIP >>> df = df.persist() # triggers computation # doctest: +SKIP
>>> df.value().min() # future computations are now fast # doctest: +SKIP -10 >>> df.value().max() # doctest: +SKIP 100
>>> from dask import persist # use persist function on multiple collections >>> a, b = persist(a, b) # doctest: +SKIP
-
dask.
visualize
(*args, **kwargs)¶ Visualize several dask graphs at once.
Requires
graphviz
to be installed. All options that are not the dask graph(s) should be passed as keyword arguments.Parameters: dsk : dict(s) or collection(s)
The dask graph(s) to visualize.
filename : str or None, optional
The name (without an extension) of the file to write to disk. If filename is None, no file will be written, and we communicate with dot using only pipes.
format : {‘png’, ‘pdf’, ‘dot’, ‘svg’, ‘jpeg’, ‘jpg’}, optional
Format in which to write output file. Default is ‘png’.
optimize_graph : bool, optional
If True, the graph is optimized before rendering. Otherwise, the graph is displayed as is. Default is False.
color: {None, ‘order’}, optional
Options to color nodes. Provide
cmap=
keyword for additional colormap**kwargs
Additional keyword arguments to forward to
to_graphviz
.Returns: result : IPython.diplay.Image, IPython.display.SVG, or None
See dask.dot.dot_graph for more information.
See also
dask.dot.dot_graph
Notes
For more information on optimization see here:
https://docs.dask.org/en/latest/optimize.html
Examples
>>> x.visualize(filename='dask.pdf') # doctest: +SKIP >>> x.visualize(filename='dask.pdf', color='order') # doctest: +SKIP
Finally, Dask has a few helpers for generating demo datasets