MCPcopy Create free account
hub / github.com/apache/arrow / NdarrayToTensor

Function NdarrayToTensor

python/pyarrow/src/arrow/python/numpy_convert.cc:199–230  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

197#undef TO_ARROW_TYPE_CASE
198
199Status NdarrayToTensor(MemoryPool* pool, PyObject* ao,
200 const std::vector<std::string>& dim_names,
201 std::shared_ptr<Tensor>* out) {
202 if (!PyArray_Check(ao)) {
203 return Status::TypeError("Did not pass ndarray object");
204 }
205
206 PyArrayObject* ndarray = reinterpret_cast<PyArrayObject*>(ao);
207
208 // TODO(wesm): What do we want to do with non-contiguous memory and negative strides?
209
210 int ndim = PyArray_NDIM(ndarray);
211
212 std::shared_ptr<Buffer> data = std::make_shared<NumPyBuffer>(ao);
213 std::vector<int64_t> shape(ndim);
214 std::vector<int64_t> strides(ndim);
215
216 npy_intp* array_strides = PyArray_STRIDES(ndarray);
217 npy_intp* array_shape = PyArray_SHAPE(ndarray);
218 for (int i = 0; i < ndim; ++i) {
219 if (array_strides[i] < 0) {
220 return Status::Invalid("Negative ndarray strides not supported");
221 }
222 shape[i] = array_shape[i];
223 strides[i] = array_strides[i];
224 }
225
226 ARROW_ASSIGN_OR_RAISE(
227 auto type, GetTensorType(reinterpret_cast<PyObject*>(PyArray_DESCR(ndarray))));
228 *out = std::make_shared<Tensor>(type, data, shape, strides, dim_names);
229 return Status::OK();
230}
231
232Status TensorToNdarray(const std::shared_ptr<Tensor>& tensor, PyObject* base,
233 PyObject** out) {

Callers 3

Calls 3

TypeErrorFunction · 0.50
InvalidFunction · 0.50
OKFunction · 0.50

Tested by

no test coverage detected