Wrap the dense data of a sparse tensor in a ndarray
| 281 | |
| 282 | // Wrap the dense data of a sparse tensor in a ndarray |
| 283 | static 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 | |
| 308 | Status SparseCOOTensorToNdarray(const std::shared_ptr<SparseCOOTensor>& sparse_tensor, |
| 309 | PyObject* base, PyObject** out_data, |
no test coverage detected