-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Julia helper functions using PyCall (#42)
* create julia_helpers.jl * a helper to convert Julia Awkward array to Python * type lock to a PrimitiveArray * fix: use np.asarray * convert only vectors of uint8 * use np.asarray instead * check if PyCall is installed * check PyCall installation * check for PyCall in Main? * move the import in * Update and rename julia_helpers.jl to AwkwardPyCall.jl * try reexport AwkwardArray * add end of module * feat: add python to julia functions * fix: add JSON * fix: check if PyCall is installed * Update AwkwardPyCall.jl * test: add tests * test: add more tests * fix: add unreleased AwkwardArray package for testing * fix: add JSON for tests * fix: reorder * test: do not install python * cleanup: remove python installation * fix: format using JuliaFormatter and add coverage for tests
- Loading branch information
Showing
5 changed files
with
144 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
module AwkwardPyCall | ||
|
||
using PyCall | ||
using JSON | ||
using AwkwardArray | ||
|
||
const ak = pyimport("awkward") | ||
const np = pyimport("numpy") | ||
|
||
function _as_numpy(array::AbstractVector{UInt8}) | ||
py_array = PyObject(array) | ||
np.asarray(py_array, dtype = np.uint8) | ||
end | ||
|
||
function julia_array_to_python(array) | ||
form, len, containers = AwkwardArray.to_buffers(array) | ||
|
||
py_buffers = Dict{String,Any}() | ||
|
||
for (key, buffer) in containers | ||
py_buffers[key] = _as_numpy(buffer) | ||
end | ||
|
||
return ak.from_buffers(form, len, py_buffers) | ||
end | ||
|
||
function _as_julia(py_buffer) | ||
uint8_buffer = reinterpret(UInt8, py_buffer) | ||
return uint8_buffer | ||
end | ||
|
||
function python_array_to_julia(py_array) | ||
form, len, containers = ak.to_buffers(py_array) | ||
|
||
julia_buffers = Dict{String,AbstractVector{UInt8}}() | ||
for (key, buffer) in containers | ||
julia_buffers[key] = _as_julia(buffer) | ||
end | ||
|
||
return AwkwardArray.from_buffers(form.to_json(), len, julia_buffers) | ||
end | ||
|
||
end # module |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
using PyCall | ||
using Test | ||
|
||
using Pkg | ||
|
||
# FIXME: remove after AwkwardArray is released | ||
function add_unreleased_AwkwardArray_package() | ||
# Specify the Git URL of the AwkwardArray package | ||
git_url = "https://github.com/JuliaHEP/AwkwardArray.jl" | ||
|
||
# Use Pkg to add the package | ||
Pkg.add(PackageSpec(url = git_url)) | ||
end | ||
|
||
# Call the function to add the unreleased AwkwardArray package | ||
add_unreleased_AwkwardArray_package() | ||
|
||
using AwkwardArray | ||
|
||
Pkg.add("JSON") | ||
using JSON | ||
|
||
include("../src/pycall/AwkwardPyCall.jl") | ||
|
||
import Main.AwkwardPyCall: ak | ||
|
||
import Main.AwkwardPyCall: julia_array_to_python | ||
|
||
# Test julia_array_to_python function | ||
@testset "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 case 2: Check if the awkward array has the correct layout | ||
py_array = julia_array_to_python(array) | ||
@test typeof(py_array) == PyObject | ||
@test ak.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 | ||
py_array = ak.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), | ||
AwkwardArray.ListOffsetArray{ | ||
SubArray{Int64,1,Vector{Int64},Tuple{UnitRange{Int64}},true}, | ||
AwkwardArray.PrimitiveArray{ | ||
Float64, | ||
SubArray{Float64,1,Vector{Float64},Tuple{UnitRange{Int64}},true}, | ||
:default, | ||
}, | ||
:default, | ||
}, | ||
) | ||
|
||
# Test case 2: Check if the awkward array has the correct layout | ||
array = python_array_to_julia(py_array) | ||
@test typeof(array) == AwkwardArray.ListOffsetArray{ | ||
SubArray{Int64,1,Vector{Int64},Tuple{UnitRange{Int64}},true}, | ||
AwkwardArray.PrimitiveArray{ | ||
Float64, | ||
SubArray{Float64,1,Vector{Float64},Tuple{UnitRange{Int64}},true}, | ||
:default, | ||
}, | ||
:default, | ||
} | ||
@test array == [[1.1, 2.2, 3.3], [], [4.4, 5.5]] | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters