| 1 |
""" |
| 2 |
pint.facets.dask |
| 3 |
~~~~~~~~~~~~~~~~ |
| 4 |
|
| 5 |
Adds pint the capability to interoperate with Dask |
| 6 |
|
| 7 |
:copyright: 2022 by Pint Authors, see AUTHORS for more details. |
| 8 |
:license: BSD, see LICENSE for more details. |
| 9 |
""" |
| 10 |
|
| 11 |
from __future__ import annotations |
| 12 |
|
| 13 |
import functools |
| 14 |
from typing import Generic |
| 15 |
|
| 16 |
from ...compat import TypeAlias, compute, dask_array, persist, visualize |
| 17 |
from ..plain import ( |
| 18 |
GenericPlainRegistry, |
| 19 |
MagnitudeT, |
| 20 |
PlainQuantity, |
| 21 |
PlainUnit, |
| 22 |
QuantityT, |
| 23 |
UnitT, |
| 24 |
) |
| 25 |
|
| 26 |
|
| 27 |
def check_dask_array(f): |
| 28 |
@functools.wraps(f) |
| 29 |
def wrapper(self, *args, **kwargs): |
| 30 |
if isinstance(self._magnitude, dask_array.Array): |
| 31 |
return f(self, *args, **kwargs) |
| 32 |
else: |
| 33 |
msg = "Method {} only implemented for objects of {}, not {}".format( |
| 34 |
f.__name__, dask_array.Array, self._magnitude.__class__ |
| 35 |
) |
| 36 |
raise AttributeError(msg) |
| 37 |
|
| 38 |
return wrapper |
| 39 |
|
| 40 |
|
| 41 |
class DaskQuantity(Generic[MagnitudeT], PlainQuantity[MagnitudeT]): |
| 42 |
# Dask.array.Array ducking |
| 43 |
def __dask_graph__(self): |
| 44 |
if isinstance(self._magnitude, dask_array.Array): |
| 45 |
return self._magnitude.__dask_graph__() |
| 46 |
|
| 47 |
return None |
| 48 |
|
| 49 |
def __dask_keys__(self): |
| 50 |
return self._magnitude.__dask_keys__() |
| 51 |
|
| 52 |
def __dask_tokenize__(self): |
| 53 |
from dask.base import tokenize |
| 54 |
|
| 55 |
return (type(self), tokenize(self._magnitude), self.units) |
| 56 |
|
| 57 |
@property |
| 58 |
def __dask_optimize__(self): |
| 59 |
return dask_array.Array.__dask_optimize__ |
| 60 |
|
| 61 |
@property |
| 62 |
def __dask_scheduler__(self): |
| 63 |
return dask_array.Array.__dask_scheduler__ |
| 64 |
|
| 65 |
def __dask_postcompute__(self): |
| 66 |
func, args = self._magnitude.__dask_postcompute__() |
| 67 |
return self._dask_finalize, (func, args, self.units) |
| 68 |
|
| 69 |
def __dask_postpersist__(self): |
| 70 |
func, args = self._magnitude.__dask_postpersist__() |
| 71 |
return self._dask_finalize, (func, args, self.units) |
| 72 |
|
| 73 |
def _dask_finalize(self, results, func, args, units): |
| 74 |
values = func(results, *args) |
| 75 |
return type(self)(values, units) |
| 76 |
|
| 77 |
@check_dask_array |
| 78 |
def compute(self, **kwargs): |
| 79 |
"""Compute the Dask array wrapped by pint.PlainQuantity. |
| 80 |
|
| 81 |
Parameters |
| 82 |
---------- |
| 83 |
**kwargs : dict |
| 84 |
Any keyword arguments to pass to ``dask.compute``. |
| 85 |
|
| 86 |
Returns |
| 87 |
------- |
| 88 |
pint.PlainQuantity |
| 89 |
A pint.PlainQuantity wrapped numpy array. |
| 90 |
""" |
| 91 |
(result,) = compute(self, **kwargs) |
| 92 |
return result |
| 93 |
|
| 94 |
@check_dask_array |
| 95 |
def persist(self, **kwargs): |
| 96 |
"""Persist the Dask Array wrapped by pint.PlainQuantity. |
| 97 |
|
| 98 |
Parameters |
| 99 |
---------- |
| 100 |
**kwargs : dict |
| 101 |
Any keyword arguments to pass to ``dask.persist``. |
| 102 |
|
| 103 |
Returns |
| 104 |
------- |
| 105 |
pint.PlainQuantity |
| 106 |
A pint.PlainQuantity wrapped Dask array. |
| 107 |
""" |
| 108 |
(result,) = persist(self, **kwargs) |
| 109 |
return result |
| 110 |
|
| 111 |
@check_dask_array |
| 112 |
def visualize(self, **kwargs): |
| 113 |
"""Produce a visual representation of the Dask graph. |
| 114 |
|
| 115 |
The graphviz library is required. |
| 116 |
|
| 117 |
Parameters |
| 118 |
---------- |
| 119 |
**kwargs : dict |
| 120 |
Any keyword arguments to pass to ``dask.visualize``. |
| 121 |
|
| 122 |
Returns |
| 123 |
------- |
| 124 |
|
| 125 |
""" |
| 126 |
visualize(self, **kwargs) |
| 127 |
|
| 128 |
|
| 129 |
class DaskUnit(PlainUnit): |
| 130 |
pass |
| 131 |
|
| 132 |
|
| 133 |
class GenericDaskRegistry( |
| 134 |
Generic[QuantityT, UnitT], GenericPlainRegistry[QuantityT, UnitT] |
| 135 |
): |
| 136 |
pass |
| 137 |
|
| 138 |
|
| 139 |
class DaskRegistry(GenericDaskRegistry[DaskQuantity, DaskUnit]): |
| 140 |
Quantity: TypeAlias = DaskQuantity |
| 141 |
Unit: TypeAlias = DaskUnit |