forked from clj-python/libpython-clj
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnp_array.clj
More file actions
114 lines (98 loc) · 4.18 KB
/
Copy pathnp_array.clj
File metadata and controls
114 lines (98 loc) · 4.18 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
(ns libpython-clj.python.np-array
"Bindings for deeper intergration of numpy into the tech.v3.datatype system. This allows somewhat more
seamless usage of numpy arrays in datatype and tensor functionality such as enabling
the tech.v3.tensor/ensure-tensor call to work with numpy arrays (as zero copying when possible)."
(:require [tech.v3.datatype.protocols :as dtype-proto]
[tech.v3.tensor :as dtt]
[libpython-clj.python.interpreter :as py-interp]
[libpython-clj.python.protocols :as py-proto]
[libpython-clj.python.bridge :as py-bridge]
[libpython-clj.python.interop :as py-interop]))
(defmethod py-proto/pyobject->jvm :ndarray
[pyobj]
(-> (py-bridge/numpy->desc pyobj)
(dtt/nd-buffer-descriptor->tensor)
(dtt/clone)))
(defmethod py-proto/pyobject-as-jvm :ndarray
[pyobj]
(py-interp/with-gil
(let [interpreter (py-interp/ensure-bound-interpreter)]
(py-bridge/bridge-pyobject
pyobj
interpreter
Iterable
(iterator [this]
(py-proto/python-obj-iterator pyobj interpreter))
py-proto/PPyObjectBridgeToMap
(as-map [item]
(py-bridge/generic-python-as-map pyobj))
py-proto/PPyObjectBridgeToList
(as-list [item]
(py-bridge/generic-python-as-list pyobj))
dtype-proto/PToTensor
(as-tensor [item]
(-> (py-bridge/numpy->desc item)
dtt/nd-buffer-descriptor->tensor))
dtype-proto/PElemwiseDatatype
(elemwise-datatype
[this]
(-> (py-proto/get-attr pyobj "dtype")
(py-proto/as-jvm {})
(py-bridge/obj-dtype->dtype)))
dtype-proto/PECount
(ecount [this] (apply * (dtype-proto/shape this)))
dtype-proto/PShape
(shape
[this]
(-> (py-proto/get-attr pyobj "shape")
(py-proto/->jvm {})))
dtype-proto/PToNativeBuffer
(convertible-to-native-buffer? [item] true)
(->native-buffer
[item]
(dtype-proto/->native-buffer
(dtype-proto/as-tensor item)))
dtype-proto/PSubBuffer
(sub-buffer
[buffer offset length]
(-> (dtype-proto/as-tensor buffer)
(dtype-proto/sub-buffer offset length)))
dtype-proto/PToNDBufferDesc
(convertible-to-nd-buffer-desc? [item] true)
(->nd-buffer-descriptor
[item]
(py-bridge/numpy->desc item))))))
(def np-mod*
"Delay that dereferences to the python numpy module"
(py-bridge/pydelay
(-> (py-interop/import-module "numpy")
(py-proto/as-jvm {}))))
(comment
;;Dispatch table for what tech.v3.datatype.functional methods are found in the numpy module.
(defn- dispatch-binary-op
[op lhs rhs options]
(case op
:max (py-proto/call-attr @np-mod "max" lhs rhs)
:min (py-proto/call-attr @np-mod "min" lhs rhs)
:+ (py-proto/call-attr @np-mod "add" lhs rhs)
:- (py-proto/call-attr @np-mod "subtract" lhs rhs)
:div (py-proto/call-attr @np-mod "divide" lhs rhs)
:* (py-proto/call-attr @np-mod "multiply" lhs rhs)
:pow (py-proto/call-attr @np-mod "power" lhs rhs)
:quot (py-proto/call-attr @np-mod "floor_divide" lhs rhs)
:rem (py-proto/call-attr @np-mod "mod" lhs rhs)
:bit-and (py-proto/call-attr @np-mod "bitwise_and" lhs rhs)
:bit-flip (py-proto/call-attr @np-mod "bitwise_not" lhs rhs)
:bit-or (py-proto/call-attr @np-mod "bitwise_or" lhs rhs)
:bit-xor (py-proto/call-attr @np-mod "bitwise_xor" lhs rhs)
:bit-shift-left (py-proto/call-attr @np-mod "left_shift" lhs rhs)
:bit-shift-right (py-proto/call-attr @np-mod "right_shift" lhs rhs)
:and (py-proto/call-attr @np-mod "logical_and" lhs rhs)
:or (py-proto/call-attr @np-mod "logical_or" lhs rhs)
:not (py-proto/call-attr @np-mod "logical_not" lhs rhs)
:xor (py-proto/call-attr @np-mod "logical_xor" lhs rhs)
:< (py-proto/call-attr @np-mod "less" lhs rhs)
:<= (py-proto/call-attr @np-mod "less_equal" lhs rhs)
:eq (py-proto/call-attr @np-mod "equal" lhs rhs)
:> (py-proto/call-attr @np-mod "greater" lhs rhs)
:>= (py-proto/call-attr @np-mod "greater_equal" lhs rhs))))