diff --git a/cpp/src/arrow/ipc/read_write_test.cc b/cpp/src/arrow/ipc/read_write_test.cc index 5d979486406..7dd3e7cf6d3 100644 --- a/cpp/src/arrow/ipc/read_write_test.cc +++ b/cpp/src/arrow/ipc/read_write_test.cc @@ -1151,8 +1151,8 @@ class TestSparseTensorRoundTrip : public ::testing::Test, public IpcTestFixture void SetUp() { IpcTestFixture::SetUp(); } void TearDown() { IpcTestFixture::TearDown(); } - void CheckSparseTensorRoundTrip(const SparseTensorCOO& sparse_tensor); - void CheckSparseTensorRoundTrip(const SparseTensorCSR& sparse_tensor); + void CheckSparseTensorRoundTrip(const SparseCOOTensor& sparse_tensor); + void CheckSparseTensorRoundTrip(const SparseCSRMatrix& sparse_tensor); protected: std::shared_ptr MakeSparseCOOIndex( @@ -1166,19 +1166,19 @@ class TestSparseTensorRoundTrip : public ::testing::Test, public IpcTestFixture } template - std::shared_ptr MakeSparseTensorCOO( + std::shared_ptr MakeSparseCOOTensor( const std::shared_ptr& si, std::vector& sparse_values, const std::vector& shape, const std::vector& dim_names = {}) const { auto data = Buffer::Wrap(sparse_values); - return std::make_shared(si, CTypeTraits::type_singleton(), + return std::make_shared(si, CTypeTraits::type_singleton(), data, shape, dim_names); } }; template void TestSparseTensorRoundTrip::CheckSparseTensorRoundTrip( - const SparseTensorCOO& sparse_tensor) { + const SparseCOOTensor& sparse_tensor) { const auto& type = checked_cast(*sparse_tensor.type()); const int elem_size = type.bit_width() / 8; const int index_elem_size = sizeof(typename IndexValueType::c_type); @@ -1214,7 +1214,7 @@ void TestSparseTensorRoundTrip::CheckSparseTensorRoundTrip( template void TestSparseTensorRoundTrip::CheckSparseTensorRoundTrip( - const SparseTensorCSR& sparse_tensor) { + const SparseCSRMatrix& sparse_tensor) { const auto& type = checked_cast(*sparse_tensor.type()); const int elem_size = type.bit_width() / 8; const int index_elem_size = sizeof(typename IndexValueType::c_type); @@ -1291,7 +1291,7 @@ TYPED_TEST_P(TestSparseTensorRoundTrip, WithSparseCOOIndexRowMajor) { std::vector shape = {2, 3, 4}; std::vector dim_names = {"foo", "bar", "baz"}; std::vector values = {1, 2, 3, 4, 5, 6, 11, 12, 13, 14, 15, 16}; - auto st = this->MakeSparseTensorCOO(si, values, shape, dim_names); + auto st = this->MakeSparseCOOTensor(si, values, shape, dim_names); this->CheckSparseTensorRoundTrip(*st); } @@ -1334,7 +1334,7 @@ TYPED_TEST_P(TestSparseTensorRoundTrip, WithSparseCOOIndexColumnMajor) { std::vector shape = {2, 3, 4}; std::vector dim_names = {"foo", "bar", "baz"}; std::vector values = {1, 2, 3, 4, 5, 6, 11, 12, 13, 14, 15, 16}; - auto st = this->MakeSparseTensorCOO(si, values, shape, dim_names); + auto st = this->MakeSparseCOOTensor(si, values, shape, dim_names); this->CheckSparseTensorRoundTrip(*st); } @@ -1353,7 +1353,7 @@ TYPED_TEST_P(TestSparseTensorRoundTrip, WithSparseCSRIndex) { auto data = Buffer::Wrap(values); NumericTensor t(data, shape, {}, dim_names); - SparseTensorImpl st(t, TypeTraits::type_singleton()); + SparseCSRMatrix st(t, TypeTraits::type_singleton()); this->CheckSparseTensorRoundTrip(st); } diff --git a/cpp/src/arrow/ipc/reader.cc b/cpp/src/arrow/ipc/reader.cc index 6c64f15d20f..66ec3578397 100644 --- a/cpp/src/arrow/ipc/reader.cc +++ b/cpp/src/arrow/ipc/reader.cc @@ -920,8 +920,7 @@ Status MakeSparseTensorWithSparseCOOIndex( const std::vector& dim_names, const std::shared_ptr& sparse_index, int64_t non_zero_length, const std::shared_ptr& data, std::shared_ptr* out) { - *out = std::make_shared>(sparse_index, type, data, - shape, dim_names); + *out = std::make_shared(sparse_index, type, data, shape, dim_names); return Status::OK(); } @@ -930,8 +929,7 @@ Status MakeSparseTensorWithSparseCSRIndex( const std::vector& dim_names, const std::shared_ptr& sparse_index, int64_t non_zero_length, const std::shared_ptr& data, std::shared_ptr* out) { - *out = std::make_shared>(sparse_index, type, data, - shape, dim_names); + *out = std::make_shared(sparse_index, type, data, shape, dim_names); return Status::OK(); } diff --git a/cpp/src/arrow/python/numpy_convert.cc b/cpp/src/arrow/python/numpy_convert.cc index 45f6fbb7490..6c1f3d7c967 100644 --- a/cpp/src/arrow/python/numpy_convert.cc +++ b/cpp/src/arrow/python/numpy_convert.cc @@ -315,7 +315,7 @@ static Status SparseTensorDataToNdarray(const SparseTensor& sparse_tensor, return Status::OK(); } -Status SparseTensorCOOToNdarray(const std::shared_ptr& sparse_tensor, +Status SparseCOOTensorToNdarray(const std::shared_ptr& sparse_tensor, PyObject* base, PyObject** out_data, PyObject** out_coords) { const auto& sparse_index = arrow::internal::checked_cast( @@ -335,7 +335,7 @@ Status SparseTensorCOOToNdarray(const std::shared_ptr& sparse_t return Status::OK(); } -Status SparseTensorCSRToNdarray(const std::shared_ptr& sparse_tensor, +Status SparseCSRMatrixToNdarray(const std::shared_ptr& sparse_tensor, PyObject* base, PyObject** out_data, PyObject** out_indptr, PyObject** out_indices) { const auto& sparse_index = arrow::internal::checked_cast( @@ -358,10 +358,10 @@ Status SparseTensorCSRToNdarray(const std::shared_ptr& sparse_t return Status::OK(); } -Status NdarraysToSparseTensorCOO(MemoryPool* pool, PyObject* data_ao, PyObject* coords_ao, +Status NdarraysToSparseCOOTensor(MemoryPool* pool, PyObject* data_ao, PyObject* coords_ao, const std::vector& shape, const std::vector& dim_names, - std::shared_ptr* out) { + std::shared_ptr* out) { if (!PyArray_Check(data_ao) || !PyArray_Check(coords_ao)) { return Status::TypeError("Did not pass ndarray object"); } @@ -383,10 +383,10 @@ Status NdarraysToSparseTensorCOO(MemoryPool* pool, PyObject* data_ao, PyObject* return Status::OK(); } -Status NdarraysToSparseTensorCSR(MemoryPool* pool, PyObject* data_ao, PyObject* indptr_ao, +Status NdarraysToSparseCSRMatrix(MemoryPool* pool, PyObject* data_ao, PyObject* indptr_ao, PyObject* indices_ao, const std::vector& shape, const std::vector& dim_names, - std::shared_ptr* out) { + std::shared_ptr* out) { if (!PyArray_Check(data_ao) || !PyArray_Check(indptr_ao) || !PyArray_Check(indices_ao)) { return Status::TypeError("Did not pass ndarray object"); @@ -412,15 +412,15 @@ Status NdarraysToSparseTensorCSR(MemoryPool* pool, PyObject* data_ao, PyObject* return Status::OK(); } -Status TensorToSparseTensorCOO(const std::shared_ptr& tensor, - std::shared_ptr* out) { - *out = std::make_shared(*tensor); +Status TensorToSparseCOOTensor(const std::shared_ptr& tensor, + std::shared_ptr* out) { + *out = std::make_shared(*tensor); return Status::OK(); } -Status TensorToSparseTensorCSR(const std::shared_ptr& tensor, - std::shared_ptr* out) { - *out = std::make_shared(*tensor); +Status TensorToSparseCSRMatrix(const std::shared_ptr& tensor, + std::shared_ptr* out) { + *out = std::make_shared(*tensor); return Status::OK(); } diff --git a/cpp/src/arrow/python/numpy_convert.h b/cpp/src/arrow/python/numpy_convert.h index 85ef36b1bc5..917c403461b 100644 --- a/cpp/src/arrow/python/numpy_convert.h +++ b/cpp/src/arrow/python/numpy_convert.h @@ -62,30 +62,30 @@ ARROW_PYTHON_EXPORT Status TensorToNdarray(const std::shared_ptr& tensor PyObject* base, PyObject** out); ARROW_PYTHON_EXPORT Status -SparseTensorCOOToNdarray(const std::shared_ptr& sparse_tensor, +SparseCOOTensorToNdarray(const std::shared_ptr& sparse_tensor, PyObject* base, PyObject** out_data, PyObject** out_coords); -ARROW_PYTHON_EXPORT Status SparseTensorCSRToNdarray( - const std::shared_ptr& sparse_tensor, PyObject* base, +ARROW_PYTHON_EXPORT Status SparseCSRMatrixToNdarray( + const std::shared_ptr& sparse_tensor, PyObject* base, PyObject** out_data, PyObject** out_indptr, PyObject** out_indices); -ARROW_PYTHON_EXPORT Status NdarraysToSparseTensorCOO( +ARROW_PYTHON_EXPORT Status NdarraysToSparseCOOTensor( MemoryPool* pool, PyObject* data_ao, PyObject* coords_ao, const std::vector& shape, const std::vector& dim_names, - std::shared_ptr* out); + std::shared_ptr* out); -ARROW_PYTHON_EXPORT Status NdarraysToSparseTensorCSR( +ARROW_PYTHON_EXPORT Status NdarraysToSparseCSRMatrix( MemoryPool* pool, PyObject* data_ao, PyObject* indptr_ao, PyObject* indices_ao, const std::vector& shape, const std::vector& dim_names, - std::shared_ptr* out); + std::shared_ptr* out); ARROW_PYTHON_EXPORT Status -TensorToSparseTensorCOO(const std::shared_ptr& tensor, - std::shared_ptr* csparse_tensor); +TensorToSparseCOOTensor(const std::shared_ptr& tensor, + std::shared_ptr* csparse_tensor); ARROW_PYTHON_EXPORT Status -TensorToSparseTensorCSR(const std::shared_ptr& tensor, - std::shared_ptr* csparse_tensor); +TensorToSparseCSRMatrix(const std::shared_ptr& tensor, + std::shared_ptr* csparse_tensor); } // namespace py } // namespace arrow diff --git a/cpp/src/arrow/python/pyarrow.cc b/cpp/src/arrow/python/pyarrow.cc index b335c29f1dd..64167c980fb 100644 --- a/cpp/src/arrow/python/pyarrow.cc +++ b/cpp/src/arrow/python/pyarrow.cc @@ -132,42 +132,42 @@ PyObject* wrap_tensor(const std::shared_ptr& tensor) { return ::pyarrow_wrap_tensor(tensor); } -bool is_sparse_tensor_csr(PyObject* sparse_tensor) { - return ::pyarrow_is_sparse_tensor_csr(sparse_tensor) != 0; +bool is_sparse_csr_matrix(PyObject* sparse_tensor) { + return ::pyarrow_is_sparse_csr_matrix(sparse_tensor) != 0; } -Status unwrap_sparse_tensor_csr(PyObject* sparse_tensor, - std::shared_ptr* out) { - *out = ::pyarrow_unwrap_sparse_tensor_csr(sparse_tensor); +Status unwrap_sparse_csr_matrix(PyObject* sparse_tensor, + std::shared_ptr* out) { + *out = ::pyarrow_unwrap_sparse_csr_matrix(sparse_tensor); if (*out) { return Status::OK(); } else { return Status::Invalid( - "Could not unwrap SparseTensorCSR from the passed Python object."); + "Could not unwrap SparseCSRMatrix from the passed Python object."); } } -PyObject* wrap_sparse_tensor_csr(const std::shared_ptr& sparse_tensor) { - return ::pyarrow_wrap_sparse_tensor_csr(sparse_tensor); +PyObject* wrap_sparse_csr_matrix(const std::shared_ptr& sparse_tensor) { + return ::pyarrow_wrap_sparse_csr_matrix(sparse_tensor); } -bool is_sparse_tensor_coo(PyObject* sparse_tensor) { - return ::pyarrow_is_sparse_tensor_coo(sparse_tensor) != 0; +bool is_sparse_coo_tensor(PyObject* sparse_tensor) { + return ::pyarrow_is_sparse_coo_tensor(sparse_tensor) != 0; } -Status unwrap_sparse_tensor_coo(PyObject* sparse_tensor, - std::shared_ptr* out) { - *out = ::pyarrow_unwrap_sparse_tensor_coo(sparse_tensor); +Status unwrap_sparse_coo_tensor(PyObject* sparse_tensor, + std::shared_ptr* out) { + *out = ::pyarrow_unwrap_sparse_coo_tensor(sparse_tensor); if (*out) { return Status::OK(); } else { return Status::Invalid( - "Could not unwrap SparseTensorCOO from the passed Python object."); + "Could not unwrap SparseCOOTensor from the passed Python object."); } } -PyObject* wrap_sparse_tensor_coo(const std::shared_ptr& sparse_tensor) { - return ::pyarrow_wrap_sparse_tensor_coo(sparse_tensor); +PyObject* wrap_sparse_coo_tensor(const std::shared_ptr& sparse_tensor) { + return ::pyarrow_wrap_sparse_coo_tensor(sparse_tensor); } bool is_table(PyObject* table) { return ::pyarrow_is_table(table) != 0; } diff --git a/cpp/src/arrow/python/pyarrow.h b/cpp/src/arrow/python/pyarrow.h index 82635243e5e..34ecbbe5651 100644 --- a/cpp/src/arrow/python/pyarrow.h +++ b/cpp/src/arrow/python/pyarrow.h @@ -75,15 +75,15 @@ ARROW_PYTHON_EXPORT PyObject* wrap_tensor(const std::shared_ptr& tensor) ARROW_PYTHON_EXPORT bool is_sparse_tensor_coo(PyObject* sparse_tensor); ARROW_PYTHON_EXPORT Status -unwrap_sparse_tensor_coo(PyObject* sparse_tensor, std::shared_ptr* out); -ARROW_PYTHON_EXPORT PyObject* wrap_sparse_tensor_coo( - const std::shared_ptr& sparse_tensor); +unwrap_sparse_coo_tensor(PyObject* sparse_tensor, std::shared_ptr* out); +ARROW_PYTHON_EXPORT PyObject* wrap_sparse_coo_tensor( + const std::shared_ptr& sparse_tensor); ARROW_PYTHON_EXPORT bool is_sparse_tensor_csr(PyObject* sparse_tensor); ARROW_PYTHON_EXPORT Status -unwrap_sparse_tensor_csr(PyObject* sparse_tensor, std::shared_ptr* out); -ARROW_PYTHON_EXPORT PyObject* wrap_sparse_tensor_csr( - const std::shared_ptr& sparse_tensor); +unwrap_sparse_csr_matrix(PyObject* sparse_tensor, std::shared_ptr* out); +ARROW_PYTHON_EXPORT PyObject* wrap_sparse_csr_matrix( + const std::shared_ptr& sparse_tensor); ARROW_PYTHON_EXPORT bool is_table(PyObject* table); ARROW_PYTHON_EXPORT Status unwrap_table(PyObject* table, std::shared_ptr* out); diff --git a/cpp/src/arrow/python/pyarrow_api.h b/cpp/src/arrow/python/pyarrow_api.h index 76e72812361..fc68363fda4 100644 --- a/cpp/src/arrow/python/pyarrow_api.h +++ b/cpp/src/arrow/python/pyarrow_api.h @@ -48,10 +48,10 @@ static PyObject *(*__pyx_api_f_7pyarrow_3lib_pyarrow_wrap_table)(std::shared_ptr #define pyarrow_wrap_table __pyx_api_f_7pyarrow_3lib_pyarrow_wrap_table static PyObject *(*__pyx_api_f_7pyarrow_3lib_pyarrow_wrap_tensor)(std::shared_ptr< arrow::Tensor> const &) = 0; #define pyarrow_wrap_tensor __pyx_api_f_7pyarrow_3lib_pyarrow_wrap_tensor -static PyObject *(*__pyx_api_f_7pyarrow_3lib_pyarrow_wrap_sparse_tensor_csr)(std::shared_ptr< arrow::SparseTensorCSR> const &) = 0; -#define pyarrow_wrap_sparse_tensor_csr __pyx_api_f_7pyarrow_3lib_pyarrow_wrap_sparse_tensor_csr -static PyObject *(*__pyx_api_f_7pyarrow_3lib_pyarrow_wrap_sparse_tensor_coo)(std::shared_ptr< arrow::SparseTensorCOO> const &) = 0; -#define pyarrow_wrap_sparse_tensor_coo __pyx_api_f_7pyarrow_3lib_pyarrow_wrap_sparse_tensor_coo +static PyObject *(*__pyx_api_f_7pyarrow_3lib_pyarrow_wrap_sparse_coo_tensor)(std::shared_ptr< arrow::SparseCOOTensor> const &) = 0; +#define pyarrow_wrap_sparse_coo_tensor __pyx_api_f_7pyarrow_3lib_pyarrow_wrap_sparse_coo_tensor +static PyObject *(*__pyx_api_f_7pyarrow_3lib_pyarrow_wrap_sparse_csr_matrix)(std::shared_ptr< arrow::SparseCSRMatrix> const &) = 0; +#define pyarrow_wrap_sparse_csr_matrix __pyx_api_f_7pyarrow_3lib_pyarrow_wrap_sparse_csr_matrix static std::shared_ptr< arrow::Array> (*__pyx_api_f_7pyarrow_3lib_pyarrow_unwrap_array)(PyObject *) = 0; #define pyarrow_unwrap_array __pyx_api_f_7pyarrow_3lib_pyarrow_unwrap_array static std::shared_ptr< arrow::RecordBatch> (*__pyx_api_f_7pyarrow_3lib_pyarrow_unwrap_batch)(PyObject *) = 0; @@ -68,10 +68,10 @@ static std::shared_ptr< arrow::Table> (*__pyx_api_f_7pyarrow_3lib_pyarrow_unwra #define pyarrow_unwrap_table __pyx_api_f_7pyarrow_3lib_pyarrow_unwrap_table static std::shared_ptr< arrow::Tensor> (*__pyx_api_f_7pyarrow_3lib_pyarrow_unwrap_tensor)(PyObject *) = 0; #define pyarrow_unwrap_tensor __pyx_api_f_7pyarrow_3lib_pyarrow_unwrap_tensor -static std::shared_ptr< arrow::SparseTensorCSR> (*__pyx_api_f_7pyarrow_3lib_pyarrow_unwrap_sparse_tensor_csr)(PyObject *) = 0; -#define pyarrow_unwrap_sparse_tensor_csr __pyx_api_f_7pyarrow_3lib_pyarrow_unwrap_sparse_tensor_csr -static std::shared_ptr< arrow::SparseTensorCOO> (*__pyx_api_f_7pyarrow_3lib_pyarrow_unwrap_sparse_tensor_coo)(PyObject *) = 0; -#define pyarrow_unwrap_sparse_tensor_coo __pyx_api_f_7pyarrow_3lib_pyarrow_unwrap_sparse_tensor_coo +static std::shared_ptr< arrow::SparseCOOTensor> (*__pyx_api_f_7pyarrow_3lib_pyarrow_unwrap_sparse_coo_tensor)(PyObject *) = 0; +#define pyarrow_unwrap_sparse_coo_tensor __pyx_api_f_7pyarrow_3lib_pyarrow_unwrap_sparse_coo_tensor +static std::shared_ptr< arrow::SparseCSRMatrix> (*__pyx_api_f_7pyarrow_3lib_pyarrow_unwrap_sparse_csr_matrix)(PyObject *) = 0; +#define pyarrow_unwrap_sparse_csr_matrix __pyx_api_f_7pyarrow_3lib_pyarrow_unwrap_sparse_csr_matrix static int (*__pyx_api_f_7pyarrow_3lib_pyarrow_internal_check_status)(arrow::Status const &) = 0; #define pyarrow_internal_check_status __pyx_api_f_7pyarrow_3lib_pyarrow_internal_check_status static int (*__pyx_api_f_7pyarrow_3lib_pyarrow_is_buffer)(PyObject *) = 0; @@ -88,10 +88,10 @@ static PyObject *(*__pyx_api_f_7pyarrow_3lib_pyarrow_wrap_scalar)(std::shared_pt #define pyarrow_wrap_scalar __pyx_api_f_7pyarrow_3lib_pyarrow_wrap_scalar static int (*__pyx_api_f_7pyarrow_3lib_pyarrow_is_tensor)(PyObject *) = 0; #define pyarrow_is_tensor __pyx_api_f_7pyarrow_3lib_pyarrow_is_tensor -static int (*__pyx_api_f_7pyarrow_3lib_pyarrow_is_sparse_tensor_csr)(PyObject *) = 0; -#define pyarrow_is_sparse_tensor_csr __pyx_api_f_7pyarrow_3lib_pyarrow_is_sparse_tensor_csr -static int (*__pyx_api_f_7pyarrow_3lib_pyarrow_is_sparse_tensor_coo)(PyObject *) = 0; -#define pyarrow_is_sparse_tensor_coo __pyx_api_f_7pyarrow_3lib_pyarrow_is_sparse_tensor_coo +static int (*__pyx_api_f_7pyarrow_3lib_pyarrow_is_sparse_coo_tensor)(PyObject *) = 0; +#define pyarrow_is_sparse_coo_tensor __pyx_api_f_7pyarrow_3lib_pyarrow_is_sparse_coo_tensor +static int (*__pyx_api_f_7pyarrow_3lib_pyarrow_is_sparse_csr_matrix)(PyObject *) = 0; +#define pyarrow_is_sparse_csr_matrix __pyx_api_f_7pyarrow_3lib_pyarrow_is_sparse_csr_matrix static int (*__pyx_api_f_7pyarrow_3lib_pyarrow_is_table)(PyObject *) = 0; #define pyarrow_is_table __pyx_api_f_7pyarrow_3lib_pyarrow_is_table static int (*__pyx_api_f_7pyarrow_3lib_pyarrow_is_batch)(PyObject *) = 0; @@ -172,8 +172,8 @@ static int import_pyarrow__lib(void) { if (__Pyx_ImportFunction(module, "pyarrow_wrap_schema", (void (**)(void))&__pyx_api_f_7pyarrow_3lib_pyarrow_wrap_schema, "PyObject *(std::shared_ptr< arrow::Schema> const &)") < 0) goto bad; if (__Pyx_ImportFunction(module, "pyarrow_wrap_table", (void (**)(void))&__pyx_api_f_7pyarrow_3lib_pyarrow_wrap_table, "PyObject *(std::shared_ptr< arrow::Table> const &)") < 0) goto bad; if (__Pyx_ImportFunction(module, "pyarrow_wrap_tensor", (void (**)(void))&__pyx_api_f_7pyarrow_3lib_pyarrow_wrap_tensor, "PyObject *(std::shared_ptr< arrow::Tensor> const &)") < 0) goto bad; - if (__Pyx_ImportFunction(module, "pyarrow_wrap_sparse_tensor_csr", (void (**)(void))&__pyx_api_f_7pyarrow_3lib_pyarrow_wrap_sparse_tensor_csr, "PyObject *(std::shared_ptr< arrow::SparseTensorCSR> const &)") < 0) goto bad; - if (__Pyx_ImportFunction(module, "pyarrow_wrap_sparse_tensor_coo", (void (**)(void))&__pyx_api_f_7pyarrow_3lib_pyarrow_wrap_sparse_tensor_csr, "PyObject *(std::shared_ptr< arrow::SparseTensorCOO> const &)") < 0) goto bad; + if (__Pyx_ImportFunction(module, "pyarrow_wrap_sparse_coo_tensor", (void (**)(void))&__pyx_api_f_7pyarrow_3lib_pyarrow_wrap_sparse_coo_tensor, "PyObject *(std::shared_ptr< arrow::SparseCOOTensor> const &)") < 0) goto bad; + if (__Pyx_ImportFunction(module, "pyarrow_wrap_sparse_csr_matrix", (void (**)(void))&__pyx_api_f_7pyarrow_3lib_pyarrow_wrap_sparse_csr_matrix, "PyObject *(std::shared_ptr< arrow::SparseCSRMatrix> const &)") < 0) goto bad; if (__Pyx_ImportFunction(module, "pyarrow_unwrap_array", (void (**)(void))&__pyx_api_f_7pyarrow_3lib_pyarrow_unwrap_array, "std::shared_ptr< arrow::Array> (PyObject *)") < 0) goto bad; if (__Pyx_ImportFunction(module, "pyarrow_unwrap_batch", (void (**)(void))&__pyx_api_f_7pyarrow_3lib_pyarrow_unwrap_batch, "std::shared_ptr< arrow::RecordBatch> (PyObject *)") < 0) goto bad; if (__Pyx_ImportFunction(module, "pyarrow_unwrap_buffer", (void (**)(void))&__pyx_api_f_7pyarrow_3lib_pyarrow_unwrap_buffer, "std::shared_ptr< arrow::Buffer> (PyObject *)") < 0) goto bad; @@ -182,8 +182,8 @@ static int import_pyarrow__lib(void) { if (__Pyx_ImportFunction(module, "pyarrow_unwrap_schema", (void (**)(void))&__pyx_api_f_7pyarrow_3lib_pyarrow_unwrap_schema, "std::shared_ptr< arrow::Schema> (PyObject *)") < 0) goto bad; if (__Pyx_ImportFunction(module, "pyarrow_unwrap_table", (void (**)(void))&__pyx_api_f_7pyarrow_3lib_pyarrow_unwrap_table, "std::shared_ptr< arrow::Table> (PyObject *)") < 0) goto bad; if (__Pyx_ImportFunction(module, "pyarrow_unwrap_tensor", (void (**)(void))&__pyx_api_f_7pyarrow_3lib_pyarrow_unwrap_tensor, "std::shared_ptr< arrow::Tensor> (PyObject *)") < 0) goto bad; - if (__Pyx_ImportFunction(module, "pyarrow_unwrap_sparse_tensor_csr", (void (**)(void))&__pyx_api_f_7pyarrow_3lib_pyarrow_unwrap_sparse_tensor_csr, "std::shared_ptr< arrow::SparseTensorCSR> (PyObject *)") < 0) goto bad; - if (__Pyx_ImportFunction(module, "pyarrow_unwrap_sparse_tensor_coo", (void (**)(void))&__pyx_api_f_7pyarrow_3lib_pyarrow_unwrap_sparse_tensor_coo, "std::shared_ptr< arrow::SparseTensorCOO> (PyObject *)") < 0) goto bad; + if (__Pyx_ImportFunction(module, "pyarrow_unwrap_sparse_coo_tensor", (void (**)(void))&__pyx_api_f_7pyarrow_3lib_pyarrow_unwrap_sparse_coo_tensor, "std::shared_ptr< arrow::SparseCOOTensor> (PyObject *)") < 0) goto bad; + if (__Pyx_ImportFunction(module, "pyarrow_unwrap_sparse_csr_matrix", (void (**)(void))&__pyx_api_f_7pyarrow_3lib_pyarrow_unwrap_sparse_csr_matrix, "std::shared_ptr< arrow::SparseCSRMatrix> (PyObject *)") < 0) goto bad; if (__Pyx_ImportFunction(module, "pyarrow_internal_check_status", (void (**)(void))&__pyx_api_f_7pyarrow_3lib_pyarrow_internal_check_status, "int (arrow::Status const &)") < 0) goto bad; if (__Pyx_ImportFunction(module, "pyarrow_is_buffer", (void (**)(void))&__pyx_api_f_7pyarrow_3lib_pyarrow_is_buffer, "int (PyObject *)") < 0) goto bad; if (__Pyx_ImportFunction(module, "pyarrow_is_data_type", (void (**)(void))&__pyx_api_f_7pyarrow_3lib_pyarrow_is_data_type, "int (PyObject *)") < 0) goto bad; @@ -192,8 +192,8 @@ static int import_pyarrow__lib(void) { if (__Pyx_ImportFunction(module, "pyarrow_is_array", (void (**)(void))&__pyx_api_f_7pyarrow_3lib_pyarrow_is_array, "int (PyObject *)") < 0) goto bad; if (__Pyx_ImportFunction(module, "pyarrow_wrap_scalar", (void (**)(void))&__pyx_api_f_7pyarrow_3lib_pyarrow_wrap_scalar, "PyObject *(std::shared_ptr< arrow::Scalar> const &)") < 0) goto bad; if (__Pyx_ImportFunction(module, "pyarrow_is_tensor", (void (**)(void))&__pyx_api_f_7pyarrow_3lib_pyarrow_is_tensor, "int (PyObject *)") < 0) goto bad; - if (__Pyx_ImportFunction(module, "pyarrow_is_sparse_tensor_csr", (void (**)(void))&__pyx_api_f_7pyarrow_3lib_pyarrow_is_sparse_tensor_csr, "int (PyObject *)") < 0) goto bad; - if (__Pyx_ImportFunction(module, "pyarrow_is_sparse_tensor_coo", (void (**)(void))&__pyx_api_f_7pyarrow_3lib_pyarrow_is_sparse_tensor_coo, "int (PyObject *)") < 0) goto bad; + if (__Pyx_ImportFunction(module, "pyarrow_is_sparse_coo_tensor", (void (**)(void))&__pyx_api_f_7pyarrow_3lib_pyarrow_is_sparse_coo_tensor, "int (PyObject *)") < 0) goto bad; + if (__Pyx_ImportFunction(module, "pyarrow_is_sparse_csr_matrix", (void (**)(void))&__pyx_api_f_7pyarrow_3lib_pyarrow_is_sparse_csr_matrix, "int (PyObject *)") < 0) goto bad; if (__Pyx_ImportFunction(module, "pyarrow_is_table", (void (**)(void))&__pyx_api_f_7pyarrow_3lib_pyarrow_is_table, "int (PyObject *)") < 0) goto bad; if (__Pyx_ImportFunction(module, "pyarrow_is_batch", (void (**)(void))&__pyx_api_f_7pyarrow_3lib_pyarrow_is_batch, "int (PyObject *)") < 0) goto bad; Py_DECREF(module); module = 0; diff --git a/cpp/src/arrow/sparse_tensor.h b/cpp/src/arrow/sparse_tensor.h index 02650a6d35e..47df0115028 100644 --- a/cpp/src/arrow/sparse_tensor.h +++ b/cpp/src/arrow/sparse_tensor.h @@ -273,11 +273,10 @@ class SparseTensorImpl : public SparseTensor { }; /// \brief EXPERIMENTAL: Type alias for COO sparse tensor -using SparseTensorCOO = SparseTensorImpl; +using SparseCOOTensor = SparseTensorImpl; /// \brief EXPERIMENTAL: Type alias for CSR sparse matrix -using SparseTensorCSR = SparseTensorImpl; -using SparseMatrixCSR = SparseTensorImpl; +using SparseCSRMatrix = SparseTensorImpl; } // namespace arrow diff --git a/cpp/src/arrow/sparse_tensor_test.cc b/cpp/src/arrow/sparse_tensor_test.cc index a37f59c3211..e37f3e46ec9 100644 --- a/cpp/src/arrow/sparse_tensor_test.cc +++ b/cpp/src/arrow/sparse_tensor_test.cc @@ -71,21 +71,21 @@ class TestSparseCOOTensorBase : public ::testing::Test { 0, 11, 0, 12, 13, 0, 14, 0, 0, 15, 0, 16}; auto dense_data = Buffer::Wrap(dense_values); NumericTensor dense_tensor(dense_data, shape_, {}, dim_names_); - sparse_tensor_from_dense_ = std::make_shared( + sparse_tensor_from_dense_ = std::make_shared( dense_tensor, TypeTraits::type_singleton()); } protected: std::vector shape_; std::vector dim_names_; - std::shared_ptr sparse_tensor_from_dense_; + std::shared_ptr sparse_tensor_from_dense_; }; class TestSparseCOOTensor : public TestSparseCOOTensorBase {}; TEST_F(TestSparseCOOTensor, CreationEmptyTensor) { - SparseTensorImpl st1(int64(), this->shape_); - SparseTensorImpl st2(int64(), this->shape_, this->dim_names_); + SparseCOOTensor st1(int64(), this->shape_); + SparseCOOTensor st2(int64(), this->shape_, this->dim_names_); ASSERT_EQ(0, st1.non_zero_length()); ASSERT_EQ(0, st2.non_zero_length()); @@ -134,7 +134,7 @@ TEST_F(TestSparseCOOTensor, CreationFromNumericTensor1D) { auto dense_data = Buffer::Wrap(dense_values); std::vector dense_shape({static_cast(dense_values.size())}); NumericTensor dense_vector(dense_data, dense_shape); - SparseTensorImpl st(dense_vector); + SparseCOOTensor st(dense_vector); ASSERT_EQ(12, st.non_zero_length()); ASSERT_TRUE(st.is_mutable()); @@ -158,7 +158,7 @@ TEST_F(TestSparseCOOTensor, CreationFromTensor) { 0, 11, 0, 12, 13, 0, 14, 0, 0, 15, 0, 16}; std::shared_ptr buffer = Buffer::Wrap(values); Tensor tensor(int64(), buffer, this->shape_, {}, this->dim_names_); - SparseTensorImpl st(tensor); + SparseCOOTensor st(tensor); ASSERT_EQ(12, st.non_zero_length()); ASSERT_TRUE(st.is_mutable()); @@ -178,7 +178,7 @@ TEST_F(TestSparseCOOTensor, CreationFromNonContiguousTensor) { std::vector strides = {192, 64, 16}; std::shared_ptr buffer = Buffer::Wrap(values); Tensor tensor(int64(), buffer, this->shape_, strides); - SparseTensorImpl st(tensor); + SparseCOOTensor st(tensor); ASSERT_EQ(12, st.non_zero_length()); ASSERT_TRUE(st.is_mutable()); @@ -195,8 +195,8 @@ TEST_F(TestSparseCOOTensor, TensorEquality) { std::shared_ptr buffer2 = Buffer::Wrap(values2); NumericTensor tensor1(buffer1, this->shape_); NumericTensor tensor2(buffer2, this->shape_); - SparseTensorImpl st1(tensor1); - SparseTensorImpl st2(tensor2); + SparseCOOTensor st1(tensor1); + SparseCOOTensor st2(tensor2); ASSERT_TRUE(st1.Equals(*this->sparse_tensor_from_dense_)); ASSERT_FALSE(st1.Equals(st2)); @@ -217,11 +217,11 @@ class TestSparseCOOTensorForIndexValueType } template - std::shared_ptr MakeSparseTensor( + std::shared_ptr MakeSparseTensor( const std::shared_ptr& si, std::vector& sparse_values) const { auto data = Buffer::Wrap(sparse_values); - return std::make_shared(si, + return std::make_shared(si, CTypeTraits::type_singleton(), data, this->shape_, this->dim_names_); } @@ -357,14 +357,14 @@ class TestSparseCSRMatrixBase : public ::testing::Test { 0, 11, 0, 12, 13, 0, 14, 0, 0, 15, 0, 16}; auto dense_data = Buffer::Wrap(dense_values); NumericTensor dense_tensor(dense_data, shape_, {}, dim_names_); - sparse_tensor_from_dense_ = std::make_shared( + sparse_tensor_from_dense_ = std::make_shared( dense_tensor, TypeTraits::type_singleton()); } protected: std::vector shape_; std::vector dim_names_; - std::shared_ptr sparse_tensor_from_dense_; + std::shared_ptr sparse_tensor_from_dense_; }; class TestSparseCSRMatrix : public TestSparseCSRMatrixBase {}; @@ -375,8 +375,8 @@ TEST_F(TestSparseCSRMatrix, CreationFromNumericTensor2D) { std::shared_ptr buffer = Buffer::Wrap(values); NumericTensor tensor(buffer, this->shape_); - SparseTensorImpl st1(tensor); - SparseTensorImpl& st2 = *this->sparse_tensor_from_dense_; + SparseCSRMatrix st1(tensor); + SparseCSRMatrix& st2 = *this->sparse_tensor_from_dense_; CheckSparseIndexFormatType(SparseTensorFormat::CSR, st1); @@ -423,7 +423,7 @@ TEST_F(TestSparseCSRMatrix, CreationFromNonContiguousTensor) { std::vector strides = {64, 16}; std::shared_ptr buffer = Buffer::Wrap(values); Tensor tensor(int64(), buffer, this->shape_, strides); - SparseTensorImpl st(tensor); + SparseCSRMatrix st(tensor); ASSERT_EQ(12, st.non_zero_length()); ASSERT_TRUE(st.is_mutable()); @@ -462,8 +462,8 @@ TEST_F(TestSparseCSRMatrix, TensorEquality) { std::shared_ptr buffer2 = Buffer::Wrap(values2); NumericTensor tensor1(buffer1, this->shape_); NumericTensor tensor2(buffer2, this->shape_); - SparseTensorImpl st1(tensor1); - SparseTensorImpl st2(tensor2); + SparseCSRMatrix st1(tensor1); + SparseCSRMatrix st2(tensor2); ASSERT_TRUE(st1.Equals(*this->sparse_tensor_from_dense_)); ASSERT_FALSE(st1.Equals(st2)); diff --git a/docs/source/cpp/api/tensor.rst b/docs/source/cpp/api/tensor.rst index eb783f296b9..1493441f5fb 100644 --- a/docs/source/cpp/api/tensor.rst +++ b/docs/source/cpp/api/tensor.rst @@ -48,6 +48,6 @@ Sparse Tensors .. doxygenclass:: arrow::SparseTensorImpl :members: -.. doxygentypedef:: arrow::SparseTensorCOO +.. doxygentypedef:: arrow::SparseCOOTensor -.. doxygentypedef:: arrow::SparseTensorCSR +.. doxygentypedef:: arrow::SparseCSRMatrix diff --git a/docs/source/python/extending.rst b/docs/source/python/extending.rst index 4ee20c77aee..a8bc3b69fc2 100644 --- a/docs/source/python/extending.rst +++ b/docs/source/python/extending.rst @@ -111,15 +111,15 @@ C++ objects. Return whether *obj* wraps an Arrow C++ :class:`Tensor` pointer; in other words, whether *obj* is a :py:class:`pyarrow.Tensor` instance. -.. function:: bool is_sparse_tensor_coo(PyObject* obj) +.. function:: bool is_sparse_coo_tensor(PyObject* obj) - Return whether *obj* wraps an Arrow C++ :class:`SparseTensorCOO` pointer; - in other words, whether *obj* is a :py:class:`pyarrow.SparseTensorCOO` instance. + Return whether *obj* wraps an Arrow C++ :class:`SparseCOOTensor` pointer; + in other words, whether *obj* is a :py:class:`pyarrow.SparseCOOTensor` instance. -.. function:: bool is_sparse_tensor_csr(PyObject* obj) +.. function:: bool is_sparse_csr_matrix(PyObject* obj) - Return whether *obj* wraps an Arrow C++ :class:`SparseTensorCSR` pointer; - in other words, whether *obj* is a :py:class:`pyarrow.SparseTensorCSR` instance. + Return whether *obj* wraps an Arrow C++ :class:`SparseCSRMatrix` pointer; + in other words, whether *obj* is a :py:class:`pyarrow.SparseCSRMatrix` instance. The following functions expect a pyarrow object, unwrap the underlying Arrow C++ API pointer, and put it in the *out* parameter. The returned @@ -158,13 +158,13 @@ occurred. If successful, *out* is guaranteed to be non-NULL. Unwrap the Arrow C++ :class:`Tensor` pointer from *obj* and put it in *out*. -.. function:: Status unwrap_sparse_tensor_coo(PyObject* obj, std::shared_ptr* out) +.. function:: Status unwrap_sparse_coo_tensor(PyObject* obj, std::shared_ptr* out) - Unwrap the Arrow C++ :class:`SparseTensorCOO` pointer from *obj* and put it in *out*. + Unwrap the Arrow C++ :class:`SparseCOOTensor` pointer from *obj* and put it in *out*. -.. function:: Status unwrap_sparse_tensor_csr(PyObject* obj, std::shared_ptr* out) +.. function:: Status unwrap_sparse_csr_matrix(PyObject* obj, std::shared_ptr* out) - Unwrap the Arrow C++ :class:`SparseTensorCSR` pointer from *obj* and put it in *out*. + Unwrap the Arrow C++ :class:`SparseCSRMatrix` pointer from *obj* and put it in *out*. The following functions take an Arrow C++ API pointer and wrap it in a pyarray object of the corresponding type. A new reference is returned. @@ -202,13 +202,13 @@ On error, NULL is returned and a Python exception is set. Wrap the Arrow C++ *tensor* in a :py:class:`pyarrow.Tensor` instance. -.. function:: PyObject* wrap_sparse_tensor_coo(const std::shared_ptr& sparse_tensor) +.. function:: PyObject* wrap_sparse_coo_tensor(const std::shared_ptr& sparse_tensor) - Wrap the Arrow C++ *COO sparse tensor* in a :py:class:`pyarrow.SparseTensorCOO` instance. + Wrap the Arrow C++ *COO sparse tensor* in a :py:class:`pyarrow.SparseCOOTensor` instance. -.. function:: PyObject* wrap_sparse_tensor_csr(const std::shared_ptr& sparse_tensor) +.. function:: PyObject* wrap_sparse_csr_matrix(const std::shared_ptr& sparse_tensor) - Wrap the Arrow C++ *CSR sparse tensor* in a :py:class:`pyarrow.SparseTensorCSR` instance. + Wrap the Arrow C++ *CSR sparse tensor* in a :py:class:`pyarrow.SparseCSRMatrix` instance. Cython API @@ -268,11 +268,11 @@ an exception) if the input is not of the right type. .. function:: pyarrow_unwrap_sparse_tensor_coo(obj) -> shared_ptr[CSparseTensorCOO] - Unwrap the Arrow C++ :cpp:class:`SparseTensorCOO` pointer from *obj*. + Unwrap the Arrow C++ :cpp:class:`SparseCOOTensor` pointer from *obj*. .. function:: pyarrow_unwrap_sparse_tensor_csr(obj) -> shared_ptr[CSparseTensorCSR] - Unwrap the Arrow C++ :cpp:class:`SparseTensorCSR` pointer from *obj*. + Unwrap the Arrow C++ :cpp:class:`SparseCSRMatrix` pointer from *obj*. The following functions take a Arrow C++ API pointer and wrap it in a pyarray object of the corresponding type. An exception is raised on error. @@ -315,11 +315,11 @@ pyarray object of the corresponding type. An exception is raised on error. .. function:: pyarrow_wrap_sparse_tensor_coo(sp_array: const shared_ptr[CSparseTensorCOO]& sparse_tensor) -> object - Wrap the Arrow C++ *COO sparse tensor* in a Python :class:`pyarrow.SparseTensorCOO` instance. + Wrap the Arrow C++ *COO sparse tensor* in a Python :class:`pyarrow.SparseCOOTensor` instance. .. function:: pyarrow_wrap_sparse_tensor_csr(sp_array: const shared_ptr[CSparseTensorCSR]& sparse_tensor) -> object - Wrap the Arrow C++ *CSR sparse tensor* in a Python :class:`pyarrow.SparseTensorCSR` instance. + Wrap the Arrow C++ *CSR sparse tensor* in a Python :class:`pyarrow.SparseCSRMatrix` instance. Example ~~~~~~~ diff --git a/python/pyarrow/__init__.pxd b/python/pyarrow/__init__.pxd index bf6015c8b71..7cc8ec6ada3 100644 --- a/python/pyarrow/__init__.pxd +++ b/python/pyarrow/__init__.pxd @@ -21,7 +21,7 @@ from libcpp.memory cimport shared_ptr from pyarrow.includes.libarrow cimport (CArray, CBuffer, CDataType, CField, CRecordBatch, CSchema, CTable, CTensor, - CSparseTensorCSR, CSparseTensorCOO) + CSparseCSRMatrix, CSparseCOOTensor) from pyarrow.compat import frombytes cdef extern from "arrow/python/pyarrow.h" namespace "arrow::py": @@ -33,8 +33,8 @@ cdef extern from "arrow/python/pyarrow.h" namespace "arrow::py": cdef object wrap_array(const shared_ptr[CArray]& sp_array) cdef object wrap_tensor(const shared_ptr[CTensor]& sp_tensor) cdef object wrap_sparse_tensor_coo( - const shared_ptr[CSparseTensorCOO]& sp_sparse_tensor) + const shared_ptr[CSparseCOOTensor]& sp_sparse_tensor) cdef object wrap_sparse_tensor_csr( - const shared_ptr[CSparseTensorCSR]& sp_sparse_tensor) + const shared_ptr[CSparseCSRMatrix]& sp_sparse_tensor) cdef object wrap_table(const shared_ptr[CTable]& ctable) cdef object wrap_batch(const shared_ptr[CRecordBatch]& cbatch) diff --git a/python/pyarrow/__init__.py b/python/pyarrow/__init__.py index a0ae4c1d80f..5d58263c10e 100644 --- a/python/pyarrow/__init__.py +++ b/python/pyarrow/__init__.py @@ -70,7 +70,7 @@ def parse_git(root, **kwargs): schema, Array, Tensor, array, chunked_array, record_batch, table, - SparseTensorCSR, SparseTensorCOO, + SparseCSRMatrix, SparseCOOTensor, infer_type, from_numpy_dtype, NullArray, NumericArray, IntegerArray, FloatingPointArray, diff --git a/python/pyarrow/includes/libarrow.pxd b/python/pyarrow/includes/libarrow.pxd index 21f9b5e1c62..95b833a7660 100644 --- a/python/pyarrow/includes/libarrow.pxd +++ b/python/pyarrow/includes/libarrow.pxd @@ -654,7 +654,7 @@ cdef extern from "arrow/api.h" namespace "arrow" nogil: Type type_id() c_bool Equals(const CTensor& other) - cdef cppclass CSparseTensorCOO" arrow::SparseTensorCOO": + cdef cppclass CSparseCOOTensor" arrow::SparseCOOTensor": shared_ptr[CDataType] type() shared_ptr[CBuffer] data() @@ -668,9 +668,9 @@ cdef extern from "arrow/api.h" namespace "arrow" nogil: c_bool is_mutable() Type type_id() - c_bool Equals(const CSparseTensorCOO& other) + c_bool Equals(const CSparseCOOTensor& other) - cdef cppclass CSparseTensorCSR" arrow::SparseTensorCSR": + cdef cppclass CSparseCSRMatrix" arrow::SparseCSRMatrix": shared_ptr[CDataType] type() shared_ptr[CBuffer] data() @@ -684,7 +684,7 @@ cdef extern from "arrow/api.h" namespace "arrow" nogil: c_bool is_mutable() Type type_id() - c_bool Equals(const CSparseTensorCSR& other) + c_bool Equals(const CSparseCSRMatrix& other) cdef cppclass CScalar" arrow::Scalar": shared_ptr[CDataType] type @@ -1403,31 +1403,31 @@ cdef extern from "arrow/python/api.h" namespace "arrow::py" nogil: CStatus TensorToNdarray(const shared_ptr[CTensor]& tensor, object base, PyObject** out) - CStatus SparseTensorCOOToNdarray( - const shared_ptr[CSparseTensorCOO]& sparse_tensor, object base, + CStatus SparseCOOTensorToNdarray( + const shared_ptr[CSparseCOOTensor]& sparse_tensor, object base, PyObject** out_data, PyObject** out_coords) - CStatus SparseTensorCSRToNdarray( - const shared_ptr[CSparseTensorCSR]& sparse_tensor, object base, + CStatus SparseCSRMatrixToNdarray( + const shared_ptr[CSparseCSRMatrix]& sparse_tensor, object base, PyObject** out_data, PyObject** out_indptr, PyObject** out_indices) - CStatus NdarraysToSparseTensorCOO(CMemoryPool* pool, object data_ao, + CStatus NdarraysToSparseCOOTensor(CMemoryPool* pool, object data_ao, object coords_ao, const vector[int64_t]& shape, const vector[c_string]& dim_names, - shared_ptr[CSparseTensorCOO]* out) + shared_ptr[CSparseCOOTensor]* out) - CStatus NdarraysToSparseTensorCSR(CMemoryPool* pool, object data_ao, + CStatus NdarraysToSparseCSRMatrix(CMemoryPool* pool, object data_ao, object indptr_ao, object indices_ao, const vector[int64_t]& shape, const vector[c_string]& dim_names, - shared_ptr[CSparseTensorCSR]* out) + shared_ptr[CSparseCSRMatrix]* out) - CStatus TensorToSparseTensorCOO(shared_ptr[CTensor], - shared_ptr[CSparseTensorCOO]* out) + CStatus TensorToSparseCOOTensor(shared_ptr[CTensor], + shared_ptr[CSparseCOOTensor]* out) - CStatus TensorToSparseTensorCSR(shared_ptr[CTensor], - shared_ptr[CSparseTensorCSR]* out) + CStatus TensorToSparseCSRMatrix(shared_ptr[CTensor], + shared_ptr[CSparseCSRMatrix]* out) CStatus ConvertArrayToPandas(const PandasOptions& options, const shared_ptr[CArray]& arr, diff --git a/python/pyarrow/lib.pxd b/python/pyarrow/lib.pxd index 771b2b147db..e533402f4a6 100644 --- a/python/pyarrow/lib.pxd +++ b/python/pyarrow/lib.pxd @@ -261,26 +261,26 @@ cdef class Tensor: cdef void init(self, const shared_ptr[CTensor]& sp_tensor) -cdef class SparseTensorCSR: +cdef class SparseCSRMatrix: cdef: - shared_ptr[CSparseTensorCSR] sp_sparse_tensor - CSparseTensorCSR* stp + shared_ptr[CSparseCSRMatrix] sp_sparse_tensor + CSparseCSRMatrix* stp cdef readonly: DataType type - cdef void init(self, const shared_ptr[CSparseTensorCSR]& sp_sparse_tensor) + cdef void init(self, const shared_ptr[CSparseCSRMatrix]& sp_sparse_tensor) -cdef class SparseTensorCOO: +cdef class SparseCOOTensor: cdef: - shared_ptr[CSparseTensorCOO] sp_sparse_tensor - CSparseTensorCOO* stp + shared_ptr[CSparseCOOTensor] sp_sparse_tensor + CSparseCOOTensor* stp cdef readonly: DataType type - cdef void init(self, const shared_ptr[CSparseTensorCOO]& sp_sparse_tensor) + cdef void init(self, const shared_ptr[CSparseCOOTensor]& sp_sparse_tensor) cdef class NullArray(Array): @@ -519,10 +519,10 @@ cdef public object pyarrow_wrap_resizable_buffer( cdef public object pyarrow_wrap_schema(const shared_ptr[CSchema]& type) cdef public object pyarrow_wrap_table(const shared_ptr[CTable]& ctable) cdef public object pyarrow_wrap_tensor(const shared_ptr[CTensor]& sp_tensor) -cdef public object pyarrow_wrap_sparse_tensor_coo( - const shared_ptr[CSparseTensorCOO]& sp_sparse_tensor) -cdef public object pyarrow_wrap_sparse_tensor_csr( - const shared_ptr[CSparseTensorCSR]& sp_sparse_tensor) +cdef public object pyarrow_wrap_sparse_coo_tensor( + const shared_ptr[CSparseCOOTensor]& sp_sparse_tensor) +cdef public object pyarrow_wrap_sparse_csr_matrix( + const shared_ptr[CSparseCSRMatrix]& sp_sparse_tensor) cdef public shared_ptr[CArray] pyarrow_unwrap_array(object array) cdef public shared_ptr[CRecordBatch] pyarrow_unwrap_batch(object batch) @@ -532,7 +532,7 @@ cdef public shared_ptr[CField] pyarrow_unwrap_field(object field) cdef public shared_ptr[CSchema] pyarrow_unwrap_schema(object schema) cdef public shared_ptr[CTable] pyarrow_unwrap_table(object table) cdef public shared_ptr[CTensor] pyarrow_unwrap_tensor(object tensor) -cdef public shared_ptr[CSparseTensorCOO] pyarrow_unwrap_sparse_tensor_coo( +cdef public shared_ptr[CSparseCOOTensor] pyarrow_unwrap_sparse_coo_tensor( object sparse_tensor) -cdef public shared_ptr[CSparseTensorCSR] pyarrow_unwrap_sparse_tensor_csr( +cdef public shared_ptr[CSparseCSRMatrix] pyarrow_unwrap_sparse_csr_matrix( object sparse_tensor) diff --git a/python/pyarrow/public-api.pxi b/python/pyarrow/public-api.pxi index 3a993405b30..a7309885748 100644 --- a/python/pyarrow/public-api.pxi +++ b/python/pyarrow/public-api.pxi @@ -19,7 +19,7 @@ from libcpp.memory cimport shared_ptr from pyarrow.includes.libarrow cimport (CArray, CDataType, CField, CRecordBatch, CSchema, CTable, CTensor, - CSparseTensorCSR, CSparseTensorCOO) + CSparseCSRMatrix, CSparseCOOTensor) # You cannot assign something to a dereferenced pointer in Cython thus these # methods don't use Status to indicate a successful operation. @@ -259,48 +259,48 @@ cdef api object pyarrow_wrap_tensor( return tensor -cdef api bint pyarrow_is_sparse_tensor_coo(object sparse_tensor): - return isinstance(sparse_tensor, SparseTensorCOO) +cdef api bint pyarrow_is_sparse_coo_tensor(object sparse_tensor): + return isinstance(sparse_tensor, SparseCOOTensor) -cdef api shared_ptr[CSparseTensorCOO] pyarrow_unwrap_sparse_tensor_coo( +cdef api shared_ptr[CSparseCOOTensor] pyarrow_unwrap_sparse_coo_tensor( object sparse_tensor): - cdef SparseTensorCOO sten - if pyarrow_is_sparse_tensor_coo(sparse_tensor): - sten = (sparse_tensor) + cdef SparseCOOTensor sten + if pyarrow_is_sparse_coo_tensor(sparse_tensor): + sten = (sparse_tensor) return sten.sp_sparse_tensor - return shared_ptr[CSparseTensorCOO]() + return shared_ptr[CSparseCOOTensor]() -cdef api object pyarrow_wrap_sparse_tensor_coo( - const shared_ptr[CSparseTensorCOO]& sp_sparse_tensor): +cdef api object pyarrow_wrap_sparse_coo_tensor( + const shared_ptr[CSparseCOOTensor]& sp_sparse_tensor): if sp_sparse_tensor.get() == NULL: - raise ValueError('SparseTensorCOO was NULL') + raise ValueError('SparseCOOTensor was NULL') - cdef SparseTensorCOO sparse_tensor = SparseTensorCOO.__new__( - SparseTensorCOO) + cdef SparseCOOTensor sparse_tensor = SparseCOOTensor.__new__( + SparseCOOTensor) sparse_tensor.init(sp_sparse_tensor) return sparse_tensor -cdef api bint pyarrow_is_sparse_tensor_csr(object sparse_tensor): - return isinstance(sparse_tensor, SparseTensorCSR) +cdef api bint pyarrow_is_sparse_csr_matrix(object sparse_tensor): + return isinstance(sparse_tensor, SparseCSRMatrix) -cdef api shared_ptr[CSparseTensorCSR] pyarrow_unwrap_sparse_tensor_csr( +cdef api shared_ptr[CSparseCSRMatrix] pyarrow_unwrap_sparse_csr_matrix( object sparse_tensor): - cdef SparseTensorCSR sten - if pyarrow_is_sparse_tensor_csr(sparse_tensor): - sten = (sparse_tensor) + cdef SparseCSRMatrix sten + if pyarrow_is_sparse_csr_matrix(sparse_tensor): + sten = (sparse_tensor) return sten.sp_sparse_tensor - return shared_ptr[CSparseTensorCSR]() + return shared_ptr[CSparseCSRMatrix]() -cdef api object pyarrow_wrap_sparse_tensor_csr( - const shared_ptr[CSparseTensorCSR]& sp_sparse_tensor): +cdef api object pyarrow_wrap_sparse_csr_matrix( + const shared_ptr[CSparseCSRMatrix]& sp_sparse_tensor): if sp_sparse_tensor.get() == NULL: - raise ValueError('SparseTensorCSR was NULL') + raise ValueError('SparseCSRMatrix was NULL') - cdef SparseTensorCSR sparse_tensor = SparseTensorCSR.__new__( - SparseTensorCSR) + cdef SparseCSRMatrix sparse_tensor = SparseCSRMatrix.__new__( + SparseCSRMatrix) sparse_tensor.init(sp_sparse_tensor) return sparse_tensor diff --git a/python/pyarrow/tensor.pxi b/python/pyarrow/tensor.pxi index 17554e61740..fb2c3c0f852 100644 --- a/python/pyarrow/tensor.pxi +++ b/python/pyarrow/tensor.pxi @@ -126,39 +126,39 @@ strides: {0.strides}""".format(self) buffer.suboffsets = NULL -cdef class SparseTensorCOO: +cdef class SparseCOOTensor: """ A sparse COO tensor. """ def __init__(self): - raise TypeError("Do not call SparseTensorCOO's constructor directly, " - "use one of the `pyarrow.SparseTensorCOO.from_*` " + raise TypeError("Do not call SparseCOOTensor's constructor directly, " + "use one of the `pyarrow.SparseCOOTensor.from_*` " "functions instead.") - cdef void init(self, const shared_ptr[CSparseTensorCOO]& sp_sparse_tensor): + cdef void init(self, const shared_ptr[CSparseCOOTensor]& sp_sparse_tensor): self.sp_sparse_tensor = sp_sparse_tensor self.stp = sp_sparse_tensor.get() self.type = pyarrow_wrap_data_type(self.stp.type()) def __repr__(self): - return """ + return """ type: {0.type} shape: {0.shape}""".format(self) @classmethod def from_dense_numpy(cls, obj, dim_names=None): """ - Convert numpy.ndarray to arrow::SparseTensorCOO + Convert numpy.ndarray to arrow::SparseCOOTensor """ return cls.from_tensor(Tensor.from_numpy(obj, dim_names=dim_names)) @staticmethod def from_numpy(data, coords, shape, dim_names=None): """ - Create arrow::SparseTensorCOO from numpy.ndarrays + Create arrow::SparseCOOTensor from numpy.ndarrays """ - cdef shared_ptr[CSparseTensorCOO] csparse_tensor + cdef shared_ptr[CSparseCOOTensor] csparse_tensor cdef vector[int64_t] c_shape cdef vector[c_string] c_dim_names @@ -168,48 +168,48 @@ shape: {0.shape}""".format(self) for x in dim_names: c_dim_names.push_back(tobytes(x)) - # Enforce precondition for SparseTensorCOO indices + # Enforce precondition for SparseCOOTensor indices coords = np.require(coords, dtype='i8', requirements='F') if coords.ndim != 2: raise ValueError("Expected 2-dimensional array for " - "SparseTensorCOO indices") + "SparseCOOTensor indices") - check_status(NdarraysToSparseTensorCOO(c_default_memory_pool(), + check_status(NdarraysToSparseCOOTensor(c_default_memory_pool(), data, coords, c_shape, c_dim_names, &csparse_tensor)) - return pyarrow_wrap_sparse_tensor_coo(csparse_tensor) + return pyarrow_wrap_sparse_coo_tensor(csparse_tensor) @staticmethod def from_tensor(obj): """ - Convert arrow::Tensor to arrow::SparseTensorCOO + Convert arrow::Tensor to arrow::SparseCOOTensor """ - cdef shared_ptr[CSparseTensorCOO] csparse_tensor + cdef shared_ptr[CSparseCOOTensor] csparse_tensor cdef shared_ptr[CTensor] ctensor = pyarrow_unwrap_tensor(obj) with nogil: - check_status(TensorToSparseTensorCOO(ctensor, &csparse_tensor)) + check_status(TensorToSparseCOOTensor(ctensor, &csparse_tensor)) - return pyarrow_wrap_sparse_tensor_coo(csparse_tensor) + return pyarrow_wrap_sparse_coo_tensor(csparse_tensor) def to_numpy(self): """ - Convert arrow::SparseTensorCOO to numpy.ndarrays with zero copy + Convert arrow::SparseCOOTensor to numpy.ndarrays with zero copy """ cdef PyObject* out_data cdef PyObject* out_coords - check_status(SparseTensorCOOToNdarray(self.sp_sparse_tensor, self, + check_status(SparseCOOTensorToNdarray(self.sp_sparse_tensor, self, &out_data, &out_coords)) return PyObject_to_object(out_data), PyObject_to_object(out_coords) - def equals(self, SparseTensorCOO other): + def equals(self, SparseCOOTensor other): """ Return true if sparse tensors contains exactly equal data """ return self.stp.Equals(deref(other.stp)) def __eq__(self, other): - if isinstance(other, SparseTensorCOO): + if isinstance(other, SparseCOOTensor): return self.equals(other) else: return NotImplemented @@ -243,39 +243,39 @@ shape: {0.shape}""".format(self) return self.stp.non_zero_length() -cdef class SparseTensorCSR: +cdef class SparseCSRMatrix: """ A sparse CSR tensor. """ def __init__(self): - raise TypeError("Do not call SparseTensorCSR's constructor directly, " - "use one of the `pyarrow.SparseTensorCSR.from_*` " + raise TypeError("Do not call SparseCSRMatrix's constructor directly, " + "use one of the `pyarrow.SparseCSRMatrix.from_*` " "functions instead.") - cdef void init(self, const shared_ptr[CSparseTensorCSR]& sp_sparse_tensor): + cdef void init(self, const shared_ptr[CSparseCSRMatrix]& sp_sparse_tensor): self.sp_sparse_tensor = sp_sparse_tensor self.stp = sp_sparse_tensor.get() self.type = pyarrow_wrap_data_type(self.stp.type()) def __repr__(self): - return """ + return """ type: {0.type} shape: {0.shape}""".format(self) @classmethod def from_dense_numpy(cls, obj, dim_names=None): """ - Convert numpy.ndarray to arrow::SparseTensorCSR + Convert numpy.ndarray to arrow::SparseCSRMatrix """ return cls.from_tensor(Tensor.from_numpy(obj, dim_names=dim_names)) @staticmethod def from_numpy(data, indptr, indices, shape, dim_names=None): """ - Create arrow::SparseTensorCSR from numpy.ndarrays + Create arrow::SparseCSRMatrix from numpy.ndarrays """ - cdef shared_ptr[CSparseTensorCSR] csparse_tensor + cdef shared_ptr[CSparseCSRMatrix] csparse_tensor cdef vector[int64_t] c_shape cdef vector[c_string] c_dim_names @@ -285,55 +285,55 @@ shape: {0.shape}""".format(self) for x in dim_names: c_dim_names.push_back(tobytes(x)) - # Enforce precondition for SparseTensorCSR indices + # Enforce precondition for SparseCSRMatrix indices indptr = np.require(indptr, dtype='i8') indices = np.require(indices, dtype='i8') if indptr.ndim != 1: raise ValueError("Expected 1-dimensional array for " - "SparseTensorCSR indptr") + "SparseCSRMatrix indptr") if indices.ndim != 1: raise ValueError("Expected 1-dimensional array for " - "SparseTensorCSR indices") + "SparseCSRMatrix indices") - check_status(NdarraysToSparseTensorCSR(c_default_memory_pool(), + check_status(NdarraysToSparseCSRMatrix(c_default_memory_pool(), data, indptr, indices, c_shape, c_dim_names, &csparse_tensor)) - return pyarrow_wrap_sparse_tensor_csr(csparse_tensor) + return pyarrow_wrap_sparse_csr_matrix(csparse_tensor) @staticmethod def from_tensor(obj): """ - Convert arrow::Tensor to arrow::SparseTensorCSR + Convert arrow::Tensor to arrow::SparseCSRMatrix """ - cdef shared_ptr[CSparseTensorCSR] csparse_tensor + cdef shared_ptr[CSparseCSRMatrix] csparse_tensor cdef shared_ptr[CTensor] ctensor = pyarrow_unwrap_tensor(obj) with nogil: - check_status(TensorToSparseTensorCSR(ctensor, &csparse_tensor)) + check_status(TensorToSparseCSRMatrix(ctensor, &csparse_tensor)) - return pyarrow_wrap_sparse_tensor_csr(csparse_tensor) + return pyarrow_wrap_sparse_csr_matrix(csparse_tensor) def to_numpy(self): """ - Convert arrow::SparseTensorCSR to numpy.ndarrays with zero copy + Convert arrow::SparseCSRMatrix to numpy.ndarrays with zero copy """ cdef PyObject* out_data cdef PyObject* out_indptr cdef PyObject* out_indices - check_status(SparseTensorCSRToNdarray(self.sp_sparse_tensor, self, + check_status(SparseCSRMatrixToNdarray(self.sp_sparse_tensor, self, &out_data, &out_indptr, &out_indices)) return (PyObject_to_object(out_data), PyObject_to_object(out_indptr), PyObject_to_object(out_indices)) - def equals(self, SparseTensorCSR other): + def equals(self, SparseCSRMatrix other): """ Return true if sparse tensors contains exactly equal data """ return self.stp.Equals(deref(other.stp)) def __eq__(self, other): - if isinstance(other, SparseTensorCSR): + if isinstance(other, SparseCSRMatrix): return self.equals(other) else: return NotImplemented diff --git a/python/pyarrow/tests/test_sparse_tensor.py b/python/pyarrow/tests/test_sparse_tensor.py index 68564dacf4b..225bbbf56dc 100644 --- a/python/pyarrow/tests/test_sparse_tensor.py +++ b/python/pyarrow/tests/test_sparse_tensor.py @@ -38,8 +38,8 @@ @pytest.mark.parametrize('sparse_tensor_type', [ - pa.SparseTensorCSR, - pa.SparseTensorCOO, + pa.SparseCSRMatrix, + pa.SparseCOOTensor, ]) def test_sparse_tensor_attrs(sparse_tensor_type): data = np.array([ @@ -68,7 +68,7 @@ def test_sparse_tensor_coo_base_object(): [0, 7, 0, 0], [0, 0, 0, 0], [0, 0, 0, 5]]) - sparse_tensor = pa.SparseTensorCOO.from_dense_numpy(array) + sparse_tensor = pa.SparseCOOTensor.from_dense_numpy(array) n = sys.getrefcount(sparse_tensor) result_data, result_coords = sparse_tensor.to_numpy() assert sys.getrefcount(sparse_tensor) == n + 2 @@ -87,7 +87,7 @@ def test_sparse_tensor_csr_base_object(): [0, 0, 3], [4, 5, 6]]) - sparse_tensor = pa.SparseTensorCSR.from_dense_numpy(array) + sparse_tensor = pa.SparseCSRMatrix.from_dense_numpy(array) n = sys.getrefcount(sparse_tensor) result_data, result_indptr, result_indices = sparse_tensor.to_numpy() assert sys.getrefcount(sparse_tensor) == n + 3 @@ -99,8 +99,8 @@ def test_sparse_tensor_csr_base_object(): @pytest.mark.parametrize('sparse_tensor_type', [ - pa.SparseTensorCSR, - pa.SparseTensorCOO, + pa.SparseCSRMatrix, + pa.SparseCOOTensor, ]) def test_sparse_tensor_equals(sparse_tensor_type): def eq(a, b): @@ -137,7 +137,7 @@ def test_sparse_tensor_coo_from_dense(dtype_str, arrow_type): tensor = pa.Tensor.from_numpy(array) # Test from numpy array - sparse_tensor = pa.SparseTensorCOO.from_dense_numpy(array) + sparse_tensor = pa.SparseCOOTensor.from_dense_numpy(array) repr(sparse_tensor) assert sparse_tensor.type == arrow_type result_data, result_coords = sparse_tensor.to_numpy() @@ -145,7 +145,7 @@ def test_sparse_tensor_coo_from_dense(dtype_str, arrow_type): assert np.array_equal(coords, result_coords) # Test from Tensor - sparse_tensor = pa.SparseTensorCOO.from_tensor(tensor) + sparse_tensor = pa.SparseCOOTensor.from_tensor(tensor) repr(sparse_tensor) assert sparse_tensor.type == arrow_type result_data, result_coords = sparse_tensor.to_numpy() @@ -166,7 +166,7 @@ def test_sparse_tensor_csr_from_dense(dtype_str, arrow_type): tensor = pa.Tensor.from_numpy(dense_data) # Test from numpy array - sparse_tensor = pa.SparseTensorCSR.from_dense_numpy(dense_data) + sparse_tensor = pa.SparseCSRMatrix.from_dense_numpy(dense_data) repr(sparse_tensor) result_data, result_indptr, result_indices = sparse_tensor.to_numpy() assert np.array_equal(data, result_data) @@ -174,7 +174,7 @@ def test_sparse_tensor_csr_from_dense(dtype_str, arrow_type): assert np.array_equal(indices, result_indices) # Test from Tensor - sparse_tensor = pa.SparseTensorCSR.from_tensor(tensor) + sparse_tensor = pa.SparseCSRMatrix.from_tensor(tensor) repr(sparse_tensor) assert sparse_tensor.type == arrow_type result_data, result_indptr, result_indices = sparse_tensor.to_numpy() @@ -191,7 +191,7 @@ def test_sparse_tensor_coo_numpy_roundtrip(dtype_str, arrow_type): shape = (4, 4) dim_names = ["x", "y"] - sparse_tensor = pa.SparseTensorCOO.from_numpy(data, coords, shape, + sparse_tensor = pa.SparseCOOTensor.from_numpy(data, coords, shape, dim_names) repr(sparse_tensor) assert sparse_tensor.type == arrow_type @@ -210,7 +210,7 @@ def test_sparse_tensor_csr_numpy_roundtrip(dtype_str, arrow_type): shape = (3, 3) dim_names = ["x", "y"] - sparse_tensor = pa.SparseTensorCSR.from_numpy(data, indptr, indices, + sparse_tensor = pa.SparseCSRMatrix.from_numpy(data, indptr, indices, shape, dim_names) repr(sparse_tensor) assert sparse_tensor.type == arrow_type