Skip to content

Commit

Permalink
fix: merge rename to convert branch (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
ianna authored Nov 28, 2023
1 parent 41db204 commit b75414d
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 49 deletions.
31 changes: 13 additions & 18 deletions ext/AwkwardPythonCallExt/AwkwardPythonCallExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,33 @@ using PythonCall
using JSON
import AwkwardArray

function _as_numpy(array::AbstractVector{UInt8})
np = pyimport("numpy")
np.asarray(array, dtype = np.uint8)
end

function AwkwardArray.julia_array_to_python(array)
form, len, containers = AwkwardArray.to_buffers(array)
function AwkwardArray.convert(layout::AwkwardArray.Content)::Py
form, len, containers = AwkwardArray.to_buffers(layout)

py_buffers = Dict{String,Any}()

for (key, buffer) in containers
py_buffers[key] = _as_numpy(buffer)
py_buffers[key] = pyimport("numpy").asarray(buffer, dtype = pyimport("numpy").uint8)
end

return pyimport("awkward").from_buffers(form, len, py_buffers)
pyimport("awkward").from_buffers(form, len, py_buffers)
end

function _as_julia(py_buffer)
uint8_buffer = reinterpret(UInt8, py_buffer)
return uint8_buffer
end

function AwkwardArray.python_array_to_julia(py_array)
form, len, _containers = pyimport("awkward").to_buffers(py_array)
function AwkwardArray.convert(array::Py)::AwkwardArray.Content
form, len, _containers = pyimport("awkward").to_buffers(array)
containers = pyconvert(Dict, _containers)

julia_buffers = Dict{String,AbstractVector{UInt8}}()

for (key, buffer) in containers
julia_buffers[key] = _as_julia(buffer)
julia_buffers[key] = reinterpret(UInt8, buffer)
end

return AwkwardArray.from_buffers(pyconvert(String, form.to_json()), pyconvert(Int, len), julia_buffers)
AwkwardArray.from_buffers(
pyconvert(String, form.to_json()),
pyconvert(Int, len),
julia_buffers,
)
end

end # module
21 changes: 8 additions & 13 deletions ext/AwkwardPythonCallExt/README.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
[PyCall](https://github.com/JuliaPy/PyCall.jl) is currently configured to use the Julia-specific Python distribution
installed by the [Conda.jl](https://github.com/JuliaPy/Conda.jl) package.

conda create --name ak-julia
conda activate ak-julia
conda install -c conda-forge awkward
[PythonCall](https://github.com/JuliaPy/PythonCall.jl) is currently configured to use the Julia-specific Python distribution
installed by the [CondaPkg.jl](https://github.com/JuliaPy/CondaPkg.jl) package.

```julia
using Conda
Conda.add("awkward")
using CondaPkg
CondaPkg.add("numpy")
CondaPkg.add("awkward")
```

```julia
using PyCall
using PythonCall

const ak = pyimport("awkward")

test() = println(ak.__version__)

test()
2.4.6
println(ak.__version__)
2.5.0
```
6 changes: 2 additions & 4 deletions src/AwkwardArray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ import Tables
include("./all_implementations.jl")
include("./tables.jl")

# stub for PyCall Extention
function julia_array_to_python end
function python_array_to_julia end

# stub for PythonCall Extention
function convert end

end # module AwkwardArray
2 changes: 1 addition & 1 deletion src/all_implementations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3606,4 +3606,4 @@ function _to_buffers_index(IndexType::DataType)
else
error("unexpected INDEX type in to_buffers: $IndexType")
end
end
end
23 changes: 11 additions & 12 deletions test/runpytests.jl
Original file line number Diff line number Diff line change
@@ -1,32 +1,31 @@
using PythonCall
using AwkwardArray: julia_array_to_python
using PythonCall
using AwkwardArray: convert

# Test julia_array_to_python function
@testset "julia_array_to_python tests" begin
# Test convert Julia array to Python function
@testset "convert Julia array to Python tests" begin
array = AwkwardArray.ListOffsetArray(
[0, 3, 3, 5],
AwkwardArray.PrimitiveArray([1.1, 2.2, 3.3, 4.4, 5.5]),
)
py_array = convert(array)

# Test case 1: Check if the function returns an awkward array
@test isa(julia_array_to_python(array), Py)
@test py_array isa Py

# Test case 2: Check if the awkward array has the correct layout
py_array = julia_array_to_python(array)
@test typeof(py_array) == Py

ak_array = pyconvert(Vector, pyimport("awkward").to_list(py_array))
@test ak_array == [[1.1, 2.2, 3.3], [], [4.4, 5.5]]


end

using AwkwardArray: python_array_to_julia

# Test python_array_to_julia function
@testset "python_array_to_julia tests" begin
# Test convert Python array to Julia function
@testset "convert Python array to Julia tests" begin
py_array = pyimport("awkward").Array([[1.1, 2.2, 3.3], [], [4.4, 5.5]])

array = python_array_to_julia(py_array)
# Test case 1: Check if the function returns an awkward array
array = convert(py_array)
@test array isa AwkwardArray.ListOffsetArray

@test array == [[1.1, 2.2, 3.3], [], [4.4, 5.5]]
Expand Down
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3170,4 +3170,4 @@ end

end # @testset "AwkwardArray.jl"

include("./runpytests.jl")
include("./runpytests.jl")

0 comments on commit b75414d

Please sign in to comment.