| 471 | } |
| 472 | |
| 473 | Status NdarraysToSparseCSFTensor(MemoryPool* pool, PyObject* data_ao, PyObject* indptr_ao, |
| 474 | PyObject* indices_ao, const std::vector<int64_t>& shape, |
| 475 | const std::vector<int64_t>& axis_order, |
| 476 | const std::vector<std::string>& dim_names, |
| 477 | std::shared_ptr<SparseCSFTensor>* out) { |
| 478 | if (!PyArray_Check(data_ao)) { |
| 479 | return Status::TypeError("Did not pass ndarray object for data"); |
| 480 | } |
| 481 | const int ndim = static_cast<const int>(shape.size()); |
| 482 | PyArrayObject* ndarray_data = reinterpret_cast<PyArrayObject*>(data_ao); |
| 483 | std::shared_ptr<Buffer> data = std::make_shared<NumPyBuffer>(data_ao); |
| 484 | ARROW_ASSIGN_OR_RAISE( |
| 485 | auto type_data, |
| 486 | GetTensorType(reinterpret_cast<PyObject*>(PyArray_DESCR(ndarray_data)))); |
| 487 | |
| 488 | std::vector<std::shared_ptr<Tensor>> indptr(ndim - 1); |
| 489 | std::vector<std::shared_ptr<Tensor>> indices(ndim); |
| 490 | |
| 491 | for (int i = 0; i < ndim - 1; ++i) { |
| 492 | #ifdef Py_GIL_DISABLED |
| 493 | PyObject* item = PySequence_ITEM(indptr_ao, i); |
| 494 | RETURN_IF_PYERROR(); |
| 495 | OwnedRef item_ref(item); |
| 496 | #else |
| 497 | PyObject* item = PySequence_Fast_GET_ITEM(indptr_ao, i); |
| 498 | #endif |
| 499 | if (!PyArray_Check(item)) { |
| 500 | return Status::TypeError("Did not pass ndarray object for indptr"); |
| 501 | } |
| 502 | RETURN_NOT_OK(NdarrayToTensor(pool, item, {}, &indptr[i])); |
| 503 | ARROW_CHECK_EQ(indptr[i]->type_id(), Type::INT64); // Should be ensured by caller |
| 504 | } |
| 505 | |
| 506 | for (int i = 0; i < ndim; ++i) { |
| 507 | #ifdef Py_GIL_DISABLED |
| 508 | PyObject* item = PySequence_ITEM(indices_ao, i); |
| 509 | RETURN_IF_PYERROR(); |
| 510 | OwnedRef item_ref(item); |
| 511 | #else |
| 512 | PyObject* item = PySequence_Fast_GET_ITEM(indices_ao, i); |
| 513 | #endif |
| 514 | if (!PyArray_Check(item)) { |
| 515 | return Status::TypeError("Did not pass ndarray object for indices"); |
| 516 | } |
| 517 | RETURN_NOT_OK(NdarrayToTensor(pool, item, {}, &indices[i])); |
| 518 | ARROW_CHECK_EQ(indices[i]->type_id(), Type::INT64); // Should be ensured by caller |
| 519 | } |
| 520 | |
| 521 | auto sparse_index = std::make_shared<SparseCSFIndex>(indptr, indices, axis_order); |
| 522 | *out = std::make_shared<SparseTensorImpl<SparseCSFIndex>>(sparse_index, type_data, data, |
| 523 | shape, dim_names); |
| 524 | return Status::OK(); |
| 525 | } |
| 526 | |
| 527 | Status NdarraysToSparseCSRMatrix(MemoryPool* pool, PyObject* data_ao, PyObject* indptr_ao, |
| 528 | PyObject* indices_ao, const std::vector<int64_t>& shape, |
nothing calls this directly
no test coverage detected