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

Function SparseTensorDataToNdarray

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

Wrap the dense data of a sparse tensor in a ndarray

Source from the content-addressed store, hash-verified

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