|
1 |
| -import numpy as np |
2 |
| -from ndtypes import ndt |
3 |
| -import xnd |
4 |
| -import gumath.functions as fn |
5 |
| -import gumath as gu |
6 |
| -import uarray as ua |
7 |
| -from uarray import Dispatchable, wrap_single_convertor |
8 |
| -from unumpy import ufunc, ufunc_list, ndarray, dtype |
9 |
| -import unumpy |
10 |
| -import functools |
| 1 | +try: |
| 2 | + import numpy as np |
| 3 | + from ndtypes import ndt |
| 4 | + import xnd |
| 5 | + import gumath.functions as fn |
| 6 | + import gumath as gu |
| 7 | + import uarray as ua |
| 8 | + from uarray import Dispatchable, wrap_single_convertor |
| 9 | + from unumpy import ufunc, ufunc_list, ndarray, dtype |
| 10 | + import unumpy |
| 11 | + import functools |
11 | 12 |
|
12 |
| -from typing import Dict |
| 13 | + from typing import Dict |
13 | 14 |
|
14 |
| -_ufunc_mapping: Dict[ufunc, np.ufunc] = {} |
| 15 | + _ufunc_mapping: Dict[ufunc, np.ufunc] = {} |
15 | 16 |
|
16 |
| -__ua_domain__ = "numpy" |
| 17 | + __ua_domain__ = "numpy" |
17 | 18 |
|
| 19 | + _implementations: Dict = { |
| 20 | + unumpy.ufunc.__call__: gu.gufunc.__call__, |
| 21 | + unumpy.ufunc.reduce: gu.reduce, |
| 22 | + } |
18 | 23 |
|
19 |
| -_implementations: Dict = { |
20 |
| - unumpy.ufunc.__call__: gu.gufunc.__call__, |
21 |
| - unumpy.ufunc.reduce: gu.reduce, |
22 |
| -} |
| 24 | + def __ua_function__(method, args, kwargs): |
| 25 | + if method in _implementations: |
| 26 | + return _implementations[method](*args, **kwargs) |
23 | 27 |
|
| 28 | + return _generic(method, args, kwargs) |
24 | 29 |
|
25 |
| -def __ua_function__(method, args, kwargs): |
26 |
| - if method in _implementations: |
27 |
| - return _implementations[method](*args, **kwargs) |
| 30 | + @wrap_single_convertor |
| 31 | + def __ua_convert__(value, dispatch_type, coerce): |
| 32 | + if dispatch_type is ndarray: |
| 33 | + return convert(value, coerce=coerce) if value is not None else None |
28 | 34 |
|
29 |
| - return _generic(method, args, kwargs) |
| 35 | + if dispatch_type is ufunc and hasattr(fn, value.name): |
| 36 | + return getattr(fn, value.name) |
30 | 37 |
|
| 38 | + if dispatch_type is dtype: |
| 39 | + return ndt(str(value)) if value is not None else None |
31 | 40 |
|
32 |
| -@wrap_single_convertor |
33 |
| -def __ua_convert__(value, dispatch_type, coerce): |
34 |
| - if dispatch_type is ndarray: |
35 |
| - return convert(value, coerce=coerce) if value is not None else None |
36 |
| - |
37 |
| - if dispatch_type is ufunc and hasattr(fn, value.name): |
38 |
| - return getattr(fn, value.name) |
| 41 | + return NotImplemented |
39 | 42 |
|
40 |
| - if dispatch_type is dtype: |
41 |
| - return ndt(str(value)) if value is not None else None |
| 43 | + def replace_self(func): |
| 44 | + @functools.wraps(func) |
| 45 | + def inner(self, *args, **kwargs): |
| 46 | + if self not in _ufunc_mapping: |
| 47 | + return NotImplemented |
42 | 48 |
|
43 |
| - return NotImplemented |
| 49 | + return func(_ufunc_mapping[self], *args, **kwargs) |
44 | 50 |
|
| 51 | + return inner |
45 | 52 |
|
46 |
| -def replace_self(func): |
47 |
| - @functools.wraps(func) |
48 |
| - def inner(self, *args, **kwargs): |
49 |
| - if self not in _ufunc_mapping: |
| 53 | + def _generic(method, args, kwargs): |
| 54 | + try: |
| 55 | + import numpy as np |
| 56 | + import unumpy.numpy_backend as NumpyBackend |
| 57 | + except ImportError: |
50 | 58 | return NotImplemented
|
51 | 59 |
|
52 |
| - return func(_ufunc_mapping[self], *args, **kwargs) |
| 60 | + with ua.set_backend(NumpyBackend, coerce=True): |
| 61 | + try: |
| 62 | + out = method(*args, **kwargs) |
| 63 | + except TypeError: |
| 64 | + return NotImplemented |
53 | 65 |
|
54 |
| - return inner |
| 66 | + return convert_out(out, coerce=False) |
55 | 67 |
|
| 68 | + def convert_out(x, coerce): |
| 69 | + if isinstance(x, (tuple, list)): |
| 70 | + return type(x)(map(lambda x: convert_out(x, coerce=coerce), x)) |
56 | 71 |
|
57 |
| -def _generic(method, args, kwargs): |
58 |
| - try: |
59 |
| - import numpy as np |
60 |
| - import unumpy.numpy_backend as NumpyBackend |
61 |
| - except ImportError: |
62 |
| - return NotImplemented |
| 72 | + return convert(x, coerce=coerce) |
| 73 | + |
| 74 | + def convert(x, coerce): |
| 75 | + if isinstance(x, xnd.array): |
| 76 | + return x |
63 | 77 |
|
64 |
| - with ua.set_backend(NumpyBackend, coerce=True): |
65 | 78 | try:
|
66 |
| - out = method(*args, **kwargs) |
| 79 | + return xnd.array.from_buffer(memoryview(x)) |
67 | 80 | except TypeError:
|
68 |
| - return NotImplemented |
69 |
| - |
70 |
| - return convert_out(out, coerce=False) |
71 |
| - |
72 |
| - |
73 |
| -def convert_out(x, coerce): |
74 |
| - if isinstance(x, (tuple, list)): |
75 |
| - return type(x)(map(lambda x: convert_out(x, coerce=coerce), x)) |
76 |
| - |
77 |
| - return convert(x, coerce=coerce) |
78 |
| - |
| 81 | + pass |
79 | 82 |
|
80 |
| -def convert(x, coerce): |
81 |
| - if isinstance(x, xnd.array): |
82 |
| - return x |
| 83 | + if coerce: |
| 84 | + return xnd.array(x) |
83 | 85 |
|
84 |
| - try: |
85 |
| - return xnd.array.from_buffer(memoryview(x)) |
86 |
| - except TypeError: |
87 |
| - pass |
| 86 | + if isinstance(x, (int, float, bool)): |
| 87 | + return x |
88 | 88 |
|
89 |
| - if coerce: |
90 |
| - return xnd.array(x) |
| 89 | + raise ua.BackendNotImplementedError("Unsupported output received.") |
91 | 90 |
|
92 |
| - if isinstance(x, (int, float, bool)): |
93 |
| - return x |
94 | 91 |
|
95 |
| - raise ua.BackendNotImplementedError("Unsupported output received.") |
| 92 | +except ImportError: |
| 93 | + pass |
0 commit comments