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

Function SparseTensorDataToNdarray

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

Wrap the dense data of a sparse tensor in a ndarray

Source from the content-addressed store, hash-verified

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