Skip to content
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

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/numpy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

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.

# 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
Copy link

Choose a reason for hiding this comment

The 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?

Copy link
Member

@stevengj stevengj Apr 21, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. For now, it can throw an error if revdims=true but in the future array2py could implement this too.

@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)
Expand Down