Skip to content
Closed
Show file tree
Hide file tree
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
36 changes: 19 additions & 17 deletions src/pycall/AwkwardPyCall.jl
Original file line number Diff line number Diff line change
@@ -1,39 +1,41 @@
module AwkwardPyCall
using PyCall
using JSON
using AwkwardArray

function _as_numpy(array::AbstractVector{UInt8})
py_array = PyObject(array)
pyimport("numpy").asarray(py_array, dtype = pyimport("numpy").uint8)
if !isdefined(Main, :PyCall)
println("PyCall is not installed. Installing...")

import Pkg

Pkg.add("PyCall")
end

using PyCall
using AwkwardArray

function julia_array_to_python(array)
form, len, containers = AwkwardArray.to_buffers(array)
function convert(layout::AwkwardArray.Content)::PyObject
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(PyObject(buffer), dtype = pyimport("numpy").uint8)
end

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

function _as_julia(py_buffer)
uint8_buffer = reinterpret(UInt8, py_buffer)
return uint8_buffer
pyimport("awkward").from_buffers(form, len, py_buffers)
end

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

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(form.to_json(), len, julia_buffers)
AwkwardArray.from_buffers(form.to_json(), len, julia_buffers)
end

end # module
20 changes: 9 additions & 11 deletions test/runpytests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,34 +22,32 @@ using JSON

include("../src/pycall/AwkwardPyCall.jl")

import Main.AwkwardPyCall: julia_array_to_python
import Main.AwkwardPyCall: 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]),
)
# Test case 1: Check if the function returns an awkward array
@test isa(julia_array_to_python(array), PyObject)
@test isa(convert(array), PyObject)

# Test case 2: Check if the awkward array has the correct layout
py_array = julia_array_to_python(array)
py_array = convert(array)
@test typeof(py_array) == PyObject
@test pyimport("awkward").to_list(py_array) == [[1.1, 2.2, 3.3], [], [4.4, 5.5]]


end

import Main.AwkwardPyCall: 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]])

# Test case 1: Check if the function returns an awkward array
@test isa(
python_array_to_julia(py_array),
convert(py_array),
AwkwardArray.ListOffsetArray{
SubArray{Int64,1,Vector{Int64},Tuple{UnitRange{Int64}},true},
AwkwardArray.PrimitiveArray{
Expand All @@ -62,7 +60,7 @@ import Main.AwkwardPyCall: python_array_to_julia
)

# Test case 2: Check if the awkward array has the correct layout
array = python_array_to_julia(py_array)
array = convert(py_array)
@test typeof(array) == AwkwardArray.ListOffsetArray{
SubArray{Int64,1,Vector{Int64},Tuple{UnitRange{Int64}},true},
AwkwardArray.PrimitiveArray{
Expand Down