rok commented on code in PR #38008:
URL: https://github.com/apache/arrow/pull/38008#discussion_r1409363670
##########
python/pyarrow/tests/test_extension_type.py:
##########
@@ -1353,6 +1386,70 @@ def test_tensor_class_methods():
arr.to_numpy_ndarray()
[email protected]("value_type", (np.int8, np.int32, np.int64,
np.float64))
+def test_variable_shape_tensor_class_methods(value_type):
+ ndim = 2
+ shape_type = pa.list_(pa.uint32(), ndim)
+ arrow_type = pa.from_numpy_dtype(value_type)
+ tensor_type = pa.variable_shape_tensor(
+ arrow_type,
+ ndim,
+ dim_names=["H", "W"],
+ permutation=[0, 1],
+ uniform_shape=[None, None],
+ )
+ fields = [pa.field("shape", shape_type), pa.field("data",
pa.list_(arrow_type))]
+
+ shapes = pa.array([[2, 3], [2, 1]], shape_type)
+ values = pa.array([[1, 2, 3, 4, 5, 6], [7, 8]], pa.list_(arrow_type))
+ struct_arr = pa.StructArray.from_arrays([shapes, values], fields=fields)
+ arr = pa.ExtensionArray.from_storage(tensor_type, struct_arr)
+ basic_arr = pa.ExtensionArray.from_storage(
+ pa.variable_shape_tensor(arrow_type, ndim), struct_arr
+ )
+
+ storage = pa.array(
+ [([2, 3], [1, 2, 3, 4, 5, 6]), ([2, 1], [7, 8])],
type=pa.struct(fields)
+ )
+ assert pa.ExtensionArray.from_storage(tensor_type, storage).equals(arr)
+
+ assert arr.type == tensor_type
+
+ ndarray_list = [
+ np.array([[1, 2, 3], [4, 5, 6]], dtype=value_type),
+ np.array([[7], [8]], dtype=value_type),
+ ]
+ assert all(zip(x == y for x, y in zip(arr.to_numpy_ndarray(),
ndarray_list)))
Review Comment:
```python
> assert arr.to_numpy_ndarray() == ndarray_list
E ValueError: The truth value of an array with more than one element
is ambiguous. Use a.any() or a.all()
```
##########
python/pyarrow/tests/test_extension_type.py:
##########
@@ -1353,6 +1386,70 @@ def test_tensor_class_methods():
arr.to_numpy_ndarray()
[email protected]("value_type", (np.int8, np.int32, np.int64,
np.float64))
+def test_variable_shape_tensor_class_methods(value_type):
+ ndim = 2
+ shape_type = pa.list_(pa.uint32(), ndim)
+ arrow_type = pa.from_numpy_dtype(value_type)
+ tensor_type = pa.variable_shape_tensor(
+ arrow_type,
+ ndim,
+ dim_names=["H", "W"],
+ permutation=[0, 1],
+ uniform_shape=[None, None],
+ )
+ fields = [pa.field("shape", shape_type), pa.field("data",
pa.list_(arrow_type))]
+
+ shapes = pa.array([[2, 3], [2, 1]], shape_type)
+ values = pa.array([[1, 2, 3, 4, 5, 6], [7, 8]], pa.list_(arrow_type))
+ struct_arr = pa.StructArray.from_arrays([shapes, values], fields=fields)
+ arr = pa.ExtensionArray.from_storage(tensor_type, struct_arr)
+ basic_arr = pa.ExtensionArray.from_storage(
+ pa.variable_shape_tensor(arrow_type, ndim), struct_arr
+ )
+
+ storage = pa.array(
+ [([2, 3], [1, 2, 3, 4, 5, 6]), ([2, 1], [7, 8])],
type=pa.struct(fields)
+ )
+ assert pa.ExtensionArray.from_storage(tensor_type, storage).equals(arr)
+
+ assert arr.type == tensor_type
+
+ ndarray_list = [
+ np.array([[1, 2, 3], [4, 5, 6]], dtype=value_type),
+ np.array([[7], [8]], dtype=value_type),
+ ]
+ assert all(zip(x == y for x, y in zip(arr.to_numpy_ndarray(),
ndarray_list)))
+
+ assert pa.VariableShapeTensorArray.from_numpy_ndarray(ndarray_list).equals(
+ basic_arr
+ )
+ assert pa.VariableShapeTensorArray.from_numpy_ndarray(
+ arr.to_numpy_ndarray()
+ ).equals(basic_arr)
+
+ assert arr.to_pylist() == [
+ {"data": [1, 2, 3, 4, 5, 6], "shape": [2, 3]},
+ {"data": [7, 8], "shape": [2, 1]},
+ ]
+
+ expected_0 = np.array([[1, 2, 3], [4, 5, 6]], dtype=value_type)
+ expected_1 = np.array([[7], [8]], dtype=value_type)
+
+ np.testing.assert_array_equal(arr[0].to_tensor().to_numpy(), expected_0)
+ np.testing.assert_array_equal(arr[1].to_tensor().to_numpy(), expected_1)
+
+ np.testing.assert_array_equal(arr[0].to_numpy_ndarray(), expected_0)
+ np.testing.assert_array_equal(arr[1].to_numpy_ndarray(), expected_1)
+
+ assert arr[0].to_tensor().equals(
+ pa.Tensor.from_numpy(expected_0, dim_names=["H", "W"]))
+
+ # TODO: due to wrong offset this would return [[1], [2]] instead of [[7],
[8]]
Review Comment:
Removed.
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]