Repository navigation
Expand file tree
/
Copy pathtensor.pxi
More file actions
367 lines (298 loc) · 11.3 KB
/
Copy pathtensor.pxi
File metadata and controls
367 lines (298 loc) · 11.3 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
cdef class Tensor:
"""
A n-dimensional array a.k.a Tensor.
"""
def __init__(self):
raise TypeError("Do not call Tensor's constructor directly, use one "
"of the `pyarrow.Tensor.from_*` functions instead.")
cdef void init(self, const shared_ptr[CTensor]& sp_tensor):
self.sp_tensor = sp_tensor
self.tp = sp_tensor.get()
self.type = pyarrow_wrap_data_type(self.tp.type())
def __repr__(self):
return """<pyarrow.Tensor>
type: {0.type}
shape: {0.shape}
strides: {0.strides}""".format(self)
@staticmethod
def from_numpy(obj, dim_names=None):
cdef:
vector[c_string] c_dim_names
shared_ptr[CTensor] ctensor
if dim_names is not None:
for x in dim_names:
c_dim_names.push_back(tobytes(x))
check_status(NdarrayToTensor(c_default_memory_pool(), obj,
c_dim_names, &ctensor))
return pyarrow_wrap_tensor(ctensor)
def to_numpy(self):
"""
Convert arrow::Tensor to numpy.ndarray with zero copy
"""
cdef PyObject* out
check_status(TensorToNdarray(self.sp_tensor, self, &out))
return PyObject_to_object(out)
def equals(self, Tensor other):
"""
Return true if the tensors contains exactly equal data
"""
return self.tp.Equals(deref(other.tp))
def __eq__(self, other):
if isinstance(other, Tensor):
return self.equals(other)
else:
return NotImplemented
def dim_name(self, i):
return frombytes(self.tp.dim_name(i))
@property
def dim_names(self):
return [frombytes(x) for x in tuple(self.tp.dim_names())]
@property
def is_mutable(self):
return self.tp.is_mutable()
@property
def is_contiguous(self):
return self.tp.is_contiguous()
@property
def ndim(self):
return self.tp.ndim()
@property
def size(self):
return self.tp.size()
@property
def shape(self):
# Cython knows how to convert a vector[T] to a Python list
return tuple(self.tp.shape())
@property
def strides(self):
return tuple(self.tp.strides())
def __getbuffer__(self, cp.Py_buffer* buffer, int flags):
buffer.buf = <char *> self.tp.data().get().data()
pep3118_format = self.type.pep3118_format
if pep3118_format is None:
raise NotImplementedError("type %s not supported for buffer "
"protocol" % (self.type,))
buffer.format = pep3118_format
buffer.itemsize = self.type.bit_width // 8
buffer.internal = NULL
buffer.len = self.tp.size() * buffer.itemsize
buffer.ndim = self.tp.ndim()
buffer.obj = self
if self.tp.is_mutable():
buffer.readonly = 0
else:
buffer.readonly = 1
# NOTE: This assumes Py_ssize_t == int64_t, and that the shape
# and strides arrays lifetime is tied to the tensor's
buffer.shape = <Py_ssize_t *> &self.tp.shape()[0]
buffer.strides = <Py_ssize_t *> &self.tp.strides()[0]
buffer.suboffsets = NULL
cdef class SparseTensorCOO:
"""
A sparse COO tensor.
"""
def __init__(self):
raise TypeError("Do not call SparseTensorCOO's constructor directly, "
"use one of the `pyarrow.SparseTensorCOO.from_*` "
"functions instead.")
cdef void init(self, const shared_ptr[CSparseTensorCOO]& sp_sparse_tensor):
self.sp_sparse_tensor = sp_sparse_tensor
self.stp = sp_sparse_tensor.get()
self.type = pyarrow_wrap_data_type(self.stp.type())
def __repr__(self):
return """<pyarrow.SparseTensorCOO>
type: {0.type}
shape: {0.shape}""".format(self)
@classmethod
def from_dense_numpy(cls, obj, dim_names=None):
"""
Convert numpy.ndarray to arrow::SparseTensorCOO
"""
return cls.from_tensor(Tensor.from_numpy(obj, dim_names=dim_names))
@staticmethod
def from_numpy(data, coords, shape, dim_names=None):
"""
Create arrow::SparseTensorCOO from numpy.ndarrays
"""
cdef shared_ptr[CSparseTensorCOO] csparse_tensor
cdef vector[int64_t] c_shape
cdef vector[c_string] c_dim_names
for x in shape:
c_shape.push_back(x)
if dim_names is not None:
for x in dim_names:
c_dim_names.push_back(tobytes(x))
# Enforce precondition for SparseTensorCOO indices
coords = np.require(coords, dtype='i8', requirements='F')
if coords.ndim != 2:
raise ValueError("Expected 2-dimensional array for "
"SparseTensorCOO indices")
check_status(NdarraysToSparseTensorCOO(c_default_memory_pool(),
data, coords, c_shape, c_dim_names, &csparse_tensor))
return pyarrow_wrap_sparse_tensor_coo(csparse_tensor)
@staticmethod
def from_tensor(obj):
"""
Convert arrow::Tensor to arrow::SparseTensorCOO
"""
cdef shared_ptr[CSparseTensorCOO] csparse_tensor
cdef shared_ptr[CTensor] ctensor = pyarrow_unwrap_tensor(obj)
with nogil:
check_status(TensorToSparseTensorCOO(ctensor, &csparse_tensor))
return pyarrow_wrap_sparse_tensor_coo(csparse_tensor)
def to_numpy(self):
"""
Convert arrow::SparseTensorCOO to numpy.ndarrays with zero copy
"""
cdef PyObject* out_data
cdef PyObject* out_coords
check_status(SparseTensorCOOToNdarray(self.sp_sparse_tensor, self,
&out_data, &out_coords))
return PyObject_to_object(out_data), PyObject_to_object(out_coords)
def equals(self, SparseTensorCOO other):
"""
Return true if sparse tensors contains exactly equal data
"""
return self.stp.Equals(deref(other.stp))
def __eq__(self, other):
if isinstance(other, SparseTensorCOO):
return self.equals(other)
else:
return NotImplemented
@property
def is_mutable(self):
return self.stp.is_mutable()
@property
def ndim(self):
return self.stp.ndim()
@property
def shape(self):
# Cython knows how to convert a vector[T] to a Python list
return tuple(self.stp.shape())
@property
def size(self):
return self.stp.size()
def dim_name(self, i):
return frombytes(self.stp.dim_name(i))
@property
def dim_names(self):
return [frombytes(x) for x in tuple(self.stp.dim_names())]
@property
def non_zero_length(self):
return self.stp.non_zero_length()
cdef class SparseTensorCSR:
"""
A sparse CSR tensor.
"""
def __init__(self):
raise TypeError("Do not call SparseTensorCSR's constructor directly, "
"use one of the `pyarrow.SparseTensorCSR.from_*` "
"functions instead.")
cdef void init(self, const shared_ptr[CSparseTensorCSR]& sp_sparse_tensor):
self.sp_sparse_tensor = sp_sparse_tensor
self.stp = sp_sparse_tensor.get()
self.type = pyarrow_wrap_data_type(self.stp.type())
def __repr__(self):
return """<pyarrow.SparseTensorCSR>
type: {0.type}
shape: {0.shape}""".format(self)
@classmethod
def from_dense_numpy(cls, obj, dim_names=None):
"""
Convert numpy.ndarray to arrow::SparseTensorCSR
"""
return cls.from_tensor(Tensor.from_numpy(obj, dim_names=dim_names))
@staticmethod
def from_numpy(data, indptr, indices, shape, dim_names=None):
"""
Create arrow::SparseTensorCSR from numpy.ndarrays
"""
cdef shared_ptr[CSparseTensorCSR] csparse_tensor
cdef vector[int64_t] c_shape
cdef vector[c_string] c_dim_names
for x in shape:
c_shape.push_back(x)
if dim_names is not None:
for x in dim_names:
c_dim_names.push_back(tobytes(x))
# Enforce precondition for SparseTensorCSR indices
indptr = np.require(indptr, dtype='i8')
indices = np.require(indices, dtype='i8')
if indptr.ndim != 1:
raise ValueError("Expected 1-dimensional array for "
"SparseTensorCSR indptr")
if indices.ndim != 1:
raise ValueError("Expected 1-dimensional array for "
"SparseTensorCSR indices")
check_status(NdarraysToSparseTensorCSR(c_default_memory_pool(),
data, indptr, indices, c_shape, c_dim_names,
&csparse_tensor))
return pyarrow_wrap_sparse_tensor_csr(csparse_tensor)
@staticmethod
def from_tensor(obj):
"""
Convert arrow::Tensor to arrow::SparseTensorCSR
"""
cdef shared_ptr[CSparseTensorCSR] csparse_tensor
cdef shared_ptr[CTensor] ctensor = pyarrow_unwrap_tensor(obj)
with nogil:
check_status(TensorToSparseTensorCSR(ctensor, &csparse_tensor))
return pyarrow_wrap_sparse_tensor_csr(csparse_tensor)
def to_numpy(self):
"""
Convert arrow::SparseTensorCSR to numpy.ndarrays with zero copy
"""
cdef PyObject* out_data
cdef PyObject* out_indptr
cdef PyObject* out_indices
check_status(SparseTensorCSRToNdarray(self.sp_sparse_tensor, self,
&out_data, &out_indptr, &out_indices))
return (PyObject_to_object(out_data), PyObject_to_object(out_indptr),
PyObject_to_object(out_indices))
def equals(self, SparseTensorCSR other):
"""
Return true if sparse tensors contains exactly equal data
"""
return self.stp.Equals(deref(other.stp))
def __eq__(self, other):
if isinstance(other, SparseTensorCSR):
return self.equals(other)
else:
return NotImplemented
@property
def is_mutable(self):
return self.stp.is_mutable()
@property
def ndim(self):
return self.stp.ndim()
@property
def shape(self):
# Cython knows how to convert a vector[T] to a Python list
return tuple(self.stp.shape())
@property
def size(self):
return self.stp.size()
def dim_name(self, i):
return frombytes(self.stp.dim_name(i))
@property
def dim_names(self):
return [frombytes(x) for x in tuple(self.stp.dim_names())]
@property
def non_zero_length(self):
return self.stp.non_zero_length()