MCPcopy Create free account
hub / github.com/dask/dask / ufunc

Class ufunc

dask/array/ufunc.py:72–169  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

70
71
72class ufunc:
73 _forward_attrs = {
74 "nin",
75 "nargs",
76 "nout",
77 "ntypes",
78 "identity",
79 "signature",
80 "types",
81 }
82
83 def __init__(self, ufunc):
84 if not isinstance(ufunc, (np.ufunc, da_frompyfunc)):
85 raise TypeError(
86 "must be an instance of `ufunc` or "
87 f"`da_frompyfunc`, got `{type(ufunc).__name__}"
88 )
89 self._ufunc = ufunc
90 self.__name__ = ufunc.__name__
91 if isinstance(ufunc, np.ufunc):
92 derived_from(np)(self)
93
94 def __dask_tokenize__(self):
95 return self.__name__, normalize_token(self._ufunc)
96
97 def __getattr__(self, key):
98 if key in self._forward_attrs:
99 return getattr(self._ufunc, key)
100 raise AttributeError(f"{type(self).__name__!r} object has no attribute {key!r}")
101
102 def __dir__(self):
103 return list(self._forward_attrs.union(dir(type(self)), self.__dict__))
104
105 def __repr__(self):
106 return repr(self._ufunc)
107
108 def __call__(self, *args, **kwargs):
109 dsks = [arg for arg in args if hasattr(arg, "_elemwise")]
110 if len(dsks) > 0:
111 for dsk in dsks:
112 result = dsk._elemwise(self._ufunc, *args, **kwargs)
113 if type(result) != type(NotImplemented):
114 return result
115 raise TypeError(
116 f"Parameters of such types are not supported by {self.__name__}"
117 )
118 else:
119 return self._ufunc(*args, **kwargs)
120
121 @derived_from(np.ufunc)
122 def outer(self, A, B, **kwargs):
123 if self.nin != 2:
124 raise ValueError("outer product only supported for binary functions")
125 if "out" in kwargs:
126 raise ValueError("`out` kwarg not supported")
127
128 A_is_dask = is_dask_collection(A)
129 B_is_dask = is_dask_collection(B)

Callers 3

frompyfuncFunction · 0.70
ufunc.pyFile · 0.70
test_reducersFunction · 0.50

Calls

no outgoing calls

Tested by 1

test_reducersFunction · 0.40