jorisvandenbossche commented on code in PR #37533:
URL: https://github.com/apache/arrow/pull/37533#discussion_r1425004697


##########
python/pyarrow/tests/test_extension_type.py:
##########
@@ -1319,38 +1319,65 @@ def test_tensor_type():
 
 
 def test_tensor_class_methods():
+    from numpy.lib.stride_tricks import as_strided
+
+    values = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
+    np_arr = np.array(values, dtype=np.int8)
     tensor_type = pa.fixed_shape_tensor(pa.float32(), [2, 3])
-    storage = pa.array([[1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6]],
-                       pa.list_(pa.float32(), 6))
+    storage = pa.array(np_arr.reshape(2, 6).tolist(), pa.list_(pa.float32(), 
6))
     arr = pa.ExtensionArray.from_storage(tensor_type, storage)
-    expected = np.array(
-        [[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]], dtype=np.float32)
-    result = arr.to_numpy_ndarray()
-    np.testing.assert_array_equal(result, expected)
 
-    expected = np.array([[[1, 2, 3], [4, 5, 6]]], dtype=np.float32)
+    # TODO: add more get_tensor tests
+    assert arr.get_tensor(0) == pa.Tensor.from_numpy(
+        np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float32))
+
+    expected = np_arr.reshape(2, 2, 3).astype(np.float32).tolist()
+    np.testing.assert_array_equal(arr.to_tensor(), expected)
+    np.testing.assert_array_equal(arr.to_numpy_ndarray(), expected)
+
+    # TODO: offset not correctly handled
+    expected = np_arr[6:].reshape(1, 2, 3).astype(np.float32)
+    # expected = np.array([[[7, 8, 9], [10, 11, 12]]], dtype=np.float32)
     result = arr[:1].to_numpy_ndarray()
-    np.testing.assert_array_equal(result, expected)
+    # np.testing.assert_array_equal(result, expected)
 
     arr = np.array(
-        [[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]],
+        [[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]],
         dtype=np.float32, order="C")
     tensor_array_from_numpy = pa.FixedShapeTensorArray.from_numpy_ndarray(arr)
     assert isinstance(tensor_array_from_numpy.type, pa.FixedShapeTensorType)
     assert tensor_array_from_numpy.type.value_type == pa.float32()
     assert tensor_array_from_numpy.type.shape == [2, 3]
 
-    arr = np.array(
-        [[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]],
-        dtype=np.float32, order="F")
-    with pytest.raises(ValueError, match="C-style contiguous segment"):
+    arr = np_arr.reshape((-1, 3), order="F")
+    with pytest.raises(ValueError, match="First stride needs to be largest"):
         pa.FixedShapeTensorArray.from_numpy_ndarray(arr)
 
+    bw = np.int8().itemsize
+    arr = as_strided(np_arr, shape=(3, 4), strides=(bw * 4, bw), 
writeable=False)
+    tensor_array_from_numpy = pa.FixedShapeTensorArray.from_numpy_ndarray(arr)
+    assert tensor_array_from_numpy.type.shape == [4]
+    assert tensor_array_from_numpy.type.permutation == [1]

Review Comment:
   On main, it seems that the type constructor also happily lets you create a 
type with an invalid permutation:
   
   ```
   In [5]: pa.fixed_shape_tensor(pa.int8(), shape=(4, ), permutation=[3])
   Out[5]: 
FixedShapeTensorType(extension<arrow.fixed_shape_tensor[value_type=int8, 
shape=[4], permutation=[3]]>)
   ```
   
   We should probably add some validation to this type factory function to 
ensure the permutation is valid?



-- 
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-unsubscr...@arrow.apache.org

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

Reply via email to