Wrap the dense data of a sparse tensor in a ndarray
| 278 | |
| 279 | // Wrap the dense data of a sparse tensor in a ndarray |
| 280 | static 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 | |
| 305 | Status SparseCOOTensorToNdarray(const std::shared_ptr<SparseCOOTensor>& sparse_tensor, |
| 306 | PyObject* base, PyObject** out_data, |
no test coverage detected