-
Notifications
You must be signed in to change notification settings - Fork 4.1k
GH-34882: [Python] Binding for FixedShapeTensorType #34883
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
d69685a
a6292f8
1bdba1d
7c395b0
d27d48f
8e790b4
64e0cd0
48cbeb3
d9ca165
d3530af
e2ce8ba
ee5d25c
f5a5c0c
52f9e7e
f9dee9e
b171d00
c0ec94c
f2d9fe7
8b5dc93
570f086
3dbbe20
223968a
dd8fd31
1ebb829
b2d0453
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3090,6 +3090,71 @@ cdef class ExtensionArray(Array): | |
| return self.storage.to_numpy(**kwargs) | ||
|
|
||
|
|
||
| class FixedShapeTensorArray(ExtensionArray): | ||
| """ | ||
| Concrete class for fixed shape tensor extension arrays. | ||
|
|
||
| Examples | ||
| -------- | ||
| Define the extension type for tensor array | ||
|
|
||
| >>> import pyarrow as pa | ||
| >>> tensor_type = FixedShapeTensorType(pa.int32(), [2, 2]) | ||
|
|
||
| Create an extension array | ||
|
|
||
| >>> arr = [[1, 2, 3, 4], [10, 20, 30, 40], [100, 200, 300, 400]] | ||
| >>> storage = pa.array(arr, pa.list_(pa.int32(), 4)) | ||
| >>> pa.ExtensionArray.from_storage(tensor_type, storage) | ||
| <pyarrow.lib.FixedShapeTensorArray object at ...> | ||
| [ | ||
| [ | ||
| 1, | ||
| 2, | ||
| 3, | ||
| 4 | ||
| ], | ||
| [ | ||
| 10, | ||
| 20, | ||
| 30, | ||
| 40 | ||
| ], | ||
| [ | ||
| 100, | ||
| 200, | ||
| 300, | ||
| 400 | ||
| ] | ||
| ] | ||
| """ | ||
|
|
||
| def to_numpy_ndarray(self): | ||
| """ | ||
| Convert fixed shape tensor extension array to a numpy array (with dim+1). | ||
| """ | ||
AlenkaF marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| np_flat = np.asarray(self.storage.values) | ||
| numpy_tensor = np_flat.reshape((len(self),) + tuple(self.type.shape), | ||
| order='C') | ||
|
||
|
|
||
| return numpy_tensor | ||
|
|
||
| def from_numpy_ndarray(obj): | ||
AlenkaF marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """ | ||
| Convert numpy tensors (ndarrays) to a fixed shape tensor extension array. | ||
AlenkaF marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """ | ||
| numpy_type = obj.flatten().dtype | ||
| arrow_type = from_numpy_dtype(numpy_type) | ||
AlenkaF marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| shape = obj.shape[1:] | ||
| size = obj.size / obj.shape[0] | ||
|
|
||
| return ExtensionArray.from_storage( | ||
| FixedShapeTensorType(arrow_type, shape), | ||
| array([t.flatten() for t in obj], | ||
| list_(arrow_type, size)) | ||
AlenkaF marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ) | ||
|
|
||
|
|
||
| cdef dict _array_classes = { | ||
| _Type_NA: NullArray, | ||
| _Type_BOOL: BooleanArray, | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.