-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathnumpy.py
More file actions
161 lines (147 loc) · 4.06 KB
/
Copy pathnumpy.py
File metadata and controls
161 lines (147 loc) · 4.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
"""Create UDFs of numpy functions supported by numba.
See list of numpy ufuncs supported by numpy here:
https://numba.readthedocs.io/en/stable/reference/numpysupported.html#math-operations
"""
import numpy as _np
from .. import _STANDARD_OPERATOR_NAMES
from .. import config as _config
from .. import unary as _unary
from ..core import _supports_udfs
from ..dtypes import _supports_complex
_delayed = {}
_unary_names = {
# Math operations
"negative",
"abs",
"absolute",
"cbrt",
"fabs",
"rint",
"sign",
"exp",
"exp2",
"log",
"log2",
"log10",
"expm1",
"log1p",
"positive",
"sqrt",
"square",
"reciprocal",
# Trigonometric functions
"sin",
"cos",
"tan",
"arcsin",
"arccos",
"arctan",
"sinh",
"cosh",
"tanh",
"arcsinh",
"arccosh",
"arctanh",
"deg2rad",
"rad2deg",
"degrees",
"radians",
# Bit-twiddling functions
"bitwise_not",
"invert",
# Comparison functions
"logical_not",
# Floating functions
"isfinite",
"isinf",
"isnan",
"signbit",
"floor",
"ceil",
"trunc",
"spacing",
# Datetime functions
# "nat", # We need to see if our UDTs support datetime dtypes!
}
_numpy_to_graphblas = {
"abs": "abs",
"absolute": "abs",
"arccos": "acos",
"arccosh": "acosh",
"arcsin": "asin",
"arcsinh": "asinh",
"arctan": "atan",
"arctanh": "atanh",
"bitwise_not": "bnot",
"cbrt": "cbrt",
"ceil": "ceil",
"cos": "cos",
"cosh": "cosh",
"exp": "exp",
"exp2": "exp2",
"expm1": "expm1",
# 'fabs': 'abs' # should we rely on coercion? fabs is only float
"floor": "floor",
"invert": "bnot",
"isfinite": "isfinite",
"isinf": "isinf",
"isnan": "isnan",
"log": "log",
"log10": "log10",
"log1p": "log1p",
"log2": "log2",
"logical_not": "lnot", # should we? result_type not the same
"negative": "ainv",
"positive": "identity", # positive is supposed to check dtype, but doesn't in numba
# "reciprocal": "minv", # has differences. We should investigate further.
"rint": "round",
# 'sign': 'signum' # signum is float-only
"sin": "sin",
"sinh": "sinh",
"sqrt": "sqrt",
"tan": "tan",
"tanh": "tanh",
"trunc": "trunc",
}
# Not included: deg2rad degrees rad2deg radians signbit spacing square
if _supports_complex:
_unary_names.update({"conj", "conjugate"})
_numpy_to_graphblas["conj"] = "conj"
_numpy_to_graphblas["conjugate"] = "conj"
# _graphblas_to_numpy = {val: key for key, val in _numpy_to_graphblas.items()} # Soon...
_STANDARD_OPERATOR_NAMES.update(f"unary.numpy.{name}" for name in _unary_names)
__all__ = list(_unary_names)
def __dir__():
if not _supports_udfs and not _config.get("mapnumpy"):
return globals().keys() # FLAKY COVERAGE
attrs = _delayed.keys() | _unary_names
if not _supports_udfs:
attrs &= _numpy_to_graphblas.keys()
return attrs | globals().keys()
def __getattr__(name):
if name in _delayed:
delayed_func, kwargs = _delayed.pop(name)
rv = delayed_func(**kwargs)
globals()[name] = rv
return rv
if name not in _unary_names:
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
if _config.get("mapnumpy") and name in _numpy_to_graphblas:
globals()[name] = getattr(_unary, _numpy_to_graphblas[name])
elif not _supports_udfs:
raise AttributeError(
f"module {__name__!r} unable to compile UDF for {name!r}; "
"install numba for UDF support"
)
else:
numpy_func = getattr(_np, name)
def func(x): # pragma: no cover (numba)
return numpy_func(x)
_unary.register_new(f"numpy.{name}", func)
if name == "reciprocal":
# numba doesn't match numpy here
def reciprocal(x): # pragma: no cover (numba)
return 1 if x else 0
op = _unary.register_anonymous(reciprocal)
globals()[name]._add(op["BOOL"])
return globals()[name]