`ak.array` gives unexpected results on a transposed numpy multi-dimensional array.
Closed this issue · 2 comments
ajpotts commented
Describe the bug
ak.array
gives unexpected results on a transposed numpy multi-dimensional array.
To Reproduce
In [81]:
...: import numpy as np
...: rows = 5
...: cols = 5
...: nda = np.random.randint(1, 10, (rows, cols))
...: nda
Out[81]:
array([[7, 7, 7, 8, 6],
[9, 4, 9, 6, 2],
[2, 2, 3, 1, 9],
[2, 7, 1, 3, 7],
[4, 2, 7, 5, 1]])
In [82]:
...: nda_to_ak = ak.array(nda)
...: nda_to_ak
...:
Out[82]: array([array([7 7 7 8 6]) array([9 4 9 6 2]) array([2 2 3 1 9]) array([2 7 1 3 7]) array([4 2 7 5 1])])
In [83]:
...: npa = np.transpose(nda)
...: npa
Out[83]:
array([[7, 9, 2, 2, 4],
[7, 4, 2, 7, 2],
[7, 9, 3, 1, 7],
[8, 6, 1, 3, 5],
[6, 2, 9, 7, 1]])
In [84]:
...: npa_to_ak = ak.array(npa)
...: npa_to_ak
...:
...:
Out[84]: array([array([7 7 7 8 6]) array([9 4 9 6 2]) array([2 2 3 1 9]) array([2 7 1 3 7]) array([4 2 7 5 1])])
ajpotts commented
This is interesting:
In [32]: ak.array(nda_transpose)
Out[32]: array([array([1 1 3 6 7]) array([4 6 6 8 1]) array([3 7 2 4 8]) array([5 6 5 9 1]) array([3 2 7 9 6])])
In [33]: ak.array(nda_transpose.copy())
Out[33]: array([array([1 4 3 5 3]) array([1 6 7 6 2]) array([3 6 2 5 7]) array([6 8 4 9 9]) array([7 1 8 1 6])])
In [34]: nda_transpose
Out[34]:
array([[1, 4, 3, 5, 3],
[1, 6, 7, 6, 2],
[3, 6, 2, 5, 7],
[6, 8, 4, 9, 9],
[7, 1, 8, 1, 6]])
stress-tess commented
Dropped a comment on the PR. The reason for this is that for numpy the transpose is being treated as a different view of the same data (switches between column and row major), but the underlying data hasn't changed. We send the underlying data over as a buffer and ignore the order. When we do a copy, it actually moves the data elsewhere
This comment of the PR explains better and looks at the flags of the array and transpose:
#3761 (comment)