dask.array.register_chunk_type
dask.array.register_chunk_type¶
- dask.array.register_chunk_type(type)[source]¶
Register the given type as a valid chunk and downcast array type
- Parameters
- typetype
Duck array type to be registered as a type Dask can safely wrap as a chunk and to which Dask does not defer in arithmetic operations and NumPy functions/ufuncs.
Notes
A
dask.array.Array
can contain any sufficiently “NumPy-like” array in its chunks. These are also referred to as “duck arrays” since they match the most important parts of NumPy’s array API, and so, behave the same way when relying on duck typing.However, for multiple duck array types to interoperate properly, they need to properly defer to each other in arithmetic operations and NumPy functions/ufuncs according to a well-defined type casting hierarchy ( see NEP 13 ). In an effort to maintain this hierarchy, Dask defers to all other duck array types except those in its internal registry. By default, this registry contains
cupy.ndarray
sparse.SparseArray
This function exists to append any other types to this registry. If a type is not in this registry, and yet is a downcast type (it comes below
dask.array.Array
in the type casting hierarchy), aTypeError
will be raised due to all operand types returningNotImplemented
.Examples
Using a mock
FlaggedArray
class as an example chunk type unknown to Dask with minimal duck array API:>>> import numpy.lib.mixins >>> class FlaggedArray(numpy.lib.mixins.NDArrayOperatorsMixin): ... def __init__(self, a, flag=False): ... self.a = a ... self.flag = flag ... def __repr__(self): ... return f"Flag: {self.flag}, Array: " + repr(self.a) ... def __array__(self): ... return np.asarray(self.a) ... def __array_ufunc__(self, ufunc, method, *inputs, **kwargs): ... if method == '__call__': ... downcast_inputs = [] ... flag = False ... for input in inputs: ... if isinstance(input, self.__class__): ... flag = flag or input.flag ... downcast_inputs.append(input.a) ... elif isinstance(input, np.ndarray): ... downcast_inputs.append(input) ... else: ... return NotImplemented ... return self.__class__(ufunc(*downcast_inputs, **kwargs), flag) ... else: ... return NotImplemented ... @property ... def shape(self): ... return self.a.shape ... @property ... def ndim(self): ... return self.a.ndim ... @property ... def dtype(self): ... return self.a.dtype ... def __getitem__(self, key): ... return type(self)(self.a[key], self.flag) ... def __setitem__(self, key, value): ... self.a[key] = value
Before registering
FlaggedArray
, both types will attempt to defer to the other:>>> import dask.array as da >>> da.ones(5) - FlaggedArray(np.ones(5), True) Traceback (most recent call last): ... TypeError: operand type(s) all returned NotImplemented ...
However, once registered, Dask will be able to handle operations with this new type:
>>> da.register_chunk_type(FlaggedArray) >>> x = da.ones(5) - FlaggedArray(np.ones(5), True) >>> x dask.array<sub, shape=(5,), dtype=float64, chunksize=(5,), chunktype=dask.FlaggedArray> >>> x.compute() Flag: True, Array: array([0., 0., 0., 0., 0.])