-
Notifications
You must be signed in to change notification settings - Fork 188
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
RFC: Reverse Array dims in no-copy conversion from Julia to Numpy #85
Changes from all commits
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 |
---|---|---|
|
@@ -175,14 +175,20 @@ const npy_typestrs = Dict( "b1"=>Bool, | |
######################################################################### | ||
# no-copy conversion of Julia arrays to NumPy arrays. | ||
|
||
function PyObject{T<:NPY_TYPES}(a::StridedArray{T}) | ||
# In some cases, an array is already in row-major order (e.g., RGB | ||
# images) and it is useful to reverse the order of dimensions passed | ||
# to PyArray_New. In these cases, set revdims=true. | ||
|
||
function PyObject{T<:NPY_TYPES}(a::StridedArray{T}, revdims::Bool=false) | ||
try | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should not silently fall back on failure to array2py(a) if revdims=true. It produces inconsistent behavior. Is there some way array2py could take revdims as an extra arg as well? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch. For now, it can throw an error if |
||
@npyinitialize | ||
size_a = revdims ? reverse(size(a)) : size(a) | ||
strides_a = revdims ? reverse(strides(a)) : strides(a) | ||
p = @pycheck ccall(npy_api[:PyArray_New], PyPtr, | ||
(PyPtr,Cint,Ptr{Int},Cint, Ptr{Int},Ptr{T}, Cint,Cint,PyPtr), | ||
npy_api[:PyArray_Type], | ||
ndims(a), Int[size(a)...], npy_type(T), | ||
Int[strides(a)...] * sizeof(eltype(a)), a, sizeof(eltype(a)), | ||
ndims(a), Int[size_a...], npy_type(T), | ||
Int[strides_a...] * sizeof(eltype(a)), a, sizeof(eltype(a)), | ||
NPY_ARRAY_ALIGNED | NPY_ARRAY_WRITEABLE, | ||
C_NULL) | ||
return PyObject(p, a) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would say rather than "Julia arrays are in column-major order, but in some cases it is useful to pass them to Python as row-major arrays simply by reversing the dimensions. For example, although NumPy works with both row-major and column-major data, some Python libraries like OpenCV seem to require row-major data (the default in NumPy). In such cases, use
PyObject(array, true)
."And then add a similar comment to the README.md file.