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

Function SparseTensorDataToNdarray

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

Wrap the dense data of a sparse tensor in a ndarray

Source from the content-addressed store, hash-verified

278
279// Wrap the dense data of a sparse tensor in a ndarray
280static Status SparseTensorDataToNdarray(const SparseTensor& sparse_tensor,
281 std::vector<npy_intp> data_shape, PyObject* base,
282 PyObject** out_data) {
283 int type_num_data = 0;
284 RETURN_NOT_OK(GetNumPyType(*sparse_tensor.type(), &type_num_data));
285 PyArray_Descr* dtype_data = PyArray_DescrNewFromType(type_num_data);
286 RETURN_IF_PYERROR();
287
288 const void* immutable_data = sparse_tensor.data()->data();
289 // Remove const =(
290 void* mutable_data = const_cast<void*>(immutable_data);
291 int array_flags = NPY_ARRAY_C_CONTIGUOUS | NPY_ARRAY_F_CONTIGUOUS;
292 if (sparse_tensor.is_mutable()) {
293 array_flags |= NPY_ARRAY_WRITEABLE;
294 }
295
296 *out_data = PyArray_NewFromDescr(&PyArray_Type, dtype_data,
297 static_cast<int>(data_shape.size()), data_shape.data(),
298 nullptr, mutable_data, array_flags, nullptr);
299 RETURN_IF_PYERROR();
300 Py_XINCREF(base);
301 PyArray_SetBaseObject(reinterpret_cast<PyArrayObject*>(*out_data), base);
302 return Status::OK();
303}
304
305Status SparseCOOTensorToNdarray(const std::shared_ptr<SparseCOOTensor>& sparse_tensor,
306 PyObject* base, PyObject** out_data,

Callers 3

SparseCOOTensorToNdarrayFunction · 0.85
SparseCSXMatrixToNdarrayFunction · 0.85
SparseCSFTensorToNdarrayFunction · 0.85

Calls 5

GetNumPyTypeFunction · 0.85
OKFunction · 0.50
typeMethod · 0.45
dataMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected