|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from ...common import _aliases |
| 4 | +from ...common._helpers import _check_device |
| 5 | + |
| 6 | +from ..._internal import get_xp |
| 7 | + |
| 8 | +import numpy as np |
| 9 | +from numpy import ( |
| 10 | + # Constants |
| 11 | + e, |
| 12 | + inf, |
| 13 | + nan, |
| 14 | + pi, |
| 15 | + newaxis, |
| 16 | + # Dtypes |
| 17 | + bool_ as bool, |
| 18 | + float32, |
| 19 | + float64, |
| 20 | + int8, |
| 21 | + int16, |
| 22 | + int32, |
| 23 | + int64, |
| 24 | + uint8, |
| 25 | + uint16, |
| 26 | + uint32, |
| 27 | + uint64, |
| 28 | + complex64, |
| 29 | + complex128, |
| 30 | + iinfo, |
| 31 | + finfo, |
| 32 | + can_cast, |
| 33 | + result_type, |
| 34 | +) |
| 35 | + |
| 36 | +from typing import TYPE_CHECKING |
| 37 | +if TYPE_CHECKING: |
| 38 | + from typing import Optional, Union |
| 39 | + from ...common._typing import ndarray, Device, Dtype |
| 40 | + |
| 41 | +import dask.array as da |
| 42 | + |
| 43 | +isdtype = get_xp(np)(_aliases.isdtype) |
| 44 | +astype = _aliases.astype |
| 45 | + |
| 46 | +# Common aliases |
| 47 | + |
| 48 | +# This arange func is modified from the common one to |
| 49 | +# not pass stop/step as keyword arguments, which will cause |
| 50 | +# an error with dask |
| 51 | + |
| 52 | +# TODO: delete the xp stuff, it shouldn't be necessary |
| 53 | +def dask_arange( |
| 54 | + start: Union[int, float], |
| 55 | + /, |
| 56 | + stop: Optional[Union[int, float]] = None, |
| 57 | + step: Union[int, float] = 1, |
| 58 | + *, |
| 59 | + xp, |
| 60 | + dtype: Optional[Dtype] = None, |
| 61 | + device: Optional[Device] = None, |
| 62 | + **kwargs |
| 63 | +) -> ndarray: |
| 64 | + _check_device(xp, device) |
| 65 | + args = [start] |
| 66 | + if stop is not None: |
| 67 | + args.append(stop) |
| 68 | + else: |
| 69 | + # stop is None, so start is actually stop |
| 70 | + # prepend the default value for start which is 0 |
| 71 | + args.insert(0, 0) |
| 72 | + args.append(step) |
| 73 | + return xp.arange(*args, dtype=dtype, **kwargs) |
| 74 | + |
| 75 | +arange = get_xp(da)(dask_arange) |
| 76 | +eye = get_xp(da)(_aliases.eye) |
| 77 | + |
| 78 | +from functools import partial |
| 79 | +asarray = partial(_aliases._asarray, namespace='dask.array') |
| 80 | +asarray.__doc__ = _aliases._asarray.__doc__ |
| 81 | + |
| 82 | +linspace = get_xp(da)(_aliases.linspace) |
| 83 | +eye = get_xp(da)(_aliases.eye) |
| 84 | +UniqueAllResult = get_xp(da)(_aliases.UniqueAllResult) |
| 85 | +UniqueCountsResult = get_xp(da)(_aliases.UniqueCountsResult) |
| 86 | +UniqueInverseResult = get_xp(da)(_aliases.UniqueInverseResult) |
| 87 | +unique_all = get_xp(da)(_aliases.unique_all) |
| 88 | +unique_counts = get_xp(da)(_aliases.unique_counts) |
| 89 | +unique_inverse = get_xp(da)(_aliases.unique_inverse) |
| 90 | +unique_values = get_xp(da)(_aliases.unique_values) |
| 91 | +permute_dims = get_xp(da)(_aliases.permute_dims) |
| 92 | +std = get_xp(da)(_aliases.std) |
| 93 | +var = get_xp(da)(_aliases.var) |
| 94 | +empty = get_xp(da)(_aliases.empty) |
| 95 | +empty_like = get_xp(da)(_aliases.empty_like) |
| 96 | +full = get_xp(da)(_aliases.full) |
| 97 | +full_like = get_xp(da)(_aliases.full_like) |
| 98 | +ones = get_xp(da)(_aliases.ones) |
| 99 | +ones_like = get_xp(da)(_aliases.ones_like) |
| 100 | +zeros = get_xp(da)(_aliases.zeros) |
| 101 | +zeros_like = get_xp(da)(_aliases.zeros_like) |
| 102 | +reshape = get_xp(da)(_aliases.reshape) |
| 103 | +matrix_transpose = get_xp(da)(_aliases.matrix_transpose) |
| 104 | +vecdot = get_xp(da)(_aliases.vecdot) |
| 105 | + |
| 106 | +nonzero = get_xp(da)(_aliases.nonzero) |
| 107 | +sum = get_xp(np)(_aliases.sum) |
| 108 | +prod = get_xp(np)(_aliases.prod) |
| 109 | +ceil = get_xp(np)(_aliases.ceil) |
| 110 | +floor = get_xp(np)(_aliases.floor) |
| 111 | +trunc = get_xp(np)(_aliases.trunc) |
| 112 | +matmul = get_xp(np)(_aliases.matmul) |
| 113 | +tensordot = get_xp(np)(_aliases.tensordot) |
| 114 | + |
| 115 | +from dask.array import ( |
| 116 | + # Element wise aliases |
| 117 | + arccos as acos, |
| 118 | + arccosh as acosh, |
| 119 | + arcsin as asin, |
| 120 | + arcsinh as asinh, |
| 121 | + arctan as atan, |
| 122 | + arctan2 as atan2, |
| 123 | + arctanh as atanh, |
| 124 | + left_shift as bitwise_left_shift, |
| 125 | + right_shift as bitwise_right_shift, |
| 126 | + invert as bitwise_invert, |
| 127 | + power as pow, |
| 128 | + # Other |
| 129 | + concatenate as concat, |
| 130 | +) |
| 131 | + |
| 132 | +# exclude these from all since |
| 133 | +_da_unsupported = ['sort', 'argsort'] |
| 134 | + |
| 135 | +common_aliases = [alias for alias in _aliases.__all__ if alias not in _da_unsupported] |
| 136 | + |
| 137 | +__all__ = common_aliases + ['asarray', 'bool', 'acos', |
| 138 | + 'acosh', 'asin', 'asinh', 'atan', 'atan2', |
| 139 | + 'atanh', 'bitwise_left_shift', 'bitwise_invert', |
| 140 | + 'bitwise_right_shift', 'concat', 'pow', |
| 141 | + 'e', 'inf', 'nan', 'pi', 'newaxis', 'float32', 'float64', 'int8', |
| 142 | + 'int16', 'int32', 'int64', 'uint8', 'uint16', 'uint32', 'uint64', |
| 143 | + 'complex64', 'complex128', 'iinfo', 'finfo', 'can_cast', 'result_type'] |
| 144 | + |
| 145 | +del da, partial, common_aliases, _da_unsupported, |
0 commit comments