unyt.dask_array module

a dask array class (unyt_dask_array) and helper functions for unyt.

class unyt.dask_array.unyt_dask_array(dask_graph, name, chunks, dtype=None, meta=None, shape=None, *, units=None, registry=None, bypass_validation=False, unyt_name=None)[source]

Bases: Array

a dask.array.core.Array subclass that tracks units. This class is only recommended for advanced usage, most cases should use the unyt_from_dask helper function to instantiate a new unyt_dask_array.

Parameters:
  • dask.array.core.Array (The following parameters are those for a standard)

  • dask (dict) – Task dependency graph

  • name (string) – Name of array in dask

  • shape (tuple of ints) – Shape of the entire array

  • chunks (iterable of tuples) – block sizes along each dimension

  • dtype (str or dtype) – Typecode or data-type for the new Dask Array

  • meta (empty ndarray) – empty ndarray created with same NumPy backend, ndim and dtype as the Dask Array being created (overrides dtype)

  • standard (The following keyword-only parameters are the same as for a)

  • definitions) (unyt.unyt_array (check there for)

  • units

  • registry

  • bypass_validation

  • unyt_name

to_dask()[source]

convert to a plain dask array

Return type:

dask.array object

Examples

>>> from unyt import dask_array
>>> import dask.array as da
>>> x = da.random.random((10000, 10000), chunks=(1000, 1000))
>>> x_da = dask_array.unyt_from_dask(x, 'm')
>>> x_da.to_dask()
...
dask.array<random_sample, shape=(10000, 10000), dtype=float64,
     chunksize=(1000, 1000), chunktype=numpy.ndarray>
prod(*args, **kwargs)[source]

Return the product of the array elements over the given axis

Refer to dask.array.prod() for full documentation.

See also

dask.array.prod

equivalent function

dask
unyt.dask_array.unyt_from_dask(dask_array, units=None, *, registry=None, bypass_validation=False, unyt_name=None)[source]

creates a unyt_dask_array from a standard dask array. This function will create a new copy of the dask graph associated with an array.

Parameters:
  • dask_array (a standard dask array)

  • units (String unit name, unit symbol object, or astropy unit) – The units of the array. Powers must be specified using python syntax (cm**3, not cm^3).

  • registry (unyt.unit_registry.UnitRegistry) – The registry to create units from. If units is already associated with a unit registry and this is specified, this will be used instead of the registry associated with the unit object.

  • bypass_validation (boolean) – If True, all input validation is skipped. Using this option may produce corrupted, invalid units or array data, but can lead to significant speedups in the input validation logic adds significant overhead. If set, units must be a valid unit object. Defaults to False.

  • unyt_name (string) – The name of the array. Defaults to None. This attribute does not propagate through mathematical operations, but is preserved under indexing and unit conversions.

Notes

All of the above parameters with the exception of the dask_array are the same as for a standard unyt.unyt_array.

Examples

>>> from unyt import dask_array, unyt_quantity
>>> import dask.array as da
>>> x = da.ones((1000, 1000), chunks=(100, 100))
>>> x_da = dask_array.unyt_from_dask(x, 'm')
>>> type(x_da)
<class 'unyt.dask_array.unyt_dask_array'>
>>> x_da.units
m
>>> x_da.mean().units
m
>>> x_da.mean().compute()
unyt_quantity(1., 'm')
>>> x_da.to('cm').mean().compute()
unyt_quantity(100., 'cm')
>>> (x_da.to('cm')/unyt_quantity(10, 's')).mean().compute()
unyt_quantity(10., 'cm/s')
unyt.dask_array.reduce_with_units(dask_func, unyt_dask_in, *args, **kwargs)[source]

Call a dask.array.reduction function and preserve units.

Parameters:
  • dask_func (function) – a function handle from dask.array.reduction (e.g., dask.array.min, dask.array.var, dask.array.nanstd) to call.

  • unyt_dask_in (unyt_dask_array) – the unyt dask array, first argument to dask_func

  • args – any arguments to the dask_func

  • kwargs – any keyword arguments to the dask_func

Returns:

the result of dask_func with units preserved

Return type:

unyt_dask_array

Examples

>>> import dask.array
>>> from unyt.dask_array import unyt_from_dask, reduce_with_units
>>> a = dask.array.ones((10000,), chunks=(100,))
>>> a = unyt_from_dask(a, 'm')
>>> b = reduce_with_units(dask.array.median, a, axis=0)
>>> b.compute()
unyt_quantity(1., 'm')