You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "westonpace (via GitHub)" <gi...@apache.org> on 2023/04/20 14:23:59 UTC

[GitHub] [arrow] westonpace commented on a diff in pull request #14378: ARROW-17933: [C++] SparseCOOTensor raises error when created with zero elements

westonpace commented on code in PR #14378:
URL: https://github.com/apache/arrow/pull/14378#discussion_r1172672469


##########
python/pyarrow/tests/test_sparse_tensor.py:
##########
@@ -434,6 +433,23 @@ def test_sparse_coo_tensor_scipy_roundtrip(dtype_str, arrow_type):
     assert sparse_tensor.has_canonical_format
     assert out_scipy_matrix.has_canonical_format
 
+    scipy_matrix = coo_matrix([[0, 0], [0, 0]])
+    sparse_tensor = pa.SparseCOOTensor.from_scipy(scipy_matrix,
+                                                  dim_names=dim_names)
+    out_scipy_matrix = sparse_tensor.to_scipy()

Review Comment:
   Can we verify the shape of `sparse_tensor` here?  It should be 2 x 2 correct?



##########
cpp/src/arrow/sparse_tensor.cc:
##########
@@ -134,6 +134,10 @@ inline Status CheckSparseCOOIndexValidity(const std::shared_ptr<DataType>& type,
 
   RETURN_NOT_OK(internal::CheckSparseIndexMaximumValue(type, shape));
 
+  // Indexes with no values are considered valid

Review Comment:
   I don't understand what is happening here (I don't understand much about the tensors so not surprising).  Why is it ok if one of the shape elements is zero?  I would expect an empty sparse matrix to still have a shape:
   
   ```
   >>> scipy.sparse.coo_matrix(numpy.zeros((2,4)), dtype=numpy.float32).shape
   (2, 4)
   ```



##########
python/pyarrow/tests/test_sparse_tensor.py:
##########
@@ -434,6 +434,21 @@ def test_sparse_coo_tensor_scipy_roundtrip(dtype_str, arrow_type):
     assert sparse_tensor.has_canonical_format
     assert out_scipy_matrix.has_canonical_format
 
+    scipy_matrix = coo_matrix([[0, 0], [0, 0]])

Review Comment:
   I believe @rok has the correct test here but I could be wrong.  The original issue is about a sparse matrix with a valid non-zero shape but no elements:
   
   ```
   >>> scipy.sparse.coo_matrix(numpy.zeros((2,4)), dtype=numpy.float32)
   <2x4 sparse matrix of type '<class 'numpy.float32'>'
   	with 0 stored elements in COOrdinate format>
   >>> numpy.zeros((2,4))
   array([[0., 0., 0., 0.],
          [0., 0., 0., 0.]])
   >>> numpy.zeros((2,4)).shape
   (2, 4)
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org